feat(messaging-streams): manually deepen all six stream utility modules
- chunker: framed seq chunks, reassemble, setChunkSize - multiplex: closeAll, getStream, per-stream byte counters - backpressure: low/high water marks, bufferedBytes - tee: branchBytes, removeBranch, depth guard - transform: pipeTo and outbound backpressure - resume-token: labeled checkpoints, clearTokens, bytesFromOffset - Update category README and chunker tests Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -8,24 +8,40 @@ class HyperP2PStreamTee extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this._branches = new Map()
|
||||
this._stats = { written: 0, branches: 0 }
|
||||
this._stats = { written: 0, branches: 0, dropped: 0 }
|
||||
this.maxBranchDepth = opts.maxBranchDepth ?? 10000
|
||||
}
|
||||
|
||||
addBranch (name) {
|
||||
if (!name) throw new Error('branch name required')
|
||||
const branch = { name, chunks: [] }
|
||||
const branch = { name, chunks: [], bytes: 0 }
|
||||
this._branches.set(name, branch)
|
||||
this._stats.branches++
|
||||
return () => this._branches.delete(name)
|
||||
this.emit('branch-added', { name })
|
||||
return () => this.removeBranch(name)
|
||||
}
|
||||
|
||||
removeBranch (name) {
|
||||
return this._branches.delete(name)
|
||||
}
|
||||
|
||||
write (chunk) {
|
||||
if (chunk == null) throw new Error('chunk required')
|
||||
const buf = typeof chunk === 'string' ? b4a.from(chunk) : chunk
|
||||
for (const branch of this._branches.values()) branch.chunks.push(buf)
|
||||
const size = buf.length || buf.byteLength || 0
|
||||
let n = 0
|
||||
for (const branch of this._branches.values()) {
|
||||
if (branch.chunks.length >= this.maxBranchDepth) {
|
||||
this._stats.dropped++
|
||||
continue
|
||||
}
|
||||
branch.chunks.push(buf)
|
||||
branch.bytes += size
|
||||
n++
|
||||
}
|
||||
this._stats.written++
|
||||
this.emit('data', buf)
|
||||
return true
|
||||
return n > 0
|
||||
}
|
||||
|
||||
readBranch (name) {
|
||||
@@ -38,14 +54,24 @@ class HyperP2PStreamTee extends EventEmitter {
|
||||
return b ? b.chunks.length : 0
|
||||
}
|
||||
|
||||
listBranches () { return [...this._branches.keys()] }
|
||||
branchBytes (name) {
|
||||
const b = this._branches.get(name)
|
||||
return b ? b.bytes : 0
|
||||
}
|
||||
|
||||
hasBranch (name) { return this._branches.has(name) }
|
||||
listBranches () {
|
||||
return [...this._branches.keys()]
|
||||
}
|
||||
|
||||
hasBranch (name) {
|
||||
return this._branches.has(name)
|
||||
}
|
||||
|
||||
drainBranch (name) {
|
||||
const b = this._branches.get(name)
|
||||
if (!b) return []
|
||||
const out = b.chunks.splice(0, b.chunks.length)
|
||||
b.bytes = 0
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -59,8 +85,14 @@ class HyperP2PStreamTee extends EventEmitter {
|
||||
return { ...this._stats, branchCount: this._branches.size, protocol: PROTOCOL }
|
||||
}
|
||||
|
||||
async ready () { return this }
|
||||
async close () { this._branches.clear() }
|
||||
async ready () {
|
||||
return this
|
||||
}
|
||||
|
||||
async close () {
|
||||
this._branches.clear()
|
||||
this.emit('closed')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { HyperP2PStreamTee, PROTOCOL }
|
||||
|
||||
Reference in New Issue
Block a user