@johnhenry/browsermesh-discovery 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 +35 -0
- package/package.json +31 -0
- package/src/dht.mjs +875 -0
- package/src/discovery.mjs +1584 -0
- package/src/index.mjs +7 -0
- package/src/naming.mjs +393 -0
- package/src/silent-catch.mjs +55 -0
- package/src/stealth.mjs +450 -0
- package/src/sw-routing.mjs +126 -0
- package/src/swarm.mjs +1440 -0
package/src/index.mjs
ADDED
package/src/naming.mjs
ADDED
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
/**
|
|
2
|
+
// STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
|
|
3
|
+
* clawser-mesh-naming.js -- Decentralized name resolution for BrowserMesh.
|
|
4
|
+
*
|
|
5
|
+
* Human-friendly names (@alice, mesh://alice/path) mapped to identity
|
|
6
|
+
* fingerprints. First-come-first-served registration, TTL-based expiry,
|
|
7
|
+
* ownership transfer, and reverse lookup.
|
|
8
|
+
*
|
|
9
|
+
* No browser-only imports at module level.
|
|
10
|
+
*
|
|
11
|
+
* Run tests:
|
|
12
|
+
* node --import ./web/test/_setup-globals.mjs --test web/test/clawser-mesh-naming.test.mjs
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { MESH_TYPE } from '@johnhenry/browsermesh-primitives';
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Constants
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
/** Default TTL in milliseconds (1 hour) */
|
|
22
|
+
export const NAME_TTL_DEFAULT = 3600000;
|
|
23
|
+
|
|
24
|
+
/** Maximum length for a name */
|
|
25
|
+
export const MAX_NAME_LENGTH = 64;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Valid name pattern: lowercase alphanumeric, dots, hyphens, underscores.
|
|
29
|
+
* Must start and end with alphanumeric. Minimum 2 characters.
|
|
30
|
+
*/
|
|
31
|
+
export const NAME_PATTERN = /^[a-z0-9][a-z0-9._-]{0,62}[a-z0-9]$/;
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// parseMeshUri
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Parse various mesh name formats into a structured object.
|
|
39
|
+
*
|
|
40
|
+
* Supported formats:
|
|
41
|
+
* - `@alice` → short name
|
|
42
|
+
* - `@alice@relay.example.com` → qualified name
|
|
43
|
+
* - `did:key:z6Mk...` → DID key reference
|
|
44
|
+
* - `mesh://alice/service/path` → mesh URI
|
|
45
|
+
*
|
|
46
|
+
* @param {string} uri
|
|
47
|
+
* @returns {{ type: string, name?: string, relay?: string|null, fingerprint?: string, path?: string|null }|null}
|
|
48
|
+
*/
|
|
49
|
+
export function parseMeshUri(uri) {
|
|
50
|
+
if (!uri || typeof uri !== 'string') return null;
|
|
51
|
+
|
|
52
|
+
// mesh://name/path
|
|
53
|
+
const meshMatch = uri.match(/^mesh:\/\/([^/]+)(\/.*)?$/);
|
|
54
|
+
if (meshMatch) {
|
|
55
|
+
return {
|
|
56
|
+
type: 'mesh',
|
|
57
|
+
name: meshMatch[1],
|
|
58
|
+
relay: null,
|
|
59
|
+
path: meshMatch[2] || null,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// did:key:fingerprint
|
|
64
|
+
const didMatch = uri.match(/^did:key:(.+)$/);
|
|
65
|
+
if (didMatch) {
|
|
66
|
+
return {
|
|
67
|
+
type: 'did',
|
|
68
|
+
fingerprint: didMatch[1],
|
|
69
|
+
path: null,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// @name@relay or @name
|
|
74
|
+
const atMatch = uri.match(/^@([^@]+)(?:@(.+))?$/);
|
|
75
|
+
if (atMatch) {
|
|
76
|
+
if (atMatch[2]) {
|
|
77
|
+
return {
|
|
78
|
+
type: 'qualified',
|
|
79
|
+
name: atMatch[1],
|
|
80
|
+
relay: atMatch[2],
|
|
81
|
+
path: null,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
type: 'short',
|
|
86
|
+
name: atMatch[1],
|
|
87
|
+
relay: null,
|
|
88
|
+
path: null,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
// NameRecord
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* A name registration record.
|
|
101
|
+
*/
|
|
102
|
+
export class NameRecord {
|
|
103
|
+
/**
|
|
104
|
+
* @param {object} opts
|
|
105
|
+
* @param {string} opts.name
|
|
106
|
+
* @param {string} opts.fingerprint - Owner's identity fingerprint
|
|
107
|
+
* @param {number} [opts.timestamp]
|
|
108
|
+
* @param {number} [opts.ttl]
|
|
109
|
+
* @param {string} [opts.relay]
|
|
110
|
+
* @param {object} [opts.metadata]
|
|
111
|
+
*/
|
|
112
|
+
constructor({ name, fingerprint, timestamp, ttl, relay, metadata }) {
|
|
113
|
+
this.name = name;
|
|
114
|
+
this.fingerprint = fingerprint;
|
|
115
|
+
this.timestamp = timestamp ?? Date.now();
|
|
116
|
+
this.ttl = ttl ?? NAME_TTL_DEFAULT;
|
|
117
|
+
this.relay = relay ?? null;
|
|
118
|
+
this.metadata = metadata ? { ...metadata } : null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {number} [now]
|
|
123
|
+
* @returns {boolean}
|
|
124
|
+
*/
|
|
125
|
+
isExpired(now = Date.now()) {
|
|
126
|
+
return now > this.timestamp + this.ttl;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
toJSON() {
|
|
130
|
+
return {
|
|
131
|
+
name: this.name,
|
|
132
|
+
fingerprint: this.fingerprint,
|
|
133
|
+
timestamp: this.timestamp,
|
|
134
|
+
ttl: this.ttl,
|
|
135
|
+
relay: this.relay,
|
|
136
|
+
metadata: this.metadata ? { ...this.metadata } : null,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
static fromJSON(data) {
|
|
141
|
+
return new NameRecord({
|
|
142
|
+
name: data.name,
|
|
143
|
+
fingerprint: data.fingerprint,
|
|
144
|
+
timestamp: data.timestamp,
|
|
145
|
+
ttl: data.ttl,
|
|
146
|
+
relay: data.relay,
|
|
147
|
+
metadata: data.metadata,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ---------------------------------------------------------------------------
|
|
153
|
+
// MeshNameResolver
|
|
154
|
+
// ---------------------------------------------------------------------------
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Decentralized name registry.
|
|
158
|
+
*
|
|
159
|
+
* First-come-first-served: only the original owner (same fingerprint)
|
|
160
|
+
* can renew or update a registration. Names expire after TTL.
|
|
161
|
+
*/
|
|
162
|
+
export class MeshNameResolver {
|
|
163
|
+
/**
|
|
164
|
+
* @param {object} [opts]
|
|
165
|
+
* @param {function} [opts.onLog]
|
|
166
|
+
*/
|
|
167
|
+
constructor(opts = {}) {
|
|
168
|
+
this._onLog = opts.onLog || (() => {});
|
|
169
|
+
/** @type {Map<string, NameRecord>} name → record */
|
|
170
|
+
this._records = new Map();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// ── Registration ───────────────────────────────────────────────────
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Register or renew a name.
|
|
177
|
+
*
|
|
178
|
+
* @param {string} name
|
|
179
|
+
* @param {string} fingerprint
|
|
180
|
+
* @param {object} [opts]
|
|
181
|
+
* @param {number} [opts.ttl]
|
|
182
|
+
* @param {string} [opts.relay]
|
|
183
|
+
* @param {object} [opts.metadata]
|
|
184
|
+
* @returns {NameRecord}
|
|
185
|
+
*/
|
|
186
|
+
register(name, fingerprint, opts = {}) {
|
|
187
|
+
this._validateName(name);
|
|
188
|
+
|
|
189
|
+
const existing = this._records.get(name);
|
|
190
|
+
if (existing && !existing.isExpired() && existing.fingerprint !== fingerprint) {
|
|
191
|
+
throw new Error(`Name "${name}" is already taken`);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const record = new NameRecord({
|
|
195
|
+
name,
|
|
196
|
+
fingerprint,
|
|
197
|
+
ttl: opts.ttl,
|
|
198
|
+
relay: opts.relay,
|
|
199
|
+
metadata: opts.metadata,
|
|
200
|
+
});
|
|
201
|
+
this._records.set(name, record);
|
|
202
|
+
return record;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Unregister a name. Only the current owner can unregister.
|
|
207
|
+
*
|
|
208
|
+
* @param {string} name
|
|
209
|
+
* @param {string} fingerprint
|
|
210
|
+
* @returns {boolean}
|
|
211
|
+
*/
|
|
212
|
+
unregister(name, fingerprint) {
|
|
213
|
+
const existing = this._records.get(name);
|
|
214
|
+
if (!existing) return false;
|
|
215
|
+
if (existing.fingerprint !== fingerprint) return false;
|
|
216
|
+
this._records.delete(name);
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// ── Resolution ─────────────────────────────────────────────────────
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Resolve a URI to a fingerprint and record.
|
|
224
|
+
*
|
|
225
|
+
* @param {string} uri
|
|
226
|
+
* @returns {{ fingerprint: string, record: NameRecord }|null}
|
|
227
|
+
*/
|
|
228
|
+
resolve(uri) {
|
|
229
|
+
const parsed = parseMeshUri(uri);
|
|
230
|
+
if (!parsed) return null;
|
|
231
|
+
|
|
232
|
+
if (parsed.type === 'did') {
|
|
233
|
+
// DID resolves directly to its fingerprint — look up any names
|
|
234
|
+
// that match, or return a synthetic result
|
|
235
|
+
const records = this.reverseResolve(parsed.fingerprint);
|
|
236
|
+
if (records.length > 0) {
|
|
237
|
+
return { fingerprint: parsed.fingerprint, record: records[0] };
|
|
238
|
+
}
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// For short, qualified, and mesh URIs, look up by name
|
|
243
|
+
const name = parsed.name;
|
|
244
|
+
if (!name) return null;
|
|
245
|
+
|
|
246
|
+
const record = this._records.get(name);
|
|
247
|
+
if (!record) return null;
|
|
248
|
+
if (record.isExpired()) return null;
|
|
249
|
+
|
|
250
|
+
// For qualified URIs, optionally verify relay matches
|
|
251
|
+
if (parsed.type === 'qualified' && record.relay && parsed.relay !== record.relay) {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return { fingerprint: record.fingerprint, record };
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Reverse-resolve: find all names registered to a fingerprint.
|
|
260
|
+
*
|
|
261
|
+
* @param {string} fingerprint
|
|
262
|
+
* @returns {NameRecord[]}
|
|
263
|
+
*/
|
|
264
|
+
reverseResolve(fingerprint) {
|
|
265
|
+
const results = [];
|
|
266
|
+
for (const record of this._records.values()) {
|
|
267
|
+
if (record.fingerprint === fingerprint && !record.isExpired()) {
|
|
268
|
+
results.push(record);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return results;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// ── Transfer ───────────────────────────────────────────────────────
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Transfer name ownership from one fingerprint to another.
|
|
278
|
+
*
|
|
279
|
+
* @param {string} name
|
|
280
|
+
* @param {string} fromFingerprint
|
|
281
|
+
* @param {string} toFingerprint
|
|
282
|
+
* @returns {NameRecord}
|
|
283
|
+
*/
|
|
284
|
+
transfer(name, fromFingerprint, toFingerprint) {
|
|
285
|
+
const existing = this._records.get(name);
|
|
286
|
+
if (!existing) {
|
|
287
|
+
throw new Error(`Name "${name}" not found`);
|
|
288
|
+
}
|
|
289
|
+
if (existing.fingerprint !== fromFingerprint) {
|
|
290
|
+
throw new Error(`Not the owner of "${name}"`);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const record = new NameRecord({
|
|
294
|
+
name,
|
|
295
|
+
fingerprint: toFingerprint,
|
|
296
|
+
ttl: existing.ttl,
|
|
297
|
+
relay: existing.relay,
|
|
298
|
+
metadata: existing.metadata,
|
|
299
|
+
});
|
|
300
|
+
this._records.set(name, record);
|
|
301
|
+
return record;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// ── Maintenance ────────────────────────────────────────────────────
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Remove expired records.
|
|
308
|
+
* @param {number} [now]
|
|
309
|
+
* @returns {number} Number of records pruned
|
|
310
|
+
*/
|
|
311
|
+
prune(now = Date.now()) {
|
|
312
|
+
let count = 0;
|
|
313
|
+
for (const [name, record] of this._records) {
|
|
314
|
+
if (record.isExpired(now)) {
|
|
315
|
+
this._records.delete(name);
|
|
316
|
+
count++;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return count;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// ── Query ──────────────────────────────────────────────────────────
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Search for names matching a query string (substring match on name or metadata).
|
|
326
|
+
*
|
|
327
|
+
* @param {string} query
|
|
328
|
+
* @returns {NameRecord[]}
|
|
329
|
+
*/
|
|
330
|
+
search(query) {
|
|
331
|
+
const q = query.toLowerCase();
|
|
332
|
+
const results = [];
|
|
333
|
+
for (const record of this._records.values()) {
|
|
334
|
+
if (record.isExpired()) continue;
|
|
335
|
+
if (record.name.includes(q)) {
|
|
336
|
+
results.push(record);
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
if (record.metadata) {
|
|
340
|
+
const metaStr = JSON.stringify(record.metadata).toLowerCase();
|
|
341
|
+
if (metaStr.includes(q)) {
|
|
342
|
+
results.push(record);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return results;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* List all non-expired records.
|
|
351
|
+
* @returns {NameRecord[]}
|
|
352
|
+
*/
|
|
353
|
+
list() {
|
|
354
|
+
const results = [];
|
|
355
|
+
for (const record of this._records.values()) {
|
|
356
|
+
if (!record.isExpired()) results.push(record);
|
|
357
|
+
}
|
|
358
|
+
return results;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// ── Serialization ──────────────────────────────────────────────────
|
|
362
|
+
|
|
363
|
+
toJSON() {
|
|
364
|
+
return {
|
|
365
|
+
records: [...this._records.values()].map(r => r.toJSON()),
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
static fromJSON(data) {
|
|
370
|
+
const resolver = new MeshNameResolver();
|
|
371
|
+
if (data.records) {
|
|
372
|
+
for (const rd of data.records) {
|
|
373
|
+
const record = NameRecord.fromJSON(rd);
|
|
374
|
+
resolver._records.set(record.name, record);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return resolver;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// ── Internal ───────────────────────────────────────────────────────
|
|
381
|
+
|
|
382
|
+
_validateName(name) {
|
|
383
|
+
if (typeof name !== 'string') {
|
|
384
|
+
throw new Error('Name must be a string');
|
|
385
|
+
}
|
|
386
|
+
if (name.length > MAX_NAME_LENGTH) {
|
|
387
|
+
throw new Error(`Name exceeds maximum length of ${MAX_NAME_LENGTH}`);
|
|
388
|
+
}
|
|
389
|
+
if (!NAME_PATTERN.test(name)) {
|
|
390
|
+
throw new Error(`Invalid name format: "${name}"`);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* silent-catch.mjs — debug-gated structured logger for the
|
|
3
|
+
* `try { … } catch { /* ignore *\/ }` pattern.
|
|
4
|
+
*
|
|
5
|
+
* Vendored from clawser-silent-catch.mjs (fully standalone, no other
|
|
6
|
+
* clawser-app imports) so this package doesn't need to reach into web/.
|
|
7
|
+
*
|
|
8
|
+
* Replaces silent catches with an opt-in inspectable record. Default
|
|
9
|
+
* behaviour is unchanged (silent), so this is a drop-in replacement.
|
|
10
|
+
* Users surface the events by enabling debug mode:
|
|
11
|
+
*
|
|
12
|
+
* localStorage.setItem('clawser_debug', 'true') // persistent
|
|
13
|
+
* // or, in DevTools: clawserDebug.enable()
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
let enabled = false;
|
|
17
|
+
try {
|
|
18
|
+
if (typeof localStorage !== 'undefined') {
|
|
19
|
+
enabled = localStorage.getItem('clawser_debug') === 'true';
|
|
20
|
+
}
|
|
21
|
+
} catch {
|
|
22
|
+
// localStorage may throw in privacy / sandbox contexts — treat as disabled
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Re-check the localStorage flag; called by clawserDebug.enable/disable. */
|
|
26
|
+
export function refreshSilentCatchState() {
|
|
27
|
+
try {
|
|
28
|
+
if (typeof localStorage !== 'undefined') {
|
|
29
|
+
enabled = localStorage.getItem('clawser_debug') === 'true';
|
|
30
|
+
}
|
|
31
|
+
} catch {
|
|
32
|
+
enabled = false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Log a silent-catch event. No-op unless debug mode is enabled.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} module — file/component (e.g. 'clawser-pod')
|
|
40
|
+
* @param {string} operation — what was attempted (e.g. 'relay-disconnect')
|
|
41
|
+
* @param {*} error — the caught value
|
|
42
|
+
* @param {object} [context] — extra structured fields
|
|
43
|
+
*/
|
|
44
|
+
export function silentCatch(module, operation, error, context) {
|
|
45
|
+
// Re-read the flag each call so toggles in DevTools take effect without
|
|
46
|
+
// restart. Cheap (one localStorage hit per silent catch in debug mode;
|
|
47
|
+
// catches happen rarely on hot paths).
|
|
48
|
+
if (!enabled) {
|
|
49
|
+
refreshSilentCatchState();
|
|
50
|
+
if (!enabled) return;
|
|
51
|
+
}
|
|
52
|
+
const entry = { module, operation, error: error?.message || String(error) };
|
|
53
|
+
if (context) Object.assign(entry, context);
|
|
54
|
+
console.warn('[clawser:silent-catch]', entry);
|
|
55
|
+
}
|