Skip to content

Commit

Permalink
Emit 'progress' event before parsing data
Browse files Browse the repository at this point in the history
The 'progress' event is meant to indicate upload progress, not parsing
progress. This patch puts things into the right order.
  • Loading branch information
Tim Koschützki authored and felixge committed Dec 19, 2010
1 parent 2b02144 commit f22993a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/formidable/incoming_form.js
Expand Up @@ -115,14 +115,14 @@ IncomingForm.prototype.write = function(buffer) {
return;
}

this.bytesReceived += buffer.length;
this.emit('progress', this.bytesReceived, this.bytesExpected);

var bytesParsed = this._parser.write(buffer);
if (bytesParsed !== buffer.length) {
this._error(new Error('parser error, '+bytesParsed+' of '+buffer.length+' bytes parsed'));
}

this.bytesReceived += bytesParsed;
this.emit('progress', this.bytesReceived, this.bytesExpected);

return bytesParsed;
};

Expand Down
16 changes: 8 additions & 8 deletions test/simple/test-incoming-form.js
Expand Up @@ -260,22 +260,24 @@ test(function write() {
form.bytesExpected = 523423;

(function testBasic() {
gently.expect(parser, 'write', function(buffer) {
assert.strictEqual(buffer, BUFFER);
return buffer.length;
});

gently.expect(form, 'emit', function(event, bytesReceived, bytesExpected) {
assert.equal(event, 'progress');
assert.equal(bytesReceived, BUFFER.length);
assert.equal(bytesExpected, form.bytesExpected);
});

gently.expect(parser, 'write', function(buffer) {
assert.strictEqual(buffer, BUFFER);
return buffer.length;
});

assert.equal(form.write(BUFFER), BUFFER.length);
assert.equal(form.bytesReceived, BUFFER.length);
})();

(function testParserError() {
gently.expect(form, 'emit');

gently.expect(parser, 'write', function(buffer) {
assert.strictEqual(buffer, BUFFER);
return buffer.length - 1;
Expand All @@ -285,10 +287,8 @@ test(function write() {
assert.ok(err.message.match(/parser error/i));
});

gently.expect(form, 'emit');

assert.equal(form.write(BUFFER), BUFFER.length - 1);
assert.equal(form.bytesReceived, BUFFER.length + BUFFER.length - 1);
assert.equal(form.bytesReceived, BUFFER.length + BUFFER.length);
})();

(function testUninitialized() {
Expand Down

0 comments on commit f22993a

Please sign in to comment.