@johnhenry/browsermesh-primitives 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 +21 -0
- package/README.md +117 -0
- package/package.json +36 -0
- package/src/acl.mjs +471 -0
- package/src/capability.mjs +99 -0
- package/src/constants.mjs +265 -0
- package/src/crdt.mjs +919 -0
- package/src/errors.mjs +46 -0
- package/src/identity.mjs +97 -0
- package/src/index.d.ts +346 -0
- package/src/index.mjs +32 -0
- package/src/test-transport.mjs +474 -0
- package/src/trust.mjs +91 -0
- package/src/wire.mjs +80 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse a capability scope string into structured parts.
|
|
3
|
+
*
|
|
4
|
+
* Scope grammar: `<namespace>:<resource>:<action>`
|
|
5
|
+
* Example: `"mesh:crdt:write"`, `"mesh:transport:*"`
|
|
6
|
+
*
|
|
7
|
+
* @param {string} scope - Scope string to parse
|
|
8
|
+
* @returns {{ namespace: string, resource: string, action: string }}
|
|
9
|
+
*/
|
|
10
|
+
export function parseScope(scope) {
|
|
11
|
+
const parts = scope.split(':');
|
|
12
|
+
return {
|
|
13
|
+
namespace: parts[0] || '*',
|
|
14
|
+
resource: parts[1] || '*',
|
|
15
|
+
action: parts[2] || '*',
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Check whether a granted scope matches a required scope.
|
|
21
|
+
* Supports wildcard (`*`) matching at any level.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} granted - The scope that was granted
|
|
24
|
+
* @param {string} required - The scope being checked against
|
|
25
|
+
* @returns {boolean} True if granted covers required
|
|
26
|
+
*/
|
|
27
|
+
export function matchScope(granted, required) {
|
|
28
|
+
const g = parseScope(granted);
|
|
29
|
+
const r = parseScope(required);
|
|
30
|
+
|
|
31
|
+
const matches = (grantedPart, requiredPart) =>
|
|
32
|
+
grantedPart === '*' || grantedPart === requiredPart;
|
|
33
|
+
|
|
34
|
+
return matches(g.namespace, r.namespace) &&
|
|
35
|
+
matches(g.resource, r.resource) &&
|
|
36
|
+
matches(g.action, r.action);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Represents a capability token — a signed grant of specific permissions.
|
|
41
|
+
*
|
|
42
|
+
* @class
|
|
43
|
+
*/
|
|
44
|
+
export class CapabilityToken {
|
|
45
|
+
/**
|
|
46
|
+
* @param {object} opts
|
|
47
|
+
* @param {string} opts.issuer - Pod ID of the issuer
|
|
48
|
+
* @param {string} opts.subject - Pod ID of the grantee
|
|
49
|
+
* @param {string[]} opts.scopes - Granted scope strings
|
|
50
|
+
* @param {number} opts.expiresAt - Unix timestamp (seconds) of expiry
|
|
51
|
+
* @param {Uint8Array} [opts.signature] - Ed25519 signature over the token
|
|
52
|
+
*/
|
|
53
|
+
constructor({ issuer, subject, scopes, expiresAt, signature }) {
|
|
54
|
+
/** @type {string} */
|
|
55
|
+
this.issuer = issuer;
|
|
56
|
+
/** @type {string} */
|
|
57
|
+
this.subject = subject;
|
|
58
|
+
/** @type {string[]} */
|
|
59
|
+
this.scopes = scopes;
|
|
60
|
+
/** @type {number} */
|
|
61
|
+
this.expiresAt = expiresAt;
|
|
62
|
+
/** @type {Uint8Array|undefined} */
|
|
63
|
+
this.signature = signature;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Check if the token has expired.
|
|
68
|
+
*
|
|
69
|
+
* @param {number} [now=Date.now()/1000] - Current time in seconds
|
|
70
|
+
* @returns {boolean}
|
|
71
|
+
*/
|
|
72
|
+
isExpired(now = Date.now() / 1000) {
|
|
73
|
+
return this.expiresAt > 0 && now >= this.expiresAt;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Check if this token's scopes cover a required scope.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} scope - The scope to check
|
|
80
|
+
* @returns {boolean}
|
|
81
|
+
*/
|
|
82
|
+
covers(scope) {
|
|
83
|
+
return this.scopes.some(s => matchScope(s, scope));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Serialize the token to a plain JSON-compatible object.
|
|
88
|
+
*
|
|
89
|
+
* @returns {{ issuer: string, subject: string, scopes: string[], expiresAt: number }}
|
|
90
|
+
*/
|
|
91
|
+
toJSON() {
|
|
92
|
+
return {
|
|
93
|
+
issuer: this.issuer,
|
|
94
|
+
subject: this.subject,
|
|
95
|
+
scopes: this.scopes,
|
|
96
|
+
expiresAt: this.expiresAt,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BrowserMesh wire format type codes.
|
|
3
|
+
* Ranges:
|
|
4
|
+
* 0xA0–0xBF Core mesh protocol (identity, ACL, chat, streams, naming, files, keepalive)
|
|
5
|
+
* 0xC0–0xEC Extended subsystems (swarm, audit, resources, quotas, payments, GPU, orchestration, marketplace, apps, consensus)
|
|
6
|
+
* 0xED–0xEF PBFT consensus (pre-prepare, prepare, commit)
|
|
7
|
+
* 0xF0–0xF3 SWIM membership (ping, ping-req, ack, membership)
|
|
8
|
+
* 0xF4–0xF5 PBFT consensus continued (view-change, new-view)
|
|
9
|
+
* 0xF6–0xFF Reserved (internal / future)
|
|
10
|
+
*
|
|
11
|
+
* @enum {number}
|
|
12
|
+
*/
|
|
13
|
+
export const MESH_TYPE = Object.freeze({
|
|
14
|
+
/** Pod-to-pod unicast message */
|
|
15
|
+
UNICAST: 0xa0,
|
|
16
|
+
/** Broadcast to all connected pods */
|
|
17
|
+
BROADCAST: 0xa1,
|
|
18
|
+
/** Capability grant envelope */
|
|
19
|
+
CAP_GRANT: 0xa2,
|
|
20
|
+
/** Capability revocation */
|
|
21
|
+
CAP_REVOKE: 0xa3,
|
|
22
|
+
/** Trust attestation */
|
|
23
|
+
TRUST_ATTEST: 0xa4,
|
|
24
|
+
/** Identity announcement (join) */
|
|
25
|
+
IDENTITY_ANNOUNCE: 0xa5,
|
|
26
|
+
/** Identity departure (leave) */
|
|
27
|
+
IDENTITY_DEPART: 0xa6,
|
|
28
|
+
/** CRDT sync frame */
|
|
29
|
+
CRDT_SYNC: 0xa7,
|
|
30
|
+
/** Consensus proposal */
|
|
31
|
+
CONSENSUS_PROPOSE: 0xa8,
|
|
32
|
+
/** Consensus vote */
|
|
33
|
+
CONSENSUS_VOTE: 0xa9,
|
|
34
|
+
/** Resource claim */
|
|
35
|
+
RESOURCE_CLAIM: 0xaa,
|
|
36
|
+
/** Resource release */
|
|
37
|
+
RESOURCE_RELEASE: 0xab,
|
|
38
|
+
|
|
39
|
+
// ── ACL operations ──────────────────────────────────────────────────
|
|
40
|
+
/** Grant access to identity */
|
|
41
|
+
ACL_GRANT: 0xac,
|
|
42
|
+
/** Revoke access */
|
|
43
|
+
ACL_REVOKE: 0xad,
|
|
44
|
+
/** Invitation token */
|
|
45
|
+
ACL_INVITE: 0xae,
|
|
46
|
+
|
|
47
|
+
// ── Chat protocol ───────────────────────────────────────────────────
|
|
48
|
+
/** Join room */
|
|
49
|
+
CHAT_JOIN: 0xb0,
|
|
50
|
+
/** Leave room */
|
|
51
|
+
CHAT_LEAVE: 0xb1,
|
|
52
|
+
/** Typing/presence update */
|
|
53
|
+
CHAT_PRESENCE: 0xb2,
|
|
54
|
+
/** Moderation action */
|
|
55
|
+
CHAT_MODERATE: 0xb3,
|
|
56
|
+
|
|
57
|
+
// ── Stream operations ──────────────────────────────────────────────
|
|
58
|
+
/** Named stream open request */
|
|
59
|
+
STREAM_OPEN: 0xaf,
|
|
60
|
+
|
|
61
|
+
// ── Name resolution ─────────────────────────────────────────────────
|
|
62
|
+
/** Register/renew a name */
|
|
63
|
+
NAME_REGISTER: 0xb5,
|
|
64
|
+
/** Resolve a name query */
|
|
65
|
+
NAME_RESOLVE: 0xb6,
|
|
66
|
+
/** Transfer name ownership */
|
|
67
|
+
NAME_TRANSFER: 0xb7,
|
|
68
|
+
|
|
69
|
+
// ── File transfer ─────────────────────────────────────────────────
|
|
70
|
+
/** Transfer offer */
|
|
71
|
+
FILE_OFFER: 0xb8,
|
|
72
|
+
/** Accept transfer */
|
|
73
|
+
FILE_ACCEPT: 0xb9,
|
|
74
|
+
/** Reject transfer */
|
|
75
|
+
FILE_REJECT: 0xba,
|
|
76
|
+
/** Progress update */
|
|
77
|
+
FILE_PROGRESS: 0xbb,
|
|
78
|
+
/** Transfer complete */
|
|
79
|
+
FILE_COMPLETE: 0xbc,
|
|
80
|
+
/** Cancel transfer */
|
|
81
|
+
FILE_CANCEL: 0xbd,
|
|
82
|
+
|
|
83
|
+
/** Ping / keepalive */
|
|
84
|
+
PING: 0xbe,
|
|
85
|
+
/** Pong / keepalive response */
|
|
86
|
+
PONG: 0xbf,
|
|
87
|
+
|
|
88
|
+
// ── Swarm coordination (0xC0–0xC3) ──────────────────────────────────
|
|
89
|
+
/** Join a swarm */
|
|
90
|
+
SWARM_JOIN: 0xc0,
|
|
91
|
+
/** Leave a swarm */
|
|
92
|
+
SWARM_LEAVE: 0xc1,
|
|
93
|
+
/** Swarm heartbeat / liveness */
|
|
94
|
+
SWARM_HEARTBEAT: 0xc2,
|
|
95
|
+
/** Assign a task within a swarm */
|
|
96
|
+
SWARM_TASK_ASSIGN: 0xc3,
|
|
97
|
+
|
|
98
|
+
// ── Audit trail (0xC4–0xC6) ─────────────────────────────────────────
|
|
99
|
+
/** Single audit log entry */
|
|
100
|
+
AUDIT_ENTRY: 0xc4,
|
|
101
|
+
/** Query an audit chain */
|
|
102
|
+
AUDIT_CHAIN_QUERY: 0xc5,
|
|
103
|
+
/** Response with audit chain data */
|
|
104
|
+
AUDIT_CHAIN_RESPONSE: 0xc6,
|
|
105
|
+
|
|
106
|
+
// ── Resource management (0xC7–0xCC) ─────────────────────────────────
|
|
107
|
+
/** Advertise local resources to the mesh */
|
|
108
|
+
RESOURCE_ADVERTISE: 0xc7,
|
|
109
|
+
/** Discover peers matching resource constraints */
|
|
110
|
+
RESOURCE_DISCOVER: 0xc8,
|
|
111
|
+
/** Response to a discovery query */
|
|
112
|
+
RESOURCE_DISCOVER_RESPONSE: 0xc9,
|
|
113
|
+
/** Submit a compute request */
|
|
114
|
+
COMPUTE_REQUEST: 0xca,
|
|
115
|
+
/** Return a compute result */
|
|
116
|
+
COMPUTE_RESULT: 0xcb,
|
|
117
|
+
/** Incremental progress update for a running job */
|
|
118
|
+
COMPUTE_PROGRESS: 0xcc,
|
|
119
|
+
|
|
120
|
+
// ── Quota metering (0xCD–0xCF) ──────────────────────────────────────
|
|
121
|
+
/** Quota rule created or updated */
|
|
122
|
+
QUOTA_UPDATE: 0xcd,
|
|
123
|
+
/** Quota violation detected */
|
|
124
|
+
QUOTA_VIOLATION: 0xce,
|
|
125
|
+
/** Periodic usage report */
|
|
126
|
+
USAGE_REPORT: 0xcf,
|
|
127
|
+
|
|
128
|
+
// ── Payment channels (0xD0–0xD3) ────────────────────────────────────
|
|
129
|
+
/** Open a payment channel */
|
|
130
|
+
PAYMENT_OPEN: 0xd0,
|
|
131
|
+
/** Update channel balance */
|
|
132
|
+
PAYMENT_UPDATE: 0xd1,
|
|
133
|
+
/** Close a payment channel */
|
|
134
|
+
PAYMENT_CLOSE: 0xd2,
|
|
135
|
+
/** Create an escrow */
|
|
136
|
+
ESCROW_CREATE: 0xd3,
|
|
137
|
+
|
|
138
|
+
// ── GPU orchestration (0xD4–0xD7) ───────────────────────────────────
|
|
139
|
+
/** GPU capability probe */
|
|
140
|
+
GPU_PROBE: 0xd4,
|
|
141
|
+
/** Assign a training shard to a peer */
|
|
142
|
+
GPU_SHARD_ASSIGN: 0xd5,
|
|
143
|
+
/** Push gradients from a shard peer */
|
|
144
|
+
GPU_GRADIENT_PUSH: 0xd6,
|
|
145
|
+
/** Training control message (start/cancel/status) */
|
|
146
|
+
GPU_TRAIN_CONTROL: 0xd7,
|
|
147
|
+
|
|
148
|
+
// ── Orchestration (0xD8–0xDE) ──────────────────────────────────────
|
|
149
|
+
/** List all pods in the mesh */
|
|
150
|
+
ORCH_LIST_PODS: 0xd8,
|
|
151
|
+
/** Get detailed pod status */
|
|
152
|
+
ORCH_POD_STATUS: 0xd9,
|
|
153
|
+
/** Execute a command on a remote pod */
|
|
154
|
+
ORCH_EXEC: 0xda,
|
|
155
|
+
/** Deploy a skill to a remote pod */
|
|
156
|
+
ORCH_DEPLOY: 0xdb,
|
|
157
|
+
/** Drain a pod (graceful disconnect) */
|
|
158
|
+
ORCH_DRAIN: 0xdc,
|
|
159
|
+
/** Expose a pod's service */
|
|
160
|
+
ORCH_EXPOSE: 0xdd,
|
|
161
|
+
/** Route a service to a target pod */
|
|
162
|
+
ORCH_ROUTE: 0xde,
|
|
163
|
+
|
|
164
|
+
// ── Marketplace (0xDF–0xE4) ────────────────────────────────────────
|
|
165
|
+
/** Publish a service listing */
|
|
166
|
+
LISTING_PUBLISH: 0xdf,
|
|
167
|
+
/** Query available listings */
|
|
168
|
+
LISTING_QUERY: 0xe0,
|
|
169
|
+
/** Response to a listing query */
|
|
170
|
+
LISTING_RESPONSE: 0xe1,
|
|
171
|
+
/** Purchase / subscribe to a service */
|
|
172
|
+
LISTING_PURCHASE: 0xe2,
|
|
173
|
+
/** Submit a review for a service */
|
|
174
|
+
REVIEW_SUBMIT: 0xe3,
|
|
175
|
+
/** Query reviews for a service */
|
|
176
|
+
REVIEW_QUERY: 0xe4,
|
|
177
|
+
|
|
178
|
+
// ── App distribution (0xE5–0xEA) ───────────────────────────────────
|
|
179
|
+
/** App manifest announcement */
|
|
180
|
+
APP_MANIFEST: 0xe5,
|
|
181
|
+
/** App install request */
|
|
182
|
+
APP_INSTALL: 0xe6,
|
|
183
|
+
/** App uninstall request */
|
|
184
|
+
APP_UNINSTALL: 0xe7,
|
|
185
|
+
/** App state synchronization */
|
|
186
|
+
APP_STATE_SYNC: 0xe8,
|
|
187
|
+
/** App RPC call */
|
|
188
|
+
APP_RPC: 0xe9,
|
|
189
|
+
/** App event broadcast */
|
|
190
|
+
APP_EVENT: 0xea,
|
|
191
|
+
|
|
192
|
+
// ── Consensus extended (0xEB–0xEC) ─────────────────────────────────
|
|
193
|
+
/** Close a proposal (author or timeout) */
|
|
194
|
+
CONSENSUS_CLOSE: 0xeb,
|
|
195
|
+
/** Broadcast final results of a closed proposal */
|
|
196
|
+
CONSENSUS_RESULT: 0xec,
|
|
197
|
+
|
|
198
|
+
// ── PBFT consensus (0xED–0xEF, 0xF4–0xF5) ────────────────────────
|
|
199
|
+
/** PBFT pre-prepare (leader block proposal) */
|
|
200
|
+
PBFT_PRE_PREPARE: 0xed,
|
|
201
|
+
/** PBFT prepare (validator acknowledgement) */
|
|
202
|
+
PBFT_PREPARE: 0xee,
|
|
203
|
+
/** PBFT commit (validator commit with signature) */
|
|
204
|
+
PBFT_COMMIT: 0xef,
|
|
205
|
+
/** PBFT view change (leader rotation request) */
|
|
206
|
+
PBFT_VIEW_CHANGE: 0xf4,
|
|
207
|
+
/** PBFT new view (view change confirmation) */
|
|
208
|
+
PBFT_NEW_VIEW: 0xf5,
|
|
209
|
+
|
|
210
|
+
// ── SWIM membership (0xF0–0xF3) ──────────────────────────────────
|
|
211
|
+
/** SWIM ping (failure detection) */
|
|
212
|
+
SWIM_PING: 0xf0,
|
|
213
|
+
/** SWIM ping-req (indirect probe) */
|
|
214
|
+
SWIM_PING_REQ: 0xf1,
|
|
215
|
+
/** SWIM ack (ping response) */
|
|
216
|
+
SWIM_ACK: 0xf2,
|
|
217
|
+
/** SWIM membership update (join/leave/suspect/faulty) */
|
|
218
|
+
SWIM_MEMBERSHIP: 0xf3,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Error codes for mesh protocol errors.
|
|
223
|
+
*
|
|
224
|
+
* @enum {number}
|
|
225
|
+
*/
|
|
226
|
+
export const MESH_ERROR = Object.freeze({
|
|
227
|
+
/** Unknown or unclassified error */
|
|
228
|
+
UNKNOWN: 0,
|
|
229
|
+
/** Invalid wire format / decode failure */
|
|
230
|
+
INVALID_FORMAT: 1,
|
|
231
|
+
/** Capability check failed */
|
|
232
|
+
CAPABILITY_DENIED: 2,
|
|
233
|
+
/** Identity verification failed */
|
|
234
|
+
IDENTITY_INVALID: 3,
|
|
235
|
+
/** Trust threshold not met */
|
|
236
|
+
TRUST_INSUFFICIENT: 4,
|
|
237
|
+
/** Message expired (TTL exceeded) */
|
|
238
|
+
MESSAGE_EXPIRED: 5,
|
|
239
|
+
/** Resource unavailable */
|
|
240
|
+
RESOURCE_UNAVAILABLE: 6,
|
|
241
|
+
/** Consensus quorum not reached */
|
|
242
|
+
QUORUM_NOT_REACHED: 7,
|
|
243
|
+
/** ACL denied */
|
|
244
|
+
ACL_DENIED: 8,
|
|
245
|
+
/** Name already taken */
|
|
246
|
+
NAME_TAKEN: 9,
|
|
247
|
+
/** Name not found */
|
|
248
|
+
NAME_NOT_FOUND: 10,
|
|
249
|
+
/** Name expired */
|
|
250
|
+
NAME_EXPIRED: 11,
|
|
251
|
+
/** Room is full */
|
|
252
|
+
ROOM_FULL: 12,
|
|
253
|
+
/** Identity is banned */
|
|
254
|
+
BANNED: 13,
|
|
255
|
+
/** Stream closed */
|
|
256
|
+
STREAM_CLOSED: 14,
|
|
257
|
+
/** Stream limit exceeded */
|
|
258
|
+
STREAM_LIMIT: 15,
|
|
259
|
+
/** Transfer rejected */
|
|
260
|
+
TRANSFER_REJECTED: 16,
|
|
261
|
+
/** Transfer failed */
|
|
262
|
+
TRANSFER_FAILED: 17,
|
|
263
|
+
/** Chunk invalid */
|
|
264
|
+
CHUNK_INVALID: 18,
|
|
265
|
+
});
|