@whatwg-node/node-fetch
Advanced tools
Comparing version 0.0.1-alpha-20221228082016-9ef2266 to 0.0.1-alpha-20221228083733-4041c71
@@ -10,3 +10,3 @@ /// <reference types="node" /> | ||
bodyUsed: boolean; | ||
readonly body: PonyfillReadableStream<Uint8Array> | null; | ||
private _body; | ||
contentType: string | null; | ||
@@ -16,2 +16,3 @@ contentLength: number | null; | ||
private bodyType?; | ||
get body(): PonyfillReadableStream<Uint8Array> | null; | ||
arrayBuffer(): Promise<ArrayBuffer>; | ||
@@ -18,0 +19,0 @@ blob(): Promise<PonyfillBlob>; |
62
index.js
@@ -204,10 +204,5 @@ 'use strict'; | ||
} | ||
// Trick Fastify | ||
pipe(writable) { | ||
return this.readable.pipe(writable); | ||
static [Symbol.hasInstance](instance) { | ||
return instance != null && typeof instance === 'object' && 'getReader' in instance; | ||
} | ||
on(event, listener) { | ||
this.readable.on(event, listener); | ||
return this; | ||
} | ||
} | ||
@@ -303,7 +298,7 @@ | ||
this.bodyUsed = false; | ||
this.body = null; | ||
this._body = null; | ||
this.contentType = null; | ||
this.contentLength = null; | ||
if (this.bodyInit == null) { | ||
this.body = null; | ||
this._body = null; | ||
} | ||
@@ -315,7 +310,7 @@ else if (typeof this.bodyInit === 'string') { | ||
this.contentLength = buffer.length; | ||
this.body = new PonyfillReadableStream(stream.Readable.from(buffer)); | ||
this._body = new PonyfillReadableStream(stream.Readable.from(buffer)); | ||
} | ||
else if (this.bodyInit instanceof PonyfillReadableStream) { | ||
this.bodyType = BodyInitType.ReadableStream; | ||
this.body = this.bodyInit; | ||
this._body = this.bodyInit; | ||
} | ||
@@ -327,3 +322,3 @@ else if (this.bodyInit instanceof PonyfillBlob) { | ||
this.contentLength = this.bodyInit.size; | ||
this.body = new PonyfillReadableStream(blobStream); | ||
this._body = new PonyfillReadableStream(blobStream); | ||
} | ||
@@ -335,3 +330,3 @@ else if (this.bodyInit instanceof PonyfillFormData) { | ||
this.contentType = `multipart/form-data; boundary=${boundary}`; | ||
this.body = new PonyfillReadableStream({ | ||
this._body = new PonyfillReadableStream({ | ||
start: async (controller) => { | ||
@@ -371,3 +366,3 @@ controller.enqueue(Buffer.from(`--${boundary}\r\n`)); | ||
this.contentLength = this.bodyInit.byteLength; | ||
this.body = new PonyfillReadableStream(stream.Readable.from(this.bodyInit)); | ||
this._body = new PonyfillReadableStream(stream.Readable.from(this.bodyInit)); | ||
} | ||
@@ -377,7 +372,7 @@ else if (this.bodyInit instanceof ArrayBuffer) { | ||
this.contentLength = this.bodyInit.byteLength; | ||
this.body = new PonyfillReadableStream(stream.Readable.from(Buffer.from(this.bodyInit))); | ||
this._body = new PonyfillReadableStream(stream.Readable.from(Buffer.from(this.bodyInit))); | ||
} | ||
else if (this.bodyInit instanceof stream.Readable) { | ||
this.bodyType = BodyInitType.Readable; | ||
this.body = new PonyfillReadableStream(this.bodyInit); | ||
this._body = new PonyfillReadableStream(this.bodyInit); | ||
} | ||
@@ -388,2 +383,27 @@ else { | ||
} | ||
get body() { | ||
if (this._body != null) { | ||
const ponyfillReadableStream = this._body; | ||
const readable = this._body.readable; | ||
return new Proxy(this._body.readable, { | ||
get(_, prop) { | ||
if (prop in ponyfillReadableStream) { | ||
const ponyfillReadableStreamProp = ponyfillReadableStream[prop]; | ||
if (typeof ponyfillReadableStreamProp === 'function') { | ||
return ponyfillReadableStreamProp.bind(ponyfillReadableStream); | ||
} | ||
return ponyfillReadableStreamProp; | ||
} | ||
if (prop in readable) { | ||
const readableProp = readable[prop]; | ||
if (typeof readableProp === 'function') { | ||
return readableProp.bind(readable); | ||
} | ||
return readableProp; | ||
} | ||
} | ||
}); | ||
} | ||
return null; | ||
} | ||
async arrayBuffer() { | ||
@@ -401,4 +421,4 @@ if (this.bodyType === BodyInitType.ArrayBuffer) { | ||
const chunks = []; | ||
if (this.body) { | ||
for await (const chunk of this.body.readable) { | ||
if (this._body) { | ||
for await (const chunk of this._body.readable) { | ||
chunks.push(chunk); | ||
@@ -430,4 +450,4 @@ } | ||
} | ||
if (this.body != null) { | ||
return this.body.readable; | ||
if (this._body != null) { | ||
return this._body.readable; | ||
} | ||
@@ -661,3 +681,3 @@ return null; | ||
const requestFn = getRequestFnForProtocol(protocol); | ||
const nodeReadable = fetchRequest.readable(); | ||
const nodeReadable = fetchRequest.body; | ||
const nodeHeaders = getHeadersObj(fetchRequest.headers); | ||
@@ -664,0 +684,0 @@ const abortListener = function abortListener(event) { |
{ | ||
"name": "@whatwg-node/node-fetch", | ||
"version": "0.0.1-alpha-20221228082016-9ef2266", | ||
"version": "0.0.1-alpha-20221228083733-4041c71", | ||
"description": "Fetch API implementation for Node", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
/// <reference types="node" /> | ||
import { Readable, Writable } from 'stream'; | ||
import { Readable } from 'stream'; | ||
export declare class PonyfillReadableStream<T> implements ReadableStream<T> { | ||
@@ -19,4 +19,3 @@ readable: Readable; | ||
}): ReadableStream<T2>; | ||
pipe(writable: Writable): Writable; | ||
on(event: string, listener: (...args: any[]) => void): this; | ||
static [Symbol.hasInstance](instance: unknown): instance is PonyfillReadableStream<unknown>; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62190
1675