diff --git a/package.json b/package.json index 9927a47..8bcdf77 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "homepage": "https://github.com/libp2p/js-libp2p-mplex#readme", "devDependencies": { "aegir": "^27.0.0", - "libp2p-interfaces": "^0.6.0", + "libp2p-interfaces": "libp2p/js-libp2p-interfaces#feat/streams", "p-defer": "^3.0.0", "random-bytes": "^1.0.0", "random-int": "^2.0.0", @@ -55,6 +55,7 @@ "abortable-iterator": "^3.0.0", "bl": "^4.0.0", "debug": "^4.2.0", + "err-code": "^2.0.3", "it-pipe": "^1.1.0", "it-pushable": "^1.4.0", "varint": "^5.0.0" diff --git a/src/mplex.js b/src/mplex.js index 22c7320..dc440e7 100644 --- a/src/mplex.js +++ b/src/mplex.js @@ -4,6 +4,7 @@ const pipe = require('it-pipe') const pushable = require('it-pushable') const log = require('debug')('libp2p:mplex') const abortable = require('abortable-iterator') +const errCode = require('err-code') const Coder = require('./coder') const restrictSize = require('./restrict-size') const { MessageTypes, MessageTypeNames } = require('./message-types') @@ -117,6 +118,9 @@ class Mplex { } log('new %s stream %s %s', type, id, name) const send = msg => { + if (!registry.has(id)) { + throw errCode(new Error('the stream is not in the muxer registry, it may have already been closed'), 'ERR_STREAM_DOESNT_EXIST') + } if (log.enabled) { log('%s stream %s %s send', type, id, name, { ...msg, type: MessageTypeNames[msg.type], data: msg.data && msg.data.slice() }) }