@whatwg-node/node-fetch 0.4.14 → 0.4.15-alpha-20230901114303-38c018b
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/cjs/Blob.js +59 -34
- package/cjs/Body.js +30 -52
- package/cjs/ReadableStream.js +1 -1
- package/cjs/TextEncoderDecoder.js +7 -0
- package/cjs/fetch.js +4 -3
- package/cjs/utils.js +1 -5
- package/esm/Blob.js +60 -35
- package/esm/Body.js +30 -52
- package/esm/ReadableStream.js +1 -1
- package/esm/TextEncoderDecoder.js +7 -0
- package/esm/fetch.js +4 -3
- package/esm/utils.js +1 -5
- package/package.json +1 -1
- package/typings/Blob.d.cts +6 -2
- package/typings/Blob.d.ts +6 -2
- package/typings/Body.d.cts +1 -2
- package/typings/Body.d.ts +1 -2
- package/typings/TextEncoderDecoder.d.cts +2 -2
- package/typings/TextEncoderDecoder.d.ts +2 -2
package/cjs/Blob.js
CHANGED
@@ -26,59 +26,84 @@ function isBlob(obj) {
|
|
26
26
|
class PonyfillBlob {
|
27
27
|
constructor(blobParts, options) {
|
28
28
|
this.blobParts = blobParts;
|
29
|
+
this._size = null;
|
29
30
|
this.type = options?.type || 'application/octet-stream';
|
30
31
|
this.encoding = options?.encoding || 'utf8';
|
32
|
+
this._size = options?.size || null;
|
33
|
+
if (blobParts.length === 1 && isBlob(blobParts[0])) {
|
34
|
+
return blobParts[0];
|
35
|
+
}
|
31
36
|
}
|
32
|
-
|
33
|
-
|
34
|
-
|
37
|
+
arrayBuffer() {
|
38
|
+
if (this.blobParts.length === 1) {
|
39
|
+
const blobPart = this.blobParts[0];
|
40
|
+
if (isBlob(blobPart)) {
|
41
|
+
return blobPart.arrayBuffer();
|
42
|
+
}
|
43
|
+
return (0, utils_js_1.fakePromise)(getBlobPartAsBuffer(blobPart));
|
44
|
+
}
|
45
|
+
const jobs = [];
|
46
|
+
const bufferChunks = this.blobParts.map((blobPart, i) => {
|
35
47
|
if (isBlob(blobPart)) {
|
36
|
-
|
37
|
-
|
38
|
-
|
48
|
+
jobs.push(blobPart.arrayBuffer().then(arrayBuf => {
|
49
|
+
bufferChunks[i] = Buffer.from(arrayBuf, undefined, blobPart.size);
|
50
|
+
}));
|
51
|
+
return undefined;
|
39
52
|
}
|
40
53
|
else {
|
41
|
-
|
42
|
-
bufferChunks.push(buf);
|
54
|
+
return getBlobPartAsBuffer(blobPart);
|
43
55
|
}
|
56
|
+
});
|
57
|
+
if (jobs.length > 0) {
|
58
|
+
return Promise.all(jobs).then(() => Buffer.concat(bufferChunks, this._size || undefined));
|
44
59
|
}
|
45
|
-
return Buffer.concat(bufferChunks);
|
60
|
+
return (0, utils_js_1.fakePromise)(Buffer.concat(bufferChunks, this._size || undefined));
|
46
61
|
}
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
async text() {
|
51
|
-
let text = '';
|
52
|
-
for (const blobPart of this.blobParts) {
|
62
|
+
text() {
|
63
|
+
if (this.blobParts.length === 1) {
|
64
|
+
const blobPart = this.blobParts[0];
|
53
65
|
if (typeof blobPart === 'string') {
|
54
|
-
|
55
|
-
}
|
56
|
-
else if (isBlob(blobPart)) {
|
57
|
-
text += await blobPart.text();
|
66
|
+
return (0, utils_js_1.fakePromise)(blobPart);
|
58
67
|
}
|
59
|
-
|
60
|
-
|
61
|
-
text += buf.toString(this.encoding);
|
68
|
+
if (isBlob(blobPart)) {
|
69
|
+
return blobPart.text();
|
62
70
|
}
|
71
|
+
const buf = getBlobPartAsBuffer(blobPart);
|
72
|
+
return (0, utils_js_1.fakePromise)(buf.toString(this.encoding));
|
63
73
|
}
|
64
|
-
return
|
74
|
+
return this.arrayBuffer().then(buf => buf.toString(this.encoding));
|
65
75
|
}
|
66
76
|
get size() {
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
+
if (this._size == null) {
|
78
|
+
this._size = 0;
|
79
|
+
for (const blobPart of this.blobParts) {
|
80
|
+
if (typeof blobPart === 'string') {
|
81
|
+
this._size += Buffer.byteLength(blobPart);
|
82
|
+
}
|
83
|
+
else if (isBlob(blobPart)) {
|
84
|
+
this._size += blobPart.size;
|
85
|
+
}
|
86
|
+
else if ((0, utils_js_1.isArrayBufferView)(blobPart)) {
|
87
|
+
this._size += blobPart.byteLength;
|
88
|
+
}
|
77
89
|
}
|
78
90
|
}
|
79
|
-
return
|
91
|
+
return this._size;
|
80
92
|
}
|
81
93
|
stream() {
|
94
|
+
if (this.blobParts.length === 1) {
|
95
|
+
const blobPart = this.blobParts[0];
|
96
|
+
if (isBlob(blobPart)) {
|
97
|
+
return blobPart.stream();
|
98
|
+
}
|
99
|
+
const buf = getBlobPartAsBuffer(blobPart);
|
100
|
+
return new ReadableStream_js_1.PonyfillReadableStream({
|
101
|
+
start: controller => {
|
102
|
+
controller.enqueue(buf);
|
103
|
+
controller.close();
|
104
|
+
},
|
105
|
+
});
|
106
|
+
}
|
82
107
|
let partQueue = [];
|
83
108
|
return new ReadableStream_js_1.PonyfillReadableStream({
|
84
109
|
start: controller => {
|
package/cjs/Body.js
CHANGED
@@ -14,11 +14,9 @@ var BodyInitType;
|
|
14
14
|
BodyInitType["ReadableStream"] = "ReadableStream";
|
15
15
|
BodyInitType["Blob"] = "Blob";
|
16
16
|
BodyInitType["FormData"] = "FormData";
|
17
|
-
BodyInitType["ArrayBuffer"] = "ArrayBuffer";
|
18
17
|
BodyInitType["String"] = "String";
|
19
18
|
BodyInitType["Readable"] = "Readable";
|
20
19
|
BodyInitType["Buffer"] = "Buffer";
|
21
|
-
BodyInitType["Uint8Array"] = "Uint8Array";
|
22
20
|
})(BodyInitType || (BodyInitType = {}));
|
23
21
|
class PonyfillBody {
|
24
22
|
constructor(bodyInit, options = {}) {
|
@@ -70,19 +68,6 @@ class PonyfillBody {
|
|
70
68
|
}
|
71
69
|
return null;
|
72
70
|
}
|
73
|
-
arrayBuffer() {
|
74
|
-
if (this.bodyType === BodyInitType.ArrayBuffer) {
|
75
|
-
return (0, utils_js_1.fakePromise)(this.bodyInit);
|
76
|
-
}
|
77
|
-
if (this._buffer) {
|
78
|
-
return (0, utils_js_1.fakePromise)(this._buffer.buffer);
|
79
|
-
}
|
80
|
-
if (this.bodyType === BodyInitType.Blob) {
|
81
|
-
const blob = this.bodyInit;
|
82
|
-
return blob.arrayBuffer();
|
83
|
-
}
|
84
|
-
return this.blob().then(blob => blob.arrayBuffer());
|
85
|
-
}
|
86
71
|
_collectChunksFromReadable() {
|
87
72
|
return new Promise((resolve, reject) => {
|
88
73
|
const chunks = [];
|
@@ -110,12 +95,14 @@ class PonyfillBody {
|
|
110
95
|
if (this._buffer) {
|
111
96
|
const blob = new Blob_js_1.PonyfillBlob([this._buffer], {
|
112
97
|
type: this.contentType || '',
|
98
|
+
size: this.contentLength,
|
113
99
|
});
|
114
100
|
return (0, utils_js_1.fakePromise)(blob);
|
115
101
|
}
|
116
102
|
return this._collectChunksFromReadable().then(chunks => {
|
117
103
|
return new Blob_js_1.PonyfillBlob(chunks, {
|
118
104
|
type: this.contentType || '',
|
105
|
+
size: this.contentLength,
|
119
106
|
});
|
120
107
|
});
|
121
108
|
}
|
@@ -183,20 +170,25 @@ class PonyfillBody {
|
|
183
170
|
_body?.readable.pipe(bb);
|
184
171
|
});
|
185
172
|
}
|
186
|
-
|
173
|
+
arrayBuffer() {
|
187
174
|
if (this._buffer) {
|
188
|
-
return (0, utils_js_1.fakePromise)(
|
175
|
+
return (0, utils_js_1.fakePromise)(this._buffer);
|
189
176
|
}
|
190
177
|
if (this.bodyType === BodyInitType.Blob) {
|
191
178
|
if (this.bodyInit instanceof Blob_js_1.PonyfillBlob) {
|
192
|
-
return this.bodyInit.
|
179
|
+
return this.bodyInit.arrayBuffer();
|
193
180
|
}
|
194
181
|
const bodyInitTyped = this.bodyInit;
|
195
182
|
return bodyInitTyped
|
196
183
|
.arrayBuffer()
|
197
184
|
.then(arrayBuffer => Buffer.from(arrayBuffer, undefined, bodyInitTyped.size));
|
198
185
|
}
|
199
|
-
return this._collectChunksFromReadable().then(chunks =>
|
186
|
+
return this._collectChunksFromReadable().then(chunks => {
|
187
|
+
if (chunks.length === 1) {
|
188
|
+
return chunks[0];
|
189
|
+
}
|
190
|
+
return Buffer.concat(chunks, this.contentLength || undefined);
|
191
|
+
});
|
200
192
|
}
|
201
193
|
json() {
|
202
194
|
return this.text().then(text => JSON.parse(text));
|
@@ -205,7 +197,7 @@ class PonyfillBody {
|
|
205
197
|
if (this.bodyType === BodyInitType.String) {
|
206
198
|
return (0, utils_js_1.fakePromise)(this.bodyInit);
|
207
199
|
}
|
208
|
-
return this.
|
200
|
+
return this.arrayBuffer().then(buffer => buffer.toString('utf-8'));
|
209
201
|
}
|
210
202
|
}
|
211
203
|
exports.PonyfillBody = PonyfillBody;
|
@@ -231,12 +223,11 @@ function processBodyInit(bodyInit) {
|
|
231
223
|
},
|
232
224
|
};
|
233
225
|
}
|
234
|
-
if (bodyInit
|
235
|
-
const contentLength = bodyInit.byteLength;
|
226
|
+
if (Buffer.isBuffer(bodyInit)) {
|
236
227
|
return {
|
237
228
|
bodyType: BodyInitType.Buffer,
|
238
|
-
contentLength,
|
239
229
|
contentType: null,
|
230
|
+
contentLength: bodyInit.length,
|
240
231
|
buffer: bodyInit,
|
241
232
|
bodyFactory() {
|
242
233
|
const readable = stream_1.Readable.from(bodyInit);
|
@@ -245,6 +236,20 @@ function processBodyInit(bodyInit) {
|
|
245
236
|
},
|
246
237
|
};
|
247
238
|
}
|
239
|
+
if ((0, utils_js_1.isArrayBufferView)(bodyInit)) {
|
240
|
+
const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
|
241
|
+
return {
|
242
|
+
bodyType: BodyInitType.Buffer,
|
243
|
+
contentLength: bodyInit.byteLength,
|
244
|
+
contentType: null,
|
245
|
+
buffer,
|
246
|
+
bodyFactory() {
|
247
|
+
const readable = stream_1.Readable.from(buffer);
|
248
|
+
const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
|
249
|
+
return body;
|
250
|
+
},
|
251
|
+
};
|
252
|
+
}
|
248
253
|
if (bodyInit instanceof ReadableStream_js_1.PonyfillReadableStream) {
|
249
254
|
return {
|
250
255
|
bodyType: BodyInitType.ReadableStream,
|
@@ -253,7 +258,7 @@ function processBodyInit(bodyInit) {
|
|
253
258
|
contentLength: null,
|
254
259
|
};
|
255
260
|
}
|
256
|
-
if (bodyInit
|
261
|
+
if (isBlob(bodyInit)) {
|
257
262
|
return {
|
258
263
|
bodyType: BodyInitType.Blob,
|
259
264
|
contentType: bodyInit.type,
|
@@ -263,26 +268,11 @@ function processBodyInit(bodyInit) {
|
|
263
268
|
},
|
264
269
|
};
|
265
270
|
}
|
266
|
-
if ((0, utils_js_1.isArrayBufferView)(bodyInit)) {
|
267
|
-
const contentLength = bodyInit.byteLength;
|
268
|
-
const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
|
269
|
-
return {
|
270
|
-
bodyType: BodyInitType.Uint8Array,
|
271
|
-
buffer,
|
272
|
-
contentLength,
|
273
|
-
contentType: null,
|
274
|
-
bodyFactory() {
|
275
|
-
const readable = stream_1.Readable.from(buffer);
|
276
|
-
const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
|
277
|
-
return body;
|
278
|
-
},
|
279
|
-
};
|
280
|
-
}
|
281
271
|
if (bodyInit instanceof ArrayBuffer) {
|
282
272
|
const contentLength = bodyInit.byteLength;
|
283
273
|
const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
|
284
274
|
return {
|
285
|
-
bodyType: BodyInitType.
|
275
|
+
bodyType: BodyInitType.Buffer,
|
286
276
|
contentType: null,
|
287
277
|
contentLength,
|
288
278
|
buffer,
|
@@ -304,18 +294,6 @@ function processBodyInit(bodyInit) {
|
|
304
294
|
},
|
305
295
|
};
|
306
296
|
}
|
307
|
-
if (isBlob(bodyInit)) {
|
308
|
-
return {
|
309
|
-
bodyType: BodyInitType.Blob,
|
310
|
-
contentType: bodyInit.type,
|
311
|
-
contentLength: bodyInit.size,
|
312
|
-
bodyFactory() {
|
313
|
-
const bodyStream = bodyInit.stream();
|
314
|
-
const body = new ReadableStream_js_1.PonyfillReadableStream(bodyStream);
|
315
|
-
return body;
|
316
|
-
},
|
317
|
-
};
|
318
|
-
}
|
319
297
|
if (isURLSearchParams(bodyInit)) {
|
320
298
|
const contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
|
321
299
|
return {
|
package/cjs/ReadableStream.js
CHANGED
@@ -36,7 +36,7 @@ function createController(desiredSize, readable) {
|
|
36
36
|
_flush() {
|
37
37
|
flushed = true;
|
38
38
|
if (chunks.length > 0) {
|
39
|
-
const concatenated = Buffer.concat(chunks);
|
39
|
+
const concatenated = chunks.length > 1 ? Buffer.concat(chunks) : chunks[0];
|
40
40
|
readable.push(concatenated);
|
41
41
|
chunks = [];
|
42
42
|
}
|
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.PonyfillBtoa = exports.PonyfillTextDecoder = exports.PonyfillTextEncoder = void 0;
|
4
|
+
const utils_js_1 = require("./utils.js");
|
4
5
|
class PonyfillTextEncoder {
|
5
6
|
constructor(encoding = 'utf-8') {
|
6
7
|
this.encoding = encoding;
|
@@ -29,6 +30,12 @@ class PonyfillTextDecoder {
|
|
29
30
|
}
|
30
31
|
}
|
31
32
|
decode(input) {
|
33
|
+
if (Buffer.isBuffer(input)) {
|
34
|
+
return input.toString(this.encoding);
|
35
|
+
}
|
36
|
+
if ((0, utils_js_1.isArrayBufferView)(input)) {
|
37
|
+
return Buffer.from(input.buffer, input.byteOffset, input.byteLength).toString(this.encoding);
|
38
|
+
}
|
32
39
|
return Buffer.from(input).toString(this.encoding);
|
33
40
|
}
|
34
41
|
}
|
package/cjs/fetch.js
CHANGED
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fetchPonyfill = void 0;
|
4
4
|
const fs_1 = require("fs");
|
5
5
|
const url_1 = require("url");
|
6
|
-
const Blob_js_1 = require("./Blob.js");
|
7
6
|
const fetchCurl_js_1 = require("./fetchCurl.js");
|
8
7
|
const fetchNodeHttp_js_1 = require("./fetchNodeHttp.js");
|
9
8
|
const Request_js_1 = require("./Request.js");
|
@@ -21,10 +20,12 @@ function getResponseForDataUri(url) {
|
|
21
20
|
if (mimeType.endsWith(BASE64_SUFFIX)) {
|
22
21
|
const buffer = Buffer.from(data, 'base64url');
|
23
22
|
const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
|
24
|
-
|
25
|
-
return new Response_js_1.PonyfillResponse(file, {
|
23
|
+
return new Response_js_1.PonyfillResponse(buffer, {
|
26
24
|
status: 200,
|
27
25
|
statusText: 'OK',
|
26
|
+
headers: {
|
27
|
+
'content-type': realMimeType,
|
28
|
+
},
|
28
29
|
});
|
29
30
|
}
|
30
31
|
return new Response_js_1.PonyfillResponse(data, {
|
package/cjs/utils.js
CHANGED
@@ -61,11 +61,7 @@ function fakePromise(value) {
|
|
61
61
|
}
|
62
62
|
exports.fakePromise = fakePromise;
|
63
63
|
function isArrayBufferView(obj) {
|
64
|
-
return
|
65
|
-
typeof obj === 'object' &&
|
66
|
-
obj.buffer != null &&
|
67
|
-
obj.byteLength != null &&
|
68
|
-
obj.byteOffset != null);
|
64
|
+
return obj != null && obj.buffer != null && obj.byteLength != null && obj.byteOffset != null;
|
69
65
|
}
|
70
66
|
exports.isArrayBufferView = isArrayBufferView;
|
71
67
|
function isNodeReadable(obj) {
|
package/esm/Blob.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
|
2
2
|
import { PonyfillReadableStream } from './ReadableStream.js';
|
3
|
-
import { isArrayBufferView } from './utils.js';
|
3
|
+
import { fakePromise, isArrayBufferView } from './utils.js';
|
4
4
|
function getBlobPartAsBuffer(blobPart) {
|
5
5
|
if (typeof blobPart === 'string') {
|
6
6
|
return Buffer.from(blobPart);
|
@@ -23,59 +23,84 @@ function isBlob(obj) {
|
|
23
23
|
export class PonyfillBlob {
|
24
24
|
constructor(blobParts, options) {
|
25
25
|
this.blobParts = blobParts;
|
26
|
+
this._size = null;
|
26
27
|
this.type = options?.type || 'application/octet-stream';
|
27
28
|
this.encoding = options?.encoding || 'utf8';
|
29
|
+
this._size = options?.size || null;
|
30
|
+
if (blobParts.length === 1 && isBlob(blobParts[0])) {
|
31
|
+
return blobParts[0];
|
32
|
+
}
|
28
33
|
}
|
29
|
-
|
30
|
-
|
31
|
-
|
34
|
+
arrayBuffer() {
|
35
|
+
if (this.blobParts.length === 1) {
|
36
|
+
const blobPart = this.blobParts[0];
|
37
|
+
if (isBlob(blobPart)) {
|
38
|
+
return blobPart.arrayBuffer();
|
39
|
+
}
|
40
|
+
return fakePromise(getBlobPartAsBuffer(blobPart));
|
41
|
+
}
|
42
|
+
const jobs = [];
|
43
|
+
const bufferChunks = this.blobParts.map((blobPart, i) => {
|
32
44
|
if (isBlob(blobPart)) {
|
33
|
-
|
34
|
-
|
35
|
-
|
45
|
+
jobs.push(blobPart.arrayBuffer().then(arrayBuf => {
|
46
|
+
bufferChunks[i] = Buffer.from(arrayBuf, undefined, blobPart.size);
|
47
|
+
}));
|
48
|
+
return undefined;
|
36
49
|
}
|
37
50
|
else {
|
38
|
-
|
39
|
-
bufferChunks.push(buf);
|
51
|
+
return getBlobPartAsBuffer(blobPart);
|
40
52
|
}
|
53
|
+
});
|
54
|
+
if (jobs.length > 0) {
|
55
|
+
return Promise.all(jobs).then(() => Buffer.concat(bufferChunks, this._size || undefined));
|
41
56
|
}
|
42
|
-
return Buffer.concat(bufferChunks);
|
57
|
+
return fakePromise(Buffer.concat(bufferChunks, this._size || undefined));
|
43
58
|
}
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
async text() {
|
48
|
-
let text = '';
|
49
|
-
for (const blobPart of this.blobParts) {
|
59
|
+
text() {
|
60
|
+
if (this.blobParts.length === 1) {
|
61
|
+
const blobPart = this.blobParts[0];
|
50
62
|
if (typeof blobPart === 'string') {
|
51
|
-
|
52
|
-
}
|
53
|
-
else if (isBlob(blobPart)) {
|
54
|
-
text += await blobPart.text();
|
63
|
+
return fakePromise(blobPart);
|
55
64
|
}
|
56
|
-
|
57
|
-
|
58
|
-
text += buf.toString(this.encoding);
|
65
|
+
if (isBlob(blobPart)) {
|
66
|
+
return blobPart.text();
|
59
67
|
}
|
68
|
+
const buf = getBlobPartAsBuffer(blobPart);
|
69
|
+
return fakePromise(buf.toString(this.encoding));
|
60
70
|
}
|
61
|
-
return
|
71
|
+
return this.arrayBuffer().then(buf => buf.toString(this.encoding));
|
62
72
|
}
|
63
73
|
get size() {
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
+
if (this._size == null) {
|
75
|
+
this._size = 0;
|
76
|
+
for (const blobPart of this.blobParts) {
|
77
|
+
if (typeof blobPart === 'string') {
|
78
|
+
this._size += Buffer.byteLength(blobPart);
|
79
|
+
}
|
80
|
+
else if (isBlob(blobPart)) {
|
81
|
+
this._size += blobPart.size;
|
82
|
+
}
|
83
|
+
else if (isArrayBufferView(blobPart)) {
|
84
|
+
this._size += blobPart.byteLength;
|
85
|
+
}
|
74
86
|
}
|
75
87
|
}
|
76
|
-
return
|
88
|
+
return this._size;
|
77
89
|
}
|
78
90
|
stream() {
|
91
|
+
if (this.blobParts.length === 1) {
|
92
|
+
const blobPart = this.blobParts[0];
|
93
|
+
if (isBlob(blobPart)) {
|
94
|
+
return blobPart.stream();
|
95
|
+
}
|
96
|
+
const buf = getBlobPartAsBuffer(blobPart);
|
97
|
+
return new PonyfillReadableStream({
|
98
|
+
start: controller => {
|
99
|
+
controller.enqueue(buf);
|
100
|
+
controller.close();
|
101
|
+
},
|
102
|
+
});
|
103
|
+
}
|
79
104
|
let partQueue = [];
|
80
105
|
return new PonyfillReadableStream({
|
81
106
|
start: controller => {
|
package/esm/Body.js
CHANGED
@@ -10,11 +10,9 @@ var BodyInitType;
|
|
10
10
|
BodyInitType["ReadableStream"] = "ReadableStream";
|
11
11
|
BodyInitType["Blob"] = "Blob";
|
12
12
|
BodyInitType["FormData"] = "FormData";
|
13
|
-
BodyInitType["ArrayBuffer"] = "ArrayBuffer";
|
14
13
|
BodyInitType["String"] = "String";
|
15
14
|
BodyInitType["Readable"] = "Readable";
|
16
15
|
BodyInitType["Buffer"] = "Buffer";
|
17
|
-
BodyInitType["Uint8Array"] = "Uint8Array";
|
18
16
|
})(BodyInitType || (BodyInitType = {}));
|
19
17
|
export class PonyfillBody {
|
20
18
|
constructor(bodyInit, options = {}) {
|
@@ -66,19 +64,6 @@ export class PonyfillBody {
|
|
66
64
|
}
|
67
65
|
return null;
|
68
66
|
}
|
69
|
-
arrayBuffer() {
|
70
|
-
if (this.bodyType === BodyInitType.ArrayBuffer) {
|
71
|
-
return fakePromise(this.bodyInit);
|
72
|
-
}
|
73
|
-
if (this._buffer) {
|
74
|
-
return fakePromise(this._buffer.buffer);
|
75
|
-
}
|
76
|
-
if (this.bodyType === BodyInitType.Blob) {
|
77
|
-
const blob = this.bodyInit;
|
78
|
-
return blob.arrayBuffer();
|
79
|
-
}
|
80
|
-
return this.blob().then(blob => blob.arrayBuffer());
|
81
|
-
}
|
82
67
|
_collectChunksFromReadable() {
|
83
68
|
return new Promise((resolve, reject) => {
|
84
69
|
const chunks = [];
|
@@ -106,12 +91,14 @@ export class PonyfillBody {
|
|
106
91
|
if (this._buffer) {
|
107
92
|
const blob = new PonyfillBlob([this._buffer], {
|
108
93
|
type: this.contentType || '',
|
94
|
+
size: this.contentLength,
|
109
95
|
});
|
110
96
|
return fakePromise(blob);
|
111
97
|
}
|
112
98
|
return this._collectChunksFromReadable().then(chunks => {
|
113
99
|
return new PonyfillBlob(chunks, {
|
114
100
|
type: this.contentType || '',
|
101
|
+
size: this.contentLength,
|
115
102
|
});
|
116
103
|
});
|
117
104
|
}
|
@@ -179,20 +166,25 @@ export class PonyfillBody {
|
|
179
166
|
_body?.readable.pipe(bb);
|
180
167
|
});
|
181
168
|
}
|
182
|
-
|
169
|
+
arrayBuffer() {
|
183
170
|
if (this._buffer) {
|
184
|
-
return fakePromise(
|
171
|
+
return fakePromise(this._buffer);
|
185
172
|
}
|
186
173
|
if (this.bodyType === BodyInitType.Blob) {
|
187
174
|
if (this.bodyInit instanceof PonyfillBlob) {
|
188
|
-
return this.bodyInit.
|
175
|
+
return this.bodyInit.arrayBuffer();
|
189
176
|
}
|
190
177
|
const bodyInitTyped = this.bodyInit;
|
191
178
|
return bodyInitTyped
|
192
179
|
.arrayBuffer()
|
193
180
|
.then(arrayBuffer => Buffer.from(arrayBuffer, undefined, bodyInitTyped.size));
|
194
181
|
}
|
195
|
-
return this._collectChunksFromReadable().then(chunks =>
|
182
|
+
return this._collectChunksFromReadable().then(chunks => {
|
183
|
+
if (chunks.length === 1) {
|
184
|
+
return chunks[0];
|
185
|
+
}
|
186
|
+
return Buffer.concat(chunks, this.contentLength || undefined);
|
187
|
+
});
|
196
188
|
}
|
197
189
|
json() {
|
198
190
|
return this.text().then(text => JSON.parse(text));
|
@@ -201,7 +193,7 @@ export class PonyfillBody {
|
|
201
193
|
if (this.bodyType === BodyInitType.String) {
|
202
194
|
return fakePromise(this.bodyInit);
|
203
195
|
}
|
204
|
-
return this.
|
196
|
+
return this.arrayBuffer().then(buffer => buffer.toString('utf-8'));
|
205
197
|
}
|
206
198
|
}
|
207
199
|
function processBodyInit(bodyInit) {
|
@@ -226,12 +218,11 @@ function processBodyInit(bodyInit) {
|
|
226
218
|
},
|
227
219
|
};
|
228
220
|
}
|
229
|
-
if (bodyInit
|
230
|
-
const contentLength = bodyInit.byteLength;
|
221
|
+
if (Buffer.isBuffer(bodyInit)) {
|
231
222
|
return {
|
232
223
|
bodyType: BodyInitType.Buffer,
|
233
|
-
contentLength,
|
234
224
|
contentType: null,
|
225
|
+
contentLength: bodyInit.length,
|
235
226
|
buffer: bodyInit,
|
236
227
|
bodyFactory() {
|
237
228
|
const readable = Readable.from(bodyInit);
|
@@ -240,6 +231,20 @@ function processBodyInit(bodyInit) {
|
|
240
231
|
},
|
241
232
|
};
|
242
233
|
}
|
234
|
+
if (isArrayBufferView(bodyInit)) {
|
235
|
+
const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
|
236
|
+
return {
|
237
|
+
bodyType: BodyInitType.Buffer,
|
238
|
+
contentLength: bodyInit.byteLength,
|
239
|
+
contentType: null,
|
240
|
+
buffer,
|
241
|
+
bodyFactory() {
|
242
|
+
const readable = Readable.from(buffer);
|
243
|
+
const body = new PonyfillReadableStream(readable);
|
244
|
+
return body;
|
245
|
+
},
|
246
|
+
};
|
247
|
+
}
|
243
248
|
if (bodyInit instanceof PonyfillReadableStream) {
|
244
249
|
return {
|
245
250
|
bodyType: BodyInitType.ReadableStream,
|
@@ -248,7 +253,7 @@ function processBodyInit(bodyInit) {
|
|
248
253
|
contentLength: null,
|
249
254
|
};
|
250
255
|
}
|
251
|
-
if (bodyInit
|
256
|
+
if (isBlob(bodyInit)) {
|
252
257
|
return {
|
253
258
|
bodyType: BodyInitType.Blob,
|
254
259
|
contentType: bodyInit.type,
|
@@ -258,26 +263,11 @@ function processBodyInit(bodyInit) {
|
|
258
263
|
},
|
259
264
|
};
|
260
265
|
}
|
261
|
-
if (isArrayBufferView(bodyInit)) {
|
262
|
-
const contentLength = bodyInit.byteLength;
|
263
|
-
const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
|
264
|
-
return {
|
265
|
-
bodyType: BodyInitType.Uint8Array,
|
266
|
-
buffer,
|
267
|
-
contentLength,
|
268
|
-
contentType: null,
|
269
|
-
bodyFactory() {
|
270
|
-
const readable = Readable.from(buffer);
|
271
|
-
const body = new PonyfillReadableStream(readable);
|
272
|
-
return body;
|
273
|
-
},
|
274
|
-
};
|
275
|
-
}
|
276
266
|
if (bodyInit instanceof ArrayBuffer) {
|
277
267
|
const contentLength = bodyInit.byteLength;
|
278
268
|
const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
|
279
269
|
return {
|
280
|
-
bodyType: BodyInitType.
|
270
|
+
bodyType: BodyInitType.Buffer,
|
281
271
|
contentType: null,
|
282
272
|
contentLength,
|
283
273
|
buffer,
|
@@ -299,18 +289,6 @@ function processBodyInit(bodyInit) {
|
|
299
289
|
},
|
300
290
|
};
|
301
291
|
}
|
302
|
-
if (isBlob(bodyInit)) {
|
303
|
-
return {
|
304
|
-
bodyType: BodyInitType.Blob,
|
305
|
-
contentType: bodyInit.type,
|
306
|
-
contentLength: bodyInit.size,
|
307
|
-
bodyFactory() {
|
308
|
-
const bodyStream = bodyInit.stream();
|
309
|
-
const body = new PonyfillReadableStream(bodyStream);
|
310
|
-
return body;
|
311
|
-
},
|
312
|
-
};
|
313
|
-
}
|
314
292
|
if (isURLSearchParams(bodyInit)) {
|
315
293
|
const contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
|
316
294
|
return {
|
package/esm/ReadableStream.js
CHANGED
@@ -33,7 +33,7 @@ function createController(desiredSize, readable) {
|
|
33
33
|
_flush() {
|
34
34
|
flushed = true;
|
35
35
|
if (chunks.length > 0) {
|
36
|
-
const concatenated = Buffer.concat(chunks);
|
36
|
+
const concatenated = chunks.length > 1 ? Buffer.concat(chunks) : chunks[0];
|
37
37
|
readable.push(concatenated);
|
38
38
|
chunks = [];
|
39
39
|
}
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import { isArrayBufferView } from './utils.js';
|
1
2
|
export class PonyfillTextEncoder {
|
2
3
|
constructor(encoding = 'utf-8') {
|
3
4
|
this.encoding = encoding;
|
@@ -25,6 +26,12 @@ export class PonyfillTextDecoder {
|
|
25
26
|
}
|
26
27
|
}
|
27
28
|
decode(input) {
|
29
|
+
if (Buffer.isBuffer(input)) {
|
30
|
+
return input.toString(this.encoding);
|
31
|
+
}
|
32
|
+
if (isArrayBufferView(input)) {
|
33
|
+
return Buffer.from(input.buffer, input.byteOffset, input.byteLength).toString(this.encoding);
|
34
|
+
}
|
28
35
|
return Buffer.from(input).toString(this.encoding);
|
29
36
|
}
|
30
37
|
}
|
package/esm/fetch.js
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
import { createReadStream } from 'fs';
|
2
2
|
import { fileURLToPath } from 'url';
|
3
|
-
import { PonyfillBlob } from './Blob.js';
|
4
3
|
import { fetchCurl } from './fetchCurl.js';
|
5
4
|
import { fetchNodeHttp } from './fetchNodeHttp.js';
|
6
5
|
import { PonyfillRequest } from './Request.js';
|
@@ -18,10 +17,12 @@ function getResponseForDataUri(url) {
|
|
18
17
|
if (mimeType.endsWith(BASE64_SUFFIX)) {
|
19
18
|
const buffer = Buffer.from(data, 'base64url');
|
20
19
|
const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
|
21
|
-
|
22
|
-
return new PonyfillResponse(file, {
|
20
|
+
return new PonyfillResponse(buffer, {
|
23
21
|
status: 200,
|
24
22
|
statusText: 'OK',
|
23
|
+
headers: {
|
24
|
+
'content-type': realMimeType,
|
25
|
+
},
|
25
26
|
});
|
26
27
|
}
|
27
28
|
return new PonyfillResponse(data, {
|
package/esm/utils.js
CHANGED
@@ -55,11 +55,7 @@ export function fakePromise(value) {
|
|
55
55
|
};
|
56
56
|
}
|
57
57
|
export function isArrayBufferView(obj) {
|
58
|
-
return
|
59
|
-
typeof obj === 'object' &&
|
60
|
-
obj.buffer != null &&
|
61
|
-
obj.byteLength != null &&
|
62
|
-
obj.byteOffset != null);
|
58
|
+
return obj != null && obj.buffer != null && obj.byteLength != null && obj.byteOffset != null;
|
63
59
|
}
|
64
60
|
export function isNodeReadable(obj) {
|
65
61
|
return obj != null && obj.pipe != null;
|
package/package.json
CHANGED
package/typings/Blob.d.cts
CHANGED
@@ -10,14 +10,18 @@ interface BlobOptions {
|
|
10
10
|
* is performed.
|
11
11
|
*/
|
12
12
|
type?: string | undefined;
|
13
|
+
/**
|
14
|
+
* The size of the Blob object in bytes.
|
15
|
+
*/
|
16
|
+
size?: number | null;
|
13
17
|
}
|
14
18
|
export declare class PonyfillBlob implements Blob {
|
15
19
|
private blobParts;
|
16
20
|
type: string;
|
17
21
|
private encoding;
|
22
|
+
private _size;
|
18
23
|
constructor(blobParts: BlobPart[], options?: BlobOptions);
|
19
|
-
|
20
|
-
arrayBuffer(): Promise<ArrayBuffer | SharedArrayBuffer>;
|
24
|
+
arrayBuffer(): Promise<Buffer>;
|
21
25
|
text(): Promise<string>;
|
22
26
|
get size(): number;
|
23
27
|
stream(): any;
|
package/typings/Blob.d.ts
CHANGED
@@ -10,14 +10,18 @@ interface BlobOptions {
|
|
10
10
|
* is performed.
|
11
11
|
*/
|
12
12
|
type?: string | undefined;
|
13
|
+
/**
|
14
|
+
* The size of the Blob object in bytes.
|
15
|
+
*/
|
16
|
+
size?: number | null;
|
13
17
|
}
|
14
18
|
export declare class PonyfillBlob implements Blob {
|
15
19
|
private blobParts;
|
16
20
|
type: string;
|
17
21
|
private encoding;
|
22
|
+
private _size;
|
18
23
|
constructor(blobParts: BlobPart[], options?: BlobOptions);
|
19
|
-
|
20
|
-
arrayBuffer(): Promise<ArrayBuffer | SharedArrayBuffer>;
|
24
|
+
arrayBuffer(): Promise<Buffer>;
|
21
25
|
text(): Promise<string>;
|
22
26
|
get size(): number;
|
23
27
|
stream(): any;
|
package/typings/Body.d.cts
CHANGED
@@ -30,13 +30,12 @@ export declare class PonyfillBody<TJSON = any> implements Body {
|
|
30
30
|
private _buffer?;
|
31
31
|
private generateBody;
|
32
32
|
get body(): PonyfillReadableStream<Uint8Array> | null;
|
33
|
-
arrayBuffer(): Promise<ArrayBuffer>;
|
34
33
|
_collectChunksFromReadable(): Promise<Uint8Array[]>;
|
35
34
|
blob(): Promise<PonyfillBlob>;
|
36
35
|
formData(opts?: {
|
37
36
|
formDataLimits: FormDataLimits;
|
38
37
|
}): Promise<PonyfillFormData>;
|
39
|
-
|
38
|
+
arrayBuffer(): Promise<Buffer>;
|
40
39
|
json(): Promise<TJSON>;
|
41
40
|
text(): Promise<string>;
|
42
41
|
}
|
package/typings/Body.d.ts
CHANGED
@@ -30,13 +30,12 @@ export declare class PonyfillBody<TJSON = any> implements Body {
|
|
30
30
|
private _buffer?;
|
31
31
|
private generateBody;
|
32
32
|
get body(): PonyfillReadableStream<Uint8Array> | null;
|
33
|
-
arrayBuffer(): Promise<ArrayBuffer>;
|
34
33
|
_collectChunksFromReadable(): Promise<Uint8Array[]>;
|
35
34
|
blob(): Promise<PonyfillBlob>;
|
36
35
|
formData(opts?: {
|
37
36
|
formDataLimits: FormDataLimits;
|
38
37
|
}): Promise<PonyfillFormData>;
|
39
|
-
|
38
|
+
arrayBuffer(): Promise<Buffer>;
|
40
39
|
json(): Promise<TJSON>;
|
41
40
|
text(): Promise<string>;
|
42
41
|
}
|
@@ -9,7 +9,7 @@ export declare class PonyfillTextDecoder implements TextDecoder {
|
|
9
9
|
encoding: BufferEncoding;
|
10
10
|
fatal: boolean;
|
11
11
|
ignoreBOM: boolean;
|
12
|
-
constructor(encoding
|
13
|
-
decode(input:
|
12
|
+
constructor(encoding?: BufferEncoding, options?: TextDecoderOptions);
|
13
|
+
decode(input: BufferSource): string;
|
14
14
|
}
|
15
15
|
export declare function PonyfillBtoa(input: string): string;
|
@@ -9,7 +9,7 @@ export declare class PonyfillTextDecoder implements TextDecoder {
|
|
9
9
|
encoding: BufferEncoding;
|
10
10
|
fatal: boolean;
|
11
11
|
ignoreBOM: boolean;
|
12
|
-
constructor(encoding
|
13
|
-
decode(input:
|
12
|
+
constructor(encoding?: BufferEncoding, options?: TextDecoderOptions);
|
13
|
+
decode(input: BufferSource): string;
|
14
14
|
}
|
15
15
|
export declare function PonyfillBtoa(input: string): string;
|