@whatwg-node/node-fetch 0.0.7-alpha-20230209150810-7d2cb9f → 0.0.7-alpha-20230209162208-3a05ba1
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/Blob.d.ts +10 -5
- package/index.js +119 -29
- package/index.mjs +119 -29
- package/package.json +1 -1
- package/utils.d.ts +1 -0
package/Blob.d.ts
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
/// <reference types="node" />
|
2
|
-
import {
|
3
|
-
declare
|
4
|
-
|
2
|
+
import { BlobOptions } from 'buffer';
|
3
|
+
export declare class PonyfillBlob implements Blob {
|
4
|
+
private blobParts;
|
5
|
+
type: string;
|
6
|
+
private encoding;
|
7
|
+
constructor(blobParts: BlobPart[], options?: BlobOptions);
|
8
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
9
|
+
text(): Promise<string>;
|
10
|
+
get size(): number;
|
5
11
|
stream(): any;
|
6
|
-
slice(
|
12
|
+
slice(): any;
|
7
13
|
}
|
8
14
|
export interface PonyfillBlob {
|
9
15
|
prototype: Blob;
|
10
16
|
new (blobParts?: BlobPart[], options?: BlobPropertyBag): Blob;
|
11
17
|
}
|
12
|
-
export {};
|
package/index.js
CHANGED
@@ -9,7 +9,6 @@ const http = require('http');
|
|
9
9
|
const https = require('https');
|
10
10
|
const stream = require('stream');
|
11
11
|
const url = require('url');
|
12
|
-
const buffer = require('buffer');
|
13
12
|
const events = require('@whatwg-node/events');
|
14
13
|
const busboy = _interopDefault(require('busboy'));
|
15
14
|
|
@@ -198,26 +197,127 @@ class PonyfillReadableStream {
|
|
198
197
|
}
|
199
198
|
}
|
200
199
|
|
201
|
-
|
202
|
-
|
203
|
-
|
200
|
+
function getHeadersObj(headers) {
|
201
|
+
if (headers == null || !('forEach' in headers)) {
|
202
|
+
return headers;
|
204
203
|
}
|
204
|
+
const obj = {};
|
205
|
+
headers.forEach((value, key) => {
|
206
|
+
obj[key] = value;
|
207
|
+
});
|
208
|
+
return obj;
|
209
|
+
}
|
210
|
+
function uint8ArrayToBuffer(uint8array) {
|
211
|
+
return uint8array.buffer.slice(uint8array.byteOffset, uint8array.byteOffset + uint8array.byteLength);
|
212
|
+
}
|
213
|
+
|
214
|
+
function getBlobPartAsBuffer(blobPart) {
|
215
|
+
if (typeof blobPart === 'string') {
|
216
|
+
return Buffer.from(blobPart);
|
217
|
+
}
|
218
|
+
else if (Buffer.isBuffer(blobPart)) {
|
219
|
+
return blobPart;
|
220
|
+
}
|
221
|
+
else if (blobPart instanceof Uint8Array) {
|
222
|
+
return Buffer.from(blobPart);
|
223
|
+
}
|
224
|
+
else if ('buffer' in blobPart) {
|
225
|
+
return Buffer.from(blobPart.buffer, blobPart.byteOffset, blobPart.byteLength);
|
226
|
+
}
|
227
|
+
else {
|
228
|
+
return Buffer.from(blobPart);
|
229
|
+
}
|
230
|
+
}
|
231
|
+
function isBlob(obj) {
|
232
|
+
return obj != null && typeof obj === 'object' && obj.stream != null;
|
205
233
|
}
|
206
234
|
// Will be removed after v14 reaches EOL
|
207
235
|
// Needed because v14 doesn't have .stream() implemented
|
208
|
-
class PonyfillBlob
|
236
|
+
class PonyfillBlob {
|
237
|
+
constructor(blobParts, options) {
|
238
|
+
this.blobParts = blobParts;
|
239
|
+
this.type = (options === null || options === void 0 ? void 0 : options.type) || 'application/octet-stream';
|
240
|
+
this.encoding = (options === null || options === void 0 ? void 0 : options.encoding) || 'utf8';
|
241
|
+
}
|
242
|
+
async arrayBuffer() {
|
243
|
+
const bufferChunks = [];
|
244
|
+
for (const blobPart of this.blobParts) {
|
245
|
+
if (isBlob(blobPart)) {
|
246
|
+
const arrayBuf = await blobPart.arrayBuffer();
|
247
|
+
const buf = Buffer.from(arrayBuf, undefined, blobPart.size);
|
248
|
+
bufferChunks.push(buf);
|
249
|
+
}
|
250
|
+
else {
|
251
|
+
const buf = getBlobPartAsBuffer(blobPart);
|
252
|
+
bufferChunks.push(buf);
|
253
|
+
}
|
254
|
+
}
|
255
|
+
return uint8ArrayToBuffer(Buffer.concat(bufferChunks));
|
256
|
+
}
|
257
|
+
async text() {
|
258
|
+
let text = '';
|
259
|
+
for (const blobPart of this.blobParts) {
|
260
|
+
if (typeof blobPart === 'string') {
|
261
|
+
text += blobPart;
|
262
|
+
}
|
263
|
+
else if ('text' in blobPart) {
|
264
|
+
text += await blobPart.text();
|
265
|
+
}
|
266
|
+
else {
|
267
|
+
const buf = getBlobPartAsBuffer(blobPart);
|
268
|
+
text += buf.toString(this.encoding);
|
269
|
+
}
|
270
|
+
}
|
271
|
+
return text;
|
272
|
+
}
|
273
|
+
get size() {
|
274
|
+
let size = 0;
|
275
|
+
for (const blobPart of this.blobParts) {
|
276
|
+
if (typeof blobPart === 'string') {
|
277
|
+
size += Buffer.byteLength(blobPart);
|
278
|
+
}
|
279
|
+
else if (isBlob(blobPart)) {
|
280
|
+
size += blobPart.size;
|
281
|
+
}
|
282
|
+
else if ('length' in blobPart) {
|
283
|
+
size += blobPart.length;
|
284
|
+
}
|
285
|
+
else if ('byteLength' in blobPart) {
|
286
|
+
size += blobPart.byteLength;
|
287
|
+
}
|
288
|
+
}
|
289
|
+
return size;
|
290
|
+
}
|
209
291
|
stream() {
|
292
|
+
let partQueue = [];
|
210
293
|
return new PonyfillReadableStream({
|
211
|
-
start:
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
294
|
+
start: controller => {
|
295
|
+
partQueue = [...this.blobParts];
|
296
|
+
if (partQueue.length === 0) {
|
297
|
+
controller.close();
|
298
|
+
}
|
299
|
+
},
|
300
|
+
pull: async (controller) => {
|
301
|
+
const blobPart = partQueue.pop();
|
302
|
+
if (blobPart) {
|
303
|
+
if (isBlob(blobPart)) {
|
304
|
+
for await (const chunk of blobPart.stream()) {
|
305
|
+
controller.enqueue(chunk);
|
306
|
+
}
|
307
|
+
}
|
308
|
+
else {
|
309
|
+
const buf = getBlobPartAsBuffer(blobPart);
|
310
|
+
controller.enqueue(buf);
|
311
|
+
}
|
312
|
+
}
|
313
|
+
else {
|
314
|
+
controller.close();
|
315
|
+
}
|
216
316
|
},
|
217
317
|
});
|
218
318
|
}
|
219
|
-
slice(
|
220
|
-
|
319
|
+
slice() {
|
320
|
+
throw new Error('Not implemented');
|
221
321
|
}
|
222
322
|
}
|
223
323
|
|
@@ -282,7 +382,7 @@ class PonyfillFormData {
|
|
282
382
|
values = [];
|
283
383
|
this.map.set(name, values);
|
284
384
|
}
|
285
|
-
const entry = isBlob(value)
|
385
|
+
const entry = isBlob$1(value)
|
286
386
|
? getNormalizedFile(name, value, fileName)
|
287
387
|
: value;
|
288
388
|
values.push(entry);
|
@@ -301,7 +401,7 @@ class PonyfillFormData {
|
|
301
401
|
return this.map.has(name);
|
302
402
|
}
|
303
403
|
set(name, value, fileName) {
|
304
|
-
const entry = isBlob(value)
|
404
|
+
const entry = isBlob$1(value)
|
305
405
|
? getNormalizedFile(name, value, fileName)
|
306
406
|
: value;
|
307
407
|
this.map.set(name, [entry]);
|
@@ -383,7 +483,7 @@ function getNormalizedFile(name, blob, fileName) {
|
|
383
483
|
}
|
384
484
|
return new PonyfillFile([blob], fileName || name, { type: blob.type });
|
385
485
|
}
|
386
|
-
function isBlob(value) {
|
486
|
+
function isBlob$1(value) {
|
387
487
|
return value != null && typeof value === 'object' && typeof value.arrayBuffer === 'function';
|
388
488
|
}
|
389
489
|
|
@@ -452,7 +552,8 @@ class PonyfillBody {
|
|
452
552
|
return this.bodyInit;
|
453
553
|
}
|
454
554
|
if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.Buffer) {
|
455
|
-
|
555
|
+
const typedBodyInit = this.bodyInit;
|
556
|
+
return uint8ArrayToBuffer(typedBodyInit);
|
456
557
|
}
|
457
558
|
const blob = await this.blob();
|
458
559
|
return blob.arrayBuffer();
|
@@ -653,7 +754,7 @@ function processBodyInit(bodyInit) {
|
|
653
754
|
contentLength,
|
654
755
|
contentType: null,
|
655
756
|
bodyFactory() {
|
656
|
-
const buffer = Buffer.from(bodyInit
|
757
|
+
const buffer = Buffer.from(bodyInit);
|
657
758
|
const readable = stream.Readable.from(buffer);
|
658
759
|
const body = new PonyfillReadableStream(readable);
|
659
760
|
return body;
|
@@ -927,17 +1028,6 @@ class PonyfillResponse extends PonyfillBody {
|
|
927
1028
|
}
|
928
1029
|
}
|
929
1030
|
|
930
|
-
function getHeadersObj(headers) {
|
931
|
-
if (headers == null || !('forEach' in headers)) {
|
932
|
-
return headers;
|
933
|
-
}
|
934
|
-
const obj = {};
|
935
|
-
headers.forEach((value, key) => {
|
936
|
-
obj[key] = value;
|
937
|
-
});
|
938
|
-
return obj;
|
939
|
-
}
|
940
|
-
|
941
1031
|
function getResponseForFile(url$1) {
|
942
1032
|
const path = url.fileURLToPath(url$1);
|
943
1033
|
const readable = fs.createReadStream(path);
|
@@ -966,7 +1056,7 @@ function fetchPonyfill(info, init) {
|
|
966
1056
|
const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
|
967
1057
|
const data = decodeURIComponent(datas.join(','));
|
968
1058
|
if (mimeType.endsWith(BASE64_SUFFIX)) {
|
969
|
-
const buffer = Buffer.from(data, '
|
1059
|
+
const buffer = Buffer.from(data, 'base64url');
|
970
1060
|
const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
|
971
1061
|
const file = new PonyfillBlob([buffer], { type: realMimeType });
|
972
1062
|
const response = new PonyfillResponse(file, {
|
package/index.mjs
CHANGED
@@ -3,7 +3,6 @@ import { request as request$1 } from 'http';
|
|
3
3
|
import { request } from 'https';
|
4
4
|
import { Readable } from 'stream';
|
5
5
|
import { fileURLToPath } from 'url';
|
6
|
-
import { Blob } from 'buffer';
|
7
6
|
import { EventTarget, CustomEvent } from '@whatwg-node/events';
|
8
7
|
import busboy from 'busboy';
|
9
8
|
|
@@ -192,26 +191,127 @@ class PonyfillReadableStream {
|
|
192
191
|
}
|
193
192
|
}
|
194
193
|
|
195
|
-
|
196
|
-
|
197
|
-
|
194
|
+
function getHeadersObj(headers) {
|
195
|
+
if (headers == null || !('forEach' in headers)) {
|
196
|
+
return headers;
|
198
197
|
}
|
198
|
+
const obj = {};
|
199
|
+
headers.forEach((value, key) => {
|
200
|
+
obj[key] = value;
|
201
|
+
});
|
202
|
+
return obj;
|
203
|
+
}
|
204
|
+
function uint8ArrayToBuffer(uint8array) {
|
205
|
+
return uint8array.buffer.slice(uint8array.byteOffset, uint8array.byteOffset + uint8array.byteLength);
|
206
|
+
}
|
207
|
+
|
208
|
+
function getBlobPartAsBuffer(blobPart) {
|
209
|
+
if (typeof blobPart === 'string') {
|
210
|
+
return Buffer.from(blobPart);
|
211
|
+
}
|
212
|
+
else if (Buffer.isBuffer(blobPart)) {
|
213
|
+
return blobPart;
|
214
|
+
}
|
215
|
+
else if (blobPart instanceof Uint8Array) {
|
216
|
+
return Buffer.from(blobPart);
|
217
|
+
}
|
218
|
+
else if ('buffer' in blobPart) {
|
219
|
+
return Buffer.from(blobPart.buffer, blobPart.byteOffset, blobPart.byteLength);
|
220
|
+
}
|
221
|
+
else {
|
222
|
+
return Buffer.from(blobPart);
|
223
|
+
}
|
224
|
+
}
|
225
|
+
function isBlob(obj) {
|
226
|
+
return obj != null && typeof obj === 'object' && obj.stream != null;
|
199
227
|
}
|
200
228
|
// Will be removed after v14 reaches EOL
|
201
229
|
// Needed because v14 doesn't have .stream() implemented
|
202
|
-
class PonyfillBlob
|
230
|
+
class PonyfillBlob {
|
231
|
+
constructor(blobParts, options) {
|
232
|
+
this.blobParts = blobParts;
|
233
|
+
this.type = (options === null || options === void 0 ? void 0 : options.type) || 'application/octet-stream';
|
234
|
+
this.encoding = (options === null || options === void 0 ? void 0 : options.encoding) || 'utf8';
|
235
|
+
}
|
236
|
+
async arrayBuffer() {
|
237
|
+
const bufferChunks = [];
|
238
|
+
for (const blobPart of this.blobParts) {
|
239
|
+
if (isBlob(blobPart)) {
|
240
|
+
const arrayBuf = await blobPart.arrayBuffer();
|
241
|
+
const buf = Buffer.from(arrayBuf, undefined, blobPart.size);
|
242
|
+
bufferChunks.push(buf);
|
243
|
+
}
|
244
|
+
else {
|
245
|
+
const buf = getBlobPartAsBuffer(blobPart);
|
246
|
+
bufferChunks.push(buf);
|
247
|
+
}
|
248
|
+
}
|
249
|
+
return uint8ArrayToBuffer(Buffer.concat(bufferChunks));
|
250
|
+
}
|
251
|
+
async text() {
|
252
|
+
let text = '';
|
253
|
+
for (const blobPart of this.blobParts) {
|
254
|
+
if (typeof blobPart === 'string') {
|
255
|
+
text += blobPart;
|
256
|
+
}
|
257
|
+
else if ('text' in blobPart) {
|
258
|
+
text += await blobPart.text();
|
259
|
+
}
|
260
|
+
else {
|
261
|
+
const buf = getBlobPartAsBuffer(blobPart);
|
262
|
+
text += buf.toString(this.encoding);
|
263
|
+
}
|
264
|
+
}
|
265
|
+
return text;
|
266
|
+
}
|
267
|
+
get size() {
|
268
|
+
let size = 0;
|
269
|
+
for (const blobPart of this.blobParts) {
|
270
|
+
if (typeof blobPart === 'string') {
|
271
|
+
size += Buffer.byteLength(blobPart);
|
272
|
+
}
|
273
|
+
else if (isBlob(blobPart)) {
|
274
|
+
size += blobPart.size;
|
275
|
+
}
|
276
|
+
else if ('length' in blobPart) {
|
277
|
+
size += blobPart.length;
|
278
|
+
}
|
279
|
+
else if ('byteLength' in blobPart) {
|
280
|
+
size += blobPart.byteLength;
|
281
|
+
}
|
282
|
+
}
|
283
|
+
return size;
|
284
|
+
}
|
203
285
|
stream() {
|
286
|
+
let partQueue = [];
|
204
287
|
return new PonyfillReadableStream({
|
205
|
-
start:
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
288
|
+
start: controller => {
|
289
|
+
partQueue = [...this.blobParts];
|
290
|
+
if (partQueue.length === 0) {
|
291
|
+
controller.close();
|
292
|
+
}
|
293
|
+
},
|
294
|
+
pull: async (controller) => {
|
295
|
+
const blobPart = partQueue.pop();
|
296
|
+
if (blobPart) {
|
297
|
+
if (isBlob(blobPart)) {
|
298
|
+
for await (const chunk of blobPart.stream()) {
|
299
|
+
controller.enqueue(chunk);
|
300
|
+
}
|
301
|
+
}
|
302
|
+
else {
|
303
|
+
const buf = getBlobPartAsBuffer(blobPart);
|
304
|
+
controller.enqueue(buf);
|
305
|
+
}
|
306
|
+
}
|
307
|
+
else {
|
308
|
+
controller.close();
|
309
|
+
}
|
210
310
|
},
|
211
311
|
});
|
212
312
|
}
|
213
|
-
slice(
|
214
|
-
|
313
|
+
slice() {
|
314
|
+
throw new Error('Not implemented');
|
215
315
|
}
|
216
316
|
}
|
217
317
|
|
@@ -276,7 +376,7 @@ class PonyfillFormData {
|
|
276
376
|
values = [];
|
277
377
|
this.map.set(name, values);
|
278
378
|
}
|
279
|
-
const entry = isBlob(value)
|
379
|
+
const entry = isBlob$1(value)
|
280
380
|
? getNormalizedFile(name, value, fileName)
|
281
381
|
: value;
|
282
382
|
values.push(entry);
|
@@ -295,7 +395,7 @@ class PonyfillFormData {
|
|
295
395
|
return this.map.has(name);
|
296
396
|
}
|
297
397
|
set(name, value, fileName) {
|
298
|
-
const entry = isBlob(value)
|
398
|
+
const entry = isBlob$1(value)
|
299
399
|
? getNormalizedFile(name, value, fileName)
|
300
400
|
: value;
|
301
401
|
this.map.set(name, [entry]);
|
@@ -377,7 +477,7 @@ function getNormalizedFile(name, blob, fileName) {
|
|
377
477
|
}
|
378
478
|
return new PonyfillFile([blob], fileName || name, { type: blob.type });
|
379
479
|
}
|
380
|
-
function isBlob(value) {
|
480
|
+
function isBlob$1(value) {
|
381
481
|
return value != null && typeof value === 'object' && typeof value.arrayBuffer === 'function';
|
382
482
|
}
|
383
483
|
|
@@ -446,7 +546,8 @@ class PonyfillBody {
|
|
446
546
|
return this.bodyInit;
|
447
547
|
}
|
448
548
|
if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.Buffer) {
|
449
|
-
|
549
|
+
const typedBodyInit = this.bodyInit;
|
550
|
+
return uint8ArrayToBuffer(typedBodyInit);
|
450
551
|
}
|
451
552
|
const blob = await this.blob();
|
452
553
|
return blob.arrayBuffer();
|
@@ -647,7 +748,7 @@ function processBodyInit(bodyInit) {
|
|
647
748
|
contentLength,
|
648
749
|
contentType: null,
|
649
750
|
bodyFactory() {
|
650
|
-
const buffer = Buffer.from(bodyInit
|
751
|
+
const buffer = Buffer.from(bodyInit);
|
651
752
|
const readable = Readable.from(buffer);
|
652
753
|
const body = new PonyfillReadableStream(readable);
|
653
754
|
return body;
|
@@ -921,17 +1022,6 @@ class PonyfillResponse extends PonyfillBody {
|
|
921
1022
|
}
|
922
1023
|
}
|
923
1024
|
|
924
|
-
function getHeadersObj(headers) {
|
925
|
-
if (headers == null || !('forEach' in headers)) {
|
926
|
-
return headers;
|
927
|
-
}
|
928
|
-
const obj = {};
|
929
|
-
headers.forEach((value, key) => {
|
930
|
-
obj[key] = value;
|
931
|
-
});
|
932
|
-
return obj;
|
933
|
-
}
|
934
|
-
|
935
1025
|
function getResponseForFile(url) {
|
936
1026
|
const path = fileURLToPath(url);
|
937
1027
|
const readable = createReadStream(path);
|
@@ -960,7 +1050,7 @@ function fetchPonyfill(info, init) {
|
|
960
1050
|
const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
|
961
1051
|
const data = decodeURIComponent(datas.join(','));
|
962
1052
|
if (mimeType.endsWith(BASE64_SUFFIX)) {
|
963
|
-
const buffer = Buffer.from(data, '
|
1053
|
+
const buffer = Buffer.from(data, 'base64url');
|
964
1054
|
const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
|
965
1055
|
const file = new PonyfillBlob([buffer], { type: realMimeType });
|
966
1056
|
const response = new PonyfillResponse(file, {
|
package/package.json
CHANGED
package/utils.d.ts
CHANGED