@whatwg-node/node-fetch 0.0.4 → 0.0.5-alpha-20230206135558-1370a81
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/Body.d.ts +1 -1
- package/FormData.d.ts +1 -1
- package/index.js +65 -52
- package/index.mjs +65 -52
- package/package.json +3 -3
package/Body.d.ts
CHANGED
@@ -4,7 +4,7 @@ import { Readable } from 'stream';
|
|
4
4
|
import { PonyfillBlob } from './Blob';
|
5
5
|
import { PonyfillFormData } from './FormData';
|
6
6
|
import { PonyfillReadableStream } from './ReadableStream';
|
7
|
-
export type BodyPonyfillInit = XMLHttpRequestBodyInit | Readable | PonyfillReadableStream<Uint8Array>;
|
7
|
+
export type BodyPonyfillInit = XMLHttpRequestBodyInit | Readable | PonyfillReadableStream<Uint8Array> | AsyncIterable<Uint8Array>;
|
8
8
|
export interface FormDataLimits {
|
9
9
|
fieldNameSize?: number;
|
10
10
|
fieldSize?: number;
|
package/FormData.d.ts
CHANGED
@@ -10,5 +10,5 @@ export declare class PonyfillFormData implements FormData {
|
|
10
10
|
set(name: string, value: PonyfillBlob | string, fileName?: string): void;
|
11
11
|
[Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>;
|
12
12
|
forEach(callback: (value: FormDataEntryValue, key: string, parent: this) => void): void;
|
13
|
-
stream(boundary?: string): PonyfillReadableStream<Uint8Array>;
|
14
13
|
}
|
14
|
+
export declare function getStreamFromFormData(formData: FormData, boundary?: string): PonyfillReadableStream<Uint8Array>;
|
package/index.js
CHANGED
@@ -32,6 +32,7 @@ class PonyfillAbortError extends Error {
|
|
32
32
|
|
33
33
|
function createController(desiredSize, readable) {
|
34
34
|
let chunks = [];
|
35
|
+
let _closed = false;
|
35
36
|
return {
|
36
37
|
desiredSize,
|
37
38
|
enqueue(chunk) {
|
@@ -42,6 +43,7 @@ function createController(desiredSize, readable) {
|
|
42
43
|
this._flush();
|
43
44
|
}
|
44
45
|
readable.push(null);
|
46
|
+
_closed = true;
|
45
47
|
},
|
46
48
|
error(error) {
|
47
49
|
if (chunks.length > 0) {
|
@@ -49,6 +51,9 @@ function createController(desiredSize, readable) {
|
|
49
51
|
}
|
50
52
|
readable.destroy(error);
|
51
53
|
},
|
54
|
+
get _closed() {
|
55
|
+
return _closed;
|
56
|
+
},
|
52
57
|
_flush() {
|
53
58
|
if (chunks.length > 0) {
|
54
59
|
const concatenated = Buffer.concat(chunks);
|
@@ -105,7 +110,9 @@ class PonyfillReadableStream {
|
|
105
110
|
started = true;
|
106
111
|
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
|
107
112
|
}
|
108
|
-
|
113
|
+
if (!controller._closed) {
|
114
|
+
await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller));
|
115
|
+
}
|
109
116
|
controller._flush();
|
110
117
|
},
|
111
118
|
async destroy(err, callback) {
|
@@ -292,47 +299,58 @@ class PonyfillFormData {
|
|
292
299
|
callback(value, key, this);
|
293
300
|
}
|
294
301
|
}
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
302
|
+
}
|
303
|
+
function getStreamFromFormData(formData, boundary = '---') {
|
304
|
+
const entries = [];
|
305
|
+
let sentInitialHeader = false;
|
306
|
+
return new PonyfillReadableStream({
|
307
|
+
start: controller => {
|
308
|
+
formData.forEach((value, key) => {
|
309
|
+
if (!sentInitialHeader) {
|
310
|
+
controller.enqueue(Buffer.from(`--${boundary}\r\n`));
|
311
|
+
sentInitialHeader = true;
|
312
|
+
}
|
313
|
+
entries.push([key, value]);
|
314
|
+
});
|
315
|
+
if (!sentInitialHeader) {
|
316
|
+
controller.enqueue(Buffer.from(`--${boundary}--\r\n`));
|
317
|
+
controller.close();
|
318
|
+
}
|
319
|
+
},
|
320
|
+
pull: async (controller) => {
|
321
|
+
const entry = entries.shift();
|
322
|
+
if (entry) {
|
323
|
+
const [key, value] = entry;
|
324
|
+
if (typeof value === 'string') {
|
325
|
+
controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
|
326
|
+
controller.enqueue(Buffer.from(value));
|
327
|
+
}
|
328
|
+
else {
|
329
|
+
let filenamePart = '';
|
330
|
+
if (value.name) {
|
331
|
+
filenamePart = `; filename="${value.name}"`;
|
324
332
|
}
|
325
|
-
|
326
|
-
|
333
|
+
controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"${filenamePart}\r\n`));
|
334
|
+
controller.enqueue(Buffer.from(`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`));
|
335
|
+
const entryStream = value.stream();
|
336
|
+
for await (const chunk of entryStream) {
|
337
|
+
controller.enqueue(chunk);
|
327
338
|
}
|
328
339
|
}
|
329
|
-
|
340
|
+
if (entries.length === 0) {
|
330
341
|
controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
|
331
342
|
controller.close();
|
332
343
|
}
|
333
|
-
|
334
|
-
|
335
|
-
|
344
|
+
else {
|
345
|
+
controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`));
|
346
|
+
}
|
347
|
+
}
|
348
|
+
else {
|
349
|
+
controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
|
350
|
+
controller.close();
|
351
|
+
}
|
352
|
+
},
|
353
|
+
});
|
336
354
|
}
|
337
355
|
function getNormalizedFile(name, blob, fileName) {
|
338
356
|
if (blob instanceof PonyfillFile) {
|
@@ -584,16 +602,6 @@ function processBodyInit(bodyInit) {
|
|
584
602
|
},
|
585
603
|
};
|
586
604
|
}
|
587
|
-
if (bodyInit instanceof PonyfillFormData) {
|
588
|
-
const boundary = Math.random().toString(36).substr(2);
|
589
|
-
const contentType = `multipart/form-data; boundary=${boundary}`;
|
590
|
-
return {
|
591
|
-
bodyType: BodyInitType.FormData,
|
592
|
-
contentType,
|
593
|
-
contentLength: null,
|
594
|
-
bodyFactory: () => bodyInit.stream(boundary),
|
595
|
-
};
|
596
|
-
}
|
597
605
|
if (bodyInit instanceof Buffer) {
|
598
606
|
const contentLength = bodyInit.length;
|
599
607
|
return {
|
@@ -688,12 +696,17 @@ function processBodyInit(bodyInit) {
|
|
688
696
|
contentType,
|
689
697
|
contentLength: null,
|
690
698
|
bodyFactory() {
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
699
|
+
return getStreamFromFormData(bodyInit, boundary);
|
700
|
+
},
|
701
|
+
};
|
702
|
+
}
|
703
|
+
if (bodyInit[Symbol.iterator] || bodyInit[Symbol.asyncIterator]) {
|
704
|
+
return {
|
705
|
+
contentType: null,
|
706
|
+
contentLength: null,
|
707
|
+
bodyFactory() {
|
708
|
+
const readable = stream.Readable.from(bodyInit);
|
709
|
+
return new PonyfillReadableStream(readable);
|
697
710
|
},
|
698
711
|
};
|
699
712
|
}
|
package/index.mjs
CHANGED
@@ -26,6 +26,7 @@ class PonyfillAbortError extends Error {
|
|
26
26
|
|
27
27
|
function createController(desiredSize, readable) {
|
28
28
|
let chunks = [];
|
29
|
+
let _closed = false;
|
29
30
|
return {
|
30
31
|
desiredSize,
|
31
32
|
enqueue(chunk) {
|
@@ -36,6 +37,7 @@ function createController(desiredSize, readable) {
|
|
36
37
|
this._flush();
|
37
38
|
}
|
38
39
|
readable.push(null);
|
40
|
+
_closed = true;
|
39
41
|
},
|
40
42
|
error(error) {
|
41
43
|
if (chunks.length > 0) {
|
@@ -43,6 +45,9 @@ function createController(desiredSize, readable) {
|
|
43
45
|
}
|
44
46
|
readable.destroy(error);
|
45
47
|
},
|
48
|
+
get _closed() {
|
49
|
+
return _closed;
|
50
|
+
},
|
46
51
|
_flush() {
|
47
52
|
if (chunks.length > 0) {
|
48
53
|
const concatenated = Buffer.concat(chunks);
|
@@ -99,7 +104,9 @@ class PonyfillReadableStream {
|
|
99
104
|
started = true;
|
100
105
|
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
|
101
106
|
}
|
102
|
-
|
107
|
+
if (!controller._closed) {
|
108
|
+
await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller));
|
109
|
+
}
|
103
110
|
controller._flush();
|
104
111
|
},
|
105
112
|
async destroy(err, callback) {
|
@@ -286,47 +293,58 @@ class PonyfillFormData {
|
|
286
293
|
callback(value, key, this);
|
287
294
|
}
|
288
295
|
}
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
296
|
+
}
|
297
|
+
function getStreamFromFormData(formData, boundary = '---') {
|
298
|
+
const entries = [];
|
299
|
+
let sentInitialHeader = false;
|
300
|
+
return new PonyfillReadableStream({
|
301
|
+
start: controller => {
|
302
|
+
formData.forEach((value, key) => {
|
303
|
+
if (!sentInitialHeader) {
|
304
|
+
controller.enqueue(Buffer.from(`--${boundary}\r\n`));
|
305
|
+
sentInitialHeader = true;
|
306
|
+
}
|
307
|
+
entries.push([key, value]);
|
308
|
+
});
|
309
|
+
if (!sentInitialHeader) {
|
310
|
+
controller.enqueue(Buffer.from(`--${boundary}--\r\n`));
|
311
|
+
controller.close();
|
312
|
+
}
|
313
|
+
},
|
314
|
+
pull: async (controller) => {
|
315
|
+
const entry = entries.shift();
|
316
|
+
if (entry) {
|
317
|
+
const [key, value] = entry;
|
318
|
+
if (typeof value === 'string') {
|
319
|
+
controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
|
320
|
+
controller.enqueue(Buffer.from(value));
|
321
|
+
}
|
322
|
+
else {
|
323
|
+
let filenamePart = '';
|
324
|
+
if (value.name) {
|
325
|
+
filenamePart = `; filename="${value.name}"`;
|
318
326
|
}
|
319
|
-
|
320
|
-
|
327
|
+
controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"${filenamePart}\r\n`));
|
328
|
+
controller.enqueue(Buffer.from(`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`));
|
329
|
+
const entryStream = value.stream();
|
330
|
+
for await (const chunk of entryStream) {
|
331
|
+
controller.enqueue(chunk);
|
321
332
|
}
|
322
333
|
}
|
323
|
-
|
334
|
+
if (entries.length === 0) {
|
324
335
|
controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
|
325
336
|
controller.close();
|
326
337
|
}
|
327
|
-
|
328
|
-
|
329
|
-
|
338
|
+
else {
|
339
|
+
controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`));
|
340
|
+
}
|
341
|
+
}
|
342
|
+
else {
|
343
|
+
controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
|
344
|
+
controller.close();
|
345
|
+
}
|
346
|
+
},
|
347
|
+
});
|
330
348
|
}
|
331
349
|
function getNormalizedFile(name, blob, fileName) {
|
332
350
|
if (blob instanceof PonyfillFile) {
|
@@ -578,16 +596,6 @@ function processBodyInit(bodyInit) {
|
|
578
596
|
},
|
579
597
|
};
|
580
598
|
}
|
581
|
-
if (bodyInit instanceof PonyfillFormData) {
|
582
|
-
const boundary = Math.random().toString(36).substr(2);
|
583
|
-
const contentType = `multipart/form-data; boundary=${boundary}`;
|
584
|
-
return {
|
585
|
-
bodyType: BodyInitType.FormData,
|
586
|
-
contentType,
|
587
|
-
contentLength: null,
|
588
|
-
bodyFactory: () => bodyInit.stream(boundary),
|
589
|
-
};
|
590
|
-
}
|
591
599
|
if (bodyInit instanceof Buffer) {
|
592
600
|
const contentLength = bodyInit.length;
|
593
601
|
return {
|
@@ -682,12 +690,17 @@ function processBodyInit(bodyInit) {
|
|
682
690
|
contentType,
|
683
691
|
contentLength: null,
|
684
692
|
bodyFactory() {
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
693
|
+
return getStreamFromFormData(bodyInit, boundary);
|
694
|
+
},
|
695
|
+
};
|
696
|
+
}
|
697
|
+
if (bodyInit[Symbol.iterator] || bodyInit[Symbol.asyncIterator]) {
|
698
|
+
return {
|
699
|
+
contentType: null,
|
700
|
+
contentLength: null,
|
701
|
+
bodyFactory() {
|
702
|
+
const readable = Readable.from(bodyInit);
|
703
|
+
return new PonyfillReadableStream(readable);
|
691
704
|
},
|
692
705
|
};
|
693
706
|
}
|
package/package.json
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
{
|
2
2
|
"name": "@whatwg-node/node-fetch",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.5-alpha-20230206135558-1370a81",
|
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",
|
11
|
-
"busboy": "1.6.0",
|
10
|
+
"@whatwg-node/events": "^0.0.2",
|
11
|
+
"busboy": "^1.6.0",
|
12
12
|
"tslib": "^2.3.1"
|
13
13
|
},
|
14
14
|
"repository": {
|