The upstream gcp aggressively closes the connection once it has
received Content-Length bytes. However the @google-cloud/storage
module doesn't handle this well and emits no event in this case.
We were setting Content-Length because it's slightly more
efficient and was important for our download progress
bar (not anymore). The download should function fine without
setting the Content-Length, and allows the storage stream to finish
before closing the upstream socket.
This commit is contained in:
Danny Coates 2018-11-14 09:50:12 -08:00
parent e264d0da62
commit 6184a70ba4
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
4 changed files with 7 additions and 16 deletions

View File

@ -162,8 +162,8 @@ function download(id, keychain, onprogress, canceller) {
}); });
xhr.addEventListener('progress', function(event) { xhr.addEventListener('progress', function(event) {
if (event.lengthComputable && event.target.status === 200) { if (event.target.status === 200) {
onprogress([event.loaded, event.total]); onprogress(event.loaded);
} }
}); });
const auth = await keychain.authHeader(); const auth = await keychain.authHeader();
@ -171,7 +171,7 @@ function download(id, keychain, onprogress, canceller) {
xhr.setRequestHeader('Authorization', auth); xhr.setRequestHeader('Authorization', auth);
xhr.responseType = 'blob'; xhr.responseType = 'blob';
xhr.send(); xhr.send();
onprogress([0, 1]); onprogress(0);
}); });
} }

View File

@ -47,7 +47,7 @@ export default class FileReceiver extends Nanobus {
this.fileInfo.name = meta.name; this.fileInfo.name = meta.name;
this.fileInfo.type = meta.type; this.fileInfo.type = meta.type;
this.fileInfo.iv = meta.iv; this.fileInfo.iv = meta.iv;
this.fileInfo.size = meta.size; this.fileInfo.size = +meta.size;
this.state = 'ready'; this.state = 'ready';
} }
@ -57,7 +57,7 @@ export default class FileReceiver extends Nanobus {
this.fileInfo.id, this.fileInfo.id,
this.keychain, this.keychain,
p => { p => {
this.progress = p; this.progress = [p, this.fileInfo.size];
this.emit('progress'); this.emit('progress');
} }
); );

View File

@ -6,13 +6,6 @@ module.exports = async function(req, res) {
const id = req.params.id; const id = req.params.id;
try { try {
const meta = req.meta; const meta = req.meta;
const contentLength = await storage.length(id);
res.writeHead(200, {
'Content-Disposition': 'attachment',
'Content-Type': 'application/octet-stream',
'Content-Length': contentLength,
'WWW-Authenticate': `send-v1 ${req.nonce}`
});
const file_stream = storage.get(id); const file_stream = storage.get(id);
let cancelled = false; let cancelled = false;
@ -21,7 +14,7 @@ module.exports = async function(req, res) {
file_stream.destroy(); file_stream.destroy();
}); });
file_stream.on('end', async () => { file_stream.pipe(res).on('finish', async () => {
if (cancelled) { if (cancelled) {
return; return;
} }
@ -38,8 +31,6 @@ module.exports = async function(req, res) {
log.info('StorageError:', id); log.info('StorageError:', id);
} }
}); });
file_stream.pipe(res);
} catch (e) { } catch (e) {
res.sendStatus(404); res.sendStatus(404);
} }

View File

@ -13,7 +13,7 @@ class GCSStorage {
} }
getStream(id) { getStream(id) {
return this.bucket.file(id).createReadStream(); return this.bucket.file(id).createReadStream({ validation: false });
} }
set(id, file) { set(id, file) {