@relai-team/access-sdk 0.19.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 +23 -0
- package/dist/index.cjs +108 -53
- package/dist/index.d.cts +95 -57
- package/dist/index.d.ts +95 -57
- package/dist/index.js +108 -53
- package/package.json +1 -1
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
|
@@ -925,6 +925,7 @@ var RelaiClient = class {
|
|
|
925
925
|
constructor(options) {
|
|
926
926
|
if (!options.publishableKey && !options.secretKey) throw new exports.RelaiMissingKeyError();
|
|
927
927
|
this.stripe = options.stripe;
|
|
928
|
+
this.handleStorage = options.handleStorage;
|
|
928
929
|
const storage = options.tokenStorage ?? new MemoryTokenStorage();
|
|
929
930
|
let auth;
|
|
930
931
|
const httpConfig = {
|
|
@@ -1006,7 +1007,12 @@ var RelaiClient = class {
|
|
|
1006
1007
|
transport: input.transport,
|
|
1007
1008
|
...input.parentBleId !== void 0 ? { parentBleId: input.parentBleId } : {},
|
|
1008
1009
|
...input.exchangeZoneId !== void 0 ? { exchangeZoneId: input.exchangeZoneId } : {},
|
|
1009
|
-
...input.keepConnected !== void 0 ? { keepConnected: input.keepConnected } : {}
|
|
1010
|
+
...input.keepConnected !== void 0 ? { keepConnected: input.keepConnected } : {},
|
|
1011
|
+
// Per-call first, then the client's. Spread rather than assigned:
|
|
1012
|
+
// exactOptionalPropertyTypes makes an explicit `undefined` a value, and
|
|
1013
|
+
// `new ParentHandleCache(undefined)` is a cache that silently does
|
|
1014
|
+
// nothing, which is precisely the bug being fixed.
|
|
1015
|
+
...input.handleStorage ?? this.handleStorage ? { handleStorage: input.handleStorage ?? this.handleStorage } : {}
|
|
1010
1016
|
}
|
|
1011
1017
|
);
|
|
1012
1018
|
}
|
|
@@ -1099,6 +1105,21 @@ function base64ToUtf8(b64) {
|
|
|
1099
1105
|
if (g.atob) return decodeURIComponent(escape(g.atob(b64)));
|
|
1100
1106
|
throw new Error("No base64 decoder available in this runtime.");
|
|
1101
1107
|
}
|
|
1108
|
+
function base64ToBytes(b64) {
|
|
1109
|
+
const g = globalThis;
|
|
1110
|
+
try {
|
|
1111
|
+
if (g.Buffer) return new Uint8Array(g.Buffer.from(b64, "base64"));
|
|
1112
|
+
if (g.atob) {
|
|
1113
|
+
const binary = g.atob(b64);
|
|
1114
|
+
const out = new Uint8Array(binary.length);
|
|
1115
|
+
for (let i = 0; i < binary.length; i++) out[i] = binary.charCodeAt(i);
|
|
1116
|
+
return out;
|
|
1117
|
+
}
|
|
1118
|
+
return null;
|
|
1119
|
+
} catch {
|
|
1120
|
+
return null;
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1102
1123
|
|
|
1103
1124
|
// src/transport/simulated.ts
|
|
1104
1125
|
function randomNonceB64Url() {
|
|
@@ -1224,23 +1245,10 @@ init_errors();
|
|
|
1224
1245
|
var COMPANY_ID = 65535;
|
|
1225
1246
|
var FORMAT_VERSION = 1;
|
|
1226
1247
|
var HEADER_BYTES = 3;
|
|
1227
|
-
var SERIAL_MAX =
|
|
1228
|
-
function decodeBase64(value) {
|
|
1229
|
-
try {
|
|
1230
|
-
const binary = typeof atob === "function" ? atob(value) : (
|
|
1231
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1232
|
-
globalThis.Buffer.from(value, "base64").toString("binary")
|
|
1233
|
-
);
|
|
1234
|
-
const out = new Uint8Array(binary.length);
|
|
1235
|
-
for (let i = 0; i < binary.length; i++) out[i] = binary.charCodeAt(i);
|
|
1236
|
-
return out;
|
|
1237
|
-
} catch {
|
|
1238
|
-
return null;
|
|
1239
|
-
}
|
|
1240
|
-
}
|
|
1248
|
+
var SERIAL_MAX = 29 - HEADER_BYTES;
|
|
1241
1249
|
function parseAdvertisedSerial(value) {
|
|
1242
1250
|
if (!value) return null;
|
|
1243
|
-
const bytes =
|
|
1251
|
+
const bytes = base64ToBytes(value);
|
|
1244
1252
|
if (!bytes || bytes.length <= HEADER_BYTES) return null;
|
|
1245
1253
|
const company = bytes[0] | bytes[1] << 8;
|
|
1246
1254
|
if (company !== COMPANY_ID) return null;
|
|
@@ -1302,6 +1310,7 @@ var NOTIFY_TIMEOUT_MS = 8e3;
|
|
|
1302
1310
|
var BleTransport = class {
|
|
1303
1311
|
constructor(opts) {
|
|
1304
1312
|
this.devices = /* @__PURE__ */ new Map();
|
|
1313
|
+
this.notify = /* @__PURE__ */ new Map();
|
|
1305
1314
|
this.manager = opts.manager;
|
|
1306
1315
|
this.scanImpl = opts.scan;
|
|
1307
1316
|
this.notifyTimeoutMs = opts.notifyTimeoutMs ?? NOTIFY_TIMEOUT_MS;
|
|
@@ -1376,6 +1385,42 @@ var BleTransport = class {
|
|
|
1376
1385
|
} catch {
|
|
1377
1386
|
}
|
|
1378
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);
|
|
1379
1424
|
return { id: device.id, parentBleId, connectedAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
1380
1425
|
}
|
|
1381
1426
|
async requestNonce(session) {
|
|
@@ -1401,6 +1446,14 @@ var BleTransport = class {
|
|
|
1401
1446
|
return Promise.resolve({});
|
|
1402
1447
|
}
|
|
1403
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
|
+
}
|
|
1404
1457
|
const device = this.devices.get(session.id);
|
|
1405
1458
|
if (device) {
|
|
1406
1459
|
await device.cancelConnection().catch(() => void 0);
|
|
@@ -1408,51 +1461,53 @@ var BleTransport = class {
|
|
|
1408
1461
|
}
|
|
1409
1462
|
}
|
|
1410
1463
|
/**
|
|
1411
|
-
* Write one value and await the first notify line that matches `accept
|
|
1412
|
-
*
|
|
1413
|
-
*
|
|
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.
|
|
1414
1477
|
*/
|
|
1415
1478
|
writeAndAwait(session, valueBase64, accept = (p) => p.kind !== "passthrough") {
|
|
1416
1479
|
const device = this.devices.get(session.id);
|
|
1417
|
-
|
|
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
|
+
}
|
|
1418
1488
|
return new Promise((resolve, reject) => {
|
|
1419
1489
|
let settled = false;
|
|
1420
|
-
const
|
|
1421
|
-
if (settled) return;
|
|
1422
|
-
if (error) {
|
|
1423
|
-
settled = true;
|
|
1424
|
-
clearTimeout(timer);
|
|
1425
|
-
sub.remove();
|
|
1426
|
-
reject(new exports.RelaiTransportError(`BLE notify error: ${String(error)}`));
|
|
1427
|
-
return;
|
|
1428
|
-
}
|
|
1429
|
-
const value = ch?.value;
|
|
1430
|
-
if (!value) return;
|
|
1431
|
-
let text;
|
|
1432
|
-
try {
|
|
1433
|
-
text = base64ToUtf8(value);
|
|
1434
|
-
} catch {
|
|
1435
|
-
return;
|
|
1436
|
-
}
|
|
1437
|
-
if (accept(parseNotifyLine(text))) {
|
|
1438
|
-
settled = true;
|
|
1439
|
-
clearTimeout(timer);
|
|
1440
|
-
sub.remove();
|
|
1441
|
-
resolve(text);
|
|
1442
|
-
}
|
|
1443
|
-
});
|
|
1444
|
-
const timer = setTimeout(() => {
|
|
1445
|
-
if (settled) return;
|
|
1446
|
-
settled = true;
|
|
1447
|
-
sub.remove();
|
|
1448
|
-
reject(new exports.RelaiTransportError("Timed out waiting for the Parent's notify response."));
|
|
1449
|
-
}, this.notifyTimeoutMs);
|
|
1450
|
-
device.writeCharacteristicWithResponseForService(RELAI_UART_SERVICE, RELAI_RX_CHAR, valueBase64).catch((err) => {
|
|
1490
|
+
const finish = (fn) => {
|
|
1451
1491
|
if (settled) return;
|
|
1452
1492
|
settled = true;
|
|
1453
1493
|
clearTimeout(timer);
|
|
1454
|
-
|
|
1455
|
-
|
|
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)}`)));
|
|
1456
1511
|
});
|
|
1457
1512
|
});
|
|
1458
1513
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -930,6 +930,60 @@ declare class TelemetryUploader {
|
|
|
930
930
|
}>;
|
|
931
931
|
}
|
|
932
932
|
|
|
933
|
+
/**
|
|
934
|
+
* Persistent telemetry queue (owner, 2026-07-24). `MemoryTelemetryQueue` loses
|
|
935
|
+
* anything queued when the app is killed; a React Native app that opens nodes
|
|
936
|
+
* offline needs its unreported opens to survive a restart. This backs the same
|
|
937
|
+
* `TelemetryQueue` interface with a key-value store.
|
|
938
|
+
*
|
|
939
|
+
* The store is AsyncStorage-shaped on purpose, so RN apps pass the library in
|
|
940
|
+
* directly (it is an OPTIONAL dependency: the SDK core never imports it):
|
|
941
|
+
*
|
|
942
|
+
* import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
943
|
+
* const client = new RelaiClient({
|
|
944
|
+
* publishableKey,
|
|
945
|
+
* telemetryQueue: new StorageTelemetryQueue(AsyncStorage),
|
|
946
|
+
* });
|
|
947
|
+
*
|
|
948
|
+
* The whole queue is one JSON blob under a single key: telemetry volume is one
|
|
949
|
+
* item per open, so this stays small; writes are serialized through an internal
|
|
950
|
+
* lock so concurrent submit/flush calls can't clobber each other.
|
|
951
|
+
*/
|
|
952
|
+
|
|
953
|
+
/** The subset of AsyncStorage (and localStorage-async wrappers) we need. */
|
|
954
|
+
interface KeyValueStorage {
|
|
955
|
+
getItem(key: string): Promise<string | null>;
|
|
956
|
+
setItem(key: string, value: string): Promise<void>;
|
|
957
|
+
removeItem(key: string): Promise<void>;
|
|
958
|
+
}
|
|
959
|
+
interface StorageTelemetryQueueOptions {
|
|
960
|
+
/** Storage key for the serialized queue. Default `@relai/telemetry-queue`. */
|
|
961
|
+
storageKey?: string;
|
|
962
|
+
/**
|
|
963
|
+
* Hard cap on queued items; when exceeded the OLDEST are dropped so a long
|
|
964
|
+
* outage can't grow storage without bound. Default 500.
|
|
965
|
+
*/
|
|
966
|
+
maxItems?: number;
|
|
967
|
+
}
|
|
968
|
+
declare class StorageTelemetryQueue implements TelemetryQueue {
|
|
969
|
+
private readonly storage;
|
|
970
|
+
private readonly key;
|
|
971
|
+
private readonly maxItems;
|
|
972
|
+
/** Serializes read-modify-write so concurrent calls don't lose updates. */
|
|
973
|
+
private chain;
|
|
974
|
+
private seq;
|
|
975
|
+
constructor(storage: KeyValueStorage, opts?: StorageTelemetryQueueOptions);
|
|
976
|
+
enqueue(payload: TelemetryPayload): Promise<void>;
|
|
977
|
+
list(): Promise<QueuedTelemetry[]>;
|
|
978
|
+
remove(id: string): Promise<void>;
|
|
979
|
+
markAttempt(id: string): Promise<void>;
|
|
980
|
+
private nextId;
|
|
981
|
+
/** Serialize an operation after any in-flight ones (a simple promise lock). */
|
|
982
|
+
private run;
|
|
983
|
+
private mutate;
|
|
984
|
+
private read;
|
|
985
|
+
}
|
|
986
|
+
|
|
933
987
|
/**
|
|
934
988
|
* The outcome of writing one authenticated command to a Parent, distilled from
|
|
935
989
|
* the notify surface. `accepted` is the ONLY control-flow signal; `verbHint` is
|
|
@@ -1004,60 +1058,6 @@ interface ParentTransport {
|
|
|
1004
1058
|
disconnect(session: Session): Promise<void>;
|
|
1005
1059
|
}
|
|
1006
1060
|
|
|
1007
|
-
/**
|
|
1008
|
-
* Persistent telemetry queue (owner, 2026-07-24). `MemoryTelemetryQueue` loses
|
|
1009
|
-
* anything queued when the app is killed; a React Native app that opens nodes
|
|
1010
|
-
* offline needs its unreported opens to survive a restart. This backs the same
|
|
1011
|
-
* `TelemetryQueue` interface with a key-value store.
|
|
1012
|
-
*
|
|
1013
|
-
* The store is AsyncStorage-shaped on purpose, so RN apps pass the library in
|
|
1014
|
-
* directly (it is an OPTIONAL dependency: the SDK core never imports it):
|
|
1015
|
-
*
|
|
1016
|
-
* import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
1017
|
-
* const client = new RelaiClient({
|
|
1018
|
-
* publishableKey,
|
|
1019
|
-
* telemetryQueue: new StorageTelemetryQueue(AsyncStorage),
|
|
1020
|
-
* });
|
|
1021
|
-
*
|
|
1022
|
-
* The whole queue is one JSON blob under a single key: telemetry volume is one
|
|
1023
|
-
* item per open, so this stays small; writes are serialized through an internal
|
|
1024
|
-
* lock so concurrent submit/flush calls can't clobber each other.
|
|
1025
|
-
*/
|
|
1026
|
-
|
|
1027
|
-
/** The subset of AsyncStorage (and localStorage-async wrappers) we need. */
|
|
1028
|
-
interface KeyValueStorage {
|
|
1029
|
-
getItem(key: string): Promise<string | null>;
|
|
1030
|
-
setItem(key: string, value: string): Promise<void>;
|
|
1031
|
-
removeItem(key: string): Promise<void>;
|
|
1032
|
-
}
|
|
1033
|
-
interface StorageTelemetryQueueOptions {
|
|
1034
|
-
/** Storage key for the serialized queue. Default `@relai/telemetry-queue`. */
|
|
1035
|
-
storageKey?: string;
|
|
1036
|
-
/**
|
|
1037
|
-
* Hard cap on queued items; when exceeded the OLDEST are dropped so a long
|
|
1038
|
-
* outage can't grow storage without bound. Default 500.
|
|
1039
|
-
*/
|
|
1040
|
-
maxItems?: number;
|
|
1041
|
-
}
|
|
1042
|
-
declare class StorageTelemetryQueue implements TelemetryQueue {
|
|
1043
|
-
private readonly storage;
|
|
1044
|
-
private readonly key;
|
|
1045
|
-
private readonly maxItems;
|
|
1046
|
-
/** Serializes read-modify-write so concurrent calls don't lose updates. */
|
|
1047
|
-
private chain;
|
|
1048
|
-
private seq;
|
|
1049
|
-
constructor(storage: KeyValueStorage, opts?: StorageTelemetryQueueOptions);
|
|
1050
|
-
enqueue(payload: TelemetryPayload): Promise<void>;
|
|
1051
|
-
list(): Promise<QueuedTelemetry[]>;
|
|
1052
|
-
remove(id: string): Promise<void>;
|
|
1053
|
-
markAttempt(id: string): Promise<void>;
|
|
1054
|
-
private nextId;
|
|
1055
|
-
/** Serialize an operation after any in-flight ones (a simple promise lock). */
|
|
1056
|
-
private run;
|
|
1057
|
-
private mutate;
|
|
1058
|
-
private read;
|
|
1059
|
-
}
|
|
1060
|
-
|
|
1061
1061
|
interface UnlockDeps {
|
|
1062
1062
|
grants: GrantsModule;
|
|
1063
1063
|
telemetry: TelemetryUploader;
|
|
@@ -1201,6 +1201,22 @@ interface RelaiClientOptions {
|
|
|
1201
1201
|
maxRetries?: number;
|
|
1202
1202
|
/** Persistent telemetry queue (default: in-memory). RN apps can back it with storage. */
|
|
1203
1203
|
telemetryQueue?: TelemetryQueue;
|
|
1204
|
+
/**
|
|
1205
|
+
* Where to remember which local handle turned out to be which Parent.
|
|
1206
|
+
*
|
|
1207
|
+
* Pass the same `KeyValueStorage` the telemetry queue uses, typically
|
|
1208
|
+
* AsyncStorage, and an open that resolved a Parent connects straight to it
|
|
1209
|
+
* next time instead of scanning. Omit it and every open rescans, which costs
|
|
1210
|
+
* a few seconds and nothing else.
|
|
1211
|
+
*
|
|
1212
|
+
* HERE rather than only on each `unlock()` call, because the mapping is
|
|
1213
|
+
* per-install device state like `tokenStorage` and `telemetryQueue`: an app
|
|
1214
|
+
* wires its store once. `UnlockInput.handleStorage` overrides it per call.
|
|
1215
|
+
*
|
|
1216
|
+
* The mapping is per device, because the identifier is: the handle this phone
|
|
1217
|
+
* connects by is not the handle another phone gets for the same Parent.
|
|
1218
|
+
*/
|
|
1219
|
+
handleStorage?: KeyValueStorage;
|
|
1204
1220
|
/**
|
|
1205
1221
|
* The `@stripe/stripe-react-native` module, for Relai checkout. Pass it and
|
|
1206
1222
|
* payment-gated opens present the standard Payment Sheet and retry
|
|
@@ -1251,6 +1267,15 @@ interface UnlockInput {
|
|
|
1251
1267
|
/** Admin/partner override link code: bypasses handoff + paywall gates. */
|
|
1252
1268
|
overrideCode?: string;
|
|
1253
1269
|
keepConnected?: boolean;
|
|
1270
|
+
/**
|
|
1271
|
+
* Override the client's `handleStorage` for this open.
|
|
1272
|
+
*
|
|
1273
|
+
* Normally omitted: set it once on the client. This exists because
|
|
1274
|
+
* `deliverOpen` has always accepted it and `unlock()` dropped it silently, so
|
|
1275
|
+
* a Relai-checkout app could not reach the cache at all without
|
|
1276
|
+
* reimplementing the payment handling (partner report, 2026-10-05).
|
|
1277
|
+
*/
|
|
1278
|
+
handleStorage?: KeyValueStorage;
|
|
1254
1279
|
}
|
|
1255
1280
|
declare class RelaiClient {
|
|
1256
1281
|
readonly auth: AuthModule;
|
|
@@ -1275,6 +1300,8 @@ declare class RelaiClient {
|
|
|
1275
1300
|
private readonly http;
|
|
1276
1301
|
/** The app-supplied Stripe module, if any. See `RelaiClientOptions.stripe`. */
|
|
1277
1302
|
private readonly stripe;
|
|
1303
|
+
/** Where resolved Parent handles are remembered. See `RelaiClientOptions.handleStorage`. */
|
|
1304
|
+
private readonly handleStorage;
|
|
1278
1305
|
constructor(options: RelaiClientOptions);
|
|
1279
1306
|
/** Drain any telemetry queued after a failed send (e.g. on app foreground). */
|
|
1280
1307
|
flushTelemetry(): Promise<{
|
|
@@ -1388,6 +1415,7 @@ interface BleTransportOptions {
|
|
|
1388
1415
|
declare class BleTransport implements ParentTransport {
|
|
1389
1416
|
private readonly manager;
|
|
1390
1417
|
private readonly devices;
|
|
1418
|
+
private readonly notify;
|
|
1391
1419
|
private readonly scanImpl;
|
|
1392
1420
|
private readonly notifyTimeoutMs;
|
|
1393
1421
|
constructor(opts: BleTransportOptions);
|
|
@@ -1411,9 +1439,19 @@ declare class BleTransport implements ParentTransport {
|
|
|
1411
1439
|
readTelemetry(_session: Session): Promise<TowerTelemetry>;
|
|
1412
1440
|
disconnect(session: Session): Promise<void>;
|
|
1413
1441
|
/**
|
|
1414
|
-
* Write one value and await the first notify line that matches `accept
|
|
1415
|
-
*
|
|
1416
|
-
*
|
|
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.
|
|
1417
1455
|
*/
|
|
1418
1456
|
private writeAndAwait;
|
|
1419
1457
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -930,6 +930,60 @@ declare class TelemetryUploader {
|
|
|
930
930
|
}>;
|
|
931
931
|
}
|
|
932
932
|
|
|
933
|
+
/**
|
|
934
|
+
* Persistent telemetry queue (owner, 2026-07-24). `MemoryTelemetryQueue` loses
|
|
935
|
+
* anything queued when the app is killed; a React Native app that opens nodes
|
|
936
|
+
* offline needs its unreported opens to survive a restart. This backs the same
|
|
937
|
+
* `TelemetryQueue` interface with a key-value store.
|
|
938
|
+
*
|
|
939
|
+
* The store is AsyncStorage-shaped on purpose, so RN apps pass the library in
|
|
940
|
+
* directly (it is an OPTIONAL dependency: the SDK core never imports it):
|
|
941
|
+
*
|
|
942
|
+
* import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
943
|
+
* const client = new RelaiClient({
|
|
944
|
+
* publishableKey,
|
|
945
|
+
* telemetryQueue: new StorageTelemetryQueue(AsyncStorage),
|
|
946
|
+
* });
|
|
947
|
+
*
|
|
948
|
+
* The whole queue is one JSON blob under a single key: telemetry volume is one
|
|
949
|
+
* item per open, so this stays small; writes are serialized through an internal
|
|
950
|
+
* lock so concurrent submit/flush calls can't clobber each other.
|
|
951
|
+
*/
|
|
952
|
+
|
|
953
|
+
/** The subset of AsyncStorage (and localStorage-async wrappers) we need. */
|
|
954
|
+
interface KeyValueStorage {
|
|
955
|
+
getItem(key: string): Promise<string | null>;
|
|
956
|
+
setItem(key: string, value: string): Promise<void>;
|
|
957
|
+
removeItem(key: string): Promise<void>;
|
|
958
|
+
}
|
|
959
|
+
interface StorageTelemetryQueueOptions {
|
|
960
|
+
/** Storage key for the serialized queue. Default `@relai/telemetry-queue`. */
|
|
961
|
+
storageKey?: string;
|
|
962
|
+
/**
|
|
963
|
+
* Hard cap on queued items; when exceeded the OLDEST are dropped so a long
|
|
964
|
+
* outage can't grow storage without bound. Default 500.
|
|
965
|
+
*/
|
|
966
|
+
maxItems?: number;
|
|
967
|
+
}
|
|
968
|
+
declare class StorageTelemetryQueue implements TelemetryQueue {
|
|
969
|
+
private readonly storage;
|
|
970
|
+
private readonly key;
|
|
971
|
+
private readonly maxItems;
|
|
972
|
+
/** Serializes read-modify-write so concurrent calls don't lose updates. */
|
|
973
|
+
private chain;
|
|
974
|
+
private seq;
|
|
975
|
+
constructor(storage: KeyValueStorage, opts?: StorageTelemetryQueueOptions);
|
|
976
|
+
enqueue(payload: TelemetryPayload): Promise<void>;
|
|
977
|
+
list(): Promise<QueuedTelemetry[]>;
|
|
978
|
+
remove(id: string): Promise<void>;
|
|
979
|
+
markAttempt(id: string): Promise<void>;
|
|
980
|
+
private nextId;
|
|
981
|
+
/** Serialize an operation after any in-flight ones (a simple promise lock). */
|
|
982
|
+
private run;
|
|
983
|
+
private mutate;
|
|
984
|
+
private read;
|
|
985
|
+
}
|
|
986
|
+
|
|
933
987
|
/**
|
|
934
988
|
* The outcome of writing one authenticated command to a Parent, distilled from
|
|
935
989
|
* the notify surface. `accepted` is the ONLY control-flow signal; `verbHint` is
|
|
@@ -1004,60 +1058,6 @@ interface ParentTransport {
|
|
|
1004
1058
|
disconnect(session: Session): Promise<void>;
|
|
1005
1059
|
}
|
|
1006
1060
|
|
|
1007
|
-
/**
|
|
1008
|
-
* Persistent telemetry queue (owner, 2026-07-24). `MemoryTelemetryQueue` loses
|
|
1009
|
-
* anything queued when the app is killed; a React Native app that opens nodes
|
|
1010
|
-
* offline needs its unreported opens to survive a restart. This backs the same
|
|
1011
|
-
* `TelemetryQueue` interface with a key-value store.
|
|
1012
|
-
*
|
|
1013
|
-
* The store is AsyncStorage-shaped on purpose, so RN apps pass the library in
|
|
1014
|
-
* directly (it is an OPTIONAL dependency: the SDK core never imports it):
|
|
1015
|
-
*
|
|
1016
|
-
* import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
1017
|
-
* const client = new RelaiClient({
|
|
1018
|
-
* publishableKey,
|
|
1019
|
-
* telemetryQueue: new StorageTelemetryQueue(AsyncStorage),
|
|
1020
|
-
* });
|
|
1021
|
-
*
|
|
1022
|
-
* The whole queue is one JSON blob under a single key: telemetry volume is one
|
|
1023
|
-
* item per open, so this stays small; writes are serialized through an internal
|
|
1024
|
-
* lock so concurrent submit/flush calls can't clobber each other.
|
|
1025
|
-
*/
|
|
1026
|
-
|
|
1027
|
-
/** The subset of AsyncStorage (and localStorage-async wrappers) we need. */
|
|
1028
|
-
interface KeyValueStorage {
|
|
1029
|
-
getItem(key: string): Promise<string | null>;
|
|
1030
|
-
setItem(key: string, value: string): Promise<void>;
|
|
1031
|
-
removeItem(key: string): Promise<void>;
|
|
1032
|
-
}
|
|
1033
|
-
interface StorageTelemetryQueueOptions {
|
|
1034
|
-
/** Storage key for the serialized queue. Default `@relai/telemetry-queue`. */
|
|
1035
|
-
storageKey?: string;
|
|
1036
|
-
/**
|
|
1037
|
-
* Hard cap on queued items; when exceeded the OLDEST are dropped so a long
|
|
1038
|
-
* outage can't grow storage without bound. Default 500.
|
|
1039
|
-
*/
|
|
1040
|
-
maxItems?: number;
|
|
1041
|
-
}
|
|
1042
|
-
declare class StorageTelemetryQueue implements TelemetryQueue {
|
|
1043
|
-
private readonly storage;
|
|
1044
|
-
private readonly key;
|
|
1045
|
-
private readonly maxItems;
|
|
1046
|
-
/** Serializes read-modify-write so concurrent calls don't lose updates. */
|
|
1047
|
-
private chain;
|
|
1048
|
-
private seq;
|
|
1049
|
-
constructor(storage: KeyValueStorage, opts?: StorageTelemetryQueueOptions);
|
|
1050
|
-
enqueue(payload: TelemetryPayload): Promise<void>;
|
|
1051
|
-
list(): Promise<QueuedTelemetry[]>;
|
|
1052
|
-
remove(id: string): Promise<void>;
|
|
1053
|
-
markAttempt(id: string): Promise<void>;
|
|
1054
|
-
private nextId;
|
|
1055
|
-
/** Serialize an operation after any in-flight ones (a simple promise lock). */
|
|
1056
|
-
private run;
|
|
1057
|
-
private mutate;
|
|
1058
|
-
private read;
|
|
1059
|
-
}
|
|
1060
|
-
|
|
1061
1061
|
interface UnlockDeps {
|
|
1062
1062
|
grants: GrantsModule;
|
|
1063
1063
|
telemetry: TelemetryUploader;
|
|
@@ -1201,6 +1201,22 @@ interface RelaiClientOptions {
|
|
|
1201
1201
|
maxRetries?: number;
|
|
1202
1202
|
/** Persistent telemetry queue (default: in-memory). RN apps can back it with storage. */
|
|
1203
1203
|
telemetryQueue?: TelemetryQueue;
|
|
1204
|
+
/**
|
|
1205
|
+
* Where to remember which local handle turned out to be which Parent.
|
|
1206
|
+
*
|
|
1207
|
+
* Pass the same `KeyValueStorage` the telemetry queue uses, typically
|
|
1208
|
+
* AsyncStorage, and an open that resolved a Parent connects straight to it
|
|
1209
|
+
* next time instead of scanning. Omit it and every open rescans, which costs
|
|
1210
|
+
* a few seconds and nothing else.
|
|
1211
|
+
*
|
|
1212
|
+
* HERE rather than only on each `unlock()` call, because the mapping is
|
|
1213
|
+
* per-install device state like `tokenStorage` and `telemetryQueue`: an app
|
|
1214
|
+
* wires its store once. `UnlockInput.handleStorage` overrides it per call.
|
|
1215
|
+
*
|
|
1216
|
+
* The mapping is per device, because the identifier is: the handle this phone
|
|
1217
|
+
* connects by is not the handle another phone gets for the same Parent.
|
|
1218
|
+
*/
|
|
1219
|
+
handleStorage?: KeyValueStorage;
|
|
1204
1220
|
/**
|
|
1205
1221
|
* The `@stripe/stripe-react-native` module, for Relai checkout. Pass it and
|
|
1206
1222
|
* payment-gated opens present the standard Payment Sheet and retry
|
|
@@ -1251,6 +1267,15 @@ interface UnlockInput {
|
|
|
1251
1267
|
/** Admin/partner override link code: bypasses handoff + paywall gates. */
|
|
1252
1268
|
overrideCode?: string;
|
|
1253
1269
|
keepConnected?: boolean;
|
|
1270
|
+
/**
|
|
1271
|
+
* Override the client's `handleStorage` for this open.
|
|
1272
|
+
*
|
|
1273
|
+
* Normally omitted: set it once on the client. This exists because
|
|
1274
|
+
* `deliverOpen` has always accepted it and `unlock()` dropped it silently, so
|
|
1275
|
+
* a Relai-checkout app could not reach the cache at all without
|
|
1276
|
+
* reimplementing the payment handling (partner report, 2026-10-05).
|
|
1277
|
+
*/
|
|
1278
|
+
handleStorage?: KeyValueStorage;
|
|
1254
1279
|
}
|
|
1255
1280
|
declare class RelaiClient {
|
|
1256
1281
|
readonly auth: AuthModule;
|
|
@@ -1275,6 +1300,8 @@ declare class RelaiClient {
|
|
|
1275
1300
|
private readonly http;
|
|
1276
1301
|
/** The app-supplied Stripe module, if any. See `RelaiClientOptions.stripe`. */
|
|
1277
1302
|
private readonly stripe;
|
|
1303
|
+
/** Where resolved Parent handles are remembered. See `RelaiClientOptions.handleStorage`. */
|
|
1304
|
+
private readonly handleStorage;
|
|
1278
1305
|
constructor(options: RelaiClientOptions);
|
|
1279
1306
|
/** Drain any telemetry queued after a failed send (e.g. on app foreground). */
|
|
1280
1307
|
flushTelemetry(): Promise<{
|
|
@@ -1388,6 +1415,7 @@ interface BleTransportOptions {
|
|
|
1388
1415
|
declare class BleTransport implements ParentTransport {
|
|
1389
1416
|
private readonly manager;
|
|
1390
1417
|
private readonly devices;
|
|
1418
|
+
private readonly notify;
|
|
1391
1419
|
private readonly scanImpl;
|
|
1392
1420
|
private readonly notifyTimeoutMs;
|
|
1393
1421
|
constructor(opts: BleTransportOptions);
|
|
@@ -1411,9 +1439,19 @@ declare class BleTransport implements ParentTransport {
|
|
|
1411
1439
|
readTelemetry(_session: Session): Promise<TowerTelemetry>;
|
|
1412
1440
|
disconnect(session: Session): Promise<void>;
|
|
1413
1441
|
/**
|
|
1414
|
-
* Write one value and await the first notify line that matches `accept
|
|
1415
|
-
*
|
|
1416
|
-
*
|
|
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.
|
|
1417
1455
|
*/
|
|
1418
1456
|
private writeAndAwait;
|
|
1419
1457
|
}
|
package/dist/index.js
CHANGED
|
@@ -762,6 +762,7 @@ var RelaiClient = class {
|
|
|
762
762
|
constructor(options) {
|
|
763
763
|
if (!options.publishableKey && !options.secretKey) throw new RelaiMissingKeyError();
|
|
764
764
|
this.stripe = options.stripe;
|
|
765
|
+
this.handleStorage = options.handleStorage;
|
|
765
766
|
const storage = options.tokenStorage ?? new MemoryTokenStorage();
|
|
766
767
|
let auth;
|
|
767
768
|
const httpConfig = {
|
|
@@ -843,7 +844,12 @@ var RelaiClient = class {
|
|
|
843
844
|
transport: input.transport,
|
|
844
845
|
...input.parentBleId !== void 0 ? { parentBleId: input.parentBleId } : {},
|
|
845
846
|
...input.exchangeZoneId !== void 0 ? { exchangeZoneId: input.exchangeZoneId } : {},
|
|
846
|
-
...input.keepConnected !== void 0 ? { keepConnected: input.keepConnected } : {}
|
|
847
|
+
...input.keepConnected !== void 0 ? { keepConnected: input.keepConnected } : {},
|
|
848
|
+
// Per-call first, then the client's. Spread rather than assigned:
|
|
849
|
+
// exactOptionalPropertyTypes makes an explicit `undefined` a value, and
|
|
850
|
+
// `new ParentHandleCache(undefined)` is a cache that silently does
|
|
851
|
+
// nothing, which is precisely the bug being fixed.
|
|
852
|
+
...input.handleStorage ?? this.handleStorage ? { handleStorage: input.handleStorage ?? this.handleStorage } : {}
|
|
847
853
|
}
|
|
848
854
|
);
|
|
849
855
|
}
|
|
@@ -933,6 +939,21 @@ function base64ToUtf8(b64) {
|
|
|
933
939
|
if (g.atob) return decodeURIComponent(escape(g.atob(b64)));
|
|
934
940
|
throw new Error("No base64 decoder available in this runtime.");
|
|
935
941
|
}
|
|
942
|
+
function base64ToBytes(b64) {
|
|
943
|
+
const g = globalThis;
|
|
944
|
+
try {
|
|
945
|
+
if (g.Buffer) return new Uint8Array(g.Buffer.from(b64, "base64"));
|
|
946
|
+
if (g.atob) {
|
|
947
|
+
const binary = g.atob(b64);
|
|
948
|
+
const out = new Uint8Array(binary.length);
|
|
949
|
+
for (let i = 0; i < binary.length; i++) out[i] = binary.charCodeAt(i);
|
|
950
|
+
return out;
|
|
951
|
+
}
|
|
952
|
+
return null;
|
|
953
|
+
} catch {
|
|
954
|
+
return null;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
936
957
|
|
|
937
958
|
// src/transport/simulated.ts
|
|
938
959
|
function randomNonceB64Url() {
|
|
@@ -1054,23 +1075,10 @@ var SimAuthenticatedTransport = class {
|
|
|
1054
1075
|
var COMPANY_ID = 65535;
|
|
1055
1076
|
var FORMAT_VERSION = 1;
|
|
1056
1077
|
var HEADER_BYTES = 3;
|
|
1057
|
-
var SERIAL_MAX =
|
|
1058
|
-
function decodeBase64(value) {
|
|
1059
|
-
try {
|
|
1060
|
-
const binary = typeof atob === "function" ? atob(value) : (
|
|
1061
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1062
|
-
globalThis.Buffer.from(value, "base64").toString("binary")
|
|
1063
|
-
);
|
|
1064
|
-
const out = new Uint8Array(binary.length);
|
|
1065
|
-
for (let i = 0; i < binary.length; i++) out[i] = binary.charCodeAt(i);
|
|
1066
|
-
return out;
|
|
1067
|
-
} catch {
|
|
1068
|
-
return null;
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1078
|
+
var SERIAL_MAX = 29 - HEADER_BYTES;
|
|
1071
1079
|
function parseAdvertisedSerial(value) {
|
|
1072
1080
|
if (!value) return null;
|
|
1073
|
-
const bytes =
|
|
1081
|
+
const bytes = base64ToBytes(value);
|
|
1074
1082
|
if (!bytes || bytes.length <= HEADER_BYTES) return null;
|
|
1075
1083
|
const company = bytes[0] | bytes[1] << 8;
|
|
1076
1084
|
if (company !== COMPANY_ID) return null;
|
|
@@ -1132,6 +1140,7 @@ var NOTIFY_TIMEOUT_MS = 8e3;
|
|
|
1132
1140
|
var BleTransport = class {
|
|
1133
1141
|
constructor(opts) {
|
|
1134
1142
|
this.devices = /* @__PURE__ */ new Map();
|
|
1143
|
+
this.notify = /* @__PURE__ */ new Map();
|
|
1135
1144
|
this.manager = opts.manager;
|
|
1136
1145
|
this.scanImpl = opts.scan;
|
|
1137
1146
|
this.notifyTimeoutMs = opts.notifyTimeoutMs ?? NOTIFY_TIMEOUT_MS;
|
|
@@ -1206,6 +1215,42 @@ var BleTransport = class {
|
|
|
1206
1215
|
} catch {
|
|
1207
1216
|
}
|
|
1208
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);
|
|
1209
1254
|
return { id: device.id, parentBleId, connectedAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
1210
1255
|
}
|
|
1211
1256
|
async requestNonce(session) {
|
|
@@ -1231,6 +1276,14 @@ var BleTransport = class {
|
|
|
1231
1276
|
return Promise.resolve({});
|
|
1232
1277
|
}
|
|
1233
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
|
+
}
|
|
1234
1287
|
const device = this.devices.get(session.id);
|
|
1235
1288
|
if (device) {
|
|
1236
1289
|
await device.cancelConnection().catch(() => void 0);
|
|
@@ -1238,51 +1291,53 @@ var BleTransport = class {
|
|
|
1238
1291
|
}
|
|
1239
1292
|
}
|
|
1240
1293
|
/**
|
|
1241
|
-
* Write one value and await the first notify line that matches `accept
|
|
1242
|
-
*
|
|
1243
|
-
*
|
|
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.
|
|
1244
1307
|
*/
|
|
1245
1308
|
writeAndAwait(session, valueBase64, accept = (p) => p.kind !== "passthrough") {
|
|
1246
1309
|
const device = this.devices.get(session.id);
|
|
1247
|
-
|
|
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
|
+
}
|
|
1248
1318
|
return new Promise((resolve, reject) => {
|
|
1249
1319
|
let settled = false;
|
|
1250
|
-
const
|
|
1251
|
-
if (settled) return;
|
|
1252
|
-
if (error) {
|
|
1253
|
-
settled = true;
|
|
1254
|
-
clearTimeout(timer);
|
|
1255
|
-
sub.remove();
|
|
1256
|
-
reject(new RelaiTransportError(`BLE notify error: ${String(error)}`));
|
|
1257
|
-
return;
|
|
1258
|
-
}
|
|
1259
|
-
const value = ch?.value;
|
|
1260
|
-
if (!value) return;
|
|
1261
|
-
let text;
|
|
1262
|
-
try {
|
|
1263
|
-
text = base64ToUtf8(value);
|
|
1264
|
-
} catch {
|
|
1265
|
-
return;
|
|
1266
|
-
}
|
|
1267
|
-
if (accept(parseNotifyLine(text))) {
|
|
1268
|
-
settled = true;
|
|
1269
|
-
clearTimeout(timer);
|
|
1270
|
-
sub.remove();
|
|
1271
|
-
resolve(text);
|
|
1272
|
-
}
|
|
1273
|
-
});
|
|
1274
|
-
const timer = setTimeout(() => {
|
|
1275
|
-
if (settled) return;
|
|
1276
|
-
settled = true;
|
|
1277
|
-
sub.remove();
|
|
1278
|
-
reject(new RelaiTransportError("Timed out waiting for the Parent's notify response."));
|
|
1279
|
-
}, this.notifyTimeoutMs);
|
|
1280
|
-
device.writeCharacteristicWithResponseForService(RELAI_UART_SERVICE, RELAI_RX_CHAR, valueBase64).catch((err) => {
|
|
1320
|
+
const finish = (fn) => {
|
|
1281
1321
|
if (settled) return;
|
|
1282
1322
|
settled = true;
|
|
1283
1323
|
clearTimeout(timer);
|
|
1284
|
-
|
|
1285
|
-
|
|
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)}`)));
|
|
1286
1341
|
});
|
|
1287
1342
|
});
|
|
1288
1343
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@relai-team/access-sdk",
|
|
3
|
-
"version": "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",
|