@whatwg-node/node-fetch
Advanced tools
Comparing version 0.0.2 to 0.0.3-alpha-20230205160101-d3a1033
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import { Readable } from 'stream'; | ||
@@ -23,3 +24,2 @@ import { PonyfillBlob } from './Blob'; | ||
bodyUsed: boolean; | ||
private _body; | ||
contentType: string | null; | ||
@@ -29,2 +29,5 @@ contentLength: number | null; | ||
private bodyType?; | ||
private _bodyFactory; | ||
private _generatedBody; | ||
private generateBody; | ||
get body(): PonyfillReadableStream<Uint8Array> | null; | ||
@@ -36,4 +39,5 @@ arrayBuffer(): Promise<ArrayBuffer>; | ||
}): Promise<PonyfillFormData>; | ||
buffer(): Promise<Buffer>; | ||
json(): Promise<TJSON>; | ||
text(): Promise<string>; | ||
} |
292
index.js
@@ -12,5 +12,5 @@ 'use strict'; | ||
const url = require('url'); | ||
const buffer = require('buffer'); | ||
const events = require('@whatwg-node/events'); | ||
const busboy = _interopDefault(require('busboy')); | ||
const buffer = require('buffer'); | ||
@@ -34,44 +34,2 @@ // Will be removed after v14 reaches EOL | ||
// Will be removed after v14 reaches EOL | ||
class PonyfillAbortSignal extends events.EventTarget { | ||
constructor() { | ||
super(...arguments); | ||
this.aborted = false; | ||
this._onabort = null; | ||
} | ||
throwIfAborted() { | ||
if (this.aborted) { | ||
throw new PonyfillAbortError(); | ||
} | ||
} | ||
get onabort() { | ||
return this._onabort; | ||
} | ||
set onabort(value) { | ||
if (this._onabort) { | ||
this.removeEventListener('abort', this._onabort); | ||
} | ||
this.addEventListener('abort', value); | ||
} | ||
abort(reason) { | ||
const abortEvent = new events.CustomEvent('abort', { detail: reason }); | ||
this.dispatchEvent(abortEvent); | ||
} | ||
static timeout(milliseconds) { | ||
const signal = new PonyfillAbortSignal(); | ||
setTimeout(() => signal.abort(`timeout in ${milliseconds} ms`), milliseconds); | ||
return signal; | ||
} | ||
} | ||
// Will be removed after v14 reaches EOL | ||
class PonyfillAbortController { | ||
constructor() { | ||
this.signal = new PonyfillAbortSignal(); | ||
} | ||
abort(reason) { | ||
this.signal.abort(reason); | ||
} | ||
} | ||
function createController(desiredSize, readable) { | ||
@@ -237,2 +195,44 @@ let chunks = []; | ||
// Will be removed after v14 reaches EOL | ||
class PonyfillAbortSignal extends events.EventTarget { | ||
constructor() { | ||
super(...arguments); | ||
this.aborted = false; | ||
this._onabort = null; | ||
} | ||
throwIfAborted() { | ||
if (this.aborted) { | ||
throw new PonyfillAbortError(); | ||
} | ||
} | ||
get onabort() { | ||
return this._onabort; | ||
} | ||
set onabort(value) { | ||
if (this._onabort) { | ||
this.removeEventListener('abort', this._onabort); | ||
} | ||
this.addEventListener('abort', value); | ||
} | ||
abort(reason) { | ||
const abortEvent = new events.CustomEvent('abort', { detail: reason }); | ||
this.dispatchEvent(abortEvent); | ||
} | ||
static timeout(milliseconds) { | ||
const signal = new PonyfillAbortSignal(); | ||
setTimeout(() => signal.abort(`timeout in ${milliseconds} ms`), milliseconds); | ||
return signal; | ||
} | ||
} | ||
// Will be removed after v14 reaches EOL | ||
class PonyfillAbortController { | ||
constructor() { | ||
this.signal = new PonyfillAbortSignal(); | ||
} | ||
abort(reason) { | ||
this.signal.abort(reason); | ||
} | ||
} | ||
class PonyfillFile extends PonyfillBlob { | ||
@@ -359,2 +359,4 @@ constructor(fileBits, name, options) { | ||
BodyInitType["Readable"] = "Readable"; | ||
BodyInitType["Buffer"] = "Buffer"; | ||
BodyInitType["Uint8Array"] = "Uint8Array"; | ||
})(BodyInitType || (BodyInitType = {})); | ||
@@ -366,7 +368,8 @@ class PonyfillBody { | ||
this.bodyUsed = false; | ||
this._body = null; | ||
this.contentType = null; | ||
this.contentLength = null; | ||
const { body, contentType, contentLength, bodyType } = processBodyInit(bodyInit); | ||
this._body = body; | ||
this._bodyFactory = () => null; | ||
this._generatedBody = null; | ||
const { bodyFactory, contentType, contentLength, bodyType } = processBodyInit(bodyInit); | ||
this._bodyFactory = bodyFactory; | ||
this.contentType = contentType; | ||
@@ -376,7 +379,16 @@ this.contentLength = contentLength; | ||
} | ||
generateBody() { | ||
if (this._generatedBody) { | ||
return this._generatedBody; | ||
} | ||
const body = this._bodyFactory(); | ||
this._generatedBody = body; | ||
return body; | ||
} | ||
get body() { | ||
if (this._body != null) { | ||
const ponyfillReadableStream = this._body; | ||
const readable = this._body.readable; | ||
return new Proxy(this._body.readable, { | ||
const _body = this.generateBody(); | ||
if (_body != null) { | ||
const ponyfillReadableStream = _body; | ||
const readable = _body.readable; | ||
return new Proxy(_body.readable, { | ||
get(_, prop) { | ||
@@ -406,2 +418,5 @@ if (prop in ponyfillReadableStream) { | ||
} | ||
if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.Buffer) { | ||
return this.bodyInit.buffer; | ||
} | ||
const blob = await this.blob(); | ||
@@ -414,9 +429,25 @@ return blob.arrayBuffer(); | ||
} | ||
if (this.bodyType === BodyInitType.String || this.bodyType === BodyInitType.Buffer || this.bodyType === BodyInitType.Uint8Array) { | ||
const bodyInitTyped = this.bodyInit; | ||
return new PonyfillBlob([bodyInitTyped], { | ||
type: this.contentType || '', | ||
}); | ||
} | ||
if (this.bodyType === BodyInitType.ArrayBuffer) { | ||
const bodyInitTyped = this.bodyInit; | ||
const buf = Buffer.from(bodyInitTyped, undefined, bodyInitTyped.byteLength); | ||
return new PonyfillBlob([buf], { | ||
type: this.contentType || '', | ||
}); | ||
} | ||
const chunks = []; | ||
if (this._body) { | ||
for await (const chunk of this._body.readable) { | ||
const _body = this.generateBody(); | ||
if (_body) { | ||
for await (const chunk of _body.readable) { | ||
chunks.push(chunk); | ||
} | ||
} | ||
return new PonyfillBlob(chunks); | ||
return new PonyfillBlob(chunks, { | ||
type: this.contentType || '', | ||
}); | ||
} | ||
@@ -428,3 +459,4 @@ formData(opts) { | ||
const formData = new PonyfillFormData(); | ||
if (this._body == null) { | ||
const _body = this.generateBody(); | ||
if (_body == null) { | ||
return Promise.resolve(formData); | ||
@@ -437,3 +469,2 @@ } | ||
return new Promise((resolve, reject) => { | ||
var _a; | ||
const bb = busboy({ | ||
@@ -486,5 +517,18 @@ headers: { | ||
}); | ||
(_a = this._body) === null || _a === void 0 ? void 0 : _a.readable.pipe(bb); | ||
_body === null || _body === void 0 ? void 0 : _body.readable.pipe(bb); | ||
}); | ||
} | ||
async buffer() { | ||
if (this.bodyType === BodyInitType.Buffer) { | ||
return this.bodyInit; | ||
} | ||
if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.ArrayBuffer) { | ||
const bodyInitTyped = this.bodyInit; | ||
const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength); | ||
return buffer; | ||
} | ||
const blob = await this.blob(); | ||
const arrayBuffer = await blob.arrayBuffer(); | ||
return Buffer.from(arrayBuffer, undefined, arrayBuffer.byteLength); | ||
} | ||
async json() { | ||
@@ -498,2 +542,10 @@ const text = await this.text(); | ||
} | ||
if (this.bodyType === BodyInitType.Buffer) { | ||
return this.bodyInit.toString('utf-8'); | ||
} | ||
if (this.bodyType === BodyInitType.ArrayBuffer || this.bodyType === BodyInitType.Uint8Array) { | ||
const bodyInitTyped = this.bodyInit; | ||
const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength); | ||
return buffer.toString('utf-8'); | ||
} | ||
const blob = await this.blob(); | ||
@@ -506,3 +558,3 @@ return blob.text(); | ||
return { | ||
body: null, | ||
bodyFactory: () => null, | ||
contentType: null, | ||
@@ -513,10 +565,11 @@ contentLength: null, | ||
if (typeof bodyInit === 'string') { | ||
const buffer = Buffer.from(bodyInit); | ||
const readable = stream.Readable.from(buffer); | ||
const body = new PonyfillReadableStream(readable); | ||
return { | ||
bodyType: BodyInitType.String, | ||
contentType: 'text/plain;charset=UTF-8', | ||
contentLength: buffer.length, | ||
body, | ||
contentLength: bodyInit.length, | ||
bodyFactory() { | ||
const buffer = Buffer.from(bodyInit); | ||
const readable = stream.Readable.from(buffer); | ||
return new PonyfillReadableStream(readable); | ||
}, | ||
}; | ||
@@ -527,3 +580,3 @@ } | ||
bodyType: BodyInitType.ReadableStream, | ||
body: bodyInit, | ||
bodyFactory: () => bodyInit, | ||
contentType: null, | ||
@@ -534,4 +587,2 @@ contentLength: null, | ||
if (bodyInit instanceof PonyfillBlob) { | ||
const readable = bodyInit.stream(); | ||
const body = new PonyfillReadableStream(readable); | ||
return { | ||
@@ -541,3 +592,5 @@ bodyType: BodyInitType.Blob, | ||
contentLength: bodyInit.size, | ||
body, | ||
bodyFactory() { | ||
return bodyInit.stream(); | ||
}, | ||
}; | ||
@@ -548,3 +601,2 @@ } | ||
const contentType = `multipart/form-data; boundary=${boundary}`; | ||
const body = bodyInit.stream(boundary); | ||
return { | ||
@@ -554,14 +606,42 @@ bodyType: BodyInitType.FormData, | ||
contentLength: null, | ||
body, | ||
bodyFactory: () => bodyInit.stream(boundary), | ||
}; | ||
} | ||
if (bodyInit instanceof Buffer) { | ||
const contentLength = bodyInit.length; | ||
return { | ||
bodyType: BodyInitType.Buffer, | ||
contentLength, | ||
contentType: null, | ||
bodyFactory() { | ||
const readable = stream.Readable.from(bodyInit); | ||
const body = new PonyfillReadableStream(readable); | ||
return body; | ||
}, | ||
}; | ||
} | ||
if (bodyInit instanceof Uint8Array) { | ||
const contentLength = bodyInit.byteLength; | ||
return { | ||
bodyType: BodyInitType.Uint8Array, | ||
contentLength, | ||
contentType: null, | ||
bodyFactory() { | ||
const readable = stream.Readable.from(bodyInit); | ||
const body = new PonyfillReadableStream(readable); | ||
return body; | ||
} | ||
}; | ||
} | ||
if ('buffer' in bodyInit) { | ||
const contentLength = bodyInit.byteLength; | ||
const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength); | ||
const readable = stream.Readable.from(buffer); | ||
const body = new PonyfillReadableStream(readable); | ||
return { | ||
contentLength, | ||
contentType: null, | ||
body, | ||
bodyFactory() { | ||
const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength); | ||
const readable = stream.Readable.from(buffer); | ||
const body = new PonyfillReadableStream(readable); | ||
return body; | ||
} | ||
}; | ||
@@ -571,5 +651,2 @@ } | ||
const contentLength = bodyInit.byteLength; | ||
const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength); | ||
const readable = stream.Readable.from(buffer); | ||
const body = new PonyfillReadableStream(readable); | ||
return { | ||
@@ -579,7 +656,11 @@ bodyType: BodyInitType.ArrayBuffer, | ||
contentLength, | ||
body, | ||
bodyFactory() { | ||
const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength); | ||
const readable = stream.Readable.from(buffer); | ||
const body = new PonyfillReadableStream(readable); | ||
return body; | ||
} | ||
}; | ||
} | ||
if (bodyInit instanceof stream.Readable) { | ||
const body = new PonyfillReadableStream(bodyInit); | ||
return { | ||
@@ -589,12 +670,17 @@ bodyType: BodyInitType.Readable, | ||
contentLength: null, | ||
body, | ||
bodyFactory() { | ||
const body = new PonyfillReadableStream(bodyInit); | ||
return body; | ||
} | ||
}; | ||
} | ||
if ('stream' in bodyInit) { | ||
const bodyStream = bodyInit.stream(); | ||
const body = new PonyfillReadableStream(bodyStream); | ||
return { | ||
contentType: bodyInit.type, | ||
contentLength: bodyInit.size, | ||
body, | ||
bodyFactory() { | ||
const bodyStream = bodyInit.stream(); | ||
const body = new PonyfillReadableStream(bodyStream); | ||
return body; | ||
} | ||
}; | ||
@@ -604,3 +690,2 @@ } | ||
const contentType = 'application/x-www-form-urlencoded;charset=UTF-8'; | ||
const body = new PonyfillReadableStream(stream.Readable.from(bodyInit.toString())); | ||
return { | ||
@@ -610,17 +695,22 @@ bodyType: BodyInitType.String, | ||
contentLength: null, | ||
body, | ||
bodyFactory() { | ||
const body = new PonyfillReadableStream(stream.Readable.from(bodyInit.toString())); | ||
return body; | ||
} | ||
}; | ||
} | ||
if ('forEach' in bodyInit) { | ||
const formData = new PonyfillFormData(); | ||
bodyInit.forEach((value, key) => { | ||
formData.append(key, value); | ||
}); | ||
const boundary = Math.random().toString(36).substr(2); | ||
const contentType = `multipart/form-data; boundary=${boundary}`; | ||
const body = formData.stream(boundary); | ||
return { | ||
contentType, | ||
contentLength: null, | ||
body, | ||
bodyFactory() { | ||
const formData = new PonyfillFormData(); | ||
bodyInit.forEach((value, key) => { | ||
formData.append(key, value); | ||
}); | ||
const body = formData.stream(boundary); | ||
return body; | ||
}, | ||
}; | ||
@@ -783,2 +873,20 @@ } | ||
} | ||
const contentTypeInHeaders = this.headers.get('content-type'); | ||
if (!contentTypeInHeaders) { | ||
if (this.contentType) { | ||
this.headers.set('content-type', this.contentType); | ||
} | ||
} | ||
else { | ||
this.contentType = contentTypeInHeaders; | ||
} | ||
const contentLengthInHeaders = this.headers.get('content-length'); | ||
if (!contentLengthInHeaders) { | ||
if (this.contentLength) { | ||
this.headers.set('content-length', this.contentLength.toString()); | ||
} | ||
} | ||
else { | ||
this.contentLength = parseInt(contentLengthInHeaders, 10); | ||
} | ||
} | ||
@@ -860,8 +968,6 @@ get ok() { | ||
const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length); | ||
const response = new PonyfillResponse(buffer, { | ||
const file = new PonyfillBlob([buffer], { type: realMimeType }); | ||
const response = new PonyfillResponse(file, { | ||
status: 200, | ||
statusText: 'OK', | ||
headers: { | ||
'content-type': realMimeType, | ||
}, | ||
}); | ||
@@ -868,0 +974,0 @@ resolve(response); |
{ | ||
"name": "@whatwg-node/node-fetch", | ||
"version": "0.0.2", | ||
"version": "0.0.3-alpha-20230205160101-d3a1033", | ||
"description": "Fetch API implementation for Node", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
Sorry, the diff of this file is not supported yet
86021
2298