@whatwg-node/node-fetch 0.0.1-alpha-20221005124650-55319ef → 0.0.1-alpha-20221225155445-dfcb269
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/AbortError.d.ts +2 -1
- package/AbortSignal.d.ts +1 -0
- package/Blob.d.ts +9 -4
- package/Body.d.ts +2 -2
- package/Headers.d.ts +1 -1
- package/ReadableStream.d.ts +3 -0
- package/Request.d.ts +3 -3
- package/Response.d.ts +4 -3
- package/TextEncoderDecoder.d.ts +15 -0
- package/index.d.ts +2 -0
- package/index.js +149 -70
- package/index.mjs +145 -70
- package/package.json +2 -1
package/AbortError.d.ts
CHANGED
package/AbortSignal.d.ts
CHANGED
package/Blob.d.ts
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
/// <reference types="node" />
|
2
|
+
import { Blob as NodeBlob } from 'buffer';
|
3
|
+
export declare class PonyfillBlob extends NodeBlob implements Blob {
|
4
|
+
stream(): any;
|
5
|
+
slice(...args: any[]): any;
|
6
|
+
}
|
7
|
+
export interface PonyfillBlob {
|
3
8
|
prototype: Blob;
|
4
|
-
|
5
|
-
|
9
|
+
new (blobParts?: BlobPart[], options?: BlobPropertyBag): Blob;
|
10
|
+
}
|
package/Body.d.ts
CHANGED
@@ -3,8 +3,8 @@ import { PonyfillBlob } from './Blob';
|
|
3
3
|
import { Readable } from 'stream';
|
4
4
|
import { PonyfillFormData } from './FormData';
|
5
5
|
import { PonyfillReadableStream } from './ReadableStream';
|
6
|
-
export
|
7
|
-
export declare class
|
6
|
+
export type BodyPonyfillInit = XMLHttpRequestBodyInit | Readable | PonyfillReadableStream<Uint8Array>;
|
7
|
+
export declare class PonyfillBody implements Body {
|
8
8
|
private bodyInit;
|
9
9
|
bodyUsed: boolean;
|
10
10
|
contentType: string | null;
|
package/Headers.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
export
|
1
|
+
export type PonyfillHeadersInit = [string, string][] | Record<string, string | string[] | undefined> | Headers;
|
2
2
|
export declare class PonyfillHeaders implements Headers {
|
3
3
|
private map;
|
4
4
|
constructor(headersInit?: PonyfillHeadersInit);
|
package/ReadableStream.d.ts
CHANGED
@@ -5,6 +5,9 @@ export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
|
|
5
5
|
constructor(underlyingSource?: UnderlyingSource<T> | Readable | ReadableStream<T> | PonyfillReadableStream<T>);
|
6
6
|
cancel(reason?: any): Promise<void>;
|
7
7
|
locked: boolean;
|
8
|
+
getReader(options: {
|
9
|
+
mode: "byob";
|
10
|
+
}): ReadableStreamBYOBReader;
|
8
11
|
getReader(): ReadableStreamDefaultReader<T>;
|
9
12
|
[Symbol.asyncIterator](): AsyncIterableIterator<any>;
|
10
13
|
tee(): [ReadableStream<T>, ReadableStream<T>];
|
package/Request.d.ts
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
import {
|
1
|
+
import { PonyfillBody, BodyPonyfillInit } from './Body';
|
2
2
|
import { PonyfillHeadersInit } from './Headers';
|
3
|
-
export
|
3
|
+
export type RequestPonyfillInit = Omit<RequestInit, 'body' | 'headers'> & {
|
4
4
|
body?: BodyPonyfillInit | null;
|
5
5
|
headers?: PonyfillHeadersInit;
|
6
6
|
};
|
7
|
-
export declare class PonyfillRequest extends
|
7
|
+
export declare class PonyfillRequest extends PonyfillBody implements Request {
|
8
8
|
constructor(input: RequestInfo | URL, options?: RequestPonyfillInit);
|
9
9
|
get body(): import("./ReadableStream").PonyfillReadableStream<Uint8Array> | null;
|
10
10
|
private postProcessedBody;
|
package/Response.d.ts
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
import {
|
1
|
+
import { PonyfillBody, BodyPonyfillInit } from './Body';
|
2
2
|
import { PonyfillHeadersInit } from './Headers';
|
3
|
-
export
|
3
|
+
export type ResponsePonyfilInit = Omit<ResponseInit, 'headers'> & {
|
4
4
|
url?: string;
|
5
5
|
redirected?: boolean;
|
6
6
|
headers?: PonyfillHeadersInit;
|
7
7
|
};
|
8
|
-
export declare class PonyfillResponse extends
|
8
|
+
export declare class PonyfillResponse extends PonyfillBody implements Response {
|
9
9
|
constructor(body?: BodyPonyfillInit | null, init?: ResponsePonyfilInit);
|
10
10
|
headers: Headers;
|
11
11
|
get ok(): boolean;
|
@@ -17,4 +17,5 @@ export declare class PonyfillResponse extends BodyPonyfill implements Response {
|
|
17
17
|
clone(): PonyfillResponse;
|
18
18
|
static error(): PonyfillResponse;
|
19
19
|
static redirect(url: string, status?: number): PonyfillResponse;
|
20
|
+
static json(data: any, init?: RequestInit): PonyfillResponse;
|
20
21
|
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
export declare class PonyfillTextEncoder implements TextEncoder {
|
3
|
+
encoding: BufferEncoding;
|
4
|
+
constructor(encoding?: BufferEncoding);
|
5
|
+
encode(input: string): Buffer;
|
6
|
+
encodeInto(source: string, destination: Uint8Array): TextEncoderEncodeIntoResult;
|
7
|
+
}
|
8
|
+
export declare class PonyfillTextDecoder implements TextDecoder {
|
9
|
+
encoding: BufferEncoding;
|
10
|
+
fatal: boolean;
|
11
|
+
ignoreBOM: boolean;
|
12
|
+
constructor(encoding: BufferEncoding, options: TextDecoderOptions);
|
13
|
+
decode(input: Uint8Array): string;
|
14
|
+
}
|
15
|
+
export declare function PonyfillBtoa(input: string): string;
|
package/index.d.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
export { fetchPonyfill as fetch } from './fetch';
|
2
2
|
export { PonyfillHeaders as Headers } from './Headers';
|
3
|
+
export { PonyfillBody as Body } from './Body';
|
3
4
|
export { PonyfillRequest as Request, RequestPonyfillInit as RequestInit } from './Request';
|
4
5
|
export { PonyfillResponse as Response, ResponsePonyfilInit as ResponseInit } from './Response';
|
5
6
|
export { PonyfillReadableStream as ReadableStream } from './ReadableStream';
|
@@ -9,3 +10,4 @@ export { PonyfillAbortController as AbortController } from './AbortController';
|
|
9
10
|
export { PonyfillAbortSignal as AbortSignal } from './AbortSignal';
|
10
11
|
export { PonyfillAbortError as AbortError } from './AbortError';
|
11
12
|
export { PonyfillBlob as Blob } from './Blob';
|
13
|
+
export { PonyfillTextEncoder as TextEncoder, PonyfillTextDecoder as TextDecoder, PonyfillBtoa as btoa, } from './TextEncoderDecoder';
|
package/index.js
CHANGED
@@ -4,17 +4,26 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
5
5
|
const http = require('http');
|
6
6
|
const https = require('https');
|
7
|
+
const events = require('@whatwg-node/events');
|
7
8
|
const buffer = require('buffer');
|
8
9
|
const stream = require('stream');
|
10
|
+
const url = require('url');
|
11
|
+
const fs = require('fs');
|
9
12
|
|
13
|
+
// Will be removed after v14 reaches EOL
|
10
14
|
class PonyfillAbortError extends Error {
|
11
|
-
constructor() {
|
12
|
-
super('The operation was aborted.'
|
15
|
+
constructor(reason) {
|
16
|
+
super('The operation was aborted.', {
|
17
|
+
cause: reason,
|
18
|
+
});
|
13
19
|
this.name = 'AbortError';
|
14
20
|
}
|
21
|
+
get reason() {
|
22
|
+
return this.cause;
|
23
|
+
}
|
15
24
|
}
|
16
25
|
|
17
|
-
class PonyfillAbortSignal extends EventTarget {
|
26
|
+
class PonyfillAbortSignal extends events.EventTarget {
|
18
27
|
constructor() {
|
19
28
|
super(...arguments);
|
20
29
|
this.aborted = false;
|
@@ -34,71 +43,13 @@ class PonyfillAbortSignal extends EventTarget {
|
|
34
43
|
}
|
35
44
|
}
|
36
45
|
|
46
|
+
// Will be removed after v14 reaches EOL
|
37
47
|
class PonyfillAbortController {
|
38
48
|
constructor() {
|
39
49
|
this.signal = new PonyfillAbortSignal();
|
40
50
|
}
|
41
51
|
abort(reason) {
|
42
|
-
this.signal.dispatchEvent(
|
43
|
-
}
|
44
|
-
}
|
45
|
-
|
46
|
-
const PonyfillBlob = buffer.Blob;
|
47
|
-
|
48
|
-
class PonyfillFile extends PonyfillBlob {
|
49
|
-
constructor(fileBits, name, options) {
|
50
|
-
super(fileBits, options);
|
51
|
-
this.name = name;
|
52
|
-
this.webkitRelativePath = '';
|
53
|
-
this.lastModified = (options === null || options === void 0 ? void 0 : options.lastModified) || Date.now();
|
54
|
-
}
|
55
|
-
}
|
56
|
-
|
57
|
-
class PonyfillFormData {
|
58
|
-
constructor() {
|
59
|
-
this.map = new Map();
|
60
|
-
}
|
61
|
-
append(name, value, fileName) {
|
62
|
-
let values = this.map.get(name);
|
63
|
-
if (!values) {
|
64
|
-
values = [];
|
65
|
-
this.map.set(name, values);
|
66
|
-
}
|
67
|
-
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
|
68
|
-
values.push(entry);
|
69
|
-
}
|
70
|
-
delete(name) {
|
71
|
-
this.map.delete(name);
|
72
|
-
}
|
73
|
-
get(name) {
|
74
|
-
const values = this.map.get(name);
|
75
|
-
return values ? values[0] : null;
|
76
|
-
}
|
77
|
-
getAll(name) {
|
78
|
-
return this.map.get(name) || [];
|
79
|
-
}
|
80
|
-
has(name) {
|
81
|
-
return this.map.has(name);
|
82
|
-
}
|
83
|
-
set(name, value, fileName) {
|
84
|
-
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
|
85
|
-
this.map.set(name, [entry]);
|
86
|
-
}
|
87
|
-
getNormalizedFile(name, blob, fileName) {
|
88
|
-
if (blob instanceof PonyfillFile) {
|
89
|
-
if (fileName == null) {
|
90
|
-
return new PonyfillFile([blob], name, { type: blob.type, lastModified: blob.lastModified });
|
91
|
-
}
|
92
|
-
return blob;
|
93
|
-
}
|
94
|
-
return new PonyfillFile([blob], fileName || name, { type: blob.type });
|
95
|
-
}
|
96
|
-
forEach(callback) {
|
97
|
-
for (const [key, values] of this.map) {
|
98
|
-
for (const value of values) {
|
99
|
-
callback(value, key, this);
|
100
|
-
}
|
101
|
-
}
|
52
|
+
this.signal.dispatchEvent(new events.CustomEvent('abort', { detail: reason }));
|
102
53
|
}
|
103
54
|
}
|
104
55
|
|
@@ -176,7 +127,7 @@ class PonyfillReadableStream {
|
|
176
127
|
this.readable.destroy(reason);
|
177
128
|
return Promise.resolve();
|
178
129
|
}
|
179
|
-
getReader() {
|
130
|
+
getReader(_options) {
|
180
131
|
const iterator = this.readable[Symbol.asyncIterator]();
|
181
132
|
return {
|
182
133
|
read() {
|
@@ -219,6 +170,81 @@ class PonyfillReadableStream {
|
|
219
170
|
}
|
220
171
|
}
|
221
172
|
|
173
|
+
// Will be removed after v14 reaches EOL
|
174
|
+
// Needed because v14 doesn't have .stream() implemented
|
175
|
+
class PonyfillBlob extends buffer.Blob {
|
176
|
+
stream() {
|
177
|
+
return new PonyfillReadableStream({
|
178
|
+
start: async (controller) => {
|
179
|
+
const arrayBuffer = await this.arrayBuffer();
|
180
|
+
const buffer = Buffer.from(arrayBuffer);
|
181
|
+
controller.enqueue(buffer);
|
182
|
+
controller.close();
|
183
|
+
},
|
184
|
+
});
|
185
|
+
}
|
186
|
+
slice(...args) {
|
187
|
+
return super.slice(...args);
|
188
|
+
}
|
189
|
+
}
|
190
|
+
|
191
|
+
class PonyfillFile extends PonyfillBlob {
|
192
|
+
constructor(fileBits, name, options) {
|
193
|
+
super(fileBits, options);
|
194
|
+
this.name = name;
|
195
|
+
this.webkitRelativePath = '';
|
196
|
+
this.lastModified = (options === null || options === void 0 ? void 0 : options.lastModified) || Date.now();
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
class PonyfillFormData {
|
201
|
+
constructor() {
|
202
|
+
this.map = new Map();
|
203
|
+
}
|
204
|
+
append(name, value, fileName) {
|
205
|
+
let values = this.map.get(name);
|
206
|
+
if (!values) {
|
207
|
+
values = [];
|
208
|
+
this.map.set(name, values);
|
209
|
+
}
|
210
|
+
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
|
211
|
+
values.push(entry);
|
212
|
+
}
|
213
|
+
delete(name) {
|
214
|
+
this.map.delete(name);
|
215
|
+
}
|
216
|
+
get(name) {
|
217
|
+
const values = this.map.get(name);
|
218
|
+
return values ? values[0] : null;
|
219
|
+
}
|
220
|
+
getAll(name) {
|
221
|
+
return this.map.get(name) || [];
|
222
|
+
}
|
223
|
+
has(name) {
|
224
|
+
return this.map.has(name);
|
225
|
+
}
|
226
|
+
set(name, value, fileName) {
|
227
|
+
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
|
228
|
+
this.map.set(name, [entry]);
|
229
|
+
}
|
230
|
+
getNormalizedFile(name, blob, fileName) {
|
231
|
+
if (blob instanceof PonyfillFile) {
|
232
|
+
if (fileName != null) {
|
233
|
+
return new PonyfillFile([blob], fileName, { type: blob.type, lastModified: blob.lastModified });
|
234
|
+
}
|
235
|
+
return blob;
|
236
|
+
}
|
237
|
+
return new PonyfillFile([blob], fileName || name, { type: blob.type });
|
238
|
+
}
|
239
|
+
forEach(callback) {
|
240
|
+
for (const [key, values] of this.map) {
|
241
|
+
for (const value of values) {
|
242
|
+
callback(value, key, this);
|
243
|
+
}
|
244
|
+
}
|
245
|
+
}
|
246
|
+
}
|
247
|
+
|
222
248
|
var BodyInitType;
|
223
249
|
(function (BodyInitType) {
|
224
250
|
BodyInitType["ReadableStream"] = "ReadableStream";
|
@@ -228,7 +254,7 @@ var BodyInitType;
|
|
228
254
|
BodyInitType["String"] = "String";
|
229
255
|
BodyInitType["Readable"] = "Readable";
|
230
256
|
})(BodyInitType || (BodyInitType = {}));
|
231
|
-
class
|
257
|
+
class PonyfillBody {
|
232
258
|
constructor(bodyInit) {
|
233
259
|
this.bodyInit = bodyInit;
|
234
260
|
this.bodyUsed = false;
|
@@ -439,7 +465,7 @@ class PonyfillHeaders {
|
|
439
465
|
function isRequest(input) {
|
440
466
|
return input[Symbol.toStringTag] === 'Request';
|
441
467
|
}
|
442
|
-
class PonyfillRequest extends
|
468
|
+
class PonyfillRequest extends PonyfillBody {
|
443
469
|
constructor(input, options) {
|
444
470
|
let url;
|
445
471
|
let bodyInit = null;
|
@@ -505,7 +531,7 @@ class PonyfillRequest extends BodyPonyfill {
|
|
505
531
|
}
|
506
532
|
}
|
507
533
|
|
508
|
-
class PonyfillResponse extends
|
534
|
+
class PonyfillResponse extends PonyfillBody {
|
509
535
|
constructor(body, init) {
|
510
536
|
super(body || null);
|
511
537
|
this.headers = new PonyfillHeaders();
|
@@ -546,6 +572,15 @@ class PonyfillResponse extends BodyPonyfill {
|
|
546
572
|
status,
|
547
573
|
});
|
548
574
|
}
|
575
|
+
static json(data, init = {}) {
|
576
|
+
return new PonyfillResponse(JSON.stringify(data), {
|
577
|
+
...init,
|
578
|
+
headers: {
|
579
|
+
"Content-Type": "application/json",
|
580
|
+
...init === null || init === void 0 ? void 0 : init.headers,
|
581
|
+
},
|
582
|
+
});
|
583
|
+
}
|
549
584
|
}
|
550
585
|
|
551
586
|
function getHeadersObj(headers) {
|
@@ -566,17 +601,22 @@ const fetchPonyfill = (info, init) => {
|
|
566
601
|
const fetchRequest = info;
|
567
602
|
return new Promise((resolve, reject) => {
|
568
603
|
try {
|
604
|
+
if (fetchRequest.url.startsWith('file://')) {
|
605
|
+
resolve(new PonyfillResponse(fs.createReadStream(url.fileURLToPath(fetchRequest.url))));
|
606
|
+
return;
|
607
|
+
}
|
569
608
|
const requestFn = fetchRequest.url.startsWith('https') ? https.request : http.request;
|
570
609
|
const nodeReadable = fetchRequest.readable();
|
571
610
|
const nodeHeaders = getHeadersObj(fetchRequest.headers);
|
572
|
-
const abortListener = function abortListener() {
|
573
|
-
|
611
|
+
const abortListener = function abortListener(event) {
|
612
|
+
nodeRequest.destroy();
|
613
|
+
reject(new PonyfillAbortError(event.detail));
|
574
614
|
};
|
575
615
|
fetchRequest.signal.addEventListener('abort', abortListener);
|
576
616
|
const nodeRequest = requestFn(fetchRequest.url, {
|
617
|
+
// signal: fetchRequest.signal will be added when v14 reaches EOL
|
577
618
|
method: fetchRequest.method,
|
578
619
|
headers: nodeHeaders,
|
579
|
-
signal: fetchRequest.signal,
|
580
620
|
}, nodeResponse => {
|
581
621
|
if (nodeResponse.headers.location) {
|
582
622
|
if (fetchRequest.redirect === 'error') {
|
@@ -601,6 +641,7 @@ const fetchPonyfill = (info, init) => {
|
|
601
641
|
url: info.url,
|
602
642
|
}));
|
603
643
|
});
|
644
|
+
nodeRequest.on('error', reject);
|
604
645
|
if (nodeReadable) {
|
605
646
|
nodeReadable.pipe(nodeRequest);
|
606
647
|
}
|
@@ -614,14 +655,52 @@ const fetchPonyfill = (info, init) => {
|
|
614
655
|
});
|
615
656
|
};
|
616
657
|
|
658
|
+
class PonyfillTextEncoder {
|
659
|
+
constructor(encoding = 'utf-8') {
|
660
|
+
this.encoding = encoding;
|
661
|
+
}
|
662
|
+
encode(input) {
|
663
|
+
return Buffer.from(input, this.encoding);
|
664
|
+
}
|
665
|
+
encodeInto(source, destination) {
|
666
|
+
const buffer = this.encode(source);
|
667
|
+
const copied = buffer.copy(destination);
|
668
|
+
return {
|
669
|
+
read: copied,
|
670
|
+
written: copied,
|
671
|
+
};
|
672
|
+
}
|
673
|
+
}
|
674
|
+
class PonyfillTextDecoder {
|
675
|
+
constructor(encoding = 'utf-8', options) {
|
676
|
+
this.encoding = encoding;
|
677
|
+
this.fatal = false;
|
678
|
+
this.ignoreBOM = false;
|
679
|
+
if (options) {
|
680
|
+
this.fatal = options.fatal || false;
|
681
|
+
this.ignoreBOM = options.ignoreBOM || false;
|
682
|
+
}
|
683
|
+
}
|
684
|
+
decode(input) {
|
685
|
+
return Buffer.from(input).toString(this.encoding);
|
686
|
+
}
|
687
|
+
}
|
688
|
+
function PonyfillBtoa(input) {
|
689
|
+
return Buffer.from(input, 'binary').toString('base64');
|
690
|
+
}
|
691
|
+
|
617
692
|
exports.AbortController = PonyfillAbortController;
|
618
693
|
exports.AbortError = PonyfillAbortError;
|
619
694
|
exports.AbortSignal = PonyfillAbortSignal;
|
620
695
|
exports.Blob = PonyfillBlob;
|
696
|
+
exports.Body = PonyfillBody;
|
621
697
|
exports.File = PonyfillFile;
|
622
698
|
exports.FormData = PonyfillFormData;
|
623
699
|
exports.Headers = PonyfillHeaders;
|
624
700
|
exports.ReadableStream = PonyfillReadableStream;
|
625
701
|
exports.Request = PonyfillRequest;
|
626
702
|
exports.Response = PonyfillResponse;
|
703
|
+
exports.TextDecoder = PonyfillTextDecoder;
|
704
|
+
exports.TextEncoder = PonyfillTextEncoder;
|
705
|
+
exports.btoa = PonyfillBtoa;
|
627
706
|
exports.fetch = fetchPonyfill;
|
package/index.mjs
CHANGED
@@ -1,13 +1,22 @@
|
|
1
1
|
import { request as request$1 } from 'http';
|
2
2
|
import { request } from 'https';
|
3
|
+
import { EventTarget, CustomEvent } from '@whatwg-node/events';
|
3
4
|
import { Blob } from 'buffer';
|
4
5
|
import { Readable } from 'stream';
|
6
|
+
import { fileURLToPath } from 'url';
|
7
|
+
import { createReadStream } from 'fs';
|
5
8
|
|
9
|
+
// Will be removed after v14 reaches EOL
|
6
10
|
class PonyfillAbortError extends Error {
|
7
|
-
constructor() {
|
8
|
-
super('The operation was aborted.'
|
11
|
+
constructor(reason) {
|
12
|
+
super('The operation was aborted.', {
|
13
|
+
cause: reason,
|
14
|
+
});
|
9
15
|
this.name = 'AbortError';
|
10
16
|
}
|
17
|
+
get reason() {
|
18
|
+
return this.cause;
|
19
|
+
}
|
11
20
|
}
|
12
21
|
|
13
22
|
class PonyfillAbortSignal extends EventTarget {
|
@@ -30,71 +39,13 @@ class PonyfillAbortSignal extends EventTarget {
|
|
30
39
|
}
|
31
40
|
}
|
32
41
|
|
42
|
+
// Will be removed after v14 reaches EOL
|
33
43
|
class PonyfillAbortController {
|
34
44
|
constructor() {
|
35
45
|
this.signal = new PonyfillAbortSignal();
|
36
46
|
}
|
37
47
|
abort(reason) {
|
38
|
-
this.signal.dispatchEvent(
|
39
|
-
}
|
40
|
-
}
|
41
|
-
|
42
|
-
const PonyfillBlob = Blob;
|
43
|
-
|
44
|
-
class PonyfillFile extends PonyfillBlob {
|
45
|
-
constructor(fileBits, name, options) {
|
46
|
-
super(fileBits, options);
|
47
|
-
this.name = name;
|
48
|
-
this.webkitRelativePath = '';
|
49
|
-
this.lastModified = (options === null || options === void 0 ? void 0 : options.lastModified) || Date.now();
|
50
|
-
}
|
51
|
-
}
|
52
|
-
|
53
|
-
class PonyfillFormData {
|
54
|
-
constructor() {
|
55
|
-
this.map = new Map();
|
56
|
-
}
|
57
|
-
append(name, value, fileName) {
|
58
|
-
let values = this.map.get(name);
|
59
|
-
if (!values) {
|
60
|
-
values = [];
|
61
|
-
this.map.set(name, values);
|
62
|
-
}
|
63
|
-
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
|
64
|
-
values.push(entry);
|
65
|
-
}
|
66
|
-
delete(name) {
|
67
|
-
this.map.delete(name);
|
68
|
-
}
|
69
|
-
get(name) {
|
70
|
-
const values = this.map.get(name);
|
71
|
-
return values ? values[0] : null;
|
72
|
-
}
|
73
|
-
getAll(name) {
|
74
|
-
return this.map.get(name) || [];
|
75
|
-
}
|
76
|
-
has(name) {
|
77
|
-
return this.map.has(name);
|
78
|
-
}
|
79
|
-
set(name, value, fileName) {
|
80
|
-
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
|
81
|
-
this.map.set(name, [entry]);
|
82
|
-
}
|
83
|
-
getNormalizedFile(name, blob, fileName) {
|
84
|
-
if (blob instanceof PonyfillFile) {
|
85
|
-
if (fileName == null) {
|
86
|
-
return new PonyfillFile([blob], name, { type: blob.type, lastModified: blob.lastModified });
|
87
|
-
}
|
88
|
-
return blob;
|
89
|
-
}
|
90
|
-
return new PonyfillFile([blob], fileName || name, { type: blob.type });
|
91
|
-
}
|
92
|
-
forEach(callback) {
|
93
|
-
for (const [key, values] of this.map) {
|
94
|
-
for (const value of values) {
|
95
|
-
callback(value, key, this);
|
96
|
-
}
|
97
|
-
}
|
48
|
+
this.signal.dispatchEvent(new CustomEvent('abort', { detail: reason }));
|
98
49
|
}
|
99
50
|
}
|
100
51
|
|
@@ -172,7 +123,7 @@ class PonyfillReadableStream {
|
|
172
123
|
this.readable.destroy(reason);
|
173
124
|
return Promise.resolve();
|
174
125
|
}
|
175
|
-
getReader() {
|
126
|
+
getReader(_options) {
|
176
127
|
const iterator = this.readable[Symbol.asyncIterator]();
|
177
128
|
return {
|
178
129
|
read() {
|
@@ -215,6 +166,81 @@ class PonyfillReadableStream {
|
|
215
166
|
}
|
216
167
|
}
|
217
168
|
|
169
|
+
// Will be removed after v14 reaches EOL
|
170
|
+
// Needed because v14 doesn't have .stream() implemented
|
171
|
+
class PonyfillBlob extends Blob {
|
172
|
+
stream() {
|
173
|
+
return new PonyfillReadableStream({
|
174
|
+
start: async (controller) => {
|
175
|
+
const arrayBuffer = await this.arrayBuffer();
|
176
|
+
const buffer = Buffer.from(arrayBuffer);
|
177
|
+
controller.enqueue(buffer);
|
178
|
+
controller.close();
|
179
|
+
},
|
180
|
+
});
|
181
|
+
}
|
182
|
+
slice(...args) {
|
183
|
+
return super.slice(...args);
|
184
|
+
}
|
185
|
+
}
|
186
|
+
|
187
|
+
class PonyfillFile extends PonyfillBlob {
|
188
|
+
constructor(fileBits, name, options) {
|
189
|
+
super(fileBits, options);
|
190
|
+
this.name = name;
|
191
|
+
this.webkitRelativePath = '';
|
192
|
+
this.lastModified = (options === null || options === void 0 ? void 0 : options.lastModified) || Date.now();
|
193
|
+
}
|
194
|
+
}
|
195
|
+
|
196
|
+
class PonyfillFormData {
|
197
|
+
constructor() {
|
198
|
+
this.map = new Map();
|
199
|
+
}
|
200
|
+
append(name, value, fileName) {
|
201
|
+
let values = this.map.get(name);
|
202
|
+
if (!values) {
|
203
|
+
values = [];
|
204
|
+
this.map.set(name, values);
|
205
|
+
}
|
206
|
+
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
|
207
|
+
values.push(entry);
|
208
|
+
}
|
209
|
+
delete(name) {
|
210
|
+
this.map.delete(name);
|
211
|
+
}
|
212
|
+
get(name) {
|
213
|
+
const values = this.map.get(name);
|
214
|
+
return values ? values[0] : null;
|
215
|
+
}
|
216
|
+
getAll(name) {
|
217
|
+
return this.map.get(name) || [];
|
218
|
+
}
|
219
|
+
has(name) {
|
220
|
+
return this.map.has(name);
|
221
|
+
}
|
222
|
+
set(name, value, fileName) {
|
223
|
+
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
|
224
|
+
this.map.set(name, [entry]);
|
225
|
+
}
|
226
|
+
getNormalizedFile(name, blob, fileName) {
|
227
|
+
if (blob instanceof PonyfillFile) {
|
228
|
+
if (fileName != null) {
|
229
|
+
return new PonyfillFile([blob], fileName, { type: blob.type, lastModified: blob.lastModified });
|
230
|
+
}
|
231
|
+
return blob;
|
232
|
+
}
|
233
|
+
return new PonyfillFile([blob], fileName || name, { type: blob.type });
|
234
|
+
}
|
235
|
+
forEach(callback) {
|
236
|
+
for (const [key, values] of this.map) {
|
237
|
+
for (const value of values) {
|
238
|
+
callback(value, key, this);
|
239
|
+
}
|
240
|
+
}
|
241
|
+
}
|
242
|
+
}
|
243
|
+
|
218
244
|
var BodyInitType;
|
219
245
|
(function (BodyInitType) {
|
220
246
|
BodyInitType["ReadableStream"] = "ReadableStream";
|
@@ -224,7 +250,7 @@ var BodyInitType;
|
|
224
250
|
BodyInitType["String"] = "String";
|
225
251
|
BodyInitType["Readable"] = "Readable";
|
226
252
|
})(BodyInitType || (BodyInitType = {}));
|
227
|
-
class
|
253
|
+
class PonyfillBody {
|
228
254
|
constructor(bodyInit) {
|
229
255
|
this.bodyInit = bodyInit;
|
230
256
|
this.bodyUsed = false;
|
@@ -435,7 +461,7 @@ class PonyfillHeaders {
|
|
435
461
|
function isRequest(input) {
|
436
462
|
return input[Symbol.toStringTag] === 'Request';
|
437
463
|
}
|
438
|
-
class PonyfillRequest extends
|
464
|
+
class PonyfillRequest extends PonyfillBody {
|
439
465
|
constructor(input, options) {
|
440
466
|
let url;
|
441
467
|
let bodyInit = null;
|
@@ -501,7 +527,7 @@ class PonyfillRequest extends BodyPonyfill {
|
|
501
527
|
}
|
502
528
|
}
|
503
529
|
|
504
|
-
class PonyfillResponse extends
|
530
|
+
class PonyfillResponse extends PonyfillBody {
|
505
531
|
constructor(body, init) {
|
506
532
|
super(body || null);
|
507
533
|
this.headers = new PonyfillHeaders();
|
@@ -542,6 +568,15 @@ class PonyfillResponse extends BodyPonyfill {
|
|
542
568
|
status,
|
543
569
|
});
|
544
570
|
}
|
571
|
+
static json(data, init = {}) {
|
572
|
+
return new PonyfillResponse(JSON.stringify(data), {
|
573
|
+
...init,
|
574
|
+
headers: {
|
575
|
+
"Content-Type": "application/json",
|
576
|
+
...init === null || init === void 0 ? void 0 : init.headers,
|
577
|
+
},
|
578
|
+
});
|
579
|
+
}
|
545
580
|
}
|
546
581
|
|
547
582
|
function getHeadersObj(headers) {
|
@@ -562,17 +597,22 @@ const fetchPonyfill = (info, init) => {
|
|
562
597
|
const fetchRequest = info;
|
563
598
|
return new Promise((resolve, reject) => {
|
564
599
|
try {
|
600
|
+
if (fetchRequest.url.startsWith('file://')) {
|
601
|
+
resolve(new PonyfillResponse(createReadStream(fileURLToPath(fetchRequest.url))));
|
602
|
+
return;
|
603
|
+
}
|
565
604
|
const requestFn = fetchRequest.url.startsWith('https') ? request : request$1;
|
566
605
|
const nodeReadable = fetchRequest.readable();
|
567
606
|
const nodeHeaders = getHeadersObj(fetchRequest.headers);
|
568
|
-
const abortListener = function abortListener() {
|
569
|
-
|
607
|
+
const abortListener = function abortListener(event) {
|
608
|
+
nodeRequest.destroy();
|
609
|
+
reject(new PonyfillAbortError(event.detail));
|
570
610
|
};
|
571
611
|
fetchRequest.signal.addEventListener('abort', abortListener);
|
572
612
|
const nodeRequest = requestFn(fetchRequest.url, {
|
613
|
+
// signal: fetchRequest.signal will be added when v14 reaches EOL
|
573
614
|
method: fetchRequest.method,
|
574
615
|
headers: nodeHeaders,
|
575
|
-
signal: fetchRequest.signal,
|
576
616
|
}, nodeResponse => {
|
577
617
|
if (nodeResponse.headers.location) {
|
578
618
|
if (fetchRequest.redirect === 'error') {
|
@@ -597,6 +637,7 @@ const fetchPonyfill = (info, init) => {
|
|
597
637
|
url: info.url,
|
598
638
|
}));
|
599
639
|
});
|
640
|
+
nodeRequest.on('error', reject);
|
600
641
|
if (nodeReadable) {
|
601
642
|
nodeReadable.pipe(nodeRequest);
|
602
643
|
}
|
@@ -610,4 +651,38 @@ const fetchPonyfill = (info, init) => {
|
|
610
651
|
});
|
611
652
|
};
|
612
653
|
|
613
|
-
|
654
|
+
class PonyfillTextEncoder {
|
655
|
+
constructor(encoding = 'utf-8') {
|
656
|
+
this.encoding = encoding;
|
657
|
+
}
|
658
|
+
encode(input) {
|
659
|
+
return Buffer.from(input, this.encoding);
|
660
|
+
}
|
661
|
+
encodeInto(source, destination) {
|
662
|
+
const buffer = this.encode(source);
|
663
|
+
const copied = buffer.copy(destination);
|
664
|
+
return {
|
665
|
+
read: copied,
|
666
|
+
written: copied,
|
667
|
+
};
|
668
|
+
}
|
669
|
+
}
|
670
|
+
class PonyfillTextDecoder {
|
671
|
+
constructor(encoding = 'utf-8', options) {
|
672
|
+
this.encoding = encoding;
|
673
|
+
this.fatal = false;
|
674
|
+
this.ignoreBOM = false;
|
675
|
+
if (options) {
|
676
|
+
this.fatal = options.fatal || false;
|
677
|
+
this.ignoreBOM = options.ignoreBOM || false;
|
678
|
+
}
|
679
|
+
}
|
680
|
+
decode(input) {
|
681
|
+
return Buffer.from(input).toString(this.encoding);
|
682
|
+
}
|
683
|
+
}
|
684
|
+
function PonyfillBtoa(input) {
|
685
|
+
return Buffer.from(input, 'binary').toString('base64');
|
686
|
+
}
|
687
|
+
|
688
|
+
export { PonyfillAbortController as AbortController, PonyfillAbortError as AbortError, PonyfillAbortSignal as AbortSignal, PonyfillBlob as Blob, PonyfillBody as Body, PonyfillFile as File, PonyfillFormData as FormData, PonyfillHeaders as Headers, PonyfillReadableStream as ReadableStream, PonyfillRequest as Request, PonyfillResponse as Response, PonyfillTextDecoder as TextDecoder, PonyfillTextEncoder as TextEncoder, PonyfillBtoa as btoa, fetchPonyfill as fetch };
|
package/package.json
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
{
|
2
2
|
"name": "@whatwg-node/node-fetch",
|
3
|
-
"version": "0.0.1-alpha-
|
3
|
+
"version": "0.0.1-alpha-20221225155445-dfcb269",
|
4
4
|
"description": "Fetch API implementation for Node",
|
5
5
|
"sideEffects": false,
|
6
6
|
"peerDependencies": {
|
7
7
|
"@types/node": "^18.0.6"
|
8
8
|
},
|
9
9
|
"dependencies": {
|
10
|
+
"@whatwg-node/events": "0.0.2",
|
10
11
|
"tslib": "^2.3.1"
|
11
12
|
},
|
12
13
|
"repository": {
|