Updates
This commit is contained in:
@@ -1,37 +1,49 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
|
||||
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
|
||||
const b4a = require('b4a')
|
||||
|
||||
const PROTOCOL = 'stream-tee/v1'
|
||||
|
||||
class HyperP2PStreamTee extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this._chunks = []
|
||||
this._stats = { chunks: 0, bytes: 0 }
|
||||
this.highWaterMark = opts.highWaterMark || 65536
|
||||
this._branches = new Map()
|
||||
this._stats = { written: 0, branches: 0 }
|
||||
}
|
||||
|
||||
addBranch (name) {
|
||||
if (!name) throw new Error('branch name required')
|
||||
const branch = { name, chunks: [] }
|
||||
this._branches.set(name, branch)
|
||||
this._stats.branches++
|
||||
return () => this._branches.delete(name)
|
||||
}
|
||||
|
||||
write (chunk) {
|
||||
if (chunk == null) throw new Error('chunk required')
|
||||
const b4a = require('b4a')
|
||||
const buf = typeof chunk === 'string' ? b4a.from(chunk) : chunk
|
||||
this._chunks.push(buf)
|
||||
this._stats.chunks++
|
||||
this._stats.bytes += buf.length || buf.byteLength || 0
|
||||
for (const branch of this._branches.values()) branch.chunks.push(buf)
|
||||
this._stats.written++
|
||||
this.emit('data', buf)
|
||||
return this._stats.bytes <= this.highWaterMark
|
||||
return true
|
||||
}
|
||||
|
||||
read () { return this._chunks.shift() || null }
|
||||
readBranch (name) {
|
||||
const b = this._branches.get(name)
|
||||
return b ? b.chunks.shift() || null : null
|
||||
}
|
||||
|
||||
pending () { return this._chunks.length }
|
||||
pending (name) {
|
||||
const b = this._branches.get(name)
|
||||
return b ? b.chunks.length : 0
|
||||
}
|
||||
|
||||
getStats () { return { ...this._stats, protocol: PROTOCOL } }
|
||||
getStats () {
|
||||
return { ...this._stats, branchCount: this._branches.size, protocol: PROTOCOL }
|
||||
}
|
||||
|
||||
async ready () { return this }
|
||||
async close () { this._chunks = [] }
|
||||
async close () { this._branches.clear() }
|
||||
}
|
||||
|
||||
module.exports = { HyperP2PStreamTee, PROTOCOL }
|
||||
|
||||
@@ -2,25 +2,34 @@ require('bare-process/global')
|
||||
const test = require('brittle')
|
||||
const { HyperP2PStreamTee, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PStreamTee)
|
||||
t.ok(PROTOCOL)
|
||||
})
|
||||
test('exports', (t) => { t.ok(HyperP2PStreamTee); t.is(PROTOCOL, 'stream-tee/v1') })
|
||||
|
||||
test('basic operation', async (t) => {
|
||||
test('tee to branches', async (t) => {
|
||||
const m = new HyperP2PStreamTee()
|
||||
m.write('hi'); t.ok(m.read())
|
||||
m.addBranch('a')
|
||||
m.addBranch('b')
|
||||
m.write(require('b4a').from('z'))
|
||||
t.ok(m.readBranch('a'))
|
||||
t.ok(m.readBranch('b'))
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PStreamTee()
|
||||
try { m.write(null) } catch (e) { t.ok(e) }
|
||||
try { m.addBranch('') } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('remove branch', async (t) => {
|
||||
const m = new HyperP2PStreamTee()
|
||||
const off = m.addBranch('x')
|
||||
off()
|
||||
t.is(m.pending('x'), 0)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PStreamTee()
|
||||
t.ok(m.getStats().protocol)
|
||||
t.is(m.getStats().protocol, 'stream-tee/v1')
|
||||
await m.close()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user