Skip to content

Commit

Permalink
Merge pull request #189 from stephenplusplus/bufferstream
Browse files Browse the repository at this point in the history
storage: use passthrough stream for buffers.
  • Loading branch information
ryanseys committed Sep 7, 2014
2 parents 3e43236 + 0728b8a commit d527fb7
Showing 1 changed file with 5 additions and 27 deletions.
32 changes: 5 additions & 27 deletions lib/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

var crypto = require('crypto');
var duplexify = require('duplexify');
var nodeutil = require('util');
var stream = require('stream');
var uuid = require('node-uuid');

Expand Down Expand Up @@ -57,31 +56,6 @@ var STORAGE_BASE_URL = 'https://www.googleapis.com/storage/v1/b';
*/
var STORAGE_UPLOAD_BASE_URL = 'https://www.googleapis.com/upload/storage/v1/b';

/**
* Readable stream implementation to stream the given buffer.
*
* @constructor
*
* @param {buffer} buffer - The buffer to stream.
*
* @private
*/
function BufferStream(buffer) {
stream.Readable.call(this);
this.data = buffer;
}

nodeutil.inherits(BufferStream, stream.Readable);

/**
* Push the provided buffer to the stream.
* @private
*/
BufferStream.prototype._read = function() {
this.push(this.data);
this.push(null);
};

/**
* Google Cloud Storage allows you to store data on Google infrastructure. See
* the guide on {@link https://developers.google.com/storage} to create a
Expand Down Expand Up @@ -388,6 +362,7 @@ Bucket.prototype.createWriteStream = function(name, metadata) {
*/
Bucket.prototype.write = function(name, options, callback) {
callback = callback || util.noop;
var bufferStream;
var data = typeof options === 'object' ? options.data : options;
var metadata = options.metadata || {};

Expand All @@ -398,9 +373,12 @@ Bucket.prototype.write = function(name, options, callback) {
}

if (typeof data === 'string' || data instanceof Buffer) {
new BufferStream(data).pipe(this.createWriteStream(name, metadata))
bufferStream = new stream.PassThrough();
bufferStream.pipe(this.createWriteStream(name, metadata))
.on('error', callback)
.on('complete', callback.bind(null, null));
bufferStream.push(data);
bufferStream.push(null);
}
};

Expand Down

0 comments on commit d527fb7

Please sign in to comment.