@vielzeug/mesh 26.11.16 → 26.11.18
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/dist/_node.cjs +1 -1
- package/dist/_node.cjs.map +1 -1
- package/dist/_node.d.ts +7 -5
- package/dist/_node.d.ts.map +1 -1
- package/dist/_node.js +73 -78
- package/dist/_node.js.map +1 -1
- package/dist/_wire.cjs +1 -1
- package/dist/_wire.cjs.map +1 -1
- package/dist/_wire.d.ts +8 -13
- package/dist/_wire.d.ts.map +1 -1
- package/dist/_wire.js +2 -13
- package/dist/_wire.js.map +1 -1
- package/dist/guest.cjs +1 -1
- package/dist/guest.cjs.map +1 -1
- package/dist/guest.d.ts.map +1 -1
- package/dist/guest.js +69 -69
- package/dist/guest.js.map +1 -1
- package/dist/host.cjs +1 -1
- package/dist/host.cjs.map +1 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +106 -89
- package/dist/host.js.map +1 -1
- package/dist/mesh.cjs +3 -3
- package/dist/mesh.cjs.map +1 -1
- package/dist/mesh.iife.js +3 -3
- package/dist/mesh.iife.js.map +1 -1
- package/dist/mesh.js +63 -61
- package/dist/mesh.js.map +1 -1
- package/dist/testing/fake-rtc.cjs +1 -1
- package/dist/testing/fake-rtc.cjs.map +1 -1
- package/dist/testing/fake-rtc.d.ts +6 -0
- package/dist/testing/fake-rtc.d.ts.map +1 -1
- package/dist/testing/fake-rtc.js +9 -0
- package/dist/testing/fake-rtc.js.map +1 -1
- package/dist/types.d.ts +6 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/_node.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./
|
|
1
|
+
const e=require("./_wire.cjs"),t=require("./errors.cjs");let n=require("@vielzeug/arsenal");function createNodeCore(){let e=(0,n.tapper)(),r=new AbortController,i={disposalCtrl:r,disposed:!1,emitTap(t){e.emit(t)},ensureLive(){if(i.disposed)throw new t.MeshDisposedError},markDisposed(){i.disposed||(i.disposed=!0,i.status=`disposed`,i.emitTap({status:`disposed`,type:`status-change`}),i.emitTap({type:`dispose`}),e.clear(),r.abort())},setStatus(e){i.disposed||i.status===e||(i.status=e,i.emitTap({status:e,type:`status-change`}))},status:`idle`,tap(t,n){return i.disposed?()=>{}:e.tap(t,n)}};return i}function wireDisposalSignal(e,t){e&&(e.aborted?t():e.addEventListener(`abort`,t,{once:!0}))}function createPeerRecord(e,t,n){let r={dc:null,id:e,name:t,pc:null,publicPeer:{get id(){return r.id},get name(){return r.name},get role(){return r.role},get status(){return r.status}},role:n,status:`connecting`};return r}function createMessenger(r){let i=new Map;function reject(e,t){r.emitTap({peerId:e,reason:t,type:`message-rejected`})}return{clear(){i.clear()},handleInbound(a,o){let s=a.id;if(typeof o!=`string`){reject(s,`malformed`);return}let c=(0,n.utf8Bytes)(o);if(c>r.maxMessageBytes){reject(s,`too-large`);return}let l;try{l=JSON.parse(o)}catch{reject(s,`malformed`);return}let u=e.toEnvelope(l);if(!u){reject(s,`malformed`);return}let d=i.get(u.t);if(!d||d.size===0){reject(s,`unknown-type`);return}r.emitTap({bytes:c,messageType:u.t,peerId:s,type:`message-received`});let f={payload:u.p,peerId:s,sentAt:u.ts};for(let e of d)try{e(f)}catch(e){r.emitTap({error:new t.MeshError(`Message handler threw`,{cause:e}),type:`error`})}},on(e,t){let n=i.get(e);return n||(n=new Set,i.set(e,n)),n.add(t),()=>{n.delete(t),n.size===0&&i.delete(e)}},sendTo(e,i,a){let o=e.dc;if(!o||o.readyState!==`open`||e.status!==`connected`)throw new t.MeshConnectionError(`Peer "${e.id}" is not connected`,e.id);let s={p:a,t:i,ts:r.clock(),v:2},c;try{c=JSON.stringify(s)}catch(e){throw new t.MeshPayloadError(`Failed to serialize message "${i}"`,{cause:e})}let l=(0,n.utf8Bytes)(c);if(l>r.maxMessageBytes)throw new t.MeshPayloadError(`Message "${i}" is ${l} bytes (max ${r.maxMessageBytes})`);try{o.send(c)}catch(n){throw new t.MeshConnectionError(`Failed to send message "${i}"`,e.id,{cause:n})}r.emitTap({bytes:l,messageType:i,peerId:e.id,type:`message-sent`})}}}exports.createMessenger=createMessenger,exports.createNodeCore=createNodeCore,exports.createPeerRecord=createPeerRecord,exports.wireDisposalSignal=wireDisposalSignal;
|
|
2
2
|
//# sourceMappingURL=_node.cjs.map
|
package/dist/_node.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_node.cjs","names":["tapper","MeshDisposedError","MessageDedupe","utf8Bytes","toEnvelope","MeshError","MeshConnectionError","randomId","MeshPayloadError","require__random","require__wire","require_errors","_vielzeug_arsenal","tappers","disposalCtrl","core","event","next","handler","options","signal","record","handlers","peerId","peer","data","bytes","decoded","envelope","set","message","cause","type","dc","payload","text"],"sources":["../src/_node.ts"],"sourcesContent":["import { type RandomSource, tapper, utf8Bytes } from '@vielzeug/arsenal';\nimport { randomId } from './_random';\nimport { MessageDedupe, toEnvelope } from './_wire';\nimport { MeshConnectionError, MeshDisposedError, MeshError, MeshPayloadError } from './errors';\nimport type {\n MeshEvent,\n MeshInbound,\n MeshPeer,\n MeshPeerStatus,\n MeshStatus,\n RTCDataChannelLike,\n RTCPeerConnectionLike,\n Unsubscribe,\n} from './types';\n\ntype Tapper = (event: MeshEvent) => void;\n\n/** Shared lifecycle core: status, tappers, disposal. */\nexport interface NodeCore {\n readonly disposalCtrl: AbortController;\n disposed: boolean;\n emitTap(event: MeshEvent): void;\n ensureLive(): void;\n /** Emits the final status-change + dispose events, clears tappers, aborts. */\n markDisposed(): void;\n setStatus(next: MeshStatus): void;\n status: MeshStatus;\n tap(handler: Tapper, options?: { readonly signal?: AbortSignal }): Unsubscribe;\n}\n\nexport function createNodeCore(signal?: AbortSignal): NodeCore {\n const tappers = tapper<MeshEvent>();\n const disposalCtrl = new AbortController();\n\n const core: NodeCore = {\n disposalCtrl,\n disposed: false,\n\n emitTap(event) {\n tappers.emit(event);\n },\n\n ensureLive() {\n if (core.disposed) throw new MeshDisposedError();\n },\n\n markDisposed() {\n if (core.disposed) return;\n core.disposed = true;\n core.status = 'disposed';\n core.emitTap({ status: 'disposed', type: 'status-change' });\n core.emitTap({ type: 'dispose' });\n tappers.clear();\n disposalCtrl.abort();\n },\n\n setStatus(next) {\n if (core.disposed || core.status === next) return;\n core.status = next;\n core.emitTap({ status: next, type: 'status-change' });\n },\n status: 'idle',\n\n tap(handler, options) {\n if (core.disposed) return () => {};\n return tappers.tap(handler, options);\n },\n };\n\n if (signal) {\n if (signal.aborted) {\n core.markDisposed();\n } else {\n signal.addEventListener('abort', () => core.markDisposed(), { once: true });\n }\n }\n\n return core;\n}\n\n// ─── Peers ───────────────────────────────────────────────────────────────────\n\n/** Internal peer record; `publicPeer` exposes live status through getters. */\nexport interface PeerRecord {\n dc: RTCDataChannelLike | null;\n readonly dedupe: MessageDedupe;\n readonly id: string;\n readonly name: string | undefined;\n pc: RTCPeerConnectionLike | null;\n readonly publicPeer: MeshPeer;\n readonly role: 'host' | 'guest';\n status: MeshPeerStatus;\n}\n\nexport function createPeerRecord(id: string, name: string | undefined, role: 'host' | 'guest'): PeerRecord {\n const record: PeerRecord = {\n dc: null,\n dedupe: new MessageDedupe(),\n id,\n name,\n pc: null,\n publicPeer: {\n get id() {\n return record.id;\n },\n get name() {\n return record.name;\n },\n get role() {\n return record.role;\n },\n get status() {\n return record.status;\n },\n },\n role,\n status: 'connecting',\n };\n return record;\n}\n\n// ─── Messenger ───────────────────────────────────────────────────────────────\n\ntype InboundHandler = (message: MeshInbound<unknown>) => void;\n\nexport interface MessengerOptions {\n readonly clock: () => number;\n readonly emitTap: (event: MeshEvent) => void;\n readonly maxMessageBytes: number;\n readonly random?: RandomSource;\n}\n\nexport interface Messenger {\n clear(): void;\n /** Entry point for `dc` 'message' events. */\n handleInbound(peer: PeerRecord, data: unknown): void;\n on(type: string, handler: InboundHandler): Unsubscribe;\n /** Sends a typed envelope to a peer. Throws `MeshConnectionError`/`MeshPayloadError`. */\n sendTo(peer: PeerRecord, type: string, payload: unknown): void;\n}\n\nexport function createMessenger(options: MessengerOptions): Messenger {\n const handlers = new Map<string, Set<InboundHandler>>();\n\n function reject(peerId: string, reason: 'too-large' | 'duplicate' | 'malformed' | 'unknown-type'): void {\n options.emitTap({ peerId, reason, type: 'message-rejected' });\n }\n\n return {\n clear() {\n handlers.clear();\n },\n\n handleInbound(peer, data) {\n const peerId = peer.id;\n\n if (typeof data !== 'string') {\n reject(peerId, 'malformed');\n return;\n }\n\n const bytes = utf8Bytes(data);\n if (bytes > options.maxMessageBytes) {\n reject(peerId, 'too-large');\n return;\n }\n\n let decoded: unknown;\n try {\n decoded = JSON.parse(data);\n } catch {\n reject(peerId, 'malformed');\n return;\n }\n\n const envelope = toEnvelope(decoded);\n if (!envelope) {\n reject(peerId, 'malformed');\n return;\n }\n\n if (!peer.dedupe.check(envelope.id)) {\n reject(peerId, 'duplicate');\n return;\n }\n\n const set = handlers.get(envelope.t);\n if (!set || set.size === 0) {\n reject(peerId, 'unknown-type');\n return;\n }\n\n options.emitTap({ bytes, messageType: envelope.t, peerId, type: 'message-received' });\n const message: MeshInbound<unknown> = {\n messageId: envelope.id,\n payload: envelope.p,\n peerId,\n sentAt: envelope.ts,\n };\n for (const handler of set) {\n try {\n handler(message);\n } catch (cause) {\n options.emitTap({ error: new MeshError('Message handler threw', { cause }), type: 'error' });\n }\n }\n },\n\n on(type, handler) {\n let set = handlers.get(type);\n if (!set) {\n set = new Set();\n handlers.set(type, set);\n }\n set.add(handler);\n return () => {\n set.delete(handler);\n if (set.size === 0) handlers.delete(type);\n };\n },\n\n sendTo(peer, type, payload) {\n const dc = peer.dc;\n if (!dc || dc.readyState !== 'open' || peer.status !== 'connected') {\n throw new MeshConnectionError(`Peer \"${peer.id}\" is not connected`, peer.id);\n }\n\n const envelope = { id: randomId(options.random), p: payload, t: type, ts: options.clock(), v: 1 as const };\n let text: string;\n try {\n text = JSON.stringify(envelope);\n } catch (cause) {\n throw new MeshPayloadError(`Failed to serialize message \"${type}\"`, { cause });\n }\n\n const bytes = utf8Bytes(text);\n if (bytes > options.maxMessageBytes) {\n throw new MeshPayloadError(`Message \"${type}\" is ${bytes} bytes (max ${options.maxMessageBytes})`);\n }\n\n try {\n dc.send(text);\n } catch (cause) {\n throw new MeshConnectionError(`Failed to send message \"${type}\"`, peer.id, { cause });\n }\n options.emitTap({ bytes, messageType: type, peerId: peer.id, type: 'message-sent' });\n },\n };\n}\n"],"mappings":"A,uHA8BA,SAAgB,eAAe,EAAgC,CAC7D,IAAMa,GAAA,EAAUb,EAAAA,OAAAA,CAAkB,EAC5Bc,EAAe,IAAI,gBAEnBC,EAAiB,CACrB,eACA,SAAU,GAEV,QAAQ,EAAO,CACbF,EAAQ,KAAKG,CAAK,CACpB,EAEA,YAAa,CACX,GAAID,EAAK,SAAU,MAAM,IAAId,EAAAA,iBAC/B,EAEA,cAAe,CACTc,EAAK,WACT,EAAK,SAAW,GAChB,EAAK,OAAS,WACdA,EAAK,QAAQ,CAAE,OAAQ,WAAY,KAAM,eAAgB,CAAC,EAC1DA,EAAK,QAAQ,CAAE,KAAM,SAAU,CAAC,EAChCF,EAAQ,MAAM,EACdC,EAAa,MAAM,EACrB,EAEA,UAAU,EAAM,CACVC,EAAK,UAAYA,EAAK,SAAWE,IACrC,EAAK,OAASA,EACdF,EAAK,QAAQ,CAAE,OAAQE,EAAM,KAAM,eAAgB,CAAC,EACtD,EACA,OAAQ,OAER,IAAI,EAAS,EAAS,CAEpB,OADIF,EAAK,aAAuB,CAAC,EAC1BF,EAAQ,IAAIK,EAASC,CAAO,CACrC,CACF,EAUA,OARIC,IACEA,EAAO,QACTL,EAAK,aAAa,EAElBK,EAAO,iBAAiB,YAAeL,EAAK,aAAa,EAAG,CAAE,KAAM,EAAK,CAAC,GAIvEA,CACT,CAgBA,SAAgB,iBAAiB,EAAY,EAA0B,EAAoC,CACzG,IAAMM,EAAqB,CACzB,GAAI,KACJ,OAAQ,IAAInB,EAAAA,c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EAAAA,oBAAoB,2BAA2B0B,EAAK,GAAIR,EAAK,GAAI,CAAE,OAAM,CAAC,CACtF,CACAL,EAAQ,QAAQ,CAAE,QAAO,YAAaa,EAAM,OAAQR,EAAK,GAAI,KAAM,cAAe,CAAC,CACrF,CACF,CACF,C"}
|
|
1
|
+
{"version":3,"file":"_node.cjs","names":["tapper","MeshDisposedError","utf8Bytes","toEnvelope","MeshError","MeshConnectionError","MeshPayloadError","require__wire","require_errors","_vielzeug_arsenal","tappers","disposalCtrl","core","event","next","handler","options","signal","dispose","record","handlers","peerId","peer","data","bytes","decoded","envelope","set","message","cause","type","dc","payload","text"],"sources":["../src/_node.ts"],"sourcesContent":["import { tapper, utf8Bytes } from '@vielzeug/arsenal';\nimport { toEnvelope } from './_wire';\nimport { MeshConnectionError, MeshDisposedError, MeshError, MeshPayloadError } from './errors';\nimport type {\n MeshEvent,\n MeshInbound,\n MeshPeer,\n MeshPeerStatus,\n MeshStatus,\n RTCDataChannelLike,\n RTCPeerConnectionLike,\n Unsubscribe,\n} from './types';\n\ntype Tapper = (event: MeshEvent) => void;\n\n/** Shared lifecycle core: status, tappers, disposal. */\nexport interface NodeCore {\n readonly disposalCtrl: AbortController;\n disposed: boolean;\n emitTap(event: MeshEvent): void;\n ensureLive(): void;\n /** Emits the final status-change + dispose events, clears tappers, aborts. */\n markDisposed(): void;\n setStatus(next: MeshStatus): void;\n status: MeshStatus;\n tap(handler: Tapper, options?: { readonly signal?: AbortSignal }): Unsubscribe;\n}\n\nexport function createNodeCore(): NodeCore {\n const tappers = tapper<MeshEvent>();\n const disposalCtrl = new AbortController();\n\n const core: NodeCore = {\n disposalCtrl,\n disposed: false,\n\n emitTap(event) {\n tappers.emit(event);\n },\n\n ensureLive() {\n if (core.disposed) throw new MeshDisposedError();\n },\n\n markDisposed() {\n if (core.disposed) return;\n core.disposed = true;\n core.status = 'disposed';\n core.emitTap({ status: 'disposed', type: 'status-change' });\n core.emitTap({ type: 'dispose' });\n tappers.clear();\n disposalCtrl.abort();\n },\n\n setStatus(next) {\n if (core.disposed || core.status === next) return;\n core.status = next;\n core.emitTap({ status: next, type: 'status-change' });\n },\n status: 'idle',\n\n tap(handler, options) {\n if (core.disposed) return () => {};\n return tappers.tap(handler, options);\n },\n };\n\n return core;\n}\n\n/**\n * Wires the `signal` option of a node: abort runs the node's full `dispose()`,\n * so pending invitations, live connections, and handlers are released, not\n * just the status flags. A signal that is already aborted disposes immediately.\n */\nexport function wireDisposalSignal(signal: AbortSignal | undefined, dispose: () => void): void {\n if (!signal) return;\n if (signal.aborted) dispose();\n else signal.addEventListener('abort', dispose, { once: true });\n}\n\n// ─── Peers ───────────────────────────────────────────────────────────────────\n\n/** Internal peer record; `publicPeer` exposes live status through getters. */\nexport interface PeerRecord {\n dc: RTCDataChannelLike | null;\n readonly id: string;\n readonly name: string | undefined;\n pc: RTCPeerConnectionLike | null;\n readonly publicPeer: MeshPeer;\n readonly role: 'host' | 'guest';\n status: MeshPeerStatus;\n}\n\nexport function createPeerRecord(id: string, name: string | undefined, role: 'host' | 'guest'): PeerRecord {\n const record: PeerRecord = {\n dc: null,\n id,\n name,\n pc: null,\n publicPeer: {\n get id() {\n return record.id;\n },\n get name() {\n return record.name;\n },\n get role() {\n return record.role;\n },\n get status() {\n return record.status;\n },\n },\n role,\n status: 'connecting',\n };\n return record;\n}\n\n// ─── Messenger ───────────────────────────────────────────────────────────────\n\ntype InboundHandler = (message: MeshInbound<unknown>) => void;\n\nexport interface MessengerOptions {\n readonly clock: () => number;\n readonly emitTap: (event: MeshEvent) => void;\n readonly maxMessageBytes: number;\n}\n\nexport interface Messenger {\n clear(): void;\n /** Entry point for `dc` 'message' events. */\n handleInbound(peer: PeerRecord, data: unknown): void;\n on(type: string, handler: InboundHandler): Unsubscribe;\n /** Sends a typed envelope to a peer. Throws `MeshConnectionError`/`MeshPayloadError`. */\n sendTo(peer: PeerRecord, type: string, payload: unknown): void;\n}\n\nexport function createMessenger(options: MessengerOptions): Messenger {\n const handlers = new Map<string, Set<InboundHandler>>();\n\n function reject(peerId: string, reason: 'too-large' | 'malformed' | 'unknown-type'): void {\n options.emitTap({ peerId, reason, type: 'message-rejected' });\n }\n\n return {\n clear() {\n handlers.clear();\n },\n\n handleInbound(peer, data) {\n const peerId = peer.id;\n\n if (typeof data !== 'string') {\n reject(peerId, 'malformed');\n return;\n }\n\n const bytes = utf8Bytes(data);\n if (bytes > options.maxMessageBytes) {\n reject(peerId, 'too-large');\n return;\n }\n\n let decoded: unknown;\n try {\n decoded = JSON.parse(data);\n } catch {\n reject(peerId, 'malformed');\n return;\n }\n\n const envelope = toEnvelope(decoded);\n if (!envelope) {\n reject(peerId, 'malformed');\n return;\n }\n\n const set = handlers.get(envelope.t);\n if (!set || set.size === 0) {\n reject(peerId, 'unknown-type');\n return;\n }\n\n options.emitTap({ bytes, messageType: envelope.t, peerId, type: 'message-received' });\n const message: MeshInbound<unknown> = {\n payload: envelope.p,\n peerId,\n sentAt: envelope.ts,\n };\n for (const handler of set) {\n try {\n handler(message);\n } catch (cause) {\n options.emitTap({ error: new MeshError('Message handler threw', { cause }), type: 'error' });\n }\n }\n },\n\n on(type, handler) {\n let set = handlers.get(type);\n if (!set) {\n set = new Set();\n handlers.set(type, set);\n }\n set.add(handler);\n return () => {\n set.delete(handler);\n if (set.size === 0) handlers.delete(type);\n };\n },\n\n sendTo(peer, type, payload) {\n const dc = peer.dc;\n if (!dc || dc.readyState !== 'open' || peer.status !== 'connected') {\n throw new MeshConnectionError(`Peer \"${peer.id}\" is not connected`, peer.id);\n }\n\n const envelope = { p: payload, t: type, ts: options.clock(), v: 2 as const };\n let text: string;\n try {\n text = JSON.stringify(envelope);\n } catch (cause) {\n throw new MeshPayloadError(`Failed to serialize message \"${type}\"`, { cause });\n }\n\n const bytes = utf8Bytes(text);\n if (bytes > options.maxMessageBytes) {\n throw new MeshPayloadError(`Message \"${type}\" is ${bytes} bytes (max ${options.maxMessageBytes})`);\n }\n\n try {\n dc.send(text);\n } catch (cause) {\n throw new MeshConnectionError(`Failed to send message \"${type}\"`, peer.id, { cause });\n }\n options.emitTap({ bytes, messageType: type, peerId: peer.id, type: 'message-sent' });\n },\n };\n}\n"],"mappings":"A,4FA6BA,SAAgB,gBAA2B,CACzC,IAAMU,GAAA,EAAUV,EAAAA,OAAAA,CAAkB,EAC5BW,EAAe,IAAI,gBAEnBC,EAAiB,CACrB,eACA,SAAU,GAEV,QAAQ,EAAO,CACbF,EAAQ,KAAKG,CAAK,CACpB,EAEA,YAAa,CACX,GAAID,EAAK,SAAU,MAAM,IAAIX,EAAAA,iBAC/B,EAEA,cAAe,CACTW,EAAK,WACT,EAAK,SAAW,GAChB,EAAK,OAAS,WACdA,EAAK,QAAQ,CAAE,OAAQ,WAAY,KAAM,eAAgB,CAAC,EAC1DA,EAAK,QAAQ,CAAE,KAAM,SAAU,CAAC,EAChCF,EAAQ,MAAM,EACdC,EAAa,MAAM,EACrB,EAEA,UAAU,EAAM,CACVC,EAAK,UAAYA,EAAK,SAAWE,IACrC,EAAK,OAASA,EACdF,EAAK,QAAQ,CAAE,OAAQE,EAAM,KAAM,eAAgB,CAAC,EACtD,EACA,OAAQ,OAER,IAAI,EAAS,EAAS,CAEpB,OADIF,EAAK,aAAuB,CAAC,EAC1BF,EAAQ,IAAIK,EAASC,CAAO,CACrC,CACF,EAEA,OAAOJ,CACT,CAOA,SAAgB,mBAAmB,EAAiC,EAA2B,CACxFK,IACDA,EAAO,QAASC,EAAQ,EACvBD,EAAO,iBAAiB,QAASC,EAAS,CAAE,KAAM,EAAK,CAAC,EAC/D,CAeA,SAAgB,iBAAiB,EAAY,EAA0B,EAAoC,CACzG,IAAMC,EAAqB,CACzB,GAAI,KACJ,KACA,OACA,GAAI,KACJ,WAAY,CACV,IAAI,IAAK,CACP,OAAOA,EAAO,EAChB,EACA,IAAI,MAAO,CACT,OAAOA,EAAO,IAChB,EACA,IAAI,MAAO,CACT,OAAOA,EAAO,IAChB,EACA,IAAI,QAAS,CACX,OAAOA,E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uB,EAAMP,EAAS,IAAIU,CAAI,EAM3B,OALKH,IACH,EAAM,IAAI,IACVP,EAAS,IAAIU,EAAMH,CAAG,GAExBA,EAAI,IAAIZ,CAAO,MACF,CACXY,EAAI,OAAOZ,CAAO,EACdY,EAAI,OAAS,GAAGP,EAAS,OAAOU,CAAI,CAC1C,CACF,EAEA,OAAO,EAAM,EAAM,EAAS,CAC1B,IAAMC,EAAKT,EAAK,GAChB,GAAI,CAACS,GAAMA,EAAG,aAAe,QAAUT,EAAK,SAAW,YACrD,MAAM,IAAIjB,EAAAA,oBAAoB,SAASiB,EAAK,GAAG,oBAAqBA,EAAK,EAAE,EAG7E,IAAMI,EAAW,CAAE,EAAGM,EAAS,EAAGF,EAAM,GAAId,EAAQ,MAAM,EAAG,EAAG,CAAW,EACvEiB,EACJ,GAAI,CACF,EAAO,KAAK,UAAUP,CAAQ,CAChC,OAASG,EAAO,CACd,MAAM,IAAIvB,EAAAA,iBAAiB,gCAAgCwB,EAAK,GAAI,CAAE,OAAM,CAAC,CAC/E,CAEA,IAAMN,GAAA,EAAQtB,EAAAA,UAAAA,CAAU+B,CAAI,EAC5B,GAAIT,EAAQR,EAAQ,gBAClB,MAAM,IAAIV,EAAAA,iBAAiB,YAAYwB,EAAK,OAAON,EAAM,cAAcR,EAAQ,gBAAgB,EAAE,EAGnG,GAAI,CACFe,EAAG,KAAKE,CAAI,CACd,OAASJ,EAAO,CACd,MAAM,IAAIxB,EAAAA,oBAAoB,2BAA2ByB,EAAK,GAAIR,EAAK,GAAI,CAAE,OAAM,CAAC,CACtF,CACAN,EAAQ,QAAQ,CAAE,QAAO,YAAac,EAAM,OAAQR,EAAK,GAAI,KAAM,cAAe,CAAC,CACrF,CACF,CACF,C"}
|
package/dist/_node.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { type RandomSource } from '@vielzeug/arsenal';
|
|
2
|
-
import { MessageDedupe } from './_wire';
|
|
3
1
|
import type { MeshEvent, MeshInbound, MeshPeer, MeshPeerStatus, MeshStatus, RTCDataChannelLike, RTCPeerConnectionLike, Unsubscribe } from './types';
|
|
4
2
|
type Tapper = (event: MeshEvent) => void;
|
|
5
3
|
/** Shared lifecycle core: status, tappers, disposal. */
|
|
@@ -16,11 +14,16 @@ export interface NodeCore {
|
|
|
16
14
|
readonly signal?: AbortSignal;
|
|
17
15
|
}): Unsubscribe;
|
|
18
16
|
}
|
|
19
|
-
export declare function createNodeCore(
|
|
17
|
+
export declare function createNodeCore(): NodeCore;
|
|
18
|
+
/**
|
|
19
|
+
* Wires the `signal` option of a node: abort runs the node's full `dispose()`,
|
|
20
|
+
* so pending invitations, live connections, and handlers are released, not
|
|
21
|
+
* just the status flags. A signal that is already aborted disposes immediately.
|
|
22
|
+
*/
|
|
23
|
+
export declare function wireDisposalSignal(signal: AbortSignal | undefined, dispose: () => void): void;
|
|
20
24
|
/** Internal peer record; `publicPeer` exposes live status through getters. */
|
|
21
25
|
export interface PeerRecord {
|
|
22
26
|
dc: RTCDataChannelLike | null;
|
|
23
|
-
readonly dedupe: MessageDedupe;
|
|
24
27
|
readonly id: string;
|
|
25
28
|
readonly name: string | undefined;
|
|
26
29
|
pc: RTCPeerConnectionLike | null;
|
|
@@ -34,7 +37,6 @@ export interface MessengerOptions {
|
|
|
34
37
|
readonly clock: () => number;
|
|
35
38
|
readonly emitTap: (event: MeshEvent) => void;
|
|
36
39
|
readonly maxMessageBytes: number;
|
|
37
|
-
readonly random?: RandomSource;
|
|
38
40
|
}
|
|
39
41
|
export interface Messenger {
|
|
40
42
|
clear(): void;
|
package/dist/_node.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_node.d.ts","sourceRoot":"","sources":["../src/_node.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"_node.d.ts","sourceRoot":"","sources":["../src/_node.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,QAAQ,EACR,cAAc,E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AAE,MAAM,GAAG,OAAO,GAAG,UAAU,CAwBzG;AAID,KAAK,cAAc,GAAG,CAAC,OAAO,EAAE,WAAW,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC;AAE9D,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,KAAK,EAAE,MAAM,MAAM,CAAC;IAC7B,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAC;IAC7C,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;CAClC;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,IAAI,IAAI,CAAC;IACd,6CAA6C;IAC7C,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI,CAAC;IACrD,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,WAAW,CAAC;IACvD,yFAAyF;IACzF,MAAM,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;CAChE;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE,gBAAgB,GAAG,SAAS,CAqGpE"}
|
package/dist/_node.js
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { tapper as s, utf8Bytes as c } from "@vielzeug/arsenal";
|
|
1
|
+
import { toEnvelope as e } from "./_wire.js";
|
|
2
|
+
import { MeshConnectionError as t, MeshDisposedError as n, MeshError as r, MeshPayloadError as i } from "./errors.js";
|
|
3
|
+
import { tapper as a, utf8Bytes as o } from "@vielzeug/arsenal";
|
|
5
4
|
// src/_node.ts
|
|
6
|
-
function createNodeCore(
|
|
7
|
-
let
|
|
8
|
-
disposalCtrl:
|
|
5
|
+
function createNodeCore() {
|
|
6
|
+
let e = a(), t = new AbortController(), r = {
|
|
7
|
+
disposalCtrl: t,
|
|
9
8
|
disposed: !1,
|
|
10
|
-
emitTap(
|
|
11
|
-
|
|
9
|
+
emitTap(t) {
|
|
10
|
+
e.emit(t);
|
|
12
11
|
},
|
|
13
12
|
ensureLive() {
|
|
14
|
-
if (r.disposed) throw new
|
|
13
|
+
if (r.disposed) throw new n();
|
|
15
14
|
},
|
|
16
15
|
markDisposed() {
|
|
17
16
|
r.disposed || (r.disposed = !0, r.status = "disposed", r.emitTap({
|
|
18
17
|
status: "disposed",
|
|
19
18
|
type: "status-change"
|
|
20
|
-
}), r.emitTap({ type: "dispose" }),
|
|
19
|
+
}), r.emitTap({ type: "dispose" }), e.clear(), t.abort());
|
|
21
20
|
},
|
|
22
21
|
setStatus(e) {
|
|
23
22
|
r.disposed || r.status === e || (r.status = e, r.emitTap({
|
|
@@ -26,141 +25,137 @@ function createNodeCore(e) {
|
|
|
26
25
|
}));
|
|
27
26
|
},
|
|
28
27
|
status: "idle",
|
|
29
|
-
tap(
|
|
30
|
-
return r.disposed ? () => {} :
|
|
28
|
+
tap(t, n) {
|
|
29
|
+
return r.disposed ? () => {} : e.tap(t, n);
|
|
31
30
|
}
|
|
32
31
|
};
|
|
33
|
-
return
|
|
32
|
+
return r;
|
|
34
33
|
}
|
|
35
|
-
function
|
|
36
|
-
|
|
34
|
+
function wireDisposalSignal(e, t) {
|
|
35
|
+
e && (e.aborted ? t() : e.addEventListener("abort", t, { once: !0 }));
|
|
36
|
+
}
|
|
37
|
+
function createPeerRecord(e, t, n) {
|
|
38
|
+
let r = {
|
|
37
39
|
dc: null,
|
|
38
|
-
dedupe: new t(),
|
|
39
40
|
id: e,
|
|
40
|
-
name:
|
|
41
|
+
name: t,
|
|
41
42
|
pc: null,
|
|
42
43
|
publicPeer: {
|
|
43
44
|
get id() {
|
|
44
|
-
return
|
|
45
|
+
return r.id;
|
|
45
46
|
},
|
|
46
47
|
get name() {
|
|
47
|
-
return
|
|
48
|
+
return r.name;
|
|
48
49
|
},
|
|
49
50
|
get role() {
|
|
50
|
-
return
|
|
51
|
+
return r.role;
|
|
51
52
|
},
|
|
52
53
|
get status() {
|
|
53
|
-
return
|
|
54
|
+
return r.status;
|
|
54
55
|
}
|
|
55
56
|
},
|
|
56
|
-
role:
|
|
57
|
+
role: n,
|
|
57
58
|
status: "connecting"
|
|
58
59
|
};
|
|
59
|
-
return
|
|
60
|
+
return r;
|
|
60
61
|
}
|
|
61
|
-
function createMessenger(
|
|
62
|
-
let
|
|
63
|
-
function reject(e,
|
|
64
|
-
|
|
62
|
+
function createMessenger(n) {
|
|
63
|
+
let a = /* @__PURE__ */ new Map();
|
|
64
|
+
function reject(e, t) {
|
|
65
|
+
n.emitTap({
|
|
65
66
|
peerId: e,
|
|
66
|
-
reason:
|
|
67
|
+
reason: t,
|
|
67
68
|
type: "message-rejected"
|
|
68
69
|
});
|
|
69
70
|
}
|
|
70
71
|
return {
|
|
71
72
|
clear() {
|
|
72
|
-
|
|
73
|
+
a.clear();
|
|
73
74
|
},
|
|
74
|
-
handleInbound(
|
|
75
|
-
let
|
|
76
|
-
if (typeof
|
|
77
|
-
reject(
|
|
75
|
+
handleInbound(t, i) {
|
|
76
|
+
let s = t.id;
|
|
77
|
+
if (typeof i != "string") {
|
|
78
|
+
reject(s, "malformed");
|
|
78
79
|
return;
|
|
79
80
|
}
|
|
80
|
-
let
|
|
81
|
-
if (
|
|
82
|
-
reject(
|
|
81
|
+
let c = o(i);
|
|
82
|
+
if (c > n.maxMessageBytes) {
|
|
83
|
+
reject(s, "too-large");
|
|
83
84
|
return;
|
|
84
85
|
}
|
|
85
86
|
let l;
|
|
86
87
|
try {
|
|
87
|
-
l = JSON.parse(
|
|
88
|
+
l = JSON.parse(i);
|
|
88
89
|
} catch {
|
|
89
|
-
reject(
|
|
90
|
+
reject(s, "malformed");
|
|
90
91
|
return;
|
|
91
92
|
}
|
|
92
|
-
let u =
|
|
93
|
+
let u = e(l);
|
|
93
94
|
if (!u) {
|
|
94
|
-
reject(
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
if (!e.dedupe.check(u.id)) {
|
|
98
|
-
reject(o, "duplicate");
|
|
95
|
+
reject(s, "malformed");
|
|
99
96
|
return;
|
|
100
97
|
}
|
|
101
|
-
let d =
|
|
98
|
+
let d = a.get(u.t);
|
|
102
99
|
if (!d || d.size === 0) {
|
|
103
|
-
reject(
|
|
100
|
+
reject(s, "unknown-type");
|
|
104
101
|
return;
|
|
105
102
|
}
|
|
106
|
-
|
|
107
|
-
bytes:
|
|
103
|
+
n.emitTap({
|
|
104
|
+
bytes: c,
|
|
108
105
|
messageType: u.t,
|
|
109
|
-
peerId:
|
|
106
|
+
peerId: s,
|
|
110
107
|
type: "message-received"
|
|
111
108
|
});
|
|
112
109
|
let f = {
|
|
113
|
-
messageId: u.id,
|
|
114
110
|
payload: u.p,
|
|
115
|
-
peerId:
|
|
111
|
+
peerId: s,
|
|
116
112
|
sentAt: u.ts
|
|
117
113
|
};
|
|
118
114
|
for (let e of d) try {
|
|
119
115
|
e(f);
|
|
120
116
|
} catch (e) {
|
|
121
|
-
|
|
122
|
-
error: new
|
|
117
|
+
n.emitTap({
|
|
118
|
+
error: new r("Message handler threw", { cause: e }),
|
|
123
119
|
type: "error"
|
|
124
120
|
});
|
|
125
121
|
}
|
|
126
122
|
},
|
|
127
123
|
on(e, t) {
|
|
128
|
-
let n =
|
|
129
|
-
return n || (n = /* @__PURE__ */ new Set(),
|
|
130
|
-
n.delete(t), n.size === 0 &&
|
|
124
|
+
let n = a.get(e);
|
|
125
|
+
return n || (n = /* @__PURE__ */ new Set(), a.set(e, n)), n.add(t), () => {
|
|
126
|
+
n.delete(t), n.size === 0 && a.delete(e);
|
|
131
127
|
};
|
|
132
128
|
},
|
|
133
|
-
sendTo(
|
|
134
|
-
let s =
|
|
135
|
-
if (!s || s.readyState !== "open" ||
|
|
136
|
-
let
|
|
137
|
-
id: e(t.random),
|
|
129
|
+
sendTo(e, r, a) {
|
|
130
|
+
let s = e.dc;
|
|
131
|
+
if (!s || s.readyState !== "open" || e.status !== "connected") throw new t(`Peer "${e.id}" is not connected`, e.id);
|
|
132
|
+
let c = {
|
|
138
133
|
p: a,
|
|
139
|
-
t:
|
|
140
|
-
ts:
|
|
141
|
-
v:
|
|
142
|
-
},
|
|
134
|
+
t: r,
|
|
135
|
+
ts: n.clock(),
|
|
136
|
+
v: 2
|
|
137
|
+
}, l;
|
|
143
138
|
try {
|
|
144
|
-
|
|
139
|
+
l = JSON.stringify(c);
|
|
145
140
|
} catch (e) {
|
|
146
|
-
throw new
|
|
141
|
+
throw new i(`Failed to serialize message "${r}"`, { cause: e });
|
|
147
142
|
}
|
|
148
|
-
let
|
|
149
|
-
if (
|
|
143
|
+
let u = o(l);
|
|
144
|
+
if (u > n.maxMessageBytes) throw new i(`Message "${r}" is ${u} bytes (max ${n.maxMessageBytes})`);
|
|
150
145
|
try {
|
|
151
|
-
s.send(
|
|
152
|
-
} catch (
|
|
153
|
-
throw new
|
|
146
|
+
s.send(l);
|
|
147
|
+
} catch (n) {
|
|
148
|
+
throw new t(`Failed to send message "${r}"`, e.id, { cause: n });
|
|
154
149
|
}
|
|
155
|
-
|
|
156
|
-
bytes:
|
|
157
|
-
messageType:
|
|
158
|
-
peerId:
|
|
150
|
+
n.emitTap({
|
|
151
|
+
bytes: u,
|
|
152
|
+
messageType: r,
|
|
153
|
+
peerId: e.id,
|
|
159
154
|
type: "message-sent"
|
|
160
155
|
});
|
|
161
156
|
}
|
|
162
157
|
};
|
|
163
158
|
}
|
|
164
|
-
export { createMessenger, createNodeCore, createPeerRecord };
|
|
159
|
+
export { createMessenger, createNodeCore, createPeerRecord, wireDisposalSignal };
|
|
165
160
|
|
|
166
161
|
//# sourceMappingURL=_node.js.map
|
package/dist/_node.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_node.js","names":["tappers","tapper","disposalCtrl","core","event","MeshDisposedError","next","handler","options","signal","record","MessageDedupe","handlers","peerId","peer","data","bytes","utf8Bytes","decoded","envelope","toEnvelope","set","message","cause","MeshError","type","dc","MeshConnectionError","randomId","payload","text","MeshPayloadError"],"sources":["../src/_node.ts"],"sourcesContent":["import { type RandomSource, tapper, utf8Bytes } from '@vielzeug/arsenal';\nimport { randomId } from './_random';\nimport { MessageDedupe, toEnvelope } from './_wire';\nimport { MeshConnectionError, MeshDisposedError, MeshError, MeshPayloadError } from './errors';\nimport type {\n MeshEvent,\n MeshInbound,\n MeshPeer,\n MeshPeerStatus,\n MeshStatus,\n RTCDataChannelLike,\n RTCPeerConnectionLike,\n Unsubscribe,\n} from './types';\n\ntype Tapper = (event: MeshEvent) => void;\n\n/** Shared lifecycle core: status, tappers, disposal. */\nexport interface NodeCore {\n readonly disposalCtrl: AbortController;\n disposed: boolean;\n emitTap(event: MeshEvent): void;\n ensureLive(): void;\n /** Emits the final status-change + dispose events, clears tappers, aborts. */\n markDisposed(): void;\n setStatus(next: MeshStatus): void;\n status: MeshStatus;\n tap(handler: Tapper, options?: { readonly signal?: AbortSignal }): Unsubscribe;\n}\n\nexport function createNodeCore(signal?: AbortSignal): NodeCore {\n const tappers = tapper<MeshEvent>();\n const disposalCtrl = new AbortController();\n\n const core: NodeCore = {\n disposalCtrl,\n disposed: false,\n\n emitTap(event) {\n tappers.emit(event);\n },\n\n ensureLive() {\n if (core.disposed) throw new MeshDisposedError();\n },\n\n markDisposed() {\n if (core.disposed) return;\n core.disposed = true;\n core.status = 'disposed';\n core.emitTap({ status: 'disposed', type: 'status-change' });\n core.emitTap({ type: 'dispose' });\n tappers.clear();\n disposalCtrl.abort();\n },\n\n setStatus(next) {\n if (core.disposed || core.status === next) return;\n core.status = next;\n core.emitTap({ status: next, type: 'status-change' });\n },\n status: 'idle',\n\n tap(handler, options) {\n if (core.disposed) return () => {};\n return tappers.tap(handler, options);\n },\n };\n\n if (signal) {\n if (signal.aborted) {\n core.markDisposed();\n } else {\n signal.addEventListener('abort', () => core.markDisposed(), { once: true });\n }\n }\n\n return core;\n}\n\n// ─── Peers ───────────────────────────────────────────────────────────────────\n\n/** Internal peer record; `publicPeer` exposes live status through getters. */\nexport interface PeerRecord {\n dc: RTCDataChannelLike | null;\n readonly dedupe: MessageDedupe;\n readonly id: string;\n readonly name: string | undefined;\n pc: RTCPeerConnectionLike | null;\n readonly publicPeer: MeshPeer;\n readonly role: 'host' | 'guest';\n status: MeshPeerStatus;\n}\n\nexport function createPeerRecord(id: string, name: string | undefined, role: 'host' | 'guest'): PeerRecord {\n const record: PeerRecord = {\n dc: null,\n dedupe: new MessageDedupe(),\n id,\n name,\n pc: null,\n publicPeer: {\n get id() {\n return record.id;\n },\n get name() {\n return record.name;\n },\n get role() {\n return record.role;\n },\n get status() {\n return record.status;\n },\n },\n role,\n status: 'connecting',\n };\n return record;\n}\n\n// ─── Messenger ───────────────────────────────────────────────────────────────\n\ntype InboundHandler = (message: MeshInbound<unknown>) => void;\n\nexport interface MessengerOptions {\n readonly clock: () => number;\n readonly emitTap: (event: MeshEvent) => void;\n readonly maxMessageBytes: number;\n readonly random?: RandomSource;\n}\n\nexport interface Messenger {\n clear(): void;\n /** Entry point for `dc` 'message' events. */\n handleInbound(peer: PeerRecord, data: unknown): void;\n on(type: string, handler: InboundHandler): Unsubscribe;\n /** Sends a typed envelope to a peer. Throws `MeshConnectionError`/`MeshPayloadError`. */\n sendTo(peer: PeerRecord, type: string, payload: unknown): void;\n}\n\nexport function createMessenger(options: MessengerOptions): Messenger {\n const handlers = new Map<string, Set<InboundHandler>>();\n\n function reject(peerId: string, reason: 'too-large' | 'duplicate' | 'malformed' | 'unknown-type'): void {\n options.emitTap({ peerId, reason, type: 'message-rejected' });\n }\n\n return {\n clear() {\n handlers.clear();\n },\n\n handleInbound(peer, data) {\n const peerId = peer.id;\n\n if (typeof data !== 'string') {\n reject(peerId, 'malformed');\n return;\n }\n\n const bytes = utf8Bytes(data);\n if (bytes > options.maxMessageBytes) {\n reject(peerId, 'too-large');\n return;\n }\n\n let decoded: unknown;\n try {\n decoded = JSON.parse(data);\n } catch {\n reject(peerId, 'malformed');\n return;\n }\n\n const envelope = toEnvelope(decoded);\n if (!envelope) {\n reject(peerId, 'malformed');\n return;\n }\n\n if (!peer.dedupe.check(envelope.id)) {\n reject(peerId, 'duplicate');\n return;\n }\n\n const set = handlers.get(envelope.t);\n if (!set || set.size === 0) {\n reject(peerId, 'unknown-type');\n return;\n }\n\n options.emitTap({ bytes, messageType: envelope.t, peerId, type: 'message-received' });\n const message: MeshInbound<unknown> = {\n messageId: envelope.id,\n payload: envelope.p,\n peerId,\n sentAt: envelope.ts,\n };\n for (const handler of set) {\n try {\n handler(message);\n } catch (cause) {\n options.emitTap({ error: new MeshError('Message handler threw', { cause }), type: 'error' });\n }\n }\n },\n\n on(type, handler) {\n let set = handlers.get(type);\n if (!set) {\n set = new Set();\n handlers.set(type, set);\n }\n set.add(handler);\n return () => {\n set.delete(handler);\n if (set.size === 0) handlers.delete(type);\n };\n },\n\n sendTo(peer, type, payload) {\n const dc = peer.dc;\n if (!dc || dc.readyState !== 'open' || peer.status !== 'connected') {\n throw new MeshConnectionError(`Peer \"${peer.id}\" is not connected`, peer.id);\n }\n\n const envelope = { id: randomId(options.random), p: payload, t: type, ts: options.clock(), v: 1 as const };\n let text: string;\n try {\n text = JSON.stringify(envelope);\n } catch (cause) {\n throw new MeshPayloadError(`Failed to serialize message \"${type}\"`, { cause });\n }\n\n const bytes = utf8Bytes(text);\n if (bytes > options.maxMessageBytes) {\n throw new MeshPayloadError(`Message \"${type}\" is ${bytes} bytes (max ${options.maxMessageBytes})`);\n }\n\n try {\n dc.send(text);\n } catch (cause) {\n throw new MeshConnectionError(`Failed to send message \"${type}\"`, peer.id, { cause });\n }\n options.emitTap({ bytes, messageType: type, peerId: peer.id, type: 'message-sent' });\n },\n };\n}\n"],"mappings":";;;;;AA8BA,SAAgB,eAAe,GAAgC;CAC7D,IAAMA,IAAUC,EAAkB,GAC5BC,IAAe,IAAI,gBAAgB,GAEnCC,IAAiB;EACrB;EACA,UAAU;EAEV,QAAQ,GAAO;GAC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,EAAS,OAAOa,CAAI;GAC1C;EACF;EAEA,OAAO,GAAM,GAAM,GAAS;GAC1B,IAAMC,IAAKZ,EAAK;GAChB,IAAI,CAACY,KAAMA,EAAG,eAAe,UAAUZ,EAAK,WAAW,aACrD,MAAM,IAAIa,EAAoB,SAASb,EAAK,GAAG,qBAAqBA,EAAK,EAAE;GAG7E,IAAMK,IAAW;IAAE,IAAIS,EAASpB,EAAQ,MAAM;IAAG,GAAGqB;IAAS,GAAGJ;IAAM,IAAIjB,EAAQ,MAAM;IAAG,GAAG;GAAW,GACrGsB;GACJ,IAAI;IACF,IAAO,KAAK,UAAUX,CAAQ;GAChC,SAASI,GAAO;IACd,MAAM,IAAIQ,EAAiB,gCAAgCN,EAAK,IAAI,EAAE,SAAM,CAAC;GAC/E;GAEA,IAAMT,IAAQC,EAAUa,CAAI;GAC5B,IAAId,IAAQR,EAAQ,iBAClB,MAAM,IAAIuB,EAAiB,YAAYN,EAAK,OAAOT,EAAM,cAAcR,EAAQ,gBAAgB,EAAE;GAGnG,IAAI;IACF,EAAG,KAAKsB,CAAI;GACd,SAASP,GAAO;IACd,MAAM,IAAII,EAAoB,2BAA2BF,EAAK,IAAIX,EAAK,IAAI,EAAE,SAAM,CAAC;GACtF;GACA,EAAQ,QAAQ;IAAE;IAAO,aAAaW;IAAM,QAAQX,EAAK;IAAI,MAAM;GAAe,CAAC;EACrF;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"_node.js","names":["tappers","tapper","disposalCtrl","core","event","MeshDisposedError","next","handler","options","signal","dispose","record","handlers","peerId","peer","data","bytes","utf8Bytes","decoded","envelope","toEnvelope","set","message","cause","MeshError","type","dc","MeshConnectionError","payload","text","MeshPayloadError"],"sources":["../src/_node.ts"],"sourcesContent":["import { tapper, utf8Bytes } from '@vielzeug/arsenal';\nimport { toEnvelope } from './_wire';\nimport { MeshConnectionError, MeshDisposedError, MeshError, MeshPayloadError } from './errors';\nimport type {\n MeshEvent,\n MeshInbound,\n MeshPeer,\n MeshPeerStatus,\n MeshStatus,\n RTCDataChannelLike,\n RTCPeerConnectionLike,\n Unsubscribe,\n} from './types';\n\ntype Tapper = (event: MeshEvent) => void;\n\n/** Shared lifecycle core: status, tappers, disposal. */\nexport interface NodeCore {\n readonly disposalCtrl: AbortController;\n disposed: boolean;\n emitTap(event: MeshEvent): void;\n ensureLive(): void;\n /** Emits the final status-change + dispose events, clears tappers, aborts. */\n markDisposed(): void;\n setStatus(next: MeshStatus): void;\n status: MeshStatus;\n tap(handler: Tapper, options?: { readonly signal?: AbortSignal }): Unsubscribe;\n}\n\nexport function createNodeCore(): NodeCore {\n const tappers = tapper<MeshEvent>();\n const disposalCtrl = new AbortController();\n\n const core: NodeCore = {\n disposalCtrl,\n disposed: false,\n\n emitTap(event) {\n tappers.emit(event);\n },\n\n ensureLive() {\n if (core.disposed) throw new MeshDisposedError();\n },\n\n markDisposed() {\n if (core.disposed) return;\n core.disposed = true;\n core.status = 'disposed';\n core.emitTap({ status: 'disposed', type: 'status-change' });\n core.emitTap({ type: 'dispose' });\n tappers.clear();\n disposalCtrl.abort();\n },\n\n setStatus(next) {\n if (core.disposed || core.status === next) return;\n core.status = next;\n core.emitTap({ status: next, type: 'status-change' });\n },\n status: 'idle',\n\n tap(handler, options) {\n if (core.disposed) return () => {};\n return tappers.tap(handler, options);\n },\n };\n\n return core;\n}\n\n/**\n * Wires the `signal` option of a node: abort runs the node's full `dispose()`,\n * so pending invitations, live connections, and handlers are released, not\n * just the status flags. A signal that is already aborted disposes immediately.\n */\nexport function wireDisposalSignal(signal: AbortSignal | undefined, dispose: () => void): void {\n if (!signal) return;\n if (signal.aborted) dispose();\n else signal.addEventListener('abort', dispose, { once: true });\n}\n\n// ─── Peers ───────────────────────────────────────────────────────────────────\n\n/** Internal peer record; `publicPeer` exposes live status through getters. */\nexport interface PeerRecord {\n dc: RTCDataChannelLike | null;\n readonly id: string;\n readonly name: string | undefined;\n pc: RTCPeerConnectionLike | null;\n readonly publicPeer: MeshPeer;\n readonly role: 'host' | 'guest';\n status: MeshPeerStatus;\n}\n\nexport function createPeerRecord(id: string, name: string | undefined, role: 'host' | 'guest'): PeerRecord {\n const record: PeerRecord = {\n dc: null,\n id,\n name,\n pc: null,\n publicPeer: {\n get id() {\n return record.id;\n },\n get name() {\n return record.name;\n },\n get role() {\n return record.role;\n },\n get status() {\n return record.status;\n },\n },\n role,\n status: 'connecting',\n };\n return record;\n}\n\n// ─── Messenger ───────────────────────────────────────────────────────────────\n\ntype InboundHandler = (message: MeshInbound<unknown>) => void;\n\nexport interface MessengerOptions {\n readonly clock: () => number;\n readonly emitTap: (event: MeshEvent) => void;\n readonly maxMessageBytes: number;\n}\n\nexport interface Messenger {\n clear(): void;\n /** Entry point for `dc` 'message' events. */\n handleInbound(peer: PeerRecord, data: unknown): void;\n on(type: string, handler: InboundHandler): Unsubscribe;\n /** Sends a typed envelope to a peer. Throws `MeshConnectionError`/`MeshPayloadError`. */\n sendTo(peer: PeerRecord, type: string, payload: unknown): void;\n}\n\nexport function createMessenger(options: MessengerOptions): Messenger {\n const handlers = new Map<string, Set<InboundHandler>>();\n\n function reject(peerId: string, reason: 'too-large' | 'malformed' | 'unknown-type'): void {\n options.emitTap({ peerId, reason, type: 'message-rejected' });\n }\n\n return {\n clear() {\n handlers.clear();\n },\n\n handleInbound(peer, data) {\n const peerId = peer.id;\n\n if (typeof data !== 'string') {\n reject(peerId, 'malformed');\n return;\n }\n\n const bytes = utf8Bytes(data);\n if (bytes > options.maxMessageBytes) {\n reject(peerId, 'too-large');\n return;\n }\n\n let decoded: unknown;\n try {\n decoded = JSON.parse(data);\n } catch {\n reject(peerId, 'malformed');\n return;\n }\n\n const envelope = toEnvelope(decoded);\n if (!envelope) {\n reject(peerId, 'malformed');\n return;\n }\n\n const set = handlers.get(envelope.t);\n if (!set || set.size === 0) {\n reject(peerId, 'unknown-type');\n return;\n }\n\n options.emitTap({ bytes, messageType: envelope.t, peerId, type: 'message-received' });\n const message: MeshInbound<unknown> = {\n payload: envelope.p,\n peerId,\n sentAt: envelope.ts,\n };\n for (const handler of set) {\n try {\n handler(message);\n } catch (cause) {\n options.emitTap({ error: new MeshError('Message handler threw', { cause }), type: 'error' });\n }\n }\n },\n\n on(type, handler) {\n let set = handlers.get(type);\n if (!set) {\n set = new Set();\n handlers.set(type, set);\n }\n set.add(handler);\n return () => {\n set.delete(handler);\n if (set.size === 0) handlers.delete(type);\n };\n },\n\n sendTo(peer, type, payload) {\n const dc = peer.dc;\n if (!dc || dc.readyState !== 'open' || peer.status !== 'connected') {\n throw new MeshConnectionError(`Peer \"${peer.id}\" is not connected`, peer.id);\n }\n\n const envelope = { p: payload, t: type, ts: options.clock(), v: 2 as const };\n let text: string;\n try {\n text = JSON.stringify(envelope);\n } catch (cause) {\n throw new MeshPayloadError(`Failed to serialize message \"${type}\"`, { cause });\n }\n\n const bytes = utf8Bytes(text);\n if (bytes > options.maxMessageBytes) {\n throw new MeshPayloadError(`Message \"${type}\" is ${bytes} bytes (max ${options.maxMessageBytes})`);\n }\n\n try {\n dc.send(text);\n } catch (cause) {\n throw new MeshConnectionError(`Failed to send message \"${type}\"`, peer.id, { cause });\n }\n options.emitTap({ bytes, messageType: type, peerId: peer.id, type: 'message-sent' });\n },\n };\n}\n"],"mappings":";;;;AA6BA,SAAgB,iBAA2B;CACzC,IAAMA,IAAUC,EAAkB,GAC5BC,IAAe,IAAI,gBAAgB,GAEnCC,IAAiB;EACrB;EACA,UAAU;EAEV,QAAQ,GAAO;GACb,EAAQ,KAAKC,CAAK;EACpB;EAEA,aAAa;GACX,IAAID,EAAK,UAAU,MAAM,IAAIE,EAAkB;EACjD;EAEA,eAAe;GACb,AAAIF,EAAK,aACT,EAAK,WAAW,IAChB,EAAK,SAAS,YACdA,EAAK,QAAQ;IAAE,QAAQ;IAAY,MAAM;GAAgB,CAAC,GAC1DA,EAAK,QAAQ,EAAE,MAAM,UAAU,CAAC,GAChCH,EAAQ,MAAM,GACdE,EAAa,MAAM;EACrB;EAEA,U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,IAAI,EAAE,SAAM,CAAC;GAC/E;GAEA,IAAMT,IAAQC,EAAUY,CAAI;GAC5B,IAAIb,IAAQR,EAAQ,iBAClB,MAAM,IAAIsB,EAAiB,YAAYL,EAAK,OAAOT,EAAM,cAAcR,EAAQ,gBAAgB,EAAE;GAGnG,IAAI;IACF,EAAG,KAAKqB,CAAI;GACd,SAASN,GAAO;IACd,MAAM,IAAII,EAAoB,2BAA2BF,EAAK,IAAIX,EAAK,IAAI,EAAE,SAAM,CAAC;GACtF;GACA,EAAQ,QAAQ;IAAE;IAAO,aAAaW;IAAM,QAAQX,EAAK;IAAI,MAAM;GAAe,CAAC;EACrF;CACF;AACF"}
|
package/dist/_wire.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function toEnvelope(e){if(typeof e!=`object`||!e)return null;let t=e;return t.v!==
|
|
1
|
+
function toEnvelope(e){if(typeof e!=`object`||!e)return null;let t=e;return t.v!==2||typeof t.t!=`string`||typeof t.ts!=`number`?null:e}exports.toEnvelope=toEnvelope;
|
|
2
2
|
//# sourceMappingURL=_wire.cjs.map
|
package/dist/_wire.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_wire.cjs","names":["value","env"
|
|
1
|
+
{"version":3,"file":"_wire.cjs","names":["value","env"],"sources":["../src/_wire.ts"],"sourcesContent":["/**\n * Wire envelope serialized through the configured `serialize` function.\n * Delivery is best-effort over the reliable, ordered data channel: v2\n * envelopes carry no per-message id because the API offers no resend with\n * a stable id, so a dedupe layer had nothing real to deduplicate.\n */\nexport interface WireEnvelope {\n readonly p: unknown;\n readonly t: string;\n readonly ts: number;\n readonly v: 2;\n}\n\n/** Returns the envelope, or `null` when the value violates the wire shape (older `v: 1` frames are malformed). */\nexport function toEnvelope(value: unknown): WireEnvelope | null {\n if (typeof value !== 'object' || value === null) return null;\n const env = value as Record<string, unknown>;\n if (env.v !== 2 || typeof env.t !== 'string' || typeof env.ts !== 'number') return null;\n return value as WireEnvelope;\n}\n"],"mappings":"AAcA,SAAgB,WAAW,EAAqC,CAC9D,GAAI,OAAOA,GAAU,WAAYA,EAAgB,OAAO,KACxD,IAAMC,EAAMD,EAEZ,OADIC,EAAI,IAAM,GAAK,OAAOA,EAAI,GAAM,UAAY,OAAOA,EAAI,IAAO,SAAiB,KAC5ED,CACT,C"}
|
package/dist/_wire.d.ts
CHANGED
|
@@ -1,20 +1,15 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Wire envelope serialized through the configured `serialize` function.
|
|
3
|
+
* Delivery is best-effort over the reliable, ordered data channel: v2
|
|
4
|
+
* envelopes carry no per-message id because the API offers no resend with
|
|
5
|
+
* a stable id, so a dedupe layer had nothing real to deduplicate.
|
|
6
|
+
*/
|
|
2
7
|
export interface WireEnvelope {
|
|
3
|
-
readonly id: string;
|
|
4
8
|
readonly p: unknown;
|
|
5
9
|
readonly t: string;
|
|
6
10
|
readonly ts: number;
|
|
7
|
-
readonly v:
|
|
11
|
+
readonly v: 2;
|
|
8
12
|
}
|
|
9
|
-
/** Returns the envelope, or `null` when the value violates the wire shape. */
|
|
13
|
+
/** Returns the envelope, or `null` when the value violates the wire shape (older `v: 1` frames are malformed). */
|
|
10
14
|
export declare function toEnvelope(value: unknown): WireEnvelope | null;
|
|
11
|
-
/** Per-peer duplicate guard: remembers the last `capacity` message ids (LRU). */
|
|
12
|
-
export declare class MessageDedupe {
|
|
13
|
-
private readonly seen;
|
|
14
|
-
private readonly order;
|
|
15
|
-
private readonly capacity;
|
|
16
|
-
constructor(capacity?: number);
|
|
17
|
-
/** `true` when the id is new (and records it); `false` for duplicates. */
|
|
18
|
-
check(id: string): boolean;
|
|
19
|
-
}
|
|
20
15
|
//# sourceMappingURL=_wire.d.ts.map
|
package/dist/_wire.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_wire.d.ts","sourceRoot":"","sources":["../src/_wire.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"_wire.d.ts","sourceRoot":"","sources":["../src/_wire.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC;IACpB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC;CACf;AAED,kHAAkH;AAClH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,YAAY,GAAG,IAAI,CAK9D"}
|
package/dist/_wire.js
CHANGED
|
@@ -2,19 +2,8 @@
|
|
|
2
2
|
function toEnvelope(e) {
|
|
3
3
|
if (typeof e != "object" || !e) return null;
|
|
4
4
|
let t = e;
|
|
5
|
-
return t.v !==
|
|
5
|
+
return t.v !== 2 || typeof t.t != "string" || typeof t.ts != "number" ? null : e;
|
|
6
6
|
}
|
|
7
|
-
|
|
8
|
-
seen = /* @__PURE__ */ new Set();
|
|
9
|
-
order = [];
|
|
10
|
-
capacity;
|
|
11
|
-
constructor(e = 256) {
|
|
12
|
-
this.capacity = e;
|
|
13
|
-
}
|
|
14
|
-
check(e) {
|
|
15
|
-
return !this.seen.has(e) && (this.seen.add(e), this.order.push(e), this.order.length > this.capacity && this.seen.delete(this.order.shift()), !0);
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
export { MessageDedupe, toEnvelope };
|
|
7
|
+
export { toEnvelope };
|
|
19
8
|
|
|
20
9
|
//# sourceMappingURL=_wire.js.map
|
package/dist/_wire.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_wire.js","names":["value","env"
|
|
1
|
+
{"version":3,"file":"_wire.js","names":["value","env"],"sources":["../src/_wire.ts"],"sourcesContent":["/**\n * Wire envelope serialized through the configured `serialize` function.\n * Delivery is best-effort over the reliable, ordered data channel: v2\n * envelopes carry no per-message id because the API offers no resend with\n * a stable id, so a dedupe layer had nothing real to deduplicate.\n */\nexport interface WireEnvelope {\n readonly p: unknown;\n readonly t: string;\n readonly ts: number;\n readonly v: 2;\n}\n\n/** Returns the envelope, or `null` when the value violates the wire shape (older `v: 1` frames are malformed). */\nexport function toEnvelope(value: unknown): WireEnvelope | null {\n if (typeof value !== 'object' || value === null) return null;\n const env = value as Record<string, unknown>;\n if (env.v !== 2 || typeof env.t !== 'string' || typeof env.ts !== 'number') return null;\n return value as WireEnvelope;\n}\n"],"mappings":";AAcA,SAAgB,WAAW,GAAqC;CAC9D,IAAI,OAAOA,KAAU,aAAYA,GAAgB,OAAO;CACxD,IAAMC,IAAMD;CAEZ,OADIC,EAAI,MAAM,KAAK,OAAOA,EAAI,KAAM,YAAY,OAAOA,EAAI,MAAO,WAAiB,OAC5ED;AACT"}
|
package/dist/guest.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
require("./_defaults.cjs");const e=require("./_node.cjs"),t=require("./_proof.cjs"),n=require("./_random.cjs"),r=require("./_rtc.cjs"),i=require("./errors.cjs");let a=require("@vielzeug/arsenal");function createMeshGuest(o={}){let s=o.rtc??r.nativeRtcFactory,c=o.clock??(()=>Date.now()),l=o.maxMessageBytes??65536,u=o.channelOpenTimeoutMs??6e4,d=o.iceGatheringTimeoutMs??5e3,f=e.createNodeCore(
|
|
1
|
+
require("./_defaults.cjs");const e=require("./_node.cjs"),t=require("./_proof.cjs"),n=require("./_random.cjs"),r=require("./_rtc.cjs"),i=require("./errors.cjs");let a=require("@vielzeug/arsenal");function createMeshGuest(o={}){let s=o.rtc??r.nativeRtcFactory,c=o.clock??(()=>Date.now()),l=o.maxMessageBytes??65536,u=o.channelOpenTimeoutMs??6e4,d=o.iceGatheringTimeoutMs??5e3,f=e.createNodeCore(),p=e.createMessenger({clock:c,emitTap:f.emitTap,maxMessageBytes:l}),m=null,h=(0,a.timer)(()=>{m?.status===`connecting`&&failPeer(m,openTimeoutError())}),openTimeoutError=()=>new i.MeshTimeoutError(`Timed out waiting ${u}ms for the data channel to open`);function failPeer(e,t){f.disposed||e!==m||e.status===`failed`||(e.status=`failed`,h.cancel(),setTimeout(()=>{e.dc?.close(),e.pc?.close()},0),f.emitTap({peerId:e.id,status:`failed`,type:`peer-status-change`}),f.emitTap({error:t,type:`error`}),f.emitTap({peer:e.publicPeer,reason:t.message,type:`peer-left`}),f.setStatus(`failed`))}function disconnectPeer(e,t){f.disposed||e!==m||e.status!==`disconnected`&&e.status!==`failed`&&(e.status=`disconnected`,e.pc?.close(),f.emitTap({peerId:e.id,status:`disconnected`,type:`peer-status-change`}),f.emitTap({peer:e.publicPeer,reason:t,type:`peer-left`}),f.setStatus(`disconnected`))}function wireChannel(e,t,n){e.dc=n,n.addEventListener(`open`,()=>{f.disposed||(h.cancel(),e.status=`connected`,f.emitTap({peerId:e.id,status:`connected`,type:`peer-status-change`}),f.emitTap({peer:e.publicPeer,type:`peer-joined`}),f.setStatus(`connected`))}),n.addEventListener(`message`,t=>{f.disposed||p.handleInbound(e,t.data)}),n.addEventListener(`close`,()=>{f.disposed||(e.status===`connecting`?failPeer(e,h.pending?openTimeoutError():new i.MeshConnectionError(`channel closed before opening`,e.id)):disconnectPeer(e,`channel closed`))}),n.addEventListener(`error`,()=>{f.disposed||failPeer(e,new i.MeshConnectionError(`channel error`,e.id))}),t.addEventListener(`iceconnectionstatechange`,()=>{if(f.disposed)return;let n=t.iceConnectionState;(n===`failed`||n===`closed`)&&failPeer(e,new i.MeshConnectionError(`ice ${n}`,e.id))})}let g={async acceptInvitation(a,l){if(f.ensureLive(),m&&m.status!==`failed`&&m.status!==`disconnected`)throw new i.MeshPairingError(`Guest is already paired`);if(a?.v!==1)throw new i.MeshPairingError(`Unsupported invitation version`);if(c()>=a.expiresAt)throw new i.MeshPairingError(`Invitation expired`);m&&=(h.cancel(),m.pc?.close(),null),f.setStatus(`pairing`);let p;try{p=s.createPeerConnection({iceServers:[...o.iceServers??[]]})}catch(e){throw f.setStatus(`idle`),e}let g=e.createPeerRecord(a.sessionId,a.hostName,`host`);g.pc=p,m=g,p.addEventListener(`datachannel`,e=>{wireChannel(g,p,e.channel)}),p.addEventListener(`iceconnectionstatechange`,()=>{f.disposed||f.emitTap({peerId:g.id,state:p.iceConnectionState,type:`ice-state`})});try{await p.setRemoteDescription({sdp:r.normalizeSdp(a.sdp),type:`offer`})}catch(e){throw m=null,f.setStatus(`idle`),new i.MeshPairingError(`Invalid invitation SDP`,{cause:e})}let _;try{await p.setLocalDescription(await p.createAnswer()),await r.waitIceGathering(p,d),_=p.localDescription?.sdp??``}catch(e){throw m=null,p.close(),f.setStatus(`idle`),new i.MeshConnectionError(`Failed to create answer`,null,{cause:e})}if(f.disposed)throw p.close(),new i.MeshDisposedError;if(!_)throw m=null,p.close(),f.setStatus(`idle`),new i.MeshConnectionError(`Local description has no SDP`);let v=n.randomId(o.random),y;try{y=await t.createProof(a.secret,_)}catch(e){throw m=null,p.close(),f.setStatus(`idle`),e}return f.setStatus(`connecting`),h.arm(u),{peer:l?.name===void 0?{id:v}:{id:v,name:l.name},proof:y,sdp:_,sessionId:a.sessionId,v:1}},get disposalSignal(){return f.disposalCtrl.signal},dispose(){f.disposed||(f.markDisposed(),h.cancel(),m&&(m.dc?.close(),m.pc?.close()),p.clear())},get disposed(){return f.disposed},get host(){return m?.publicPeer??null},on(e,t){return f.ensureLive(),p.on(e,t)},send(e,t){if(f.ensureLive(),!m)throw new i.MeshConnectionError(`Guest is not paired`);p.sendTo(m,e,t)},get status(){return f.status},tap(e,t){return f.tap(e,t)},[Symbol.dispose](){this.dispose()}};return e.wireDisposalSignal(o.signal,()=>g.dispose()),g}exports.createMeshGuest=createMeshGuest;
|
|
2
2
|
//# sourceMappingURL=guest.cjs.map
|
package/dist/guest.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guest.cjs","names":["nativeRtcFactory","createNodeCore","createMessenger","timer","MeshTimeoutError","MeshConnectionError","MeshPairingError","createPeerRecord","normalizeSdp","waitIceGathering","MeshDisposedError","randomId","createProof","require__node","require__proof","require__random","require__rtc","require_errors","_vielzeug_arsenal","rtc","options","clock","maxMessageBytes","channelOpenTimeoutMs","iceGatheringTimeoutMs","core","messenger","hostPeer","openTimer","peer","error","dc","event","pc","state","invitation","cause","answerSdp","peerId","proof","meta","type","handler","payload","tapOptions"],"sources":["../src/guest.ts"],"sourcesContent":["import { timer } from '@vielzeug/arsenal';\nimport {\n DEFAULT_CHANNEL_OPEN_TIMEOUT_MS,\n DEFAULT_ICE_GATHERING_TIMEOUT_MS,\n DEFAULT_MAX_MESSAGE_BYTES,\n} from './_defaults';\nimport { createMessenger, createNodeCore, createPeerRecord, type PeerRecord } from './_node';\nimport { createProof } from './_proof';\nimport { randomId } from './_random';\nimport { nativeRtcFactory, normalizeSdp, waitIceGathering } from './_rtc';\nimport { MeshConnectionError, MeshDisposedError, type MeshError, MeshPairingError, MeshTimeoutError } from './errors';\nimport type {\n MeshGuest,\n MeshGuestOptions,\n MeshInbound,\n MeshPeer,\n MeshProtocol,\n RTCDataChannelLike,\n RTCPeerConnectionLike,\n} from './types';\n\n/**\n * Create a guest mesh node. The guest pairs with one host by consuming an\n * out-of-band invitation and returning an answer.\n */\nexport function createMeshGuest<P extends MeshProtocol>(options: MeshGuestOptions = {}): MeshGuest<P> {\n const rtc = options.rtc ?? nativeRtcFactory;\n const clock = options.clock ?? (() => Date.now());\n const maxMessageBytes = options.maxMessageBytes ?? DEFAULT_MAX_MESSAGE_BYTES;\n const channelOpenTimeoutMs = options.channelOpenTimeoutMs ?? DEFAULT_CHANNEL_OPEN_TIMEOUT_MS;\n const iceGatheringTimeoutMs = options.iceGatheringTimeoutMs ?? DEFAULT_ICE_GATHERING_TIMEOUT_MS;\n\n const core = createNodeCore(options.signal);\n const messenger = createMessenger({\n clock,\n emitTap: core.emitTap,\n maxMessageBytes,\n random: options.random,\n });\n\n let hostPeer: PeerRecord | null = null;\n const openTimer = timer(() => {\n if (hostPeer?.status === 'connecting') {\n failPeer(hostPeer, openTimeoutError());\n }\n });\n\n const openTimeoutError = (): MeshTimeoutError =>\n new MeshTimeoutError(`Timed out waiting ${channelOpenTimeoutMs}ms for the data channel to open`);\n\n function failPeer(peer: PeerRecord, error: MeshError): void {\n // Ignore events from a superseded peer after re-pairing replaced hostPeer.\n if (core.disposed || peer !== hostPeer || peer.status === 'failed') return;\n peer.status = 'failed';\n openTimer.cancel();\n // Teardown is deferred a macrotask so a peer racing its own open\n // deadline reports its timeout rather than this hangup.\n setTimeout(() => {\n peer.dc?.close();\n peer.pc?.close();\n }, 0);\n core.emitTap({ peerId: peer.id, status: 'failed', type: 'peer-status-change' });\n core.emitTap({ error, type: 'error' });\n core.emitTap({ peer: peer.publicPeer, reason: error.message, type: 'peer-left' });\n core.setStatus('failed');\n }\n\n function disconnectPeer(peer: PeerRecord, reason: string): void {\n if (core.disposed || peer !== hostPeer) return;\n if (peer.status === 'disconnected' || peer.status === 'failed') return;\n peer.status = 'disconnected';\n peer.pc?.close();\n core.emitTap({ peerId: peer.id, status: 'disconnected', type: 'peer-status-change' });\n core.emitTap({ peer: peer.publicPeer, reason, type: 'peer-left' });\n core.setStatus('disconnected');\n }\n\n function wireChannel(peer: PeerRecord, pc: RTCPeerConnectionLike, dc: RTCDataChannelLike): void {\n peer.dc = dc;\n\n dc.addEventListener('open', () => {\n if (core.disposed) return;\n openTimer.cancel();\n peer.status = 'connected';\n core.emitTap({ peerId: peer.id, status: 'connected', type: 'peer-status-change' });\n core.emitTap({ peer: peer.publicPeer, type: 'peer-joined' });\n core.setStatus('connected');\n });\n dc.addEventListener('message', (event) => {\n if (core.disposed) return;\n messenger.handleInbound(peer, event.data);\n });\n dc.addEventListener('close', () => {\n if (core.disposed) return;\n if (peer.status === 'connecting')\n failPeer(\n peer,\n openTimer.pending ? openTimeoutError() : new MeshConnectionError('channel closed before opening', peer.id),\n );\n else disconnectPeer(peer, 'channel closed');\n });\n dc.addEventListener('error', () => {\n if (core.disposed) return;\n failPeer(peer, new MeshConnectionError('channel error', peer.id));\n });\n\n // The ice-state emission lives on the pairing-scoped listener above; this\n // one owns channel-lifecycle transitions only, so each ICE change emits\n // exactly one ice-state event.\n pc.addEventListener('iceconnectionstatechange', () => {\n if (core.disposed) return;\n const state = pc.iceConnectionState;\n if (state === 'failed' || state === 'closed') {\n failPeer(peer, new MeshConnectionError(`ice ${state}`, peer.id));\n } else if (state === 'disconnected' && peer.status === 'connected') {\n disconnectPeer(peer, 'ice disconnected');\n }\n });\n }\n\n const guest: MeshGuest<P> = {\n async acceptInvitation(invitation, meta) {\n core.ensureLive();\n // A terminal host peer (failed/disconnected) may be replaced by a fresh\n // pairing on the same node: listeners stay attached and keep working.\n if (hostPeer && hostPeer.status !== 'failed' && hostPeer.status !== 'disconnected') {\n throw new MeshPairingError('Guest is already paired');\n }\n if (invitation?.v !== 1) throw new MeshPairingError('Unsupported invitation version');\n if (clock() >= invitation.expiresAt) throw new MeshPairingError('Invitation expired');\n\n if (hostPeer) {\n openTimer.cancel();\n hostPeer.pc?.close();\n hostPeer = null;\n }\n\n core.setStatus('pairing');\n let pc: RTCPeerConnectionLike;\n try {\n pc = rtc.createPeerConnection({ iceServers: [...(options.iceServers ?? [])] });\n } catch (error) {\n core.setStatus('idle');\n throw error;\n }\n\n // The invitation carries no host identity beyond an optional display\n // name: the session id doubles as the host-side peer id for inbound\n // metadata and tap events.\n const peer = createPeerRecord(invitation.sessionId, invitation.hostName, 'host');\n peer.pc = pc;\n hostPeer = peer;\n\n pc.addEventListener('datachannel', (event) => {\n wireChannel(peer, pc, event.channel);\n });\n pc.addEventListener('iceconnectionstatechange', () => {\n if (core.disposed) return;\n core.emitTap({ peerId: peer.id, state: pc.iceConnectionState, type: 'ice-state' });\n });\n\n try {\n await pc.setRemoteDescription({ sdp: normalizeSdp(invitation.sdp), type: 'offer' });\n } catch (cause) {\n hostPeer = null;\n core.setStatus('idle');\n throw new MeshPairingError('Invalid invitation SDP', { cause });\n }\n\n let answerSdp: string;\n try {\n await pc.setLocalDescription(await pc.createAnswer());\n await waitIceGathering(pc, iceGatheringTimeoutMs);\n answerSdp = pc.localDescription?.sdp ?? '';\n } catch (cause) {\n hostPeer = null;\n pc.close();\n core.setStatus('idle');\n throw new MeshConnectionError('Failed to create answer', null, { cause });\n }\n if (core.disposed) {\n pc.close();\n throw new MeshDisposedError();\n }\n if (!answerSdp) {\n hostPeer = null;\n pc.close();\n core.setStatus('idle');\n throw new MeshConnectionError('Local description has no SDP');\n }\n\n const peerId = randomId(options.random);\n let proof: string;\n try {\n proof = await createProof(invitation.secret, answerSdp);\n } catch (cause) {\n hostPeer = null;\n pc.close();\n core.setStatus('idle');\n throw cause;\n }\n core.setStatus('connecting');\n\n // If the channel never opens (host never accepts the answer), fail loudly\n // instead of hanging in 'connecting' forever. The deadline must cover\n // the human carry-back of the answer, not just machine negotiation.\n openTimer.arm(channelOpenTimeoutMs);\n\n return {\n peer: meta?.name === undefined ? { id: peerId } : { id: peerId, name: meta.name },\n proof,\n sdp: answerSdp,\n sessionId: invitation.sessionId,\n v: 1,\n };\n },\n get disposalSignal() {\n return core.disposalCtrl.signal;\n },\n\n dispose() {\n if (core.disposed) return;\n core.markDisposed();\n openTimer.cancel();\n if (hostPeer) {\n hostPeer.dc?.close();\n hostPeer.pc?.close();\n }\n messenger.clear();\n },\n get disposed() {\n return core.disposed;\n },\n get host(): MeshPeer | null {\n return hostPeer?.publicPeer ?? null;\n },\n\n on(type, handler) {\n core.ensureLive();\n return messenger.on(type, handler as (message: MeshInbound<unknown>) => void);\n },\n\n send(type, payload) {\n core.ensureLive();\n if (!hostPeer) throw new MeshConnectionError('Guest is not paired');\n messenger.sendTo(hostPeer, type, payload);\n },\n get status() {\n return core.status;\n },\n\n tap(handler, tapOptions) {\n return core.tap(handler, tapOptions);\n },\n\n [Symbol.dispose]() {\n this.dispose();\n },\n };\n\n return guest;\n}\n"],"mappings":"A,oMAyBA,SAAgB,gBAAwC,EAA4B,CAA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K,MACd,EAEA,IAAI,EAAS,EAAY,CACvB,OAAOA,EAAK,IAAIiB,EAASE,CAAU,CACrC,EAEA,CAAC,OAAO,UAAW,CACjB,KAAK,QAAQ,CACf,CAGK,CACT,C"}
|
|
1
|
+
{"version":3,"file":"guest.cjs","names":["nativeRtcFactory","createNodeCore","createMessenger","timer","MeshTimeoutError","MeshConnectionError","MeshPairingError","createPeerRecord","normalizeSdp","waitIceGathering","MeshDisposedError","randomId","createProof","require__node","require__proof","require__random","require__rtc","require_errors","_vielzeug_arsenal","rtc","options","clock","maxMessageBytes","channelOpenTimeoutMs","iceGatheringTimeoutMs","core","messenger","hostPeer","openTimer","peer","error","dc","event","pc","state","guest","invitation","cause","answerSdp","peerId","proof","meta","type","handler","payload","tapOptions"],"sources":["../src/guest.ts"],"sourcesContent":["import { timer } from '@vielzeug/arsenal';\nimport {\n DEFAULT_CHANNEL_OPEN_TIMEOUT_MS,\n DEFAULT_ICE_GATHERING_TIMEOUT_MS,\n DEFAULT_MAX_MESSAGE_BYTES,\n} from './_defaults';\nimport { createMessenger, createNodeCore, createPeerRecord, type PeerRecord, wireDisposalSignal } from './_node';\nimport { createProof } from './_proof';\nimport { randomId } from './_random';\nimport { nativeRtcFactory, normalizeSdp, waitIceGathering } from './_rtc';\nimport { MeshConnectionError, MeshDisposedError, type MeshError, MeshPairingError, MeshTimeoutError } from './errors';\nimport type {\n MeshGuest,\n MeshGuestOptions,\n MeshInbound,\n MeshPeer,\n MeshProtocol,\n RTCDataChannelLike,\n RTCPeerConnectionLike,\n} from './types';\n\n/**\n * Create a guest mesh node. The guest pairs with one host by consuming an\n * out-of-band invitation and returning an answer.\n */\nexport function createMeshGuest<P extends MeshProtocol>(options: MeshGuestOptions = {}): MeshGuest<P> {\n const rtc = options.rtc ?? nativeRtcFactory;\n const clock = options.clock ?? (() => Date.now());\n const maxMessageBytes = options.maxMessageBytes ?? DEFAULT_MAX_MESSAGE_BYTES;\n const channelOpenTimeoutMs = options.channelOpenTimeoutMs ?? DEFAULT_CHANNEL_OPEN_TIMEOUT_MS;\n const iceGatheringTimeoutMs = options.iceGatheringTimeoutMs ?? DEFAULT_ICE_GATHERING_TIMEOUT_MS;\n\n const core = createNodeCore();\n const messenger = createMessenger({\n clock,\n emitTap: core.emitTap,\n maxMessageBytes,\n });\n\n let hostPeer: PeerRecord | null = null;\n const openTimer = timer(() => {\n if (hostPeer?.status === 'connecting') {\n failPeer(hostPeer, openTimeoutError());\n }\n });\n\n const openTimeoutError = (): MeshTimeoutError =>\n new MeshTimeoutError(`Timed out waiting ${channelOpenTimeoutMs}ms for the data channel to open`);\n\n function failPeer(peer: PeerRecord, error: MeshError): void {\n // Ignore events from a superseded peer after re-pairing replaced hostPeer.\n if (core.disposed || peer !== hostPeer || peer.status === 'failed') return;\n peer.status = 'failed';\n openTimer.cancel();\n // Teardown is deferred a macrotask so a peer racing its own open\n // deadline reports its timeout rather than this hangup.\n setTimeout(() => {\n peer.dc?.close();\n peer.pc?.close();\n }, 0);\n core.emitTap({ peerId: peer.id, status: 'failed', type: 'peer-status-change' });\n core.emitTap({ error, type: 'error' });\n core.emitTap({ peer: peer.publicPeer, reason: error.message, type: 'peer-left' });\n core.setStatus('failed');\n }\n\n function disconnectPeer(peer: PeerRecord, reason: string): void {\n if (core.disposed || peer !== hostPeer) return;\n if (peer.status === 'disconnected' || peer.status === 'failed') return;\n peer.status = 'disconnected';\n peer.pc?.close();\n core.emitTap({ peerId: peer.id, status: 'disconnected', type: 'peer-status-change' });\n core.emitTap({ peer: peer.publicPeer, reason, type: 'peer-left' });\n core.setStatus('disconnected');\n }\n\n function wireChannel(peer: PeerRecord, pc: RTCPeerConnectionLike, dc: RTCDataChannelLike): void {\n peer.dc = dc;\n\n dc.addEventListener('open', () => {\n if (core.disposed) return;\n openTimer.cancel();\n peer.status = 'connected';\n core.emitTap({ peerId: peer.id, status: 'connected', type: 'peer-status-change' });\n core.emitTap({ peer: peer.publicPeer, type: 'peer-joined' });\n core.setStatus('connected');\n });\n dc.addEventListener('message', (event) => {\n if (core.disposed) return;\n messenger.handleInbound(peer, event.data);\n });\n dc.addEventListener('close', () => {\n if (core.disposed) return;\n if (peer.status === 'connecting')\n failPeer(\n peer,\n openTimer.pending ? openTimeoutError() : new MeshConnectionError('channel closed before opening', peer.id),\n );\n else disconnectPeer(peer, 'channel closed');\n });\n dc.addEventListener('error', () => {\n if (core.disposed) return;\n failPeer(peer, new MeshConnectionError('channel error', peer.id));\n });\n\n // The ice-state emission lives on the pairing-scoped listener above; this\n // one owns channel-lifecycle transitions only, so each ICE change emits\n // exactly one ice-state event. ICE `disconnected` is transient: the\n // channel's own `close`/`error` events own peer teardown.\n pc.addEventListener('iceconnectionstatechange', () => {\n if (core.disposed) return;\n const state = pc.iceConnectionState;\n if (state === 'failed' || state === 'closed') {\n failPeer(peer, new MeshConnectionError(`ice ${state}`, peer.id));\n }\n });\n }\n\n const guest: MeshGuest<P> = {\n async acceptInvitation(invitation, meta) {\n core.ensureLive();\n // A terminal host peer (failed/disconnected) may be replaced by a fresh\n // pairing on the same node: listeners stay attached and keep working.\n if (hostPeer && hostPeer.status !== 'failed' && hostPeer.status !== 'disconnected') {\n throw new MeshPairingError('Guest is already paired');\n }\n if (invitation?.v !== 1) throw new MeshPairingError('Unsupported invitation version');\n if (clock() >= invitation.expiresAt) throw new MeshPairingError('Invitation expired');\n\n if (hostPeer) {\n openTimer.cancel();\n hostPeer.pc?.close();\n hostPeer = null;\n }\n\n core.setStatus('pairing');\n let pc: RTCPeerConnectionLike;\n try {\n pc = rtc.createPeerConnection({ iceServers: [...(options.iceServers ?? [])] });\n } catch (error) {\n core.setStatus('idle');\n throw error;\n }\n\n // The invitation carries no host identity beyond an optional display\n // name: the session id doubles as the host-side peer id for inbound\n // metadata and tap events.\n const peer = createPeerRecord(invitation.sessionId, invitation.hostName, 'host');\n peer.pc = pc;\n hostPeer = peer;\n\n pc.addEventListener('datachannel', (event) => {\n wireChannel(peer, pc, event.channel);\n });\n pc.addEventListener('iceconnectionstatechange', () => {\n if (core.disposed) return;\n core.emitTap({ peerId: peer.id, state: pc.iceConnectionState, type: 'ice-state' });\n });\n\n try {\n await pc.setRemoteDescription({ sdp: normalizeSdp(invitation.sdp), type: 'offer' });\n } catch (cause) {\n hostPeer = null;\n core.setStatus('idle');\n throw new MeshPairingError('Invalid invitation SDP', { cause });\n }\n\n let answerSdp: string;\n try {\n await pc.setLocalDescription(await pc.createAnswer());\n await waitIceGathering(pc, iceGatheringTimeoutMs);\n answerSdp = pc.localDescription?.sdp ?? '';\n } catch (cause) {\n hostPeer = null;\n pc.close();\n core.setStatus('idle');\n throw new MeshConnectionError('Failed to create answer', null, { cause });\n }\n if (core.disposed) {\n pc.close();\n throw new MeshDisposedError();\n }\n if (!answerSdp) {\n hostPeer = null;\n pc.close();\n core.setStatus('idle');\n throw new MeshConnectionError('Local description has no SDP');\n }\n\n const peerId = randomId(options.random);\n let proof: string;\n try {\n proof = await createProof(invitation.secret, answerSdp);\n } catch (cause) {\n hostPeer = null;\n pc.close();\n core.setStatus('idle');\n throw cause;\n }\n core.setStatus('connecting');\n\n // If the channel never opens (host never accepts the answer), fail loudly\n // instead of hanging in 'connecting' forever. The deadline must cover\n // the human carry-back of the answer, not just machine negotiation.\n openTimer.arm(channelOpenTimeoutMs);\n\n return {\n peer: meta?.name === undefined ? { id: peerId } : { id: peerId, name: meta.name },\n proof,\n sdp: answerSdp,\n sessionId: invitation.sessionId,\n v: 1,\n };\n },\n get disposalSignal() {\n return core.disposalCtrl.signal;\n },\n\n dispose() {\n if (core.disposed) return;\n core.markDisposed();\n openTimer.cancel();\n if (hostPeer) {\n hostPeer.dc?.close();\n hostPeer.pc?.close();\n }\n messenger.clear();\n },\n get disposed() {\n return core.disposed;\n },\n get host(): MeshPeer | null {\n return hostPeer?.publicPeer ?? null;\n },\n\n on(type, handler) {\n core.ensureLive();\n return messenger.on(type, handler as (message: MeshInbound<unknown>) => void);\n },\n\n send(type, payload) {\n core.ensureLive();\n if (!hostPeer) throw new MeshConnectionError('Guest is not paired');\n messenger.sendTo(hostPeer, type, payload);\n },\n get status() {\n return core.status;\n },\n\n tap(handler, tapOptions) {\n return core.tap(handler, tapOptions);\n },\n\n [Symbol.dispose]() {\n this.dispose();\n },\n };\n\n wireDisposalSignal(options.signal, () => guest.dispose());\n return guest;\n}\n"],"mappings":"A,oMAyBA,SAAgB,gBAAwC,EAA4B,CAAC,EAAiB,CACpG,IAAMmB,EAAMC,EAAQ,KAAOpB,EAAAA,iBACrBqB,EAAQD,EAAQ,YAAgB,KAAK,IAAI,GACzCE,EAAkBF,EAAQ,iBAAA,MAC1BG,EAAuBH,EAAQ,sBAAA,IAC/BI,EAAwBJ,EAAQ,uBAAA,IAEhCK,EAAOxB,EAAAA,eAAe,EACtByB,EAAYxB,EAAAA,gBAAgB,CAChC,QACA,QAASuB,EAAK,QACd,iBACF,CAAC,EAEGE,EAA8B,KAC5BC,GAAA,EAAYzB,EAAAA,MAAAA,KAAY,CACxBwB,GAAU,SAAW,cACvB,SAASA,EAAU,iBAAiB,CAAC,CAEzC,CAAC,EAEK,qBACJ,IAAIvB,EAAAA,iBAAiB,qBAAqBmB,EAAqB,gCAAgC,EAEjG,SAAS,SAAS,EAAkB,EAAwB,CAEtDE,EAAK,UAAYI,IAASF,GAAYE,EAAK,SAAW,WAC1D,EAAK,OAAS,SACdD,EAAU,OAAO,EAGjB,eAAiB,CACfC,EAAK,IAAI,MAAM,EACfA,EAAK,IAAI,MAAM,CACjB,EAAG,CAAC,EACJJ,EAAK,QAAQ,CAAE,OAAQI,EAAK,GAAI,OAAQ,SAAU,KAAM,oBAAqB,CAAC,EAC9EJ,EAAK,QAAQ,CAAE,QAAO,KAAM,OAAQ,CAAC,EACrCA,EAAK,QAAQ,CAAE,KAAMI,EAAK,WAAY,OAAQC,EAAM,QAAS,KAAM,WAAY,CAAC,EAChFL,EAAK,UAAU,QAAQ,EACzB,CAEA,SAAS,eAAe,EAAkB,EAAsB,CAC1DA,EAAK,UAAY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CAElB,GADAlB,EAAK,WAAW,EACZ,CAACE,EAAU,MAAM,IAAItB,EAAAA,oBAAoB,qBAAqB,EAClEqB,EAAU,OAAOC,EAAUe,EAAME,CAAO,CAC1C,EACA,IAAI,QAAS,CACX,OAAOnB,EAAK,MACd,EAEA,IAAI,EAAS,EAAY,CACvB,OAAOA,EAAK,IAAIkB,EAASE,CAAU,CACrC,EAEA,CAAC,OAAO,UAAW,CACjB,KAAK,QAAQ,CACf,CACF,EAGA,OADAhC,EAAA,mBAAmBO,EAAQ,WAAce,EAAM,QAAQ,CAAC,EACjDA,CACT,C"}
|
package/dist/guest.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guest.d.ts","sourceRoot":"","sources":["../src/guest.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EAGhB,YAAY,EAGb,MAAM,SAAS,CAAC;AAEjB;;;GAGG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,YAAY,EAAE,OAAO,GAAE,gBAAqB,GAAG,SAAS,CAAC,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"guest.d.ts","sourceRoot":"","sources":["../src/guest.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EAGhB,YAAY,EAGb,MAAM,SAAS,CAAC;AAEjB;;;GAGG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,YAAY,EAAE,OAAO,GAAE,gBAAqB,GAAG,SAAS,CAAC,CAAC,CAAC,CA2OpG"}
|