@relai-team/access-sdk 0.20.0 → 0.20.2
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 +130 -49
- package/dist/index.d.cts +29 -3
- package/dist/index.d.ts +29 -3
- package/dist/index.js +130 -49
- 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
|
@@ -874,10 +874,13 @@ async function deliverOpen(deps, open, opts) {
|
|
|
874
874
|
}).catch(() => void 0);
|
|
875
875
|
}
|
|
876
876
|
let candidates = orderCandidates({ cached: await cache.get(parentBleId), parentBleId });
|
|
877
|
+
const tried = /* @__PURE__ */ new Set();
|
|
877
878
|
let scanned = false;
|
|
879
|
+
let attempts = 0;
|
|
878
880
|
let lastConnectError;
|
|
879
|
-
|
|
880
|
-
|
|
881
|
+
while (attempts < MAX_PARENT_ATTEMPTS) {
|
|
882
|
+
let handle = candidates.find((h) => !tried.has(h));
|
|
883
|
+
if (!handle) {
|
|
881
884
|
if (scanned) break;
|
|
882
885
|
scanned = true;
|
|
883
886
|
const found = await transport.scan().catch(() => []);
|
|
@@ -892,9 +895,11 @@ async function deliverOpen(deps, open, opts) {
|
|
|
892
895
|
parentSerial: open.grant.parent_serial
|
|
893
896
|
});
|
|
894
897
|
await cache.forget(parentBleId);
|
|
895
|
-
|
|
898
|
+
handle = candidates.find((h) => !tried.has(h));
|
|
899
|
+
if (!handle) break;
|
|
896
900
|
}
|
|
897
|
-
|
|
901
|
+
tried.add(handle);
|
|
902
|
+
attempts += 1;
|
|
898
903
|
try {
|
|
899
904
|
const result = await attempt(handle);
|
|
900
905
|
if (result) return result;
|
|
@@ -1307,12 +1312,15 @@ var RELAI_RX_CHAR = "0000ff02-0000-1000-8000-00805f9b34fb";
|
|
|
1307
1312
|
var RELAI_TX_CHAR = "0000ff03-0000-1000-8000-00805f9b34fb";
|
|
1308
1313
|
var MIN_MTU = 110;
|
|
1309
1314
|
var NOTIFY_TIMEOUT_MS = 8e3;
|
|
1315
|
+
var CONNECT_TIMEOUT_MS = 1e4;
|
|
1310
1316
|
var BleTransport = class {
|
|
1311
1317
|
constructor(opts) {
|
|
1312
1318
|
this.devices = /* @__PURE__ */ new Map();
|
|
1319
|
+
this.notify = /* @__PURE__ */ new Map();
|
|
1313
1320
|
this.manager = opts.manager;
|
|
1314
1321
|
this.scanImpl = opts.scan;
|
|
1315
1322
|
this.notifyTimeoutMs = opts.notifyTimeoutMs ?? NOTIFY_TIMEOUT_MS;
|
|
1323
|
+
this.connectTimeoutMs = opts.connectTimeoutMs ?? CONNECT_TIMEOUT_MS;
|
|
1316
1324
|
}
|
|
1317
1325
|
/**
|
|
1318
1326
|
* Relai Parents in range.
|
|
@@ -1374,17 +1382,78 @@ var BleTransport = class {
|
|
|
1374
1382
|
}
|
|
1375
1383
|
});
|
|
1376
1384
|
}
|
|
1385
|
+
/**
|
|
1386
|
+
* Connect, discover, and start listening, or leave nothing behind.
|
|
1387
|
+
*
|
|
1388
|
+
* Every step after `connectToDevice` can fail, and a failure here returns no
|
|
1389
|
+
* Session, so NOBODY WILL EVER CALL `disconnect` for this device: the caller
|
|
1390
|
+
* has nothing to pass it. Anything this method set up has to be undone on the
|
|
1391
|
+
* way out or the connection is held until the app restarts. On iOS that is
|
|
1392
|
+
* not merely untidy: `deliverOpen` walks several candidates, and each failed
|
|
1393
|
+
* one would leave a live CoreBluetooth connection behind it.
|
|
1394
|
+
*/
|
|
1377
1395
|
async connect(parentBleId) {
|
|
1378
|
-
let device = await this.manager.connectToDevice(parentBleId);
|
|
1379
|
-
if (this.manager.discoverAllServicesAndCharacteristicsForDevice) {
|
|
1380
|
-
device = await this.manager.discoverAllServicesAndCharacteristicsForDevice(device.id);
|
|
1381
|
-
}
|
|
1396
|
+
let device = await this.manager.connectToDevice(parentBleId, { timeout: this.connectTimeoutMs });
|
|
1382
1397
|
try {
|
|
1383
|
-
|
|
1384
|
-
|
|
1398
|
+
if (this.manager.discoverAllServicesAndCharacteristicsForDevice) {
|
|
1399
|
+
device = await this.manager.discoverAllServicesAndCharacteristicsForDevice(device.id);
|
|
1400
|
+
}
|
|
1401
|
+
try {
|
|
1402
|
+
device = await device.requestMTU(MIN_MTU);
|
|
1403
|
+
} catch {
|
|
1404
|
+
}
|
|
1405
|
+
const previous = this.notify.get(device.id);
|
|
1406
|
+
if (previous) {
|
|
1407
|
+
try {
|
|
1408
|
+
previous.remove();
|
|
1409
|
+
} catch {
|
|
1410
|
+
}
|
|
1411
|
+
this.notify.delete(device.id);
|
|
1412
|
+
}
|
|
1413
|
+
this.devices.set(device.id, device);
|
|
1414
|
+
const state = {
|
|
1415
|
+
waiter: void 0,
|
|
1416
|
+
accept: void 0,
|
|
1417
|
+
failed: void 0,
|
|
1418
|
+
error: void 0,
|
|
1419
|
+
remove: () => void 0
|
|
1420
|
+
};
|
|
1421
|
+
const sub = device.monitorCharacteristicForService(
|
|
1422
|
+
RELAI_UART_SERVICE,
|
|
1423
|
+
RELAI_TX_CHAR,
|
|
1424
|
+
(error, ch) => {
|
|
1425
|
+
if (error) {
|
|
1426
|
+
const err = new exports.RelaiTransportError(`BLE notify error: ${String(error)}`);
|
|
1427
|
+
state.error = err;
|
|
1428
|
+
state.failed?.(err);
|
|
1429
|
+
return;
|
|
1430
|
+
}
|
|
1431
|
+
const value = ch?.value;
|
|
1432
|
+
if (!value) return;
|
|
1433
|
+
let text;
|
|
1434
|
+
try {
|
|
1435
|
+
text = base64ToUtf8(value);
|
|
1436
|
+
} catch {
|
|
1437
|
+
return;
|
|
1438
|
+
}
|
|
1439
|
+
if (state.waiter && state.accept?.(parseNotifyLine(text))) {
|
|
1440
|
+
const resolve = state.waiter;
|
|
1441
|
+
state.waiter = void 0;
|
|
1442
|
+
state.accept = void 0;
|
|
1443
|
+
state.failed = void 0;
|
|
1444
|
+
resolve(text);
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
);
|
|
1448
|
+
state.remove = () => sub.remove();
|
|
1449
|
+
this.notify.set(device.id, state);
|
|
1450
|
+
return { id: device.id, parentBleId, connectedAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
1451
|
+
} catch (err) {
|
|
1452
|
+
this.notify.delete(device.id);
|
|
1453
|
+
this.devices.delete(device.id);
|
|
1454
|
+
await device.cancelConnection().catch(() => void 0);
|
|
1455
|
+
throw err;
|
|
1385
1456
|
}
|
|
1386
|
-
this.devices.set(device.id, device);
|
|
1387
|
-
return { id: device.id, parentBleId, connectedAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
1388
1457
|
}
|
|
1389
1458
|
async requestNonce(session) {
|
|
1390
1459
|
const line = await this.writeAndAwait(session, utf8ToBase64("CHALLENGE"));
|
|
@@ -1409,6 +1478,14 @@ var BleTransport = class {
|
|
|
1409
1478
|
return Promise.resolve({});
|
|
1410
1479
|
}
|
|
1411
1480
|
async disconnect(session) {
|
|
1481
|
+
const state = this.notify.get(session.id);
|
|
1482
|
+
if (state) {
|
|
1483
|
+
try {
|
|
1484
|
+
state.remove();
|
|
1485
|
+
} catch {
|
|
1486
|
+
}
|
|
1487
|
+
this.notify.delete(session.id);
|
|
1488
|
+
}
|
|
1412
1489
|
const device = this.devices.get(session.id);
|
|
1413
1490
|
if (device) {
|
|
1414
1491
|
await device.cancelConnection().catch(() => void 0);
|
|
@@ -1416,51 +1493,55 @@ var BleTransport = class {
|
|
|
1416
1493
|
}
|
|
1417
1494
|
}
|
|
1418
1495
|
/**
|
|
1419
|
-
* Write one value and await the first notify line that matches `accept
|
|
1420
|
-
*
|
|
1421
|
-
*
|
|
1496
|
+
* Write one value and await the first notify line that matches `accept`.
|
|
1497
|
+
*
|
|
1498
|
+
* REGISTER, WRITE, WAIT, in that order, on the session's single subscription.
|
|
1499
|
+
*
|
|
1500
|
+
* The waiter is registered SYNCHRONOUSLY, before the write is issued, so
|
|
1501
|
+
* there is no window in which a reply could arrive unwatched: the notify
|
|
1502
|
+
* callback cannot run before this function returns to the event loop. That is
|
|
1503
|
+
* why there is no buffer of unclaimed lines (see `NotifyState`), and why a
|
|
1504
|
+
* line that arrives with no waiter is dropped rather than kept: the Parent
|
|
1505
|
+
* speaks only in answer to a write, so an unclaimed line is stale, and a
|
|
1506
|
+
* stale `NONCE` carried forward would be minted against and then refused by
|
|
1507
|
+
* the Parent as a replay.
|
|
1508
|
+
*
|
|
1509
|
+
* The subscription itself is opened in `connect` and held for the session, so
|
|
1510
|
+
* the gap between the nonce leg and the command leg is covered too.
|
|
1422
1511
|
*/
|
|
1423
1512
|
writeAndAwait(session, valueBase64, accept = (p) => p.kind !== "passthrough") {
|
|
1424
1513
|
const device = this.devices.get(session.id);
|
|
1425
|
-
|
|
1514
|
+
const state = this.notify.get(session.id);
|
|
1515
|
+
if (!device || !state) return Promise.reject(new exports.RelaiTransportError("Not connected."));
|
|
1516
|
+
if (state.error) return Promise.reject(state.error);
|
|
1517
|
+
if (state.waiter) {
|
|
1518
|
+
return Promise.reject(
|
|
1519
|
+
new exports.RelaiTransportError("A command is already in flight on this Parent session.")
|
|
1520
|
+
);
|
|
1521
|
+
}
|
|
1426
1522
|
return new Promise((resolve, reject) => {
|
|
1427
1523
|
let settled = false;
|
|
1428
|
-
const
|
|
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) => {
|
|
1524
|
+
const finish = (fn) => {
|
|
1459
1525
|
if (settled) return;
|
|
1460
1526
|
settled = true;
|
|
1461
1527
|
clearTimeout(timer);
|
|
1462
|
-
|
|
1463
|
-
|
|
1528
|
+
state.waiter = void 0;
|
|
1529
|
+
state.accept = void 0;
|
|
1530
|
+
state.failed = void 0;
|
|
1531
|
+
fn();
|
|
1532
|
+
};
|
|
1533
|
+
state.accept = accept;
|
|
1534
|
+
state.waiter = (line) => finish(() => resolve(line));
|
|
1535
|
+
state.failed = (err) => finish(() => reject(err));
|
|
1536
|
+
const timer = setTimeout(() => {
|
|
1537
|
+
finish(
|
|
1538
|
+
() => reject(new exports.RelaiTransportError("Timed out waiting for the Parent's notify response."))
|
|
1539
|
+
);
|
|
1540
|
+
}, this.notifyTimeoutMs);
|
|
1541
|
+
device.writeCharacteristicWithResponseForService(RELAI_UART_SERVICE, RELAI_RX_CHAR, valueBase64).then(() => {
|
|
1542
|
+
if (state.error) finish(() => reject(state.error));
|
|
1543
|
+
}).catch((err) => {
|
|
1544
|
+
finish(() => reject(new exports.RelaiTransportError(`BLE write failed: ${String(err)}`)));
|
|
1464
1545
|
});
|
|
1465
1546
|
});
|
|
1466
1547
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1411,12 +1411,16 @@ interface BleTransportOptions {
|
|
|
1411
1411
|
/** Discovery is app-specific (scan is often driven by your own UI); optional. */
|
|
1412
1412
|
scan?: () => Promise<DiscoveredParent[]>;
|
|
1413
1413
|
notifyTimeoutMs?: number;
|
|
1414
|
+
/** Per-candidate connection timeout; see CONNECT_TIMEOUT_MS. */
|
|
1415
|
+
connectTimeoutMs?: number;
|
|
1414
1416
|
}
|
|
1415
1417
|
declare class BleTransport implements ParentTransport {
|
|
1416
1418
|
private readonly manager;
|
|
1417
1419
|
private readonly devices;
|
|
1420
|
+
private readonly notify;
|
|
1418
1421
|
private readonly scanImpl;
|
|
1419
1422
|
private readonly notifyTimeoutMs;
|
|
1423
|
+
private readonly connectTimeoutMs;
|
|
1420
1424
|
constructor(opts: BleTransportOptions);
|
|
1421
1425
|
/**
|
|
1422
1426
|
* Relai Parents in range.
|
|
@@ -1432,15 +1436,37 @@ declare class BleTransport implements ParentTransport {
|
|
|
1432
1436
|
* the command; this ordering only decides who gets asked first.
|
|
1433
1437
|
*/
|
|
1434
1438
|
scan(timeoutMs?: number): Promise<DiscoveredParent[]>;
|
|
1439
|
+
/**
|
|
1440
|
+
* Connect, discover, and start listening, or leave nothing behind.
|
|
1441
|
+
*
|
|
1442
|
+
* Every step after `connectToDevice` can fail, and a failure here returns no
|
|
1443
|
+
* Session, so NOBODY WILL EVER CALL `disconnect` for this device: the caller
|
|
1444
|
+
* has nothing to pass it. Anything this method set up has to be undone on the
|
|
1445
|
+
* way out or the connection is held until the app restarts. On iOS that is
|
|
1446
|
+
* not merely untidy: `deliverOpen` walks several candidates, and each failed
|
|
1447
|
+
* one would leave a live CoreBluetooth connection behind it.
|
|
1448
|
+
*/
|
|
1435
1449
|
connect(parentBleId: string): Promise<Session>;
|
|
1436
1450
|
requestNonce(session: Session): Promise<string>;
|
|
1437
1451
|
sendCommand(session: Session, token: string): Promise<CommandAck>;
|
|
1438
1452
|
readTelemetry(_session: Session): Promise<TowerTelemetry>;
|
|
1439
1453
|
disconnect(session: Session): Promise<void>;
|
|
1440
1454
|
/**
|
|
1441
|
-
* Write one value and await the first notify line that matches `accept
|
|
1442
|
-
*
|
|
1443
|
-
*
|
|
1455
|
+
* Write one value and await the first notify line that matches `accept`.
|
|
1456
|
+
*
|
|
1457
|
+
* REGISTER, WRITE, WAIT, in that order, on the session's single subscription.
|
|
1458
|
+
*
|
|
1459
|
+
* The waiter is registered SYNCHRONOUSLY, before the write is issued, so
|
|
1460
|
+
* there is no window in which a reply could arrive unwatched: the notify
|
|
1461
|
+
* callback cannot run before this function returns to the event loop. That is
|
|
1462
|
+
* why there is no buffer of unclaimed lines (see `NotifyState`), and why a
|
|
1463
|
+
* line that arrives with no waiter is dropped rather than kept: the Parent
|
|
1464
|
+
* speaks only in answer to a write, so an unclaimed line is stale, and a
|
|
1465
|
+
* stale `NONCE` carried forward would be minted against and then refused by
|
|
1466
|
+
* the Parent as a replay.
|
|
1467
|
+
*
|
|
1468
|
+
* The subscription itself is opened in `connect` and held for the session, so
|
|
1469
|
+
* the gap between the nonce leg and the command leg is covered too.
|
|
1444
1470
|
*/
|
|
1445
1471
|
private writeAndAwait;
|
|
1446
1472
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1411,12 +1411,16 @@ interface BleTransportOptions {
|
|
|
1411
1411
|
/** Discovery is app-specific (scan is often driven by your own UI); optional. */
|
|
1412
1412
|
scan?: () => Promise<DiscoveredParent[]>;
|
|
1413
1413
|
notifyTimeoutMs?: number;
|
|
1414
|
+
/** Per-candidate connection timeout; see CONNECT_TIMEOUT_MS. */
|
|
1415
|
+
connectTimeoutMs?: number;
|
|
1414
1416
|
}
|
|
1415
1417
|
declare class BleTransport implements ParentTransport {
|
|
1416
1418
|
private readonly manager;
|
|
1417
1419
|
private readonly devices;
|
|
1420
|
+
private readonly notify;
|
|
1418
1421
|
private readonly scanImpl;
|
|
1419
1422
|
private readonly notifyTimeoutMs;
|
|
1423
|
+
private readonly connectTimeoutMs;
|
|
1420
1424
|
constructor(opts: BleTransportOptions);
|
|
1421
1425
|
/**
|
|
1422
1426
|
* Relai Parents in range.
|
|
@@ -1432,15 +1436,37 @@ declare class BleTransport implements ParentTransport {
|
|
|
1432
1436
|
* the command; this ordering only decides who gets asked first.
|
|
1433
1437
|
*/
|
|
1434
1438
|
scan(timeoutMs?: number): Promise<DiscoveredParent[]>;
|
|
1439
|
+
/**
|
|
1440
|
+
* Connect, discover, and start listening, or leave nothing behind.
|
|
1441
|
+
*
|
|
1442
|
+
* Every step after `connectToDevice` can fail, and a failure here returns no
|
|
1443
|
+
* Session, so NOBODY WILL EVER CALL `disconnect` for this device: the caller
|
|
1444
|
+
* has nothing to pass it. Anything this method set up has to be undone on the
|
|
1445
|
+
* way out or the connection is held until the app restarts. On iOS that is
|
|
1446
|
+
* not merely untidy: `deliverOpen` walks several candidates, and each failed
|
|
1447
|
+
* one would leave a live CoreBluetooth connection behind it.
|
|
1448
|
+
*/
|
|
1435
1449
|
connect(parentBleId: string): Promise<Session>;
|
|
1436
1450
|
requestNonce(session: Session): Promise<string>;
|
|
1437
1451
|
sendCommand(session: Session, token: string): Promise<CommandAck>;
|
|
1438
1452
|
readTelemetry(_session: Session): Promise<TowerTelemetry>;
|
|
1439
1453
|
disconnect(session: Session): Promise<void>;
|
|
1440
1454
|
/**
|
|
1441
|
-
* Write one value and await the first notify line that matches `accept
|
|
1442
|
-
*
|
|
1443
|
-
*
|
|
1455
|
+
* Write one value and await the first notify line that matches `accept`.
|
|
1456
|
+
*
|
|
1457
|
+
* REGISTER, WRITE, WAIT, in that order, on the session's single subscription.
|
|
1458
|
+
*
|
|
1459
|
+
* The waiter is registered SYNCHRONOUSLY, before the write is issued, so
|
|
1460
|
+
* there is no window in which a reply could arrive unwatched: the notify
|
|
1461
|
+
* callback cannot run before this function returns to the event loop. That is
|
|
1462
|
+
* why there is no buffer of unclaimed lines (see `NotifyState`), and why a
|
|
1463
|
+
* line that arrives with no waiter is dropped rather than kept: the Parent
|
|
1464
|
+
* speaks only in answer to a write, so an unclaimed line is stale, and a
|
|
1465
|
+
* stale `NONCE` carried forward would be minted against and then refused by
|
|
1466
|
+
* the Parent as a replay.
|
|
1467
|
+
*
|
|
1468
|
+
* The subscription itself is opened in `connect` and held for the session, so
|
|
1469
|
+
* the gap between the nonce leg and the command leg is covered too.
|
|
1444
1470
|
*/
|
|
1445
1471
|
private writeAndAwait;
|
|
1446
1472
|
}
|
package/dist/index.js
CHANGED
|
@@ -711,10 +711,13 @@ async function deliverOpen(deps, open, opts) {
|
|
|
711
711
|
}).catch(() => void 0);
|
|
712
712
|
}
|
|
713
713
|
let candidates = orderCandidates({ cached: await cache.get(parentBleId), parentBleId });
|
|
714
|
+
const tried = /* @__PURE__ */ new Set();
|
|
714
715
|
let scanned = false;
|
|
716
|
+
let attempts = 0;
|
|
715
717
|
let lastConnectError;
|
|
716
|
-
|
|
717
|
-
|
|
718
|
+
while (attempts < MAX_PARENT_ATTEMPTS) {
|
|
719
|
+
let handle = candidates.find((h) => !tried.has(h));
|
|
720
|
+
if (!handle) {
|
|
718
721
|
if (scanned) break;
|
|
719
722
|
scanned = true;
|
|
720
723
|
const found = await transport.scan().catch(() => []);
|
|
@@ -729,9 +732,11 @@ async function deliverOpen(deps, open, opts) {
|
|
|
729
732
|
parentSerial: open.grant.parent_serial
|
|
730
733
|
});
|
|
731
734
|
await cache.forget(parentBleId);
|
|
732
|
-
|
|
735
|
+
handle = candidates.find((h) => !tried.has(h));
|
|
736
|
+
if (!handle) break;
|
|
733
737
|
}
|
|
734
|
-
|
|
738
|
+
tried.add(handle);
|
|
739
|
+
attempts += 1;
|
|
735
740
|
try {
|
|
736
741
|
const result = await attempt(handle);
|
|
737
742
|
if (result) return result;
|
|
@@ -1137,12 +1142,15 @@ var RELAI_RX_CHAR = "0000ff02-0000-1000-8000-00805f9b34fb";
|
|
|
1137
1142
|
var RELAI_TX_CHAR = "0000ff03-0000-1000-8000-00805f9b34fb";
|
|
1138
1143
|
var MIN_MTU = 110;
|
|
1139
1144
|
var NOTIFY_TIMEOUT_MS = 8e3;
|
|
1145
|
+
var CONNECT_TIMEOUT_MS = 1e4;
|
|
1140
1146
|
var BleTransport = class {
|
|
1141
1147
|
constructor(opts) {
|
|
1142
1148
|
this.devices = /* @__PURE__ */ new Map();
|
|
1149
|
+
this.notify = /* @__PURE__ */ new Map();
|
|
1143
1150
|
this.manager = opts.manager;
|
|
1144
1151
|
this.scanImpl = opts.scan;
|
|
1145
1152
|
this.notifyTimeoutMs = opts.notifyTimeoutMs ?? NOTIFY_TIMEOUT_MS;
|
|
1153
|
+
this.connectTimeoutMs = opts.connectTimeoutMs ?? CONNECT_TIMEOUT_MS;
|
|
1146
1154
|
}
|
|
1147
1155
|
/**
|
|
1148
1156
|
* Relai Parents in range.
|
|
@@ -1204,17 +1212,78 @@ var BleTransport = class {
|
|
|
1204
1212
|
}
|
|
1205
1213
|
});
|
|
1206
1214
|
}
|
|
1215
|
+
/**
|
|
1216
|
+
* Connect, discover, and start listening, or leave nothing behind.
|
|
1217
|
+
*
|
|
1218
|
+
* Every step after `connectToDevice` can fail, and a failure here returns no
|
|
1219
|
+
* Session, so NOBODY WILL EVER CALL `disconnect` for this device: the caller
|
|
1220
|
+
* has nothing to pass it. Anything this method set up has to be undone on the
|
|
1221
|
+
* way out or the connection is held until the app restarts. On iOS that is
|
|
1222
|
+
* not merely untidy: `deliverOpen` walks several candidates, and each failed
|
|
1223
|
+
* one would leave a live CoreBluetooth connection behind it.
|
|
1224
|
+
*/
|
|
1207
1225
|
async connect(parentBleId) {
|
|
1208
|
-
let device = await this.manager.connectToDevice(parentBleId);
|
|
1209
|
-
if (this.manager.discoverAllServicesAndCharacteristicsForDevice) {
|
|
1210
|
-
device = await this.manager.discoverAllServicesAndCharacteristicsForDevice(device.id);
|
|
1211
|
-
}
|
|
1226
|
+
let device = await this.manager.connectToDevice(parentBleId, { timeout: this.connectTimeoutMs });
|
|
1212
1227
|
try {
|
|
1213
|
-
|
|
1214
|
-
|
|
1228
|
+
if (this.manager.discoverAllServicesAndCharacteristicsForDevice) {
|
|
1229
|
+
device = await this.manager.discoverAllServicesAndCharacteristicsForDevice(device.id);
|
|
1230
|
+
}
|
|
1231
|
+
try {
|
|
1232
|
+
device = await device.requestMTU(MIN_MTU);
|
|
1233
|
+
} catch {
|
|
1234
|
+
}
|
|
1235
|
+
const previous = this.notify.get(device.id);
|
|
1236
|
+
if (previous) {
|
|
1237
|
+
try {
|
|
1238
|
+
previous.remove();
|
|
1239
|
+
} catch {
|
|
1240
|
+
}
|
|
1241
|
+
this.notify.delete(device.id);
|
|
1242
|
+
}
|
|
1243
|
+
this.devices.set(device.id, device);
|
|
1244
|
+
const state = {
|
|
1245
|
+
waiter: void 0,
|
|
1246
|
+
accept: void 0,
|
|
1247
|
+
failed: void 0,
|
|
1248
|
+
error: void 0,
|
|
1249
|
+
remove: () => void 0
|
|
1250
|
+
};
|
|
1251
|
+
const sub = device.monitorCharacteristicForService(
|
|
1252
|
+
RELAI_UART_SERVICE,
|
|
1253
|
+
RELAI_TX_CHAR,
|
|
1254
|
+
(error, ch) => {
|
|
1255
|
+
if (error) {
|
|
1256
|
+
const err = new RelaiTransportError(`BLE notify error: ${String(error)}`);
|
|
1257
|
+
state.error = err;
|
|
1258
|
+
state.failed?.(err);
|
|
1259
|
+
return;
|
|
1260
|
+
}
|
|
1261
|
+
const value = ch?.value;
|
|
1262
|
+
if (!value) return;
|
|
1263
|
+
let text;
|
|
1264
|
+
try {
|
|
1265
|
+
text = base64ToUtf8(value);
|
|
1266
|
+
} catch {
|
|
1267
|
+
return;
|
|
1268
|
+
}
|
|
1269
|
+
if (state.waiter && state.accept?.(parseNotifyLine(text))) {
|
|
1270
|
+
const resolve = state.waiter;
|
|
1271
|
+
state.waiter = void 0;
|
|
1272
|
+
state.accept = void 0;
|
|
1273
|
+
state.failed = void 0;
|
|
1274
|
+
resolve(text);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
);
|
|
1278
|
+
state.remove = () => sub.remove();
|
|
1279
|
+
this.notify.set(device.id, state);
|
|
1280
|
+
return { id: device.id, parentBleId, connectedAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
1281
|
+
} catch (err) {
|
|
1282
|
+
this.notify.delete(device.id);
|
|
1283
|
+
this.devices.delete(device.id);
|
|
1284
|
+
await device.cancelConnection().catch(() => void 0);
|
|
1285
|
+
throw err;
|
|
1215
1286
|
}
|
|
1216
|
-
this.devices.set(device.id, device);
|
|
1217
|
-
return { id: device.id, parentBleId, connectedAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
1218
1287
|
}
|
|
1219
1288
|
async requestNonce(session) {
|
|
1220
1289
|
const line = await this.writeAndAwait(session, utf8ToBase64("CHALLENGE"));
|
|
@@ -1239,6 +1308,14 @@ var BleTransport = class {
|
|
|
1239
1308
|
return Promise.resolve({});
|
|
1240
1309
|
}
|
|
1241
1310
|
async disconnect(session) {
|
|
1311
|
+
const state = this.notify.get(session.id);
|
|
1312
|
+
if (state) {
|
|
1313
|
+
try {
|
|
1314
|
+
state.remove();
|
|
1315
|
+
} catch {
|
|
1316
|
+
}
|
|
1317
|
+
this.notify.delete(session.id);
|
|
1318
|
+
}
|
|
1242
1319
|
const device = this.devices.get(session.id);
|
|
1243
1320
|
if (device) {
|
|
1244
1321
|
await device.cancelConnection().catch(() => void 0);
|
|
@@ -1246,51 +1323,55 @@ var BleTransport = class {
|
|
|
1246
1323
|
}
|
|
1247
1324
|
}
|
|
1248
1325
|
/**
|
|
1249
|
-
* Write one value and await the first notify line that matches `accept
|
|
1250
|
-
*
|
|
1251
|
-
*
|
|
1326
|
+
* Write one value and await the first notify line that matches `accept`.
|
|
1327
|
+
*
|
|
1328
|
+
* REGISTER, WRITE, WAIT, in that order, on the session's single subscription.
|
|
1329
|
+
*
|
|
1330
|
+
* The waiter is registered SYNCHRONOUSLY, before the write is issued, so
|
|
1331
|
+
* there is no window in which a reply could arrive unwatched: the notify
|
|
1332
|
+
* callback cannot run before this function returns to the event loop. That is
|
|
1333
|
+
* why there is no buffer of unclaimed lines (see `NotifyState`), and why a
|
|
1334
|
+
* line that arrives with no waiter is dropped rather than kept: the Parent
|
|
1335
|
+
* speaks only in answer to a write, so an unclaimed line is stale, and a
|
|
1336
|
+
* stale `NONCE` carried forward would be minted against and then refused by
|
|
1337
|
+
* the Parent as a replay.
|
|
1338
|
+
*
|
|
1339
|
+
* The subscription itself is opened in `connect` and held for the session, so
|
|
1340
|
+
* the gap between the nonce leg and the command leg is covered too.
|
|
1252
1341
|
*/
|
|
1253
1342
|
writeAndAwait(session, valueBase64, accept = (p) => p.kind !== "passthrough") {
|
|
1254
1343
|
const device = this.devices.get(session.id);
|
|
1255
|
-
|
|
1344
|
+
const state = this.notify.get(session.id);
|
|
1345
|
+
if (!device || !state) return Promise.reject(new RelaiTransportError("Not connected."));
|
|
1346
|
+
if (state.error) return Promise.reject(state.error);
|
|
1347
|
+
if (state.waiter) {
|
|
1348
|
+
return Promise.reject(
|
|
1349
|
+
new RelaiTransportError("A command is already in flight on this Parent session.")
|
|
1350
|
+
);
|
|
1351
|
+
}
|
|
1256
1352
|
return new Promise((resolve, reject) => {
|
|
1257
1353
|
let settled = false;
|
|
1258
|
-
const
|
|
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) => {
|
|
1354
|
+
const finish = (fn) => {
|
|
1289
1355
|
if (settled) return;
|
|
1290
1356
|
settled = true;
|
|
1291
1357
|
clearTimeout(timer);
|
|
1292
|
-
|
|
1293
|
-
|
|
1358
|
+
state.waiter = void 0;
|
|
1359
|
+
state.accept = void 0;
|
|
1360
|
+
state.failed = void 0;
|
|
1361
|
+
fn();
|
|
1362
|
+
};
|
|
1363
|
+
state.accept = accept;
|
|
1364
|
+
state.waiter = (line) => finish(() => resolve(line));
|
|
1365
|
+
state.failed = (err) => finish(() => reject(err));
|
|
1366
|
+
const timer = setTimeout(() => {
|
|
1367
|
+
finish(
|
|
1368
|
+
() => reject(new RelaiTransportError("Timed out waiting for the Parent's notify response."))
|
|
1369
|
+
);
|
|
1370
|
+
}, this.notifyTimeoutMs);
|
|
1371
|
+
device.writeCharacteristicWithResponseForService(RELAI_UART_SERVICE, RELAI_RX_CHAR, valueBase64).then(() => {
|
|
1372
|
+
if (state.error) finish(() => reject(state.error));
|
|
1373
|
+
}).catch((err) => {
|
|
1374
|
+
finish(() => reject(new RelaiTransportError(`BLE write failed: ${String(err)}`)));
|
|
1294
1375
|
});
|
|
1295
1376
|
});
|
|
1296
1377
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@relai-team/access-sdk",
|
|
3
|
-
"version": "0.20.
|
|
3
|
+
"version": "0.20.2",
|
|
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",
|