Skip to content

Commit

Permalink
feat: adds support for asyncIterators (via readable-stream@3 dependency)
Browse files Browse the repository at this point in the history
  • Loading branch information
AVaksman authored and bcoe committed Sep 6, 2019
1 parent 5f69a3d commit dd5ae7f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -68,6 +68,7 @@
"onetime": "^5.1.0",
"p-limit": "^2.2.0",
"pumpify": "^2.0.0",
"readable-stream": "^3.4.0",
"snakeize": "^0.1.0",
"stream-events": "^1.0.1",
"through2": "^3.0.0",
Expand Down
3 changes: 3 additions & 0 deletions src/file.ts
Expand Up @@ -1190,6 +1190,9 @@ class File extends ServiceObject<File> {

// tslint:disable-next-line:no-any
let validateStream: any; // Created later, if necessary.

// TODO: remove `through2` dependency in favor of native PassThrough
// once Node 8 support is discontinued
const throughStream = streamEvents(through()) as Duplex;

let crc32c = true;
Expand Down
13 changes: 13 additions & 0 deletions system-test/storage.ts
Expand Up @@ -2061,6 +2061,19 @@ describe('storage', () => {
});
});

it('should support readable[Symbol.asyncIterator]()', async () => {
const fileContents = fs.readFileSync(FILES.big.path);

const [file] = await bucket.upload(FILES.big.path);
const stream = file.createReadStream();
const chunks: Buffer[] = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
const remoteContents = Buffer.concat(chunks).toString();
assert.strictEqual(String(fileContents), String(remoteContents));
});

it('should download a file to memory', done => {
const fileContents = fs.readFileSync(FILES.big.path);
bucket.upload(FILES.big.path, (err: Error | null, file?: File | null) => {
Expand Down

0 comments on commit dd5ae7f

Please sign in to comment.