Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: improve resiliency of internals _send (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun authored and vasco-santos committed Oct 1, 2018
1 parent c25a6dc commit 70dafb7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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"
Expand Down
13 changes: 8 additions & 5 deletions src/internals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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|null> */)/* : Channel */ {
Expand Down

0 comments on commit 70dafb7

Please sign in to comment.