Skip to content

Commit 1a7f4a9

Browse files
authored
fix: added support for compression - gzip, deflate, brotli (#1017)
* Added Support for Compression For: Gzip, Deflate, Brotli and Compress * Update Formidable.js * Removed Temporary Console.log statements --------- Co-authored-by: Coding4ever123 <148124024+Coding4ever123@users.noreply.github.com>
1 parent 8a25a45 commit 1a7f4a9

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

src/Formidable.js

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,23 @@ class IncomingForm extends EventEmitter {
232232
// Parse headers and setup the parser, ready to start listening for data.
233233
await this.writeHeaders(req.headers);
234234

235+
let datafn = (buffer) => {
236+
try {
237+
this.write(buffer);
238+
} catch (err) {
239+
this._error(err);
240+
}
241+
}
242+
let endfn = () => {
243+
if (this.error) {
244+
return;
245+
}
246+
if (this._parser) {
247+
this._parser.end();
248+
}
249+
}
250+
let pipe = null;
251+
235252
// Start listening for data.
236253
req
237254
.on('error', (err) => {
@@ -240,22 +257,33 @@ class IncomingForm extends EventEmitter {
240257
.on('aborted', () => {
241258
this.emit('aborted');
242259
this._error(new FormidableError('Request aborted', errors.aborted));
243-
})
244-
.on('data', (buffer) => {
245-
try {
246-
this.write(buffer);
247-
} catch (err) {
248-
this._error(err);
249-
}
250-
})
251-
.on('end', () => {
252-
if (this.error) {
253-
return;
254-
}
255-
if (this._parser) {
256-
this._parser.end();
257-
}
258-
});
260+
})
261+
262+
switch (this.headers['content-encoding']) {
263+
case "gzip":
264+
pipe = require("zlib").createGunzip();
265+
break;
266+
case "deflate":
267+
pipe = require("zlib").createInflate();
268+
break;
269+
case "br":
270+
pipe = require("zlib").createBrotliDecompress();
271+
break;
272+
case "compress":
273+
pipe = require("zlib").createUnzip();
274+
break;
275+
276+
default:
277+
pipe = node_stream.Transform({
278+
transform: function (chunk, encoding, callback) {
279+
callback(null, chunk);
280+
}
281+
282+
})
283+
}
284+
pipe.on("data", datafn).on('end', endfn);
285+
req.pipe(pipe)
286+
259287
if (promise) {
260288
return promise;
261289
}

0 commit comments

Comments
 (0)