@johnhenry/browsermesh-transport 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Clawser Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # browsermesh-transport
2
+
3
+ WebSocket, WebRTC, WebTransport, relay, and streaming adapters for BrowserMesh.
4
+
5
+ ## Provenance
6
+
7
+ Extracted from the private `clawser` monorepo (previously `packages/browsermesh-transport`), where it was manually published to npm, unscoped, as `browsermesh-transport@0.1.0` (2026-07-17) with no CI ever automating that publish. This is its first release as part of the `@johnhenry/browsermesh` monorepo; the version restarts at `0.0.0` per family convention.
8
+
9
+
10
+ ## Modules
11
+
12
+ | Module | Key Exports |
13
+ |--------|-------------|
14
+ | transport | `MeshTransport`, `MockMeshTransport`, `MeshTransportNegotiator` |
15
+ | websocket | `WebSocketTransport`, `WebRTCTransport`, `WebTransportTransport`, `NATTraversal`, `TransportFactory` |
16
+ | webrtc | `WebRTCPeerConnection`, `WebRTCMeshManager`, `WebRTCTransportAdapter` |
17
+ | webtransport | `WebTransportBridge`, `WebTransportAdapterFactory` |
18
+ | relay | `MeshRelayClient`, `MockRelayServer` |
19
+ | gateway | `GatewayNode`, `GatewayDiscovery`, `RouteTable` |
20
+ | streams | `MeshStream`, `StreamMultiplexer` |
21
+ | cross-origin | `CrossOriginBridge`, `CrossOriginHandshake`, `RateLimiter` |
22
+ | wsh-bridge | `MeshWshBridge` |
23
+ | wisp | `WispTransport` |
24
+ | channel-relay | `ChannelRelay` |
25
+
26
+ ## Install
27
+
28
+ ```bash
29
+ npm install @johnhenry/browsermesh-transport @johnhenry/browsermesh-primitives
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ```js
35
+ import { MeshTransport, WebSocketTransport, StreamMultiplexer } from 'browsermesh-transport';
36
+ ```
37
+
38
+ ## License
39
+
40
+ MIT
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@johnhenry/browsermesh-transport",
3
+ "version": "0.0.0",
4
+ "description": "WebSocket, WebRTC, WebTransport, and relay adapters for BrowserMesh",
5
+ "type": "module",
6
+ "main": "./src/index.mjs",
7
+ "exports": {
8
+ ".": "./src/index.mjs"
9
+ },
10
+ "files": [
11
+ "src",
12
+ "LICENSE",
13
+ "README.md"
14
+ ],
15
+ "license": "MIT",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/johnhenry/browsermesh",
19
+ "directory": "packages/browsermesh-transport"
20
+ },
21
+ "peerDependencies": {
22
+ "@johnhenry/browsermesh-primitives": ">=0.0.0"
23
+ },
24
+ "scripts": {
25
+ "test": "node --import ./test/_setup-globals.mjs --test test/*.test.mjs"
26
+ },
27
+ "homepage": "https://opensource.johnhenry.me/browsermesh/",
28
+ "engines": {
29
+ "node": ">=24.0.0"
30
+ }
31
+ }
@@ -0,0 +1,225 @@
1
+ import { silentCatch } from './silent-catch.mjs'
2
+ // clawser-channel-relay.js — Channel Relay Plugin
3
+ //
4
+ // Virtual server route for webhooks + BroadcastChannel relay.
5
+ // Normalizes inbound messages via createInboundMessage().
6
+ // Supports named routes for dispatching webhook payloads.
7
+
8
+ // ── Helpers ──────────────────────────────────────────────────
9
+
10
+ let relayCounter = 0;
11
+
12
+ function generateId() {
13
+ return `relay_${Date.now()}_${++relayCounter}`;
14
+ }
15
+
16
+ // ── ChannelRelay ─────────────────────────────────────────────
17
+
18
+ /**
19
+ * Virtual webhook server + BroadcastChannel relay.
20
+ * Receives inbound webhooks via handleWebhook() and relays via BroadcastChannel.
21
+ * Supports named routes for dispatching different webhook types.
22
+ */
23
+ export class ChannelRelay {
24
+ /** @type {object} */
25
+ config;
26
+
27
+ /** @type {boolean} */
28
+ running = false;
29
+
30
+ /** @type {Function|null} */
31
+ _callback = null;
32
+
33
+ /** @type {object|null} BroadcastChannel instance */
34
+ _bc = null;
35
+
36
+ /** @type {Map<string, Function>} named route handlers */
37
+ #routes = new Map();
38
+
39
+ /**
40
+ * @param {object} opts
41
+ * @param {number} [opts.port=0] — virtual port (for documentation/config)
42
+ * @param {string} [opts.path='/webhook'] — webhook path
43
+ * @param {string} [opts.bcName='clawser-relay'] — BroadcastChannel name
44
+ */
45
+ constructor(opts = {}) {
46
+ this.config = {
47
+ port: opts.port || 0,
48
+ path: opts.path || '/webhook',
49
+ bcName: opts.bcName || 'clawser-relay',
50
+ };
51
+ }
52
+
53
+ // ── Message normalization ───────────────────────────────
54
+
55
+ /**
56
+ * Normalize a raw webhook payload into standard inbound message format.
57
+ * @param {object} raw
58
+ * @returns {object} Standard InboundMessage
59
+ */
60
+ createInboundMessage(raw) {
61
+ const senderRaw = raw.sender;
62
+ const senderObj = (typeof senderRaw === 'object' && senderRaw !== null)
63
+ ? senderRaw
64
+ : { id: senderRaw || 'unknown', name: senderRaw || 'Unknown' };
65
+
66
+ return {
67
+ id: raw.id || generateId(),
68
+ channel: 'relay',
69
+ channelId: raw.channelId || null,
70
+ sender: {
71
+ id: senderObj.id || 'unknown',
72
+ name: senderObj.name || 'Unknown',
73
+ username: senderObj.username || null,
74
+ },
75
+ content: raw.body || raw.text || raw.content || '',
76
+ attachments: raw.attachments || [],
77
+ replyTo: raw.replyTo || null,
78
+ timestamp: raw.timestamp || Date.now(),
79
+ };
80
+ }
81
+
82
+ // ── Lifecycle ───────────────────────────────────────────
83
+
84
+ /**
85
+ * Start the relay — creates BroadcastChannel and begins listening.
86
+ */
87
+ start() {
88
+ if (this.running) return;
89
+ this.running = true;
90
+
91
+ // Create BroadcastChannel for cross-tab relay
92
+ if (typeof BroadcastChannel !== 'undefined' && !this._bc) {
93
+ try {
94
+ this._bc = new BroadcastChannel(this.config.bcName);
95
+ this._bc.onmessage = (event) => this._handleBcMessage(event);
96
+ } catch {
97
+ // BroadcastChannel not available — proceed without
98
+ }
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Stop the relay — closes BroadcastChannel.
104
+ */
105
+ stop() {
106
+ if (!this.running) return;
107
+ this.running = false;
108
+
109
+ if (this._bc) {
110
+ try { this._bc.close(); } catch (e) { silentCatch('clawser-channel-relay', 'this._bc.close', e) }
111
+ this._bc = null;
112
+ }
113
+ }
114
+
115
+ // ── Inbound handling ────────────────────────────────────
116
+
117
+ /**
118
+ * Register a callback for inbound messages.
119
+ * @param {Function} callback — (msg: InboundMessage) => void
120
+ */
121
+ onMessage(callback) {
122
+ this._callback = callback;
123
+ }
124
+
125
+ /**
126
+ * Handle an incoming webhook payload.
127
+ * Dispatches to named route if payload has a `route` property,
128
+ * otherwise normalizes and forwards to onMessage callback.
129
+ * @param {object} payload
130
+ */
131
+ handleWebhook(payload) {
132
+ if (!this.running) return;
133
+
134
+ // Check for named route
135
+ if (payload.route && this.#routes.has(payload.route)) {
136
+ this.#routes.get(payload.route)(payload);
137
+ return;
138
+ }
139
+
140
+ // Normalize and dispatch
141
+ const msg = this.createInboundMessage(payload);
142
+ if (this._callback) {
143
+ this._callback(msg);
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Handle a message received from BroadcastChannel.
149
+ * @param {object} event — MessageEvent-like {data: ...}
150
+ */
151
+ _handleBcMessage(event) {
152
+ if (!this.running) return;
153
+ const raw = event.data || event;
154
+ const msg = this.createInboundMessage(raw);
155
+ if (this._callback) {
156
+ this._callback(msg);
157
+ }
158
+ }
159
+
160
+ // ── Outbound ────────────────────────────────────────────
161
+
162
+ /**
163
+ * Send a message via BroadcastChannel relay.
164
+ * @param {string} text
165
+ * @param {object} [opts]
166
+ * @returns {boolean}
167
+ */
168
+ sendMessage(text, opts = {}) {
169
+ if (!this.running) return false;
170
+
171
+ const msg = {
172
+ text,
173
+ channel: 'relay',
174
+ sender: opts.sender || 'clawser',
175
+ timestamp: Date.now(),
176
+ };
177
+
178
+ if (this._bc) {
179
+ try {
180
+ this._bc.postMessage(msg);
181
+ return true;
182
+ } catch {
183
+ return false;
184
+ }
185
+ }
186
+ return false;
187
+ }
188
+
189
+ // ── Route table ─────────────────────────────────────────
190
+
191
+ /**
192
+ * Add a named route handler.
193
+ * @param {string} name
194
+ * @param {Function} handler — (payload) => void
195
+ */
196
+ addRoute(name, handler) {
197
+ this.#routes.set(name, handler);
198
+ }
199
+
200
+ /**
201
+ * Remove a named route handler.
202
+ * @param {string} name
203
+ * @returns {boolean}
204
+ */
205
+ removeRoute(name) {
206
+ return this.#routes.delete(name);
207
+ }
208
+
209
+ /**
210
+ * Check if a named route exists.
211
+ * @param {string} name
212
+ * @returns {boolean}
213
+ */
214
+ hasRoute(name) {
215
+ return this.#routes.has(name);
216
+ }
217
+
218
+ /**
219
+ * List all registered route names.
220
+ * @returns {string[]}
221
+ */
222
+ listRoutes() {
223
+ return [...this.#routes.keys()];
224
+ }
225
+ }