Expand category docs and module APIs across the library.
Manual pass adds helpers (listChannels, taskCounts, openProposals), richer getStats with protocol fields, category README hubs, and tightened api.md for core, messaging, network, routing, supercomputer, and consensus modules. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -1,84 +1,23 @@
|
||||
# API: hyper-p2p-stream-chunker
|
||||
|
||||
**Protocol:** `stream-chunker/v1` (local transform; no Hyperswarm)
|
||||
|
||||
**Export:** `HyperP2PStreamChunker`, `PROTOCOL`
|
||||
|
||||
## Overview
|
||||
|
||||
`HyperP2PStreamChunker` is a local byte-buffer utility for Bare/Pear stream pipelines. It accumulates inbound `Buffer` or string data, emits fixed-size slices via `push()` and `flush()`, and reports stats. There is no P2P wire — use with `hyper-p2p-stream-multiplex` or `hyper-p2p-stream-backpressure` for networked framing.
|
||||
|
||||
Extends `bare-events` `EventEmitter`.
|
||||
|
||||
## Constructor
|
||||
|
||||
```js
|
||||
const chunker = new HyperP2PStreamChunker(opts)
|
||||
```
|
||||
|
||||
| Option | Type | Default | Description |
|
||||
|--------|------|---------|-------------|
|
||||
| `chunkSize` | `number` | `4096` | Maximum bytes per emitted chunk |
|
||||
|
||||
Internal state: `_pending` buffer, `_stats.chunks`, `_stats.bytes`.
|
||||
**Protocol:** `stream-chunker/v1` · **Export:** `HyperP2PStreamChunker`
|
||||
|
||||
## Methods
|
||||
|
||||
### `push(data)`
|
||||
### `push(data) → void`
|
||||
|
||||
Appends data and emits complete chunks.
|
||||
Appends bytes; emits `chunk` when `chunkSize` reached.
|
||||
|
||||
- **Parameters:** `data` — `string` (UTF-8 via `b4a.from`) or `Buffer`
|
||||
- **Returns:** `Array<Buffer>` — slices emitted in this call (may be empty)
|
||||
- **Throws:** — (invalid buffer types may fail in `b4a.concat`)
|
||||
### `flush() → Buffer | null`
|
||||
|
||||
While `_pending.length >= chunkSize`, takes `subarray(0, chunkSize)`, advances pending, increments `chunks`, emits `chunk` event.
|
||||
Emits trailing partial buffer.
|
||||
|
||||
### `flush()`
|
||||
### `pendingLength() → number` / `reset() → void`
|
||||
|
||||
Emits any remaining pending bytes as one final chunk.
|
||||
### `getStats() → { chunks, bytes, pending, protocol }`
|
||||
|
||||
- **Returns:** `Buffer | null` — tail buffer or `null` if nothing pending
|
||||
- **Emits:** `chunk` when tail non-empty
|
||||
|
||||
### `getStats()`
|
||||
|
||||
- **Returns:** `{ chunks, bytes, pending, protocol }` — `pending` is current buffer length
|
||||
|
||||
### `ready()` / `close()`
|
||||
|
||||
- **Returns:** `Promise<this>` — no-op ready; `close()` zeroes pending buffer
|
||||
### `async ready()` / `async close()`
|
||||
|
||||
## Events
|
||||
|
||||
| Event | Payload | When |
|
||||
|-------|---------|------|
|
||||
| `chunk` | `Buffer` | Each slice from `push` or `flush` |
|
||||
|
||||
## Wire / P2P
|
||||
|
||||
None. Protocol constant exists for registry and composition docs only.
|
||||
|
||||
## Errors
|
||||
|
||||
No validation errors on normal use. Empty `push` is allowed.
|
||||
|
||||
## Composition
|
||||
|
||||
| Module | Role |
|
||||
|--------|------|
|
||||
| `hyper-p2p-stream-multiplex` | Frame chunked slices per stream id |
|
||||
| `hyper-p2p-stream-backpressure` | Gate `push` when high water mark hit |
|
||||
| `hyper-p2p-stream-transform` | Map/filter between chunker and mux |
|
||||
|
||||
## Testing
|
||||
|
||||
`npm test` — multi-chunk `push`, partial tail `flush`, stats.
|
||||
|
||||
## Example
|
||||
|
||||
`examples/basic.js`
|
||||
|
||||
## See also
|
||||
|
||||
[`docs/architecture.md`](architecture.md)
|
||||
`chunk`, `flush`
|
||||
|
||||
@@ -27,6 +27,13 @@ class HyperP2PStreamChunker extends EventEmitter {
|
||||
return emitted
|
||||
}
|
||||
|
||||
pendingLength () { return this._pending.length }
|
||||
|
||||
reset () {
|
||||
this._pending = b4a.alloc(0)
|
||||
return this
|
||||
}
|
||||
|
||||
flush () {
|
||||
if (!this._pending.length) return null
|
||||
const tail = this._pending
|
||||
|
||||
Reference in New Issue
Block a user