|
1 | 1 | /* eslint-disable @typescript-eslint/ban-ts-comment */
|
2 | 2 | import { Buffer } from 'node:buffer';
|
3 | 3 | import { IncomingMessage } from 'node:http';
|
4 |
| -import { Readable } from 'node:stream'; |
5 |
| -import busboy from 'busboy'; |
| 4 | +import { addAbortSignal, Readable } from 'node:stream'; |
| 5 | +import { Busboy, BusboyFileStream } from '@fastify/busboy'; |
6 | 6 | import { handleMaybePromise, MaybePromise } from '@whatwg-node/promise-helpers';
|
7 | 7 | import { hasArrayBufferMethod, hasBufferMethod, hasBytesMethod, PonyfillBlob } from './Blob.js';
|
8 | 8 | import { PonyfillFile } from './File.js';
|
@@ -244,59 +244,104 @@ export class PonyfillBody<TJSON = any> implements Body {
|
244 | 244 | ...opts?.formDataLimits,
|
245 | 245 | };
|
246 | 246 | return new Promise((resolve, reject) => {
|
247 |
| - const bb = busboy({ |
| 247 | + const stream = this.body?.readable; |
| 248 | + if (!stream) { |
| 249 | + return reject(new Error('No stream available')); |
| 250 | + } |
| 251 | + |
| 252 | + // form data file that is currently being processed, it's |
| 253 | + // important to keep track of it in case the stream ends early |
| 254 | + let currFile: BusboyFileStream | null = null; |
| 255 | + |
| 256 | + const bb = new Busboy({ |
248 | 257 | headers: {
|
| 258 | + 'content-length': |
| 259 | + typeof this.contentLength === 'number' |
| 260 | + ? this.contentLength.toString() |
| 261 | + : this.contentLength || '', |
249 | 262 | 'content-type': this.contentType || '',
|
250 | 263 | },
|
251 | 264 | limits: formDataLimits,
|
252 |
| - defParamCharset: 'utf-8', |
| 265 | + defCharset: 'utf-8', |
253 | 266 | });
|
254 |
| - bb.on('field', (name, value, { nameTruncated, valueTruncated }) => { |
255 |
| - if (nameTruncated) { |
256 |
| - reject(new Error(`Field name size exceeded: ${formDataLimits?.fieldNameSize} bytes`)); |
| 267 | + |
| 268 | + if (this.signal) { |
| 269 | + addAbortSignal(this.signal, bb); |
| 270 | + } |
| 271 | + |
| 272 | + let completed = false; |
| 273 | + const complete = (err: unknown) => { |
| 274 | + if (completed) return; |
| 275 | + completed = true; |
| 276 | + stream!.unpipe(bb); |
| 277 | + bb.destroy(); |
| 278 | + if (currFile) { |
| 279 | + currFile.destroy(); |
| 280 | + currFile = null; |
| 281 | + } |
| 282 | + if (err) { |
| 283 | + reject(err); |
| 284 | + } else { |
| 285 | + // no error occured, this is a successful end/complete/finish |
| 286 | + resolve(this._formData!); |
| 287 | + } |
| 288 | + }; |
| 289 | + |
| 290 | + // we dont need to listen to the stream close event because bb will close or error when necessary |
| 291 | + // stream.on('close', complete); |
| 292 | + |
| 293 | + // stream can be aborted, for example |
| 294 | + stream.on('error', complete); |
| 295 | + |
| 296 | + bb.on('field', (name, value, fieldnameTruncated, valueTruncated) => { |
| 297 | + if (fieldnameTruncated) { |
| 298 | + return complete( |
| 299 | + new Error(`Field name size exceeded: ${formDataLimits?.fieldNameSize} bytes`), |
| 300 | + ); |
257 | 301 | }
|
258 | 302 | if (valueTruncated) {
|
259 |
| - reject(new Error(`Field value size exceeded: ${formDataLimits?.fieldSize} bytes`)); |
| 303 | + return complete( |
| 304 | + new Error(`Field value size exceeded: ${formDataLimits?.fieldSize} bytes`), |
| 305 | + ); |
260 | 306 | }
|
261 | 307 | this._formData!.set(name, value);
|
262 | 308 | });
|
| 309 | + |
| 310 | + bb.on('file', (name, fileStream, filename, _transferEncoding, mimeType) => { |
| 311 | + currFile = fileStream; |
| 312 | + const chunks: BlobPart[] = []; |
| 313 | + fileStream.on('data', chunk => { |
| 314 | + chunks.push(chunk); |
| 315 | + }); |
| 316 | + fileStream.on('error', complete); |
| 317 | + fileStream.on('limit', () => { |
| 318 | + complete(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`)); |
| 319 | + }); |
| 320 | + fileStream.on('close', () => { |
| 321 | + if (fileStream.truncated) { |
| 322 | + complete(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`)); |
| 323 | + } |
| 324 | + currFile = null; |
| 325 | + const file = new PonyfillFile(chunks, filename, { type: mimeType }); |
| 326 | + this._formData!.set(name, file); |
| 327 | + }); |
| 328 | + }); |
| 329 | + |
263 | 330 | bb.on('fieldsLimit', () => {
|
264 |
| - reject(new Error(`Fields limit exceeded: ${formDataLimits?.fields}`)); |
| 331 | + complete(new Error(`Fields limit exceeded: ${formDataLimits?.fields}`)); |
265 | 332 | });
|
266 |
| - bb.on( |
267 |
| - 'file', |
268 |
| - (name, fileStream: Readable & { truncated: boolean }, { filename, mimeType }) => { |
269 |
| - const chunks: BlobPart[] = []; |
270 |
| - fileStream.on('limit', () => { |
271 |
| - reject(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`)); |
272 |
| - }); |
273 |
| - fileStream.on('data', chunk => { |
274 |
| - chunks.push(chunk); |
275 |
| - }); |
276 |
| - fileStream.on('close', () => { |
277 |
| - if (fileStream.truncated) { |
278 |
| - reject(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`)); |
279 |
| - } |
280 |
| - const file = new PonyfillFile(chunks, filename, { type: mimeType }); |
281 |
| - this._formData!.set(name, file); |
282 |
| - }); |
283 |
| - }, |
284 |
| - ); |
285 | 333 | bb.on('filesLimit', () => {
|
286 |
| - reject(new Error(`Files limit exceeded: ${formDataLimits?.files}`)); |
| 334 | + complete(new Error(`Files limit exceeded: ${formDataLimits?.files}`)); |
287 | 335 | });
|
288 | 336 | bb.on('partsLimit', () => {
|
289 |
| - reject(new Error(`Parts limit exceeded: ${formDataLimits?.parts}`)); |
| 337 | + complete(new Error(`Parts limit exceeded: ${formDataLimits?.parts}`)); |
290 | 338 | });
|
291 |
| - bb.on('close', () => { |
292 |
| - resolve(this._formData!); |
293 |
| - }); |
294 |
| - bb.on('error', (err: any = 'An error occurred while parsing the form data') => { |
295 |
| - const errMessage = err.message || err.toString(); |
296 |
| - // @ts-ignore - `cause` is in `TypeError`in node |
297 |
| - reject(new TypeError(errMessage, err.cause)); |
298 |
| - }); |
299 |
| - _body?.readable.pipe(bb); |
| 339 | + bb.on('end', complete); |
| 340 | + bb.on('finish', complete); |
| 341 | + bb.on('close', complete); |
| 342 | + bb.on('error', complete); |
| 343 | + |
| 344 | + stream.pipe(bb); |
300 | 345 | });
|
301 | 346 | }
|
302 | 347 |
|
|
0 commit comments