formstream
Advanced tools
Comparing version 0.0.3 to 0.0.4
0.0.4 / 2012-11-06 | ||
================== | ||
* fixed #2 support form.buffer() | ||
* add doc for setTotalStreamSize() | ||
0.0.3 / 2012-11-06 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -43,2 +43,3 @@ /*! | ||
var NEW_LINE = '\r\n'; | ||
var NEW_LINE_BUFFER = new Buffer(NEW_LINE); | ||
@@ -54,2 +55,3 @@ function FormStream() { | ||
this._fields = []; | ||
this._buffers = []; | ||
this._endData = new Buffer(PADDING + this._boundary + PADDING + NEW_LINE); | ||
@@ -90,4 +92,12 @@ } | ||
size += item[0].length; | ||
size += NEW_LINE.length; // stream field end pedding size | ||
size += NEW_LINE_BUFFER.length; // stream field end pedding size | ||
} | ||
// plus buffers size | ||
for (var i = 0; i < this._buffers.length; i++) { | ||
var item = this._buffers[i]; | ||
size += item[0].length; | ||
size += item[1].length; | ||
size += NEW_LINE_BUFFER.length; | ||
} | ||
@@ -128,6 +138,3 @@ // end padding data size | ||
FormStream.prototype.stream = function (name, stream, filename, mimeType) { | ||
if (!mimeType) { | ||
// guesss from filename | ||
mimeType = mime.lookup(filename); | ||
} | ||
mimeType = mimeType || mime.lookup(filename); | ||
var ps = parseStream().pause(); | ||
@@ -142,2 +149,11 @@ stream.pipe(ps); | ||
FormStream.prototype.buffer = function (name, buffer, filename, mimeType) { | ||
mimeType = mimeType || mime.lookup(filename); | ||
this._buffers.push([ | ||
this._formatStreamField(name, filename, mimeType), | ||
buffer | ||
]); | ||
process.nextTick(this.resume.bind(this)); | ||
}; | ||
FormStream.prototype._emitEnd = function () { | ||
@@ -178,2 +194,15 @@ // ending format: | ||
FormStream.prototype._emitBuffers = function () { | ||
if (!this._buffers.length) { | ||
return; | ||
} | ||
for (var i = 0; i < this._buffers.length; i++) { | ||
var item = this._buffers[i]; | ||
this.emit('data', item[0]); | ||
this.emit('data', item[1]); | ||
this.emit('data', NEW_LINE_BUFFER); | ||
} | ||
this._buffers = []; | ||
}; | ||
FormStream.prototype._formatStreamField = function (name, filename, mimeType) { | ||
@@ -197,3 +226,3 @@ var data = PADDING + this._boundary + NEW_LINE; | ||
stream.on('end', function () { | ||
self.emit('data', NEW_LINE); | ||
self.emit('data', NEW_LINE_BUFFER); | ||
return process.nextTick(self.drain.bind(self)); | ||
@@ -206,2 +235,3 @@ }); | ||
this._emitFields(); | ||
this._emitBuffers(); | ||
var item = this._streams.shift(); | ||
@@ -208,0 +238,0 @@ if (item) { |
{ | ||
"name": "formstream", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A multipart/form-data encoded stream, helper for file upload.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -23,6 +23,14 @@ formstream [![Build Status](https://secure.travis-ci.org/fengmk2/formstream.png)](http://travis-ci.org/fengmk2/formstream) | ||
var form = formstream(); | ||
// form.file('file', filepath, filename); | ||
form.file('file', './logo.png', 'upload-logo.png'); | ||
form.field('foo', 'bar'); | ||
// other form fields | ||
form.field('foo', 'fengmk2'); | ||
form.field('love', 'aerdeng'); | ||
// even send file content buffer directly | ||
// form.buffer(name, buffer, filename, mimeType) | ||
form.buffer('file2', new Buffer('This is file2 content.'), 'foo.txt'); | ||
var options = { | ||
@@ -44,2 +52,32 @@ method: 'POST', | ||
### `form.setTotalStreamSize(size)`: Upload file with `Content-Length` | ||
If you know the `ReadStream` total size and you must to set `Content-Length`. | ||
You may want to use `form.setTotalStreamSize(size)`. | ||
```js | ||
var formstream = require('formstream'); | ||
var http = require('http'); | ||
var fs = require('fs'); | ||
fs.stat('./logo.png', function (err, stat) { | ||
var form = formstream(); | ||
form.file('file', './logo.png', 'upload-logo.png'); | ||
form.setTotalStreamSize(stat.size); | ||
var options = { | ||
method: 'POST', | ||
host: 'upload.cnodejs.net', | ||
path: '/store', | ||
headers: form.headers() | ||
}; | ||
var req = http.request(options, function (res) { | ||
console.log('Status: %s', res.statusCode); | ||
res.on('data', function (data) { | ||
console.log(data.toString()); | ||
}); | ||
}); | ||
form.pipe(req); | ||
}); | ||
``` | ||
## License | ||
@@ -46,0 +84,0 @@ |
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
11845
217
104