formstream
Advanced tools
Comparing version 0.0.2 to 0.0.3
0.0.3 / 2012-11-06 | ||
================== | ||
* support content-length use form.setTotalStreamSize(size) | ||
* update readme | ||
0.0.2 / 2012-10-11 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -53,4 +53,5 @@ /*! | ||
this._fields = []; | ||
this._endData = new Buffer(PADDING + this._boundary + PADDING + NEW_LINE); | ||
} | ||
} | ||
util.inherits(FormStream, Stream); | ||
@@ -71,2 +72,27 @@ module.exports = FormStream; | ||
/** | ||
* Set total stream size. | ||
* | ||
* You know total stream data size and you want to set `Content-Length` in headers. | ||
*/ | ||
FormStream.prototype.setTotalStreamSize = function (size) { | ||
size = size || 0; | ||
// plus fileds data size | ||
this._formatFields(); | ||
if (this._fieldsData) { | ||
size += this._fieldsData.length; | ||
} | ||
// plus stream field data size | ||
for (var i = 0; i < this._streams.length; i++) { | ||
var item = this._streams[i]; | ||
size += item[0].length; | ||
size += NEW_LINE.length; // stream field end pedding size | ||
} | ||
// end padding data size | ||
size += this._endData.length; | ||
this._contentLength = size; | ||
}; | ||
FormStream.prototype.headers = function (options) { | ||
@@ -76,2 +102,5 @@ var headers = { | ||
}; | ||
if (typeof this._contentLength === 'number') { | ||
headers['Content-Length'] = String(this._contentLength); | ||
} | ||
if (options) { | ||
@@ -105,3 +134,6 @@ for (var k in options) { | ||
stream.pipe(ps); | ||
this._streams.push([ name, filename, mimeType, ps ]); | ||
this._streams.push([ | ||
this._formatStreamField(name, filename, mimeType), | ||
ps | ||
]); | ||
process.nextTick(this.resume.bind(this)); | ||
@@ -114,11 +146,11 @@ }; | ||
// --{boundary}--\r\n | ||
var endData = PADDING + this._boundary + PADDING + NEW_LINE; | ||
this.emit('data', endData); | ||
this.emit('data', this._endData); | ||
this.emit('end'); | ||
}; | ||
FormStream.prototype._emitFields = function () { | ||
if (this._fields.length === 0) { | ||
FormStream.prototype._formatFields = function () { | ||
if (!this._fields.length) { | ||
return; | ||
} | ||
var lines = ''; | ||
@@ -133,15 +165,29 @@ for (var i = 0; i < this._fields.length; i++) { | ||
} | ||
this._fieldsData = new Buffer(lines); | ||
this._fields = []; | ||
this.emit('data', lines); | ||
}; | ||
FormStream.prototype._emitStream = function (item) { | ||
var self = this; | ||
FormStream.prototype._emitFields = function () { | ||
this._formatFields(); | ||
if (this._fieldsData) { | ||
var data = this._fieldsData; | ||
this._fieldsData = null; | ||
this.emit('data', data); | ||
} | ||
}; | ||
FormStream.prototype._formatStreamField = function (name, filename, mimeType) { | ||
var data = PADDING + this._boundary + NEW_LINE; | ||
data += 'Content-Disposition: form-data; name="' + item[0] +'"; filename="' + item[1] + '"' + NEW_LINE; | ||
data += 'Content-Type: ' + item[2] + NEW_LINE; | ||
data += 'Content-Disposition: form-data; name="' + name +'"; filename="' + filename + '"' + NEW_LINE; | ||
data += 'Content-Type: ' + mimeType + NEW_LINE; | ||
data += NEW_LINE; | ||
self.emit('data', data); | ||
return new Buffer(data); | ||
}; | ||
var stream = item[3]; | ||
FormStream.prototype._emitStream = function (item) { | ||
var self = this; | ||
// item: [ fieldData, stream ] | ||
self.emit('data', item[0]); | ||
var stream = item[1]; | ||
stream.on('data', function (data) { | ||
@@ -148,0 +194,0 @@ self.emit('data', data); |
{ | ||
"name": "formstream", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A multipart/form-data encoded stream, helper for file upload.", | ||
@@ -26,7 +26,2 @@ "main": "index.js", | ||
], | ||
"devDependencies": { | ||
"connect": "2.6.0", | ||
"should": "*", | ||
"mocha": "*" | ||
}, | ||
"dependencies": { | ||
@@ -37,4 +32,11 @@ "mime": "1.2.7", | ||
}, | ||
"devDependencies": { | ||
"connect": "*", | ||
"should": "*", | ||
"pedding": "*", | ||
"jscover": "*", | ||
"mocha": "*" | ||
}, | ||
"author": "fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)", | ||
"license": "MIT" | ||
} |
@@ -23,3 +23,5 @@ formstream [![Build Status](https://secure.travis-ci.org/fengmk2/formstream.png)](http://travis-ci.org/fengmk2/formstream) | ||
var form = formstream(); | ||
form.file('file', './logo.png'); | ||
// form.file('file', filepath, filename); | ||
form.file('file', './logo.png', 'upload-logo.png'); | ||
form.field('foo', 'bar'); | ||
@@ -26,0 +28,0 @@ var options = { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
9861
190
66
0
5