@whatwg-node/node-fetch 0.0.1-alpha-20221005124556-f602970 → 0.0.1-alpha-20221005131815-b0ce77a
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/AbortController.d.ts +1 -1
- package/AbortError.d.ts +2 -1
- package/AbortSignal.d.ts +1 -0
- package/Blob.d.ts +6 -5
- package/Body.d.ts +3 -3
- package/File.d.ts +1 -1
- package/ReadableStream.d.ts +2 -2
- package/Request.d.ts +2 -2
- package/Response.d.ts +2 -2
- package/fetch.d.ts +2 -2
- package/index.js +106 -77
- package/index.mjs +105 -76
- package/package.json +2 -1
package/AbortController.d.ts
CHANGED
package/AbortError.d.ts
CHANGED
package/AbortSignal.d.ts
CHANGED
package/Blob.d.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
+
}
|
package/Body.d.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
/// <reference types="node" />
|
2
2
|
import { PonyfillBlob } from './Blob';
|
3
|
-
import { Readable } from
|
4
|
-
import { PonyfillFormData } from
|
5
|
-
import { PonyfillReadableStream } from
|
3
|
+
import { Readable } from 'stream';
|
4
|
+
import { PonyfillFormData } from './FormData';
|
5
|
+
import { PonyfillReadableStream } from './ReadableStream';
|
6
6
|
export declare type BodyPonyfillInit = XMLHttpRequestBodyInit | Readable | PonyfillReadableStream<Uint8Array>;
|
7
7
|
export declare class BodyPonyfill implements Body {
|
8
8
|
private bodyInit;
|
package/File.d.ts
CHANGED
package/ReadableStream.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
/// <reference types="node" />
|
2
|
-
import { Readable } from
|
2
|
+
import { Readable } from 'stream';
|
3
3
|
export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
|
4
4
|
readable: Readable;
|
5
5
|
constructor(underlyingSource?: UnderlyingSource<T> | Readable | ReadableStream<T> | PonyfillReadableStream<T>);
|
@@ -9,7 +9,7 @@ export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
|
|
9
9
|
[Symbol.asyncIterator](): AsyncIterableIterator<any>;
|
10
10
|
tee(): [ReadableStream<T>, ReadableStream<T>];
|
11
11
|
pipeTo(destination: WritableStream<T>): Promise<void>;
|
12
|
-
pipeThrough<T2>({ writable, readable }: {
|
12
|
+
pipeThrough<T2>({ writable, readable, }: {
|
13
13
|
writable: WritableStream<T>;
|
14
14
|
readable: ReadableStream<T2>;
|
15
15
|
}): ReadableStream<T2>;
|
package/Request.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import { BodyPonyfill, BodyPonyfillInit } from
|
2
|
-
import { PonyfillHeadersInit } from
|
1
|
+
import { BodyPonyfill, BodyPonyfillInit } from './Body';
|
2
|
+
import { PonyfillHeadersInit } from './Headers';
|
3
3
|
export declare type RequestPonyfillInit = Omit<RequestInit, 'body' | 'headers'> & {
|
4
4
|
body?: BodyPonyfillInit | null;
|
5
5
|
headers?: PonyfillHeadersInit;
|
package/Response.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import { BodyPonyfill, BodyPonyfillInit } from
|
2
|
-
import { PonyfillHeadersInit } from
|
1
|
+
import { BodyPonyfill, BodyPonyfillInit } from './Body';
|
2
|
+
import { PonyfillHeadersInit } from './Headers';
|
3
3
|
export declare type ResponsePonyfilInit = Omit<ResponseInit, 'headers'> & {
|
4
4
|
url?: string;
|
5
5
|
redirected?: boolean;
|
package/fetch.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import { PonyfillRequest, RequestPonyfillInit } from
|
2
|
-
import { PonyfillResponse } from
|
1
|
+
import { PonyfillRequest, RequestPonyfillInit } from './Request';
|
2
|
+
import { PonyfillResponse } from './Response';
|
3
3
|
export declare const fetchPonyfill: (info: string | PonyfillRequest | URL, init?: RequestPonyfillInit) => Promise<PonyfillResponse>;
|
package/index.js
CHANGED
@@ -4,17 +4,24 @@ 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');
|
9
10
|
|
11
|
+
// Will be removed after v14 reaches EOL
|
10
12
|
class PonyfillAbortError extends Error {
|
11
|
-
constructor() {
|
12
|
-
super('The operation was aborted.'
|
13
|
+
constructor(reason) {
|
14
|
+
super('The operation was aborted.', {
|
15
|
+
cause: reason,
|
16
|
+
});
|
13
17
|
this.name = 'AbortError';
|
14
18
|
}
|
19
|
+
get reason() {
|
20
|
+
return this.cause;
|
21
|
+
}
|
15
22
|
}
|
16
23
|
|
17
|
-
class PonyfillAbortSignal extends EventTarget {
|
24
|
+
class PonyfillAbortSignal extends events.EventTarget {
|
18
25
|
constructor() {
|
19
26
|
super(...arguments);
|
20
27
|
this.aborted = false;
|
@@ -34,71 +41,13 @@ class PonyfillAbortSignal extends EventTarget {
|
|
34
41
|
}
|
35
42
|
}
|
36
43
|
|
44
|
+
// Will be removed after v14 reaches EOL
|
37
45
|
class PonyfillAbortController {
|
38
46
|
constructor() {
|
39
47
|
this.signal = new PonyfillAbortSignal();
|
40
48
|
}
|
41
49
|
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
|
-
}
|
50
|
+
this.signal.dispatchEvent(new events.CustomEvent('abort', { detail: reason }));
|
102
51
|
}
|
103
52
|
}
|
104
53
|
|
@@ -113,7 +62,7 @@ function createController(desiredSize, readable) {
|
|
113
62
|
},
|
114
63
|
error(error) {
|
115
64
|
readable.destroy(error);
|
116
|
-
}
|
65
|
+
},
|
117
66
|
};
|
118
67
|
}
|
119
68
|
class PonyfillReadableStream {
|
@@ -130,20 +79,23 @@ class PonyfillReadableStream {
|
|
130
79
|
const reader = underlyingSource.getReader();
|
131
80
|
this.readable = new stream.Readable({
|
132
81
|
read() {
|
133
|
-
reader
|
82
|
+
reader
|
83
|
+
.read()
|
84
|
+
.then(({ value, done }) => {
|
134
85
|
if (done) {
|
135
86
|
this.push(null);
|
136
87
|
}
|
137
88
|
else {
|
138
89
|
this.push(value);
|
139
90
|
}
|
140
|
-
})
|
91
|
+
})
|
92
|
+
.catch(err => {
|
141
93
|
this.destroy(err);
|
142
94
|
});
|
143
95
|
},
|
144
96
|
destroy(err, callback) {
|
145
97
|
reader.cancel(err).then(() => callback(err), callback);
|
146
|
-
}
|
98
|
+
},
|
147
99
|
});
|
148
100
|
}
|
149
101
|
else {
|
@@ -165,7 +117,7 @@ class PonyfillReadableStream {
|
|
165
117
|
catch (err) {
|
166
118
|
callback(err);
|
167
119
|
}
|
168
|
-
}
|
120
|
+
},
|
169
121
|
});
|
170
122
|
}
|
171
123
|
}
|
@@ -210,12 +162,87 @@ class PonyfillReadableStream {
|
|
210
162
|
await writer.ready;
|
211
163
|
return writer.close();
|
212
164
|
}
|
213
|
-
pipeThrough({ writable, readable }) {
|
165
|
+
pipeThrough({ writable, readable, }) {
|
214
166
|
this.pipeTo(writable);
|
215
167
|
return readable;
|
216
168
|
}
|
217
169
|
}
|
218
170
|
|
171
|
+
// Will be removed after v14 reaches EOL
|
172
|
+
// Needed because v14 doesn't have .stream() implemented
|
173
|
+
class PonyfillBlob extends buffer.Blob {
|
174
|
+
stream() {
|
175
|
+
return new PonyfillReadableStream({
|
176
|
+
start: async (controller) => {
|
177
|
+
const arrayBuffer = await this.arrayBuffer();
|
178
|
+
const buffer = Buffer.from(arrayBuffer);
|
179
|
+
controller.enqueue(buffer);
|
180
|
+
controller.close();
|
181
|
+
},
|
182
|
+
});
|
183
|
+
}
|
184
|
+
slice(...args) {
|
185
|
+
return super.slice(...args);
|
186
|
+
}
|
187
|
+
}
|
188
|
+
|
189
|
+
class PonyfillFile extends PonyfillBlob {
|
190
|
+
constructor(fileBits, name, options) {
|
191
|
+
super(fileBits, options);
|
192
|
+
this.name = name;
|
193
|
+
this.webkitRelativePath = '';
|
194
|
+
this.lastModified = (options === null || options === void 0 ? void 0 : options.lastModified) || Date.now();
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
class PonyfillFormData {
|
199
|
+
constructor() {
|
200
|
+
this.map = new Map();
|
201
|
+
}
|
202
|
+
append(name, value, fileName) {
|
203
|
+
let values = this.map.get(name);
|
204
|
+
if (!values) {
|
205
|
+
values = [];
|
206
|
+
this.map.set(name, values);
|
207
|
+
}
|
208
|
+
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
|
209
|
+
values.push(entry);
|
210
|
+
}
|
211
|
+
delete(name) {
|
212
|
+
this.map.delete(name);
|
213
|
+
}
|
214
|
+
get(name) {
|
215
|
+
const values = this.map.get(name);
|
216
|
+
return values ? values[0] : null;
|
217
|
+
}
|
218
|
+
getAll(name) {
|
219
|
+
return this.map.get(name) || [];
|
220
|
+
}
|
221
|
+
has(name) {
|
222
|
+
return this.map.has(name);
|
223
|
+
}
|
224
|
+
set(name, value, fileName) {
|
225
|
+
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
|
226
|
+
this.map.set(name, [entry]);
|
227
|
+
}
|
228
|
+
getNormalizedFile(name, blob, fileName) {
|
229
|
+
if (blob instanceof PonyfillFile) {
|
230
|
+
if (fileName == null) {
|
231
|
+
return new PonyfillFile([blob], name, { type: blob.type, lastModified: blob.lastModified });
|
232
|
+
}
|
233
|
+
return blob;
|
234
|
+
}
|
235
|
+
return new PonyfillFile([blob], fileName || name, { type: blob.type });
|
236
|
+
}
|
237
|
+
forEach(callback) {
|
238
|
+
for (const [key, values] of this.map) {
|
239
|
+
for (const value of values) {
|
240
|
+
callback(value, key, this);
|
241
|
+
}
|
242
|
+
}
|
243
|
+
}
|
244
|
+
}
|
245
|
+
|
219
246
|
var BodyInitType;
|
220
247
|
(function (BodyInitType) {
|
221
248
|
BodyInitType["ReadableStream"] = "ReadableStream";
|
@@ -290,7 +317,7 @@ class BodyPonyfill {
|
|
290
317
|
controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
|
291
318
|
controller.enqueue(Buffer.from(value));
|
292
319
|
}
|
293
|
-
if (Number(i) ===
|
320
|
+
if (Number(i) === entries.length - 1) {
|
294
321
|
controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
|
295
322
|
}
|
296
323
|
else {
|
@@ -298,7 +325,7 @@ class BodyPonyfill {
|
|
298
325
|
}
|
299
326
|
}
|
300
327
|
controller.close();
|
301
|
-
}
|
328
|
+
},
|
302
329
|
});
|
303
330
|
}
|
304
331
|
else if ('buffer' in this.bodyInit) {
|
@@ -528,7 +555,7 @@ class PonyfillResponse extends BodyPonyfill {
|
|
528
555
|
static error() {
|
529
556
|
const response = new PonyfillResponse(null, {
|
530
557
|
status: 500,
|
531
|
-
statusText: 'Internal Server Error'
|
558
|
+
statusText: 'Internal Server Error',
|
532
559
|
});
|
533
560
|
return response;
|
534
561
|
}
|
@@ -538,9 +565,9 @@ class PonyfillResponse extends BodyPonyfill {
|
|
538
565
|
}
|
539
566
|
return new PonyfillResponse(null, {
|
540
567
|
headers: {
|
541
|
-
location: url
|
568
|
+
location: url,
|
542
569
|
},
|
543
|
-
status
|
570
|
+
status,
|
544
571
|
});
|
545
572
|
}
|
546
573
|
}
|
@@ -566,14 +593,15 @@ const fetchPonyfill = (info, init) => {
|
|
566
593
|
const requestFn = fetchRequest.url.startsWith('https') ? https.request : http.request;
|
567
594
|
const nodeReadable = fetchRequest.readable();
|
568
595
|
const nodeHeaders = getHeadersObj(fetchRequest.headers);
|
569
|
-
const abortListener = function abortListener() {
|
570
|
-
|
596
|
+
const abortListener = function abortListener(event) {
|
597
|
+
nodeRequest.destroy();
|
598
|
+
reject(new PonyfillAbortError(event.detail));
|
571
599
|
};
|
572
600
|
fetchRequest.signal.addEventListener('abort', abortListener);
|
573
601
|
const nodeRequest = requestFn(fetchRequest.url, {
|
602
|
+
// signal: fetchRequest.signal will be added when v14 reaches EOL
|
574
603
|
method: fetchRequest.method,
|
575
604
|
headers: nodeHeaders,
|
576
|
-
signal: fetchRequest.signal,
|
577
605
|
}, nodeResponse => {
|
578
606
|
if (nodeResponse.headers.location) {
|
579
607
|
if (fetchRequest.redirect === 'error') {
|
@@ -598,6 +626,7 @@ const fetchPonyfill = (info, init) => {
|
|
598
626
|
url: info.url,
|
599
627
|
}));
|
600
628
|
});
|
629
|
+
nodeRequest.on('error', reject);
|
601
630
|
if (nodeReadable) {
|
602
631
|
nodeReadable.pipe(nodeRequest);
|
603
632
|
}
|
package/index.mjs
CHANGED
@@ -1,13 +1,20 @@
|
|
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';
|
5
6
|
|
7
|
+
// Will be removed after v14 reaches EOL
|
6
8
|
class PonyfillAbortError extends Error {
|
7
|
-
constructor() {
|
8
|
-
super('The operation was aborted.'
|
9
|
+
constructor(reason) {
|
10
|
+
super('The operation was aborted.', {
|
11
|
+
cause: reason,
|
12
|
+
});
|
9
13
|
this.name = 'AbortError';
|
10
14
|
}
|
15
|
+
get reason() {
|
16
|
+
return this.cause;
|
17
|
+
}
|
11
18
|
}
|
12
19
|
|
13
20
|
class PonyfillAbortSignal extends EventTarget {
|
@@ -30,71 +37,13 @@ class PonyfillAbortSignal extends EventTarget {
|
|
30
37
|
}
|
31
38
|
}
|
32
39
|
|
40
|
+
// Will be removed after v14 reaches EOL
|
33
41
|
class PonyfillAbortController {
|
34
42
|
constructor() {
|
35
43
|
this.signal = new PonyfillAbortSignal();
|
36
44
|
}
|
37
45
|
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
|
-
}
|
46
|
+
this.signal.dispatchEvent(new CustomEvent('abort', { detail: reason }));
|
98
47
|
}
|
99
48
|
}
|
100
49
|
|
@@ -109,7 +58,7 @@ function createController(desiredSize, readable) {
|
|
109
58
|
},
|
110
59
|
error(error) {
|
111
60
|
readable.destroy(error);
|
112
|
-
}
|
61
|
+
},
|
113
62
|
};
|
114
63
|
}
|
115
64
|
class PonyfillReadableStream {
|
@@ -126,20 +75,23 @@ class PonyfillReadableStream {
|
|
126
75
|
const reader = underlyingSource.getReader();
|
127
76
|
this.readable = new Readable({
|
128
77
|
read() {
|
129
|
-
reader
|
78
|
+
reader
|
79
|
+
.read()
|
80
|
+
.then(({ value, done }) => {
|
130
81
|
if (done) {
|
131
82
|
this.push(null);
|
132
83
|
}
|
133
84
|
else {
|
134
85
|
this.push(value);
|
135
86
|
}
|
136
|
-
})
|
87
|
+
})
|
88
|
+
.catch(err => {
|
137
89
|
this.destroy(err);
|
138
90
|
});
|
139
91
|
},
|
140
92
|
destroy(err, callback) {
|
141
93
|
reader.cancel(err).then(() => callback(err), callback);
|
142
|
-
}
|
94
|
+
},
|
143
95
|
});
|
144
96
|
}
|
145
97
|
else {
|
@@ -161,7 +113,7 @@ class PonyfillReadableStream {
|
|
161
113
|
catch (err) {
|
162
114
|
callback(err);
|
163
115
|
}
|
164
|
-
}
|
116
|
+
},
|
165
117
|
});
|
166
118
|
}
|
167
119
|
}
|
@@ -206,12 +158,87 @@ class PonyfillReadableStream {
|
|
206
158
|
await writer.ready;
|
207
159
|
return writer.close();
|
208
160
|
}
|
209
|
-
pipeThrough({ writable, readable }) {
|
161
|
+
pipeThrough({ writable, readable, }) {
|
210
162
|
this.pipeTo(writable);
|
211
163
|
return readable;
|
212
164
|
}
|
213
165
|
}
|
214
166
|
|
167
|
+
// Will be removed after v14 reaches EOL
|
168
|
+
// Needed because v14 doesn't have .stream() implemented
|
169
|
+
class PonyfillBlob extends Blob {
|
170
|
+
stream() {
|
171
|
+
return new PonyfillReadableStream({
|
172
|
+
start: async (controller) => {
|
173
|
+
const arrayBuffer = await this.arrayBuffer();
|
174
|
+
const buffer = Buffer.from(arrayBuffer);
|
175
|
+
controller.enqueue(buffer);
|
176
|
+
controller.close();
|
177
|
+
},
|
178
|
+
});
|
179
|
+
}
|
180
|
+
slice(...args) {
|
181
|
+
return super.slice(...args);
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
class PonyfillFile extends PonyfillBlob {
|
186
|
+
constructor(fileBits, name, options) {
|
187
|
+
super(fileBits, options);
|
188
|
+
this.name = name;
|
189
|
+
this.webkitRelativePath = '';
|
190
|
+
this.lastModified = (options === null || options === void 0 ? void 0 : options.lastModified) || Date.now();
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
class PonyfillFormData {
|
195
|
+
constructor() {
|
196
|
+
this.map = new Map();
|
197
|
+
}
|
198
|
+
append(name, value, fileName) {
|
199
|
+
let values = this.map.get(name);
|
200
|
+
if (!values) {
|
201
|
+
values = [];
|
202
|
+
this.map.set(name, values);
|
203
|
+
}
|
204
|
+
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
|
205
|
+
values.push(entry);
|
206
|
+
}
|
207
|
+
delete(name) {
|
208
|
+
this.map.delete(name);
|
209
|
+
}
|
210
|
+
get(name) {
|
211
|
+
const values = this.map.get(name);
|
212
|
+
return values ? values[0] : null;
|
213
|
+
}
|
214
|
+
getAll(name) {
|
215
|
+
return this.map.get(name) || [];
|
216
|
+
}
|
217
|
+
has(name) {
|
218
|
+
return this.map.has(name);
|
219
|
+
}
|
220
|
+
set(name, value, fileName) {
|
221
|
+
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
|
222
|
+
this.map.set(name, [entry]);
|
223
|
+
}
|
224
|
+
getNormalizedFile(name, blob, fileName) {
|
225
|
+
if (blob instanceof PonyfillFile) {
|
226
|
+
if (fileName == null) {
|
227
|
+
return new PonyfillFile([blob], name, { type: blob.type, lastModified: blob.lastModified });
|
228
|
+
}
|
229
|
+
return blob;
|
230
|
+
}
|
231
|
+
return new PonyfillFile([blob], fileName || name, { type: blob.type });
|
232
|
+
}
|
233
|
+
forEach(callback) {
|
234
|
+
for (const [key, values] of this.map) {
|
235
|
+
for (const value of values) {
|
236
|
+
callback(value, key, this);
|
237
|
+
}
|
238
|
+
}
|
239
|
+
}
|
240
|
+
}
|
241
|
+
|
215
242
|
var BodyInitType;
|
216
243
|
(function (BodyInitType) {
|
217
244
|
BodyInitType["ReadableStream"] = "ReadableStream";
|
@@ -286,7 +313,7 @@ class BodyPonyfill {
|
|
286
313
|
controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
|
287
314
|
controller.enqueue(Buffer.from(value));
|
288
315
|
}
|
289
|
-
if (Number(i) ===
|
316
|
+
if (Number(i) === entries.length - 1) {
|
290
317
|
controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
|
291
318
|
}
|
292
319
|
else {
|
@@ -294,7 +321,7 @@ class BodyPonyfill {
|
|
294
321
|
}
|
295
322
|
}
|
296
323
|
controller.close();
|
297
|
-
}
|
324
|
+
},
|
298
325
|
});
|
299
326
|
}
|
300
327
|
else if ('buffer' in this.bodyInit) {
|
@@ -524,7 +551,7 @@ class PonyfillResponse extends BodyPonyfill {
|
|
524
551
|
static error() {
|
525
552
|
const response = new PonyfillResponse(null, {
|
526
553
|
status: 500,
|
527
|
-
statusText: 'Internal Server Error'
|
554
|
+
statusText: 'Internal Server Error',
|
528
555
|
});
|
529
556
|
return response;
|
530
557
|
}
|
@@ -534,9 +561,9 @@ class PonyfillResponse extends BodyPonyfill {
|
|
534
561
|
}
|
535
562
|
return new PonyfillResponse(null, {
|
536
563
|
headers: {
|
537
|
-
location: url
|
564
|
+
location: url,
|
538
565
|
},
|
539
|
-
status
|
566
|
+
status,
|
540
567
|
});
|
541
568
|
}
|
542
569
|
}
|
@@ -562,14 +589,15 @@ const fetchPonyfill = (info, init) => {
|
|
562
589
|
const requestFn = fetchRequest.url.startsWith('https') ? request : request$1;
|
563
590
|
const nodeReadable = fetchRequest.readable();
|
564
591
|
const nodeHeaders = getHeadersObj(fetchRequest.headers);
|
565
|
-
const abortListener = function abortListener() {
|
566
|
-
|
592
|
+
const abortListener = function abortListener(event) {
|
593
|
+
nodeRequest.destroy();
|
594
|
+
reject(new PonyfillAbortError(event.detail));
|
567
595
|
};
|
568
596
|
fetchRequest.signal.addEventListener('abort', abortListener);
|
569
597
|
const nodeRequest = requestFn(fetchRequest.url, {
|
598
|
+
// signal: fetchRequest.signal will be added when v14 reaches EOL
|
570
599
|
method: fetchRequest.method,
|
571
600
|
headers: nodeHeaders,
|
572
|
-
signal: fetchRequest.signal,
|
573
601
|
}, nodeResponse => {
|
574
602
|
if (nodeResponse.headers.location) {
|
575
603
|
if (fetchRequest.redirect === 'error') {
|
@@ -594,6 +622,7 @@ const fetchPonyfill = (info, init) => {
|
|
594
622
|
url: info.url,
|
595
623
|
}));
|
596
624
|
});
|
625
|
+
nodeRequest.on('error', reject);
|
597
626
|
if (nodeReadable) {
|
598
627
|
nodeReadable.pipe(nodeRequest);
|
599
628
|
}
|
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-20221005131815-b0ce77a",
|
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": {
|