@relai-team/access-sdk 0.20.0 → 0.20.1

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
@@ -536,6 +536,8 @@ import { RelaiClient, StorageTelemetryQueue } from "@relai-team/access-sdk";
536
536
  const client = new RelaiClient({
537
537
  publishableKey,
538
538
  telemetryQueue: new StorageTelemetryQueue(AsyncStorage),
539
+ // Same store, different job: see below.
540
+ handleStorage: AsyncStorage,
539
541
  });
540
542
 
541
543
  // e.g. on AppState 'active' or a NetInfo reconnect:
@@ -545,6 +547,27 @@ await client.flushTelemetry();
545
547
  The queue caps at 500 items (oldest dropped) and self-heals from a corrupt
546
548
  blob. Telemetry never blocks or fails an unlock.
547
549
 
550
+ ### `handleStorage`: don't rescan on every open
551
+
552
+ `handleStorage` takes the same `getItem/setItem/removeItem` store and answers a
553
+ different question: which local Bluetooth handle turned out to be which Parent.
554
+
555
+ A grant names its Parent with `parent_ble_id`, which is the address recorded when
556
+ that Parent was provisioned. On some platforms that string is a connection
557
+ handle and the SDK connects straight to it. On others it is not: the OS issues
558
+ its own identifiers, per install, that exist only after a scan. There the SDK
559
+ scans and finds the right Parent by trying, since a command is signed with that
560
+ Parent's key and any other one refuses it.
561
+
562
+ Give it a store and a resolved handle is remembered, so the next open at that
563
+ tower connects once and never scans. Omit it and every open rescans, which is a
564
+ few seconds each time and no correctness problem. The mapping is per device,
565
+ because the identifier is: another phone resolving the same Parent gets a
566
+ different one.
567
+
568
+ Set it on the client, once. `relai.unlock({ ..., handleStorage })` overrides it
569
+ for a single call if you ever need a different store.
570
+
548
571
  ## The contract this package is checked against
549
572
 
550
573
  The platform publishes its OpenAPI 3.1 description at
package/dist/index.cjs CHANGED
@@ -1310,6 +1310,7 @@ var NOTIFY_TIMEOUT_MS = 8e3;
1310
1310
  var BleTransport = class {
1311
1311
  constructor(opts) {
1312
1312
  this.devices = /* @__PURE__ */ new Map();
1313
+ this.notify = /* @__PURE__ */ new Map();
1313
1314
  this.manager = opts.manager;
1314
1315
  this.scanImpl = opts.scan;
1315
1316
  this.notifyTimeoutMs = opts.notifyTimeoutMs ?? NOTIFY_TIMEOUT_MS;
@@ -1384,6 +1385,42 @@ var BleTransport = class {
1384
1385
  } catch {
1385
1386
  }
1386
1387
  this.devices.set(device.id, device);
1388
+ const state = {
1389
+ waiter: void 0,
1390
+ accept: void 0,
1391
+ failed: void 0,
1392
+ error: void 0,
1393
+ remove: () => void 0
1394
+ };
1395
+ const sub = device.monitorCharacteristicForService(
1396
+ RELAI_UART_SERVICE,
1397
+ RELAI_TX_CHAR,
1398
+ (error, ch) => {
1399
+ if (error) {
1400
+ const err = new exports.RelaiTransportError(`BLE notify error: ${String(error)}`);
1401
+ state.error = err;
1402
+ state.failed?.(err);
1403
+ return;
1404
+ }
1405
+ const value = ch?.value;
1406
+ if (!value) return;
1407
+ let text;
1408
+ try {
1409
+ text = base64ToUtf8(value);
1410
+ } catch {
1411
+ return;
1412
+ }
1413
+ if (state.waiter && state.accept?.(parseNotifyLine(text))) {
1414
+ const resolve = state.waiter;
1415
+ state.waiter = void 0;
1416
+ state.accept = void 0;
1417
+ state.failed = void 0;
1418
+ resolve(text);
1419
+ }
1420
+ }
1421
+ );
1422
+ state.remove = () => sub.remove();
1423
+ this.notify.set(device.id, state);
1387
1424
  return { id: device.id, parentBleId, connectedAt: (/* @__PURE__ */ new Date()).toISOString() };
1388
1425
  }
1389
1426
  async requestNonce(session) {
@@ -1409,6 +1446,14 @@ var BleTransport = class {
1409
1446
  return Promise.resolve({});
1410
1447
  }
1411
1448
  async disconnect(session) {
1449
+ const state = this.notify.get(session.id);
1450
+ if (state) {
1451
+ try {
1452
+ state.remove();
1453
+ } catch {
1454
+ }
1455
+ this.notify.delete(session.id);
1456
+ }
1412
1457
  const device = this.devices.get(session.id);
1413
1458
  if (device) {
1414
1459
  await device.cancelConnection().catch(() => void 0);
@@ -1416,51 +1461,53 @@ var BleTransport = class {
1416
1461
  }
1417
1462
  }
1418
1463
  /**
1419
- * Write one value and await the first notify line that matches `accept` (the
1420
- * default accepts the first non-passthrough line). Ordinary command output is
1421
- * skipped via the passthrough filter so we lock onto the structured line.
1464
+ * Write one value and await the first notify line that matches `accept`.
1465
+ *
1466
+ * CLEAR, WRITE, WAIT, in that order, against the session's buffer.
1467
+ *
1468
+ * Clearing first is not tidiness. The nonce is single-use and the Parent
1469
+ * burns it: a `NONCE` left over from an earlier `CHALLENGE` would be returned
1470
+ * to the caller, minted against by the platform, and then rejected by the
1471
+ * Parent as `NONCE`, which reads as a replay rather than as stale state.
1472
+ *
1473
+ * Waiting on the buffer rather than on a fresh subscription means a reply
1474
+ * that lands before the waiter is registered is still found, and the window
1475
+ * between the nonce leg and the command leg is covered: there is no moment in
1476
+ * the session when nobody is listening.
1422
1477
  */
1423
1478
  writeAndAwait(session, valueBase64, accept = (p) => p.kind !== "passthrough") {
1424
1479
  const device = this.devices.get(session.id);
1425
- if (!device) return Promise.reject(new exports.RelaiTransportError("Not connected."));
1480
+ const state = this.notify.get(session.id);
1481
+ if (!device || !state) return Promise.reject(new exports.RelaiTransportError("Not connected."));
1482
+ if (state.error) return Promise.reject(state.error);
1483
+ if (state.waiter) {
1484
+ return Promise.reject(
1485
+ new exports.RelaiTransportError("A command is already in flight on this Parent session.")
1486
+ );
1487
+ }
1426
1488
  return new Promise((resolve, reject) => {
1427
1489
  let settled = false;
1428
- const sub = device.monitorCharacteristicForService(RELAI_UART_SERVICE, RELAI_TX_CHAR, (error, ch) => {
1429
- if (settled) return;
1430
- if (error) {
1431
- settled = true;
1432
- clearTimeout(timer);
1433
- sub.remove();
1434
- reject(new exports.RelaiTransportError(`BLE notify error: ${String(error)}`));
1435
- return;
1436
- }
1437
- const value = ch?.value;
1438
- if (!value) return;
1439
- let text;
1440
- try {
1441
- text = base64ToUtf8(value);
1442
- } catch {
1443
- return;
1444
- }
1445
- if (accept(parseNotifyLine(text))) {
1446
- settled = true;
1447
- clearTimeout(timer);
1448
- sub.remove();
1449
- resolve(text);
1450
- }
1451
- });
1452
- const timer = setTimeout(() => {
1453
- if (settled) return;
1454
- settled = true;
1455
- sub.remove();
1456
- reject(new exports.RelaiTransportError("Timed out waiting for the Parent's notify response."));
1457
- }, this.notifyTimeoutMs);
1458
- device.writeCharacteristicWithResponseForService(RELAI_UART_SERVICE, RELAI_RX_CHAR, valueBase64).catch((err) => {
1490
+ const finish = (fn) => {
1459
1491
  if (settled) return;
1460
1492
  settled = true;
1461
1493
  clearTimeout(timer);
1462
- sub.remove();
1463
- reject(new exports.RelaiTransportError(`BLE write failed: ${String(err)}`));
1494
+ state.waiter = void 0;
1495
+ state.accept = void 0;
1496
+ state.failed = void 0;
1497
+ fn();
1498
+ };
1499
+ state.accept = accept;
1500
+ state.waiter = (line) => finish(() => resolve(line));
1501
+ state.failed = (err) => finish(() => reject(err));
1502
+ const timer = setTimeout(() => {
1503
+ finish(
1504
+ () => reject(new exports.RelaiTransportError("Timed out waiting for the Parent's notify response."))
1505
+ );
1506
+ }, this.notifyTimeoutMs);
1507
+ device.writeCharacteristicWithResponseForService(RELAI_UART_SERVICE, RELAI_RX_CHAR, valueBase64).then(() => {
1508
+ if (state.error) finish(() => reject(state.error));
1509
+ }).catch((err) => {
1510
+ finish(() => reject(new exports.RelaiTransportError(`BLE write failed: ${String(err)}`)));
1464
1511
  });
1465
1512
  });
1466
1513
  }
package/dist/index.d.cts CHANGED
@@ -1415,6 +1415,7 @@ interface BleTransportOptions {
1415
1415
  declare class BleTransport implements ParentTransport {
1416
1416
  private readonly manager;
1417
1417
  private readonly devices;
1418
+ private readonly notify;
1418
1419
  private readonly scanImpl;
1419
1420
  private readonly notifyTimeoutMs;
1420
1421
  constructor(opts: BleTransportOptions);
@@ -1438,9 +1439,19 @@ declare class BleTransport implements ParentTransport {
1438
1439
  readTelemetry(_session: Session): Promise<TowerTelemetry>;
1439
1440
  disconnect(session: Session): Promise<void>;
1440
1441
  /**
1441
- * Write one value and await the first notify line that matches `accept` (the
1442
- * default accepts the first non-passthrough line). Ordinary command output is
1443
- * skipped via the passthrough filter so we lock onto the structured line.
1442
+ * Write one value and await the first notify line that matches `accept`.
1443
+ *
1444
+ * CLEAR, WRITE, WAIT, in that order, against the session's buffer.
1445
+ *
1446
+ * Clearing first is not tidiness. The nonce is single-use and the Parent
1447
+ * burns it: a `NONCE` left over from an earlier `CHALLENGE` would be returned
1448
+ * to the caller, minted against by the platform, and then rejected by the
1449
+ * Parent as `NONCE`, which reads as a replay rather than as stale state.
1450
+ *
1451
+ * Waiting on the buffer rather than on a fresh subscription means a reply
1452
+ * that lands before the waiter is registered is still found, and the window
1453
+ * between the nonce leg and the command leg is covered: there is no moment in
1454
+ * the session when nobody is listening.
1444
1455
  */
1445
1456
  private writeAndAwait;
1446
1457
  }
package/dist/index.d.ts CHANGED
@@ -1415,6 +1415,7 @@ interface BleTransportOptions {
1415
1415
  declare class BleTransport implements ParentTransport {
1416
1416
  private readonly manager;
1417
1417
  private readonly devices;
1418
+ private readonly notify;
1418
1419
  private readonly scanImpl;
1419
1420
  private readonly notifyTimeoutMs;
1420
1421
  constructor(opts: BleTransportOptions);
@@ -1438,9 +1439,19 @@ declare class BleTransport implements ParentTransport {
1438
1439
  readTelemetry(_session: Session): Promise<TowerTelemetry>;
1439
1440
  disconnect(session: Session): Promise<void>;
1440
1441
  /**
1441
- * Write one value and await the first notify line that matches `accept` (the
1442
- * default accepts the first non-passthrough line). Ordinary command output is
1443
- * skipped via the passthrough filter so we lock onto the structured line.
1442
+ * Write one value and await the first notify line that matches `accept`.
1443
+ *
1444
+ * CLEAR, WRITE, WAIT, in that order, against the session's buffer.
1445
+ *
1446
+ * Clearing first is not tidiness. The nonce is single-use and the Parent
1447
+ * burns it: a `NONCE` left over from an earlier `CHALLENGE` would be returned
1448
+ * to the caller, minted against by the platform, and then rejected by the
1449
+ * Parent as `NONCE`, which reads as a replay rather than as stale state.
1450
+ *
1451
+ * Waiting on the buffer rather than on a fresh subscription means a reply
1452
+ * that lands before the waiter is registered is still found, and the window
1453
+ * between the nonce leg and the command leg is covered: there is no moment in
1454
+ * the session when nobody is listening.
1444
1455
  */
1445
1456
  private writeAndAwait;
1446
1457
  }
package/dist/index.js CHANGED
@@ -1140,6 +1140,7 @@ var NOTIFY_TIMEOUT_MS = 8e3;
1140
1140
  var BleTransport = class {
1141
1141
  constructor(opts) {
1142
1142
  this.devices = /* @__PURE__ */ new Map();
1143
+ this.notify = /* @__PURE__ */ new Map();
1143
1144
  this.manager = opts.manager;
1144
1145
  this.scanImpl = opts.scan;
1145
1146
  this.notifyTimeoutMs = opts.notifyTimeoutMs ?? NOTIFY_TIMEOUT_MS;
@@ -1214,6 +1215,42 @@ var BleTransport = class {
1214
1215
  } catch {
1215
1216
  }
1216
1217
  this.devices.set(device.id, device);
1218
+ const state = {
1219
+ waiter: void 0,
1220
+ accept: void 0,
1221
+ failed: void 0,
1222
+ error: void 0,
1223
+ remove: () => void 0
1224
+ };
1225
+ const sub = device.monitorCharacteristicForService(
1226
+ RELAI_UART_SERVICE,
1227
+ RELAI_TX_CHAR,
1228
+ (error, ch) => {
1229
+ if (error) {
1230
+ const err = new RelaiTransportError(`BLE notify error: ${String(error)}`);
1231
+ state.error = err;
1232
+ state.failed?.(err);
1233
+ return;
1234
+ }
1235
+ const value = ch?.value;
1236
+ if (!value) return;
1237
+ let text;
1238
+ try {
1239
+ text = base64ToUtf8(value);
1240
+ } catch {
1241
+ return;
1242
+ }
1243
+ if (state.waiter && state.accept?.(parseNotifyLine(text))) {
1244
+ const resolve = state.waiter;
1245
+ state.waiter = void 0;
1246
+ state.accept = void 0;
1247
+ state.failed = void 0;
1248
+ resolve(text);
1249
+ }
1250
+ }
1251
+ );
1252
+ state.remove = () => sub.remove();
1253
+ this.notify.set(device.id, state);
1217
1254
  return { id: device.id, parentBleId, connectedAt: (/* @__PURE__ */ new Date()).toISOString() };
1218
1255
  }
1219
1256
  async requestNonce(session) {
@@ -1239,6 +1276,14 @@ var BleTransport = class {
1239
1276
  return Promise.resolve({});
1240
1277
  }
1241
1278
  async disconnect(session) {
1279
+ const state = this.notify.get(session.id);
1280
+ if (state) {
1281
+ try {
1282
+ state.remove();
1283
+ } catch {
1284
+ }
1285
+ this.notify.delete(session.id);
1286
+ }
1242
1287
  const device = this.devices.get(session.id);
1243
1288
  if (device) {
1244
1289
  await device.cancelConnection().catch(() => void 0);
@@ -1246,51 +1291,53 @@ var BleTransport = class {
1246
1291
  }
1247
1292
  }
1248
1293
  /**
1249
- * Write one value and await the first notify line that matches `accept` (the
1250
- * default accepts the first non-passthrough line). Ordinary command output is
1251
- * skipped via the passthrough filter so we lock onto the structured line.
1294
+ * Write one value and await the first notify line that matches `accept`.
1295
+ *
1296
+ * CLEAR, WRITE, WAIT, in that order, against the session's buffer.
1297
+ *
1298
+ * Clearing first is not tidiness. The nonce is single-use and the Parent
1299
+ * burns it: a `NONCE` left over from an earlier `CHALLENGE` would be returned
1300
+ * to the caller, minted against by the platform, and then rejected by the
1301
+ * Parent as `NONCE`, which reads as a replay rather than as stale state.
1302
+ *
1303
+ * Waiting on the buffer rather than on a fresh subscription means a reply
1304
+ * that lands before the waiter is registered is still found, and the window
1305
+ * between the nonce leg and the command leg is covered: there is no moment in
1306
+ * the session when nobody is listening.
1252
1307
  */
1253
1308
  writeAndAwait(session, valueBase64, accept = (p) => p.kind !== "passthrough") {
1254
1309
  const device = this.devices.get(session.id);
1255
- if (!device) return Promise.reject(new RelaiTransportError("Not connected."));
1310
+ const state = this.notify.get(session.id);
1311
+ if (!device || !state) return Promise.reject(new RelaiTransportError("Not connected."));
1312
+ if (state.error) return Promise.reject(state.error);
1313
+ if (state.waiter) {
1314
+ return Promise.reject(
1315
+ new RelaiTransportError("A command is already in flight on this Parent session.")
1316
+ );
1317
+ }
1256
1318
  return new Promise((resolve, reject) => {
1257
1319
  let settled = false;
1258
- const sub = device.monitorCharacteristicForService(RELAI_UART_SERVICE, RELAI_TX_CHAR, (error, ch) => {
1259
- if (settled) return;
1260
- if (error) {
1261
- settled = true;
1262
- clearTimeout(timer);
1263
- sub.remove();
1264
- reject(new RelaiTransportError(`BLE notify error: ${String(error)}`));
1265
- return;
1266
- }
1267
- const value = ch?.value;
1268
- if (!value) return;
1269
- let text;
1270
- try {
1271
- text = base64ToUtf8(value);
1272
- } catch {
1273
- return;
1274
- }
1275
- if (accept(parseNotifyLine(text))) {
1276
- settled = true;
1277
- clearTimeout(timer);
1278
- sub.remove();
1279
- resolve(text);
1280
- }
1281
- });
1282
- const timer = setTimeout(() => {
1283
- if (settled) return;
1284
- settled = true;
1285
- sub.remove();
1286
- reject(new RelaiTransportError("Timed out waiting for the Parent's notify response."));
1287
- }, this.notifyTimeoutMs);
1288
- device.writeCharacteristicWithResponseForService(RELAI_UART_SERVICE, RELAI_RX_CHAR, valueBase64).catch((err) => {
1320
+ const finish = (fn) => {
1289
1321
  if (settled) return;
1290
1322
  settled = true;
1291
1323
  clearTimeout(timer);
1292
- sub.remove();
1293
- reject(new RelaiTransportError(`BLE write failed: ${String(err)}`));
1324
+ state.waiter = void 0;
1325
+ state.accept = void 0;
1326
+ state.failed = void 0;
1327
+ fn();
1328
+ };
1329
+ state.accept = accept;
1330
+ state.waiter = (line) => finish(() => resolve(line));
1331
+ state.failed = (err) => finish(() => reject(err));
1332
+ const timer = setTimeout(() => {
1333
+ finish(
1334
+ () => reject(new RelaiTransportError("Timed out waiting for the Parent's notify response."))
1335
+ );
1336
+ }, this.notifyTimeoutMs);
1337
+ device.writeCharacteristicWithResponseForService(RELAI_UART_SERVICE, RELAI_RX_CHAR, valueBase64).then(() => {
1338
+ if (state.error) finish(() => reject(state.error));
1339
+ }).catch((err) => {
1340
+ finish(() => reject(new RelaiTransportError(`BLE write failed: ${String(err)}`)));
1294
1341
  });
1295
1342
  });
1296
1343
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@relai-team/access-sdk",
3
- "version": "0.20.0",
3
+ "version": "0.20.1",
4
4
  "description": "Official Relai Access SDK, open Relai Exchange Zone nodes over BLE, backed by the Relai Access platform (server-owns-trust).",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "module",