-
Notifications
You must be signed in to change notification settings - Fork 639
Closed
Labels
Description
Our support for readable streams in our library is now almost everywhere possible, which is awesome. But, the way we use them is not in the most efficient way.
How we do it, using bucket.getFiles for example:
- Make API request to server to get all of the files from a bucket
- Get a large JSON response with all of the files' metadata
- Split the array into transformed "File" objects
- Push all of the objects onto a readable stream that the user is reading from
- If there's a "nextPageToken", repeat step 1.
How we could do it:
- Make streaming API request to server to get all of the files from a bucket
- Parse the JSON response as it comes in in chunks
- Transform the returned files into "File" objects
How we do it currently (simplified):
bucket.getFiles = function() {
var stream = through.obj();
request.get('https://.../bucket/files', function(err, resp) {
// A potentially huge JSON response exists in memory now
resp.items.forEach(function(fileMetadata) {
var file = new File(fileMetadata.name);
file.metadata = fileMetadata;
stream.push(file);
});
stream.push(null);
});
return stream;
};How it would look (simplified):
bucket.getFiles = function() {
return request.get('https://...bucket/files')
.pipe(JSONStream.parse('items.*')) // link to JSONStream below
.pipe(through.obj(toFileObject));
function toFileObject(obj, enc, next) {
var file = new File(obj.name);
file.metadata = obj;
this.push(file);
next();
}
};With the second example, the object is only in memory until it is flushed to the next handler in the user's own pipeline. At that point, the user is free to let it buffer up until they're all in or write them to a destination in chunks, not letting any extra memory pile up.
* JSONStream: https://github.com/dominictarr/JSONStream