@johnhenry/browsermesh-pod 0.0.0

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.
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Runtime entrypoints for BrowserMesh pods.
3
+ *
4
+ * These convenience functions provide the documented API surface
5
+ * (installPodRuntime, createRuntime, createClient, createServer)
6
+ * as thin wrappers over the existing Pod infrastructure.
7
+ */
8
+ import { Pod } from './pod.mjs'
9
+
10
+ /**
11
+ * Create and boot a Pod in the given context.
12
+ *
13
+ * @param {object} [opts]
14
+ * @param {typeof globalThis} [opts.context] - Global object to install on (default: globalThis)
15
+ * @param {import('@johnhenry/browsermesh-primitives').PodIdentity} [opts.identity] - Pre-existing identity
16
+ * @param {object} [opts.discovery] - Discovery options
17
+ * @param {number} [opts.discoveryTimeout] - Discovery timeout in ms (default: 2000)
18
+ * @param {number} [opts.handshakeTimeout] - Handshake timeout in ms (default: 2000)
19
+ * @returns {Promise<Pod>}
20
+ */
21
+ export async function installPodRuntime(opts = {}) {
22
+ const {
23
+ context = globalThis,
24
+ identity,
25
+ discoveryTimeout = 2000,
26
+ handshakeTimeout = 2000,
27
+ ...rest
28
+ } = opts
29
+
30
+ const pod = new Pod()
31
+ await pod.boot({
32
+ globalThis: context,
33
+ identity,
34
+ discoveryTimeout,
35
+ handshakeTimeout,
36
+ ...rest,
37
+ })
38
+ return pod
39
+ }
40
+
41
+ /**
42
+ * Alias for installPodRuntime — creates and boots a Pod runtime.
43
+ *
44
+ * @param {object} [opts] - Same options as installPodRuntime
45
+ * @returns {Promise<Pod>}
46
+ */
47
+ export const createRuntime = installPodRuntime
48
+
49
+ /**
50
+ * Create a lightweight client pod for messaging and discovery.
51
+ * Boots with a short discovery timeout and no handshake.
52
+ *
53
+ * @param {object} [opts]
54
+ * @param {typeof globalThis} [opts.context] - Global object
55
+ * @param {import('@johnhenry/browsermesh-primitives').PodIdentity} [opts.identity] - Pre-existing identity
56
+ * @param {number} [opts.discoveryTimeout] - Discovery timeout in ms (default: 500)
57
+ * @returns {Promise<Pod>}
58
+ */
59
+ export async function createClient(opts = {}) {
60
+ const {
61
+ context = globalThis,
62
+ identity,
63
+ discoveryTimeout = 500,
64
+ ...rest
65
+ } = opts
66
+
67
+ const pod = new Pod()
68
+ await pod.boot({
69
+ globalThis: context,
70
+ identity,
71
+ discoveryTimeout,
72
+ handshakeTimeout: 100,
73
+ ...rest,
74
+ })
75
+ return pod
76
+ }
77
+
78
+ /**
79
+ * Create a server-oriented pod that listens for connections.
80
+ * Same as installPodRuntime but signals intent as a service provider.
81
+ *
82
+ * @param {object} [opts]
83
+ * @param {typeof globalThis} [opts.context] - Global object
84
+ * @param {import('@johnhenry/browsermesh-primitives').PodIdentity} [opts.identity] - Pre-existing identity
85
+ * @param {number} [opts.discoveryTimeout] - Discovery timeout in ms (default: 2000)
86
+ * @returns {Promise<Pod>}
87
+ */
88
+ export async function createServer(opts = {}) {
89
+ const {
90
+ context = globalThis,
91
+ identity,
92
+ discoveryTimeout = 2000,
93
+ ...rest
94
+ } = opts
95
+
96
+ const pod = new Pod()
97
+ await pod.boot({
98
+ globalThis: context,
99
+ identity,
100
+ discoveryTimeout,
101
+ handshakeTimeout: 2000,
102
+ ...rest,
103
+ })
104
+ return pod
105
+ }
@@ -0,0 +1,164 @@
1
+ /**
2
+ * transport.mjs — Pluggable transport adapters for Pod messaging.
3
+ *
4
+ * A TransportAdapter is any object that provides:
5
+ * send(msg) — send a message to all peers
6
+ * onMessage(handler) — register a message handler
7
+ * open() — connect/initialize the transport
8
+ * close() — disconnect/cleanup
9
+ * ready — boolean, whether the transport is connected
10
+ *
11
+ * Three built-in implementations:
12
+ * BroadcastChannelTransport — browser BroadcastChannel (same-origin tabs)
13
+ * EventEmitterTransport — in-process event emitter (Node.js / testing)
14
+ * NullTransport — no-op (headless / disabled)
15
+ */
16
+
17
+ // ---------------------------------------------------------------------------
18
+ // BroadcastChannelTransport
19
+ // ---------------------------------------------------------------------------
20
+
21
+ /**
22
+ * Transport backed by the browser BroadcastChannel API.
23
+ * Messages are delivered to all same-origin browsing contexts
24
+ * subscribed to the same channel name.
25
+ */
26
+ export class BroadcastChannelTransport {
27
+ #channel = null
28
+ #channelName
29
+ #handler = null
30
+ #BCConstructor
31
+
32
+ /**
33
+ * @param {string} channelName - BroadcastChannel name
34
+ * @param {Function} [BCConstructor] - BroadcastChannel constructor (for testing)
35
+ */
36
+ constructor(channelName, BCConstructor) {
37
+ this.#channelName = channelName
38
+ this.#BCConstructor = BCConstructor || globalThis.BroadcastChannel
39
+ }
40
+
41
+ get ready() { return this.#channel !== null }
42
+
43
+ /**
44
+ * @param {(msg: object) => void} handler
45
+ */
46
+ onMessage(handler) {
47
+ this.#handler = handler
48
+ }
49
+
50
+ async open() {
51
+ if (this.#channel) return
52
+ this.#channel = new this.#BCConstructor(this.#channelName)
53
+ this.#channel.onmessage = (event) => {
54
+ if (this.#handler) this.#handler(event.data)
55
+ }
56
+ }
57
+
58
+ /**
59
+ * @param {object} msg
60
+ */
61
+ send(msg) {
62
+ if (!this.#channel) return
63
+ this.#channel.postMessage(msg)
64
+ }
65
+
66
+ async close() {
67
+ if (this.#channel) {
68
+ this.#channel.close()
69
+ this.#channel = null
70
+ }
71
+ }
72
+ }
73
+
74
+ // ---------------------------------------------------------------------------
75
+ // EventEmitterTransport
76
+ // ---------------------------------------------------------------------------
77
+
78
+ /**
79
+ * In-process transport using a shared subscriber list.
80
+ * Useful for Node.js and testing where BroadcastChannel is unavailable.
81
+ *
82
+ * Create a shared bus and pass it to multiple EventEmitterTransport instances
83
+ * to simulate multi-pod communication:
84
+ *
85
+ * const bus = EventEmitterTransport.createBus()
86
+ * const t1 = new EventEmitterTransport(bus)
87
+ * const t2 = new EventEmitterTransport(bus)
88
+ */
89
+ export class EventEmitterTransport {
90
+ #bus
91
+ #handler = null
92
+ #ready = false
93
+ #boundReceiver = null
94
+
95
+ /**
96
+ * @param {Set<Function>} [bus] - Shared subscriber set
97
+ */
98
+ constructor(bus) {
99
+ this.#bus = bus || EventEmitterTransport.createBus()
100
+ }
101
+
102
+ /**
103
+ * Create a shared message bus.
104
+ * @returns {Set<Function>}
105
+ */
106
+ static createBus() {
107
+ return new Set()
108
+ }
109
+
110
+ get ready() { return this.#ready }
111
+ get bus() { return this.#bus }
112
+
113
+ /**
114
+ * @param {(msg: object) => void} handler
115
+ */
116
+ onMessage(handler) {
117
+ this.#handler = handler
118
+ }
119
+
120
+ async open() {
121
+ if (this.#ready) return
122
+ this.#boundReceiver = (msg, sender) => {
123
+ if (sender !== this && this.#handler) {
124
+ this.#handler(msg)
125
+ }
126
+ }
127
+ this.#bus.add(this.#boundReceiver)
128
+ this.#ready = true
129
+ }
130
+
131
+ /**
132
+ * @param {object} msg
133
+ */
134
+ send(msg) {
135
+ if (!this.#ready) return
136
+ for (const receiver of this.#bus) {
137
+ try { receiver(msg, this) } catch { /* listener errors don't crash */ }
138
+ }
139
+ }
140
+
141
+ async close() {
142
+ if (this.#boundReceiver) {
143
+ this.#bus.delete(this.#boundReceiver)
144
+ this.#boundReceiver = null
145
+ }
146
+ this.#ready = false
147
+ }
148
+ }
149
+
150
+ // ---------------------------------------------------------------------------
151
+ // NullTransport
152
+ // ---------------------------------------------------------------------------
153
+
154
+ /**
155
+ * No-op transport. Messages are silently dropped.
156
+ * Used when no transport is available or desired.
157
+ */
158
+ export class NullTransport {
159
+ get ready() { return true }
160
+ onMessage(_handler) {}
161
+ async open() {}
162
+ send(_msg) {}
163
+ async close() {}
164
+ }