mirror of
https://gitlab.com/timvisee/send.git
synced 2025-12-06 14:10:53 +03:00
implemented websocket flow control to prevent over buffering
This commit is contained in:
@@ -3,10 +3,11 @@ 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 fxa = require('../fxa');
|
||||
|
||||
const { Duplex } = require('stream');
|
||||
|
||||
const log = mozlog('send.upload');
|
||||
|
||||
module.exports = function(ws, req) {
|
||||
@@ -72,12 +73,27 @@ module.exports = function(ws, req) {
|
||||
id: newId
|
||||
})
|
||||
);
|
||||
|
||||
const limiter = new Limiter(maxFileSize);
|
||||
const parser = new Parser();
|
||||
const flowControl = new Duplex({
|
||||
read() {
|
||||
ws.resume();
|
||||
},
|
||||
write(chunk, encoding, callback) {
|
||||
if (chunk.length === 1 && chunk[0] === 0) {
|
||||
this.push(null);
|
||||
} else {
|
||||
if (!this.push(chunk)) {
|
||||
ws.pause();
|
||||
}
|
||||
}
|
||||
callback();
|
||||
}
|
||||
});
|
||||
|
||||
fileStream = wsStream(ws, { binary: true })
|
||||
.pipe(limiter)
|
||||
.pipe(parser);
|
||||
.pipe(flowControl);
|
||||
|
||||
await storage.set(newId, fileStream, meta, timeLimit);
|
||||
|
||||
if (ws.readyState === 1) {
|
||||
|
||||
Reference in New Issue
Block a user