use a Duplex stream for EOF
This commit is contained in:
parent
beccd80902
commit
126ea8c7e6
|
@ -52,7 +52,7 @@ export default function(state, emitter) {
|
||||||
checkFiles();
|
checkFiles();
|
||||||
});
|
});
|
||||||
|
|
||||||
//emitter.on('navigate', checkFiles);
|
emitter.on('navigate', checkFiles);
|
||||||
|
|
||||||
emitter.on('render', () => {
|
emitter.on('render', () => {
|
||||||
lastRender = Date.now();
|
lastRender = Date.now();
|
||||||
|
|
|
@ -50,10 +50,16 @@ module.exports = async function(ws, req) {
|
||||||
fileStream = wsStream(ws, { binary: true })
|
fileStream = wsStream(ws, { binary: true })
|
||||||
.pipe(limiter)
|
.pipe(limiter)
|
||||||
.pipe(parser);
|
.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.
|
||||||
|
|
||||||
|
// 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(
|
ws.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
url,
|
url,
|
||||||
|
@ -62,8 +68,10 @@ module.exports = async function(ws, req) {
|
||||||
authentication: `send-v1 ${meta.nonce}`
|
authentication: `send-v1 ${meta.nonce}`
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.error('upload', e);
|
log.error('upload', e);
|
||||||
|
if (ws.readyState === 1) {
|
||||||
ws.send(
|
ws.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
error: e === 'limit' ? 413 : 500
|
error: e === 'limit' ? 413 : 500
|
||||||
|
@ -71,5 +79,6 @@ module.exports = async function(ws, req) {
|
||||||
);
|
);
|
||||||
ws.close();
|
ws.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,23 +1,15 @@
|
||||||
const { Transform } = require('stream');
|
const { Duplex } = require('stream');
|
||||||
|
|
||||||
class StreamParser extends Transform {
|
class StreamParser extends Duplex {
|
||||||
constructor() {
|
_write(chunk, encoding, callback) {
|
||||||
super();
|
|
||||||
let res;
|
|
||||||
this.promise = new Promise(resolve => {
|
|
||||||
res = resolve;
|
|
||||||
});
|
|
||||||
this.res = res;
|
|
||||||
}
|
|
||||||
|
|
||||||
_transform(chunk, encoding, callback) {
|
|
||||||
if (chunk.byteLength === 1 && chunk[0] === 0) {
|
if (chunk.byteLength === 1 && chunk[0] === 0) {
|
||||||
this.res();
|
this.push(null);
|
||||||
} else {
|
} else {
|
||||||
this.push(chunk);
|
this.push(chunk);
|
||||||
}
|
}
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
|
_read() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = StreamParser;
|
module.exports = StreamParser;
|
||||||
|
|
Loading…
Reference in New Issue