@whatwg-node/node-fetch 0.0.1-alpha-20221005124650-55319ef → 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/AbortError.d.ts +2 -1
- package/AbortSignal.d.ts +1 -0
- package/Blob.d.ts +6 -5
- package/index.js +92 -66
- package/index.mjs +91 -65
- package/package.json +2 -1
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/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
|
|
@@ -219,6 +168,81 @@ class PonyfillReadableStream {
|
|
219
168
|
}
|
220
169
|
}
|
221
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
|
+
|
222
246
|
var BodyInitType;
|
223
247
|
(function (BodyInitType) {
|
224
248
|
BodyInitType["ReadableStream"] = "ReadableStream";
|
@@ -569,14 +593,15 @@ const fetchPonyfill = (info, init) => {
|
|
569
593
|
const requestFn = fetchRequest.url.startsWith('https') ? https.request : http.request;
|
570
594
|
const nodeReadable = fetchRequest.readable();
|
571
595
|
const nodeHeaders = getHeadersObj(fetchRequest.headers);
|
572
|
-
const abortListener = function abortListener() {
|
573
|
-
|
596
|
+
const abortListener = function abortListener(event) {
|
597
|
+
nodeRequest.destroy();
|
598
|
+
reject(new PonyfillAbortError(event.detail));
|
574
599
|
};
|
575
600
|
fetchRequest.signal.addEventListener('abort', abortListener);
|
576
601
|
const nodeRequest = requestFn(fetchRequest.url, {
|
602
|
+
// signal: fetchRequest.signal will be added when v14 reaches EOL
|
577
603
|
method: fetchRequest.method,
|
578
604
|
headers: nodeHeaders,
|
579
|
-
signal: fetchRequest.signal,
|
580
605
|
}, nodeResponse => {
|
581
606
|
if (nodeResponse.headers.location) {
|
582
607
|
if (fetchRequest.redirect === 'error') {
|
@@ -601,6 +626,7 @@ const fetchPonyfill = (info, init) => {
|
|
601
626
|
url: info.url,
|
602
627
|
}));
|
603
628
|
});
|
629
|
+
nodeRequest.on('error', reject);
|
604
630
|
if (nodeReadable) {
|
605
631
|
nodeReadable.pipe(nodeRequest);
|
606
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
|
|
@@ -215,6 +164,81 @@ class PonyfillReadableStream {
|
|
215
164
|
}
|
216
165
|
}
|
217
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
|
+
|
218
242
|
var BodyInitType;
|
219
243
|
(function (BodyInitType) {
|
220
244
|
BodyInitType["ReadableStream"] = "ReadableStream";
|
@@ -565,14 +589,15 @@ const fetchPonyfill = (info, init) => {
|
|
565
589
|
const requestFn = fetchRequest.url.startsWith('https') ? request : request$1;
|
566
590
|
const nodeReadable = fetchRequest.readable();
|
567
591
|
const nodeHeaders = getHeadersObj(fetchRequest.headers);
|
568
|
-
const abortListener = function abortListener() {
|
569
|
-
|
592
|
+
const abortListener = function abortListener(event) {
|
593
|
+
nodeRequest.destroy();
|
594
|
+
reject(new PonyfillAbortError(event.detail));
|
570
595
|
};
|
571
596
|
fetchRequest.signal.addEventListener('abort', abortListener);
|
572
597
|
const nodeRequest = requestFn(fetchRequest.url, {
|
598
|
+
// signal: fetchRequest.signal will be added when v14 reaches EOL
|
573
599
|
method: fetchRequest.method,
|
574
600
|
headers: nodeHeaders,
|
575
|
-
signal: fetchRequest.signal,
|
576
601
|
}, nodeResponse => {
|
577
602
|
if (nodeResponse.headers.location) {
|
578
603
|
if (fetchRequest.redirect === 'error') {
|
@@ -597,6 +622,7 @@ const fetchPonyfill = (info, init) => {
|
|
597
622
|
url: info.url,
|
598
623
|
}));
|
599
624
|
});
|
625
|
+
nodeRequest.on('error', reject);
|
600
626
|
if (nodeReadable) {
|
601
627
|
nodeReadable.pipe(nodeRequest);
|
602
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": {
|