wenay-common2 1.0.65 → 1.0.67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +20 -0
- package/doc/ROADMAP.md +73 -7
- package/doc/changes/1.0.65.md +2 -1
- package/doc/changes/1.0.66.md +8 -0
- package/doc/changes/1.0.67.md +10 -0
- package/doc/wenay-common2-rare.md +73 -3
- package/doc/wenay-common2.md +11 -0
- package/lib/Common/events/replay-index.d.ts +1 -0
- package/lib/Common/events/replay-index.js +1 -0
- package/lib/Common/events/route-coordinator.d.ts +274 -0
- package/lib/Common/events/route-coordinator.js +316 -0
- package/lib/Common/media/media-index.d.ts +1 -0
- package/lib/Common/media/media-index.js +17 -0
- package/lib/Common/media/media-source.d.ts +94 -0
- package/lib/Common/media/media-source.js +537 -0
- package/lib/client.d.ts +2 -0
- package/lib/client.js +3 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +3 -1
- package/package.json +4 -2
package/CLAUDE.md
CHANGED
|
@@ -51,6 +51,26 @@ Write new code in this style by default — no need to ask.
|
|
|
51
51
|
## Comments
|
|
52
52
|
- Section dividers like `// ===...===` with a block heading.
|
|
53
53
|
- Explain "why", not "what". Keep them short.
|
|
54
|
+
|
|
55
|
+
## Work progress files
|
|
56
|
+
|
|
57
|
+
- For any task that is more than a tiny/local edit, create a temporary progress file before
|
|
58
|
+
starting broad changes.
|
|
59
|
+
- Put progress files under `doc/progress/`. This is the working-doc area, separate from public
|
|
60
|
+
API docs, roadmap docs, and release notes.
|
|
61
|
+
- Name them by task, for example `doc/progress/replay-route-handoff.md`.
|
|
62
|
+
- Keep the file short: goal, current checkpoint list, notable decisions, blockers, and verification
|
|
63
|
+
already run or still needed.
|
|
64
|
+
- Update the progress file as checkpoints are completed, especially before switching context or
|
|
65
|
+
making broad edits.
|
|
66
|
+
- When the task is finished, delete the progress file. Preserve only the durable outcome:
|
|
67
|
+
final response, commit message, and, for publishable changes, the matching `doc/changes/<version>.md`
|
|
68
|
+
entry.
|
|
69
|
+
- If work is paused or blocked locally, leave the progress file in place and make the next required
|
|
70
|
+
action explicit. If the paused state must be committed or handed off, promote the useful part into
|
|
71
|
+
a durable doc (`ROADMAP`, `RECOMMENDATIONS`, `doc/target`, or `doc/changes`) instead of relying on
|
|
72
|
+
an ignored progress file.
|
|
73
|
+
|
|
54
74
|
## Documentation and release notes
|
|
55
75
|
|
|
56
76
|
- `README.md` is navigation only. Do not put API guides, examples, or long explanations there.
|
package/doc/ROADMAP.md
CHANGED
|
@@ -37,7 +37,12 @@ This implies a useful split:
|
|
|
37
37
|
- **Replay layer** resumes streams and mirrors from the last known `seq`.
|
|
38
38
|
- **Authority layer** decides whether an incoming write is accepted, predicted, reconciled, or merged.
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
Critical ordering rule: do **not** start with WebRTC/NAT plumbing or CRDTs. The next useful layer is a
|
|
41
|
+
small route/account/policy coordinator with a fake/in-process transport adapter. Direct transport and
|
|
42
|
+
multi-writer merge are expensive adapters; they only become safe after the coordinator state machine is
|
|
43
|
+
boring and well-tested.
|
|
44
|
+
|
|
45
|
+
### 0.1 Account route coordinator ("wrapper over wrappers") 🟡
|
|
41
46
|
|
|
42
47
|
Some deployments need separate client/account identities to communicate directly when policy and
|
|
43
48
|
network conditions allow it, while still allowing the server/relay to step back into the path later.
|
|
@@ -61,12 +66,73 @@ mirror, and replay primitives.
|
|
|
61
66
|
- **Privacy rule:** direct account links are opt-in and policy-gated. Peers only receive the endpoint
|
|
62
67
|
and session material needed for that specific relationship; no implicit broad account discovery.
|
|
63
68
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
Concrete next API shape, not final names:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
createRouteCoordinator({
|
|
73
|
+
policy,
|
|
74
|
+
routes,
|
|
75
|
+
resources,
|
|
76
|
+
}) -> {
|
|
77
|
+
pair(a, b),
|
|
78
|
+
state(pair),
|
|
79
|
+
promoteDirect(pair, opts?),
|
|
80
|
+
reinterposeRelay(pair, reason?),
|
|
81
|
+
block(pair, reason?),
|
|
82
|
+
fallback(pair, reason?),
|
|
83
|
+
onRoute(cb),
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Policy must run **before** transport promotion:
|
|
88
|
+
|
|
89
|
+
- `canDirect(pair, ctx)` - may these accounts attempt direct at all?
|
|
90
|
+
- `mustRelay(pair, ctx)` - force relay path because of NDA/audit/moderation/reauth.
|
|
91
|
+
- `mustShadowRelay(pair, ctx)` - allow direct payload path, but keep audit/observe copy.
|
|
92
|
+
- `canExposeEndpoint(pair, ctx)` - whether signaling may reveal endpoint/session material.
|
|
93
|
+
- `canReinterpose(pair, ctx)` - whether/when relay is allowed or required to step back in.
|
|
94
|
+
|
|
95
|
+
Minimum state machine:
|
|
96
|
+
|
|
97
|
+
- `relay` -> `direct:connecting` -> `direct` -> `relay:reinterposing` -> `relay`
|
|
98
|
+
- `relay` -> `direct:connecting` -> `fallback` -> `relay`
|
|
99
|
+
- `direct` -> `direct+shadowRelay`
|
|
100
|
+
- any state -> `blocked`
|
|
101
|
+
|
|
102
|
+
Required failure modes:
|
|
103
|
+
|
|
104
|
+
- direct setup never completes: keep relay active and mark fallback;
|
|
105
|
+
- replacement route catches up too slowly: keep old route active and fail the switch;
|
|
106
|
+
- policy changes mid-stream: re-interpose relay through replay hand-off;
|
|
107
|
+
- account reauth changes facade/ACL: rebuild route policy before accepting new writes;
|
|
108
|
+
- endpoint/session material is revoked: close direct and resume relay from `seq`;
|
|
109
|
+
- audit/shadow route lags: decide whether to throttle, fallback, or block by policy.
|
|
110
|
+
|
|
111
|
+
Acceptance tests for 0.1:
|
|
112
|
+
|
|
113
|
+
- policy denial: direct is never attempted;
|
|
114
|
+
- direct promotion: old relay stays live until replacement catches up;
|
|
115
|
+
- failed direct: old relay continues with no data gap;
|
|
116
|
+
- re-interposition: direct -> relay resumes from `seq`;
|
|
117
|
+
- `direct+shadowRelay`: direct path is active while relay/audit mirror observes;
|
|
118
|
+
- revocation: direct closes and relay resumes without changing facade API;
|
|
119
|
+
- account map uses `noStrict`, but all access checks live in policy/facade code;
|
|
120
|
+
- `createStoreManager` starts/stops selected per-account mirrors without store-core changes.
|
|
121
|
+
|
|
122
|
+
Open questions: signaling envelope; whether the relay sees payloads or only coordinates encrypted
|
|
123
|
+
direct streams; backpressure across multi-hop and direct paths; group topology beyond a pair of
|
|
124
|
+
accounts; `noStrict(accountMap)` / `createStoreManager` lifecycle integration for dynamic peer maps.
|
|
125
|
+
|
|
126
|
+
Status: 🟡 partial (2026-07-09, v1.0.67). Core implemented as `Replay.createRouteCoordinator`
|
|
127
|
+
(`src/Common/events/route-coordinator.ts`): `RouteConnector` contract (pure transport: open/close/state/
|
|
128
|
+
metrics/onFail/capabilities), all five policy hooks, the full state machine above (including
|
|
129
|
+
`direct+shadowRelay` audit copy, catch-up timeout, revocation auto-fallback, terminal `blocked`), data
|
|
130
|
+
continuity through `replayRouteSubscribe`. Acceptance oracle: `replay/route-coordinator.test.ts` over
|
|
131
|
+
fake in-process connectors — policy denial never touches transport, promotion keeps the old relay live,
|
|
132
|
+
failed/slow direct falls back gap-free, re-interposition resumes from `seq`, shadow relay observes the
|
|
133
|
+
switch window, revocation closes direct without facade changes, block is terminal. Still open: real
|
|
134
|
+
signaling adapter + WebRTC connector (steps 9-10 of `doc/target/webrtc-route-coordinator-tasks.md`) and
|
|
135
|
+
the account-map lifecycle integration.
|
|
70
136
|
|
|
71
137
|
## 1. Connection hand-off — relay ↔ direct promotion ("port forwarding") 🟡
|
|
72
138
|
|
package/doc/changes/1.0.65.md
CHANGED
|
@@ -4,4 +4,5 @@
|
|
|
4
4
|
- Add `Observe.syncStoreReplayRoute` for store mirrors that can switch relay/direct routes without gaps or duplicate patch delivery.
|
|
5
5
|
- Add `replay/route-handoff.test.ts` covering relay -> direct promotion, direct -> relay re-interposition, failed replacement fallback, and store mirror convergence.
|
|
6
6
|
- Update roadmap/replay docs to mark connection hand-off as partially implemented and keep signaling/WebRTC/policy triggers explicitly open.
|
|
7
|
-
- Normalize package metadata for `wenay-common2@1.0.65`.
|
|
7
|
+
- Normalize package metadata for `wenay-common2@1.0.65`.
|
|
8
|
+
- Add `doc/progress/` working-checkpoint rules for non-trivial tasks and keep task progress files out of package output.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# 1.0.66
|
|
2
|
+
|
|
3
|
+
- Add `Media.createAudioSource` and `Media.createVideoSource` browser capture helpers that expose media as ordinary binary `Listen` sources.
|
|
4
|
+
- Add fixed-header media frame helpers: `Media.encodeMediaFrame` and `Media.decodeMediaFrame` (`Uint8Array` header + raw payload, no JSON envelope).
|
|
5
|
+
- Add optional `replay:true` media source mode so `createRpcServerAuto` can expose media listeners with legacy + replay surfaces under the same key.
|
|
6
|
+
- Add `wenay-common2/media` package export and root `Media` namespace export.
|
|
7
|
+
- Add `replay/media-socket.test.ts` covering media binary frame headers, no-device typed state, replay media source shape, and real Socket.IO/RPC binary delivery.
|
|
8
|
+
- Document media-over-socket backpressure defaults and reserve WebRTC as a future explicit SFU/signaling opt-in, not the default transport.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 1.0.67
|
|
2
|
+
|
|
3
|
+
- Add `Replay.createRouteCoordinator`: policy-gated relay <-> direct routing shell over pure transport connectors (ROADMAP 0.1 core).
|
|
4
|
+
- Add the `RouteConnector` contract: `open/close/state`, capabilities (`binary/ordered/reliable`), route label, `metrics` (rtt/pending), and `onFail` events — connectors stay pure transports, the coordinator alone owns route decisions.
|
|
5
|
+
- Route state machine: `relay -> direct:connecting -> direct | direct+shadowRelay`, re-interposition (`relay:reinterposing -> relay`), `fallback` on failed/slow/revoked direct, terminal `blocked`.
|
|
6
|
+
- Policy hooks run before any transport action: `canDirect`, `mustRelay`, `mustShadowRelay`, `canExposeEndpoint`, `canReinterpose`; denial means direct is never attempted.
|
|
7
|
+
- `promoteDirect({timeoutMs?})` treats policy denial and transport failure as result objects, not exceptions; a slow replacement route times out while the old route keeps flowing; direct `onFail` (endpoint revoked) auto-falls back to relay from the last `seq`.
|
|
8
|
+
- `direct+shadowRelay`: payload rides direct while `shadow(ref, ...ev)` receives the relay audit copy starting from the consumers' seq coordinate (the switch window never escapes the audit).
|
|
9
|
+
- Data continuity is `replayRouteSubscribe` underneath: every route change is gap-free and dup-free by the `seq` contract; consumer facade/authority semantics never learn the transport switched.
|
|
10
|
+
- Add acceptance oracle `replay/route-coordinator.test.ts` over fake in-process relay/direct connectors (policy denial, promotion, failed direct, catch-up timeout, shadow relay, revocation, block, store mirror over a switched route).
|
|
@@ -205,6 +205,50 @@ Contract:
|
|
|
205
205
|
- Static dotted keys are also supported: `api["a.b"].c` is distinct from `api.a.b.c`. Internal route/listen/cache identity must stay lossless; dotted strings are only a debug display form.
|
|
206
206
|
- If a branch is a fixed public API, keep it strict. If a branch is a personal/dynamic keyspace, wrap that branch in `noStrict` instead of trying to publish all current keys as schema.
|
|
207
207
|
|
|
208
|
+
## 🎙️ Media over socket — browser capture as binary Listen
|
|
209
|
+
> `import { Media } from "wenay-common2"` or `import * as Media from "wenay-common2/media"`.
|
|
210
|
+
> The hot path event is ONE `Uint8Array`: fixed 40-byte common2 media header + raw payload. No JSON envelope.
|
|
211
|
+
```
|
|
212
|
+
Media.createAudioSource(opts?) -> [emit, listen] & control
|
|
213
|
+
Media.createVideoSource(opts?) -> [emit, listen] & control
|
|
214
|
+
Media.encodeMediaFrame(meta, payload) -> Uint8Array
|
|
215
|
+
Media.decodeMediaFrame(frame) -> {kind, codec, seq, tMono, payload, sampleRate?, channels?, nSamples?, width?, height?}
|
|
216
|
+
|
|
217
|
+
control: start() -> Promise<MediaSourceState> · stop() · getStats() · setDevice(id) · listDevices()
|
|
218
|
+
state: 'idle'|'requesting'|'live'|'denied'|'no-device'|'error'
|
|
219
|
+
```
|
|
220
|
+
Audio source:
|
|
221
|
+
- default `mode:'pcm'`, `format:'int16'`, raw PCM payload; uses `AudioWorklet` when available and falls back to `ScriptProcessor` only when the browser cannot run a worklet.
|
|
222
|
+
- `mode:'record'` uses `MediaRecorder` chunks (`webm-opus`) for record/upload flows, not live STT.
|
|
223
|
+
- `getStats().rms` gives a VU-meter signal; permission denied/no device returns typed state, not a thrown public failure.
|
|
224
|
+
|
|
225
|
+
Video source:
|
|
226
|
+
- default snapshots, not a 30fps video stream: `video->canvas`, JPEG, `fps` default 3, `quality` default 0.82.
|
|
227
|
+
- each frame carries absolute image bytes, so `replay:true` can safely keep the latest frame for lag recovery.
|
|
228
|
+
- `worker` is API-reserved; current implementation stays main-thread. Any future worker path must transfer `ArrayBuffer` payloads, never structured-clone frame objects.
|
|
229
|
+
|
|
230
|
+
Replay/RPC wiring:
|
|
231
|
+
```ts
|
|
232
|
+
const audio = Media.createAudioSource({sourceId: 'mic'}) // plain lossless queue Listen
|
|
233
|
+
const video = Media.createVideoSource({sourceId: 'cam', fps: 2, replay: true})
|
|
234
|
+
|
|
235
|
+
createRpcServerAuto({
|
|
236
|
+
socket, socketKey: 'media',
|
|
237
|
+
object: {audio: audio[1], video: video[1]},
|
|
238
|
+
replayOpts: {highWater: 64, lowWater: 8},
|
|
239
|
+
})
|
|
240
|
+
```
|
|
241
|
+
`replay:true` makes the returned listen a `Replay.replayListen` surface before capture emits into it, so `createRpcServerAuto` brand-detects it and exposes legacy + replay under the same key. Defaults differ by media kind: audio replay is a sacred queue (`history:1024`, no keyframe/frame, do not drop samples); video replay is keep-latest (`history:256`, `current:'last'`, `frame` returns the newest covered frame). Pass `replay:{...}` for custom history/current/frame.
|
|
242
|
+
|
|
243
|
+
Backpressure rule: audio consumers should use the default queue policy unless the app explicitly accepts loss. Video consumers can use `Replay.replaySubscribe(remote.video, cb, {policy:'frame'})`; a slow socket drains to the latest frame instead of accumulating stale images. The binary frame itself is RPC-safe because `rpc-walk` passes `TypedArray`/`ArrayBuffer` leaves through natively and applies `maxBinaryLen`.
|
|
244
|
+
|
|
245
|
+
WebRTC future contract:
|
|
246
|
+
- `transport:'socket'` is the implemented default today.
|
|
247
|
+
- `transport:'webrtc'` is reserved and currently reports `state:'error'` on `start()`; it is not a hidden second transport.
|
|
248
|
+
- Future WebRTC support must be explicit opt-in for sub-200ms human duplex. Signaling belongs on the existing socket/RPC control channel (offer/answer/ICE), and backend/AI access requires an SFU that re-emits media bytes into the same `Media` Listen/replay surface. Downstream RPC/replay/store consumers must not change.
|
|
249
|
+
|
|
250
|
+
Oracle: `npx ts-node replay/media-socket.test.ts` checks header decode, plain Listen shape, `replay:true`, typed no-device state in Node, and real Socket.IO binary delivery.
|
|
251
|
+
|
|
208
252
|
## 📈 exchange — params (`CParams`)
|
|
209
253
|
```
|
|
210
254
|
class CParams / CParamsReadonly implements IParams
|
|
@@ -420,8 +464,8 @@ exposeReplay(replay) <-> replaySubscribe(remote, cb, {since?, onSeq?, staleMs?
|
|
|
420
464
|
// on the line (RPC_STOP — e.g. the gate's loud sacred failure) surfaces via onError + off, never silence.
|
|
421
465
|
// hint reaches the frame lambda on catch-up and on every explicit frame(seq, hint) call (pull); the push-gate's
|
|
422
466
|
// drain recovery uses the line's DEFAULT condensation — client-specific rules/pace = the pull path.
|
|
423
|
-
// off.ready (catch-up done) · off.seq() (reconnect point) · off.isStale()/off.lastTs(); reconnect = call again with {since: prev.seq()}
|
|
424
|
-
replayRouteSubscribe(remote, cb, {label?, since?, onSeq?, onError?, onRoute?}) -> off & {ready, switch(nextRemote, {label?, since?, reset?, policy?, hint?}), seq(), label(), active()}
|
|
467
|
+
// off.ready (catch-up done) · off.seq() (reconnect point) · off.isStale()/off.lastTs(); reconnect = call again with {since: prev.seq()}
|
|
468
|
+
replayRouteSubscribe(remote, cb, {label?, since?, onSeq?, onError?, onRoute?}) -> off & {ready, switch(nextRemote, {label?, since?, reset?, policy?, hint?}), seq(), label(), active()}
|
|
425
469
|
// transport hand-off helper: old route remains live, replacement subscribes+catches up from seq, then old closes; overlap is seq-deduped. Use for relay -> direct and direct -> relay over any ordered ReplayRemote.
|
|
426
470
|
// DELIVERY CONTRACT (guaranteed, not best-effort): the subscriber's cb sees ONE uniform stream —
|
|
427
471
|
// first delivery = the snapshot (keyframe as an event of the SAME type; store: root patch),
|
|
@@ -441,7 +485,33 @@ replayRouteSubscribe(remote, cb, {label?, since?, onSeq?, onError?, onRoute?}) -
|
|
|
441
485
|
// of clock skew) + ENVELOPE-TS AGE checked at delivery (producer clock — a stale keyframe reports stale
|
|
442
486
|
// IMMEDIATELY; clock-skew caveat: producer/client clocks may disagree, skewMs tolerance absorbs it, default 0).
|
|
443
487
|
// A since-tail's historical ts never flaps mid-catch-up (one assessment after handover); off() disarms the timer.
|
|
444
|
-
|
|
488
|
+
createRouteCoordinator({connect, policy?, shadow?, catchUpTimeoutMs?}) -> coordinator // policy-gated relay <-> direct routing shell over pure connectors (ROADMAP 0.1)
|
|
489
|
+
// LAYERING: a CONNECTOR is a pure transport (no route decisions) — {info: {label, kind: 'relay'|'direct',
|
|
490
|
+
// binary?, ordered?, reliable?}, open() -> ReplayRemote, close(), state(), metrics?() -> {rtt?, pending?},
|
|
491
|
+
// onFail?}; the COORDINATOR alone owns promotion/fallback (state machine + policy); data continuity is
|
|
492
|
+
// replayRouteSubscribe underneath, so ANY route change is gap-free and dup-free by the seq contract.
|
|
493
|
+
// WebRTC/NAT is deliberately absent here: a future datachannel is just another RouteConnector.
|
|
494
|
+
// connect(ref, kind) -> RouteConnector — transport factory per pair and kind (called per activation).
|
|
495
|
+
// policy hooks run BEFORE any transport action; absent hook = allowed, present hook must return true:
|
|
496
|
+
// canDirect (may the pair attempt direct) · mustRelay (force relay: NDA/audit/moderation — beats canDirect)
|
|
497
|
+
// · mustShadowRelay (direct payload + relay audit copy) · canExposeEndpoint (may signaling reveal
|
|
498
|
+
// endpoint/session material) · canReinterpose (may relay step back into the path).
|
|
499
|
+
// coordinator.pair(a, b) -> link (symmetric key: pair(a,b) == pair(b,a)) · state/promoteDirect/
|
|
500
|
+
// reinterposeRelay/fallback/block(pairOrKey, ...) · onRoute(cb) -> off (all transitions, all pairs)
|
|
501
|
+
// · pairs() · close()
|
|
502
|
+
// link: .subscribe(cb, opts?) -> off & {ready, seq(), label(), active()} // the pair's data: a replay stream
|
|
503
|
+
// that survives every route change; facade/authority semantics never learn the transport switched
|
|
504
|
+
// .promoteDirect({timeoutMs?, reason?}) -> Promise<{ok, state, reason?}> // policy denial and transport
|
|
505
|
+
// failure are EXPECTED OUTCOMES (result object), not exceptions; ops are serialized per link
|
|
506
|
+
// .reinterposeRelay(reason?) / .fallback(reason?) / .block(reason?) · .state() · .label() · .metrics() · .close()
|
|
507
|
+
// STATE MACHINE: relay -> direct:connecting -> direct | direct+shadowRelay; direct -> relay:reinterposing -> relay;
|
|
508
|
+
// failed/slow direct (timeoutMs) -> fallback (relay kept live the whole time, switch failed loudly);
|
|
509
|
+
// direct onFail (endpoint revoked, link died) -> auto fallback: close direct, resume relay from seq;
|
|
510
|
+
// any state -> blocked (terminal: subs closed, new subscribe throws, promote denied).
|
|
511
|
+
// direct+shadowRelay: payload rides direct while deps.shadow(ref, ...ev) receives the relay audit copy,
|
|
512
|
+
// starting from the consumers' seq coordinate — the switch window never escapes the audit.
|
|
513
|
+
// Acceptance oracle: replay/route-coordinator.test.ts (fake in-process relay/direct connectors).
|
|
514
|
+
exposeStoreReplay(store, opts?) <-> syncStoreReplay(mirror, remote, opts?) // layer B: patch line; keyframe = root patch ({path: [], value: snapshot})
|
|
445
515
|
syncStoreReplayRoute(mirror, remote, opts?) -> off & {ready, switch(nextRemote, opts), seq(), label(), active()} // same patch fold, but route-replaceable for relay/direct promotion
|
|
446
516
|
syncStoreReplayEach<T>(remote, cb, opts?) -> off & {store, ready, seq(), isStale(), lastTs()} // one-call per-key fold over the patch line (mirror + syncStoreReplay + store.each()); most-used surface — full contract + example in wenay-common2.md
|
|
447
517
|
createOfflineStore({key, remote?, initial, storage, version?, debounceMs?, syncOpts?}) -> Promise<OfflineStore<T>>
|
package/doc/wenay-common2.md
CHANGED
|
@@ -143,6 +143,17 @@ await l.ticks.frame(mySeq) // pull at YOUR pace (
|
|
|
143
143
|
// full guide + examples → rpc.md; frame model / lag policies → 🎞️ recipe below and rare docs
|
|
144
144
|
```
|
|
145
145
|
|
|
146
|
+
## 🎙️ Media over socket — binary Listen frames
|
|
147
|
+
```
|
|
148
|
+
import { Media } from "wenay-common2" // or: import * as Media from "wenay-common2/media"
|
|
149
|
+
|
|
150
|
+
Media.createAudioSource({format?: 'int16'|'float32', mode?: 'pcm'|'record', replay?}) -> [emit, listen] & control
|
|
151
|
+
Media.createVideoSource({fps? = 3, codec? = 'jpeg', quality?, replay?}) -> [emit, listen] & control
|
|
152
|
+
control: start() -> Promise<'idle'|'requesting'|'live'|'denied'|'no-device'|'error'> · stop() · getStats() · setDevice(id) · listDevices() · state
|
|
153
|
+
Media.encodeMediaFrame(meta, payload) / Media.decodeMediaFrame(frame) // one Uint8Array = 40-byte fixed header + raw payload
|
|
154
|
+
```
|
|
155
|
+
Audio default is PCM frames from `AudioWorklet` where available (`mode:'record'` uses MediaRecorder chunks). Video default is camera snapshots (`video->canvas`, JPEG, low fps for vision). Put `listen` into `createRpcServerAuto` like any other Listen; with `replay:true`, the returned listen is a replay line, so RPC auto exposes legacy + replay surfaces under the same key. Backpressure policy: audio is lossless queue; video `replay:true` defaults to keep-latest frame recovery. `transport:'webrtc'` is reserved for a future SFU/signaling adapter; socket binary is the default today.
|
|
156
|
+
|
|
146
157
|
## 🔁 Observe — reactive state + store/mirror API
|
|
147
158
|
> `import { Observe } from "wenay-common2"` or `import * as Observe from "wenay-common2/observe"`.
|
|
148
159
|
> This is the documented v2 reactive/store surface.
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { Listener } from './Listen';
|
|
2
|
+
import { ReplayRemote } from './replay-wire';
|
|
3
|
+
import { ReplayRouteSubscribeOpts } from './replay-route';
|
|
4
|
+
export type tRouteKind = 'relay' | 'direct';
|
|
5
|
+
export type tConnectorState = 'idle' | 'opening' | 'open' | 'closed' | 'failed';
|
|
6
|
+
export type RouteConnectorInfo = {
|
|
7
|
+
label: string;
|
|
8
|
+
kind: tRouteKind;
|
|
9
|
+
binary?: boolean;
|
|
10
|
+
ordered?: boolean;
|
|
11
|
+
reliable?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export type RouteConnectorMetrics = {
|
|
14
|
+
rtt?: number;
|
|
15
|
+
pending?: number;
|
|
16
|
+
};
|
|
17
|
+
export type RouteConnector<Z extends any[] = any[]> = {
|
|
18
|
+
info: RouteConnectorInfo;
|
|
19
|
+
open: () => Promise<ReplayRemote<Z>> | ReplayRemote<Z>;
|
|
20
|
+
close: () => void;
|
|
21
|
+
state: () => tConnectorState;
|
|
22
|
+
metrics?: () => RouteConnectorMetrics;
|
|
23
|
+
onFail?: {
|
|
24
|
+
on: (cb: (reason?: unknown) => void) => any;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
export type RoutePairRef = {
|
|
28
|
+
a: string;
|
|
29
|
+
b: string;
|
|
30
|
+
key: string;
|
|
31
|
+
};
|
|
32
|
+
export type RoutePolicyCtx = RoutePairRef & {
|
|
33
|
+
state: tRouteState;
|
|
34
|
+
reason?: unknown;
|
|
35
|
+
};
|
|
36
|
+
type tPolicyHook = (ctx: RoutePolicyCtx) => boolean | Promise<boolean>;
|
|
37
|
+
export type RoutePolicy = {
|
|
38
|
+
canDirect?: tPolicyHook;
|
|
39
|
+
mustRelay?: tPolicyHook;
|
|
40
|
+
mustShadowRelay?: tPolicyHook;
|
|
41
|
+
canExposeEndpoint?: tPolicyHook;
|
|
42
|
+
canReinterpose?: tPolicyHook;
|
|
43
|
+
};
|
|
44
|
+
export type tRouteState = 'relay' | 'direct:connecting' | 'direct' | 'direct+shadowRelay' | 'relay:reinterposing' | 'fallback' | 'blocked' | 'closed';
|
|
45
|
+
export type RouteChangeEvent = RoutePairRef & {
|
|
46
|
+
from: tRouteState;
|
|
47
|
+
to: tRouteState;
|
|
48
|
+
reason?: unknown;
|
|
49
|
+
};
|
|
50
|
+
export type RouteOpResult = {
|
|
51
|
+
ok: boolean;
|
|
52
|
+
state: tRouteState;
|
|
53
|
+
reason?: unknown;
|
|
54
|
+
};
|
|
55
|
+
export type PromoteDirectOpts = {
|
|
56
|
+
timeoutMs?: number;
|
|
57
|
+
reason?: unknown;
|
|
58
|
+
};
|
|
59
|
+
export type RouteCoordinatorDeps<Z extends any[] = any[]> = {
|
|
60
|
+
policy?: RoutePolicy;
|
|
61
|
+
connect: (ref: RoutePairRef, kind: tRouteKind) => RouteConnector<Z>;
|
|
62
|
+
shadow?: (ref: RoutePairRef, ...ev: Z) => void;
|
|
63
|
+
catchUpTimeoutMs?: number;
|
|
64
|
+
};
|
|
65
|
+
export declare function createRouteCoordinator<Z extends any[] = any[]>(deps: RouteCoordinatorDeps<Z>): {
|
|
66
|
+
pair(a: string, b: string): {
|
|
67
|
+
ref: RoutePairRef;
|
|
68
|
+
state: () => tRouteState;
|
|
69
|
+
reason: () => unknown;
|
|
70
|
+
label: () => string;
|
|
71
|
+
metrics: () => {
|
|
72
|
+
relay: {
|
|
73
|
+
rtt?: number;
|
|
74
|
+
pending?: number;
|
|
75
|
+
state: tConnectorState;
|
|
76
|
+
} | null;
|
|
77
|
+
direct: {
|
|
78
|
+
rtt?: number;
|
|
79
|
+
pending?: number;
|
|
80
|
+
state: tConnectorState;
|
|
81
|
+
} | null;
|
|
82
|
+
};
|
|
83
|
+
subscribe: (cb: Listener<Z>, opts?: Omit<ReplayRouteSubscribeOpts, "label">) => (() => void) & {
|
|
84
|
+
ready: Promise<void>;
|
|
85
|
+
seq: () => number;
|
|
86
|
+
label: () => string | undefined;
|
|
87
|
+
active: () => boolean;
|
|
88
|
+
};
|
|
89
|
+
promoteDirect: (opts?: PromoteDirectOpts) => Promise<RouteOpResult>;
|
|
90
|
+
reinterposeRelay: (reason?: unknown) => Promise<RouteOpResult>;
|
|
91
|
+
fallback: (reason?: unknown) => Promise<RouteOpResult>;
|
|
92
|
+
block: (reason?: unknown) => Promise<RouteOpResult>;
|
|
93
|
+
close: () => void;
|
|
94
|
+
};
|
|
95
|
+
state: (pairOrKey: {
|
|
96
|
+
ref: RoutePairRef;
|
|
97
|
+
state: () => tRouteState;
|
|
98
|
+
reason: () => unknown;
|
|
99
|
+
label: () => string;
|
|
100
|
+
metrics: () => {
|
|
101
|
+
relay: {
|
|
102
|
+
rtt?: number;
|
|
103
|
+
pending?: number;
|
|
104
|
+
state: tConnectorState;
|
|
105
|
+
} | null;
|
|
106
|
+
direct: {
|
|
107
|
+
rtt?: number;
|
|
108
|
+
pending?: number;
|
|
109
|
+
state: tConnectorState;
|
|
110
|
+
} | null;
|
|
111
|
+
};
|
|
112
|
+
subscribe: (cb: Listener<Z>, opts?: Omit<ReplayRouteSubscribeOpts, "label">) => (() => void) & {
|
|
113
|
+
ready: Promise<void>;
|
|
114
|
+
seq: () => number;
|
|
115
|
+
label: () => string | undefined;
|
|
116
|
+
active: () => boolean;
|
|
117
|
+
};
|
|
118
|
+
promoteDirect: (opts?: PromoteDirectOpts) => Promise<RouteOpResult>;
|
|
119
|
+
reinterposeRelay: (reason?: unknown) => Promise<RouteOpResult>;
|
|
120
|
+
fallback: (reason?: unknown) => Promise<RouteOpResult>;
|
|
121
|
+
block: (reason?: unknown) => Promise<RouteOpResult>;
|
|
122
|
+
close: () => void;
|
|
123
|
+
} | RoutePairRef | string) => tRouteState;
|
|
124
|
+
promoteDirect: (pairOrKey: {
|
|
125
|
+
ref: RoutePairRef;
|
|
126
|
+
state: () => tRouteState;
|
|
127
|
+
reason: () => unknown;
|
|
128
|
+
label: () => string;
|
|
129
|
+
metrics: () => {
|
|
130
|
+
relay: {
|
|
131
|
+
rtt?: number;
|
|
132
|
+
pending?: number;
|
|
133
|
+
state: tConnectorState;
|
|
134
|
+
} | null;
|
|
135
|
+
direct: {
|
|
136
|
+
rtt?: number;
|
|
137
|
+
pending?: number;
|
|
138
|
+
state: tConnectorState;
|
|
139
|
+
} | null;
|
|
140
|
+
};
|
|
141
|
+
subscribe: (cb: Listener<Z>, opts?: Omit<ReplayRouteSubscribeOpts, "label">) => (() => void) & {
|
|
142
|
+
ready: Promise<void>;
|
|
143
|
+
seq: () => number;
|
|
144
|
+
label: () => string | undefined;
|
|
145
|
+
active: () => boolean;
|
|
146
|
+
};
|
|
147
|
+
promoteDirect: (opts?: PromoteDirectOpts) => Promise<RouteOpResult>;
|
|
148
|
+
reinterposeRelay: (reason?: unknown) => Promise<RouteOpResult>;
|
|
149
|
+
fallback: (reason?: unknown) => Promise<RouteOpResult>;
|
|
150
|
+
block: (reason?: unknown) => Promise<RouteOpResult>;
|
|
151
|
+
close: () => void;
|
|
152
|
+
} | RoutePairRef | string, opts?: PromoteDirectOpts) => Promise<RouteOpResult>;
|
|
153
|
+
reinterposeRelay: (pairOrKey: {
|
|
154
|
+
ref: RoutePairRef;
|
|
155
|
+
state: () => tRouteState;
|
|
156
|
+
reason: () => unknown;
|
|
157
|
+
label: () => string;
|
|
158
|
+
metrics: () => {
|
|
159
|
+
relay: {
|
|
160
|
+
rtt?: number;
|
|
161
|
+
pending?: number;
|
|
162
|
+
state: tConnectorState;
|
|
163
|
+
} | null;
|
|
164
|
+
direct: {
|
|
165
|
+
rtt?: number;
|
|
166
|
+
pending?: number;
|
|
167
|
+
state: tConnectorState;
|
|
168
|
+
} | null;
|
|
169
|
+
};
|
|
170
|
+
subscribe: (cb: Listener<Z>, opts?: Omit<ReplayRouteSubscribeOpts, "label">) => (() => void) & {
|
|
171
|
+
ready: Promise<void>;
|
|
172
|
+
seq: () => number;
|
|
173
|
+
label: () => string | undefined;
|
|
174
|
+
active: () => boolean;
|
|
175
|
+
};
|
|
176
|
+
promoteDirect: (opts?: PromoteDirectOpts) => Promise<RouteOpResult>;
|
|
177
|
+
reinterposeRelay: (reason?: unknown) => Promise<RouteOpResult>;
|
|
178
|
+
fallback: (reason?: unknown) => Promise<RouteOpResult>;
|
|
179
|
+
block: (reason?: unknown) => Promise<RouteOpResult>;
|
|
180
|
+
close: () => void;
|
|
181
|
+
} | RoutePairRef | string, reason?: unknown) => Promise<RouteOpResult>;
|
|
182
|
+
fallback: (pairOrKey: {
|
|
183
|
+
ref: RoutePairRef;
|
|
184
|
+
state: () => tRouteState;
|
|
185
|
+
reason: () => unknown;
|
|
186
|
+
label: () => string;
|
|
187
|
+
metrics: () => {
|
|
188
|
+
relay: {
|
|
189
|
+
rtt?: number;
|
|
190
|
+
pending?: number;
|
|
191
|
+
state: tConnectorState;
|
|
192
|
+
} | null;
|
|
193
|
+
direct: {
|
|
194
|
+
rtt?: number;
|
|
195
|
+
pending?: number;
|
|
196
|
+
state: tConnectorState;
|
|
197
|
+
} | null;
|
|
198
|
+
};
|
|
199
|
+
subscribe: (cb: Listener<Z>, opts?: Omit<ReplayRouteSubscribeOpts, "label">) => (() => void) & {
|
|
200
|
+
ready: Promise<void>;
|
|
201
|
+
seq: () => number;
|
|
202
|
+
label: () => string | undefined;
|
|
203
|
+
active: () => boolean;
|
|
204
|
+
};
|
|
205
|
+
promoteDirect: (opts?: PromoteDirectOpts) => Promise<RouteOpResult>;
|
|
206
|
+
reinterposeRelay: (reason?: unknown) => Promise<RouteOpResult>;
|
|
207
|
+
fallback: (reason?: unknown) => Promise<RouteOpResult>;
|
|
208
|
+
block: (reason?: unknown) => Promise<RouteOpResult>;
|
|
209
|
+
close: () => void;
|
|
210
|
+
} | RoutePairRef | string, reason?: unknown) => Promise<RouteOpResult>;
|
|
211
|
+
block: (pairOrKey: {
|
|
212
|
+
ref: RoutePairRef;
|
|
213
|
+
state: () => tRouteState;
|
|
214
|
+
reason: () => unknown;
|
|
215
|
+
label: () => string;
|
|
216
|
+
metrics: () => {
|
|
217
|
+
relay: {
|
|
218
|
+
rtt?: number;
|
|
219
|
+
pending?: number;
|
|
220
|
+
state: tConnectorState;
|
|
221
|
+
} | null;
|
|
222
|
+
direct: {
|
|
223
|
+
rtt?: number;
|
|
224
|
+
pending?: number;
|
|
225
|
+
state: tConnectorState;
|
|
226
|
+
} | null;
|
|
227
|
+
};
|
|
228
|
+
subscribe: (cb: Listener<Z>, opts?: Omit<ReplayRouteSubscribeOpts, "label">) => (() => void) & {
|
|
229
|
+
ready: Promise<void>;
|
|
230
|
+
seq: () => number;
|
|
231
|
+
label: () => string | undefined;
|
|
232
|
+
active: () => boolean;
|
|
233
|
+
};
|
|
234
|
+
promoteDirect: (opts?: PromoteDirectOpts) => Promise<RouteOpResult>;
|
|
235
|
+
reinterposeRelay: (reason?: unknown) => Promise<RouteOpResult>;
|
|
236
|
+
fallback: (reason?: unknown) => Promise<RouteOpResult>;
|
|
237
|
+
block: (reason?: unknown) => Promise<RouteOpResult>;
|
|
238
|
+
close: () => void;
|
|
239
|
+
} | RoutePairRef | string, reason?: unknown) => Promise<RouteOpResult>;
|
|
240
|
+
onRoute: (cb: (ev: RouteChangeEvent) => void) => import("./Listen").ListenOff;
|
|
241
|
+
pairs: () => {
|
|
242
|
+
ref: RoutePairRef;
|
|
243
|
+
state: () => tRouteState;
|
|
244
|
+
reason: () => unknown;
|
|
245
|
+
label: () => string;
|
|
246
|
+
metrics: () => {
|
|
247
|
+
relay: {
|
|
248
|
+
rtt?: number;
|
|
249
|
+
pending?: number;
|
|
250
|
+
state: tConnectorState;
|
|
251
|
+
} | null;
|
|
252
|
+
direct: {
|
|
253
|
+
rtt?: number;
|
|
254
|
+
pending?: number;
|
|
255
|
+
state: tConnectorState;
|
|
256
|
+
} | null;
|
|
257
|
+
};
|
|
258
|
+
subscribe: (cb: Listener<Z>, opts?: Omit<ReplayRouteSubscribeOpts, "label">) => (() => void) & {
|
|
259
|
+
ready: Promise<void>;
|
|
260
|
+
seq: () => number;
|
|
261
|
+
label: () => string | undefined;
|
|
262
|
+
active: () => boolean;
|
|
263
|
+
};
|
|
264
|
+
promoteDirect: (opts?: PromoteDirectOpts) => Promise<RouteOpResult>;
|
|
265
|
+
reinterposeRelay: (reason?: unknown) => Promise<RouteOpResult>;
|
|
266
|
+
fallback: (reason?: unknown) => Promise<RouteOpResult>;
|
|
267
|
+
block: (reason?: unknown) => Promise<RouteOpResult>;
|
|
268
|
+
close: () => void;
|
|
269
|
+
}[];
|
|
270
|
+
close(): void;
|
|
271
|
+
};
|
|
272
|
+
export type RouteCoordinator<Z extends any[] = any[]> = ReturnType<typeof createRouteCoordinator<Z>>;
|
|
273
|
+
export type RouteLink<Z extends any[] = any[]> = ReturnType<RouteCoordinator<Z>['pair']>;
|
|
274
|
+
export {};
|