Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions src/Formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@ class IncomingForm extends EventEmitter {
// Parse headers and setup the parser, ready to start listening for data.
await this.writeHeaders(req.headers);

let datafn = (buffer) => {
try {
this.write(buffer);
} catch (err) {
this._error(err);
}
}
let endfn = () => {
if (this.error) {
return;
}
if (this._parser) {
this._parser.end();
}
}
let pipe = null;

// Start listening for data.
req
.on('error', (err) => {
Expand All @@ -240,22 +257,33 @@ class IncomingForm extends EventEmitter {
.on('aborted', () => {
this.emit('aborted');
this._error(new FormidableError('Request aborted', errors.aborted));
})
.on('data', (buffer) => {
try {
this.write(buffer);
} catch (err) {
this._error(err);
}
})
.on('end', () => {
if (this.error) {
return;
}
if (this._parser) {
this._parser.end();
}
});
})

switch (this.headers['content-encoding']) {
case "gzip":
pipe = require("zlib").createGunzip();
break;
case "deflate":
pipe = require("zlib").createInflate();
break;
case "br":
pipe = require("zlib").createBrotliDecompress();
break;
case "compress":
pipe = require("zlib").createUnzip();
break;

default:
pipe = node_stream.Transform({
transform: function (chunk, encoding, callback) {
callback(null, chunk);
}

})
}
pipe.on("data", datafn).on('end', endfn);
req.pipe(pipe)

if (promise) {
return promise;
}
Expand Down