@gjsify/fetch 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/lib/cjs/body.js +284 -0
- package/lib/cjs/errors/abort-error.js +28 -0
- package/lib/cjs/errors/base.js +36 -0
- package/lib/cjs/errors/fetch-error.js +40 -0
- package/lib/cjs/headers.js +231 -0
- package/lib/cjs/index.js +246 -0
- package/lib/cjs/request.js +306 -0
- package/lib/cjs/response.js +162 -0
- package/lib/cjs/types/index.js +17 -0
- package/lib/cjs/types/system-error.js +16 -0
- package/lib/cjs/utils/blob-from.js +124 -0
- package/lib/cjs/utils/get-search.js +30 -0
- package/lib/cjs/utils/is-redirect.js +26 -0
- package/lib/cjs/utils/is.js +47 -0
- package/lib/cjs/utils/multipart-parser.js +372 -0
- package/lib/cjs/utils/referrer.js +172 -0
- package/lib/esm/body.js +255 -0
- package/lib/esm/errors/abort-error.js +9 -0
- package/lib/esm/errors/base.js +17 -0
- package/lib/esm/errors/fetch-error.js +21 -0
- package/lib/esm/headers.js +202 -0
- package/lib/esm/index.js +224 -0
- package/lib/esm/request.js +281 -0
- package/lib/esm/response.js +133 -0
- package/lib/esm/types/index.js +1 -0
- package/lib/esm/types/system-error.js +1 -0
- package/lib/esm/utils/blob-from.js +101 -0
- package/lib/esm/utils/get-search.js +11 -0
- package/lib/esm/utils/is-redirect.js +7 -0
- package/lib/esm/utils/is.js +28 -0
- package/lib/esm/utils/multipart-parser.js +353 -0
- package/lib/esm/utils/referrer.js +153 -0
- package/package.json +53 -0
- package/src/body.ts +415 -0
- package/src/errors/abort-error.ts +10 -0
- package/src/errors/base.ts +20 -0
- package/src/errors/fetch-error.ts +26 -0
- package/src/headers.ts +279 -0
- package/src/index.spec.ts +13 -0
- package/src/index.ts +367 -0
- package/src/request.ts +396 -0
- package/src/response.ts +197 -0
- package/src/test.mts +6 -0
- package/src/types/index.ts +1 -0
- package/src/types/system-error.ts +11 -0
- package/src/utils/blob-from.ts +168 -0
- package/src/utils/get-search.ts +9 -0
- package/src/utils/is-redirect.ts +11 -0
- package/src/utils/is.ts +88 -0
- package/src/utils/multipart-parser.ts +448 -0
- package/src/utils/referrer.ts +350 -0
- package/test.gjs.js +34758 -0
- package/test.gjs.mjs +53177 -0
- package/test.node.js +1226 -0
- package/test.node.mjs +6294 -0
- package/tsconfig.json +19 -0
- package/tsconfig.types.json +8 -0
package/src/headers.ts
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Headers.js
|
|
3
|
+
*
|
|
4
|
+
* Headers class offers convenient helpers
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import Soup from '@girs/soup-3.0';
|
|
8
|
+
import { URLSearchParams } from '@gjsify/deno-runtime/ext/url/00_url';
|
|
9
|
+
|
|
10
|
+
import { types } from 'util';
|
|
11
|
+
import * as http from 'http';
|
|
12
|
+
import type { IncomingMessage } from 'http';
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
/* c8 ignore next 9 */
|
|
16
|
+
|
|
17
|
+
const validateHeaderName = http.validateHeaderName;
|
|
18
|
+
const validateHeaderValue = http.validateHeaderValue;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* This Fetch API interface allows you to perform various actions on HTTP request and response headers.
|
|
22
|
+
* These actions include retrieving, setting, adding to, and removing.
|
|
23
|
+
* A Headers object has an associated header list, which is initially empty and consists of zero or more name and value pairs.
|
|
24
|
+
* You can add to this using methods like append() (see Examples.)
|
|
25
|
+
* In all methods of this interface, header names are matched by case-insensitive byte sequence.
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
export default class Headers extends URLSearchParams implements globalThis.Headers, Iterable<[string, string]> {
|
|
29
|
+
/**
|
|
30
|
+
* Headers class
|
|
31
|
+
*
|
|
32
|
+
* @constructor
|
|
33
|
+
* @param init Response headers
|
|
34
|
+
*/
|
|
35
|
+
constructor(init?: HeadersInit) {
|
|
36
|
+
// Validate and normalize init object in [name, value(s)][]
|
|
37
|
+
let result: string[][] = [];
|
|
38
|
+
if (init instanceof Headers) {
|
|
39
|
+
const raw = init.raw();
|
|
40
|
+
for (const [name, values] of Object.entries(raw)) {
|
|
41
|
+
result.push(...values.map(value => [name, value]));
|
|
42
|
+
}
|
|
43
|
+
} else if (init == null) { // eslint-disable-line no-eq-null, eqeqeq
|
|
44
|
+
// No op
|
|
45
|
+
} else if (typeof init === 'object' && !types.isBoxedPrimitive(init)) {
|
|
46
|
+
const method = init[Symbol.iterator];
|
|
47
|
+
// eslint-disable-next-line no-eq-null, eqeqeq
|
|
48
|
+
if (method == null) {
|
|
49
|
+
// Record<ByteString, ByteString>
|
|
50
|
+
result.push(...Object.entries(init));
|
|
51
|
+
} else {
|
|
52
|
+
if (typeof method !== 'function') {
|
|
53
|
+
throw new TypeError('Header pairs must be iterable');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Sequence<sequence<ByteString>>
|
|
57
|
+
// Note: per spec we have to first exhaust the lists then process them
|
|
58
|
+
result = [...(init as string[][])] // TODO check if this works with Objects
|
|
59
|
+
.map(pair => {
|
|
60
|
+
if (
|
|
61
|
+
typeof pair !== 'object' || types.isBoxedPrimitive(pair)
|
|
62
|
+
) {
|
|
63
|
+
throw new TypeError('Each header pair must be an iterable object');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return [...pair];
|
|
67
|
+
}).map(pair => {
|
|
68
|
+
if (pair.length !== 2) {
|
|
69
|
+
throw new TypeError('Each header pair must be a name/value tuple');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return [...pair];
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
throw new TypeError('Failed to construct \'Headers\': The provided value is not of type \'(sequence<sequence<ByteString>> or record<ByteString, ByteString>)');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Validate and lowercase
|
|
80
|
+
result =
|
|
81
|
+
result.length > 0 ?
|
|
82
|
+
result.map(([name, value]) => {
|
|
83
|
+
validateHeaderName(name);
|
|
84
|
+
validateHeaderValue(name, String(value));
|
|
85
|
+
return [String(name).toLowerCase(), String(value)];
|
|
86
|
+
}) :
|
|
87
|
+
undefined;
|
|
88
|
+
|
|
89
|
+
super(result);
|
|
90
|
+
|
|
91
|
+
// Returning a Proxy that will lowercase key names, validate parameters and sort keys
|
|
92
|
+
// eslint-disable-next-line no-constructor-return
|
|
93
|
+
return new Proxy(this, {
|
|
94
|
+
get(target, p, receiver) {
|
|
95
|
+
switch (p) {
|
|
96
|
+
case 'append':
|
|
97
|
+
case 'set':
|
|
98
|
+
return (name: string, value: any) => {
|
|
99
|
+
validateHeaderName(name);
|
|
100
|
+
validateHeaderValue(name, String(value));
|
|
101
|
+
return URLSearchParams.prototype[p].call(
|
|
102
|
+
target,
|
|
103
|
+
String(name).toLowerCase(),
|
|
104
|
+
String(value)
|
|
105
|
+
);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
case 'delete':
|
|
109
|
+
case 'has':
|
|
110
|
+
case 'getAll':
|
|
111
|
+
return (name: string) => {
|
|
112
|
+
validateHeaderName(name);
|
|
113
|
+
return URLSearchParams.prototype[p].call(
|
|
114
|
+
target,
|
|
115
|
+
String(name).toLowerCase()
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
case 'keys':
|
|
120
|
+
return () => {
|
|
121
|
+
target.sort();
|
|
122
|
+
return new Set(URLSearchParams.prototype.keys.call(target)).keys();
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
default:
|
|
126
|
+
return Reflect.get(target, p, receiver);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
/* c8 ignore next */
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
get [Symbol.toStringTag]() {
|
|
134
|
+
return this.constructor.name;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
toString() {
|
|
138
|
+
return Object.prototype.toString.call(this);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
_appendToSoupMessage(message?: Soup.Message, type = Soup.MessageHeadersType.REQUEST) {
|
|
142
|
+
const soupHeaders = message ? message.get_request_headers() : new Soup.MessageHeaders(type);
|
|
143
|
+
for (const header in this.entries()) {
|
|
144
|
+
soupHeaders.append(header, this.get(header));
|
|
145
|
+
}
|
|
146
|
+
return soupHeaders;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
static _newFromSoupMessage(message: Soup.Message, type: Soup.MessageHeadersType = Soup.MessageHeadersType.RESPONSE) {
|
|
151
|
+
let soupHeaders: Soup.MessageHeaders;
|
|
152
|
+
const headers = new Headers();
|
|
153
|
+
|
|
154
|
+
if (type === Soup.MessageHeadersType.RESPONSE) {
|
|
155
|
+
soupHeaders = message.get_response_headers();
|
|
156
|
+
} else if(type === Soup.MessageHeadersType.REQUEST) {
|
|
157
|
+
soupHeaders = message.get_request_headers();
|
|
158
|
+
} else {
|
|
159
|
+
for (const header in message.get_request_headers()) {
|
|
160
|
+
headers.append(header, soupHeaders[header]);
|
|
161
|
+
}
|
|
162
|
+
soupHeaders = message.get_response_headers();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
for (const header in soupHeaders) {
|
|
166
|
+
headers.append(header, soupHeaders[header]);
|
|
167
|
+
}
|
|
168
|
+
return headers;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
get(name: string) {
|
|
172
|
+
const values = this.getAll(name);
|
|
173
|
+
if (values.length === 0) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
let value = values.join(', ');
|
|
178
|
+
if (/^content-encoding$/i.test(name)) {
|
|
179
|
+
value = value.toLowerCase();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return value;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
forEach(callback, thisArg = undefined) {
|
|
186
|
+
for (const name of this.keys()) {
|
|
187
|
+
Reflect.apply(callback, thisArg, [this.get(name), name, this]);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
* values() {
|
|
192
|
+
for (const name of this.keys()) {
|
|
193
|
+
yield this.get(name);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
*
|
|
199
|
+
*/
|
|
200
|
+
* entries(): IterableIterator<[string, string]> {
|
|
201
|
+
for (const name of this.keys()) {
|
|
202
|
+
yield [name, this.get(name)];
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
[Symbol.iterator]() {
|
|
207
|
+
return this.entries();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Node-fetch non-spec method
|
|
212
|
+
* returning all headers and their values as array
|
|
213
|
+
*/
|
|
214
|
+
raw(): Record<string, string[]> {
|
|
215
|
+
return [...this.keys()].reduce((result, key) => {
|
|
216
|
+
result[key] = this.getAll(key);
|
|
217
|
+
return result;
|
|
218
|
+
}, {});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* For better console.log(headers) and also to convert Headers into Node.js Request compatible format
|
|
223
|
+
*/
|
|
224
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
225
|
+
return [...this.keys()].reduce((result, key) => {
|
|
226
|
+
const values = this.getAll(key);
|
|
227
|
+
// Http.request() only supports string as Host header.
|
|
228
|
+
// This hack makes specifying custom Host header possible.
|
|
229
|
+
if (key === 'host') {
|
|
230
|
+
result[key] = values[0];
|
|
231
|
+
} else {
|
|
232
|
+
result[key] = values.length > 1 ? values : values[0];
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return result;
|
|
236
|
+
}, {});
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Re-shaping object for Web IDL tests
|
|
242
|
+
* Only need to do it for overridden methods
|
|
243
|
+
*/
|
|
244
|
+
Object.defineProperties(
|
|
245
|
+
Headers.prototype,
|
|
246
|
+
['get', 'entries', 'forEach', 'values'].reduce((result, property) => {
|
|
247
|
+
result[property] = { enumerable: true };
|
|
248
|
+
return result;
|
|
249
|
+
}, {})
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Create a Headers object from an http.IncomingMessage.rawHeaders, ignoring those that do
|
|
254
|
+
* not conform to HTTP grammar productions.
|
|
255
|
+
* @param headers
|
|
256
|
+
*/
|
|
257
|
+
export function fromRawHeaders(headers: IncomingMessage['rawHeaders'] = []) {
|
|
258
|
+
return new Headers(
|
|
259
|
+
headers
|
|
260
|
+
// Split into pairs
|
|
261
|
+
.reduce((result, value, index, array) => {
|
|
262
|
+
if (index % 2 === 0) {
|
|
263
|
+
result.push(array.slice(index, index + 2));
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return result;
|
|
267
|
+
}, [])
|
|
268
|
+
.filter(([name, value]) => {
|
|
269
|
+
try {
|
|
270
|
+
validateHeaderName(name);
|
|
271
|
+
validateHeaderValue(name, String(value));
|
|
272
|
+
return true;
|
|
273
|
+
} catch {
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
);
|
|
279
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, it, expect } from '@gjsify/unit';
|
|
2
|
+
|
|
3
|
+
import fetch from 'node-fetch';
|
|
4
|
+
|
|
5
|
+
export default async () => {
|
|
6
|
+
|
|
7
|
+
await describe('fetch', async () => {
|
|
8
|
+
await it('fetch should be a function', async () => {
|
|
9
|
+
expect(typeof fetch).toBe("function");
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import Soup from '@girs/soup-3.0';
|
|
2
|
+
import Gio from '@girs/gio-2.0';
|
|
3
|
+
import Request from './request.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Index.js
|
|
7
|
+
*
|
|
8
|
+
* a request API compatible with window.fetch
|
|
9
|
+
*
|
|
10
|
+
* All spec algorithm step numbers are based on https://fetch.spec.whatwg.org/commit-snapshots/ae716822cb3a61843226cd090eefc6589446c1d2/.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import zlib from 'zlib';
|
|
14
|
+
import Stream, { PassThrough, pipeline as pump } from 'stream';
|
|
15
|
+
|
|
16
|
+
import dataUriToBuffer from 'data-uri-to-buffer';
|
|
17
|
+
|
|
18
|
+
import { writeToStream, clone } from './body.js';
|
|
19
|
+
import Response from './response.js';
|
|
20
|
+
import Headers from './headers.js';
|
|
21
|
+
import { getSoupRequestOptions } from './request.js';
|
|
22
|
+
import { FetchError } from './errors/fetch-error.js';
|
|
23
|
+
import { AbortError } from './errors/abort-error.js';
|
|
24
|
+
import { isRedirect } from './utils/is-redirect.js';
|
|
25
|
+
import { FormData } from 'formdata-polyfill/esm.min.js';
|
|
26
|
+
import { isDomainOrSubdomain, isSameProtocol } from './utils/is.js';
|
|
27
|
+
import { parseReferrerPolicyFromHeader } from './utils/referrer.js';
|
|
28
|
+
import {
|
|
29
|
+
Blob,
|
|
30
|
+
File,
|
|
31
|
+
fileFromSync,
|
|
32
|
+
fileFrom,
|
|
33
|
+
blobFromSync,
|
|
34
|
+
blobFrom
|
|
35
|
+
} from './utils/blob-from.js';
|
|
36
|
+
|
|
37
|
+
import { URL } from '@gjsify/deno-runtime/ext/url/00_url';
|
|
38
|
+
|
|
39
|
+
export { FormData, Headers, Request, Response, FetchError, AbortError, isRedirect };
|
|
40
|
+
export { Blob, File, fileFromSync, fileFrom, blobFromSync, blobFrom };
|
|
41
|
+
|
|
42
|
+
import type { SystemError } from './types/index.js';
|
|
43
|
+
|
|
44
|
+
const supportedSchemas = new Set(['data:', 'http:', 'https:']);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Fetch function
|
|
48
|
+
*
|
|
49
|
+
* @param url Absolute url or Request instance
|
|
50
|
+
* @param init Fetch options
|
|
51
|
+
*/
|
|
52
|
+
export default async function fetch(url: RequestInfo | URL, init: RequestInit = {}): Promise<Response> {
|
|
53
|
+
return new Promise(async (resolve, reject) => {
|
|
54
|
+
// Build request object
|
|
55
|
+
const request = new Request(url, init);
|
|
56
|
+
const { parsedURL, options } = getSoupRequestOptions(request);
|
|
57
|
+
if (!supportedSchemas.has(parsedURL.protocol)) {
|
|
58
|
+
throw new TypeError(`@gjsify/fetch cannot load ${url}. URL scheme "${parsedURL.protocol.replace(/:$/, '')}" is not supported.`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (parsedURL.protocol === 'data:') {
|
|
62
|
+
const data = dataUriToBuffer(request.url);
|
|
63
|
+
const response = new Response(data, { headers: { 'Content-Type': data.typeFull } });
|
|
64
|
+
resolve(response);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const { signal } = request;
|
|
69
|
+
let response = null;
|
|
70
|
+
|
|
71
|
+
const abort = () => {
|
|
72
|
+
const error = new AbortError('The operation was aborted.');
|
|
73
|
+
reject(error);
|
|
74
|
+
if (request.body && request.body instanceof Stream.Readable) {
|
|
75
|
+
request.body.destroy(error);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!response || !response.body) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
response.body.emit('error', error);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
if (signal && signal.aborted) {
|
|
86
|
+
abort();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const abortAndFinalize = () => {
|
|
91
|
+
abort();
|
|
92
|
+
finalize();
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
let readable: Stream.Readable;
|
|
96
|
+
let cancellable: Gio.Cancellable;
|
|
97
|
+
|
|
98
|
+
// Send request
|
|
99
|
+
try {
|
|
100
|
+
const sendRes = await request._send(options);
|
|
101
|
+
readable = sendRes.readable;
|
|
102
|
+
cancellable = sendRes.cancellable;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
reject(error);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (signal) {
|
|
108
|
+
signal.addEventListener('abort', abortAndFinalize);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const cancelledSignalId = cancellable.connect('cancelled', () => {
|
|
112
|
+
abortAndFinalize();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const finalize = () => {
|
|
116
|
+
cancellable.cancel()
|
|
117
|
+
if (signal) {
|
|
118
|
+
signal.removeEventListener('abort', abortAndFinalize);
|
|
119
|
+
}
|
|
120
|
+
cancellable.disconnect(cancelledSignalId);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const message = request._message;
|
|
124
|
+
|
|
125
|
+
readable.on('error', (error: SystemError) => {
|
|
126
|
+
reject(new FetchError(`request to ${request.url} failed, reason: ${error.message}`, 'system', error));
|
|
127
|
+
finalize();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
message.connect('finished', (message) => {
|
|
131
|
+
|
|
132
|
+
const headers = Headers._newFromSoupMessage( request._message, Soup.MessageHeadersType.RESPONSE);
|
|
133
|
+
const statusCode = message.status_code;
|
|
134
|
+
const statusMessage = message.get_reason_phrase() ;
|
|
135
|
+
|
|
136
|
+
// HTTP fetch step 5
|
|
137
|
+
if (isRedirect(statusCode)) {
|
|
138
|
+
// HTTP fetch step 5.2
|
|
139
|
+
const location = headers.get('Location');
|
|
140
|
+
|
|
141
|
+
// HTTP fetch step 5.3
|
|
142
|
+
let locationURL = null;
|
|
143
|
+
try {
|
|
144
|
+
locationURL = location === null ? null : new URL(location, request.url);
|
|
145
|
+
} catch {
|
|
146
|
+
// error here can only be invalid URL in Location: header
|
|
147
|
+
// do not throw when options.redirect == manual
|
|
148
|
+
// let the user extract the errorneous redirect URL
|
|
149
|
+
if (request.redirect !== 'manual') {
|
|
150
|
+
reject(new FetchError(`uri requested responds with an invalid redirect URL: ${location}`, 'invalid-redirect'));
|
|
151
|
+
finalize();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// HTTP fetch step 5.5
|
|
157
|
+
switch (request.redirect) {
|
|
158
|
+
case 'error':
|
|
159
|
+
reject(new FetchError(`uri requested responds with a redirect, redirect mode is set to error: ${request.url}`, 'no-redirect'));
|
|
160
|
+
finalize();
|
|
161
|
+
return;
|
|
162
|
+
case 'manual':
|
|
163
|
+
// Nothing to do
|
|
164
|
+
break;
|
|
165
|
+
case 'follow': {
|
|
166
|
+
// HTTP-redirect fetch step 2
|
|
167
|
+
if (locationURL === null) {
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// HTTP-redirect fetch step 5
|
|
172
|
+
if (request.counter >= request.follow) {
|
|
173
|
+
reject(new FetchError(`maximum redirect reached at: ${request.url}`, 'max-redirect'));
|
|
174
|
+
finalize();
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// HTTP-redirect fetch step 6 (counter increment)
|
|
179
|
+
// Create a new Request object.
|
|
180
|
+
const requestOptions = {
|
|
181
|
+
headers: new Headers(request.headers),
|
|
182
|
+
follow: request.follow,
|
|
183
|
+
counter: request.counter + 1,
|
|
184
|
+
agent: request.agent,
|
|
185
|
+
compress: request.compress,
|
|
186
|
+
method: request.method,
|
|
187
|
+
body: clone(request),
|
|
188
|
+
signal: request.signal,
|
|
189
|
+
size: request.size,
|
|
190
|
+
referrer: request.referrer,
|
|
191
|
+
referrerPolicy: request.referrerPolicy
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// when forwarding sensitive headers like "Authorization",
|
|
195
|
+
// "WWW-Authenticate", and "Cookie" to untrusted targets,
|
|
196
|
+
// headers will be ignored when following a redirect to a domain
|
|
197
|
+
// that is not a subdomain match or exact match of the initial domain.
|
|
198
|
+
// For example, a redirect from "foo.com" to either "foo.com" or "sub.foo.com"
|
|
199
|
+
// will forward the sensitive headers, but a redirect to "bar.com" will not.
|
|
200
|
+
// headers will also be ignored when following a redirect to a domain using
|
|
201
|
+
// a different protocol. For example, a redirect from "https://foo.com" to "http://foo.com"
|
|
202
|
+
// will not forward the sensitive headers
|
|
203
|
+
if (!isDomainOrSubdomain(request.url, locationURL) || !isSameProtocol(request.url, locationURL)) {
|
|
204
|
+
for (const name of ['authorization', 'www-authenticate', 'cookie', 'cookie2']) {
|
|
205
|
+
requestOptions.headers.delete(name);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// HTTP-redirect fetch step 9
|
|
210
|
+
if (statusCode !== 303 && request.body && init.body instanceof Stream.Readable) {
|
|
211
|
+
reject(new FetchError('Cannot follow redirect with body being a readable stream', 'unsupported-redirect'));
|
|
212
|
+
finalize();
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// HTTP-redirect fetch step 11
|
|
217
|
+
if (statusCode === 303 || ((statusCode === 301 || statusCode === 302) && request.method === 'POST')) {
|
|
218
|
+
requestOptions.method = 'GET';
|
|
219
|
+
requestOptions.body = undefined;
|
|
220
|
+
requestOptions.headers.delete('content-length');
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// HTTP-redirect fetch step 14
|
|
224
|
+
const responseReferrerPolicy = parseReferrerPolicyFromHeader(headers);
|
|
225
|
+
if (responseReferrerPolicy) {
|
|
226
|
+
requestOptions.referrerPolicy = responseReferrerPolicy;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// HTTP-redirect fetch step 15
|
|
230
|
+
resolve(fetch(new Request(locationURL, requestOptions)));
|
|
231
|
+
finalize();
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
default:
|
|
236
|
+
return reject(new TypeError(`Redirect option '${request.redirect}' is not a valid value of RequestRedirect`));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Prepare response
|
|
241
|
+
// if (signal) {
|
|
242
|
+
// response_.once('end', () => {
|
|
243
|
+
// signal.removeEventListener('abort', abortAndFinalize);
|
|
244
|
+
// });
|
|
245
|
+
// }
|
|
246
|
+
|
|
247
|
+
let body = pump(response_, new PassThrough(), error => {
|
|
248
|
+
if (error) {
|
|
249
|
+
reject(error);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
// see https://github.com/nodejs/node/pull/29376
|
|
253
|
+
/* c8 ignore next 3 */
|
|
254
|
+
// if (process.version < 'v12.10') {
|
|
255
|
+
// response_.on('aborted', abortAndFinalize);
|
|
256
|
+
// }
|
|
257
|
+
|
|
258
|
+
const responseOptions = {
|
|
259
|
+
url: request.url,
|
|
260
|
+
status: statusCode,
|
|
261
|
+
statusText: statusMessage,
|
|
262
|
+
headers,
|
|
263
|
+
size: request.size,
|
|
264
|
+
counter: request.counter,
|
|
265
|
+
highWaterMark: request.highWaterMark
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// HTTP-network fetch step 12.1.1.3
|
|
269
|
+
const codings = headers.get('Content-Encoding');
|
|
270
|
+
|
|
271
|
+
// HTTP-network fetch step 12.1.1.4: handle content codings
|
|
272
|
+
|
|
273
|
+
// in following scenarios we ignore compression support
|
|
274
|
+
// 1. compression support is disabled
|
|
275
|
+
// 2. HEAD request
|
|
276
|
+
// 3. no Content-Encoding header
|
|
277
|
+
// 4. no content response (204)
|
|
278
|
+
// 5. content not modified response (304)
|
|
279
|
+
if (!request.compress || request.method === 'HEAD' || codings === null || statusCode === 204 || statusCode === 304) {
|
|
280
|
+
response = new Response(body, responseOptions);
|
|
281
|
+
resolve(response);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// For Node v6+
|
|
286
|
+
// Be less strict when decoding compressed responses, since sometimes
|
|
287
|
+
// servers send slightly invalid responses that are still accepted
|
|
288
|
+
// by common browsers.
|
|
289
|
+
// Always using Z_SYNC_FLUSH is what cURL does.
|
|
290
|
+
const zlibOptions = {
|
|
291
|
+
flush: zlib.Z_SYNC_FLUSH,
|
|
292
|
+
finishFlush: zlib.Z_SYNC_FLUSH
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
// For gzip
|
|
296
|
+
if (codings === 'gzip' || codings === 'x-gzip') {
|
|
297
|
+
body = pump(body, zlib.createGunzip(zlibOptions), error => {
|
|
298
|
+
if (error) {
|
|
299
|
+
reject(error);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
response = new Response(body, responseOptions);
|
|
303
|
+
resolve(response);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// For deflate
|
|
308
|
+
// if (codings === 'deflate' || codings === 'x-deflate') {
|
|
309
|
+
// // Handle the infamous raw deflate response from old servers
|
|
310
|
+
// // a hack for old IIS and Apache servers
|
|
311
|
+
// const raw = pump(response_, new PassThrough(), error => {
|
|
312
|
+
// if (error) {
|
|
313
|
+
// reject(error);
|
|
314
|
+
// }
|
|
315
|
+
// });
|
|
316
|
+
|
|
317
|
+
// raw.once('data', chunk => {
|
|
318
|
+
// // See http://stackoverflow.com/questions/37519828
|
|
319
|
+
// if ((chunk[0] & 0x0F) === 0x08) {
|
|
320
|
+
// body = pump(body, zlib.createInflate(), error => {
|
|
321
|
+
// if (error) {
|
|
322
|
+
// reject(error);
|
|
323
|
+
// }
|
|
324
|
+
// });
|
|
325
|
+
// } else {
|
|
326
|
+
// body = pump(body, zlib.createInflateRaw(), error => {
|
|
327
|
+
// if (error) {
|
|
328
|
+
// reject(error);
|
|
329
|
+
// }
|
|
330
|
+
// });
|
|
331
|
+
// }
|
|
332
|
+
|
|
333
|
+
// response = new Response(body, responseOptions);
|
|
334
|
+
// resolve(response);
|
|
335
|
+
// });
|
|
336
|
+
|
|
337
|
+
// raw.once('end', () => {
|
|
338
|
+
// // Some old IIS servers return zero-length OK deflate responses, so
|
|
339
|
+
// // 'data' is never emitted. See https://github.com/node-fetch/node-fetch/pull/903
|
|
340
|
+
// if (!response) {
|
|
341
|
+
// response = new Response(body, responseOptions);
|
|
342
|
+
// resolve(response);
|
|
343
|
+
// }
|
|
344
|
+
// });
|
|
345
|
+
// return;
|
|
346
|
+
// }
|
|
347
|
+
|
|
348
|
+
// For br
|
|
349
|
+
if (codings === 'br') {
|
|
350
|
+
body = pump(body, zlib.createBrotliDecompress(), error => {
|
|
351
|
+
if (error) {
|
|
352
|
+
reject(error);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
response = new Response(body, responseOptions);
|
|
356
|
+
resolve(response);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Otherwise, use response as-is
|
|
361
|
+
response = new Response(body, responseOptions);
|
|
362
|
+
resolve(response);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
writeToStream(inputStream, request).catch(reject);
|
|
366
|
+
});
|
|
367
|
+
}
|