Skip to content

Agent Mesh

sdk/js: one SDK for Node and the browser

Antonio Ojeafcbeedc32264
diff --git a/sdk/js/package-lock.json b/sdk/js/package-lock.json
index dcc383c4..9376cbb5 100644
--- a/sdk/js/package-lock.json
+++ b/sdk/js/package-lock.json
@@ -11,6 +11,7 @@
"dependencies": {
"@biscuit-auth/biscuit-wasm": "^0.6.0",
"@bufbuild/protobuf": "^2.15.0",
+ "@chainsafe/libp2p-noise": "^17.0.0",
"@chainsafe/libp2p-yamux": "^8.0.1",
"@libp2p/circuit-relay-v2": "^4.2.13",
"@libp2p/crypto": "^5.1.23",
@@ -37,6 +38,7 @@
"@bufbuild/protoc-gen-es": "^2.15.0",
"@types/express": "^5.0.6",
"@types/node": "^26.6.1",
+ "esbuild": "^0.28.2",
"express": "^5.2.1",
"typescript": "^7.0.2"
},
@@ -138,12 +140,74 @@
"node": ">=14.17"
}
},
+ "node_modules/@chainsafe/as-chacha20poly1305": {
+ "version": "0.1.0",
+ "resolved": "https://registry.npmjs.org/@chainsafe/as-chacha20poly1305/-/as-chacha20poly1305-0.1.0.tgz",
+ "integrity": "sha512-BpNcL8/lji/GM3+vZ/bgRWqJ1q5kwvTFmGPk7pxm/QQZDbaMI98waOHjEymTjq2JmdD/INdNBFOVSyJofXg7ew=="
+ },
+ "node_modules/@chainsafe/as-sha256": {
+ "version": "1.2.5",
+ "resolved": "https://registry.npmjs.org/@chainsafe/as-sha256/-/as-sha256-1.2.5.tgz",
+ "integrity": "sha512-8LzmWxBC/2O5BgbP+aRom/FaDJMrM04VZMlVIivYTc6yNQKYrONmIv27m3q/1VBgxrfrNNqmibitb2hE2tQzIw=="
+ },
"node_modules/@chainsafe/is-ip": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/@chainsafe/is-ip/-/is-ip-2.1.0.tgz",
"integrity": "sha512-KIjt+6IfysQ4GCv66xihEitBjvhU/bixbbbFxdJ1sqCp4uJ0wuZiYBPhksZoy4lfaF0k9cwNzY5upEW/VWdw3w==",
"license": "MIT"
},
+ "node_modules/@chainsafe/libp2p-noise": {
+ "version": "17.0.0",
+ "resolved": "https://registry.npmjs.org/@chainsafe/libp2p-noise/-/libp2p-noise-17.0.0.tgz",
+ "integrity": "sha512-vwrmY2Y+L1xYhIDiEpl61KHxwrLCZoXzTpwhyk34u+3+6zCAZPL3GxH3i2cs+u5IYNoyLptORdH17RKFXy7upA==",
+ "dependencies": {
+ "@chainsafe/as-chacha20poly1305": "^0.1.0",
+ "@chainsafe/as-sha256": "^1.2.0",
+ "@libp2p/crypto": "^5.1.9",
+ "@libp2p/interface": "^3.0.0",
+ "@libp2p/peer-id": "^6.0.0",
+ "@libp2p/utils": "^7.0.0",
+ "@noble/ciphers": "^2.0.1",
+ "@noble/curves": "^2.0.1",
+ "@noble/hashes": "^2.0.1",
+ "protons-runtime": "^5.6.0",
+ "uint8arraylist": "^2.4.8",
+ "uint8arrays": "^5.1.0",
+ "wherearewe": "^2.0.1"
+ }
+ },
+ "node_modules/@chainsafe/libp2p-noise/node_modules/multiformats": {
+ "version": "13.4.2",
+ "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-13.4.2.tgz",
+ "integrity": "sha512-eh6eHCrRi1+POZ3dA+Dq1C6jhP1GNtr9CRINMb67OKzqW9I5DUuZM/3jLPlzhgpGeiNUlEGEbkCYChXMCc/8DQ=="
+ },
+ "node_modules/@chainsafe/libp2p-noise/node_modules/protons-runtime": {
+ "version": "5.6.0",
+ "resolved": "https://registry.npmjs.org/protons-runtime/-/protons-runtime-5.6.0.tgz",
+ "integrity": "sha512-/Kde+sB9DsMFrddJT/UZWe6XqvL7SL5dbag/DBCElFKhkwDj7XKt53S+mzLyaDP5OqS0wXjV5SA572uWDaT0Hg==",
+ "dependencies": {
+ "uint8-varint": "^2.0.2",
+ "uint8arraylist": "^2.4.3",
+ "uint8arrays": "^5.0.1"
+ }
+ },
+ "node_modules/@chainsafe/libp2p-noise/node_modules/uint8-varint": {
+ "version": "2.0.5",
+ "resolved": "https://registry.npmjs.org/uint8-varint/-/uint8-varint-2.0.5.tgz",
+ "integrity": "sha512-jeFLbL/x30wBRnWjKE1qVBXeumG46r7XmYkpis955lTQ+blccGKFrOsSMHlxePwYB1pI7L8YPHz1t4jLxEs3nA==",
+ "dependencies": {
+ "uint8arraylist": "^2.0.0",
+ "uint8arrays": "^5.0.0"
+ }
+ },
+ "node_modules/@chainsafe/libp2p-noise/node_modules/uint8arrays": {
+ "version": "5.1.1",
+ "resolved": "https://registry.npmjs.org/uint8arrays/-/uint8arrays-5.1.1.tgz",
+ "integrity": "sha512-9muQwa4wZG4dKi9gMAIBtnk2Pw87SRpvWTH6lOGm19V2Uqxr4uomUf2PGqPnWc+qs06sN8owUU4jfcoWOcfwVQ==",
+ "dependencies": {
+ "multiformats": "^13.0.0"
+ }
+ },
"node_modules/@chainsafe/libp2p-yamux": {
"version": "8.0.1",
"resolved": "https://registry.npmjs.org/@chainsafe/libp2p-yamux/-/libp2p-yamux-8.0.1.tgz",
@@ -178,6 +242,422 @@
"node": ">=6"
}
},
+ "node_modules/@esbuild/aix-ppc64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz",
+ "integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==",
+ "cpu": [
+ "ppc64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "aix"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/android-arm": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz",
+ "integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==",
+ "cpu": [
+ "arm"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "android"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/android-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz",
+ "integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "android"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/android-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz",
+ "integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "android"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/darwin-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz",
+ "integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "darwin"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/darwin-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz",
+ "integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "darwin"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/freebsd-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz",
+ "integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "freebsd"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/freebsd-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz",
+ "integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "freebsd"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-arm": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz",
+ "integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==",
+ "cpu": [
+ "arm"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz",
+ "integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-ia32": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz",
+ "integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==",
+ "cpu": [
+ "ia32"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-loong64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz",
+ "integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==",
+ "cpu": [
+ "loong64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-mips64el": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz",
+ "integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==",
+ "cpu": [
+ "mips64el"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-ppc64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz",
+ "integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==",
+ "cpu": [
+ "ppc64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-riscv64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz",
+ "integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==",
+ "cpu": [
+ "riscv64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-s390x": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz",
+ "integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==",
+ "cpu": [
+ "s390x"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/linux-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz",
+ "integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/netbsd-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz",
+ "integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "netbsd"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/netbsd-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz",
+ "integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "netbsd"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/openbsd-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz",
+ "integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "openbsd"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/openbsd-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz",
+ "integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "openbsd"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/openharmony-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz",
+ "integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "openharmony"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/sunos-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz",
+ "integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "sunos"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/win32-arm64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz",
+ "integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/win32-ia32": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz",
+ "integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==",
+ "cpu": [
+ "ia32"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/@esbuild/win32-x64": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.2.tgz",
+ "integrity": "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">=18"
+ }
+ },
"node_modules/@hono/node-server": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/@hono/node-server/-/node-server-2.1.1.tgz",
@@ -769,6 +1249,17 @@
"@multiformats/multiaddr": "^13.0.0"
}
},
+ "node_modules/@noble/ciphers": {
+ "version": "2.4.0",
+ "resolved": "https://registry.npmjs.org/@noble/ciphers/-/ciphers-2.4.0.tgz",
+ "integrity": "sha512-AnjFn0Jv92laAkvMrghlFZq4qQCIN/4DxFV/eooqtC2YTjB7kBeLMS2T9KJX4Dn+ZVXLOwK0lSgqDtx9gvxtiw==",
+ "engines": {
+ "node": ">= 20.19.0"
+ },
+ "funding": {
+ "url": "https://paulmillr.com/funding/"
+ }
+ },
"node_modules/@noble/curves": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/@noble/curves/-/curves-2.4.0.tgz",
@@ -1857,6 +2348,47 @@
"node": ">= 0.4"
}
},
+ "node_modules/esbuild": {
+ "version": "0.28.2",
+ "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz",
+ "integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==",
+ "dev": true,
+ "hasInstallScript": true,
+ "bin": {
+ "esbuild": "bin/esbuild"
+ },
+ "engines": {
+ "node": ">=18"
+ },
+ "optionalDependencies": {
+ "@esbuild/aix-ppc64": "0.28.2",
+ "@esbuild/android-arm": "0.28.2",
+ "@esbuild/android-arm64": "0.28.2",
+ "@esbuild/android-x64": "0.28.2",
+ "@esbuild/darwin-arm64": "0.28.2",
+ "@esbuild/darwin-x64": "0.28.2",
+ "@esbuild/freebsd-arm64": "0.28.2",
+ "@esbuild/freebsd-x64": "0.28.2",
+ "@esbuild/linux-arm": "0.28.2",
+ "@esbuild/linux-arm64": "0.28.2",
+ "@esbuild/linux-ia32": "0.28.2",
+ "@esbuild/linux-loong64": "0.28.2",
+ "@esbuild/linux-mips64el": "0.28.2",
+ "@esbuild/linux-ppc64": "0.28.2",
+ "@esbuild/linux-riscv64": "0.28.2",
+ "@esbuild/linux-s390x": "0.28.2",
+ "@esbuild/linux-x64": "0.28.2",
+ "@esbuild/netbsd-arm64": "0.28.2",
+ "@esbuild/netbsd-x64": "0.28.2",
+ "@esbuild/openbsd-arm64": "0.28.2",
+ "@esbuild/openbsd-x64": "0.28.2",
+ "@esbuild/openharmony-arm64": "0.28.2",
+ "@esbuild/sunos-x64": "0.28.2",
+ "@esbuild/win32-arm64": "0.28.2",
+ "@esbuild/win32-ia32": "0.28.2",
+ "@esbuild/win32-x64": "0.28.2"
+ }
+ },
"node_modules/escape-html": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
@@ -2198,6 +2730,11 @@
"node": ">= 0.10"
}
},
+ "node_modules/is-electron": {
+ "version": "2.2.2",
+ "resolved": "https://registry.npmjs.org/is-electron/-/is-electron-2.2.2.tgz",
+ "integrity": "sha512-FO/Rhvz5tuw4MCWkpMzHFKWD2LsfHzIb7i6MdPYZ/KW7AlxawyLkqdy+jPZP1WubqEADE3O4FUENlJHDfQASRg=="
+ },
"node_modules/is-loopback-addr": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/is-loopback-addr/-/is-loopback-addr-2.1.0.tgz",
@@ -3313,6 +3850,18 @@
"tslib": "^2.8.1"
}
},
+ "node_modules/wherearewe": {
+ "version": "2.0.1",
+ "resolved": "https://registry.npmjs.org/wherearewe/-/wherearewe-2.0.1.tgz",
+ "integrity": "sha512-XUguZbDxCA2wBn2LoFtcEhXL6AXo+hVjGonwhSTTTU9SzbWG8Xu3onNIpzf9j/mYUcJQ0f+m37SzG77G851uFw==",
+ "dependencies": {
+ "is-electron": "^2.2.0"
+ },
+ "engines": {
+ "node": ">=16.0.0",
+ "npm": ">=7.0.0"
+ }
+ },
"node_modules/which": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
diff --git a/sdk/js/package.json b/sdk/js/package.json
index a735ce3b..ea85f34f 100644
--- a/sdk/js/package.json
+++ b/sdk/js/package.json
@@ -1,7 +1,7 @@
{
"name": "@sam-mesh/sdk",
"version": "0.1.0",
- "description": "Native SDK for joining a SAM agent mesh from inside a Node.js agent process",
+ "description": "Native SDK for joining a SAM agent mesh from a Node.js agent process or a browser page",
"license": "Apache-2.0",
"repository": {
"type": "git",
@@ -18,6 +18,12 @@
"default": "./dist/index.js"
}
},
+ "browser": {
+ "./dist/platform/ingress.js": "./dist/platform/ingress.browser.js",
+ "./dist/platform/state.js": "./dist/platform/state.browser.js",
+ "./dist/platform/transports.js": "./dist/platform/transports.browser.js",
+ "./dist/platform/wasm.js": "./dist/platform/wasm.browser.js"
+ },
"files": [
"dist",
"README.md"
@@ -31,11 +37,13 @@
"examples": "tsc -p tsconfig.examples.json",
"typecheck": "tsc -p tsconfig.json --noEmit",
"pretest": "tsc -p tsconfig.test.json",
- "test": "node --test 'build/**/*.test.js'"
+ "test": "node --test 'build/**/*.test.js'",
+ "bundle:browser": "node scripts/bundle-browser.mjs"
},
"dependencies": {
"@biscuit-auth/biscuit-wasm": "^0.6.0",
"@bufbuild/protobuf": "^2.15.0",
+ "@chainsafe/libp2p-noise": "^17.0.0",
"@chainsafe/libp2p-yamux": "^8.0.1",
"@libp2p/circuit-relay-v2": "^4.2.13",
"@libp2p/crypto": "^5.1.23",
@@ -62,6 +70,7 @@
"@bufbuild/protoc-gen-es": "^2.15.0",
"@types/express": "^5.0.6",
"@types/node": "^26.6.1",
+ "esbuild": "^0.28.2",
"express": "^5.2.1",
"typescript": "^7.0.2"
}
diff --git a/sdk/js/scripts/bundle-browser.mjs b/sdk/js/scripts/bundle-browser.mjs
new file mode 100644
index 00000000..77891fdb
--- /dev/null
+++ b/sdk/js/scripts/bundle-browser.mjs
@@ -0,0 +1,97 @@
+#!/usr/bin/env node
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Bundles an ES module for a browser page with esbuild, the way a page's own
+// bundler would: package.json "browser" fields swap the Node platform files
+// for their browser twins, and a .wasm import (biscuit-wasm is built for
+// bundlers and imports its module that way) becomes a module that fetches
+// the file beside the bundle and instantiates it. Bundling dist/index.js
+// this way also proves that nothing in the browser graph reaches for Node.
+//
+// node scripts/bundle-browser.mjs [entry] [outdir]
+//
+// Defaults: dist/index.js into build/browser.
+
+import * as esbuild from "esbuild";
+import { readFile } from "node:fs/promises";
+import { basename, dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+
+const here = dirname(fileURLToPath(import.meta.url));
+const [entry = join(here, "..", "dist", "index.js"), outdir = join(here, "..", "build", "browser")] = process.argv.slice(2);
+
+/** import * as wasm from "./x.wasm" as WebAssembly ESM integration would resolve it. */
+const wasmModules = {
+ name: "wasm-esm",
+ setup(build) {
+ build.onResolve({ filter: /\.wasm$/ }, (args) => ({ path: join(args.resolveDir, args.path), namespace: "wasm-esm" }));
+ build.onLoad({ filter: /.*/, namespace: "wasm-esm" }, async (args) => {
+ const bytes = await readFile(args.path);
+ const module = new WebAssembly.Module(bytes);
+ const importModules = [...new Set(WebAssembly.Module.imports(module).map((imp) => imp.module))];
+ const exports = WebAssembly.Module.exports(module).map((exp) => exp.name);
+ const lines = [];
+ const imports = [];
+ importModules.forEach((mod, i) => {
+ if (mod.startsWith("./") || mod.startsWith("../")) {
+ lines.push(`import * as m${i} from ${JSON.stringify(mod)};`);
+ imports.push(`${JSON.stringify(mod)}: m${i}`);
+ } else {
+ // wasm-bindgen's own placeholder module: intrinsics the bindings provide by name.
+ imports.push(`${JSON.stringify(mod)}: { performance_now: () => performance.now() }`);
+ }
+ });
+ lines.push(`const url = new URL(${JSON.stringify(basename(args.path))}, import.meta.url);`);
+ lines.push(`const { instance } = await WebAssembly.instantiateStreaming(fetch(url), { ${imports.join(", ")} });`);
+ for (const name of exports) {
+ lines.push(`export const ${name} = instance.exports[${JSON.stringify(name)}];`);
+ }
+ return { contents: lines.join("\n"), loader: "js", resolveDir: dirname(args.path), watchFiles: [args.path] };
+ });
+ // The .wasm file itself goes beside the bundle under its own name.
+ build.onResolve({ filter: /\.wasm$/, namespace: "wasm-esm" }, (args) => ({ path: args.path, namespace: "wasm-file" }));
+ },
+};
+
+const result = await esbuild.build({
+ entryPoints: [entry],
+ bundle: true,
+ format: "esm",
+ platform: "browser",
+ target: "es2022",
+ outdir,
+ entryNames: "[name]",
+ assetNames: "[name]",
+ sourcemap: true,
+ plugins: [wasmModules],
+ metafile: true,
+ logLevel: "warning",
+});
+
+// The bytes the stub fetches: copy every .wasm the graph touched beside the bundle.
+const { copyFile, mkdir } = await import("node:fs/promises");
+await mkdir(outdir, { recursive: true });
+for (const input of Object.keys(result.metafile.inputs)) {
+ if (input.startsWith("wasm-esm:")) {
+ const file = input.slice("wasm-esm:".length);
+ await copyFile(file, join(outdir, basename(file)));
+ }
+}
+const nodeBuiltins = Object.keys(result.metafile.inputs).filter((p) => p.startsWith("node:"));
+if (nodeBuiltins.length > 0) {
+ console.error(`browser bundle reaches Node: ${nodeBuiltins.join(", ")}`);
+ process.exit(1);
+}
+console.log(`bundled ${entry} into ${outdir}`);
diff --git a/sdk/js/src/biscuit.ts b/sdk/js/src/biscuit.ts
index 2ef5bd0f..9e2f6d32 100644
--- a/sdk/js/src/biscuit.ts
+++ b/sdk/js/src/biscuit.ts
@@ -16,46 +16,13 @@
// signed by a trusted control plane key, authority block only, unexpired,
// and bound to the peer at the other end of the connection.
-import { readFile } from "node:fs/promises";
-import { fileURLToPath } from "node:url";
-
-// biscuit-wasm is built for bundlers and imports its .wasm as a module,
-// which Node only does behind a flag. Instantiating it by hand avoids the
-// flag; the module's import list says what it needs.
-type BiscuitWasm = typeof import("@biscuit-auth/biscuit-wasm");
+import { loadBiscuitWasm, type BiscuitWasm } from "./platform/wasm.ts";
let loading: Promise<BiscuitWasm> | undefined;
+/** The biscuit-wasm module, loaded once, the way the runtime loads it. */
export function loadBiscuit(): Promise<BiscuitWasm> {
- loading ??= (async () => {
- const dir = new URL("./", import.meta.resolve("@biscuit-auth/biscuit-wasm"));
- const bindings = (await import(new URL("biscuit_bg.js", dir).href)) as BiscuitWasm & {
- __wbg_set_wasm(exports: WebAssembly.Exports): void;
- };
- const module = await WebAssembly.compile(await readFile(fileURLToPath(new URL("biscuit_bg.wasm", dir))));
- const imports: WebAssembly.Imports = {};
- for (const imp of WebAssembly.Module.imports(module)) {
- const ns = (imports[imp.module] ??= {}) as Record<string, WebAssembly.ImportValue>;
- if (imp.module === "./biscuit_bg.js") {
- ns[imp.name] = (bindings as unknown as Record<string, WebAssembly.ImportValue>)[imp.name] as WebAssembly.ImportValue;
- } else if (imp.name === "performance_now") {
- ns[imp.name] = () => performance.now();
- } else {
- throw new Error(`biscuit-wasm needs an unknown import ${imp.module}:${imp.name}`);
- }
- }
- const instance = await WebAssembly.instantiate(module, imports);
- bindings.__wbg_set_wasm(instance.exports);
- // The module's start hook logs a greeting to the console.
- const log = console.log;
- console.log = () => {};
- try {
- (instance.exports as { __wbindgen_start(): void }).__wbindgen_start();
- } finally {
- console.log = log;
- }
- return bindings;
- })();
+ loading ??= loadBiscuitWasm();
return loading;
}
diff --git a/sdk/js/src/host.ts b/sdk/js/src/host.ts
index 39eb6d7b..93f09b8c 100644
--- a/sdk/js/src/host.ts
+++ b/sdk/js/src/host.ts
@@ -13,11 +13,10 @@
// limitations under the License.
// The libp2p host a member joins the mesh with, configured the way
-// sam-node's is (internal/node/node.go): TLS for the security protocol,
-// which every peer offers first, yamux the muxer, circuit relay v2 for
-// reachability through the routers. TCP and WebSocket are the transports:
-// the testnets' routers listen on TCP, sam-one's single port is a WebSocket
-// listener.
+// sam-node's is (internal/node/node.go): yamux the muxer, circuit relay v2
+// for reachability through the routers. Transports and the security
+// protocol come from the runtime (platform/transports.ts): on Node TCP and
+// WebSocket with TLS, in a browser WebSocket with Noise.
import { yamux } from "@chainsafe/libp2p-yamux";
import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
@@ -27,13 +26,11 @@ import { identify } from "@libp2p/identify";
import type { Libp2p, PeerId } from "@libp2p/interface";
import { kadDHT, passthroughMapper } from "@libp2p/kad-dht";
import { ping } from "@libp2p/ping";
-import { tcp } from "@libp2p/tcp";
-import { tls } from "@libp2p/tls";
-import { webSockets } from "@libp2p/websockets";
import type { Multiaddr } from "@multiformats/multiaddr";
import { createLibp2p, type Libp2pOptions } from "libp2p";
import { DHT_PROTOCOL } from "./discovery.ts";
import type { Identity } from "./identity.ts";
+import { connectionEncrypters, transports } from "./platform/transports.ts";
export interface MeshHostOptions {
/**
@@ -62,8 +59,8 @@ export async function createMeshHost(identity: Identity, options: MeshHostOption
privateKey: privateKeyFromProtobuf(identity.toLibp2pPrivateKey()),
addresses: { listen: options.listenAddrs ?? [] },
...(options.dns !== undefined ? { dns: options.dns } : {}),
- transports: [tcp(), webSockets(), circuitRelayTransport()],
- connectionEncrypters: [tls()],
+ transports: [...transports(), circuitRelayTransport()],
+ connectionEncrypters: connectionEncrypters(),
streamMuxers: [yamux()],
connectionGater: {
denyDialPeer: denyBanned,
diff --git a/sdk/js/src/index.ts b/sdk/js/src/index.ts
index 5648df3b..47d2b496 100644
--- a/sdk/js/src/index.ts
+++ b/sdk/js/src/index.ts
@@ -67,6 +67,7 @@ export {
type NodeRequestListener,
type ProviderOptions,
} from "./libp2p-http.ts";
-export { nodeIngressHandler, streamToNodeDuplex } from "./libp2p-http-node.ts";
+export { ingressHandler } from "./platform/ingress.ts";
+export type { StateStore } from "./platform/types.ts";
export { BASELINE_DATALOG } from "./gen/datalog.ts";
export { BanSet, EVENT_FRESHNESS_MS, GOSSIP_EVENTS_TOPIC, verifyMeshEvent, type VerifiedMeshEvent } from "./sync.ts";
diff --git a/sdk/js/src/mesh.ts b/sdk/js/src/mesh.ts
index f08be813..5634f574 100644
--- a/sdk/js/src/mesh.ts
+++ b/sdk/js/src/mesh.ts
@@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
-import { join } from "node:path";
import { toHex } from "./bytes.ts";
import { ControlPlaneClient, ROLE_NODE, type Enrollment } from "./controlplane.ts";
import { credentialFromJSON, credentialPredatesRotation, credentialTimeToLiveSeconds, credentialToJSON, encodeAuthFrame, type MeshCredential } from "./credential.ts";
import { Identity } from "./identity.ts";
+import { openState, readTextFile } from "./platform/state.ts";
+import type { StateStore } from "./platform/types.ts";
import { joinMesh, type JoinOptions, type MeshSession } from "./session.ts";
const IDENTITY_FILE = "identity.key";
@@ -31,8 +31,9 @@ export interface AgentMeshOptions {
/** Accept plaintext http:// to a non-loopback control plane. Off by default. */
allowInsecure?: boolean;
/**
- * Directory that keeps the identity key and the credential across
- * restarts. Without it the identity lives only in this process.
+ * Where the identity key and the credential are kept across restarts: a
+ * directory on Node, an IndexedDB database of that name in a browser.
+ * Without it the identity lives only in this process.
*/
stateDir?: string | undefined;
/** Use this identity instead of the persisted or a freshly generated one. */
@@ -48,7 +49,7 @@ export interface AgentMeshOptions {
export interface EnrollOptions extends AgentMeshOptions {
/** A bootstrap token value, when the caller already holds it in memory. */
bootstrapToken?: string | undefined;
- /** Path of a file holding the bootstrap token. Preferred over a value. */
+ /** Path of a file holding the bootstrap token. Preferred over a value on Node; a browser has no files. */
bootstrapTokenPath?: string | undefined;
/** An OIDC ID token, for meshes that enroll identities interactively. */
jwt?: string | undefined;
@@ -87,13 +88,13 @@ export class AgentMesh {
readonly identity: Identity;
readonly controlPlane: ControlPlaneClient;
#credential: MeshCredential;
- readonly #stateDir: string | undefined;
+ readonly #state: StateStore | undefined;
- private constructor(identity: Identity, controlPlane: ControlPlaneClient, credential: MeshCredential, stateDir: string | undefined) {
+ private constructor(identity: Identity, controlPlane: ControlPlaneClient, credential: MeshCredential, state: StateStore | undefined) {
this.identity = identity;
this.controlPlane = controlPlane;
this.#credential = credential;
- this.#stateDir = stateDir;
+ this.#state = state;
}
get peerId(): string {
@@ -115,13 +116,14 @@ export class AgentMesh {
* the state directory to enroll afresh, for instance with other labels.
*/
static async enroll(options: EnrollOptions): Promise<AgentMesh> {
- const saved = await loadIdentity(options.stateDir);
+ const state = options.stateDir !== undefined ? openState(options.stateDir) : undefined;
+ const saved = await loadIdentity(state);
const identity = options.identity ?? saved ?? Identity.generate();
const controlPlane = newClient(options);
- if (options.stateDir !== undefined && saved !== undefined && saved.peerId === identity.peerId) {
- const credential = await loadCredential(options.stateDir);
+ if (state !== undefined && saved !== undefined && saved.peerId === identity.peerId) {
+ const credential = await loadCredential(state);
if (credential !== undefined && sameBaseUrl(credential.controlPlaneUrl, controlPlane.url) && credentialTimeToLiveSeconds(credential) > REUSE_MIN_TTL_SECONDS) {
- return new AgentMesh(identity, controlPlane, credential, options.stateDir);
+ return new AgentMesh(identity, controlPlane, credential, state);
}
}
const given = [options.bootstrapToken, options.bootstrapTokenPath, options.jwt, options.jwtPath].filter((v) => v !== undefined).length;
@@ -133,10 +135,10 @@ export class AgentMesh {
let enrollment: Enrollment;
if (options.jwt !== undefined || options.jwtPath !== undefined) {
- const jwt = options.jwtPath !== undefined ? (await readFile(options.jwtPath, "utf8")).trim() : (options.jwt as string);
+ const jwt = options.jwtPath !== undefined ? (await readTextFile(options.jwtPath)).trim() : (options.jwt as string);
enrollment = await controlPlane.register({ identity, jwt, role, ...labelsOf(options) });
} else {
- const bootstrapToken = options.bootstrapTokenPath !== undefined ? (await readFile(options.bootstrapTokenPath, "utf8")).trim() : (options.bootstrapToken as string);
+ const bootstrapToken = options.bootstrapTokenPath !== undefined ? (await readTextFile(options.bootstrapTokenPath)).trim() : (options.bootstrapToken as string);
enrollment = await controlPlane.enrollBootstrap({
identity,
bootstrapToken,
@@ -168,7 +170,7 @@ export class AgentMesh {
issuedUnderKeys: controlPlaneKeys,
routerAddresses: enrollment.routerAddresses,
},
- options.stateDir,
+ state,
);
await mesh.save();
return mesh;
@@ -180,15 +182,16 @@ export class AgentMesh {
* plane URL comes from the saved credential.
*/
static async load(options: Omit<AgentMeshOptions, "controlPlaneUrl"> & { stateDir: string }): Promise<AgentMesh> {
- const identity = options.identity ?? (await loadIdentity(options.stateDir));
+ const state = openState(options.stateDir);
+ const identity = options.identity ?? (await loadIdentity(state));
if (!identity) {
throw new Error(`no identity in ${options.stateDir}; enroll first`);
}
- const credential = await loadCredential(options.stateDir);
+ const credential = await loadCredential(state);
if (credential === undefined) {
throw new Error(`no credential in ${options.stateDir}; enroll first`);
}
- return new AgentMesh(identity, newClient({ ...options, controlPlaneUrl: credential.controlPlaneUrl }), credential, options.stateDir);
+ return new AgentMesh(identity, newClient({ ...options, controlPlaneUrl: credential.controlPlaneUrl }), credential, state);
}
/**
@@ -286,14 +289,13 @@ export class AgentMesh {
return joinMesh(this, options);
}
- /** Writes identity and credential to the state directory, if one is configured. */
+ /** Writes identity and credential to the state store, if one is configured. */
async save(): Promise<void> {
- if (this.#stateDir === undefined) {
+ if (this.#state === undefined) {
return;
}
- await mkdir(this.#stateDir, { recursive: true, mode: 0o700 });
- await writeAtomic(join(this.#stateDir, IDENTITY_FILE), this.identity.toLibp2pPrivateKey(), 0o600);
- await writeAtomic(join(this.#stateDir, CREDENTIAL_FILE), credentialToJSON(this.#credential), 0o600);
+ await this.#state.write(IDENTITY_FILE, this.identity.toLibp2pPrivateKey());
+ await this.#state.write(CREDENTIAL_FILE, new TextEncoder().encode(credentialToJSON(this.#credential)));
}
}
@@ -318,29 +320,14 @@ function labelsOf(options: AgentMeshOptions): { labels?: Record<string, string>
return options.labels !== undefined ? { labels: options.labels } : {};
}
-async function loadIdentity(stateDir: string | undefined): Promise<Identity | undefined> {
- if (stateDir === undefined) {
- return undefined;
- }
- try {
- return Identity.fromLibp2pPrivateKey(new Uint8Array(await readFile(join(stateDir, IDENTITY_FILE))));
- } catch (err) {
- if ((err as NodeJS.ErrnoException).code === "ENOENT") {
- return undefined;
- }
- throw err;
- }
+async function loadIdentity(state: StateStore | undefined): Promise<Identity | undefined> {
+ const bytes = await state?.read(IDENTITY_FILE);
+ return bytes === undefined ? undefined : Identity.fromLibp2pPrivateKey(bytes);
}
-async function loadCredential(stateDir: string): Promise<MeshCredential | undefined> {
- try {
- return credentialFromJSON(await readFile(join(stateDir, CREDENTIAL_FILE), "utf8"));
- } catch (err) {
- if ((err as NodeJS.ErrnoException).code === "ENOENT") {
- return undefined;
- }
- throw err;
- }
+async function loadCredential(state: StateStore): Promise<MeshCredential | undefined> {
+ const bytes = await state.read(CREDENTIAL_FILE);
+ return bytes === undefined ? undefined : credentialFromJSON(new TextDecoder().decode(bytes));
}
/** The form every implementation persists: scheme, host, port, path, no trailing slash. */
@@ -351,9 +338,3 @@ function baseUrl(url: URL | string): string {
function sameBaseUrl(a: URL | string, b: URL | string): boolean {
return baseUrl(a) === baseUrl(b);
}
-
-async function writeAtomic(path: string, data: Uint8Array | string, mode: number): Promise<void> {
- const tmp = `${path}.tmp`;
- await writeFile(tmp, data, { mode });
- await rename(tmp, path);
-}
diff --git a/sdk/js/src/platform/ingress.browser.ts b/sdk/js/src/platform/ingress.browser.ts
new file mode 100644
index 00000000..992c3125
--- /dev/null
+++ b/sdk/js/src/platform/ingress.browser.ts
@@ -0,0 +1,18 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// The /libp2p-http ingress in a browser: a fetch handler or a URL; a Node
+// request listener is answered 501, there is no Node HTTP server to run it.
+
+export { httpIngressHandler as ingressHandler } from "../libp2p-http.ts";
diff --git a/sdk/js/src/platform/ingress.ts b/sdk/js/src/platform/ingress.ts
new file mode 100644
index 00000000..9fbd806f
--- /dev/null
+++ b/sdk/js/src/platform/ingress.ts
@@ -0,0 +1,17 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// The /libp2p-http ingress on Node serves a Node request listener too.
+
+export { nodeIngressHandler as ingressHandler } from "../libp2p-http-node.ts";
diff --git a/sdk/js/src/platform/state.browser.ts b/sdk/js/src/platform/state.browser.ts
new file mode 100644
index 00000000..06913964
--- /dev/null
+++ b/sdk/js/src/platform/state.browser.ts
@@ -0,0 +1,58 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// State in a browser: an IndexedDB database per state location, one record
+// per name, kept by the browser for the page's origin. A put replaces the
+// record in one transaction, so a reader sees the old or the new value.
+
+import type { StateStore } from "./types.ts";
+
+const STORE = "state";
+
+function request<T>(req: IDBRequest<T>): Promise<T> {
+ return new Promise((resolve, reject) => {
+ req.onsuccess = () => resolve(req.result);
+ req.onerror = () => reject(req.error ?? new Error("IndexedDB request failed"));
+ });
+}
+
+function openDatabase(name: string): Promise<IDBDatabase> {
+ const req = indexedDB.open(`sam-mesh:${name}`, 1);
+ req.onupgradeneeded = () => {
+ req.result.createObjectStore(STORE);
+ };
+ return request(req);
+}
+
+/** The store for a state location: a database named after it. */
+export function openState(location: string): StateStore {
+ let db: Promise<IDBDatabase> | undefined;
+ const open = () => (db ??= openDatabase(location));
+ return {
+ async read(name) {
+ const tx = (await open()).transaction(STORE, "readonly");
+ const value = await request(tx.objectStore(STORE).get(name));
+ return value instanceof Uint8Array ? value : undefined;
+ },
+ async write(name, data) {
+ const tx = (await open()).transaction(STORE, "readwrite");
+ await request(tx.objectStore(STORE).put(new Uint8Array(data), name));
+ },
+ };
+}
+
+/** A browser has no file system a page may read; give the token itself. */
+export async function readTextFile(path: string): Promise<string> {
+ throw new Error(`cannot read ${path}: a browser has no files to read a token from; pass the value instead`);
+}
diff --git a/sdk/js/src/platform/state.ts b/sdk/js/src/platform/state.ts
new file mode 100644
index 00000000..49df0c48
--- /dev/null
+++ b/sdk/js/src/platform/state.ts
@@ -0,0 +1,48 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// State on Node: a directory, one file per name, owner-only, written to a
+// temporary name and renamed into place. Where a token file is read from.
+
+import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
+import { join } from "node:path";
+import type { StateStore } from "./types.ts";
+
+/** The store for a state location: a directory, created on first write. */
+export function openState(location: string): StateStore {
+ return {
+ async read(name) {
+ try {
+ return new Uint8Array(await readFile(join(location, name)));
+ } catch (err) {
+ if ((err as NodeJS.ErrnoException).code === "ENOENT") {
+ return undefined;
+ }
+ throw err;
+ }
+ },
+ async write(name, data) {
+ await mkdir(location, { recursive: true, mode: 0o700 });
+ const path = join(location, name);
+ const tmp = `${path}.tmp`;
+ await writeFile(tmp, data, { mode: 0o600 });
+ await rename(tmp, path);
+ },
+ };
+}
+
+/** A text file, for a token an operator or a platform placed on disk. */
+export async function readTextFile(path: string): Promise<string> {
+ return readFile(path, "utf8");
+}
diff --git a/sdk/js/src/platform/transports.browser.ts b/sdk/js/src/platform/transports.browser.ts
new file mode 100644
index 00000000..e020de13
--- /dev/null
+++ b/sdk/js/src/platform/transports.browser.ts
@@ -0,0 +1,32 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// How a member in a browser reaches the mesh: WebSocket, the one transport a
+// page can open to a router (sam-one's single port, or a router behind a
+// TLS-terminating proxy as wss), secured with Noise. A browser cannot run
+// libp2p's TLS, which needs a self-signed certificate over a raw socket;
+// routers and nodes accept Noise beside TLS, and both bind the connection to
+// the peer ID.
+
+import { noise } from "@chainsafe/libp2p-noise";
+import { webSockets } from "@libp2p/websockets";
+import type { Libp2pOptions } from "libp2p";
+
+export function transports(): NonNullable<Libp2pOptions["transports"]> {
+ return [webSockets()];
+}
+
+export function connectionEncrypters(): NonNullable<Libp2pOptions["connectionEncrypters"]> {
+ return [noise()];
+}
diff --git a/sdk/js/src/platform/transports.ts b/sdk/js/src/platform/transports.ts
new file mode 100644
index 00000000..7ed3e374
--- /dev/null
+++ b/sdk/js/src/platform/transports.ts
@@ -0,0 +1,30 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// How a member on Node reaches the mesh: TCP and WebSocket, the transports
+// the routers listen on, secured with TLS 1.3, which every peer offers
+// first (internal/node/node.go).
+
+import { tcp } from "@libp2p/tcp";
+import { tls } from "@libp2p/tls";
+import { webSockets } from "@libp2p/websockets";
+import type { Libp2pOptions } from "libp2p";
+
+export function transports(): NonNullable<Libp2pOptions["transports"]> {
+ return [tcp(), webSockets()];
+}
+
+export function connectionEncrypters(): NonNullable<Libp2pOptions["connectionEncrypters"]> {
+ return [tls()];
+}
diff --git a/sdk/js/src/platform/types.ts b/sdk/js/src/platform/types.ts
new file mode 100644
index 00000000..4118e0b7
--- /dev/null
+++ b/sdk/js/src/platform/types.ts
@@ -0,0 +1,25 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// What differs between Node and a browser, as the rest of the SDK sees it.
+// Each concern has a Node module and a .browser.ts twin with the same
+// exports; package.json's "browser" field points a bundler at the twin.
+
+/** Where a member keeps its identity and credential between runs. */
+export interface StateStore {
+ /** The bytes under a name, or undefined when nothing was written yet. */
+ read(name: string): Promise<Uint8Array | undefined>;
+ /** Writes the bytes under a name so that a reader sees the old or the new value, never a mix. */
+ write(name: string, data: Uint8Array): Promise<void>;
+}
diff --git a/sdk/js/src/platform/wasm.browser.ts b/sdk/js/src/platform/wasm.browser.ts
new file mode 100644
index 00000000..68aaf04e
--- /dev/null
+++ b/sdk/js/src/platform/wasm.browser.ts
@@ -0,0 +1,25 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// biscuit-wasm is built for bundlers: its entry imports the .wasm as an ES
+// module, which the bundler of the page resolves (webpack's
+// asyncWebAssembly, vite-plugin-wasm, or the esbuild plugin in
+// scripts/bundle-browser.mjs). The module is loaded once the page first
+// needs it.
+
+export type BiscuitWasm = typeof import("@biscuit-auth/biscuit-wasm");
+
+export async function loadBiscuitWasm(): Promise<BiscuitWasm> {
+ return import("@biscuit-auth/biscuit-wasm");
+}
diff --git a/sdk/js/src/platform/wasm.ts b/sdk/js/src/platform/wasm.ts
new file mode 100644
index 00000000..35479ed4
--- /dev/null
+++ b/sdk/js/src/platform/wasm.ts
@@ -0,0 +1,52 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// biscuit-wasm is built for bundlers and imports its .wasm as a module,
+// which Node only does behind a flag. Instantiating it by hand avoids the
+// flag; the module's import list says what it needs.
+
+import { readFile } from "node:fs/promises";
+import { fileURLToPath } from "node:url";
+
+export type BiscuitWasm = typeof import("@biscuit-auth/biscuit-wasm");
+
+export async function loadBiscuitWasm(): Promise<BiscuitWasm> {
+ const dir = new URL("./", import.meta.resolve("@biscuit-auth/biscuit-wasm"));
+ const bindings = (await import(new URL("biscuit_bg.js", dir).href)) as BiscuitWasm & {
+ __wbg_set_wasm(exports: WebAssembly.Exports): void;
+ };
+ const module = await WebAssembly.compile(await readFile(fileURLToPath(new URL("biscuit_bg.wasm", dir))));
+ const imports: WebAssembly.Imports = {};
+ for (const imp of WebAssembly.Module.imports(module)) {
+ const ns = (imports[imp.module] ??= {}) as Record<string, WebAssembly.ImportValue>;
+ if (imp.module === "./biscuit_bg.js") {
+ ns[imp.name] = (bindings as unknown as Record<string, WebAssembly.ImportValue>)[imp.name] as WebAssembly.ImportValue;
+ } else if (imp.name === "performance_now") {
+ ns[imp.name] = () => performance.now();
+ } else {
+ throw new Error(`biscuit-wasm needs an unknown import ${imp.module}:${imp.name}`);
+ }
+ }
+ const instance = await WebAssembly.instantiate(module, imports);
+ bindings.__wbg_set_wasm(instance.exports);
+ // The module's start hook logs a greeting to the console.
+ const log = console.log;
+ console.log = () => {};
+ try {
+ (instance.exports as { __wbindgen_start(): void }).__wbindgen_start();
+ } finally {
+ console.log = log;
+ }
+ return bindings;
+}
diff --git a/sdk/js/src/session.ts b/sdk/js/src/session.ts
index bb09a881..50f57249 100644
--- a/sdk/js/src/session.ts
+++ b/sdk/js/src/session.ts
@@ -38,7 +38,7 @@ import {
type HTTPResponse,
type ProviderOptions,
} from "./libp2p-http.ts";
-import { nodeIngressHandler } from "./libp2p-http-node.ts";
+import { ingressHandler } from "./platform/ingress.ts";
import { BanSet, GOSSIP_EVENTS_TOPIC, MeshEvent_Type, verifyMeshEvent } from "./sync.ts";
export interface JoinOptions extends MeshHostOptions {
@@ -491,7 +491,7 @@ export class MeshSession {
isBanned: (peerId) => this.banned.has(peerId),
onAuthorized: (peerId, verified) => this.authenticatedPeers.set(peerId, verified.expiration),
};
- await this.node.handle(HTTP_PROTOCOL, nodeIngressHandler(endpoint, providerOptions), HTTP_HANDLER_OPTIONS);
+ await this.node.handle(HTTP_PROTOCOL, ingressHandler(endpoint, providerOptions), HTTP_HANDLER_OPTIONS);
this.#policyTimer = setInterval(() => void this.syncPolicy().catch(() => {}), this.#policySyncMs);
this.#policyTimer.unref?.();
this.endpoint = endpoint;