Skip to content

fix: import Transform from node:stream to fix ReferenceError on unencoded requests#1069

Open
guoyangzhen wants to merge 3 commits intonode-formidable:masterfrom
guoyangzhen:fix/content-encoding-crash
Open

fix: import Transform from node:stream to fix ReferenceError on unencoded requests#1069
guoyangzhen wants to merge 3 commits intonode-formidable:masterfrom
guoyangzhen:fix/content-encoding-crash

Conversation

@guoyangzhen
Copy link

@guoyangzhen guoyangzhen commented Mar 21, 2026

Fixes #1063

Problem

Commit 1a7f4a9 introduced compression support with a switch on this.headers["content-encoding"] in src/Formidable.js. This has two ESM-breaking bugs:

  1. Default case (uncompressed): References node_stream.Transform() but node_stream is never imported → ReferenceError
  2. Compressed cases: Uses require("zlib") in an ESM module (package.json has "type": "module") → ReferenceError: require is not defined

Every real-world multipart upload without Content-Encoding header hits case #1. Every compressed request hits case #2.

Fix

  1. Added import { Transform } from "node:stream" and changed node_stream.Transform({...}) to new Transform({...})
  2. Replaced all require("zlib").createXxx() calls with proper ESM imports: import { createGunzip, createInflate, createBrotliDecompress, createUnzip } from "node:zlib"

Testing

Verified the imports are valid ESM by checking node:zlib and node:stream export these functions in Node.js 18+.

guoyangzhen and others added 3 commits March 16, 2026 22:46
Fixes node-formidable#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.
…oded requests

Fixes node-formidable#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
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 node-formidable#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)
@GrosSacASac
Copy link
Contributor

could we merge existing PR before creating new ones ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compression support (#1017) crashes on requests without Content-Encoding

2 participants