Skip to content

Commit

Permalink
feat(fetch): new fieldsFirst option to allow async stream consumpti…
Browse files Browse the repository at this point in the history
…on for multipart forms
  • Loading branch information
ardatan committed Sep 7, 2022
1 parent 6a92487 commit 005937c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-experts-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@whatwg-node/fetch': minor
---

feat(fetch): new `fieldsFirst` option to allow async stream consumption for multipart forms
91 changes: 75 additions & 16 deletions packages/fetch/dist/getFormDataMethod.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
const busboy = require('busboy');
const { resolve } = require('path');
const streams = require("stream");

module.exports = function getFormDataMethod(File, limits) {
function consumeStreamAsFile({
name,
filename,
mimeType,
fileStream,
formData,
}) {
if (fileStream._consumedAsFile) {
return Promise.resolve(formData.get(name));
}
return new Promise((resolve, reject) => {
const chunks = [];
fileStream.on('limit', () => {
reject(new Error(`File size limit exceeded: ${limits.fileSize} bytes`));
})
fileStream.on('data', (chunk) => {
chunks.push(chunk);
})
fileStream.on('close', () => {
const file = new File(chunks, filename, { type: mimeType });
formData.set(name, file);
fileStream._consumedAsFile = true;
resolve(file);
});
})
}

return function formData() {
if (this.body == null) {
return null;
Expand All @@ -17,18 +45,6 @@ module.exports = function getFormDataMethod(File, limits) {
});
return new Promise((resolve, reject) => {
const formData = new Map();
bb.on('file', (name, fileStream, { filename, mimeType }) => {
const chunks = [];
fileStream.on('limit', () => {
reject(new Error(`File size limit exceeded: ${limits.fileSize} bytes`));
})
fileStream.on('data', (chunk) => {
chunks.push(chunk);
})
fileStream.on('close', () => {
formData.set(name, new File(chunks, filename, { type: mimeType }));
});
})
bb.on('field', (name, value, { nameTruncated, valueTruncated }) => {
if (nameTruncated) {
reject(new Error(`Field name size exceeded: ${limits.fieldNameSize} bytes`));
Expand All @@ -38,14 +54,57 @@ module.exports = function getFormDataMethod(File, limits) {
}
formData.set(name, value)
})
bb.on('partsLimit', () => {
reject(new Error(`Parts limit exceeded: ${limits.parts}`));
bb.on('fieldsLimit', () => {
reject(new Error(`Fields limit exceeded: ${limits.fields}`));
})
bb.on('file', (name, fileStream, { filename, mimeType }) => {
if (limits.fieldsFirst) {
resolve(formData);
const fakeFileObj = {
name: filename,
type: mimeType,
}
Object.setPrototypeOf(fakeFileObj, File.prototype);
formData.set(name, new Proxy(fakeFileObj, {
get: (target, prop) => {
switch(prop) {
case 'name':
return filename;
case 'type':
return mimeType;
case 'stream':
return () => fileStream;
case 'size':
throw new Error(`Cannot access file size before consuming the stream.`);
case 'slice':
throw new Error(`Cannot slice file before consuming the stream.`);
case 'text':
case 'arrayBuffer':
return () => consumeStreamAsFile({
name,
filename,
mimeType,
fileStream,
formData,
}).then(file => file[prop]())
}
},
}))
} else {
consumeStreamAsFile({
name,
filename,
mimeType,
fileStream,
formData,
}).catch(err => reject(err));
}
})
bb.on('filesLimit', () => {
reject(new Error(`Files limit exceeded: ${limits.files}`));
})
bb.on('fieldsLimit', () => {
reject(new Error(`Fields limit exceeded: ${limits.fields}`));
bb.on('partsLimit', () => {
reject(new Error(`Parts limit exceeded: ${limits.parts}`));
})
bb.on('close', () => {
resolve(formData);
Expand Down
2 changes: 2 additions & 0 deletions packages/fetch/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ declare module "@whatwg-node/fetch" {
parts?: number;
/* For multipart forms, the max number of header key-value pairs to parse. Default: 2000. */
headerSize?: number;
/* For multipart forms, enable this if your data has fields first, then files. Default: false. */
fieldsFirst?: boolean;
}
export const createFetch: (opts?: { useNodeFetch?: boolean; formDataLimits?: FormDataLimits }) => ({
fetch: typeof _fetch,
Expand Down

0 comments on commit 005937c

Please sign in to comment.