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

fix: improve resiliency of internals _send #84

Merged
merged 1 commit into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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