|
2 | 2 |
|
3 | 3 | 'use strict' |
4 | 4 |
|
5 | | -const Headers = require('./headers') |
6 | 5 | const { kHeadersList } = require('../core/symbols') |
| 6 | +const Headers = require('./headers') |
| 7 | +const Response = require('./response') |
7 | 8 | const { Readable } = require('stream') |
8 | | -const { METHODS, STATUS_CODES } = require('http') |
| 9 | +const { METHODS } = require('http') |
9 | 10 | const { |
10 | 11 | InvalidArgumentError, |
11 | 12 | NotSupportedError, |
12 | 13 | RequestAbortedError |
13 | 14 | } = require('../core/errors') |
14 | 15 | const util = require('../core/util') |
15 | 16 | const { addSignal, removeSignal } = require('./abort-signal') |
16 | | -const { Blob } = require('buffer') |
17 | | - |
18 | | -const kType = Symbol('type') |
19 | | -const kStatus = Symbol('status') |
20 | | -const kStatusText = Symbol('status text') |
21 | | -const kUrlList = Symbol('url list') |
22 | | -const kHeaders = Symbol('headers') |
23 | | -const kBody = Symbol('body') |
24 | 17 |
|
25 | 18 | let ReadableStream |
26 | 19 | let TransformStream |
27 | 20 |
|
28 | | -class Response { |
29 | | - constructor ({ |
30 | | - type, |
31 | | - url, |
32 | | - body, |
33 | | - statusCode, |
34 | | - headers, |
35 | | - context |
36 | | - }) { |
37 | | - this[kType] = type || 'default' |
38 | | - this[kStatus] = statusCode || 0 |
39 | | - this[kStatusText] = STATUS_CODES[statusCode] || '' |
40 | | - this[kUrlList] = Array.isArray(url) ? url : (url ? [url] : []) |
41 | | - this[kHeaders] = headers || new Headers() |
42 | | - this[kBody] = body || null |
43 | | - |
44 | | - if (context && context.history) { |
45 | | - this[kUrlList].push(...context.history) |
46 | | - } |
47 | | - } |
48 | | - |
49 | | - get type () { |
50 | | - return this[kType] |
51 | | - } |
52 | | - |
53 | | - get url () { |
54 | | - const length = this[kUrlList].length |
55 | | - return length === 0 ? '' : this[kUrlList][length - 1].toString() |
56 | | - } |
57 | | - |
58 | | - get redirected () { |
59 | | - return this[kUrlList].length > 1 |
60 | | - } |
61 | | - |
62 | | - get status () { |
63 | | - return this[kStatus] |
64 | | - } |
65 | | - |
66 | | - get ok () { |
67 | | - return this[kStatus] >= 200 && this[kStatus] <= 299 |
68 | | - } |
69 | | - |
70 | | - get statusText () { |
71 | | - return this[kStatusText] |
72 | | - } |
73 | | - |
74 | | - get headers () { |
75 | | - return this[kHeaders] |
76 | | - } |
77 | | - |
78 | | - async blob () { |
79 | | - const chunks = [] |
80 | | - if (this.body) { |
81 | | - if (this.bodyUsed || this.body.locked) { |
82 | | - throw new TypeError('unusable') |
83 | | - } |
84 | | - |
85 | | - for await (const chunk of this.body) { |
86 | | - chunks.push(chunk) |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - return new Blob(chunks, { type: this.headers.get('Content-Type') || '' }) |
91 | | - } |
92 | | - |
93 | | - async arrayBuffer () { |
94 | | - const blob = await this.blob() |
95 | | - return await blob.arrayBuffer() |
96 | | - } |
97 | | - |
98 | | - async text () { |
99 | | - const blob = await this.blob() |
100 | | - return await blob.text() |
101 | | - } |
102 | | - |
103 | | - async json () { |
104 | | - return JSON.parse(await this.text()) |
105 | | - } |
106 | | - |
107 | | - async formData () { |
108 | | - // TODO: Implement. |
109 | | - throw new NotSupportedError('formData') |
110 | | - } |
111 | | - |
112 | | - get body () { |
113 | | - return this[kBody] |
114 | | - } |
115 | | - |
116 | | - get bodyUsed () { |
117 | | - return util.isDisturbed(this.body) |
118 | | - } |
119 | | - |
120 | | - clone () { |
121 | | - let body = null |
122 | | - |
123 | | - if (this[kBody]) { |
124 | | - if (util.isDisturbed(this[kBody])) { |
125 | | - throw new TypeError('disturbed') |
126 | | - } |
127 | | - |
128 | | - if (this[kBody].locked) { |
129 | | - throw new TypeError('locked') |
130 | | - } |
131 | | - |
132 | | - // https://fetch.spec.whatwg.org/#concept-body-clone |
133 | | - const [out1, out2] = this[kBody].tee() |
134 | | - |
135 | | - this[kBody] = out1 |
136 | | - body = out2 |
137 | | - } |
138 | | - |
139 | | - return new Response({ |
140 | | - type: this[kType], |
141 | | - statusCode: this[kStatus], |
142 | | - url: this[kUrlList], |
143 | | - headers: this[kHeaders], |
144 | | - body |
145 | | - }) |
146 | | - } |
147 | | -} |
148 | | - |
149 | 21 | class FetchHandler { |
150 | 22 | constructor (opts, callback) { |
151 | 23 | if (!opts || typeof opts !== 'object') { |
|
0 commit comments