Skip to content

Commit c74be8e

Browse files
fix: import Transform from node:stream to fix ReferenceError on unencoded requests (#1069)
* fix: prevent file handle leak when maxFiles is exceeded Fixes #987 When maxFiles limit is reached, the fileBegin event handler calls _error(), but _handlePart continues and opens a write stream for the new file. These file handles are never closed. Fix: check this.error after emitting fileBegin and before file.open(). If an error occurred (e.g., maxFiles exceeded), decrement _flushing and return early to prevent the file stream from being opened. * fix: import Transform from node:stream to fix ReferenceError on unencoded requests Fixes #1063 The compression support switch (commit 1a7f4a9) references in the default case, but is never imported. Every request without a Content-Encoding header throws . Changes: - Add - Use instead of * fix: use ESM imports for node:zlib instead of require() The module uses 'type': "module" in package.json, so require() is not available. Replace all require('zlib') calls with proper ESM imports (createGunzip, createInflate, createBrotliDecompress, createUnzip). This fixes #1063 more completely: - The node_stream Transform import from previous commit fixes the ReferenceError for uncompressed requests - This commit fixes ReferenceError: require is not defined for compressed requests (gzip, deflate, br, compress) --------- Co-authored-by: guoyangzhen <guoyangzhen@users.noreply.github.com> Co-authored-by: tunnckoCore <5038030+tunnckoCore@users.noreply.github.com>
1 parent cf9abfe commit c74be8e

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

src/Formidable.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ import dezalgo from 'dezalgo';
66
import { EventEmitter } from 'node:events';
77
import fsPromises from 'node:fs/promises';
88
import os from 'node:os';
9-
import stream from 'node:stream';
109
import path from 'node:path';
1110
import { StringDecoder } from 'node:string_decoder';
11+
import { Transform } from 'node:stream';
12+
import {
13+
createGunzip,
14+
createInflate,
15+
createBrotliDecompress,
16+
createUnzip,
17+
} from 'node:zlib';
1218
import once from 'once';
1319
import FormidableError, * as errors from './FormidableError.js';
1420
import PersistentFile from './PersistentFile.js';
@@ -262,25 +268,24 @@ class IncomingForm extends EventEmitter {
262268

263269
switch (this.headers['content-encoding']) {
264270
case "gzip":
265-
pipe = require("zlib").createGunzip();
271+
pipe = createGunzip();
266272
break;
267273
case "deflate":
268-
pipe = require("zlib").createInflate();
274+
pipe = createInflate();
269275
break;
270276
case "br":
271-
pipe = require("zlib").createBrotliDecompress();
277+
pipe = createBrotliDecompress();
272278
break;
273279
case "compress":
274-
pipe = require("zlib").createUnzip();
280+
pipe = createUnzip();
275281
break;
276282

277-
default:
278-
pipe = stream.Transform({
279-
transform: function (chunk, encoding, callback) {
283+
default:
284+
pipe = new Transform({
285+
transform(chunk, encoding, callback) {
280286
callback(null, chunk);
281-
}
282-
283-
})
287+
},
288+
});
284289
}
285290
pipe.on("data", datafn).on('end', endfn);
286291
req.pipe(pipe)

0 commit comments

Comments
 (0)