Skip to content

Commit 0728b8a

Browse files
storage: use passthrough stream for buffers.
1 parent b2721e0 commit 0728b8a

File tree

1 file changed

+5
-27
lines changed

1 file changed

+5
-27
lines changed

lib/storage/index.js

+5-27
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
var crypto = require('crypto');
2424
var duplexify = require('duplexify');
25-
var nodeutil = require('util');
2625
var stream = require('stream');
2726
var uuid = require('node-uuid');
2827

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

60-
/**
61-
* Readable stream implementation to stream the given buffer.
62-
*
63-
* @constructor
64-
*
65-
* @param {buffer} buffer - The buffer to stream.
66-
*
67-
* @private
68-
*/
69-
function BufferStream(buffer) {
70-
stream.Readable.call(this);
71-
this.data = buffer;
72-
}
73-
74-
nodeutil.inherits(BufferStream, stream.Readable);
75-
76-
/**
77-
* Push the provided buffer to the stream.
78-
* @private
79-
*/
80-
BufferStream.prototype._read = function() {
81-
this.push(this.data);
82-
this.push(null);
83-
};
84-
8559
/**
8660
* Google Cloud Storage allows you to store data on Google infrastructure. See
8761
* the guide on {@link https://developers.google.com/storage} to create a
@@ -388,6 +362,7 @@ Bucket.prototype.createWriteStream = function(name, metadata) {
388362
*/
389363
Bucket.prototype.write = function(name, options, callback) {
390364
callback = callback || util.noop;
365+
var bufferStream;
391366
var data = typeof options === 'object' ? options.data : options;
392367
var metadata = options.metadata || {};
393368

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

400375
if (typeof data === 'string' || data instanceof Buffer) {
401-
new BufferStream(data).pipe(this.createWriteStream(name, metadata))
376+
bufferStream = new stream.PassThrough();
377+
bufferStream.pipe(this.createWriteStream(name, metadata))
402378
.on('error', callback)
403379
.on('complete', callback.bind(null, null));
380+
bufferStream.push(data);
381+
bufferStream.push(null);
404382
}
405383
};
406384

0 commit comments

Comments
 (0)