stream footer

This commit is contained in:
Emily Hou 2018-06-25 11:26:48 -07:00
parent a4cf46c0eb
commit 9d04514f8e
3 changed files with 36 additions and 8 deletions

View file

@ -3,6 +3,7 @@ const storage = require('../storage');
const config = require('../config');
const mozlog = require('../log');
const Limiter = require('../limiter');
const Parser = require('../streamparser');
const wsStream = require('websocket-stream/stream');
const log = mozlog('send.upload');
@ -45,7 +46,10 @@ module.exports = async function(ws, req) {
const url = `${protocol}://${req.get('host')}/download/${newId}/`;
const limiter = new Limiter(config.max_file_size);
fileStream = wsStream(ws, { binary: true }).pipe(limiter);
const parser = new Parser();
fileStream = wsStream(ws, { binary: true })
.pipe(limiter)
.pipe(parser);
storage.set(newId, fileStream, meta);
ws.send(
@ -60,7 +64,7 @@ module.exports = async function(ws, req) {
log.error('upload', e);
ws.send(
JSON.stringify({
error: 500
error: e === 'limit' ? 413 : 500
})
);
ws.close();

23
server/streamparser.js Normal file
View file

@ -0,0 +1,23 @@
const { Transform } = require('stream');
class StreamParser extends Transform {
constructor() {
super();
let res;
this.promise = new Promise(resolve => {
res = resolve;
});
this.res = res;
}
_transform(chunk, encoding, callback) {
if (chunk.byteLength === 1 && chunk[0] === 0) {
this.res();
} else {
this.push(chunk);
}
callback();
}
}
module.exports = StreamParser;