summaryrefslogtreecommitdiff
path: root/cli/js/web/request.ts
blob: a9dcce2de4cb37eb83ffd5c7e0e4c897ff6228aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

import * as body from "./body.ts";
import * as domTypes from "./dom_types.d.ts";
import { ReadableStreamImpl } from "./streams/readable_stream.ts";

function byteUpperCase(s: string): string {
  return String(s).replace(/[a-z]/g, function byteUpperCaseReplace(c): string {
    return c.toUpperCase();
  });
}

function normalizeMethod(m: string): string {
  const u = byteUpperCase(m);
  if (
    u === "DELETE" ||
    u === "GET" ||
    u === "HEAD" ||
    u === "OPTIONS" ||
    u === "POST" ||
    u === "PUT"
  ) {
    return u;
  }
  return m;
}

export class Request extends body.Body implements domTypes.Request {
  public method: string;
  public url: string;
  public credentials?: "omit" | "same-origin" | "include";
  public headers: Headers;

  constructor(input: domTypes.RequestInfo, init?: domTypes.RequestInit) {
    if (arguments.length < 1) {
      throw TypeError("Not enough arguments");
    }

    if (!init) {
      init = {};
    }

    let b: BodyInit;

    // prefer body from init
    if (init.body) {
      b = init.body;
    } else if (input instanceof Request && input._bodySource) {
      if (input.bodyUsed) {
        throw TypeError(body.BodyUsedError);
      }
      b = input._bodySource;
    } else if (typeof input === "object" && "body" in input && input.body) {
      if (input.bodyUsed) {
        throw TypeError(body.BodyUsedError);
      }
      b = input.body;
    } else {
      b = "";
    }

    let headers: Headers;

    // prefer headers from init
    if (init.headers) {
      headers = new Headers(init.headers);
    } else if (input instanceof Request) {
      headers = input.headers;
    } else {
      headers = new Headers();
    }

    const contentType = headers.get("content-type") || "";
    super(b, contentType);
    this.headers = headers;

    // readonly attribute ByteString method;
    this.method = "GET";

    // readonly attribute USVString url;
    this.url = "";

    // readonly attribute RequestCredentials credentials;
    this.credentials = "omit";

    if (input instanceof Request) {
      if (input.bodyUsed) {
        throw TypeError(body.BodyUsedError);
      }
      this.method = input.method;
      this.url = input.url;
      this.headers = new Headers(input.headers);
      this.credentials = input.credentials;
      this._stream = input._stream;
    } else if (typeof input === "string") {
      this.url = input;
    }

    if (init && "method" in init) {
      this.method = normalizeMethod(init.method as string);
    }

    if (
      init &&
      "credentials" in init &&
      init.credentials &&
      ["omit", "same-origin", "include"].indexOf(init.credentials) !== -1
    ) {
      this.credentials = init.credentials;
    }
  }

  public clone(): domTypes.Request {
    if (this.bodyUsed) {
      throw TypeError(body.BodyUsedError);
    }

    const iterators = this.headers.entries();
    const headersList: Array<[string, string]> = [];
    for (const header of iterators) {
      headersList.push(header);
    }

    let body2 = this._bodySource;

    if (this._bodySource instanceof ReadableStreamImpl) {
      const tees = this._bodySource.tee();
      this._stream = this._bodySource = tees[0];
      body2 = tees[1];
    }

    return new Request(this.url, {
      body: body2,
      method: this.method,
      headers: new Headers(headersList),
      credentials: this.credentials,
    });
  }
}