@whatwg-node/node-fetch
Advanced tools
Comparing version 0.0.1-alpha-20221228083733-4041c71 to 0.0.1-alpha-20221228110205-1d6dcac
@@ -7,4 +7,17 @@ /// <reference types="node" /> | ||
export type BodyPonyfillInit = XMLHttpRequestBodyInit | Readable | PonyfillReadableStream<Uint8Array>; | ||
export interface FormDataLimits { | ||
fieldNameSize?: number; | ||
fieldSize?: number; | ||
fields?: number; | ||
fileSize?: number; | ||
files?: number; | ||
parts?: number; | ||
headerSize?: number; | ||
} | ||
export interface PonyfillBodyOptions { | ||
formDataLimits?: FormDataLimits; | ||
} | ||
export declare class PonyfillBody implements Body { | ||
private bodyInit; | ||
private options; | ||
bodyUsed: boolean; | ||
@@ -14,3 +27,3 @@ private _body; | ||
contentLength: number | null; | ||
constructor(bodyInit: BodyPonyfillInit | null); | ||
constructor(bodyInit: BodyPonyfillInit | null, options?: PonyfillBodyOptions); | ||
private bodyType?; | ||
@@ -17,0 +30,0 @@ get body(): PonyfillReadableStream<Uint8Array> | null; |
83
index.js
@@ -5,2 +5,4 @@ 'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
const http = require('http'); | ||
@@ -11,2 +13,3 @@ const https = require('https'); | ||
const stream = require('stream'); | ||
const busboy = _interopDefault(require('busboy')); | ||
const url = require('url'); | ||
@@ -296,4 +299,5 @@ const fs = require('fs'); | ||
class PonyfillBody { | ||
constructor(bodyInit) { | ||
constructor(bodyInit, options = {}) { | ||
this.bodyInit = bodyInit; | ||
this.options = options; | ||
this.bodyUsed = false; | ||
@@ -423,7 +427,62 @@ this._body = null; | ||
} | ||
async formData() { | ||
formData() { | ||
if (this.bodyType === BodyInitType.FormData) { | ||
return this.bodyInit; | ||
return Promise.resolve(this.bodyInit); | ||
} | ||
throw new Error('Not implemented'); | ||
const formData = new PonyfillFormData(); | ||
if (this._body == null) { | ||
return Promise.resolve(formData); | ||
} | ||
const formDataLimits = this.options.formDataLimits; | ||
return new Promise((resolve, reject) => { | ||
var _a; | ||
const bb = busboy({ | ||
headers: { | ||
'content-type': this.contentType || '' | ||
}, | ||
limits: formDataLimits, | ||
defParamCharset: 'utf-8' | ||
}); | ||
bb.on('field', (name, value, { nameTruncated, valueTruncated }) => { | ||
if (nameTruncated) { | ||
reject(new Error(`Field name size exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fieldNameSize} bytes`)); | ||
} | ||
if (valueTruncated) { | ||
reject(new Error(`Field value size exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fieldSize} bytes`)); | ||
} | ||
formData.set(name, value); | ||
}); | ||
bb.on('fieldsLimit', () => { | ||
reject(new Error(`Fields limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fields}`)); | ||
}); | ||
bb.on('file', (name, fileStream, { filename, mimeType }) => { | ||
const chunks = []; | ||
fileStream.on('limit', () => { | ||
reject(new Error(`File size limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fileSize} bytes`)); | ||
}); | ||
fileStream.on('data', (chunk) => { | ||
chunks.push(Buffer.from(chunk)); | ||
}); | ||
fileStream.on('close', () => { | ||
if (fileStream.truncated) { | ||
reject(new Error(`File size limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fileSize} bytes`)); | ||
} | ||
const file = new PonyfillFile(chunks, filename, { type: mimeType }); | ||
formData.set(name, file); | ||
}); | ||
}); | ||
bb.on('filesLimit', () => { | ||
reject(new Error(`Files limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.files}`)); | ||
}); | ||
bb.on('partsLimit', () => { | ||
reject(new Error(`Parts limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.parts}`)); | ||
}); | ||
bb.on('close', () => { | ||
resolve(formData); | ||
}); | ||
bb.on('error', err => { | ||
reject(err); | ||
}); | ||
(_a = this._body) === null || _a === void 0 ? void 0 : _a.readable.pipe(bb); | ||
}); | ||
} | ||
@@ -547,3 +606,3 @@ async json() { | ||
} | ||
super(bodyInit); | ||
super(bodyInit, options); | ||
this.destination = ''; | ||
@@ -568,3 +627,4 @@ this.priority = 'auto'; | ||
} | ||
if (!this.headers.has('content-type')) { | ||
const contentTypeInHeaders = this.headers.get('content-type'); | ||
if (!contentTypeInHeaders) { | ||
if (this.contentType) { | ||
@@ -574,3 +634,7 @@ this.headers.set('content-type', this.contentType); | ||
} | ||
if (!this.headers.has('content-length')) { | ||
else { | ||
this.contentType = contentTypeInHeaders; | ||
} | ||
const contentLengthInHeaders = this.headers.get('content-length'); | ||
if (!contentLengthInHeaders) { | ||
if (this.contentLength) { | ||
@@ -580,2 +644,5 @@ this.headers.set('content-length', this.contentLength.toString()); | ||
} | ||
else { | ||
this.contentLength = parseInt(contentLengthInHeaders, 10); | ||
} | ||
} | ||
@@ -589,3 +656,3 @@ clone() { | ||
constructor(body, init) { | ||
super(body || null); | ||
super(body || null, init); | ||
this.headers = new PonyfillHeaders(); | ||
@@ -592,0 +659,0 @@ this.status = 200; |
{ | ||
"name": "@whatwg-node/node-fetch", | ||
"version": "0.0.1-alpha-20221228083733-4041c71", | ||
"version": "0.0.1-alpha-20221228110205-1d6dcac", | ||
"description": "Fetch API implementation for Node", | ||
@@ -11,2 +11,3 @@ "sideEffects": false, | ||
"@whatwg-node/events": "0.0.2", | ||
"busboy": "1.6.0", | ||
"tslib": "^2.3.1" | ||
@@ -13,0 +14,0 @@ }, |
@@ -1,4 +0,4 @@ | ||
import { PonyfillBody, BodyPonyfillInit } from './Body'; | ||
import { PonyfillBody, BodyPonyfillInit, PonyfillBodyOptions } from './Body'; | ||
import { PonyfillHeadersInit } from './Headers'; | ||
export type RequestPonyfillInit = Omit<RequestInit, 'body' | 'headers'> & { | ||
export type RequestPonyfillInit = PonyfillBodyOptions & Omit<RequestInit, 'body' | 'headers'> & { | ||
body?: BodyPonyfillInit | null; | ||
@@ -5,0 +5,0 @@ headers?: PonyfillHeadersInit; |
@@ -1,4 +0,4 @@ | ||
import { PonyfillBody, BodyPonyfillInit } from './Body'; | ||
import { PonyfillBody, BodyPonyfillInit, PonyfillBodyOptions } from './Body'; | ||
import { PonyfillHeadersInit } from './Headers'; | ||
export type ResponsePonyfilInit = Omit<ResponseInit, 'headers'> & { | ||
export type ResponsePonyfilInit = PonyfillBodyOptions & Omit<ResponseInit, 'headers'> & { | ||
url?: string; | ||
@@ -5,0 +5,0 @@ redirected?: boolean; |
Sorry, the diff of this file is not supported yet
69262
1819
4
+ Addedbusboy@1.6.0
+ Addedbusboy@1.6.0(transitive)
+ Addedstreamsearch@1.1.0(transitive)