@whatwg-node/node-fetch 0.0.1-alpha-20221228081805-75e6dc7 → 0.0.1-alpha-20221228083733-4041c71
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 +2 -1
- package/ReadableStream.d.ts +2 -3
- package/index.js +42 -22
- package/index.mjs +42 -22
- package/package.json +1 -1
package/Body.d.ts
CHANGED
@@ -7,11 +7,12 @@ export type BodyPonyfillInit = XMLHttpRequestBodyInit | Readable | PonyfillReada
|
|
7
7
|
export declare class PonyfillBody implements Body {
|
8
8
|
private bodyInit;
|
9
9
|
bodyUsed: boolean;
|
10
|
-
|
10
|
+
private _body;
|
11
11
|
contentType: string | null;
|
12
12
|
contentLength: number | null;
|
13
13
|
constructor(bodyInit: BodyPonyfillInit | null);
|
14
14
|
private bodyType?;
|
15
|
+
get body(): PonyfillReadableStream<Uint8Array> | null;
|
15
16
|
arrayBuffer(): Promise<ArrayBuffer>;
|
16
17
|
blob(): Promise<PonyfillBlob>;
|
17
18
|
formData(): Promise<PonyfillFormData>;
|
package/ReadableStream.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
/// <reference types="node" />
|
2
|
-
import { Readable
|
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>);
|
@@ -16,6 +16,5 @@ export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
|
|
16
16
|
writable: WritableStream<T>;
|
17
17
|
readable: ReadableStream<T2>;
|
18
18
|
}): ReadableStream<T2>;
|
19
|
-
|
20
|
-
on(event: string, listener: (...args: any[]) => void): this;
|
19
|
+
static [Symbol.hasInstance](instance: unknown): instance is PonyfillReadableStream<unknown>;
|
21
20
|
}
|
package/index.js
CHANGED
@@ -128,7 +128,7 @@ class PonyfillReadableStream {
|
|
128
128
|
var _a;
|
129
129
|
try {
|
130
130
|
const controller = createController(0, this);
|
131
|
-
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource,
|
131
|
+
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
|
132
132
|
controller._flush();
|
133
133
|
callback(null);
|
134
134
|
}
|
@@ -201,13 +201,8 @@ class PonyfillReadableStream {
|
|
201
201
|
this.pipeTo(writable);
|
202
202
|
return readable;
|
203
203
|
}
|
204
|
-
|
205
|
-
|
206
|
-
return this.readable.pipe(writable);
|
207
|
-
}
|
208
|
-
on(event, listener) {
|
209
|
-
this.readable.on(event, listener);
|
210
|
-
return this;
|
204
|
+
static [Symbol.hasInstance](instance) {
|
205
|
+
return instance != null && typeof instance === 'object' && 'getReader' in instance;
|
211
206
|
}
|
212
207
|
}
|
213
208
|
|
@@ -299,36 +294,36 @@ class PonyfillBody {
|
|
299
294
|
constructor(bodyInit) {
|
300
295
|
this.bodyInit = bodyInit;
|
301
296
|
this.bodyUsed = false;
|
302
|
-
this.
|
297
|
+
this._body = null;
|
303
298
|
this.contentType = null;
|
304
299
|
this.contentLength = null;
|
305
300
|
if (this.bodyInit == null) {
|
306
|
-
this.
|
301
|
+
this._body = null;
|
307
302
|
}
|
308
303
|
else if (typeof this.bodyInit === 'string') {
|
309
304
|
this.bodyType = BodyInitType.String;
|
310
305
|
const buffer = Buffer.from(this.bodyInit);
|
311
306
|
this.contentType = 'text/plain;charset=UTF-8';
|
312
307
|
this.contentLength = buffer.length;
|
313
|
-
this.
|
308
|
+
this._body = new PonyfillReadableStream(stream.Readable.from(buffer));
|
314
309
|
}
|
315
310
|
else if (this.bodyInit instanceof PonyfillReadableStream) {
|
316
311
|
this.bodyType = BodyInitType.ReadableStream;
|
317
|
-
this.
|
312
|
+
this._body = this.bodyInit;
|
318
313
|
}
|
319
314
|
else if (this.bodyInit instanceof PonyfillBlob) {
|
320
315
|
this.bodyType = BodyInitType.Blob;
|
321
316
|
const blobStream = this.bodyInit.stream();
|
322
317
|
this.contentType = this.bodyInit.type;
|
323
318
|
this.contentLength = this.bodyInit.size;
|
324
|
-
this.
|
319
|
+
this._body = new PonyfillReadableStream(blobStream);
|
325
320
|
}
|
326
321
|
else if (this.bodyInit instanceof PonyfillFormData) {
|
327
322
|
this.bodyType = BodyInitType.FormData;
|
328
323
|
const boundary = Math.random().toString(36).substr(2);
|
329
324
|
const formData = this.bodyInit;
|
330
325
|
this.contentType = `multipart/form-data; boundary=${boundary}`;
|
331
|
-
this.
|
326
|
+
this._body = new PonyfillReadableStream({
|
332
327
|
start: async (controller) => {
|
333
328
|
controller.enqueue(Buffer.from(`--${boundary}\r\n`));
|
334
329
|
const entries = [];
|
@@ -363,21 +358,46 @@ class PonyfillBody {
|
|
363
358
|
}
|
364
359
|
else if ('buffer' in this.bodyInit) {
|
365
360
|
this.contentLength = this.bodyInit.byteLength;
|
366
|
-
this.
|
361
|
+
this._body = new PonyfillReadableStream(stream.Readable.from(this.bodyInit));
|
367
362
|
}
|
368
363
|
else if (this.bodyInit instanceof ArrayBuffer) {
|
369
364
|
this.bodyType = BodyInitType.ArrayBuffer;
|
370
365
|
this.contentLength = this.bodyInit.byteLength;
|
371
|
-
this.
|
366
|
+
this._body = new PonyfillReadableStream(stream.Readable.from(Buffer.from(this.bodyInit)));
|
372
367
|
}
|
373
368
|
else if (this.bodyInit instanceof stream.Readable) {
|
374
369
|
this.bodyType = BodyInitType.Readable;
|
375
|
-
this.
|
370
|
+
this._body = new PonyfillReadableStream(this.bodyInit);
|
376
371
|
}
|
377
372
|
else {
|
378
373
|
throw new Error('Unknown body type');
|
379
374
|
}
|
380
375
|
}
|
376
|
+
get body() {
|
377
|
+
if (this._body != null) {
|
378
|
+
const ponyfillReadableStream = this._body;
|
379
|
+
const readable = this._body.readable;
|
380
|
+
return new Proxy(this._body.readable, {
|
381
|
+
get(_, prop) {
|
382
|
+
if (prop in ponyfillReadableStream) {
|
383
|
+
const ponyfillReadableStreamProp = ponyfillReadableStream[prop];
|
384
|
+
if (typeof ponyfillReadableStreamProp === 'function') {
|
385
|
+
return ponyfillReadableStreamProp.bind(ponyfillReadableStream);
|
386
|
+
}
|
387
|
+
return ponyfillReadableStreamProp;
|
388
|
+
}
|
389
|
+
if (prop in readable) {
|
390
|
+
const readableProp = readable[prop];
|
391
|
+
if (typeof readableProp === 'function') {
|
392
|
+
return readableProp.bind(readable);
|
393
|
+
}
|
394
|
+
return readableProp;
|
395
|
+
}
|
396
|
+
}
|
397
|
+
});
|
398
|
+
}
|
399
|
+
return null;
|
400
|
+
}
|
381
401
|
async arrayBuffer() {
|
382
402
|
if (this.bodyType === BodyInitType.ArrayBuffer) {
|
383
403
|
return this.bodyInit;
|
@@ -390,8 +410,8 @@ class PonyfillBody {
|
|
390
410
|
return this.bodyInit;
|
391
411
|
}
|
392
412
|
const chunks = [];
|
393
|
-
if (this.
|
394
|
-
for await (const chunk of this.
|
413
|
+
if (this._body) {
|
414
|
+
for await (const chunk of this._body.readable) {
|
395
415
|
chunks.push(chunk);
|
396
416
|
}
|
397
417
|
}
|
@@ -418,8 +438,8 @@ class PonyfillBody {
|
|
418
438
|
if (this.bodyType === BodyInitType.Readable) {
|
419
439
|
return this.bodyInit;
|
420
440
|
}
|
421
|
-
if (this.
|
422
|
-
return this.
|
441
|
+
if (this._body != null) {
|
442
|
+
return this._body.readable;
|
423
443
|
}
|
424
444
|
return null;
|
425
445
|
}
|
@@ -648,7 +668,7 @@ const fetchPonyfill = (info, init) => {
|
|
648
668
|
return;
|
649
669
|
}
|
650
670
|
const requestFn = getRequestFnForProtocol(protocol);
|
651
|
-
const nodeReadable = fetchRequest.
|
671
|
+
const nodeReadable = fetchRequest.body;
|
652
672
|
const nodeHeaders = getHeadersObj(fetchRequest.headers);
|
653
673
|
const abortListener = function abortListener(event) {
|
654
674
|
nodeRequest.destroy();
|
package/index.mjs
CHANGED
@@ -124,7 +124,7 @@ class PonyfillReadableStream {
|
|
124
124
|
var _a;
|
125
125
|
try {
|
126
126
|
const controller = createController(0, this);
|
127
|
-
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource,
|
127
|
+
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
|
128
128
|
controller._flush();
|
129
129
|
callback(null);
|
130
130
|
}
|
@@ -197,13 +197,8 @@ class PonyfillReadableStream {
|
|
197
197
|
this.pipeTo(writable);
|
198
198
|
return readable;
|
199
199
|
}
|
200
|
-
|
201
|
-
|
202
|
-
return this.readable.pipe(writable);
|
203
|
-
}
|
204
|
-
on(event, listener) {
|
205
|
-
this.readable.on(event, listener);
|
206
|
-
return this;
|
200
|
+
static [Symbol.hasInstance](instance) {
|
201
|
+
return instance != null && typeof instance === 'object' && 'getReader' in instance;
|
207
202
|
}
|
208
203
|
}
|
209
204
|
|
@@ -295,36 +290,36 @@ class PonyfillBody {
|
|
295
290
|
constructor(bodyInit) {
|
296
291
|
this.bodyInit = bodyInit;
|
297
292
|
this.bodyUsed = false;
|
298
|
-
this.
|
293
|
+
this._body = null;
|
299
294
|
this.contentType = null;
|
300
295
|
this.contentLength = null;
|
301
296
|
if (this.bodyInit == null) {
|
302
|
-
this.
|
297
|
+
this._body = null;
|
303
298
|
}
|
304
299
|
else if (typeof this.bodyInit === 'string') {
|
305
300
|
this.bodyType = BodyInitType.String;
|
306
301
|
const buffer = Buffer.from(this.bodyInit);
|
307
302
|
this.contentType = 'text/plain;charset=UTF-8';
|
308
303
|
this.contentLength = buffer.length;
|
309
|
-
this.
|
304
|
+
this._body = new PonyfillReadableStream(Readable.from(buffer));
|
310
305
|
}
|
311
306
|
else if (this.bodyInit instanceof PonyfillReadableStream) {
|
312
307
|
this.bodyType = BodyInitType.ReadableStream;
|
313
|
-
this.
|
308
|
+
this._body = this.bodyInit;
|
314
309
|
}
|
315
310
|
else if (this.bodyInit instanceof PonyfillBlob) {
|
316
311
|
this.bodyType = BodyInitType.Blob;
|
317
312
|
const blobStream = this.bodyInit.stream();
|
318
313
|
this.contentType = this.bodyInit.type;
|
319
314
|
this.contentLength = this.bodyInit.size;
|
320
|
-
this.
|
315
|
+
this._body = new PonyfillReadableStream(blobStream);
|
321
316
|
}
|
322
317
|
else if (this.bodyInit instanceof PonyfillFormData) {
|
323
318
|
this.bodyType = BodyInitType.FormData;
|
324
319
|
const boundary = Math.random().toString(36).substr(2);
|
325
320
|
const formData = this.bodyInit;
|
326
321
|
this.contentType = `multipart/form-data; boundary=${boundary}`;
|
327
|
-
this.
|
322
|
+
this._body = new PonyfillReadableStream({
|
328
323
|
start: async (controller) => {
|
329
324
|
controller.enqueue(Buffer.from(`--${boundary}\r\n`));
|
330
325
|
const entries = [];
|
@@ -359,21 +354,46 @@ class PonyfillBody {
|
|
359
354
|
}
|
360
355
|
else if ('buffer' in this.bodyInit) {
|
361
356
|
this.contentLength = this.bodyInit.byteLength;
|
362
|
-
this.
|
357
|
+
this._body = new PonyfillReadableStream(Readable.from(this.bodyInit));
|
363
358
|
}
|
364
359
|
else if (this.bodyInit instanceof ArrayBuffer) {
|
365
360
|
this.bodyType = BodyInitType.ArrayBuffer;
|
366
361
|
this.contentLength = this.bodyInit.byteLength;
|
367
|
-
this.
|
362
|
+
this._body = new PonyfillReadableStream(Readable.from(Buffer.from(this.bodyInit)));
|
368
363
|
}
|
369
364
|
else if (this.bodyInit instanceof Readable) {
|
370
365
|
this.bodyType = BodyInitType.Readable;
|
371
|
-
this.
|
366
|
+
this._body = new PonyfillReadableStream(this.bodyInit);
|
372
367
|
}
|
373
368
|
else {
|
374
369
|
throw new Error('Unknown body type');
|
375
370
|
}
|
376
371
|
}
|
372
|
+
get body() {
|
373
|
+
if (this._body != null) {
|
374
|
+
const ponyfillReadableStream = this._body;
|
375
|
+
const readable = this._body.readable;
|
376
|
+
return new Proxy(this._body.readable, {
|
377
|
+
get(_, prop) {
|
378
|
+
if (prop in ponyfillReadableStream) {
|
379
|
+
const ponyfillReadableStreamProp = ponyfillReadableStream[prop];
|
380
|
+
if (typeof ponyfillReadableStreamProp === 'function') {
|
381
|
+
return ponyfillReadableStreamProp.bind(ponyfillReadableStream);
|
382
|
+
}
|
383
|
+
return ponyfillReadableStreamProp;
|
384
|
+
}
|
385
|
+
if (prop in readable) {
|
386
|
+
const readableProp = readable[prop];
|
387
|
+
if (typeof readableProp === 'function') {
|
388
|
+
return readableProp.bind(readable);
|
389
|
+
}
|
390
|
+
return readableProp;
|
391
|
+
}
|
392
|
+
}
|
393
|
+
});
|
394
|
+
}
|
395
|
+
return null;
|
396
|
+
}
|
377
397
|
async arrayBuffer() {
|
378
398
|
if (this.bodyType === BodyInitType.ArrayBuffer) {
|
379
399
|
return this.bodyInit;
|
@@ -386,8 +406,8 @@ class PonyfillBody {
|
|
386
406
|
return this.bodyInit;
|
387
407
|
}
|
388
408
|
const chunks = [];
|
389
|
-
if (this.
|
390
|
-
for await (const chunk of this.
|
409
|
+
if (this._body) {
|
410
|
+
for await (const chunk of this._body.readable) {
|
391
411
|
chunks.push(chunk);
|
392
412
|
}
|
393
413
|
}
|
@@ -414,8 +434,8 @@ class PonyfillBody {
|
|
414
434
|
if (this.bodyType === BodyInitType.Readable) {
|
415
435
|
return this.bodyInit;
|
416
436
|
}
|
417
|
-
if (this.
|
418
|
-
return this.
|
437
|
+
if (this._body != null) {
|
438
|
+
return this._body.readable;
|
419
439
|
}
|
420
440
|
return null;
|
421
441
|
}
|
@@ -644,7 +664,7 @@ const fetchPonyfill = (info, init) => {
|
|
644
664
|
return;
|
645
665
|
}
|
646
666
|
const requestFn = getRequestFnForProtocol(protocol);
|
647
|
-
const nodeReadable = fetchRequest.
|
667
|
+
const nodeReadable = fetchRequest.body;
|
648
668
|
const nodeHeaders = getHeadersObj(fetchRequest.headers);
|
649
669
|
const abortListener = function abortListener(event) {
|
650
670
|
nodeRequest.destroy();
|
package/package.json
CHANGED