@loaders.gl/polyfills 3.3.0-alpha.12 → 3.3.0-alpha.14
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/dist/dist.min.js +64 -15
- package/dist/es5/index.js +17 -11
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/node/fetch/fetch-file.node.js +88 -0
- package/dist/es5/node/fetch/fetch-file.node.js.map +1 -0
- package/dist/es5/node/fetch/fetch.node.js +90 -35
- package/dist/es5/node/fetch/fetch.node.js.map +1 -1
- package/dist/es5/node/fetch/headers.node.js +2 -2
- package/dist/es5/node/fetch/headers.node.js.map +1 -1
- package/dist/es5/node/fetch/response.node.js +5 -4
- package/dist/es5/node/fetch/response.node.js.map +1 -1
- package/dist/es5/node/fetch/utils/stream-utils.node.js +9 -93
- package/dist/es5/node/fetch/utils/stream-utils.node.js.map +1 -1
- package/dist/esm/index.js +5 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/node/fetch/fetch-file.node.js +54 -0
- package/dist/esm/node/fetch/fetch-file.node.js.map +1 -0
- package/dist/esm/node/fetch/fetch.node.js +43 -18
- package/dist/esm/node/fetch/fetch.node.js.map +1 -1
- package/dist/esm/node/fetch/headers.node.js +1 -1
- package/dist/esm/node/fetch/headers.node.js.map +1 -1
- package/dist/esm/node/fetch/response.node.js +3 -2
- package/dist/esm/node/fetch/response.node.js.map +1 -1
- package/dist/esm/node/fetch/utils/stream-utils.node.js +3 -46
- package/dist/esm/node/fetch/utils/stream-utils.node.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -14
- package/dist/lib/encoding.js +1 -1
- package/dist/node/fetch/fetch-file.node.d.ts +4 -0
- package/dist/node/fetch/fetch-file.node.d.ts.map +1 -0
- package/dist/node/fetch/fetch-file.node.js +51 -0
- package/dist/node/fetch/fetch.node.d.ts +6 -1
- package/dist/node/fetch/fetch.node.d.ts.map +1 -1
- package/dist/node/fetch/fetch.node.js +57 -31
- package/dist/node/fetch/headers.node.d.ts +1 -1
- package/dist/node/fetch/headers.node.d.ts.map +1 -1
- package/dist/node/fetch/headers.node.js +2 -1
- package/dist/node/fetch/response.node.d.ts +2 -2
- package/dist/node/fetch/response.node.d.ts.map +1 -1
- package/dist/node/fetch/response.node.js +5 -6
- package/dist/node/fetch/utils/stream-utils.node.d.ts +8 -1
- package/dist/node/fetch/utils/stream-utils.node.d.ts.map +1 -1
- package/dist/node/fetch/utils/stream-utils.node.js +10 -54
- package/package.json +3 -2
- package/src/index.ts +5 -4
- package/src/node/fetch/fetch-file.node.ts +51 -0
- package/src/node/fetch/fetch.node.ts +64 -30
- package/src/node/fetch/headers.node.ts +1 -1
- package/src/node/fetch/response.node.ts +4 -2
- package/src/node/fetch/utils/stream-utils.node.ts +10 -59
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response.node.js","names":["assert","decompressReadStream","concatenateReadStream","Headers","isBoolean","x","isFunction","isObject","isReadableNodeStream","read","pipe","readable","Readable","Response","constructor","body","options","headers","status","statusText","url","ok","_body","from","TextEncoder","encode","ArrayBuffer","bodyUsed","arrayBuffer","data","text","textDecoder","TextDecoder","decode","json","JSON","parse","blob","Blob","Error"],"sources":["../../../../src/node/fetch/response.node.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"response.node.js","names":["assert","decompressReadStream","concatenateReadStream","Headers","isBoolean","x","isFunction","isObject","isReadableNodeStream","read","pipe","readable","Readable","Response","constructor","body","options","headers","status","statusText","url","ok","_body","from","TextEncoder","encode","ArrayBuffer","bodyUsed","arrayBuffer","data","text","textDecoder","TextDecoder","decode","json","JSON","parse","blob","Blob","Error"],"sources":["../../../../src/node/fetch/response.node.ts"],"sourcesContent":["// loaders.gl, MIT license\n\nimport {assert} from '../../utils/assert';\nimport {decompressReadStream, concatenateReadStream} from './utils/stream-utils.node';\nimport {Headers} from './headers.node';\n\nconst isBoolean = (x) => typeof x === 'boolean';\nconst isFunction = (x) => typeof x === 'function';\nconst isObject = (x) => x !== null && typeof x === 'object';\nconst isReadableNodeStream = (x) =>\n isObject(x) && isFunction(x.read) && isFunction(x.pipe) && isBoolean(x.readable);\n\n/**\n * Polyfill for Browser Response\n *\n * Under Node.js we return a mock \"fetch response object\"\n * so that apps can use the same API as in the browser.\n *\n * Note: This is intended to be a \"lightweight\" implementation and will have limitations.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/API/Response\n */\nimport {Readable} from 'stream';\n\nexport class Response {\n readonly ok: boolean;\n readonly status: number;\n readonly statusText: string;\n readonly headers: Headers;\n readonly url: string;\n bodyUsed: boolean = false;\n private readonly _body;\n\n // TODO - handle ArrayBuffer, ArrayBufferView, Buffer\n constructor(\n body,\n options: {\n headers?;\n status?: number;\n statusText?: string;\n url: string;\n }\n ) {\n const {headers, status = 200, statusText = 'OK', url} = options || {};\n\n this.url = url;\n this.ok = status === 200;\n this.status = status; // TODO - handle errors and set status\n this.statusText = statusText;\n this.headers = new Headers(options?.headers || {});\n\n // Check for content-encoding and create a decompression stream\n if (isReadableNodeStream(body)) {\n this._body = decompressReadStream(body, headers);\n } else if (typeof body === 'string') {\n this._body = Readable.from([new TextEncoder().encode(body)]);\n } else {\n this._body = Readable.from([body || new ArrayBuffer(0)]);\n }\n }\n\n // Subset of Properties\n\n // Returns a readable stream to the \"body\" of the response (or file)\n get body() {\n assert(!this.bodyUsed);\n assert(isReadableNodeStream(this._body)); // Not implemented: conversion of ArrayBuffer etc to stream\n this.bodyUsed = true;\n return this._body;\n }\n\n // Subset of Methods\n\n async arrayBuffer() {\n if (!isReadableNodeStream(this._body)) {\n return this._body || new ArrayBuffer(0);\n }\n const data = await concatenateReadStream(this._body);\n return data;\n }\n\n async text() {\n const arrayBuffer = await this.arrayBuffer();\n const textDecoder = new TextDecoder();\n return textDecoder.decode(arrayBuffer);\n }\n\n async json() {\n const text = await this.text();\n return JSON.parse(text);\n }\n\n async blob() {\n if (typeof Blob === 'undefined') {\n throw new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AAI,OAAOC,IAAI,KAAK,WAAW,EAAE;MAC/B,MAAM,IAAIC,KAAK,CAAC,6BAA6B,CAAC;IAChD;IACA,OAAO,IAAID,IAAI,CAAC,CAAC,MAAM,IAAI,CAACV,WAAW,EAAE,CAAC,CAAC;EAC7C;AACF"}
|
|
@@ -1,32 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import https from 'https';
|
|
1
|
+
|
|
2
|
+
|
|
4
3
|
import zlib from 'zlib';
|
|
5
4
|
import { toArrayBuffer } from './decode-data-uri.node';
|
|
6
|
-
function isRequestURL(url) {
|
|
7
|
-
return url.startsWith('http:') || url.startsWith('https:');
|
|
8
|
-
}
|
|
9
5
|
|
|
10
|
-
export async function createReadStream(url, options) {
|
|
11
|
-
if (!isRequestURL(url)) {
|
|
12
|
-
const noqueryUrl = url.split('?')[0];
|
|
13
|
-
return await new Promise((resolve, reject) => {
|
|
14
|
-
const stream = fs.createReadStream(noqueryUrl, {
|
|
15
|
-
encoding: null
|
|
16
|
-
});
|
|
17
|
-
stream.once('readable', () => resolve(stream));
|
|
18
|
-
stream.on('error', error => reject(error));
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return await new Promise((resolve, reject) => {
|
|
23
|
-
const requestFunction = url.startsWith('https:') ? https.request : http.request;
|
|
24
|
-
const requestOptions = getRequestOptions(url, options);
|
|
25
|
-
const req = requestFunction(requestOptions, res => resolve(res));
|
|
26
|
-
req.on('error', error => reject(error));
|
|
27
|
-
req.end();
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
6
|
export function decompressReadStream(readStream, headers) {
|
|
31
7
|
switch (headers.get('content-encoding')) {
|
|
32
8
|
case 'br':
|
|
@@ -39,6 +15,7 @@ export function decompressReadStream(readStream, headers) {
|
|
|
39
15
|
return readStream;
|
|
40
16
|
}
|
|
41
17
|
}
|
|
18
|
+
|
|
42
19
|
export async function concatenateReadStream(readStream) {
|
|
43
20
|
const arrayBufferChunks = [];
|
|
44
21
|
return await new Promise((resolve, reject) => {
|
|
@@ -58,26 +35,6 @@ export async function concatenateReadStream(readStream) {
|
|
|
58
35
|
});
|
|
59
36
|
}
|
|
60
37
|
|
|
61
|
-
function getRequestOptions(url, options) {
|
|
62
|
-
const originalHeaders = (options === null || options === void 0 ? void 0 : options.headers) || {};
|
|
63
|
-
const headers = {};
|
|
64
|
-
for (const key of Object.keys(originalHeaders)) {
|
|
65
|
-
headers[key.toLowerCase()] = originalHeaders[key];
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
headers['accept-encoding'] = headers['accept-encoding'] || 'gzip,br,deflate';
|
|
69
|
-
const urlObject = new URL(url);
|
|
70
|
-
return {
|
|
71
|
-
hostname: urlObject.hostname,
|
|
72
|
-
path: urlObject.pathname,
|
|
73
|
-
method: 'GET',
|
|
74
|
-
...options,
|
|
75
|
-
...(options === null || options === void 0 ? void 0 : options.fetch),
|
|
76
|
-
headers,
|
|
77
|
-
port: urlObject.port
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
38
|
export function concatenateArrayBuffers(sources) {
|
|
82
39
|
const sourceArrays = sources.map(source2 => source2 instanceof ArrayBuffer ? new Uint8Array(source2) : source2);
|
|
83
40
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-utils.node.js","names":["
|
|
1
|
+
{"version":3,"file":"stream-utils.node.js","names":["zlib","toArrayBuffer","decompressReadStream","readStream","headers","get","pipe","createBrotliDecompress","createGunzip","createDeflate","concatenateReadStream","arrayBufferChunks","Promise","resolve","reject","on","error","read","chunk","Error","push","arrayBuffer","concatenateArrayBuffers","sources","sourceArrays","map","source2","ArrayBuffer","Uint8Array","byteLength","reduce","length","typedArray","result","offset","sourceArray","set","buffer"],"sources":["../../../../../src/node/fetch/utils/stream-utils.node.ts"],"sourcesContent":["// loaders.gl, MIT license\n\nimport zlib from 'zlib';\n\nimport {toArrayBuffer} from './decode-data-uri.node';\n\n/**\n *\n */\nexport function decompressReadStream(readStream, headers) {\n switch (headers.get('content-encoding')) {\n case 'br':\n return readStream.pipe(zlib.createBrotliDecompress());\n case 'gzip':\n return readStream.pipe(zlib.createGunzip());\n case 'deflate':\n return readStream.pipe(zlib.createDeflate());\n default:\n // No compression or an unknown one, just return it as is\n return readStream;\n }\n}\n\n/**\n *\n * @param readStream\n * @returns\n */\nexport async function concatenateReadStream(readStream): Promise<ArrayBuffer> {\n const arrayBufferChunks: ArrayBuffer[] = [];\n\n return await new Promise((resolve, reject) => {\n readStream.on('error', (error) => reject(error));\n\n // Once the readable callback has been added, stream switches to \"flowing mode\"\n // In Node 10 (but not 12 and 14) this causes `data` and `end` to never be called unless we read data here\n readStream.on('readable', () => readStream.read());\n\n readStream.on('data', (chunk) => {\n if (typeof chunk === 'string') {\n reject(new Error('Read stream not binary'));\n }\n arrayBufferChunks.push(toArrayBuffer(chunk));\n });\n\n readStream.on('end', () => {\n const arrayBuffer = concatenateArrayBuffers(arrayBufferChunks);\n resolve(arrayBuffer);\n });\n });\n}\n\n/**\n * Concatenate a sequence of ArrayBuffers\n * @return A concatenated ArrayBuffer\n * @note duplicates loader-utils since polyfills should be independent\n */\nexport function concatenateArrayBuffers(sources: (ArrayBuffer | Uint8Array)[]): ArrayBuffer {\n // Make sure all inputs are wrapped in typed arrays\n const sourceArrays = sources.map((source2) =>\n source2 instanceof ArrayBuffer ? new Uint8Array(source2) : source2\n );\n\n // Get length of all inputs\n const byteLength = sourceArrays.reduce((length, typedArray) => length + typedArray.byteLength, 0);\n\n // Allocate array with space for all inputs\n const result = new Uint8Array(byteLength);\n\n // Copy the subarrays\n let offset = 0;\n for (const sourceArray of sourceArrays) {\n result.set(sourceArray, offset);\n offset += sourceArray.byteLength;\n }\n\n // We work with ArrayBuffers, discard the typed array wrapper\n return result.buffer;\n}\n"],"mappings":";;AAEA,OAAOA,IAAI,MAAM,MAAM;AAEvB,SAAQC,aAAa,QAAO,wBAAwB;;AAKpD,OAAO,SAASC,oBAAoB,CA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,CAAEC,OAAO,IACvCA,OAAO,YAAYC,WAAW,GAAG,IAAIC,UAAU,CAACF,OAAO,CAAC,GAAGA,OAAO,CACnE;;EAGD,MAAMG,UAAU,GAAGL,YAAY,CAACM,MAAM,CAAC,CAACC,MAAM,EAAEC,UAAU,KAAKD,MAAM,GAAGC,UAAU,CAACH,UAAU,EAAE,CAAC,CAAC;;EAGjG,MAAMI,MAAM,GAAG,IAAIL,UAAU,CAACC,UAAU,CAAC;;EAGzC,IAAIK,MAAM,GAAG,CAAC;EACd,KAAK,MAAMC,WAAW,IAAIX,YAAY,EAAE;IACtCS,MAAM,CAACG,GAAG,CAACD,WAAW,EAAED,MAAM,CAAC;IAC/BA,MAAM,IAAIC,WAAW,CAACN,UAAU;EAClC;;EAGA,OAAOI,MAAM,CAACI,MAAM;AACtB"}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,5 +3,6 @@ export { BlobPolyfill } from './node/file/blob';
|
|
|
3
3
|
export { FileReaderPolyfill } from './node/file/file-reader';
|
|
4
4
|
export { FilePolyfill } from './node/file/file';
|
|
5
5
|
export { installFilePolyfills } from './node/file/install-file-polyfills';
|
|
6
|
-
export {
|
|
6
|
+
export { fetchNode as _fetchNode } from './node/fetch/fetch.node';
|
|
7
|
+
export { fetchFileNode as _fetchFileNode } from './node/fetch/fetch-file.node';
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAC,sBAAsB,EAAC,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAC,kBAAkB,EAAC,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAC,oBAAoB,EAAC,MAAM,oCAAoC,CAAC;AACxE,OAAO,EAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAC,sBAAsB,EAAC,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAC,kBAAkB,EAAC,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAC,oBAAoB,EAAC,MAAM,oCAAoC,CAAC;AACxE,OAAO,EAAC,SAAS,IAAI,UAAU,EAAC,MAAM,yBAAyB,CAAC;AAChE,OAAO,EAAC,aAAa,IAAI,cAAc,EAAC,MAAM,8BAA8B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -22,11 +22,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports._fetchNode = exports.installFilePolyfills = exports.FilePolyfill = exports.FileReaderPolyfill = exports.BlobPolyfill = exports.ReadableStreamPolyfill = void 0;
|
|
26
|
+
exports._fetchFileNode = exports._fetchNode = exports.installFilePolyfills = exports.FilePolyfill = exports.FileReaderPolyfill = exports.BlobPolyfill = exports.ReadableStreamPolyfill = void 0;
|
|
30
27
|
/* eslint-disable dot-notation */
|
|
31
28
|
const xmldom_1 = require("@xmldom/xmldom");
|
|
32
29
|
const globals_1 = require("./utils/globals");
|
|
@@ -34,9 +31,9 @@ const encoding_1 = require("./lib/encoding");
|
|
|
34
31
|
const all_settled_1 = require("./promise/all-settled");
|
|
35
32
|
// Node specific
|
|
36
33
|
const base64 = __importStar(require("./node/buffer/btoa.node"));
|
|
37
|
-
const headers_node_1 =
|
|
38
|
-
const response_node_1 =
|
|
39
|
-
const fetch_node_1 =
|
|
34
|
+
const headers_node_1 = require("./node/fetch/headers.node");
|
|
35
|
+
const response_node_1 = require("./node/fetch/response.node");
|
|
36
|
+
const fetch_node_1 = require("./node/fetch/fetch.node");
|
|
40
37
|
const encode_image_node_1 = require("./node/images/encode-image.node");
|
|
41
38
|
const parse_image_node_1 = require("./node/images/parse-image.node");
|
|
42
39
|
var readable_stream_1 = require("./node/file/readable-stream");
|
|
@@ -50,7 +47,9 @@ Object.defineProperty(exports, "FilePolyfill", { enumerable: true, get: function
|
|
|
50
47
|
var install_file_polyfills_1 = require("./node/file/install-file-polyfills");
|
|
51
48
|
Object.defineProperty(exports, "installFilePolyfills", { enumerable: true, get: function () { return install_file_polyfills_1.installFilePolyfills; } });
|
|
52
49
|
var fetch_node_2 = require("./node/fetch/fetch.node");
|
|
53
|
-
Object.defineProperty(exports, "_fetchNode", { enumerable: true, get: function () { return
|
|
50
|
+
Object.defineProperty(exports, "_fetchNode", { enumerable: true, get: function () { return fetch_node_2.fetchNode; } });
|
|
51
|
+
var fetch_file_node_1 = require("./node/fetch/fetch-file.node");
|
|
52
|
+
Object.defineProperty(exports, "_fetchFileNode", { enumerable: true, get: function () { return fetch_file_node_1.fetchFileNode; } });
|
|
54
53
|
// POLYFILLS: TextEncoder, TextDecoder
|
|
55
54
|
// - Recent Node versions have these classes but virtually no encodings unless special build.
|
|
56
55
|
// - Browser: Edge, IE11 do not have these
|
|
@@ -74,14 +73,14 @@ if (!globals_1.isBrowser && !('btoa' in globals_1.global) && base64.btoa) {
|
|
|
74
73
|
// POLYFILL: fetch
|
|
75
74
|
// - Node: Yes
|
|
76
75
|
// - Browser: No. For This polyfill is node only, IE11 etc, install external polyfill
|
|
77
|
-
if (!globals_1.isBrowser && !('Headers' in globals_1.global) && headers_node_1.
|
|
78
|
-
globals_1.global['Headers'] = headers_node_1.
|
|
76
|
+
if (!globals_1.isBrowser && !('Headers' in globals_1.global) && headers_node_1.Headers) {
|
|
77
|
+
globals_1.global['Headers'] = headers_node_1.Headers;
|
|
79
78
|
}
|
|
80
|
-
if (!globals_1.isBrowser && !('Response' in globals_1.global) && response_node_1.
|
|
81
|
-
globals_1.global['Response'] = response_node_1.
|
|
79
|
+
if (!globals_1.isBrowser && !('Response' in globals_1.global) && response_node_1.Response) {
|
|
80
|
+
globals_1.global['Response'] = response_node_1.Response;
|
|
82
81
|
}
|
|
83
|
-
if (!globals_1.isBrowser && !('fetch' in globals_1.global) && fetch_node_1.
|
|
84
|
-
globals_1.global['fetch'] = fetch_node_1.
|
|
82
|
+
if (!globals_1.isBrowser && !('fetch' in globals_1.global) && fetch_node_1.fetchNode) {
|
|
83
|
+
globals_1.global['fetch'] = fetch_node_1.fetchNode;
|
|
85
84
|
}
|
|
86
85
|
// POLYFILL: DOMParser
|
|
87
86
|
// - Node: Yes
|
package/dist/lib/encoding.js
CHANGED
|
@@ -11,7 +11,7 @@ exports.TextDecoder = exports.TextEncoder = void 0;
|
|
|
11
11
|
// See LICENSE.md for more information.
|
|
12
12
|
const encoding_indexes_1 = __importDefault(require("./encoding-indexes"));
|
|
13
13
|
// Note: Aaian character indices add half a megabyte to bundle. Ignore, since we really only want the built-in UTF8...
|
|
14
|
-
// import indexes from './encoding-indexes-asian
|
|
14
|
+
// import indexes from './encoding-indexes-asian';
|
|
15
15
|
global['encoding-indexes'] = encoding_indexes_1.default || {};
|
|
16
16
|
//
|
|
17
17
|
// Utilities
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-file.node.d.ts","sourceRoot":"","sources":["../../../src/node/fetch/fetch-file.node.ts"],"names":[],"mappings":"AAGA,OAAO,EAAC,QAAQ,EAAC,MAAM,iBAAiB,CAAC;AAGzC,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEjD;AAED,wBAAsB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,KAAA,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsB3E"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// loaders.gl, MIT license
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.fetchFileNode = exports.isRequestURL = void 0;
|
|
8
|
+
const fs_1 = __importDefault(require("fs")); // `fs` will be empty object in browsers (see package.json "browser" field).
|
|
9
|
+
const response_node_1 = require("./response.node");
|
|
10
|
+
const headers_node_1 = require("./headers.node");
|
|
11
|
+
function isRequestURL(url) {
|
|
12
|
+
return url.startsWith('http:') || url.startsWith('https:');
|
|
13
|
+
}
|
|
14
|
+
exports.isRequestURL = isRequestURL;
|
|
15
|
+
async function fetchFileNode(url, options) {
|
|
16
|
+
const noqueryUrl = url.split('?')[0];
|
|
17
|
+
try {
|
|
18
|
+
// Now open the stream
|
|
19
|
+
const body = await new Promise((resolve, reject) => {
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
const stream = fs_1.default.createReadStream(noqueryUrl, { encoding: null });
|
|
22
|
+
stream.once('readable', () => resolve(stream));
|
|
23
|
+
stream.on('error', (error) => reject(error));
|
|
24
|
+
});
|
|
25
|
+
const status = 200;
|
|
26
|
+
const statusText = 'OK';
|
|
27
|
+
const headers = getHeadersForFile(noqueryUrl);
|
|
28
|
+
return new response_node_1.Response(body, { headers, status, statusText, url });
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
const status = 400;
|
|
32
|
+
const statusText = error.message;
|
|
33
|
+
const headers = {};
|
|
34
|
+
return new response_node_1.Response(error.message, { headers, status, statusText, url });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.fetchFileNode = fetchFileNode;
|
|
38
|
+
function getHeadersForFile(noqueryUrl) {
|
|
39
|
+
const headers = {};
|
|
40
|
+
// Fix up content length if we can for best progress experience
|
|
41
|
+
if (!headers['content-length']) {
|
|
42
|
+
const stats = fs_1.default.statSync(noqueryUrl);
|
|
43
|
+
headers['content-length'] = stats.size;
|
|
44
|
+
}
|
|
45
|
+
// Automatically decompress gzipped files with .gz extension
|
|
46
|
+
if (noqueryUrl.endsWith('.gz')) {
|
|
47
|
+
noqueryUrl = noqueryUrl.slice(0, -3);
|
|
48
|
+
headers['content-encoding'] = 'gzip';
|
|
49
|
+
}
|
|
50
|
+
return new headers_node_1.Headers(headers);
|
|
51
|
+
}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import http from 'http';
|
|
3
|
+
import { Response } from './response.node';
|
|
1
4
|
/**
|
|
2
5
|
* Emulation of Browser fetch for Node.js
|
|
3
6
|
* @param url
|
|
4
7
|
* @param options
|
|
5
8
|
*/
|
|
6
|
-
export
|
|
9
|
+
export declare function fetchNode(url: string, options: any): Promise<Response>;
|
|
10
|
+
/** Returns a promise that resolves to a readable stream */
|
|
11
|
+
export declare function createHTTPRequestReadStream(url: string, options: any): Promise<http.IncomingMessage>;
|
|
7
12
|
//# sourceMappingURL=fetch.node.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.node.d.ts","sourceRoot":"","sources":["../../../src/node/fetch/fetch.node.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetch.node.d.ts","sourceRoot":"","sources":["../../../src/node/fetch/fetch.node.ts"],"names":[],"mappings":";AAEA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAC,QAAQ,EAAC,MAAM,iBAAiB,CAAC;AASzC;;;;GAIG;AAEH,wBAAsB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,KAAA,GAAG,OAAO,CAAC,QAAQ,CAAC,CA6CvE;AAED,2DAA2D;AAC3D,wBAAsB,2BAA2B,CAC/C,GAAG,EAAE,MAAM,EACX,OAAO,KAAA,GACN,OAAO,CAAC,IAAI,CAAC,eAAe,CAAC,CAW/B"}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// loaders.gl, MIT license
|
|
2
3
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
5
|
};
|
|
5
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
7
|
+
exports.createHTTPRequestReadStream = exports.fetchNode = void 0;
|
|
8
|
+
const http_1 = __importDefault(require("http"));
|
|
9
|
+
const https_1 = __importDefault(require("https"));
|
|
10
|
+
const response_node_1 = require("./response.node");
|
|
11
|
+
const headers_node_1 = require("./headers.node");
|
|
9
12
|
const decode_data_uri_node_1 = require("./utils/decode-data-uri.node");
|
|
10
|
-
const
|
|
13
|
+
const fetch_file_node_1 = require("./fetch-file.node");
|
|
11
14
|
const isDataURL = (url) => url.startsWith('data:');
|
|
12
15
|
const isRequestURL = (url) => url.startsWith('http:') || url.startsWith('https:');
|
|
13
16
|
/**
|
|
@@ -15,13 +18,18 @@ const isRequestURL = (url) => url.startsWith('http:') || url.startsWith('https:'
|
|
|
15
18
|
* @param url
|
|
16
19
|
* @param options
|
|
17
20
|
*/
|
|
21
|
+
// eslint-disable-next-line complexity
|
|
18
22
|
async function fetchNode(url, options) {
|
|
19
23
|
try {
|
|
24
|
+
// Handle file streams in node
|
|
25
|
+
if (!isRequestURL(url) && !isDataURL(url)) {
|
|
26
|
+
return await (0, fetch_file_node_1.fetchFileNode)(url, options);
|
|
27
|
+
}
|
|
20
28
|
// Handle data urls in node, to match `fetch``
|
|
21
29
|
// Note - this loses the MIME type, data URIs are handled directly in fetch
|
|
22
30
|
if (isDataURL(url)) {
|
|
23
31
|
const { arrayBuffer, mimeType } = (0, decode_data_uri_node_1.decodeDataUri)(url);
|
|
24
|
-
const response = new response_node_1.
|
|
32
|
+
const response = new response_node_1.Response(arrayBuffer, {
|
|
25
33
|
headers: { 'content-type': mimeType },
|
|
26
34
|
url
|
|
27
35
|
});
|
|
@@ -35,7 +43,7 @@ async function fetchNode(url, options) {
|
|
|
35
43
|
syntheticResponseHeaders['content-encoding'] = 'gzip';
|
|
36
44
|
}
|
|
37
45
|
// Need to create the stream in advance since Response constructor needs to be sync
|
|
38
|
-
const body = await (
|
|
46
|
+
const body = await createHTTPRequestReadStream(originalUrl, options);
|
|
39
47
|
const headers = getHeaders(url, body, syntheticResponseHeaders);
|
|
40
48
|
const { status, statusText } = getStatus(body);
|
|
41
49
|
const followRedirect = !options || options.followRedirect || options.followRedirect === undefined;
|
|
@@ -44,14 +52,28 @@ async function fetchNode(url, options) {
|
|
|
44
52
|
// Redirect
|
|
45
53
|
return await fetchNode(redirectUrl, options);
|
|
46
54
|
}
|
|
47
|
-
return new response_node_1.
|
|
55
|
+
return new response_node_1.Response(body, { headers, status, statusText, url });
|
|
48
56
|
}
|
|
49
57
|
catch (error) {
|
|
50
58
|
// TODO - what error code to use here?
|
|
51
|
-
return new response_node_1.
|
|
59
|
+
return new response_node_1.Response(null, { status: 400, statusText: String(error), url });
|
|
52
60
|
}
|
|
53
61
|
}
|
|
54
|
-
exports.
|
|
62
|
+
exports.fetchNode = fetchNode;
|
|
63
|
+
/** Returns a promise that resolves to a readable stream */
|
|
64
|
+
async function createHTTPRequestReadStream(url, options) {
|
|
65
|
+
// HANDLE HTTP/HTTPS REQUESTS IN NODE
|
|
66
|
+
// TODO: THIS IS BAD SINCE WE RETURN A PROMISE INSTEAD OF A STREAM
|
|
67
|
+
return await new Promise((resolve, reject) => {
|
|
68
|
+
const requestOptions = getRequestOptions(url, options);
|
|
69
|
+
const req = url.startsWith('https:')
|
|
70
|
+
? https_1.default.request(requestOptions, (res) => resolve(res))
|
|
71
|
+
: http_1.default.request(requestOptions, (res) => resolve(res));
|
|
72
|
+
req.on('error', (error) => reject(error));
|
|
73
|
+
req.end();
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
exports.createHTTPRequestReadStream = createHTTPRequestReadStream;
|
|
55
77
|
/**
|
|
56
78
|
* Generate redirect url from location without origin and protocol.
|
|
57
79
|
* @param originalUrl
|
|
@@ -67,7 +89,28 @@ function generateRedirectUrl(originalUrl, location) {
|
|
|
67
89
|
return url.href;
|
|
68
90
|
}
|
|
69
91
|
// HELPER FUNCTIONS
|
|
70
|
-
|
|
92
|
+
function getRequestOptions(url, options) {
|
|
93
|
+
// Ensure header keys are lower case so that we can merge without duplicates
|
|
94
|
+
const originalHeaders = options?.headers || {};
|
|
95
|
+
const headers = {};
|
|
96
|
+
for (const key of Object.keys(originalHeaders)) {
|
|
97
|
+
headers[key.toLowerCase()] = originalHeaders[key];
|
|
98
|
+
}
|
|
99
|
+
// Add default accept-encoding to headers
|
|
100
|
+
headers['accept-encoding'] = headers['accept-encoding'] || 'gzip,br,deflate';
|
|
101
|
+
const urlObject = new URL(url);
|
|
102
|
+
return {
|
|
103
|
+
hostname: urlObject.hostname,
|
|
104
|
+
path: urlObject.pathname,
|
|
105
|
+
method: 'GET',
|
|
106
|
+
// Add options and user provided 'options.fetch' overrides if available
|
|
107
|
+
...options,
|
|
108
|
+
...options?.fetch,
|
|
109
|
+
// Override with updated headers with accepted encodings:
|
|
110
|
+
headers,
|
|
111
|
+
port: urlObject.port
|
|
112
|
+
};
|
|
113
|
+
}
|
|
71
114
|
function getStatus(httpResponse) {
|
|
72
115
|
if (httpResponse.statusCode) {
|
|
73
116
|
return { status: httpResponse.statusCode, statusText: httpResponse.statusMessage || 'NA' };
|
|
@@ -91,27 +134,10 @@ function getHeaders(url, httpResponse, additionalHeaders = {}) {
|
|
|
91
134
|
}
|
|
92
135
|
}
|
|
93
136
|
Object.assign(headers, additionalHeaders);
|
|
94
|
-
return new headers_node_1.
|
|
137
|
+
return new headers_node_1.Headers(headers);
|
|
95
138
|
}
|
|
139
|
+
/** Needs to be read from actual headers */
|
|
96
140
|
function getContentLength(url) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
else if (isDataURL(url)) {
|
|
102
|
-
// TODO - remove media type etc
|
|
103
|
-
return url.length - 'data:'.length;
|
|
104
|
-
}
|
|
105
|
-
// File URL
|
|
106
|
-
// TODO - how to handle non-existing file, this presumably just throws
|
|
107
|
-
try {
|
|
108
|
-
// strip query params from URL
|
|
109
|
-
const noqueryUrl = url.split('?')[0];
|
|
110
|
-
const stats = fs_1.default.statSync(noqueryUrl);
|
|
111
|
-
return stats.size;
|
|
112
|
-
}
|
|
113
|
-
catch (error) {
|
|
114
|
-
// ignore for now
|
|
115
|
-
}
|
|
116
|
-
return null;
|
|
141
|
+
// TODO - remove media type etc
|
|
142
|
+
return isDataURL(url) ? url.length - 'data:'.length : null;
|
|
117
143
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"headers.node.d.ts","sourceRoot":"","sources":["../../../src/node/fetch/headers.node.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,
|
|
1
|
+
{"version":3,"file":"headers.node.d.ts","sourceRoot":"","sources":["../../../src/node/fetch/headers.node.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,qBAAa,OAAO;IAClB,GAAG,EAAE,EAAE,CAAC;gBAEI,OAAO,KAAA;IAYnB,MAAM,CAAC,IAAI,KAAA,EAAE,KAAK,KAAA;IAOlB,MAAM,CAAC,IAAI,KAAA;IAIX,GAAG,CAAC,IAAI,KAAA;IAKR,GAAG,CAAC,IAAI,KAAA;IAIR,GAAG,CAAC,IAAI,KAAA,EAAE,KAAK,KAAA;IAIf,OAAO,CAAC,OAAO,KAAA,EAAE,OAAO,OAAO;IAY/B,IAAI;;;;;;IAQJ,MAAM;;;;;;IAQN,OAAO;;;;;;IAQN,CAAC,MAAM,CAAC,QAAQ,CAAC;CAInB"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Headers = void 0;
|
|
3
4
|
/**
|
|
4
5
|
* Polyfill for Browser Headers
|
|
5
6
|
* Based on https://github.com/github/fetch under MIT license
|
|
@@ -74,7 +75,7 @@ class Headers {
|
|
|
74
75
|
yield* this.entries();
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
|
-
exports.
|
|
78
|
+
exports.Headers = Headers;
|
|
78
79
|
function normalizeName(name) {
|
|
79
80
|
if (typeof name !== 'string') {
|
|
80
81
|
name = String(name);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response.node.d.ts","sourceRoot":"","sources":["../../../src/node/fetch/response.node.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"response.node.d.ts","sourceRoot":"","sources":["../../../src/node/fetch/response.node.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,OAAO,EAAC,MAAM,gBAAgB,CAAC;AAoBvC,qBAAa,QAAQ;IACnB,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAC;IACrB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,OAAO,CAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC;gBAIrB,IAAI,KAAA,EACJ,OAAO,EAAE;QACP,OAAO,CAAC,MAAC;QACT,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,GAAG,EAAE,MAAM,CAAC;KACb;IAuBH,IAAI,IAAI,QAKP;IAIK,WAAW;IAQX,IAAI;IAMJ,IAAI;IAKJ,IAAI;CAMX"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
2
|
+
// loaders.gl, MIT license
|
|
5
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Response = void 0;
|
|
6
5
|
const assert_1 = require("../../utils/assert");
|
|
7
6
|
const stream_utils_node_1 = require("./utils/stream-utils.node");
|
|
8
|
-
const headers_node_1 =
|
|
7
|
+
const headers_node_1 = require("./headers.node");
|
|
9
8
|
const isBoolean = (x) => typeof x === 'boolean';
|
|
10
9
|
const isFunction = (x) => typeof x === 'function';
|
|
11
10
|
const isObject = (x) => x !== null && typeof x === 'object';
|
|
@@ -30,7 +29,7 @@ class Response {
|
|
|
30
29
|
this.ok = status === 200;
|
|
31
30
|
this.status = status; // TODO - handle errors and set status
|
|
32
31
|
this.statusText = statusText;
|
|
33
|
-
this.headers = new headers_node_1.
|
|
32
|
+
this.headers = new headers_node_1.Headers(options?.headers || {});
|
|
34
33
|
// Check for content-encoding and create a decompression stream
|
|
35
34
|
if (isReadableNodeStream(body)) {
|
|
36
35
|
this._body = (0, stream_utils_node_1.decompressReadStream)(body, headers);
|
|
@@ -74,4 +73,4 @@ class Response {
|
|
|
74
73
|
return new Blob([await this.arrayBuffer()]);
|
|
75
74
|
}
|
|
76
75
|
}
|
|
77
|
-
exports.
|
|
76
|
+
exports.Response = Response;
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
*/
|
|
2
4
|
export declare function decompressReadStream(readStream: any, headers: any): any;
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param readStream
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
3
10
|
export declare function concatenateReadStream(readStream: any): Promise<ArrayBuffer>;
|
|
4
11
|
/**
|
|
5
12
|
* Concatenate a sequence of ArrayBuffers
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-utils.node.d.ts","sourceRoot":"","sources":["../../../../src/node/fetch/utils/stream-utils.node.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stream-utils.node.d.ts","sourceRoot":"","sources":["../../../../src/node/fetch/utils/stream-utils.node.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,UAAU,KAAA,EAAE,OAAO,KAAA,OAYvD;AAED;;;;GAIG;AACH,wBAAsB,qBAAqB,CAAC,UAAU,KAAA,GAAG,OAAO,CAAC,WAAW,CAAC,CAsB5E;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,CAAC,WAAW,GAAG,UAAU,CAAC,EAAE,GAAG,WAAW,CAqB1F"}
|
|
@@ -1,41 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// loaders.gl, MIT license
|
|
2
3
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
5
|
};
|
|
5
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.concatenateArrayBuffers = exports.concatenateReadStream = exports.decompressReadStream =
|
|
7
|
-
const fs_1 = __importDefault(require("fs")); // `fs` will be empty object in browsers (see package.json "browser" field).
|
|
8
|
-
const http_1 = __importDefault(require("http"));
|
|
9
|
-
const https_1 = __importDefault(require("https"));
|
|
7
|
+
exports.concatenateArrayBuffers = exports.concatenateReadStream = exports.decompressReadStream = void 0;
|
|
10
8
|
const zlib_1 = __importDefault(require("zlib"));
|
|
11
9
|
const decode_data_uri_node_1 = require("./decode-data-uri.node");
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// Returns a promise that resolves to a readable stream
|
|
16
|
-
async function createReadStream(url, options) {
|
|
17
|
-
// Handle file streams in node
|
|
18
|
-
if (!isRequestURL(url)) {
|
|
19
|
-
const noqueryUrl = url.split('?')[0];
|
|
20
|
-
// Now open the stream
|
|
21
|
-
return await new Promise((resolve, reject) => {
|
|
22
|
-
// @ts-ignore
|
|
23
|
-
const stream = fs_1.default.createReadStream(noqueryUrl, { encoding: null });
|
|
24
|
-
stream.once('readable', () => resolve(stream));
|
|
25
|
-
stream.on('error', (error) => reject(error));
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
// HANDLE HTTP/HTTPS REQUESTS IN NODE
|
|
29
|
-
// TODO: THIS IS BAD SINCE WE RETURN A PROMISE INSTEAD OF A STREAM
|
|
30
|
-
return await new Promise((resolve, reject) => {
|
|
31
|
-
const requestFunction = url.startsWith('https:') ? https_1.default.request : http_1.default.request;
|
|
32
|
-
const requestOptions = getRequestOptions(url, options);
|
|
33
|
-
const req = requestFunction(requestOptions, (res) => resolve(res));
|
|
34
|
-
req.on('error', (error) => reject(error));
|
|
35
|
-
req.end();
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
exports.createReadStream = createReadStream;
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
39
13
|
function decompressReadStream(readStream, headers) {
|
|
40
14
|
switch (headers.get('content-encoding')) {
|
|
41
15
|
case 'br':
|
|
@@ -50,6 +24,11 @@ function decompressReadStream(readStream, headers) {
|
|
|
50
24
|
}
|
|
51
25
|
}
|
|
52
26
|
exports.decompressReadStream = decompressReadStream;
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
* @param readStream
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
53
32
|
async function concatenateReadStream(readStream) {
|
|
54
33
|
const arrayBufferChunks = [];
|
|
55
34
|
return await new Promise((resolve, reject) => {
|
|
@@ -70,29 +49,6 @@ async function concatenateReadStream(readStream) {
|
|
|
70
49
|
});
|
|
71
50
|
}
|
|
72
51
|
exports.concatenateReadStream = concatenateReadStream;
|
|
73
|
-
// HELPERS
|
|
74
|
-
function getRequestOptions(url, options) {
|
|
75
|
-
// Ensure header keys are lower case so that we can merge without duplicates
|
|
76
|
-
const originalHeaders = options?.headers || {};
|
|
77
|
-
const headers = {};
|
|
78
|
-
for (const key of Object.keys(originalHeaders)) {
|
|
79
|
-
headers[key.toLowerCase()] = originalHeaders[key];
|
|
80
|
-
}
|
|
81
|
-
// Add default accept-encoding to headers
|
|
82
|
-
headers['accept-encoding'] = headers['accept-encoding'] || 'gzip,br,deflate';
|
|
83
|
-
const urlObject = new URL(url);
|
|
84
|
-
return {
|
|
85
|
-
hostname: urlObject.hostname,
|
|
86
|
-
path: urlObject.pathname,
|
|
87
|
-
method: 'GET',
|
|
88
|
-
// Add options and user provided 'options.fetch' overrides if available
|
|
89
|
-
...options,
|
|
90
|
-
...options?.fetch,
|
|
91
|
-
// Override with updated headers with accepted encodings:
|
|
92
|
-
headers,
|
|
93
|
-
port: urlObject.port
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
52
|
/**
|
|
97
53
|
* Concatenate a sequence of ArrayBuffers
|
|
98
54
|
* @return A concatenated ArrayBuffer
|