From ba3eda7e783d4d80504f003d31a90739826de50b Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Mon, 1 Oct 2018 16:13:52 +0200 Subject: [PATCH] fix: improve resiliency of internals _send --- package.json | 8 ++++---- src/internals/index.js | 13 ++++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 4ec17bf..1ad7525 100644 --- a/package.json +++ b/package.json @@ -30,10 +30,10 @@ "homepage": "https://github.com/libp2p/js-libp2p-mplex#readme", "devDependencies": { "aegir": "^15.2.0", - "chai": "^4.1.2", + "chai": "^4.2.0", "dirty-chai": "^2.0.1", "interface-stream-muxer": "~0.5.9", - "libp2p-tcp": "~0.12.0", + "libp2p-tcp": "~0.13.0", "libp2p-websockets": "~0.12.0", "pull-pair": "^1.1.0" }, @@ -45,10 +45,10 @@ "duplexify": "^3.6.0", "interface-connection": "~0.3.2", "pull-catch": "^1.0.0", - "pull-stream": "^3.6.8", + "pull-stream": "^3.6.9", "pull-stream-to-stream": "^1.3.4", "pump": "^3.0.0", - "readable-stream": "^2.3.6", + "readable-stream": "^3.0.3", "stream-to-pull-stream": "^1.7.2", "through2": "^2.0.3", "varint": "^5.0.0" diff --git a/src/internals/index.js b/src/internals/index.js index 1d4ac0b..b1806d7 100644 --- a/src/internals/index.js +++ b/src/internals/index.js @@ -148,7 +148,6 @@ class Multiplex extends stream.Duplex { _send (header/* : number */, data /* :: ?: Buffer */)/* : bool */ { const len = data ? data.length : 0 const oldUsed = used - let drained = true this.log('_send', header, len) @@ -157,18 +156,22 @@ class Multiplex extends stream.Duplex { varint.encode(len, pool, used) used += varint.encode.bytes - drained = this.push(pool.slice(oldUsed, used)) + let buf = pool.slice(oldUsed, used) if (pool.length - used < 100) { pool = Buffer.alloc(10 * 1024) used = 0 } - if (data && drained) { - drained = this.push(data) + if (data) { + buf = Buffer.concat([ + buf, + data + ]) } - return drained + // Push and return the results + return this.push(buf) } _addChannel (channel/* : Channel */, id/* : number */, list/* : Array */)/* : Channel */ {