@johnhenry/browsermesh-pod 0.0.0 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -4
- package/package.json +9 -5
- package/src/index.d.ts +326 -0
package/README.md
CHANGED
|
@@ -1,14 +1,47 @@
|
|
|
1
1
|
# browsermesh-pod
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@johnhenry/browsermesh-pod)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
3
6
|
Pod base class for browser execution contexts with Ed25519 identity, BroadcastChannel discovery, and peer messaging.
|
|
4
7
|
|
|
5
8
|
A Pod is any browser execution context (window, iframe, worker, service worker) that can execute code, receive messages, and be discovered/addressed. This package provides the standalone base class with zero framework dependencies.
|
|
6
9
|
|
|
7
10
|
Pods automatically generate an Ed25519 cryptographic identity, detect their execution context, discover same-origin peers via BroadcastChannel, and establish roles (autonomous, child, peer).
|
|
8
11
|
|
|
12
|
+
## Contents
|
|
13
|
+
|
|
14
|
+
- [Why this exists](#why-this-exists)
|
|
15
|
+
- [Used by](#used-by)
|
|
16
|
+
- [Provenance](#provenance)
|
|
17
|
+
- [Install](#install)
|
|
18
|
+
- [Quick Start](#quick-start)
|
|
19
|
+
- [Boot Sequence](#boot-sequence)
|
|
20
|
+
- [Boot Options](#boot-options)
|
|
21
|
+
- [API](#api)
|
|
22
|
+
- [Getters](#getters)
|
|
23
|
+
- [Methods](#methods)
|
|
24
|
+
- [Events](#events)
|
|
25
|
+
- [Subclass Hooks](#subclass-hooks)
|
|
26
|
+
- [Runtime Convenience Functions](#runtime-convenience-functions)
|
|
27
|
+
- [Pod Kinds](#pod-kinds)
|
|
28
|
+
- [Capabilities](#capabilities)
|
|
29
|
+
- [Wire Protocol](#wire-protocol)
|
|
30
|
+
- [InjectedPod](#injectedpod)
|
|
31
|
+
- [Peer Dependency](#peer-dependency)
|
|
32
|
+
- [License](#license)
|
|
33
|
+
|
|
34
|
+
## Why this exists
|
|
35
|
+
|
|
36
|
+
A mesh needs *something* to be the addressable unit -- the thing with an identity, a place in the topology, and a lifecycle other peers can observe. In a browser that unit is naturally an execution context (a tab, an iframe, a worker), but the platform gives you no way to ask "who else is running near me" or "am I a top-level window or something a parent spawned." `Pod` is that missing base class: it generates an identity, classifies its own context (`window`, `iframe`, `worker`, `service-worker`, ...), runs a 6-phase boot sequence to find and be found by same-origin peers, and gives every higher-level package in this monorepo (and outside it) a stable `podId`/`role`/`peers` surface to build on, without dragging in any framework or transport of its own. It depends on nothing but `browsermesh-primitives`, deliberately -- boot and identity should work the same whether or not a mesh transport, sync engine, or kernel is present.
|
|
37
|
+
|
|
38
|
+
## Used by
|
|
39
|
+
|
|
40
|
+
`@johnhenry/browsermesh-embed`'s `EmbeddedPod` extends `Pod` directly, layering a `sendMessage`/message-log widget on top of the same boot sequence and peer discovery this package provides -- see that package's README for what it adds.
|
|
41
|
+
|
|
9
42
|
## Provenance
|
|
10
43
|
|
|
11
|
-
Previously published as `browsermesh-pod@0.2.1
|
|
44
|
+
Previously maintained as an independent, standalone repository and published to npm, unscoped, as `browsermesh-pod@0.2.1` (initial release `0.1.0`, 2026-03-15), with its own CI already wired up (tests, CodeQL, dependency review). Imported into the `@johnhenry/browsermesh` monorepo via `git subtree` -- preserving its full commit history -- and rescoped to `@johnhenry/browsermesh-pod`; the version restarts at `0.0.0` per family convention.
|
|
12
45
|
|
|
13
46
|
## Install
|
|
14
47
|
|
|
@@ -21,7 +54,7 @@ npm install @johnhenry/browsermesh-pod @johnhenry/browsermesh-primitives
|
|
|
21
54
|
## Quick Start
|
|
22
55
|
|
|
23
56
|
```js
|
|
24
|
-
import { Pod } from 'browsermesh-pod'
|
|
57
|
+
import { Pod } from '@johnhenry/browsermesh-pod'
|
|
25
58
|
|
|
26
59
|
const pod = new Pod()
|
|
27
60
|
await pod.boot()
|
|
@@ -120,7 +153,7 @@ await pod.boot({
|
|
|
120
153
|
## Runtime Convenience Functions
|
|
121
154
|
|
|
122
155
|
```js
|
|
123
|
-
import { installPodRuntime, createRuntime, createClient, createServer } from 'browsermesh-pod'
|
|
156
|
+
import { installPodRuntime, createRuntime, createClient, createServer } from '@johnhenry/browsermesh-pod'
|
|
124
157
|
|
|
125
158
|
// Create and boot a pod (createRuntime is an alias)
|
|
126
159
|
const pod = await installPodRuntime({ context: globalThis })
|
|
@@ -178,7 +211,7 @@ Message factories: `createHello()`, `createHelloAck()`, `createGoodbye()`, `crea
|
|
|
178
211
|
Lightweight subclass for Chrome extension injection or bookmarklet use. Adds page text extraction, structured data extraction, and a visual overlay indicator.
|
|
179
212
|
|
|
180
213
|
```js
|
|
181
|
-
import { InjectedPod } from 'browsermesh-pod'
|
|
214
|
+
import { InjectedPod } from '@johnhenry/browsermesh-pod'
|
|
182
215
|
|
|
183
216
|
const pod = new InjectedPod({ extensionBridge: chrome.runtime.connect() })
|
|
184
217
|
await pod.boot()
|
package/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnhenry/browsermesh-pod",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Pod base class for browser execution contexts with Ed25519 identity, BroadcastChannel discovery, and peer messaging",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.mjs",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
7
8
|
"exports": {
|
|
8
|
-
".":
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"import": "./src/index.mjs"
|
|
12
|
+
}
|
|
9
13
|
},
|
|
10
14
|
"files": [
|
|
11
15
|
"src",
|
|
@@ -31,12 +35,12 @@
|
|
|
31
35
|
"test": "node --test test/*.test.mjs"
|
|
32
36
|
},
|
|
33
37
|
"peerDependencies": {
|
|
34
|
-
"@johnhenry/browsermesh-primitives": ">=0.0.0"
|
|
38
|
+
"@johnhenry/browsermesh-primitives": ">=0.0.1 <1.0.0"
|
|
35
39
|
},
|
|
36
40
|
"devDependencies": {
|
|
37
|
-
"@johnhenry/browsermesh-primitives": "^0.0.
|
|
41
|
+
"@johnhenry/browsermesh-primitives": "^0.0.2"
|
|
38
42
|
},
|
|
39
43
|
"engines": {
|
|
40
|
-
"node": ">=
|
|
44
|
+
"node": ">=26.0.0"
|
|
41
45
|
}
|
|
42
46
|
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
// Type definitions for @johnhenry/browsermesh-pod
|
|
2
|
+
|
|
3
|
+
import type { PodIdentity } from "@johnhenry/browsermesh-primitives";
|
|
4
|
+
|
|
5
|
+
// ── Pod kind & capability detection ─────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
export type PodKind =
|
|
8
|
+
| "service-worker"
|
|
9
|
+
| "shared-worker"
|
|
10
|
+
| "worker"
|
|
11
|
+
| "worklet"
|
|
12
|
+
| "server"
|
|
13
|
+
| "iframe"
|
|
14
|
+
| "spawned"
|
|
15
|
+
| "window";
|
|
16
|
+
|
|
17
|
+
export declare function detectPodKind(g?: object): PodKind;
|
|
18
|
+
|
|
19
|
+
export interface PodCapabilities {
|
|
20
|
+
messaging: {
|
|
21
|
+
postMessage: boolean;
|
|
22
|
+
messageChannel: boolean;
|
|
23
|
+
broadcastChannel: boolean;
|
|
24
|
+
sharedWorker: boolean;
|
|
25
|
+
serviceWorker: boolean;
|
|
26
|
+
};
|
|
27
|
+
network: {
|
|
28
|
+
fetch: boolean;
|
|
29
|
+
webSocket: boolean;
|
|
30
|
+
webTransport: boolean;
|
|
31
|
+
webRTC: boolean;
|
|
32
|
+
};
|
|
33
|
+
storage: {
|
|
34
|
+
indexedDB: boolean;
|
|
35
|
+
cacheAPI: boolean;
|
|
36
|
+
opfs: boolean;
|
|
37
|
+
};
|
|
38
|
+
compute: {
|
|
39
|
+
wasm: boolean;
|
|
40
|
+
sharedArrayBuffer: boolean;
|
|
41
|
+
offscreenCanvas: boolean;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export declare function detectCapabilities(g?: object): PodCapabilities;
|
|
46
|
+
|
|
47
|
+
// ── Wire protocol messages ───────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
export declare const POD_HELLO: "pod:hello";
|
|
50
|
+
export declare const POD_HELLO_ACK: "pod:hello-ack";
|
|
51
|
+
export declare const POD_GOODBYE: "pod:goodbye";
|
|
52
|
+
export declare const POD_MESSAGE: "pod:message";
|
|
53
|
+
export declare const POD_RPC_REQUEST: "pod:rpc-request";
|
|
54
|
+
export declare const POD_RPC_RESPONSE: "pod:rpc-response";
|
|
55
|
+
|
|
56
|
+
export interface PodHelloMessage {
|
|
57
|
+
type: typeof POD_HELLO;
|
|
58
|
+
podId: string;
|
|
59
|
+
kind: string;
|
|
60
|
+
capabilities: PodCapabilities | null;
|
|
61
|
+
ts: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface PodHelloAckMessage {
|
|
65
|
+
type: typeof POD_HELLO_ACK;
|
|
66
|
+
podId: string;
|
|
67
|
+
kind: string;
|
|
68
|
+
targetPodId: string;
|
|
69
|
+
ts: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface PodGoodbyeMessage {
|
|
73
|
+
type: typeof POD_GOODBYE;
|
|
74
|
+
podId: string;
|
|
75
|
+
ts: number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface PodMessage {
|
|
79
|
+
type: typeof POD_MESSAGE;
|
|
80
|
+
from: string;
|
|
81
|
+
to: string;
|
|
82
|
+
payload: unknown;
|
|
83
|
+
ts: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface PodRpcRequestMessage {
|
|
87
|
+
type: typeof POD_RPC_REQUEST;
|
|
88
|
+
from: string;
|
|
89
|
+
to: string;
|
|
90
|
+
method: string;
|
|
91
|
+
params: unknown;
|
|
92
|
+
requestId: string;
|
|
93
|
+
ts: number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface PodRpcResponseMessage {
|
|
97
|
+
type: typeof POD_RPC_RESPONSE;
|
|
98
|
+
from: string;
|
|
99
|
+
to: string;
|
|
100
|
+
requestId: string;
|
|
101
|
+
result: unknown;
|
|
102
|
+
error: string | null;
|
|
103
|
+
ts: number;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export declare function createHello(opts: {
|
|
107
|
+
podId: string;
|
|
108
|
+
kind: string;
|
|
109
|
+
capabilities?: PodCapabilities | null;
|
|
110
|
+
}): PodHelloMessage;
|
|
111
|
+
|
|
112
|
+
export declare function createHelloAck(opts: {
|
|
113
|
+
podId: string;
|
|
114
|
+
kind: string;
|
|
115
|
+
targetPodId: string;
|
|
116
|
+
}): PodHelloAckMessage;
|
|
117
|
+
|
|
118
|
+
export declare function createGoodbye(opts: { podId: string }): PodGoodbyeMessage;
|
|
119
|
+
|
|
120
|
+
export declare function createMessage(opts: {
|
|
121
|
+
from: string;
|
|
122
|
+
to: string;
|
|
123
|
+
payload?: unknown;
|
|
124
|
+
}): PodMessage;
|
|
125
|
+
|
|
126
|
+
export declare function createRpcRequest(opts: {
|
|
127
|
+
from: string;
|
|
128
|
+
to: string;
|
|
129
|
+
method: string;
|
|
130
|
+
params?: unknown;
|
|
131
|
+
requestId: string;
|
|
132
|
+
}): PodRpcRequestMessage;
|
|
133
|
+
|
|
134
|
+
export declare function createRpcResponse(opts: {
|
|
135
|
+
from: string;
|
|
136
|
+
to: string;
|
|
137
|
+
requestId: string;
|
|
138
|
+
result?: unknown;
|
|
139
|
+
error?: string;
|
|
140
|
+
}): PodRpcResponseMessage;
|
|
141
|
+
|
|
142
|
+
// ── Transport adapters ───────────────────────────────────────────────────────
|
|
143
|
+
|
|
144
|
+
export interface TransportAdapter {
|
|
145
|
+
readonly ready: boolean;
|
|
146
|
+
onMessage(handler: (msg: any) => void): void;
|
|
147
|
+
open(): Promise<void>;
|
|
148
|
+
send(msg: any): void;
|
|
149
|
+
close(): Promise<void>;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export declare class BroadcastChannelTransport implements TransportAdapter {
|
|
153
|
+
constructor(channelName: string, BCConstructor?: new (name: string) => any);
|
|
154
|
+
get ready(): boolean;
|
|
155
|
+
onMessage(handler: (msg: any) => void): void;
|
|
156
|
+
open(): Promise<void>;
|
|
157
|
+
send(msg: any): void;
|
|
158
|
+
close(): Promise<void>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export declare class EventEmitterTransport implements TransportAdapter {
|
|
162
|
+
constructor(bus?: Set<(msg: any, sender: EventEmitterTransport) => void>);
|
|
163
|
+
static createBus(): Set<(msg: any, sender: EventEmitterTransport) => void>;
|
|
164
|
+
get ready(): boolean;
|
|
165
|
+
get bus(): Set<(msg: any, sender: EventEmitterTransport) => void>;
|
|
166
|
+
onMessage(handler: (msg: any) => void): void;
|
|
167
|
+
open(): Promise<void>;
|
|
168
|
+
send(msg: any): void;
|
|
169
|
+
close(): Promise<void>;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export declare class NullTransport implements TransportAdapter {
|
|
173
|
+
get ready(): boolean;
|
|
174
|
+
onMessage(handler: (msg: any) => void): void;
|
|
175
|
+
open(): Promise<void>;
|
|
176
|
+
send(msg: any): void;
|
|
177
|
+
close(): Promise<void>;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// ── Discovery adapters ────────────────────────────────────────────────────────
|
|
181
|
+
|
|
182
|
+
export interface PeerFoundInfo {
|
|
183
|
+
podId: string;
|
|
184
|
+
kind?: string;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export interface PeerLostInfo {
|
|
188
|
+
podId: string;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export interface DiscoveryAdapter {
|
|
192
|
+
onPeerDiscovered(handler: (info: PeerFoundInfo) => void): void;
|
|
193
|
+
onPeerLost(handler: (info: PeerLostInfo) => void): void;
|
|
194
|
+
onMessage(handler: (msg: any) => void): void;
|
|
195
|
+
start(): Promise<void>;
|
|
196
|
+
stop(opts?: { silent?: boolean }): Promise<void>;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export declare class TransportDiscovery implements DiscoveryAdapter {
|
|
200
|
+
constructor(opts: {
|
|
201
|
+
transport: TransportAdapter;
|
|
202
|
+
localPodId: string;
|
|
203
|
+
localKind: string;
|
|
204
|
+
capabilities?: PodCapabilities | null;
|
|
205
|
+
timeout?: number;
|
|
206
|
+
});
|
|
207
|
+
onPeerDiscovered(handler: (info: PeerFoundInfo) => void): void;
|
|
208
|
+
onPeerLost(handler: (info: PeerLostInfo) => void): void;
|
|
209
|
+
onMessage(handler: (msg: any) => void): void;
|
|
210
|
+
start(): Promise<void>;
|
|
211
|
+
stop(opts?: { silent?: boolean }): Promise<void>;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export declare class NullDiscovery implements DiscoveryAdapter {
|
|
215
|
+
onPeerDiscovered(handler: (info: PeerFoundInfo) => void): void;
|
|
216
|
+
onPeerLost(handler: (info: PeerLostInfo) => void): void;
|
|
217
|
+
onMessage(handler: (msg: any) => void): void;
|
|
218
|
+
start(): Promise<void>;
|
|
219
|
+
stop(opts?: { silent?: boolean }): Promise<void>;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// ── Pod ────────────────────────────────────────────────────────────────────
|
|
223
|
+
|
|
224
|
+
export type PodState = "idle" | "booting" | "ready" | "shutdown";
|
|
225
|
+
export type PodRole = "autonomous" | "child" | "peer" | "controlled" | "hybrid";
|
|
226
|
+
|
|
227
|
+
export interface PeerInfo {
|
|
228
|
+
podId: string;
|
|
229
|
+
kind?: string;
|
|
230
|
+
role?: string;
|
|
231
|
+
lastSeen: number;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export interface PodBootOptions {
|
|
235
|
+
identity?: PodIdentity | { podId: string };
|
|
236
|
+
transport?: TransportAdapter;
|
|
237
|
+
discovery?: DiscoveryAdapter;
|
|
238
|
+
discoveryChannel?: string;
|
|
239
|
+
handshakeTimeout?: number;
|
|
240
|
+
discoveryTimeout?: number;
|
|
241
|
+
globalThis?: typeof globalThis;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export interface PodShutdownOptions {
|
|
245
|
+
silent?: boolean;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export interface PodJSON {
|
|
249
|
+
podId: string | null;
|
|
250
|
+
kind: PodKind | null;
|
|
251
|
+
role: PodRole;
|
|
252
|
+
state: PodState;
|
|
253
|
+
capabilities: PodCapabilities | null;
|
|
254
|
+
peerCount: number;
|
|
255
|
+
peers: string[];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export declare class Pod {
|
|
259
|
+
constructor();
|
|
260
|
+
|
|
261
|
+
get podId(): string | null;
|
|
262
|
+
get identity(): PodIdentity | null;
|
|
263
|
+
get capabilities(): PodCapabilities | null;
|
|
264
|
+
get kind(): PodKind | null;
|
|
265
|
+
get role(): PodRole;
|
|
266
|
+
get state(): PodState;
|
|
267
|
+
get peers(): Map<string, PeerInfo>;
|
|
268
|
+
get transport(): BroadcastChannelTransport | EventEmitterTransport | NullTransport | null;
|
|
269
|
+
|
|
270
|
+
boot(opts?: PodBootOptions): Promise<void>;
|
|
271
|
+
shutdown(opts?: PodShutdownOptions): Promise<void>;
|
|
272
|
+
send(targetPodId: string, payload: unknown): void;
|
|
273
|
+
broadcast(payload: unknown): void;
|
|
274
|
+
on(event: string, cb: (data: any) => void): void;
|
|
275
|
+
off(event: string, cb: (data: any) => void): void;
|
|
276
|
+
toJSON(): PodJSON;
|
|
277
|
+
|
|
278
|
+
protected _onInstallListeners(g: object): void;
|
|
279
|
+
protected _onReady(): void;
|
|
280
|
+
protected _onMessage(msg: any): void;
|
|
281
|
+
protected _emit(event: string, data: any): void;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export interface InjectedPodOptions {
|
|
285
|
+
extensionBridge?: { postMessage: (msg: any) => void } | null;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export interface PageContext {
|
|
289
|
+
url: string;
|
|
290
|
+
title: string;
|
|
291
|
+
origin: string;
|
|
292
|
+
favicon: string;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export interface StructuredPageData {
|
|
296
|
+
title: string;
|
|
297
|
+
url: string;
|
|
298
|
+
meta: Record<string, string>;
|
|
299
|
+
headings: Array<{ tag: string; text: string }>;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export declare class InjectedPod extends Pod {
|
|
303
|
+
constructor(opts?: InjectedPodOptions);
|
|
304
|
+
get pageContext(): PageContext | null;
|
|
305
|
+
extractText(): string;
|
|
306
|
+
extractStructured(): StructuredPageData | Record<string, never>;
|
|
307
|
+
showOverlay(): void;
|
|
308
|
+
hideOverlay(): void;
|
|
309
|
+
emit(event: string, data: any): void;
|
|
310
|
+
shutdown(opts?: PodShutdownOptions): Promise<void>;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ── Runtime entrypoints ───────────────────────────────────────────────────────
|
|
314
|
+
|
|
315
|
+
export interface RuntimeOptions {
|
|
316
|
+
context?: typeof globalThis;
|
|
317
|
+
identity?: PodIdentity;
|
|
318
|
+
discoveryTimeout?: number;
|
|
319
|
+
handshakeTimeout?: number;
|
|
320
|
+
[key: string]: unknown;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export declare function installPodRuntime(opts?: RuntimeOptions): Promise<Pod>;
|
|
324
|
+
export declare const createRuntime: typeof installPodRuntime;
|
|
325
|
+
export declare function createClient(opts?: RuntimeOptions): Promise<Pod>;
|
|
326
|
+
export declare function createServer(opts?: RuntimeOptions): Promise<Pod>;
|