From 7f37b6dbeb76cfa54e0ed8672812bf016c1df4fa Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Wed, 7 Sep 2022 18:07:13 +0300 Subject: [PATCH] fix(fetch): respect filesLimit even with fieldsFirst --- .changeset/tasty-seahorses-grin.md | 5 +++ packages/fetch/dist/getFormDataMethod.js | 44 ++++++++++++++---------- 2 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 .changeset/tasty-seahorses-grin.md diff --git a/.changeset/tasty-seahorses-grin.md b/.changeset/tasty-seahorses-grin.md new file mode 100644 index 00000000000..de761b4d20f --- /dev/null +++ b/.changeset/tasty-seahorses-grin.md @@ -0,0 +1,5 @@ +--- +'@whatwg-node/fetch': patch +--- + +fix(fetch): respect filesLimit even with fieldsFirst diff --git a/packages/fetch/dist/getFormDataMethod.js b/packages/fetch/dist/getFormDataMethod.js index 871e6a9fa56..0255e37c43d 100644 --- a/packages/fetch/dist/getFormDataMethod.js +++ b/packages/fetch/dist/getFormDataMethod.js @@ -10,9 +10,6 @@ module.exports = function getFormDataMethod(File, limits) { fileStream, formData, }) { - if (fileStream._consumedAsFile) { - return Promise.resolve(formData.get(name)); - } return new Promise((resolve, reject) => { const chunks = []; fileStream.on('limit', () => { @@ -22,9 +19,11 @@ module.exports = function getFormDataMethod(File, limits) { chunks.push(chunk); }) fileStream.on('close', () => { + if (fileStream.truncated) { + reject(new Error(`File size limit exceeded: ${limits.fileSize} bytes`)); + } const file = new File(chunks, filename, { type: mimeType }); formData.set(name, file); - fileStream._consumedAsFile = true; resolve(file); }); }) @@ -58,6 +57,7 @@ module.exports = function getFormDataMethod(File, limits) { reject(new Error(`Fields limit exceeded: ${limits.fields}`)); }) bb.on('file', (name, fileStream, { filename, mimeType }) => { + let file$; if (limits && limits.fieldsFirst) { resolve(formData); const fakeFileObj = { @@ -80,24 +80,32 @@ module.exports = function getFormDataMethod(File, limits) { 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]()) + return () => { + if (!file$) { + file$ = consumeStreamAsFile({ + name, + filename, + mimeType, + fileStream, + formData, + }) + } + return file$.then(file => file[prop]()); + } } }, })) } else { - consumeStreamAsFile({ - name, - filename, - mimeType, - fileStream, - formData, - }).catch(err => reject(err)); + if (!file$) { + file$ = consumeStreamAsFile({ + name, + filename, + mimeType, + fileStream, + formData, + }) + } + file$.catch(reject); } }) bb.on('filesLimit', () => {