-
-
Notifications
You must be signed in to change notification settings - Fork 644
feat: add support for multipart/form-data #1606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a5cbef7
6f9dd53
ff3a079
4bdc428
746d288
0580767
0a9785b
0b4c505
b079acb
5e9e5a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
'use strict' | ||
|
||
const Busboy = require('busboy') | ||
const util = require('../core/util') | ||
const { ReadableStreamFrom, toUSVString, isBlobLike } = require('./util') | ||
const { FormData } = require('./formdata') | ||
|
@@ -8,9 +9,9 @@ const { webidl } = require('./webidl') | |
const { Blob } = require('buffer') | ||
const { kBodyUsed } = require('../core/symbols') | ||
const assert = require('assert') | ||
const { NotSupportedError } = require('../core/errors') | ||
const { isErrored } = require('../core/util') | ||
const { isUint8Array, isArrayBuffer } = require('util/types') | ||
const { File } = require('./file') | ||
|
||
let ReadableStream | ||
|
||
|
@@ -397,7 +398,47 @@ function bodyMixinMethods (instance) { | |
|
||
// If mimeType’s essence is "multipart/form-data", then: | ||
if (/multipart\/form-data/.test(contentType)) { | ||
throw new NotSupportedError('multipart/form-data not supported') | ||
const headers = {} | ||
for (const [key, value] of this.headers) headers[key.toLowerCase()] = value | ||
|
||
const responseFormData = new FormData() | ||
|
||
let busboy | ||
mcollina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
try { | ||
busboy = Busboy({ headers }) | ||
} catch (err) { | ||
// Error due to headers: | ||
throw Object.assign(new TypeError(), { cause: err }) | ||
} | ||
|
||
busboy.on('field', (name, value) => { | ||
responseFormData.append(name, value) | ||
}) | ||
busboy.on('file', (name, value, info) => { | ||
const { filename, encoding, mimeType } = info | ||
const base64 = encoding.toLowerCase() === 'base64' | ||
const chunks = [] | ||
value.on('data', (chunk) => { | ||
if (base64) chunk = Buffer.from(chunk.toString(), 'base64') | ||
mcollina marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't just decrypt a base64 chunk, because the length of the base64 chunk must be a multiple of 4 characters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const data = Buffer.from(Buffer.from('hello world').toString('base64'));
const chunks1 = [];
const chunks2 = [];
for (let offset = 0, step = 6; offset < data.length; offset += step) {
const chunk = data.subarray(offset, offset + step);
// Decode each chunk
chunks1.push(Buffer.from(chunk.toString(), 'base64'));
// Here we collect as is
chunks2.push(chunk);
}
// Decode after the transfer is completed
const buffer = Buffer.from(Buffer.concat(chunks2).toString(), 'base64');
// hell�v�ld
console.log(await new Blob(chunks1).text());
// hello world
console.log(await new Blob([buffer]).text()); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have the https://nodejs.org/api/string_decoder.html for this purpose. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mcollina does string decoder need to be applied somewhere in this code explicitly, or it will be called down the line automatically? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cameron-robey see what @repsac-by is suggesting, plus test to cover that case. inlining the code should be fine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the problem exactly? You need 4 bytes to decode base64 at minimum, so why don't you accumulate until you have 4 bytes of multiples? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mcollina I'm assuming that you mean: push chunks until the total length of unprocessed data is divisible by 4, then decode and clear the chunk array, and repeat with the next pieces of data. Accumulating like that seems unreliable, since it cannot be known when that multiple of 4 will be reached. It might happen only after a huge number of chunks. The streaming method suggested above is superior because the number of unprocessed bytes that are kept in memory never exceeds 3. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The one in the comment also looks good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cameron-robey Do you need any help with the remaining part? |
||
chunks.push(chunk) | ||
}) | ||
value.on('end', () => { | ||
const file = new File(chunks, filename, { type: mimeType }) | ||
responseFormData.append(name, file) | ||
}) | ||
}) | ||
mcollina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const busboyResolve = new Promise((resolve, reject) => { | ||
busboy.on('finish', resolve) | ||
busboy.on('error', (err) => reject(err)) | ||
mcollina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
|
||
if (this.body !== null) for await (const chunk of consumeBody(this[kState].body)) busboy.write(chunk) | ||
busboy.end() | ||
await busboyResolve | ||
|
||
return responseFormData | ||
} else if (/application\/x-www-form-urlencoded/.test(contentType)) { | ||
// Otherwise, if mimeType’s essence is "application/x-www-form-urlencoded", then: | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.