@johnhenry/browsermesh-apps 0.14.0 → 0.15.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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/peer-node.mjs +20 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnhenry/browsermesh-apps",
3
- "version": "0.14.0",
3
+ "version": "0.15.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",
@@ -59,7 +59,7 @@
59
59
  },
60
60
  "devDependencies": {
61
61
  "@johnhenry/andbox": ">=0.0.1 <1.0.0",
62
- "@johnhenry/browsermesh-pod": "^0.1.0",
62
+ "@johnhenry/browsermesh-pod": "^0.1.1",
63
63
  "node-datachannel": "^0.33.4",
64
64
  "fake-indexeddb": "^6.2.5",
65
65
  "webgpu": "^0.6.1"
package/src/peer-node.mjs CHANGED
@@ -39,6 +39,11 @@ const DEFAULT_CONNECTION_ID = 'default'
39
39
  // PeerNode
40
40
  // ---------------------------------------------------------------------------
41
41
 
42
+ /** A syntactically valid named-channel name (see WebRTCPeerConnection#openChannel). */
43
+ function isChannelName(v) {
44
+ return typeof v === 'string' && /^[A-Za-z0-9._-]{1,64}$/.test(v);
45
+ }
46
+
42
47
  /**
43
48
  * Top-level orchestrator for a mesh peer. Manages the full lifecycle
44
49
  * from identity bootstrap through discovery, connection, and shutdown.
@@ -553,8 +558,10 @@ export class PeerNode {
553
558
  * @param {object} [opts]
554
559
  * @param {string} [opts.connectionId] - Target this specific connection's
555
560
  * session (issue #116) rather than "most recently created".
556
- * @param {'control'|'bulk'} [opts.channel] - Which of the transport's data
557
- * channels to send on. Omit (or pass `'control'`) for the ordered control
561
+ * @param {string} [opts.channel] - Which of the transport's data
562
+ * channels to send on. Besides 'control' and 'bulk' this may be the name of
563
+ * a channel opened with `transport.openChannel(name)` (#115); a name the
564
+ * session's transport has not opened throws a `TypeError`. Omit (or pass `'control'`) for the ordered control
558
565
  * lane; `'bulk'` selects the second, unordered `mesh-bulk` channel, so
559
566
  * large payloads (file/torrent chunks) cannot queue in front of control
560
567
  * traffic. Passed through as `transport.send(data, { channel })` only
@@ -570,8 +577,8 @@ export class PeerNode {
570
577
  */
571
578
  async sendTo(pubKey, data, { connectionId, channel } = {}) {
572
579
  this.#ensureRunning('sendTo');
573
- if (channel !== undefined && channel !== 'control' && channel !== 'bulk') {
574
- throw new TypeError(`PeerNode.sendTo: unknown channel ${JSON.stringify(channel)} (expected 'control' or 'bulk')`);
580
+ if (channel !== undefined && channel !== 'control' && channel !== 'bulk' && !isChannelName(channel)) {
581
+ throw new TypeError(`PeerNode.sendTo: unknown channel ${JSON.stringify(channel)} (expected 'control', 'bulk' or a named channel)`);
575
582
  }
576
583
  let session = null;
577
584
  for (const [, s] of this.#sessions) {
@@ -586,6 +593,13 @@ export class PeerNode {
586
593
  if (!session.transportInstance || typeof session.transportInstance.send !== 'function') {
587
594
  throw new Error(`PeerNode.sendTo: session for ${pubKey} has no transport.send`);
588
595
  }
596
+ if (channel !== undefined && channel !== 'control' && channel !== 'bulk') {
597
+ // A named channel (#115) must have been opened on this session's transport.
598
+ const open = session.transportInstance.channels;
599
+ if (!Array.isArray(open) || !open.includes(channel)) {
600
+ throw new TypeError(`PeerNode.sendTo: unknown channel ${JSON.stringify(channel)} for ${pubKey} (open it with transport.openChannel())`);
601
+ }
602
+ }
589
603
  return channel === undefined
590
604
  ? session.transportInstance.send(data)
591
605
  : session.transportInstance.send(data, { channel });
@@ -624,8 +638,8 @@ export class PeerNode {
624
638
  */
625
639
  async broadcast(data, { channel, exclude, concurrency = 8 } = {}) {
626
640
  this.#ensureRunning('broadcast');
627
- if (channel !== undefined && channel !== 'control' && channel !== 'bulk') {
628
- throw new TypeError(`PeerNode.broadcast: unknown channel ${JSON.stringify(channel)} (expected 'control' or 'bulk')`);
641
+ if (channel !== undefined && channel !== 'control' && channel !== 'bulk' && !isChannelName(channel)) {
642
+ throw new TypeError(`PeerNode.broadcast: unknown channel ${JSON.stringify(channel)} (expected 'control', 'bulk' or a named channel)`);
629
643
  }
630
644
  const skip = typeof exclude === 'function'
631
645
  ? exclude