Skip to content

Commit

Permalink
fix: truncated createReadStream through early end event (#2056)
Browse files Browse the repository at this point in the history
* test: read entire file when range is `{start: 0}`

* fix: `ReturnType<F>` regression

* fix: Remove premature `.end()` calls
  • Loading branch information
d-goog authored Sep 1, 2022
1 parent c90ba0f commit a4716a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
15 changes: 5 additions & 10 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1417,9 +1417,7 @@ class File extends ServiceObject<File> {
// Authenticate the request, then pipe the remote API request to the stream
// returned to the user.
const makeRequest = () => {
const query = {
alt: 'media',
} as FileQuery;
const query: FileQuery = {alt: 'media'};

if (this.generation) {
query.generation = this.generation;
Expand Down Expand Up @@ -1460,8 +1458,7 @@ class File extends ServiceObject<File> {
})
.on('response', res => {
throughStream.emit('response', res);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
util.handleResp(null, res, null, onResponse as any);
util.handleResp(null, res, null, onResponse);
})
.resume();

Expand Down Expand Up @@ -1525,7 +1522,7 @@ class File extends ServiceObject<File> {
const handoffStream = new PassThrough({
final: async cb => {
// Preserving `onComplete`'s ability to
// close `throughStream` before pipeline
// destroy `throughStream` before pipeline
// attempts to.
await onComplete(null);
cb();
Expand Down Expand Up @@ -1558,7 +1555,6 @@ class File extends ServiceObject<File> {
}

if (rangeRequest || !shouldRunValidation) {
throughStream.end();
return;
}

Expand All @@ -1576,7 +1572,6 @@ class File extends ServiceObject<File> {
return;
}
if (this.metadata.contentEncoding === 'gzip') {
throughStream.end();
return;
}
}
Expand Down Expand Up @@ -1611,14 +1606,14 @@ class File extends ServiceObject<File> {

throughStream.destroy(mismatchError);
} else {
throughStream.end();
return;
}
};
};

throughStream.on('reading', makeRequest);

return throughStream as Readable;
return throughStream;
}

createResumableUpload(
Expand Down
10 changes: 9 additions & 1 deletion system-test/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,7 @@ describe('storage', () => {

// Validate the desired functionality
const results = await testFunction(USER_PROJECT_OPTIONS);
return results;
return results as ReturnType<F>;
}

it('bucket#combine', async () => {
Expand Down Expand Up @@ -2119,6 +2119,14 @@ describe('storage', () => {
assert.strictEqual(String(fileContents), String(remoteContents));
});

it('should download an entire file if range `start:0` is provided', async () => {
const fileContents = fs.readFileSync(FILES.big.path);
const [file] = await bucket.upload(FILES.big.path);
const [result] = await file.download({start: 0});

assert.strictEqual(result.toString(), fileContents.toString());
});

it('should download an empty file', async () => {
const fileContents = fs.readFileSync(FILES.empty.path);
const [file] = await bucket.upload(FILES.empty.path);
Expand Down

0 comments on commit a4716a4

Please sign in to comment.