@net-mesh/browser 0.39.0 → 0.41.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.
@@ -57,7 +57,12 @@ export declare const MAX_OUTSTANDING = 64;
57
57
  export interface JoinStoreOptions<S extends object, A extends ActionSpec, I extends InputSpec> {
58
58
  readonly definition: StoreDefinition<S, A, I>;
59
59
  readonly transport: StoreTransport;
60
- /** The node hosting the store. */
60
+ /**
61
+ * The node hosting the store: 16 hex digits, as `nodeIdHex()` and a
62
+ * descriptor's `peerIdHex` print it. The decimal `peerNode` of an
63
+ * event is accepted too, unless it is exactly sixteen digits long —
64
+ * that is the hex spelling, and it is read as hex.
65
+ */
61
66
  readonly host: string;
62
67
  /** The audience this caller wants. */
63
68
  readonly audience: readonly string[];
@@ -23,7 +23,7 @@
23
23
  */
24
24
  import { StoreCore } from './core.js';
25
25
  import { isStaleStream, StoreError } from './errors.js';
26
- import { peerHexOf, samePeer } from './host.js';
26
+ import { nodeHexOf, peerHexOf } from './host.js';
27
27
  import { StoreReplica } from './replica.js';
28
28
  import { decodeMessage, encodeMessage, utf8Length } from './wire.js';
29
29
  /** How often a live handle renews its lease (§1.6). */
@@ -83,7 +83,10 @@ export function joinStore(options) {
83
83
  // had already accepted the subscription. Refused here instead,
84
84
  // where the caller can read what it did wrong.
85
85
  const selfId = options.transport.nodeIdHex();
86
- if (selfId !== null && samePeer(selfId, options.host)) {
86
+ // Read ONCE, as an id the caller holds: hex when it is the sixteen
87
+ // digits `nodeIdHex()` prints, even when none of them is a letter.
88
+ const hostHex = nodeHexOf(options.host);
89
+ if (selfId !== null && hostHex !== null && nodeHexOf(selfId) === hostHex) {
87
90
  throw new StoreError('invalid-data', 'a replica cannot join the node it runs on: hold the hostStore handle instead');
88
91
  }
89
92
  const streamId = options.streamId ?? `store/${options.definition.id}`;
@@ -124,8 +127,7 @@ export function joinStore(options) {
124
127
  return upstream;
125
128
  if (opening !== null)
126
129
  return opening;
127
- const addressable = peerHexOf(options.host);
128
- if (addressable === null) {
130
+ if (hostHex === null) {
129
131
  return Promise.reject(new StoreError('invalid-data', `the host id is not a node id: ${options.host}`));
130
132
  }
131
133
  // `label`, never `streamId`: see {@link StoreTransport.openStream}.
@@ -139,11 +141,11 @@ export function joinStore(options) {
139
141
  // the relay is installed before the direct half is even tried, so
140
142
  // the open below is the thing that decides, and its refusal is
141
143
  // typed.
142
- opening = Promise.resolve(options.transport.connectPeer?.(addressable))
144
+ opening = Promise.resolve(options.transport.connectPeer?.(hostHex))
143
145
  .catch(() => undefined)
144
146
  .then(() => options.transport.openStream({
145
147
  reliability: 'reliable',
146
- peer: addressable,
148
+ peer: hostHex,
147
149
  label: streamId,
148
150
  }))
149
151
  .then(open => {
@@ -350,8 +352,12 @@ export function joinStore(options) {
350
352
  // is what identifies this store's traffic.
351
353
  // The host's identity, compared as a NUMBER: the event carries an
352
354
  // exact decimal and `options.host` is hex, so `!==` on the strings
353
- // rejected every frame the host ever sent.
354
- if (typeof event.peerNode !== 'string' || !samePeer(event.peerNode, options.host))
355
+ // rejected every frame the host ever sent. Each side is read in
356
+ // its OWN spelling: one reader for both took an all-digit hex host
357
+ // for a decimal, and then no frame of that host's ever matched.
358
+ if (typeof event.peerNode !== 'string' || hostHex === null)
359
+ return;
360
+ if (peerHexOf(event.peerNode) !== hostHex)
355
361
  return;
356
362
  const payload = event.payload;
357
363
  if (payload === undefined)
@@ -14,7 +14,7 @@
14
14
  * handoffs.handoff('ship-7', 'r:4:8'); // when the ship crosses the border
15
15
  * ```
16
16
  */
17
- import { peerHexOf } from '../store/host.js';
17
+ import { nodeHexOf, peerHexOf } from '../store/host.js';
18
18
  import { ghostTargets, onForwardedAction, pruneActs, } from './border.js';
19
19
  import { beginHandoff, locate, onHandoffOffer, onHandoffReply, pruneHandled, reofferHandoff, retryHandoffs, } from './handoff.js';
20
20
  const encoder = new TextEncoder();
@@ -140,7 +140,7 @@ export function handoffLink(options) {
140
140
  // handoff on another region's behalf.
141
141
  const sender = typeof event.peerNode === 'string' ? peerHexOf(event.peerNode) : null;
142
142
  const expected = options.peerOf(body.from);
143
- if (sender === null || expected === null || peerHexOf(expected) !== sender) {
143
+ if (sender === null || expected === null || nodeHexOf(expected) !== sender) {
144
144
  drop('unauthenticated');
145
145
  refresh(body.from);
146
146
  return;
@@ -153,7 +153,7 @@ export function handoffLink(options) {
153
153
  if (closed)
154
154
  return;
155
155
  const peer = options.peerOf(message.to);
156
- const hex = peer === null ? null : peerHexOf(peer);
156
+ const hex = peer === null ? null : nodeHexOf(peer);
157
157
  if (hex === null) {
158
158
  drop('unknown-region');
159
159
  refresh(message.to);
@@ -27,7 +27,7 @@
27
27
  * directory (`handoffLink({ peerOf: directory.peerOf })`) needs the list:
28
28
  * otherwise a node that announces a region could also send offers as it.
29
29
  */
30
- import { peerHexOf } from '../store/host.js';
30
+ import { nodeHexOf } from '../store/host.js';
31
31
  import { joinStore } from '../store/join.js';
32
32
  /** Region `r:<rx>:<rz>` for a position, with regions `size` units square. */
33
33
  export function regionOf(x, z, size) {
@@ -77,7 +77,7 @@ export function announceRegions(node, world, regions, options = {}) {
77
77
  export function regionDirectory(options) {
78
78
  const trusted = options.trustedHosts === undefined
79
79
  ? null
80
- : new Set(options.trustedHosts.map(id => peerHexOf(id)).filter((id) => id !== null));
80
+ : new Set(options.trustedHosts.map(id => nodeHexOf(id)).filter((id) => id !== null));
81
81
  const cache = new Map();
82
82
  return {
83
83
  async lookup(region) {
@@ -90,7 +90,7 @@ export function regionDirectory(options) {
90
90
  }
91
91
  const hosts = [
92
92
  ...new Set(found
93
- .map(descriptor => peerHexOf(descriptor.peerIdHex))
93
+ .map(descriptor => nodeHexOf(descriptor.peerIdHex))
94
94
  .filter((id) => id !== null && (trusted === null || trusted.has(id)))),
95
95
  ].sort();
96
96
  const answer = hosts.length === 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@net-mesh/browser",
3
- "version": "0.39.0",
3
+ "version": "0.41.0",
4
4
  "description": "Browser-native Net mesh leaf \u2014 idiomatic TypeScript over the net-mesh-leaf wasm node",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",