@johnhenry/browsermesh-primitives 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 +23 -2
- package/package.json +7 -3
- package/src/identity.mjs +78 -5
- package/src/index.mjs +1 -1
package/README.md
CHANGED
|
@@ -1,10 +1,31 @@
|
|
|
1
1
|
# browsermesh-primitives
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@johnhenry/browsermesh-primitives)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
3
6
|
Shared primitives for browser mesh networking -- wire format, identity (Ed25519), CRDTs, capabilities, trust model, and ACL engine. Zero dependencies, pure ES modules, runs in browsers and Node.js.
|
|
4
7
|
|
|
8
|
+
## Why this exists
|
|
9
|
+
|
|
10
|
+
Every other package in this monorepo needs the same handful of building blocks -- an identity to sign with, a wire format to put bytes on the network, CRDTs that merge deterministically across peers, and a shared vocabulary for "does this peer have permission to do X." Reimplementing any of those per package would mean N slightly-different Ed25519 key derivations, N incompatible wire encodings, and no guarantee that two packages' capability tokens even mean the same thing. `browsermesh-primitives` exists to be that one shared, dependency-free floor: every downstream package imports its identity, CRDT, capability, or wire-format types directly rather than rolling its own, so two peers running different BrowserMesh packages built against the same primitives version can always talk to, sign for, and verify each other. It has zero dependencies of its own on purpose -- nothing downstream should have to worry about *this* package dragging in a second copy of anything.
|
|
11
|
+
|
|
12
|
+
## Used by
|
|
13
|
+
|
|
14
|
+
Every other package in this monorepo depends on `browsermesh-primitives` directly, or transitively through one that does:
|
|
15
|
+
|
|
16
|
+
- `@johnhenry/browsermesh-core` -- identity, keyring, ACL, and trust-graph classes wrap this package's `PodIdentity`, `MESH_TYPE`, and capability/CRDT types.
|
|
17
|
+
- `@johnhenry/browsermesh-discovery` -- DHT, naming, and swarm messages are tagged with this package's `MESH_TYPE` constants.
|
|
18
|
+
- `@johnhenry/browsermesh-pod` -- `Pod` generates its Ed25519 identity via `PodIdentity.generate()`.
|
|
19
|
+
- `@johnhenry/browsermesh-sync` -- `SyncDocument` is built directly on this package's `VectorClock`, `LWWRegister`, `GCounter`, `PNCounter`, `ORSet`, `RGA`, and `LWWMap` CRDT classes.
|
|
20
|
+
- `@johnhenry/browsermesh-transport` -- transport message/error types (`MESH_TYPE`, `MESH_ERROR`) come from here.
|
|
21
|
+
- `@johnhenry/browsermesh-apps` -- imports this package directly (`MESH_TYPE`, `LWWMap`, base64url helpers) alongside its required `browsermesh-sync` peer.
|
|
22
|
+
- `@johnhenry/browsermesh-embed` -- transitively, through its hard dependency on `browsermesh-pod`.
|
|
23
|
+
|
|
24
|
+
`browsermesh-kernel`, `browsermesh-netway`, and `browsermesh-priority-mux` are the exceptions: each is deliberately zero-dependency and imports nothing from this package.
|
|
25
|
+
|
|
5
26
|
## Provenance
|
|
6
27
|
|
|
7
|
-
Previously published as `browsermesh-primitives@0.1.1
|
|
28
|
+
Previously maintained as an independent, standalone repository and published to npm, unscoped, as `browsermesh-primitives@0.1.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-primitives`; the version restarts at `0.0.0` per family convention.
|
|
8
29
|
|
|
9
30
|
## Install
|
|
10
31
|
|
|
@@ -31,7 +52,7 @@ import {
|
|
|
31
52
|
ACLEngine,
|
|
32
53
|
encodeMeshMessage,
|
|
33
54
|
decodeMeshMessage,
|
|
34
|
-
} from 'browsermesh-primitives'
|
|
55
|
+
} from '@johnhenry/browsermesh-primitives'
|
|
35
56
|
|
|
36
57
|
// Generate an Ed25519 identity
|
|
37
58
|
const identity = await PodIdentity.generate()
|
package/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnhenry/browsermesh-primitives",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Shared primitives for browser mesh networking — wire format, identity (Ed25519), CRDTs, capabilities, trust model, and ACL engine",
|
|
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,6 +35,6 @@
|
|
|
31
35
|
"test": "node --test test/*.test.mjs"
|
|
32
36
|
},
|
|
33
37
|
"engines": {
|
|
34
|
-
"node": ">=
|
|
38
|
+
"node": ">=26.0.0"
|
|
35
39
|
}
|
|
36
40
|
}
|
package/src/identity.mjs
CHANGED
|
@@ -25,6 +25,63 @@ export function decodeBase64url(str) {
|
|
|
25
25
|
return bytes;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/** @type {boolean|null} Cached Ed25519 probe result; null until it resolves. */
|
|
29
|
+
let _ed25519Supported = null;
|
|
30
|
+
|
|
31
|
+
/** @type {Promise<boolean>|null} In-flight probe, so it runs at most once. */
|
|
32
|
+
let _ed25519Probe = null;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Probe for WebCrypto Ed25519 support by asking for a key. Resolves true or
|
|
36
|
+
* false, never throws, and caches the answer.
|
|
37
|
+
*
|
|
38
|
+
* This library has no software fallback for Ed25519, so a false here means the
|
|
39
|
+
* whole identity layer is unavailable in this environment. WebCrypto Ed25519
|
|
40
|
+
* shipped in **Safari 17** and **Chromium 137** — the latter is recent enough
|
|
41
|
+
* that Android devices with an un-updated System WebView will not have it.
|
|
42
|
+
* Call this before PodIdentity.generate() if you need to degrade gracefully
|
|
43
|
+
* rather than catch a NotSupportedError from four frames down.
|
|
44
|
+
*
|
|
45
|
+
* @returns {Promise<boolean>}
|
|
46
|
+
*/
|
|
47
|
+
export function probeEd25519Support() {
|
|
48
|
+
if (_ed25519Probe) return _ed25519Probe;
|
|
49
|
+
_ed25519Probe = (async () => {
|
|
50
|
+
if (typeof crypto === 'undefined' || !crypto.subtle ||
|
|
51
|
+
typeof crypto.subtle.generateKey !== 'function') {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
await crypto.subtle.generateKey({ name: 'Ed25519' }, false, ['sign', 'verify']);
|
|
56
|
+
return true;
|
|
57
|
+
} catch {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
})().then((ok) => {
|
|
61
|
+
_ed25519Supported = ok;
|
|
62
|
+
return ok;
|
|
63
|
+
});
|
|
64
|
+
return _ed25519Probe;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Cached answer from probeEd25519Support(), or null when the probe has not
|
|
69
|
+
* resolved yet. Treat null as "unknown"; await probeEd25519Support() for a
|
|
70
|
+
* definite answer.
|
|
71
|
+
*
|
|
72
|
+
* @returns {boolean|null}
|
|
73
|
+
*/
|
|
74
|
+
export function supportsEd25519() {
|
|
75
|
+
if (_ed25519Supported === null) probeEd25519Support();
|
|
76
|
+
return _ed25519Supported;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Reset the cached Ed25519 probe. Tests only. */
|
|
80
|
+
export function _resetEd25519Probe() {
|
|
81
|
+
_ed25519Supported = null;
|
|
82
|
+
_ed25519Probe = null;
|
|
83
|
+
}
|
|
84
|
+
|
|
28
85
|
/**
|
|
29
86
|
* Derive a pod ID from an Ed25519 public key.
|
|
30
87
|
* Pod ID = base64url(SHA-256(raw public key bytes)).
|
|
@@ -59,14 +116,30 @@ export class PodIdentity {
|
|
|
59
116
|
/**
|
|
60
117
|
* Generate a new PodIdentity with a fresh Ed25519 key pair.
|
|
61
118
|
*
|
|
119
|
+
* Requires WebCrypto Ed25519 (Safari 17+, Chromium 137+). There is no
|
|
120
|
+
* software fallback: where the algorithm is missing this throws with a
|
|
121
|
+
* message naming the requirement rather than a bare NotSupportedError.
|
|
122
|
+
* Use probeEd25519Support() to check first.
|
|
123
|
+
*
|
|
62
124
|
* @returns {Promise<PodIdentity>}
|
|
63
125
|
*/
|
|
64
126
|
static async generate() {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
127
|
+
let keyPair;
|
|
128
|
+
try {
|
|
129
|
+
keyPair = await crypto.subtle.generateKey(
|
|
130
|
+
{ name: 'Ed25519' },
|
|
131
|
+
true, // extractable
|
|
132
|
+
['sign', 'verify']
|
|
133
|
+
);
|
|
134
|
+
} catch (err) {
|
|
135
|
+
throw new Error(
|
|
136
|
+
'WebCrypto Ed25519 is unavailable in this environment, and this library ' +
|
|
137
|
+
'has no software fallback. Ed25519 requires Safari 17+ or Chromium 137+ ' +
|
|
138
|
+
'(including the Android System WebView). Original error: ' +
|
|
139
|
+
(err && err.message ? err.message : String(err)),
|
|
140
|
+
{ cause: err }
|
|
141
|
+
);
|
|
142
|
+
}
|
|
70
143
|
const podId = await derivePodId(keyPair.publicKey);
|
|
71
144
|
return new PodIdentity({ keyPair, podId });
|
|
72
145
|
}
|
package/src/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ export { MESH_TYPE, MESH_ERROR } from "./constants.mjs";
|
|
|
5
5
|
export { MeshError, MeshProtocolError, MeshCapabilityError } from "./errors.mjs";
|
|
6
6
|
|
|
7
7
|
// Identity
|
|
8
|
-
export { PodIdentity, derivePodId, encodeBase64url, decodeBase64url } from "./identity.mjs";
|
|
8
|
+
export { PodIdentity, derivePodId, encodeBase64url, decodeBase64url, supportsEd25519, probeEd25519Support, _resetEd25519Probe } from "./identity.mjs";
|
|
9
9
|
|
|
10
10
|
// Wire format
|
|
11
11
|
export { messageTypeRegistry, encodeMeshMessage, decodeMeshMessage } from "./wire.mjs";
|