Stream files directly from GCS instead of buffering and sending.

This commit is contained in:
Eric Peterson
2021-01-15 18:02:37 +01:00
parent 9420aef1ae
commit 8d950d0ae7
@@ -169,30 +169,23 @@ export class GoogleGCSPublish implements PublisherBase {
// Files with different extensions (CSS, HTML) need to be served with different headers
const fileExtension = path.extname(filePath);
const responseHeaders = getHeadersForFileExtension(fileExtension);
res.writeHead(200, {
...{
'Transfer-Encoding': 'chunked',
},
...responseHeaders,
});
const fileStreamChunks: Array<any> = [];
// Pipe file chunks directly from storage to client.
this.storageClient
.bucket(this.bucketName)
.file(filePath)
.createReadStream()
.on('error', err => {
this.logger.warn(err.message);
res.status(404).send(err.message);
res.destroy(err);
})
.on('data', chunk => {
fileStreamChunks.push(chunk);
})
.on('end', () => {
const fileContent = Buffer.concat(fileStreamChunks);
// Inject response headers
for (const [headerKey, headerValue] of Object.entries(
responseHeaders,
)) {
res.setHeader(headerKey, headerValue);
}
res.send(fileContent);
});
.pipe(res);
};
}