hypercore-protocol-substream
Advanced tools
Comparing version 2.0.3 to 2.0.5
55
index.js
@@ -21,2 +21,3 @@ const _debug = require('debug') | ||
constructor (feed) { | ||
this.id = randbytes() | ||
this.subs = [] | ||
@@ -59,3 +60,5 @@ this._ridTable = {} | ||
const remoteId = decId(msg.id) | ||
if (msg.op === OP_START_STREAM) { | ||
// console.log('Received hanshake ns:', msg.data.hexSlice) | ||
const sub = this._nameTable[msg.data.toString('utf8')] | ||
@@ -93,3 +96,2 @@ if (!sub) return this.emitStream('substream-discovered', msg.data.toString('utf8')) | ||
if (typeof name === 'string') name = Buffer.from(name) | ||
this.name = name || randbytes() | ||
@@ -99,9 +101,6 @@ this.state = INIT | ||
// Install router into feed if missing. | ||
if (!feed.__subrouter) { | ||
feed.__subrouter = new SubstreamRouter(feed) | ||
} | ||
this.router = feed.__subrouter | ||
this.router.addSub(this) | ||
this.debug = _debug(`substream/${this.id}`) | ||
this.debug = _debug(`substream/R ${this.router.id.hexSlice(0, 2)} S ${this.id}`) | ||
this.debug('Initializing new substream') | ||
@@ -126,7 +125,9 @@ } | ||
_read (size) { | ||
this.debug('sub read req') | ||
// I don't know if it matters that we completely ignore | ||
// the client reads and sizes. | ||
// this.debug('sub read req', size) | ||
} | ||
_sendHandshake () { | ||
this.debug('Sending handshake') | ||
this.debug('Sending handshake, ns:', this.name.hexSlice()) | ||
@@ -163,3 +164,2 @@ this.router.transmit(SubstreamOp.encode({ | ||
this.router.emitStream('substream-connected', this) | ||
this.debug('Received hanshake from remote, INITIALIZED!') | ||
break | ||
@@ -210,4 +210,2 @@ case CLOSING: | ||
switch (this.state) { | ||
case INIT: | ||
throw new Error('Impossible state, BUG!') | ||
case ESTABLISHED: | ||
@@ -225,2 +223,9 @@ switch (msg.op) { | ||
break | ||
case CLOSING: | ||
// If we're already closing we can ignore the | ||
// closing echos from the other end. | ||
if (msg.op === OP_CLOSE_STREAM) break | ||
// else fall through to invalid state error | ||
case INIT: | ||
case END: | ||
@@ -245,2 +250,19 @@ throw new Error('Impossible state, BUG!') | ||
if (typeof name === 'string') name = Buffer.from(name) | ||
assert(Buffer.isBuffer(name), '"namespace" must be a String or a Buffer') | ||
// Install router into feed if missing. | ||
if (!feed.__subrouter) { | ||
feed.__subrouter = new SubstreamRouter(feed) | ||
} | ||
// Detect duplicate namespaces | ||
const router = feed.__subrouter | ||
if (router._nameTable[name.toString('utf8')]) { | ||
const err = new NamespaceConflictError(name) | ||
if (typeof cb === 'function') return cb(err) | ||
else throw err | ||
} | ||
const sub = new SubStream(feed, name, opts, cb) | ||
@@ -288,1 +310,12 @@ | ||
} | ||
class NamespaceConflictError extends Error { | ||
constructor (name, ...params) { | ||
const msg = 'A substream with the same already exists on this feed' | ||
super(msg, ...params) | ||
this.conflict = name | ||
// Maintains proper stack trace for where our error was thrown (only available on V8) | ||
if (Error.captureStackTrace) Error.captureStackTrace(this, NamespaceConflictError) | ||
this.name = this.type = 'NamespaceConflictError' | ||
} | ||
} |
{ | ||
"name": "hypercore-protocol-substream", | ||
"version": "2.0.3", | ||
"version": "2.0.5", | ||
"description": "Create independent virtual streams on a hypercore-protocol stream", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -138,4 +138,3 @@ # hypercore-protocol-substream | ||
`namespace` a buffer, keep it short if possible, as it produces overhead on | ||
your data. | ||
`namespace` a string or buffer identifying the channel. | ||
@@ -142,0 +141,0 @@ `opts` Object |
18
test.js
@@ -165,1 +165,19 @@ const test = require('tape') | ||
test('duplicate namespace should throw NamespaceConflictError', t => { | ||
const key = Buffer.alloc(32) | ||
key.write('encryption secret') | ||
const stream1 = protocol({ | ||
extensions: [substream.EXTENSION] | ||
}) | ||
const vfeed1 = stream1.feed(key) | ||
const sub1 = substream(vfeed1, 'dupe') | ||
sub1.once('error', t.error) | ||
t.ok(sub1) | ||
substream(vfeed1, 'dupe', (err, sub2) => { | ||
t.equal(err.type, 'NamespaceConflictError') | ||
t.notOk(sub2, 'Callback invoked') | ||
sub1.end() | ||
t.end() | ||
}) | ||
}) |
24515
534
184