@whatwg-node/node-fetch
Advanced tools
Comparing version 0.0.1-alpha-20221228115139-6b81aa1 to 0.0.1-alpha-20221230075257-cc3a93b
@@ -31,5 +31,7 @@ /// <reference types="node" /> | ||
blob(): Promise<PonyfillBlob>; | ||
formData(): Promise<PonyfillFormData>; | ||
formData(opts?: { | ||
formDataLimits: FormDataLimits; | ||
}): Promise<PonyfillFormData>; | ||
json(): Promise<any>; | ||
text(): Promise<string>; | ||
} |
import { PonyfillBlob } from './Blob'; | ||
import { PonyfillReadableStream } from './ReadableStream'; | ||
export declare class PonyfillFormData implements FormData { | ||
@@ -10,4 +11,5 @@ private map; | ||
set(name: string, value: PonyfillBlob | string, fileName?: string): void; | ||
private getNormalizedFile; | ||
[Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>; | ||
forEach(callback: (value: FormDataEntryValue, key: string, parent: this) => void): void; | ||
stream(boundary?: string): PonyfillReadableStream<Uint8Array>; | ||
} |
240
index.js
@@ -238,3 +238,3 @@ 'use strict'; | ||
} | ||
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value; | ||
const entry = value instanceof PonyfillBlob ? getNormalizedFile(name, value, fileName) : value; | ||
values.push(entry); | ||
@@ -256,21 +256,63 @@ } | ||
set(name, value, fileName) { | ||
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value; | ||
const entry = value instanceof PonyfillBlob ? getNormalizedFile(name, value, fileName) : value; | ||
this.map.set(name, [entry]); | ||
} | ||
getNormalizedFile(name, blob, fileName) { | ||
if (blob instanceof PonyfillFile) { | ||
if (fileName != null) { | ||
return new PonyfillFile([blob], fileName, { type: blob.type, lastModified: blob.lastModified }); | ||
*[Symbol.iterator]() { | ||
for (const [key, values] of this.map) { | ||
for (const value of values) { | ||
yield [key, value]; | ||
} | ||
return blob; | ||
} | ||
return new PonyfillFile([blob], fileName || name, { type: blob.type }); | ||
} | ||
forEach(callback) { | ||
for (const [key, values] of this.map) { | ||
for (const value of values) { | ||
callback(value, key, this); | ||
for (const [key, value] of this) { | ||
callback(value, key, this); | ||
} | ||
} | ||
stream(boundary = '---') { | ||
const entries = []; | ||
return new PonyfillReadableStream({ | ||
start: async (controller) => { | ||
controller.enqueue(Buffer.from(`--${boundary}\r\n`)); | ||
this.forEach((value, key) => { | ||
entries.push([key, value]); | ||
}); | ||
}, | ||
pull: async (controller) => { | ||
const entry = entries.shift(); | ||
if (entry) { | ||
const [key, value] = entry; | ||
if (value instanceof PonyfillBlob) { | ||
let filenamePart = ''; | ||
if (value.name) { | ||
filenamePart = `; filename="${value.name}"`; | ||
} | ||
controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"${filenamePart}\r\n`)); | ||
controller.enqueue(Buffer.from(`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`)); | ||
controller.enqueue(Buffer.from(await value.arrayBuffer())); | ||
} | ||
else { | ||
controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`)); | ||
controller.enqueue(Buffer.from(value)); | ||
} | ||
if (entries.length === 0) { | ||
controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`)); | ||
controller.close(); | ||
} | ||
else { | ||
controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`)); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
function getNormalizedFile(name, blob, fileName) { | ||
if (blob instanceof PonyfillFile) { | ||
if (fileName != null) { | ||
return new PonyfillFile([blob], fileName, { type: blob.type, lastModified: blob.lastModified }); | ||
} | ||
return blob; | ||
} | ||
return new PonyfillFile([blob], fileName || name, { type: blob.type }); | ||
} | ||
@@ -295,77 +337,7 @@ | ||
this.contentLength = null; | ||
if (this.bodyInit == null) { | ||
this._body = null; | ||
} | ||
else if (typeof this.bodyInit === 'string') { | ||
this.bodyType = BodyInitType.String; | ||
const buffer = Buffer.from(this.bodyInit); | ||
this.contentType = 'text/plain;charset=UTF-8'; | ||
this.contentLength = buffer.length; | ||
this._body = new PonyfillReadableStream(stream.Readable.from(buffer)); | ||
} | ||
else if (this.bodyInit instanceof PonyfillReadableStream) { | ||
this.bodyType = BodyInitType.ReadableStream; | ||
this._body = this.bodyInit; | ||
} | ||
else if (this.bodyInit instanceof PonyfillBlob) { | ||
this.bodyType = BodyInitType.Blob; | ||
const blobStream = this.bodyInit.stream(); | ||
this.contentType = this.bodyInit.type; | ||
this.contentLength = this.bodyInit.size; | ||
this._body = new PonyfillReadableStream(blobStream); | ||
} | ||
else if (this.bodyInit instanceof PonyfillFormData) { | ||
this.bodyType = BodyInitType.FormData; | ||
const boundary = Math.random().toString(36).substr(2); | ||
const formData = this.bodyInit; | ||
this.contentType = `multipart/form-data; boundary=${boundary}`; | ||
this._body = new PonyfillReadableStream({ | ||
start: async (controller) => { | ||
controller.enqueue(Buffer.from(`--${boundary}\r\n`)); | ||
const entries = []; | ||
formData.forEach((value, key) => { | ||
entries.push([key, value]); | ||
}); | ||
for (const i in entries) { | ||
const [key, value] = entries[i]; | ||
if (value instanceof PonyfillBlob) { | ||
let filenamePart = ''; | ||
if (value.name) { | ||
filenamePart = `; filename="${value.name}"`; | ||
} | ||
controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"${filenamePart}\r\n`)); | ||
controller.enqueue(Buffer.from(`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`)); | ||
controller.enqueue(Buffer.from(await value.arrayBuffer())); | ||
} | ||
else { | ||
controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`)); | ||
controller.enqueue(Buffer.from(value)); | ||
} | ||
if (Number(i) === entries.length - 1) { | ||
controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`)); | ||
} | ||
else { | ||
controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`)); | ||
} | ||
} | ||
controller.close(); | ||
}, | ||
}); | ||
} | ||
else if ('buffer' in this.bodyInit) { | ||
this.contentLength = this.bodyInit.byteLength; | ||
this._body = new PonyfillReadableStream(stream.Readable.from(this.bodyInit)); | ||
} | ||
else if (this.bodyInit instanceof ArrayBuffer) { | ||
this.bodyType = BodyInitType.ArrayBuffer; | ||
this.contentLength = this.bodyInit.byteLength; | ||
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); | ||
} | ||
else { | ||
throw new Error('Unknown body type'); | ||
} | ||
const { body, contentType, contentLength, bodyType, } = processBodyInit(bodyInit); | ||
this._body = body; | ||
this.contentType = contentType; | ||
this.contentLength = contentLength; | ||
this.bodyType = bodyType; | ||
} | ||
@@ -416,3 +388,3 @@ get body() { | ||
} | ||
formData() { | ||
formData(opts) { | ||
if (this.bodyType === BodyInitType.FormData) { | ||
@@ -425,3 +397,6 @@ return Promise.resolve(this.bodyInit); | ||
} | ||
const formDataLimits = this.options.formDataLimits; | ||
const formDataLimits = { | ||
...this.options.formDataLimits, | ||
...opts === null || opts === void 0 ? void 0 : opts.formDataLimits, | ||
}; | ||
return new Promise((resolve, reject) => { | ||
@@ -491,2 +466,84 @@ var _a; | ||
} | ||
function processBodyInit(bodyInit) { | ||
if (bodyInit == null) { | ||
return { | ||
body: null, | ||
contentType: null, | ||
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, | ||
}; | ||
} | ||
if (bodyInit instanceof PonyfillReadableStream) { | ||
return { | ||
bodyType: BodyInitType.ReadableStream, | ||
body: bodyInit, | ||
contentType: null, | ||
contentLength: null, | ||
}; | ||
} | ||
if (bodyInit instanceof PonyfillBlob) { | ||
const readable = bodyInit.stream(); | ||
const body = new PonyfillReadableStream(readable); | ||
return { | ||
bodyType: BodyInitType.Blob, | ||
contentType: bodyInit.type, | ||
contentLength: bodyInit.size, | ||
body, | ||
}; | ||
} | ||
if (bodyInit instanceof PonyfillFormData) { | ||
const boundary = Math.random().toString(36).substr(2); | ||
const contentType = `multipart/form-data; boundary=${boundary}`; | ||
const body = bodyInit.stream(boundary); | ||
return { | ||
bodyType: BodyInitType.FormData, | ||
contentType, | ||
contentLength: null, | ||
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, | ||
}; | ||
} | ||
if (bodyInit instanceof ArrayBuffer) { | ||
const contentLength = bodyInit.byteLength; | ||
const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength); | ||
const readable = stream.Readable.from(buffer); | ||
const body = new PonyfillReadableStream(readable); | ||
return { | ||
bodyType: BodyInitType.ArrayBuffer, | ||
contentType: null, | ||
contentLength, | ||
body, | ||
}; | ||
} | ||
if (bodyInit instanceof stream.Readable) { | ||
const body = new PonyfillReadableStream(bodyInit); | ||
return { | ||
bodyType: BodyInitType.Readable, | ||
contentType: null, | ||
contentLength: null, | ||
body, | ||
}; | ||
} | ||
throw new Error('Unknown body type'); | ||
} | ||
@@ -511,3 +568,3 @@ class PonyfillHeaders { | ||
headersInit.forEach((value, key) => { | ||
this.set(key, value); | ||
this.append(key, value); | ||
}); | ||
@@ -740,3 +797,4 @@ } | ||
headers: nodeHeaders, | ||
}, nodeResponse => { | ||
}); | ||
nodeRequest.once('response', nodeResponse => { | ||
if (nodeResponse.headers.location) { | ||
@@ -769,3 +827,3 @@ if (fetchRequest.redirect === 'error') { | ||
}); | ||
nodeRequest.on('error', reject); | ||
nodeRequest.once('error', reject); | ||
if (nodeReadable) { | ||
@@ -772,0 +830,0 @@ nodeReadable.pipe(nodeRequest); |
{ | ||
"name": "@whatwg-node/node-fetch", | ||
"version": "0.0.1-alpha-20221228115139-6b81aa1", | ||
"version": "0.0.1-alpha-20221230075257-cc3a93b", | ||
"description": "Fetch API implementation for Node", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
Sorry, the diff of this file is not supported yet
70844
1906