Skip to content

Commit

Permalink
Merge branch 'streaming'
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Sep 20, 2010
2 parents 2bdd87f + 37f57bc commit 08174dd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ Each HTTP verb has an alternate method with the "File" suffix, for example `put(
// Logic
});

Another alternative is to stream via `Client#putStream()`, for example:

var stream = fs.createReadStream('data.json');
client.putStream(stream, '/some-data.json', function(err, res){
// Logic
});

### GET

Below is an example __GET__ request on the file we just shoved at s3, and simply outputs the response status code, headers, and body.
Expand Down
33 changes: 33 additions & 0 deletions lib/knox/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,39 @@ Client.prototype.putFile = function(src, filename, headers, fn){
});
};

/**
* PUT the given `stream` as `filename` with optional `headers`.
*
* @param {Stream} stream
* @param {String} filename
* @param {Object|Function} headers
* @param {Function} fn
* @api public
*/

Client.prototype.putStream = function(stream, filename, headers, fn){
var self = this;
if ('function' == typeof headers) {
fn = headers;
headers = {};
};
fs.stat(stream.path, function(err, stat){
if (err) return fn(err);
// TODO: sys.pump() wtf?
var req = self.put(filename, utils.merge({
'Content-Length': stat.size
, 'Content-Type': mime.lookup(stream.path)
}, headers));
req.on('response', function(res){
fn(null, res);
});
stream
.on('error', function(err){fn(null, err); })
.on('data', function(chunk){ req.write(chunk); })
.on('end', function(){ req.end(); });
});
};

/**
* GET `filename` with optional `headers`.
*
Expand Down
9 changes: 9 additions & 0 deletions test/ns3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ module.exports = {
});
},

'test .putStream()': function(assert, done){
var stream = fs.createReadStream(jsonFixture);
client.putStream(stream, '/test/user.json', function(err, res){
assert.ok(!err);
if (100 !== res.statusCode) assert.equal(200, res.statusCode);
done();
});
},

'test .getFile()': function(assert, done){
client.getFile('/test/user.json', function(err, res){
assert.ok(!err);
Expand Down

0 comments on commit 08174dd

Please sign in to comment.