wenay-common2 1.0.68 β 1.0.69
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/README.md +2 -0
- package/doc/ROADMAP.md +9 -3
- package/doc/changes/1.0.69.md +11 -0
- package/doc/wenay-common2-rare.md +8 -0
- package/doc/wenay-common2.md +42 -6
- package/lib/Common/events/route-signal-webrtc.js +4 -2
- package/lib/Common/peer/peer-client.d.ts +46 -0
- package/lib/Common/peer/peer-client.js +108 -0
- package/lib/Common/peer/peer-host.d.ts +32 -0
- package/lib/Common/peer/peer-host.js +45 -0
- package/lib/Common/peer/peer-index.d.ts +3 -0
- package/lib/Common/peer/peer-index.js +19 -0
- package/lib/Common/peer/peer-relay.d.ts +14 -0
- package/lib/Common/peer/peer-relay.js +77 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +2 -1
- package/package.json +3 -2
package/README.md
CHANGED
package/doc/ROADMAP.md
CHANGED
|
@@ -5,7 +5,10 @@
|
|
|
5
5
|
> `createStoreMirror`, and the replay `seq` / `keyframe` / `frame` contract. None of it requires
|
|
6
6
|
> changing the store core; the store stays single-authority (one `seq` sequencer, last-writer-wins
|
|
7
7
|
> per path), and these features are layers or adapters above that.
|
|
8
|
-
> Status: π΄ not started Β· π‘ partial / ongoing Β· π§
|
|
8
|
+
> Status: π΄ not started Β· π‘ partial / ongoing Β· π§ deferred (super-low priority, not forbidden).
|
|
9
|
+
>
|
|
10
|
+
> Current focus is NOT more transport: see `doc/target/library-uplift-tasks.md` (showcase, SDK facade,
|
|
11
|
+
> vertical demo app). Transport items below stay available but rank below everything in that plan.
|
|
9
12
|
|
|
10
13
|
## 0. Distributed Runtime Model
|
|
11
14
|
|
|
@@ -195,7 +198,9 @@ source of truth. Decompose by write topology:
|
|
|
195
198
|
silently drop a concurrent write. This is the one case that genuinely needs CRDT/OT. Design lead:
|
|
196
199
|
wrap a Yjs/Automerge doc as a `RemoteStore`-shaped source and mirror from it β the storeβtransport
|
|
197
200
|
decoupling makes this a small adapter, not a rewrite.
|
|
198
|
-
- Status:
|
|
201
|
+
- Status: π§ deferred, super-low priority. Prediction layer waits for the demo app to demand and shape
|
|
202
|
+
it (`doc/target/library-uplift-tasks.md` task 4); CRDT adapter reopens only on a real co-write need.
|
|
203
|
+
Partitioned-authority already expressible today.
|
|
199
204
|
|
|
200
205
|
## 4. Data-transfer optimization backlog (ongoing) π‘
|
|
201
206
|
|
|
@@ -203,4 +208,5 @@ Open-ended transfer/perf work, especially for backend-heavy models. Never "done"
|
|
|
203
208
|
|
|
204
209
|
- Candidates: tighter `frame` condensation per line; delta/patch minimization; binary framing for hot
|
|
205
210
|
paths; batching heuristics beyond the current `pipe` / `space` modes.
|
|
206
|
-
- Status:
|
|
211
|
+
- Status: π§ deferred, super-low priority; pick items only as real bottlenecks surface in the SDK/demo
|
|
212
|
+
consumers, not speculatively.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# 1.0.69
|
|
2
|
+
|
|
3
|
+
- Fix `createWebRtcConnector` / `acceptWebRtcDirect`: `RTCIceCandidate` now rides the signaling wire as its `toJSON()` init β real browser candidate class instances were mangled by transport serialization, so direct never established (caught by the live demo stand, invisible to fakes).
|
|
4
|
+
- Add the demo stand `demo/` (`npm run demo`): shared cursors in two browser tabs over the Peer SDK next to a legacy rpc key β relay mirroring, "Go direct" (real `RTCPeerConnection`), "Back to relay" re-interposition, signaling/route event log. Verified live in Chrome: seq continues across both hand-offs, no keyframe reset, cursor never jumps.
|
|
5
|
+
|
|
6
|
+
- Add the `Peer` namespace (`wenay-common2/peer`) β the one-call SDK over rpc + store + replay + route coordinator, legacy-friendly by design: the server side is an object fragment spread into an EXISTING `createRpcServerAuto` object (old keys untouched), the client side rides the existing connection's deep proxy.
|
|
7
|
+
- `Peer.createPeerHost({authorize?, history?})`: per-account signaling ports + relay journals + a `noStrict` dynamic `peers` feed; `authorize` is the server-side `canExposeEndpoint`; `revoke(pair, accounts)` pushes both parties back to relay.
|
|
8
|
+
- `Peer.createPeerClient({remote, account, initial, rtc?, ...})`: own store published as a patch line; `peer(account)` returns a live mirrored store + route control (`promoteDirect/reinterposeRelay/fallback/block`, `route()`, `ready`, `seq()`); mirrors survive any route change; omitting `rtc` gives a relay-only client.
|
|
9
|
+
- `Peer.createPatchRelayJournal({history?})`: server-side journal of OWNER-sequenced envelopes β relay and direct share the owner's seq space, so a relay <-> direct hand-off is a plain seq resume (no keyframe reset); keyframe is folded server-side, late joiners work while the owner is offline.
|
|
10
|
+
- Add GitHub Actions CI (build + publish gate + full oracle suite on windows-latest) and a README badge.
|
|
11
|
+
- Add oracle `replay/peer-sdk.test.ts`: two accounts over a real Socket.IO/RPC wire next to a legacy rpc key β relay mirroring, direct promotion via injected fake WebRTC runtime, same-seq-space hand-off assertion, server revoke fallback, late joiner from the folded keyframe, loud refusal of `promoteDirect` without an `rtc` factory.
|
|
@@ -529,6 +529,14 @@ acceptWebRtcDirect({port, rtc, self, serve, accept?}) -> close()
|
|
|
529
529
|
// responder side: on offer, negotiates answer/ICE and serves serve(env) (exposeReplay(...) as is) into
|
|
530
530
|
// the incoming datachannel; accept(env) validates session material and rejects with a loud revoke
|
|
531
531
|
// (the initiator fails fast, not by timeout). Repeated offer for a pair recreates the session.
|
|
532
|
+
Peer.createPatchRelayJournal({history?}) -> {push(env), remote, seq(), snapshot(), close()}
|
|
533
|
+
// server-side mirror of an OWNER-sequenced patch line: push() takes the owner's envelopes VERBATIM
|
|
534
|
+
// (dedup by seq; a root patch with a LOWER seq = owner restart, legitimate reset point), keyframe is
|
|
535
|
+
// folded server-side (late joiners don't need the owner online), frame condenses last-patch-per-path.
|
|
536
|
+
// Owner seq space is the point: relay and direct routes share coordinates -> hand-off is a seq resume.
|
|
537
|
+
// remote is ReplayRemote-shaped and rpc-exposable as is (line is a REAL Listen β the rpc layer detects
|
|
538
|
+
// listen nodes by registry, a hand-rolled {on: cb => ...} wrapper would not stream).
|
|
539
|
+
// The full SDK on top (createPeerHost/createPeerClient) is most-used surface -> wenay-common2.md.
|
|
532
540
|
serveReplayChannel(source, channel) <-> channelReplayRemote(channel) -> ReplayRemote
|
|
533
541
|
// replay wire over ANY ordered string channel (datachannel/MessagePort/worker/pipe): tiny JSON
|
|
534
542
|
// sub/req/res protocol, no RPC core β a direct channel lives OUTSIDE the main rpc connection.
|
package/doc/wenay-common2.md
CHANGED
|
@@ -136,8 +136,8 @@ l.ticks.once(v => console.log(v)) // one event, then aut
|
|
|
136
136
|
const [tick, ticks] = replayListen<[number]>({history: 1024, current: 'last'}) // after β same facade, same key
|
|
137
137
|
// legacy subscribers unchanged (byte-for-byte). Replay consumers now also get:
|
|
138
138
|
const sub = replaySubscribe(l.ticks, v => {}, {since: saved, onSeq: s => saved = s}) // catch-up + live, no gaps/dups
|
|
139
|
-
const sub2 = replaySubscribe(c.math.func.ticks, v => {}) // replay members project on func/strict directly β no cast needed
|
|
140
|
-
const routed = replayRouteSubscribe(l.ticks, v => {}, {label: 'relay'})
|
|
139
|
+
const sub2 = replaySubscribe(c.math.func.ticks, v => {}) // replay members project on func/strict directly β no cast needed
|
|
140
|
+
const routed = replayRouteSubscribe(l.ticks, v => {}, {label: 'relay'})
|
|
141
141
|
await routed.switch(nextRemoteTicks, {label: 'direct'}) // relay/direct hand-off: old route closes after catch-up
|
|
142
142
|
await l.ticks.frame(mySeq) // pull at YOUR pace (50ms timer etc.) β server condenses via the line's frame lambda
|
|
143
143
|
// full guide + examples β rpc.md; frame model / lag policies β ποΈ recipe below and rare docs
|
|
@@ -152,8 +152,44 @@ Media.createVideoSource({fps? = 3, codec? = 'jpeg', quality?, replay?}) -> [emit
|
|
|
152
152
|
control: start() -> Promise<'idle'|'requesting'|'live'|'denied'|'no-device'|'error'> Β· stop() Β· getStats() Β· setDevice(id) Β· listDevices() Β· state
|
|
153
153
|
Media.encodeMediaFrame(meta, payload) / Media.decodeMediaFrame(frame) // one Uint8Array = 40-byte fixed header + raw payload
|
|
154
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
|
-
|
|
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
|
+
|
|
157
|
+
## π€ Peer β accounts see each other's stores (one-call SDK)
|
|
158
|
+
> `import { Peer } from "wenay-common2"` or `import * as Peer from "wenay-common2/peer"`.
|
|
159
|
+
> The happy-path facade over rpc + store + replay + route coordinator. Legacy-friendly by design:
|
|
160
|
+
> the server side is a FRAGMENT spread into your EXISTING `createRpcServerAuto` object, the client
|
|
161
|
+
> side rides your existing connection β old keys keep working untouched.
|
|
162
|
+
```
|
|
163
|
+
// SERVER β next to your legacy object:
|
|
164
|
+
const host = Peer.createPeerHost({authorize?, history?}) // authorize(env) = server-side canExposeEndpoint
|
|
165
|
+
io.on('connection', socket => {
|
|
166
|
+
const peer = host.connection(accountOf(socket)) // per-account signal port + relay journal
|
|
167
|
+
createRpcServerAuto({socket, socketKey, object: {...legacyObject, peer: peer.fragment}, disconnectListen})
|
|
168
|
+
disconnectListen.on(peer.close)
|
|
169
|
+
})
|
|
170
|
+
host: connection(account) Β· relay(account) Β· accounts() Β· revoke(pair, accounts, reason?) Β· close()
|
|
171
|
+
|
|
172
|
+
// CLIENT β the whole happy path:
|
|
173
|
+
const me = Peer.createPeerClient<World>({
|
|
174
|
+
remote: c.app.func.peer, // deep proxy of the fragment β rest of the connection is yours
|
|
175
|
+
account: 'a',
|
|
176
|
+
initial: {...}, // own store: write me.store.state β others see it
|
|
177
|
+
rtc?: () => new RTCPeerConnection(cfg), // omit = relay-only (promoteDirect unavailable)
|
|
178
|
+
})
|
|
179
|
+
const bob = me.peer('b') // mirror + route control for another account
|
|
180
|
+
await bob.ready // keyframe/tail landed
|
|
181
|
+
bob.store.state // live mirror β reads survive ANY route change
|
|
182
|
+
await bob.promoteDirect() // relay -> WebRTC direct; {ok, state, reason?} result, not a throw
|
|
183
|
+
bob.route() // 'relay' | 'direct' Β· bob.reinterposeRelay() Β· bob.fallback() Β· bob.block()
|
|
184
|
+
me.onRoute(ev => {}) // route transitions for metrics/UI
|
|
185
|
+
```
|
|
186
|
+
Key property: the relay journal stores the owner's envelopes VERBATIM (owner seq space), so a
|
|
187
|
+
relay <-> direct hand-off is a plain seq resume β no keyframe reset, no gaps, no dups. Late joiners
|
|
188
|
+
get a keyframe folded server-side even while the owner is offline. Policy/session material:
|
|
189
|
+
`createPeerClient({session, accept, policy})` + host `authorize` β see rare docs for the envelope
|
|
190
|
+
contract and the underlying primitives (`createRouteCoordinator`, `createSignalHub`,
|
|
191
|
+
`createWebRtcConnector`). Oracle: `replay/peer-sdk.test.ts`.
|
|
192
|
+
|
|
157
193
|
## π Observe β reactive state + store/mirror API
|
|
158
194
|
> `import { Observe } from "wenay-common2"` or `import * as Observe from "wenay-common2/observe"`.
|
|
159
195
|
> This is the documented v2 reactive/store surface.
|
|
@@ -199,8 +235,8 @@ Observe.exposeStoreReplay(store, {history? = 1024}) -> { api /* spread into the
|
|
|
199
235
|
Observe.syncStoreReplay(mirror, remote /*{line, since, keyframe, frame?} of api.replay*/, {since?, onSeq?}) -> off
|
|
200
236
|
// off.ready (catch-up done) Β· off.seq() (save for reconnect: syncStoreReplay(..., {since: prev.seq()}))
|
|
201
237
|
// lagging/late client NEVER gets a backlog: evicted seq -> ONE fresh keyframe + live
|
|
202
|
-
// freshness is an option, not consumer boilerplate: {staleMs, onStale} flags a silent line / stale keyframe (edge-triggered both ways; ποΈ in rare docs)
|
|
203
|
-
Observe.syncStoreReplayRoute(mirror, remote, {label?}) -> off & {switch(nextRemote, opts), ready, seq(), label(), active()}
|
|
238
|
+
// freshness is an option, not consumer boilerplate: {staleMs, onStale} flags a silent line / stale keyframe (edge-triggered both ways; ποΈ in rare docs)
|
|
239
|
+
Observe.syncStoreReplayRoute(mirror, remote, {label?}) -> off & {switch(nextRemote, opts), ready, seq(), label(), active()}
|
|
204
240
|
// relay/direct promotion and re-interposition: replacement route catches up by seq before the old route closes
|
|
205
241
|
Observe.syncStoreReplayEach<T>(remote, (key, value, ctx) => {}, opts?) -> off & {store, ready, seq(), isStale(), lastTs()}
|
|
206
242
|
// one-call remote fold: mirror store + syncStoreReplay + store.each() β the callback fires per CHANGED
|
|
@@ -130,7 +130,8 @@ function createWebRtcConnector(deps) {
|
|
|
130
130
|
});
|
|
131
131
|
me.onicecandidate = function onIce(ev) {
|
|
132
132
|
if (ev?.candidate != null) {
|
|
133
|
-
|
|
133
|
+
const c = ev.candidate;
|
|
134
|
+
void port.send({ type: 'ice', pair, from: self, to: peer, candidate: c?.toJSON ? c.toJSON() : c });
|
|
134
135
|
}
|
|
135
136
|
};
|
|
136
137
|
try {
|
|
@@ -202,7 +203,8 @@ function acceptWebRtcDirect(deps) {
|
|
|
202
203
|
};
|
|
203
204
|
pc.onicecandidate = function onIce(ev) {
|
|
204
205
|
if (ev?.candidate != null) {
|
|
205
|
-
|
|
206
|
+
const c = ev.candidate;
|
|
207
|
+
void port.send({ type: 'ice', pair: env.pair, from: self, to: env.from, candidate: c?.toJSON ? c.toJSON() : c });
|
|
206
208
|
}
|
|
207
209
|
};
|
|
208
210
|
try {
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { StoreDrain, StorePatch } from '../Observe/store';
|
|
2
|
+
import { ReplayRemote } from '../events/replay-wire';
|
|
3
|
+
import { RoutePolicy } from '../events/route-coordinator';
|
|
4
|
+
import { RtcPeerConnection, SignalEnvelope } from '../events/route-signal-webrtc';
|
|
5
|
+
import { PatchEnvelope } from './peer-relay';
|
|
6
|
+
export type PeerRemote = {
|
|
7
|
+
signal: {
|
|
8
|
+
send: (env: SignalEnvelope) => Promise<boolean | void>;
|
|
9
|
+
signals: {
|
|
10
|
+
on: (cb: (env: SignalEnvelope) => void) => any;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
publish: (env: PatchEnvelope) => Promise<boolean | void> | boolean | void;
|
|
14
|
+
peers: Record<string, ReplayRemote<[StorePatch]>>;
|
|
15
|
+
};
|
|
16
|
+
export type PeerClientDeps<T extends object> = {
|
|
17
|
+
remote: PeerRemote;
|
|
18
|
+
account: string;
|
|
19
|
+
initial: T;
|
|
20
|
+
rtc?: () => RtcPeerConnection;
|
|
21
|
+
session?: unknown;
|
|
22
|
+
accept?: (env: SignalEnvelope) => boolean | Promise<boolean>;
|
|
23
|
+
policy?: RoutePolicy;
|
|
24
|
+
peerInitial?: () => T;
|
|
25
|
+
history?: number;
|
|
26
|
+
drain?: StoreDrain;
|
|
27
|
+
};
|
|
28
|
+
export declare function createPeerClient<T extends object>(deps: PeerClientDeps<T>): {
|
|
29
|
+
store: import("../Observe/store").Store<T>;
|
|
30
|
+
peer: (other: string) => {
|
|
31
|
+
account: string;
|
|
32
|
+
store: import("../Observe/store").Store<T>;
|
|
33
|
+
ready: Promise<void>;
|
|
34
|
+
seq: () => number;
|
|
35
|
+
route: () => string;
|
|
36
|
+
state: () => import("../events/route-coordinator").tRouteState;
|
|
37
|
+
promoteDirect: (opts?: import("../events/route-coordinator").PromoteDirectOpts) => Promise<import("../events/route-coordinator").RouteOpResult>;
|
|
38
|
+
reinterposeRelay: (reason?: unknown) => Promise<import("../events/route-coordinator").RouteOpResult>;
|
|
39
|
+
fallback: (reason?: unknown) => Promise<import("../events/route-coordinator").RouteOpResult>;
|
|
40
|
+
block: (reason?: unknown) => Promise<import("../events/route-coordinator").RouteOpResult>;
|
|
41
|
+
close(): void;
|
|
42
|
+
};
|
|
43
|
+
onRoute: (cb: (ev: import("../events/route-coordinator").RouteChangeEvent) => void) => import("../..").ListenOff;
|
|
44
|
+
close(): void;
|
|
45
|
+
};
|
|
46
|
+
export type PeerClient<T extends object> = ReturnType<typeof createPeerClient<T>>;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPeerClient = createPeerClient;
|
|
4
|
+
const store_1 = require("../Observe/store");
|
|
5
|
+
const store_replay_1 = require("../Observe/store-replay");
|
|
6
|
+
const replay_wire_1 = require("../events/replay-wire");
|
|
7
|
+
const route_coordinator_1 = require("../events/route-coordinator");
|
|
8
|
+
const route_signal_webrtc_1 = require("../events/route-signal-webrtc");
|
|
9
|
+
function createPeerClient(deps) {
|
|
10
|
+
const { remote, account, initial, rtc, session, accept, policy, peerInitial = () => ({}), history, drain, } = deps;
|
|
11
|
+
const store = (0, store_1.createStore)(initial, drain !== undefined ? { drain } : {});
|
|
12
|
+
const exposed = (0, store_replay_1.exposeStoreReplay)(store, history !== undefined ? { history } : {});
|
|
13
|
+
const offPublish = exposed.replay.line.on(function publishEnvelope(env) {
|
|
14
|
+
void remote.publish(env);
|
|
15
|
+
});
|
|
16
|
+
const warmup = exposed.replay.keyframe();
|
|
17
|
+
if (warmup)
|
|
18
|
+
void remote.publish(warmup);
|
|
19
|
+
const port = {
|
|
20
|
+
send: env => remote.signal.send(env),
|
|
21
|
+
signals: { on: cb => remote.signal.signals.on(cb) },
|
|
22
|
+
};
|
|
23
|
+
const stopAccept = rtc
|
|
24
|
+
? (0, route_signal_webrtc_1.acceptWebRtcDirect)({
|
|
25
|
+
port, rtc, self: account,
|
|
26
|
+
serve: () => (0, replay_wire_1.exposeReplay)(exposed.replay),
|
|
27
|
+
...(accept ? { accept } : {}),
|
|
28
|
+
})
|
|
29
|
+
: null;
|
|
30
|
+
function relayConnector(other) {
|
|
31
|
+
let state = 'idle';
|
|
32
|
+
return {
|
|
33
|
+
info: { label: 'relay', kind: 'relay', ordered: true, reliable: true },
|
|
34
|
+
open() {
|
|
35
|
+
const node = remote.peers[other];
|
|
36
|
+
state = 'open';
|
|
37
|
+
return {
|
|
38
|
+
line: { on: (cb) => node.line.on(cb) },
|
|
39
|
+
since: (seq) => node.since(seq),
|
|
40
|
+
keyframe: () => node.keyframe(),
|
|
41
|
+
frame: (seq, hint) => node.frame(seq, hint),
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
close: () => { state = 'closed'; },
|
|
45
|
+
state: () => state,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const coord = (0, route_coordinator_1.createRouteCoordinator)({
|
|
49
|
+
...(policy ? { policy } : {}),
|
|
50
|
+
connect(ref, kind) {
|
|
51
|
+
const other = ref.a == account ? ref.b : ref.a;
|
|
52
|
+
if (kind == 'relay')
|
|
53
|
+
return relayConnector(other);
|
|
54
|
+
if (!rtc)
|
|
55
|
+
throw new Error('peer client: promoteDirect needs an rtc factory (deps.rtc)');
|
|
56
|
+
return (0, route_signal_webrtc_1.createWebRtcConnector)({
|
|
57
|
+
port, rtc, self: account, peer: other, pair: ref.key, session,
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
const views = new Map();
|
|
62
|
+
function makeView(other) {
|
|
63
|
+
const link = coord.pair(account, other);
|
|
64
|
+
const mirror = (0, store_1.createStore)(peerInitial(), drain !== undefined ? { drain } : {});
|
|
65
|
+
const sub = link.subscribe(function mirrorPatch(patch) {
|
|
66
|
+
(0, store_1.applyStorePatch)(mirror, patch);
|
|
67
|
+
});
|
|
68
|
+
const view = {
|
|
69
|
+
account: other,
|
|
70
|
+
store: mirror,
|
|
71
|
+
ready: sub.ready,
|
|
72
|
+
seq: sub.seq,
|
|
73
|
+
route: link.label,
|
|
74
|
+
state: link.state,
|
|
75
|
+
promoteDirect: link.promoteDirect,
|
|
76
|
+
reinterposeRelay: link.reinterposeRelay,
|
|
77
|
+
fallback: link.fallback,
|
|
78
|
+
block: link.block,
|
|
79
|
+
close() {
|
|
80
|
+
views.delete(other);
|
|
81
|
+
sub();
|
|
82
|
+
link.close();
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
return view;
|
|
86
|
+
}
|
|
87
|
+
function peer(other) {
|
|
88
|
+
const existing = views.get(other);
|
|
89
|
+
if (existing)
|
|
90
|
+
return existing;
|
|
91
|
+
const view = makeView(other);
|
|
92
|
+
views.set(other, view);
|
|
93
|
+
return view;
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
store,
|
|
97
|
+
peer,
|
|
98
|
+
onRoute: coord.onRoute,
|
|
99
|
+
close() {
|
|
100
|
+
for (const view of Array.from(views.values()))
|
|
101
|
+
view.close();
|
|
102
|
+
coord.close();
|
|
103
|
+
stopAccept?.();
|
|
104
|
+
offPublish();
|
|
105
|
+
exposed.close();
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { StorePatch } from '../Observe/store';
|
|
2
|
+
import { ReplayRemote } from '../events/replay-wire';
|
|
3
|
+
import { SignalEnvelope } from '../events/route-signal-webrtc';
|
|
4
|
+
import { PatchEnvelope } from './peer-relay';
|
|
5
|
+
export type PeerHostDeps = {
|
|
6
|
+
authorize?: (env: SignalEnvelope) => boolean | Promise<boolean>;
|
|
7
|
+
history?: number;
|
|
8
|
+
};
|
|
9
|
+
export declare function createPeerHost(deps?: PeerHostDeps): {
|
|
10
|
+
connection: (account: string) => {
|
|
11
|
+
fragment: {
|
|
12
|
+
signal: {
|
|
13
|
+
send: (env: SignalEnvelope) => Promise<boolean>;
|
|
14
|
+
signals: import("../..").ListenApi<[SignalEnvelope]>;
|
|
15
|
+
};
|
|
16
|
+
publish: (env: PatchEnvelope) => boolean;
|
|
17
|
+
peers: Record<string, ReplayRemote<[StorePatch]>>;
|
|
18
|
+
};
|
|
19
|
+
close: () => void;
|
|
20
|
+
};
|
|
21
|
+
relay: (account: string) => {
|
|
22
|
+
push: (env: PatchEnvelope) => boolean;
|
|
23
|
+
remote: ReplayRemote<[StorePatch]>;
|
|
24
|
+
seq: () => number;
|
|
25
|
+
snapshot: () => any;
|
|
26
|
+
close: () => void;
|
|
27
|
+
};
|
|
28
|
+
accounts: () => string[];
|
|
29
|
+
revoke: (pair: string, accounts: string[], reason?: string) => void;
|
|
30
|
+
close(): void;
|
|
31
|
+
};
|
|
32
|
+
export type PeerHost = ReturnType<typeof createPeerHost>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPeerHost = createPeerHost;
|
|
4
|
+
const rpc_dynamic_1 = require("../rcp/rpc-dynamic");
|
|
5
|
+
const route_signal_webrtc_1 = require("../events/route-signal-webrtc");
|
|
6
|
+
const peer_relay_1 = require("./peer-relay");
|
|
7
|
+
function createPeerHost(deps = {}) {
|
|
8
|
+
const { authorize, history } = deps;
|
|
9
|
+
const hub = (0, route_signal_webrtc_1.createSignalHub)({ authorize });
|
|
10
|
+
const relays = new Map();
|
|
11
|
+
const peersView = (0, rpc_dynamic_1.noStrict)({});
|
|
12
|
+
function ensureRelay(account) {
|
|
13
|
+
let relay = relays.get(account);
|
|
14
|
+
if (!relay) {
|
|
15
|
+
relay = (0, peer_relay_1.createPatchRelayJournal)({ history });
|
|
16
|
+
relays.set(account, relay);
|
|
17
|
+
peersView[account] = relay.remote;
|
|
18
|
+
}
|
|
19
|
+
return relay;
|
|
20
|
+
}
|
|
21
|
+
function connection(account) {
|
|
22
|
+
const port = hub.register(account);
|
|
23
|
+
const mine = ensureRelay(account);
|
|
24
|
+
return {
|
|
25
|
+
fragment: {
|
|
26
|
+
signal: { send: port.send, signals: port.signals },
|
|
27
|
+
publish: (env) => mine.push(env),
|
|
28
|
+
peers: peersView,
|
|
29
|
+
},
|
|
30
|
+
close: () => port.close(),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
connection,
|
|
35
|
+
relay: ensureRelay,
|
|
36
|
+
accounts: () => Array.from(relays.keys()),
|
|
37
|
+
revoke: hub.revoke,
|
|
38
|
+
close() {
|
|
39
|
+
hub.close();
|
|
40
|
+
for (const relay of relays.values())
|
|
41
|
+
relay.close();
|
|
42
|
+
relays.clear();
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./peer-relay"), exports);
|
|
18
|
+
__exportStar(require("./peer-host"), exports);
|
|
19
|
+
__exportStar(require("./peer-client"), exports);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ReplayEvent } from '../events/replay-listen';
|
|
2
|
+
import { ReplayRemote } from '../events/replay-wire';
|
|
3
|
+
import { StorePatch } from '../Observe/store';
|
|
4
|
+
export type PatchEnvelope = ReplayEvent<[StorePatch]>;
|
|
5
|
+
export declare function createPatchRelayJournal(opts?: {
|
|
6
|
+
history?: number;
|
|
7
|
+
}): {
|
|
8
|
+
push: (env: PatchEnvelope) => boolean;
|
|
9
|
+
remote: ReplayRemote<[StorePatch]>;
|
|
10
|
+
seq: () => number;
|
|
11
|
+
snapshot: () => any;
|
|
12
|
+
close: () => void;
|
|
13
|
+
};
|
|
14
|
+
export type PatchRelayJournal = ReturnType<typeof createPatchRelayJournal>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPatchRelayJournal = createPatchRelayJournal;
|
|
4
|
+
const Listen_1 = require("../events/Listen");
|
|
5
|
+
const store_1 = require("../Observe/store");
|
|
6
|
+
const store_replay_1 = require("../Observe/store-replay");
|
|
7
|
+
function condensePatchTail(tail) {
|
|
8
|
+
const held = new Map();
|
|
9
|
+
for (const ev of tail) {
|
|
10
|
+
const k = (0, store_replay_1.storePatchKey)(ev.event[0]);
|
|
11
|
+
if (k == null)
|
|
12
|
+
return tail;
|
|
13
|
+
held.delete(k);
|
|
14
|
+
held.set(k, ev);
|
|
15
|
+
}
|
|
16
|
+
return Array.from(held.values());
|
|
17
|
+
}
|
|
18
|
+
function createPatchRelayJournal(opts = {}) {
|
|
19
|
+
const { history = 1024 } = opts;
|
|
20
|
+
const [emitLine, line] = (0, Listen_1.listen)();
|
|
21
|
+
const fold = (0, store_1.createStore)({}, { drain: 'micro' });
|
|
22
|
+
let ring = [];
|
|
23
|
+
let last = -1;
|
|
24
|
+
let lastTs = 0;
|
|
25
|
+
let hasState = false;
|
|
26
|
+
function push(env) {
|
|
27
|
+
if (env == null || typeof env.seq != 'number')
|
|
28
|
+
return false;
|
|
29
|
+
const patch = env.event?.[0];
|
|
30
|
+
if (patch == null || !Array.isArray(patch.path))
|
|
31
|
+
return false;
|
|
32
|
+
const isRoot = patch.path.length == 0;
|
|
33
|
+
if (env.seq <= last) {
|
|
34
|
+
if (!isRoot || env.seq == last)
|
|
35
|
+
return false;
|
|
36
|
+
ring = [];
|
|
37
|
+
}
|
|
38
|
+
last = env.seq;
|
|
39
|
+
lastTs = env.ts ?? lastTs;
|
|
40
|
+
hasState = true;
|
|
41
|
+
(0, store_1.applyStorePatch)(fold, patch);
|
|
42
|
+
ring.push(env);
|
|
43
|
+
if (ring.length > history)
|
|
44
|
+
ring.splice(0, ring.length - history);
|
|
45
|
+
emitLine(env);
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
function keyframe() {
|
|
49
|
+
if (!hasState)
|
|
50
|
+
return null;
|
|
51
|
+
return { seq: last, ts: lastTs, event: [{ path: [], exists: true, value: fold.snapshot() }] };
|
|
52
|
+
}
|
|
53
|
+
function since(seq) {
|
|
54
|
+
if (!hasState)
|
|
55
|
+
return null;
|
|
56
|
+
if (seq >= last)
|
|
57
|
+
return [];
|
|
58
|
+
if (!ring.length || seq < ring[0].seq - 1)
|
|
59
|
+
return null;
|
|
60
|
+
return ring.filter(ev => ev.seq > seq);
|
|
61
|
+
}
|
|
62
|
+
function frame(seq, _hint) {
|
|
63
|
+
const tail = since(seq);
|
|
64
|
+
if (tail)
|
|
65
|
+
return condensePatchTail(tail);
|
|
66
|
+
const kf = keyframe();
|
|
67
|
+
return kf ? [kf] : null;
|
|
68
|
+
}
|
|
69
|
+
const remote = { line, since, keyframe, frame };
|
|
70
|
+
return {
|
|
71
|
+
push,
|
|
72
|
+
remote,
|
|
73
|
+
seq: () => last,
|
|
74
|
+
snapshot: () => fold.snapshot(),
|
|
75
|
+
close: () => { line.close(); },
|
|
76
|
+
};
|
|
77
|
+
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -36,7 +36,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
36
36
|
};
|
|
37
37
|
})();
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.Media = exports.Replay = exports.Observe = exports.Color = exports.Time = exports.Params = exports.Bars = exports.Math = void 0;
|
|
39
|
+
exports.Peer = exports.Media = exports.Replay = exports.Observe = exports.Color = exports.Time = exports.Params = exports.Bars = exports.Math = void 0;
|
|
40
40
|
__exportStar(require("./Common/node_console"), exports);
|
|
41
41
|
__exportStar(require("./Common/core/Decorator"), exports);
|
|
42
42
|
__exportStar(require("./Common/core/BaseTypes"), exports);
|
|
@@ -74,3 +74,4 @@ exports.Color = __importStar(require("./Common/Color"));
|
|
|
74
74
|
exports.Observe = __importStar(require("./Common/Observe"));
|
|
75
75
|
exports.Replay = __importStar(require("./Common/events/replay-index"));
|
|
76
76
|
exports.Media = __importStar(require("./Common/media/media-index"));
|
|
77
|
+
exports.Peer = __importStar(require("./Common/peer/peer-index"));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wenay-common2",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.69",
|
|
4
4
|
"description": "Common library",
|
|
5
5
|
"strict": true,
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -35,7 +35,8 @@
|
|
|
35
35
|
"./client": "./lib/client.js",
|
|
36
36
|
"./server": "./lib/server.js",
|
|
37
37
|
"./package.json": "./package.json",
|
|
38
|
-
"./media": "./lib/Common/media/media-index.js"
|
|
38
|
+
"./media": "./lib/Common/media/media-index.js",
|
|
39
|
+
"./peer": "./lib/Common/peer/peer-index.js"
|
|
39
40
|
},
|
|
40
41
|
"dependencies": {
|
|
41
42
|
"express": "^5.2.1"
|