Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

New 'progress' event

Also fixed some problems with the existing bytes properties.
  • Loading branch information...
commit 7efb618ceb66f98c87419b0650f5fb5ba6866d89 1 parent 4043e42
@felixge authored
View
6 Readme.md
@@ -85,7 +85,7 @@ Either 'multipart' or 'urlencoded' depending on the incoming request.
#### incomingForm.bytesReceived
-The amount of bytes received for this form so far. Useful for rolling your own upload progress bar.
+The amount of bytes received for this form so far.
#### incomingForm.bytesExpected
@@ -120,6 +120,10 @@ If you want to use formidable to only handle certain parts for you, you can do s
Check the code in this method for further inspiration.
+#### Event: 'progress' (bytesReceived, bytesExpected)
+
+Emitted after each incoming chunk of data that has been parsed. Can be used to roll your own progress bar.
+
#### Event: 'field' (name, value)
Emitted whenever a field / value pair has been received.
View
10 lib/formidable/incoming_form.js
@@ -15,7 +15,8 @@ function IncomingForm() {
this.encoding = 'utf-8';
this.headers = null;
this.type = null;
- this.bytesTotal = null;
+
+ this.bytesReceived = null;
this.bytesExpected = null;
this._parser = null;
@@ -83,7 +84,8 @@ IncomingForm.prototype.write = function(buffer) {
this._error(new Error('parser error, '+bytesParsed+' of '+buffer.length+' bytes parsed'));
}
- this.bytesExpected += bytesParsed;
+ this.bytesReceived += bytesParsed;
+ this.emit('progress', this.bytesReceived, this.bytesExpected);
return bytesParsed;
};
@@ -195,8 +197,8 @@ IncomingForm.prototype._error = function(err) {
IncomingForm.prototype._parseContentLength = function() {
if (this.headers['content-length']) {
- this.bytesExpected = 0;
- this.bytesTotal = parseInt(this.headers['content-length'], 10);
+ this.bytesReceived = 0;
+ this.bytesExpected = parseInt(this.headers['content-length'], 10);
}
};
View
23 test/simple/test-incoming-form.js
@@ -23,7 +23,7 @@ test(function constructor() {
assert.strictEqual(form.headers, null);
assert.strictEqual(form.uploadDir, '/tmp');
assert.strictEqual(form.encoding, 'utf-8');
- assert.strictEqual(form.bytesTotal, null);
+ assert.strictEqual(form.bytesReceived, null);
assert.strictEqual(form.bytesExpected, null);
assert.strictEqual(form._parser, null);
assert.strictEqual(form._flushing, 0);
@@ -156,6 +156,7 @@ test(function write() {
, BUFFER = [1, 2, 3];
form._parser = parser;
+ form.bytesExpected = 523423;
(function testBasic() {
gently.expect(parser, 'write', function(buffer) {
@@ -163,8 +164,14 @@ test(function write() {
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);
+ });
+
assert.equal(form.write(BUFFER), BUFFER.length);
- assert.equal(form.bytesExpected, BUFFER.length);
+ assert.equal(form.bytesReceived, BUFFER.length);
})();
(function testParserError() {
@@ -177,8 +184,10 @@ 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.bytesExpected, BUFFER.length + BUFFER.length - 1);
+ assert.equal(form.bytesReceived, BUFFER.length + BUFFER.length - 1);
})();
(function testUninitialized() {
@@ -244,17 +253,17 @@ test(function parseContentLength() {
form.headers = {};
form._parseContentLength();
- assert.equal(form.bytesTotal, null);
+ assert.strictEqual(form.bytesExpected, null);
form.headers['content-length'] = '8';
form._parseContentLength();
- assert.strictEqual(form.bytesExpected, 0);
- assert.strictEqual(form.bytesTotal, 8);
+ assert.strictEqual(form.bytesReceived, 0);
+ assert.strictEqual(form.bytesExpected, 8);
// JS can be evil, lets make sure we are not
form.headers['content-length'] = '08';
form._parseContentLength();
- assert.strictEqual(form.bytesTotal, 8);
+ assert.strictEqual(form.bytesExpected, 8);
});
test(function _initMultipart() {
Please sign in to comment.
Something went wrong with that request. Please try again.