Skip to content

Commit

Permalink
use sreaing api
Browse files Browse the repository at this point in the history
end does not return errors, use 'error' event instead
write does not return length anymore
  • Loading branch information
GrosSacASac committed Dec 17, 2019
1 parent 2f9191d commit 214708a
Showing 1 changed file with 11 additions and 37 deletions.
48 changes: 11 additions & 37 deletions lib/multipart_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ for (s in S) {
exports[s] = S[s];
}

function capital(string) {
return `${string.substr(0, 1).toUpperCase()}${string.substr(1)}`;
}

class MultipartParser extends Transform {
constructor() {
super({ readableObjectMode: true });
Expand All @@ -56,11 +52,15 @@ class MultipartParser extends Transform {
this.flags = 0;
}

_flush(callback) {
if (this.state !== S.END) {
callback(new Error(this.explain()));
}
callback();
_final(callback) {
if ((this.state == S.HEADER_FIELD_START && this.index === 0) ||
(this.state == S.PART_DATA && this.index == this.boundary.length)) {
this.callback('partEnd');
this.callback('end');
callback();
} else if (this.state != S.END) {
callback(new Error(`MultipartParser.end(): stream ended unexpectedly: ${this.explain()}`));
}
}

initWithBoundary (str) {
Expand Down Expand Up @@ -100,11 +100,7 @@ class MultipartParser extends Transform {
if (start !== undefined && start === end) {
return;
}

var callbackSymbol = `on${capital(name)}`;
if (callbackSymbol in this) {
this[callbackSymbol](buffer, start, end);
}
this.push({name, buffer, start, end});
},
dataCallback = (name, clear) => {
var markSymbol = `${name}Mark`;
Expand Down Expand Up @@ -314,24 +310,8 @@ class MultipartParser extends Transform {

return len;
}

end () {
var callback = function(multipartParser, name) {
var callbackSymbol = `on${capital(name)}`;
if (callbackSymbol in multipartParser) {
multipartParser[callbackSymbol]();
}
};
if ((this.state == S.HEADER_FIELD_START && this.index === 0) ||
(this.state == S.PART_DATA && this.index == this.boundary.length)) {
callback(this, 'partEnd');
callback(this, 'end');
} else if (this.state != S.END) {
return new Error(`MultipartParser.end(): stream ended unexpectedly: ${this.explain()}`);
}
}

explain = function() {
explain () {
return `state = ${MultipartParser.stateToString(this.state)}`;
}
}
Expand All @@ -343,9 +323,3 @@ MultipartParser.stateToString = function(stateNumber) {
}
};
exports.MultipartParser = MultipartParser;






0 comments on commit 214708a

Please sign in to comment.