use a Duplex stream for EOF

This commit is contained in:
Danny Coates 2018-06-25 14:01:08 -07:00
parent beccd80902
commit 126ea8c7e6
No known key found for this signature in database
GPG key ID: 4C442633C62E00CB
3 changed files with 31 additions and 30 deletions

View file

@ -50,26 +50,35 @@ module.exports = async function(ws, req) {
fileStream = wsStream(ws, { binary: true })
.pipe(limiter)
.pipe(parser);
storage.set(newId, fileStream, meta);
await storage.set(newId, fileStream, meta);
await parser.promise;
if (ws.readyState === 1) {
// if the socket is closed by a cancelled upload the stream
// ends without an error so we need to check the state
// before sending a reply.
ws.send(
JSON.stringify({
url,
owner: meta.owner,
id: newId,
authentication: `send-v1 ${meta.nonce}`
})
);
// TODO: we should handle cancelled uploads differently
// in order to avoid having to check socket state and clean
// up storage, possibly with an exception that we can catch.
ws.send(
JSON.stringify({
url,
owner: meta.owner,
id: newId,
authentication: `send-v1 ${meta.nonce}`
})
);
}
} catch (e) {
log.error('upload', e);
ws.send(
JSON.stringify({
error: e === 'limit' ? 413 : 500
})
);
ws.close();
if (ws.readyState === 1) {
ws.send(
JSON.stringify({
error: e === 'limit' ? 413 : 500
})
);
ws.close();
}
}
});
};