@johnhenry/browsermesh-apps 0.7.0 → 0.8.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/README.md CHANGED
@@ -1,7 +1,29 @@
1
1
  # browsermesh-apps
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/%40johnhenry%2Fbrowsermesh-apps.svg)](https://www.npmjs.com/package/@johnhenry/browsermesh-apps)
4
+ [![license](https://img.shields.io/npm/l/%40johnhenry%2Fbrowsermesh-apps.svg)](LICENSE)
5
+
3
6
  Application layer for BrowserMesh: marketplace, chat, payments, compute orchestration, and agent tools.
4
7
 
8
+ ## Contents
9
+
10
+ - [Provenance](#provenance)
11
+ - [Modules](#modules)
12
+ - [GPU compute](#gpu-compute)
13
+ - [Install](#install)
14
+ - [Usage](#usage)
15
+ - [Connecting a real mesh node: `createMeshNode()`](#connecting-a-real-mesh-node-createmeshnode)
16
+ - [TURN server configuration](#turn-server-configuration)
17
+ - [Sharing a `VirtualNetwork` with specific peers: mesh relay](#sharing-a-virtualnetwork-with-specific-peers-mesh-relay)
18
+ - [CloudStorage bucket authorization and key distribution](#cloudstorage-bucket-authorization-and-key-distribution)
19
+ - [CloudStorage: the ergonomic SDK](#cloudstorage-the-ergonomic-sdk)
20
+ - [Putting it all together: sync + kernel-gated mesh + relay on one connection](#putting-it-all-together-sync--kernel-gated-mesh--relay-on-one-connection)
21
+ - [`fetch()`/`WebSocket`-shaped mesh access: `browserMeshFetch` and `BrowserMeshWebSocket`](#fetchwebsocket-shaped-mesh-access-browsermeshfetch-and-browsermeshwebsocket)
22
+ - [`browserMeshFetch`](#browsermeshfetch)
23
+ - [`BrowserMeshWebSocket`](#browsermeshwebsocket)
24
+ - [LLM tool-calling: `BrowserToolRegistry` and `createAgentRuntime`](#llm-tool-calling-browsertoolregistry-and-createagentruntime)
25
+ - [License](#license)
26
+
5
27
  ## Provenance
6
28
 
7
29
  Extracted from the private `clawser` monorepo (previously `packages/browsermesh-apps`), where it was manually published to npm, unscoped, as `browsermesh-apps@0.1.0` (2026-07-17) with no CI ever automating that publish. This is its first release as part of the `@johnhenry/browsermesh` monorepo; the version restarts at `0.0.0` per family convention.
@@ -133,7 +155,7 @@ npm install @johnhenry/browsermesh-apps @johnhenry/browsermesh-primitives @johnh
133
155
  ## Usage
134
156
 
135
157
  ```js
136
- import { MeshChat, AppRegistry, MeshOrchestrator } from 'browsermesh-apps';
158
+ import { MeshChat, AppRegistry, MeshOrchestrator } from '@johnhenry/browsermesh-apps';
137
159
  ```
138
160
 
139
161
  ## Connecting a real mesh node: `createMeshNode()`
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@johnhenry/browsermesh-apps",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Application layer: marketplace, chat, payments, compute, orchestration, and agent tools for BrowserMesh",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
7
7
  "exports": {
8
- ".": "./src/index.mjs",
8
+ ".": {
9
+ "browser": "./src/index.mjs",
10
+ "default": "./src/index.mjs"
11
+ },
9
12
  "./compat": "./src/compat.mjs",
10
13
  "./mesh-rpc": "./src/mesh-rpc.mjs",
11
14
  "./mesh-fetch": "./src/mesh-fetch.mjs",
@@ -25,7 +28,7 @@
25
28
  },
26
29
  "peerDependencies": {
27
30
  "@johnhenry/browsermesh-primitives": ">=0.0.1 <1.0.0",
28
- "@johnhenry/browsermesh-core": ">=0.1.0 <1.0.0",
31
+ "@johnhenry/browsermesh-core": ">=0.5.0 <1.0.0",
29
32
  "@johnhenry/browsermesh-transport": ">=0.1.0 <1.0.0",
30
33
  "@johnhenry/browsermesh-sync": ">=0.0.0 <1.0.0",
31
34
  "@johnhenry/browsermesh-discovery": ">=0.0.1 <1.0.0",
@@ -66,6 +69,6 @@
66
69
  },
67
70
  "homepage": "https://opensource.johnhenry.me/browsermesh/",
68
71
  "engines": {
69
- "node": ">=24.0.0"
72
+ "node": ">=26.0.0"
70
73
  }
71
74
  }
@@ -142,10 +142,11 @@
142
142
  * No browser-only imports at module level.
143
143
  *
144
144
  * `@johnhenry/browsermesh-netway` (an optional peerDependency) is lazily
145
- * resolved via `createRequire(import.meta.url)` -- Node's stable
146
- * synchronous `require()` of an ES module (Node >=22.12/23, well within
147
- * this package's own `engines.node: >=24` floor). `Backend` is used as a
148
- * base class (`class CloudStorageBackend extends Backend`); a class's
145
+ * resolved via `createLazyRequire(import.meta.url)`
146
+ * (`./internal/lazy-node-require.mjs`, using
147
+ * `process.getBuiltinModule('module')` rather than a static
148
+ * `import ... from 'node:module'`, so no Node-only code runs merely from
149
+ * importing this file -- see issue #183). `Backend` is used as a
149
150
  * `extends` clause is evaluated at class-DECLARATION time, and a
150
151
  * `class ... extends ... {}` statement written at module top level always
151
152
  * runs at module-LOAD time, no matter how the `Backend` reference feeding
@@ -167,13 +168,11 @@
167
168
  * full rationale and the sibling `mesh-relay-backend.mjs` fix.
168
169
  */
169
170
 
170
- import { createRequire } from 'node:module'
171
+ import { createLazyRequire } from './internal/lazy-node-require.mjs'
171
172
 
172
173
  import { IndexedDBChunkStore, IndexedDBSyncStorage, TRANSFER_DEFAULTS } from '@johnhenry/browsermesh-sync'
173
174
  import { LWWMap } from '@johnhenry/browsermesh-primitives'
174
175
 
175
- const require = createRequire(import.meta.url)
176
-
177
176
  /** 256KB, matching `MeshFileTransfer`'s existing chunking convention exactly (imported, not duplicated as a literal). */
178
177
  const CHUNK_SIZE = TRANSFER_DEFAULTS.chunkSize
179
178
 
@@ -325,6 +324,7 @@ let _CloudStorageBackendClass = null
325
324
  */
326
325
  function resolveCloudStorageBackendClass() {
327
326
  if (_CloudStorageBackendClass) return _CloudStorageBackendClass
327
+ const require = createLazyRequire(import.meta.url)
328
328
  const { Backend, StreamSocket } = require('@johnhenry/browsermesh-netway')
329
329
 
330
330
  /**
package/src/grant-log.mjs CHANGED
@@ -304,7 +304,7 @@ export class GrantLog {
304
304
  #localPodId
305
305
 
306
306
  /** @type {import('@johnhenry/browsermesh-core').IdentityWallet} Duck-typed:
307
- * only `.sign(podId, data)`, `.verify(pubKeyBytes, data, sig)`,
307
+ * only `.sign(podId, data)`, `.verify(pubKeyBytes, sig, data)`,
308
308
  * `.getPublicKeyBytes(podId)` are used (the real `IdentityWallet`'s
309
309
  * confirmed API, see identity-wallet.mjs / identity.mjs). */
310
310
  #wallet
@@ -550,7 +550,7 @@ export class GrantLog {
550
550
  if (derivedPodId !== signedBy) return false
551
551
 
552
552
  const payload = new TextEncoder().encode(signedPayloadOf({ pubKey, scope, action, at, signedBy }))
553
- return await this.#wallet.verify(rawPubKeyBytes, payload, sigBytes)
553
+ return await this.#wallet.verify(rawPubKeyBytes, sigBytes, payload)
554
554
  } catch {
555
555
  // Malformed base64url, wrong-length keys/signatures, etc. are all
556
556
  // "reject", never a thrown error the caller has to remember to catch.
@@ -0,0 +1,42 @@
1
+ /**
2
+ * A Node-only, browser-safe way to obtain a CommonJS-style `require()`
3
+ * bound to a given module URL -- WITHOUT ever writing
4
+ * `import { createRequire } from 'node:module'` at a file's top level.
5
+ *
6
+ * Why this exists: several files in this package resolve an optional
7
+ * peerDependency lazily via `require()` rather than a dynamic `import()`,
8
+ * because the call site is synchronous (a constructor, or a factory whose
9
+ * validation throw is directly tested with `assert.throws()`) and a
10
+ * dynamic `import()` is inherently async. `createRequire()` itself is
11
+ * fine to call lazily, inside the function that needs it -- the bug this
12
+ * file fixes is that a *static* `import ... from 'node:module'` at a
13
+ * file's top level is unconditionally resolved when that file is loaded,
14
+ * before any function runs. In a browser (no bundler, or a bundler that
15
+ * doesn't polyfill Node builtins) that static import fails immediately,
16
+ * which broke importing this package's root entry at all -- even for
17
+ * callers who never touch the optional-peer code path.
18
+ *
19
+ * `process.getBuiltinModule('module')` (Node >=22.3) returns the same
20
+ * `node:module` export without any `import`/`require` of its own, so
21
+ * referencing it only from inside a function -- guarded by
22
+ * `typeof process !== 'undefined'` -- means nothing Node-only executes,
23
+ * and no unresolvable specifier is ever touched, just from importing a
24
+ * file that uses this helper.
25
+ *
26
+ * @param {string} url - Pass `import.meta.url` from the calling module.
27
+ * @returns {NodeRequire} A `require()` bound to `url`.
28
+ * @throws {Error} If called outside Node.js (e.g. in a browser), but only
29
+ * when a caller actually invokes this -- never merely from importing a
30
+ * module that references it.
31
+ */
32
+ export function createLazyRequire(url) {
33
+ const getBuiltinModule = typeof process !== 'undefined' && process.getBuiltinModule
34
+ if (!getBuiltinModule) {
35
+ throw new Error(
36
+ 'createLazyRequire() requires Node.js (process.getBuiltinModule is unavailable) -- ' +
37
+ 'this code path is not supported in a browser environment.',
38
+ )
39
+ }
40
+ const { createRequire } = getBuiltinModule('module')
41
+ return createRequire(url)
42
+ }
@@ -452,7 +452,7 @@ export function createKeyDistributionService({
452
452
  const payload = new TextEncoder().encode(signedPayloadOf({ resource, ephemeralPublicKey: envelope.ephemeralPublicKey, wrappedKey: envelope.wrappedKey, iv: envelope.iv, at, signedBy }))
453
453
  let validSig = false
454
454
  try {
455
- validSig = await peerNode.wallet.verify(rawPubKeyBytes, payload, sigBytes)
455
+ validSig = await peerNode.wallet.verify(rawPubKeyBytes, sigBytes, payload)
456
456
  } catch {
457
457
  validSig = false
458
458
  }
package/src/mesh-dht.mjs CHANGED
@@ -51,9 +51,11 @@
51
51
  * No browser-only imports at module level.
52
52
  *
53
53
  * `@johnhenry/browsermesh-discovery` (an optional peerDependency) is
54
- * lazily resolved via `createRequire(import.meta.url)` -- Node's stable
55
- * synchronous `require()` of an ES module (Node >=22.12/23, well within
56
- * this package's own `engines.node: >=24` floor), not a dynamic `import()`.
54
+ * lazily resolved via `createLazyRequire(import.meta.url)`
55
+ * (`./internal/lazy-node-require.mjs`, using
56
+ * `process.getBuiltinModule('module')` rather than a static
57
+ * `import ... from 'node:module'`, so no Node-only code runs merely from
58
+ * importing this file -- see issue #183), not a dynamic `import()`.
57
59
  * `createMeshDht()` is synchronous, has a directly-tested synchronous
58
60
  * validation-throw contract (`test/mesh-dht.test.mjs`'s
59
61
  * `assert.throws(...)`), and returns `{strategy, dhtNode, teardown}`
@@ -65,9 +67,7 @@
65
67
  * changes.
66
68
  */
67
69
 
68
- import { createRequire } from 'node:module'
69
-
70
- const require = createRequire(import.meta.url)
70
+ import { createLazyRequire } from './internal/lazy-node-require.mjs'
71
71
 
72
72
  /** Default `type` tag for DHT wire messages multiplexed onto a shared transport. */
73
73
  const DEFAULT_MESSAGE_TYPE = 'dht-relay'
@@ -164,6 +164,7 @@ export function createMeshDht({
164
164
 
165
165
  // Lazy, synchronous (see module doc comment) -- only reached once
166
166
  // validation above has already passed.
167
+ const require = createLazyRequire(import.meta.url)
167
168
  const { DhtDiscoveryStrategy } = require('@johnhenry/browsermesh-discovery')
168
169
 
169
170
  const sendFn = (targetId, msg) => {
@@ -91,34 +91,37 @@
91
91
  * server ever answered, which is exactly what a `mesh-rpc` timeout or
92
92
  * send failure represents.
93
93
  *
94
- * No browser-only imports at module level.
95
- *
94
+ * No browser-only imports at module level, and -- as of the fix for
95
+ * issue #183 -- no Node-only imports at module level either:
96
96
  * `@johnhenry/browsermesh-discovery` (an optional peerDependency) is
97
- * lazily resolved via `createRequire(import.meta.url)` -- Node's stable
98
- * synchronous `require()` of an ES module (Node >=22.12/23, well within
99
- * this package's own `engines.node: >=24` floor) -- rather than a dynamic
100
- * `import()`. This matters because `browserMeshFetch(url, init)` below is
101
- * deliberately NOT async (see "ERROR-VS-REJECT SEMANTICS" above -- it must
102
- * throw a `TypeError` *synchronously* for a malformed URL, matching real
103
- * `fetch()`), and `createBrowserMeshFetch()`'s own synchronous
104
- * meshRpcApi-validation throw is directly tested
105
- * (`test/mesh-fetch.test.mjs`'s `assert.throws(() => createBrowserMeshFetch(...))`).
106
- * A dynamic `import()` is inherently async and would have broken both of
107
- * those; `require()` resolves the module lazily (only when
108
- * `createBrowserMeshFetch()` is actually called, not at this module's own
109
- * load time -- achieving the same "optional peer" goal a dynamic import
110
- * would) while staying fully synchronous, so neither function's signature
111
- * or throw semantics change at all. See the CHANGELOG entry documenting
112
- * this fix across the package for the sibling files that use the same
113
- * technique, and `mesh-websocket.mjs`'s module doc comment for the one
114
- * case (a constructor) where a dynamic `import()` genuinely could not have
115
- * worked even with an async rewrite, which is what led to trying
116
- * `require()` here in the first place.
97
+ * lazily resolved via `createLazyRequire(import.meta.url)`
98
+ * (`./internal/lazy-node-require.mjs`), which itself gets Node's
99
+ * `require()` from `process.getBuiltinModule('module')` rather than a
100
+ * static `import ... from 'node:module'`. This matters for two reasons:
101
+ * first, `browserMeshFetch(url, init)` below is deliberately NOT async
102
+ * (see "ERROR-VS-REJECT SEMANTICS" above -- it must throw a `TypeError`
103
+ * *synchronously* for a malformed URL, matching real `fetch()`), and
104
+ * `createBrowserMeshFetch()`'s own synchronous meshRpcApi-validation throw
105
+ * is directly tested (`test/mesh-fetch.test.mjs`'s
106
+ * `assert.throws(() => createBrowserMeshFetch(...))`) -- a dynamic
107
+ * `import()` is inherently async and would have broken both of those.
108
+ * Second, and the reason it's `createLazyRequire()` and not a top-level
109
+ * `createRequire(import.meta.url)`: a *static* `import` of `node:module`
110
+ * at this file's top level would fail immediately when this file is
111
+ * loaded in a browser, even for callers who never call
112
+ * `createBrowserMeshFetch()` at all -- since this file is re-exported from
113
+ * the package root. `createLazyRequire()` resolves the module lazily
114
+ * (only when `createBrowserMeshFetch()` is actually called, not at this
115
+ * module's own load time) while staying fully synchronous, so neither
116
+ * function's signature or throw semantics change at all. See the
117
+ * CHANGELOG entry documenting this fix across the package for the sibling
118
+ * files that use the same technique, and `mesh-websocket.mjs`'s module doc
119
+ * comment for the one case (a constructor) where a dynamic `import()`
120
+ * genuinely could not have worked even with an async rewrite, which is
121
+ * what led to trying `require()` here in the first place.
117
122
  */
118
123
 
119
- import { createRequire } from 'node:module'
120
-
121
- const require = createRequire(import.meta.url)
124
+ import { createLazyRequire } from './internal/lazy-node-require.mjs'
122
125
 
123
126
  /**
124
127
  * @typedef {object} MeshRpcApi
@@ -148,6 +151,7 @@ export function createBrowserMeshFetch(meshRpcApi) {
148
151
  // Lazy, synchronous (see module doc comment) -- only resolved once a
149
152
  // caller actually builds a browserMeshFetch, and cached by require()'s
150
153
  // own module cache for every subsequent call.
154
+ const require = createLazyRequire(import.meta.url)
151
155
  const { parseMeshRequest } = require('@johnhenry/browsermesh-discovery')
152
156
 
153
157
  /**
@@ -25,9 +25,11 @@
25
25
  * No browser-only imports at module level.
26
26
  *
27
27
  * `@johnhenry/browsermesh-netway` (an optional peerDependency) is lazily
28
- * resolved via `createRequire(import.meta.url)` -- Node's stable
29
- * synchronous `require()` of an ES module (Node >=22.12/23, well within
30
- * this package's own `engines.node: >=24` floor). `Backend` is used as a
28
+ * resolved via `createLazyRequire(import.meta.url)`
29
+ * (`./internal/lazy-node-require.mjs`, using
30
+ * `process.getBuiltinModule('module')` rather than a static
31
+ * `import ... from 'node:module'`, so no Node-only code runs merely from
32
+ * importing this file -- see issue #183). `Backend` is used as a
31
33
  * base class (`class MeshRelayBackend extends Backend`); a class's
32
34
  * `extends` clause is evaluated at class-DECLARATION time, and a
33
35
  * `class ... extends ... {}` statement written at module top level always
@@ -48,9 +50,7 @@
48
50
  * rationale and the sibling `cloud-storage-backend.mjs` fix.
49
51
  */
50
52
 
51
- import { createRequire } from 'node:module'
52
-
53
- const require = createRequire(import.meta.url)
53
+ import { createLazyRequire } from './internal/lazy-node-require.mjs'
54
54
 
55
55
  /** Default `envelope.type` used to route relay payloads on the shared `onIncomingData()` bus. */
56
56
  const DEFAULT_ENVELOPE_TYPE = 'mesh-relay'
@@ -98,6 +98,7 @@ let _MeshRelayBackendClass = null
98
98
  */
99
99
  function resolveMeshRelayBackendClass() {
100
100
  if (_MeshRelayBackendClass) return _MeshRelayBackendClass
101
+ const require = createLazyRequire(import.meta.url)
101
102
  const { Backend, StreamSocket, ConnectionRefusedError } = require('@johnhenry/browsermesh-netway')
102
103
 
103
104
  /**
@@ -206,9 +206,11 @@
206
206
  * No browser-only imports at module level.
207
207
  *
208
208
  * `@johnhenry/browsermesh-discovery` (an optional peerDependency) is
209
- * lazily resolved via `createRequire(import.meta.url)` -- Node's stable
210
- * synchronous `require()` of an ES module (Node >=22.12/23, well within
211
- * this package's own `engines.node: >=24` floor), not a dynamic `import()`.
209
+ * lazily resolved via `createLazyRequire(import.meta.url)`
210
+ * (`./internal/lazy-node-require.mjs`, using
211
+ * `process.getBuiltinModule('module')` rather than a static
212
+ * `import ... from 'node:module'`, so no Node-only code runs merely from
213
+ * importing this file -- see issue #183), not a dynamic `import()`.
212
214
  * `SwimMembership`/`SwarmCoordinator` are both constructed synchronously
213
215
  * inside this service's `attach()`, whose synchronous-return contract is a
214
216
  * hard, repo-wide convention (`mesh-service.mjs`'s `attachService()`,
@@ -219,9 +221,7 @@
219
221
  * `attach()`'s contract doesn't change.
220
222
  */
221
223
 
222
- import { createRequire } from 'node:module'
223
-
224
- const require = createRequire(import.meta.url)
224
+ import { createLazyRequire } from './internal/lazy-node-require.mjs'
225
225
 
226
226
  const DEFAULT_SWIM_ENVELOPE_TYPE = 'swarm-swim'
227
227
  const DEFAULT_HEARTBEAT_ENVELOPE_TYPE = 'swarm-heartbeat'
@@ -288,6 +288,7 @@ export function createSwarmService(opts = {}) {
288
288
  const localPodId = peerNode.podId
289
289
 
290
290
  // Lazy, synchronous (see module doc comment).
291
+ const require = createLazyRequire(import.meta.url)
291
292
  const {
292
293
  SwarmCoordinator,
293
294
  SwimMembership,
@@ -204,29 +204,31 @@
204
204
  * reason}` -- an accepted session transitioned to `CLOSED`, from either
205
205
  * side (this peer's own `close()` or a received `ws-close`).
206
206
  *
207
- * No browser-only imports at module level.
207
+ * No browser-only imports at module level, and -- as of the fix for
208
+ * issue #183 -- no Node-only imports at module level either.
208
209
  *
209
210
  * `@johnhenry/browsermesh-discovery` (an optional peerDependency) is
210
- * lazily resolved via `createRequire(import.meta.url)` -- Node's stable
211
- * synchronous `require()` of an ES module (Node >=22.12/23, well within
212
- * this package's own `engines.node: >=24` floor). This is the file that
213
- * motivated trying `require()` at all: `parseMeshRequest()` is used inside
214
- * `BrowserMeshWebSocket`'s *constructor*, which can never be `async` --
215
- * `import()` was a dead end here (an async factory replacing direct `new
216
- * BrowserMeshWebSocket(...)` construction would have been a real,
217
- * unwanted breaking change to this class's whole reason for existing: matching
218
- * real `new WebSocket(url)` construction ergonomics as closely as
219
- * possible, per this file's own "API SHAPE DECISION" precedent in
220
- * `mesh-fetch.mjs`). `require()` resolves synchronously, so it works
211
+ * lazily resolved via `createLazyRequire(import.meta.url)`
212
+ * (`./internal/lazy-node-require.mjs`), which gets Node's `require()`
213
+ * from `process.getBuiltinModule('module')` rather than a static
214
+ * `import ... from 'node:module'` -- see that file's doc comment for why
215
+ * the static-vs-lazy distinction matters for browser safety. This is the
216
+ * file that motivated trying `require()` at all: `parseMeshRequest()` is
217
+ * used inside `BrowserMeshWebSocket`'s *constructor*, which can never be
218
+ * `async` -- `import()` was a dead end here (an async factory replacing
219
+ * direct `new BrowserMeshWebSocket(...)` construction would have been a
220
+ * real, unwanted breaking change to this class's whole reason for
221
+ * existing: matching real `new WebSocket(url)` construction ergonomics as
222
+ * closely as possible, per this file's own "API SHAPE DECISION" precedent
223
+ * in `mesh-fetch.mjs`). `require()` resolves synchronously, so it works
221
224
  * directly inside the constructor with zero API changes: the module is
222
225
  * only actually loaded the first time a `BrowserMeshWebSocket` is
223
226
  * constructed (or `createMeshWebSocketService()`'s `api.connect()` is
224
- * called), not at this module's own load time.
227
+ * called), not at this module's own load time -- and, unlike before,
228
+ * `process.getBuiltinModule` is not even referenced until then either.
225
229
  */
226
230
 
227
- import { createRequire } from 'node:module'
228
-
229
- const require = createRequire(import.meta.url)
231
+ import { createLazyRequire } from './internal/lazy-node-require.mjs'
230
232
 
231
233
  /** Default `envelope.type` used to route mesh-websocket payloads on the shared `onIncomingData()` bus. */
232
234
  const DEFAULT_ENVELOPE_TYPE = 'mesh-websocket'
@@ -376,6 +378,7 @@ export class BrowserMeshWebSocket {
376
378
  }
377
379
  // Lazy, synchronous (see module doc comment) -- cached by require()'s
378
380
  // own module cache after the first construction.
381
+ const require = createLazyRequire(import.meta.url)
379
382
  const { parseMeshRequest } = require('@johnhenry/browsermesh-discovery')
380
383
  const parsed = parseMeshRequest(url)
381
384
  if (!parsed) {
package/src/payments.mjs CHANGED
@@ -63,7 +63,7 @@ function generateEscrowId() {
63
63
  // in without this published, general-purpose package taking a hard
64
64
  // dependency on it. Shape matches peer-chat.mjs's convention:
65
65
  // signFn: async (data: Uint8Array) => Uint8Array
66
- // verifyFn: async (pubKey: Uint8Array, data: Uint8Array, sig: Uint8Array) => boolean
66
+ // verifyFn: async (pubKey: Uint8Array, sig: Uint8Array, data: Uint8Array) => boolean
67
67
 
68
68
  /**
69
69
  * Encode a Uint8Array to a base64 string.
@@ -436,7 +436,7 @@ export class PaymentChannel {
436
436
  * @param {Function} [opts.signFn] - async (data: Uint8Array) => Uint8Array.
437
437
  * When omitted, PaymentUpdate/close signatures stay `null` and close()
438
438
  * stays unilateral -- fully backward compatible.
439
- * @param {Function} [opts.verifyFn] - async (pubKey, data, sig) => boolean
439
+ * @param {Function} [opts.verifyFn] - async (pubKey, sig, data) => boolean
440
440
  * @param {Uint8Array} [opts.remotePublicKey] - Remote pod's raw public key,
441
441
  * required alongside verifyFn to verify incoming updates/claims.
442
442
  * @param {string} [opts.channelId] - Use this exact channel ID instead of
@@ -682,7 +682,7 @@ export class PaymentChannel {
682
682
  try {
683
683
  const data = canonicalBytes(fields);
684
684
  const sigBytes = base64ToBytes(signatureB64);
685
- return await this.#verifyFn(this.#remotePublicKey, data, sigBytes);
685
+ return await this.#verifyFn(this.#remotePublicKey, sigBytes, data);
686
686
  } catch {
687
687
  return false;
688
688
  }
package/src/peer-chat.mjs CHANGED
@@ -195,7 +195,7 @@ export class PeerChat {
195
195
  * an envelope -- the caller, typically `createChatService()`, adds
196
196
  * whatever envelope shape its own transport needs).
197
197
  * @param {Function} [opts.signFn] - async (data: Uint8Array) => Uint8Array
198
- * @param {Function} [opts.verifyFn] - async (fromPubKey, data, sig) => boolean
198
+ * @param {Function} [opts.verifyFn] - async (fromPubKey, sig, data) => boolean
199
199
  * @param {number} [opts.maxHistory=1000] - Maximum messages to retain
200
200
  * @param {Function} [opts.autoResponder] - async (message) => string|null
201
201
  * @param {Function} [opts.onLog] - Logging callback
@@ -349,7 +349,7 @@ export class PeerChat {
349
349
  timestamp: message.timestamp,
350
350
  }))
351
351
  const sigBytes = base64ToBytes(message.signature)
352
- message.verified = await this.#verifyFn(fromPubKey, data, sigBytes)
352
+ message.verified = await this.#verifyFn(fromPubKey, sigBytes, data)
353
353
  } catch (err) {
354
354
  this.#onLog(1, `Signature verification failed: ${err.message}`)
355
355
  message.verified = false
@@ -498,7 +498,7 @@ export class PeerChat {
498
498
  * @param {object} [opts]
499
499
  * @param {Function} [opts.signFn] - async (data: Uint8Array) => Uint8Array.
500
500
  * Passed straight through to `PeerChat`; omit to send unsigned messages.
501
- * @param {Function} [opts.verifyFn] - async (fromPubKey, data, sig) => boolean.
501
+ * @param {Function} [opts.verifyFn] - async (fromPubKey, sig, data) => boolean.
502
502
  * Passed straight through to `PeerChat`; called with each message's real
503
503
  * sender, not a fixed key -- see module doc comment's point 2.
504
504
  * @param {number} [opts.maxHistory=1000]
@@ -35,9 +35,11 @@
35
35
  * @module serverless-fetch
36
36
  *
37
37
  * `@johnhenry/browsermesh-discovery` (an optional peerDependency) is
38
- * lazily resolved via `createRequire(import.meta.url)` -- Node's stable
39
- * synchronous `require()` of an ES module (Node >=22.12/23, well within
40
- * this package's own `engines.node: >=24` floor), not a dynamic `import()`.
38
+ * lazily resolved via `createLazyRequire(import.meta.url)`
39
+ * (`./internal/lazy-node-require.mjs`, itself using
40
+ * `process.getBuiltinModule('module')` rather than a static
41
+ * `import ... from 'node:module'`, so no Node-only code runs merely from
42
+ * importing this file -- see issue #183), not a dynamic `import()`.
41
43
  * `createServerlessFetchRouter()` is synchronous, has a directly-tested
42
44
  * synchronous validation-throw contract (`test/serverless-fetch.test.mjs`'s
43
45
  * `assert.throws(...)`), and returns a real `MeshFetchRouter` instance
@@ -49,12 +51,10 @@
49
51
  * changes.
50
52
  */
51
53
 
52
- import { createRequire } from 'node:module'
54
+ import { createLazyRequire } from './internal/lazy-node-require.mjs'
53
55
 
54
56
  import { decodeWireResponse } from './serverless-wire.mjs'
55
57
 
56
- const require = createRequire(import.meta.url)
57
-
58
58
  /**
59
59
  * @param {{request: (podId: string, req: {method?: string, path?: string, headers?: object, body?: *}) => Promise<{status: number, headers: object, body: *}>}} meshRpcApi
60
60
  * The `.api` returned by attaching a `mesh-rpc.mjs`-based service bound to
@@ -77,6 +77,7 @@ export function createServerlessFetchRouter(meshRpcApi, { resolveSite } = {}) {
77
77
  }
78
78
 
79
79
  // Lazy, synchronous (see module doc comment).
80
+ const require = createLazyRequire(import.meta.url)
80
81
  const { MeshFetchRouter } = require('@johnhenry/browsermesh-discovery')
81
82
 
82
83
  return new MeshFetchRouter({