@relai-team/access-sdk 0.20.1 → 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/dist/index.cjs +88 -54
- package/dist/index.d.cts +24 -9
- package/dist/index.d.ts +24 -9
- package/dist/index.js +88 -54
- package/package.json +1 -1
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,6 +1312,7 @@ 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();
|
|
@@ -1314,6 +1320,7 @@ var BleTransport = class {
|
|
|
1314
1320
|
this.manager = opts.manager;
|
|
1315
1321
|
this.scanImpl = opts.scan;
|
|
1316
1322
|
this.notifyTimeoutMs = opts.notifyTimeoutMs ?? NOTIFY_TIMEOUT_MS;
|
|
1323
|
+
this.connectTimeoutMs = opts.connectTimeoutMs ?? CONNECT_TIMEOUT_MS;
|
|
1317
1324
|
}
|
|
1318
1325
|
/**
|
|
1319
1326
|
* Relai Parents in range.
|
|
@@ -1375,53 +1382,78 @@ var BleTransport = class {
|
|
|
1375
1382
|
}
|
|
1376
1383
|
});
|
|
1377
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
|
+
*/
|
|
1378
1395
|
async connect(parentBleId) {
|
|
1379
|
-
let device = await this.manager.connectToDevice(parentBleId);
|
|
1380
|
-
if (this.manager.discoverAllServicesAndCharacteristicsForDevice) {
|
|
1381
|
-
device = await this.manager.discoverAllServicesAndCharacteristicsForDevice(device.id);
|
|
1382
|
-
}
|
|
1396
|
+
let device = await this.manager.connectToDevice(parentBleId, { timeout: this.connectTimeoutMs });
|
|
1383
1397
|
try {
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
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;
|
|
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) {
|
|
1408
1407
|
try {
|
|
1409
|
-
|
|
1408
|
+
previous.remove();
|
|
1410
1409
|
} 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
1410
|
}
|
|
1411
|
+
this.notify.delete(device.id);
|
|
1420
1412
|
}
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
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;
|
|
1456
|
+
}
|
|
1425
1457
|
}
|
|
1426
1458
|
async requestNonce(session) {
|
|
1427
1459
|
const line = await this.writeAndAwait(session, utf8ToBase64("CHALLENGE"));
|
|
@@ -1463,17 +1495,19 @@ var BleTransport = class {
|
|
|
1463
1495
|
/**
|
|
1464
1496
|
* Write one value and await the first notify line that matches `accept`.
|
|
1465
1497
|
*
|
|
1466
|
-
*
|
|
1498
|
+
* REGISTER, WRITE, WAIT, in that order, on the session's single subscription.
|
|
1467
1499
|
*
|
|
1468
|
-
*
|
|
1469
|
-
*
|
|
1470
|
-
*
|
|
1471
|
-
*
|
|
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.
|
|
1472
1508
|
*
|
|
1473
|
-
*
|
|
1474
|
-
*
|
|
1475
|
-
* between the nonce leg and the command leg is covered: there is no moment in
|
|
1476
|
-
* the session when nobody is listening.
|
|
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.
|
|
1477
1511
|
*/
|
|
1478
1512
|
writeAndAwait(session, valueBase64, accept = (p) => p.kind !== "passthrough") {
|
|
1479
1513
|
const device = this.devices.get(session.id);
|
package/dist/index.d.cts
CHANGED
|
@@ -1411,6 +1411,8 @@ 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;
|
|
@@ -1418,6 +1420,7 @@ declare class BleTransport implements ParentTransport {
|
|
|
1418
1420
|
private readonly notify;
|
|
1419
1421
|
private readonly scanImpl;
|
|
1420
1422
|
private readonly notifyTimeoutMs;
|
|
1423
|
+
private readonly connectTimeoutMs;
|
|
1421
1424
|
constructor(opts: BleTransportOptions);
|
|
1422
1425
|
/**
|
|
1423
1426
|
* Relai Parents in range.
|
|
@@ -1433,6 +1436,16 @@ declare class BleTransport implements ParentTransport {
|
|
|
1433
1436
|
* the command; this ordering only decides who gets asked first.
|
|
1434
1437
|
*/
|
|
1435
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
|
+
*/
|
|
1436
1449
|
connect(parentBleId: string): Promise<Session>;
|
|
1437
1450
|
requestNonce(session: Session): Promise<string>;
|
|
1438
1451
|
sendCommand(session: Session, token: string): Promise<CommandAck>;
|
|
@@ -1441,17 +1454,19 @@ declare class BleTransport implements ParentTransport {
|
|
|
1441
1454
|
/**
|
|
1442
1455
|
* Write one value and await the first notify line that matches `accept`.
|
|
1443
1456
|
*
|
|
1444
|
-
*
|
|
1457
|
+
* REGISTER, WRITE, WAIT, in that order, on the session's single subscription.
|
|
1445
1458
|
*
|
|
1446
|
-
*
|
|
1447
|
-
*
|
|
1448
|
-
*
|
|
1449
|
-
*
|
|
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.
|
|
1450
1467
|
*
|
|
1451
|
-
*
|
|
1452
|
-
*
|
|
1453
|
-
* between the nonce leg and the command leg is covered: there is no moment in
|
|
1454
|
-
* the session when nobody is listening.
|
|
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.
|
|
1455
1470
|
*/
|
|
1456
1471
|
private writeAndAwait;
|
|
1457
1472
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1411,6 +1411,8 @@ 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;
|
|
@@ -1418,6 +1420,7 @@ declare class BleTransport implements ParentTransport {
|
|
|
1418
1420
|
private readonly notify;
|
|
1419
1421
|
private readonly scanImpl;
|
|
1420
1422
|
private readonly notifyTimeoutMs;
|
|
1423
|
+
private readonly connectTimeoutMs;
|
|
1421
1424
|
constructor(opts: BleTransportOptions);
|
|
1422
1425
|
/**
|
|
1423
1426
|
* Relai Parents in range.
|
|
@@ -1433,6 +1436,16 @@ declare class BleTransport implements ParentTransport {
|
|
|
1433
1436
|
* the command; this ordering only decides who gets asked first.
|
|
1434
1437
|
*/
|
|
1435
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
|
+
*/
|
|
1436
1449
|
connect(parentBleId: string): Promise<Session>;
|
|
1437
1450
|
requestNonce(session: Session): Promise<string>;
|
|
1438
1451
|
sendCommand(session: Session, token: string): Promise<CommandAck>;
|
|
@@ -1441,17 +1454,19 @@ declare class BleTransport implements ParentTransport {
|
|
|
1441
1454
|
/**
|
|
1442
1455
|
* Write one value and await the first notify line that matches `accept`.
|
|
1443
1456
|
*
|
|
1444
|
-
*
|
|
1457
|
+
* REGISTER, WRITE, WAIT, in that order, on the session's single subscription.
|
|
1445
1458
|
*
|
|
1446
|
-
*
|
|
1447
|
-
*
|
|
1448
|
-
*
|
|
1449
|
-
*
|
|
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.
|
|
1450
1467
|
*
|
|
1451
|
-
*
|
|
1452
|
-
*
|
|
1453
|
-
* between the nonce leg and the command leg is covered: there is no moment in
|
|
1454
|
-
* the session when nobody is listening.
|
|
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.
|
|
1455
1470
|
*/
|
|
1456
1471
|
private writeAndAwait;
|
|
1457
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,6 +1142,7 @@ 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();
|
|
@@ -1144,6 +1150,7 @@ var BleTransport = class {
|
|
|
1144
1150
|
this.manager = opts.manager;
|
|
1145
1151
|
this.scanImpl = opts.scan;
|
|
1146
1152
|
this.notifyTimeoutMs = opts.notifyTimeoutMs ?? NOTIFY_TIMEOUT_MS;
|
|
1153
|
+
this.connectTimeoutMs = opts.connectTimeoutMs ?? CONNECT_TIMEOUT_MS;
|
|
1147
1154
|
}
|
|
1148
1155
|
/**
|
|
1149
1156
|
* Relai Parents in range.
|
|
@@ -1205,53 +1212,78 @@ var BleTransport = class {
|
|
|
1205
1212
|
}
|
|
1206
1213
|
});
|
|
1207
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
|
+
*/
|
|
1208
1225
|
async connect(parentBleId) {
|
|
1209
|
-
let device = await this.manager.connectToDevice(parentBleId);
|
|
1210
|
-
if (this.manager.discoverAllServicesAndCharacteristicsForDevice) {
|
|
1211
|
-
device = await this.manager.discoverAllServicesAndCharacteristicsForDevice(device.id);
|
|
1212
|
-
}
|
|
1226
|
+
let device = await this.manager.connectToDevice(parentBleId, { timeout: this.connectTimeoutMs });
|
|
1213
1227
|
try {
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
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;
|
|
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) {
|
|
1238
1237
|
try {
|
|
1239
|
-
|
|
1238
|
+
previous.remove();
|
|
1240
1239
|
} 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
1240
|
}
|
|
1241
|
+
this.notify.delete(device.id);
|
|
1250
1242
|
}
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
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;
|
|
1286
|
+
}
|
|
1255
1287
|
}
|
|
1256
1288
|
async requestNonce(session) {
|
|
1257
1289
|
const line = await this.writeAndAwait(session, utf8ToBase64("CHALLENGE"));
|
|
@@ -1293,17 +1325,19 @@ var BleTransport = class {
|
|
|
1293
1325
|
/**
|
|
1294
1326
|
* Write one value and await the first notify line that matches `accept`.
|
|
1295
1327
|
*
|
|
1296
|
-
*
|
|
1328
|
+
* REGISTER, WRITE, WAIT, in that order, on the session's single subscription.
|
|
1297
1329
|
*
|
|
1298
|
-
*
|
|
1299
|
-
*
|
|
1300
|
-
*
|
|
1301
|
-
*
|
|
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.
|
|
1302
1338
|
*
|
|
1303
|
-
*
|
|
1304
|
-
*
|
|
1305
|
-
* between the nonce leg and the command leg is covered: there is no moment in
|
|
1306
|
-
* the session when nobody is listening.
|
|
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.
|
|
1307
1341
|
*/
|
|
1308
1342
|
writeAndAwait(session, valueBase64, accept = (p) => p.kind !== "passthrough") {
|
|
1309
1343
|
const device = this.devices.get(session.id);
|
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",
|