@attraccess/plugins-backend-sdk 1.10.0 → 1.11.0-beta.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.
Files changed (56) hide show
  1. package/package.json +13 -8
  2. package/src/index.d.ts +8 -0
  3. package/src/index.js +8 -0
  4. package/src/index.js.map +1 -1
  5. package/src/internal.d.ts +6 -0
  6. package/src/internal.js +11 -0
  7. package/src/internal.js.map +1 -0
  8. package/src/lib/auth.types.d.ts +3 -1
  9. package/src/lib/dual-auth.guard.js +4 -0
  10. package/src/lib/dual-auth.guard.js.map +1 -1
  11. package/src/lib/plugin-audit.d.ts +34 -33
  12. package/src/lib/plugin-audit.js +9 -9
  13. package/src/lib/plugin-audit.js.map +1 -1
  14. package/src/lib/plugin-backend-module.d.ts +71 -0
  15. package/src/lib/plugin-backend-module.js +3 -0
  16. package/src/lib/plugin-backend-module.js.map +1 -0
  17. package/src/lib/plugin-ble-fake-clock.d.ts +15 -0
  18. package/src/lib/plugin-ble-fake-clock.js +52 -0
  19. package/src/lib/plugin-ble-fake-clock.js.map +1 -0
  20. package/src/lib/plugin-ble-fake.d.ts +94 -0
  21. package/src/lib/plugin-ble-fake.js +406 -0
  22. package/src/lib/plugin-ble-fake.js.map +1 -0
  23. package/src/lib/plugin-ble-notification-stream.d.ts +28 -0
  24. package/src/lib/plugin-ble-notification-stream.js +119 -0
  25. package/src/lib/plugin-ble-notification-stream.js.map +1 -0
  26. package/src/lib/plugin-ble-session-engine.d.ts +119 -0
  27. package/src/lib/plugin-ble-session-engine.js +410 -0
  28. package/src/lib/plugin-ble-session-engine.js.map +1 -0
  29. package/src/lib/plugin-ble-validation.d.ts +71 -0
  30. package/src/lib/plugin-ble-validation.js +453 -0
  31. package/src/lib/plugin-ble-validation.js.map +1 -0
  32. package/src/lib/plugin-ble.d.ts +249 -0
  33. package/src/lib/plugin-ble.js +85 -0
  34. package/src/lib/plugin-ble.js.map +1 -0
  35. package/src/lib/plugin-context.d.ts +58 -54
  36. package/src/lib/plugin-context.js +6 -6
  37. package/src/lib/plugin-context.js.map +1 -1
  38. package/src/lib/plugin-flow-node.d.ts +52 -3
  39. package/src/lib/plugin-live-updates.d.ts +21 -0
  40. package/src/lib/plugin-live-updates.js +3 -0
  41. package/src/lib/plugin-live-updates.js.map +1 -0
  42. package/src/lib/plugin-notifications.d.ts +83 -0
  43. package/src/lib/plugin-notifications.js +3 -0
  44. package/src/lib/plugin-notifications.js.map +1 -0
  45. package/src/lib/plugin-resource-access.d.ts +89 -0
  46. package/src/lib/plugin-resource-access.js +3 -0
  47. package/src/lib/plugin-resource-access.js.map +1 -0
  48. package/src/lib/plugin.interface.d.ts +92 -5
  49. package/src/lib/plugin.interface.js +28 -1
  50. package/src/lib/plugin.interface.js.map +1 -1
  51. package/src/lib/reader-ui.d.ts +359 -0
  52. package/src/lib/reader-ui.js +115 -0
  53. package/src/lib/reader-ui.js.map +1 -0
  54. package/src/lib/shared-live-sampler.d.ts +11 -0
  55. package/src/lib/shared-live-sampler.js +33 -0
  56. package/src/lib/shared-live-sampler.js.map +1 -0
@@ -0,0 +1,119 @@
1
+ import { PluginBleCallOptions, PluginBleCloseReason, PluginBleNotificationStream, PluginBleProfile, PluginBleSession, PluginBleWriteOptions } from './plugin-ble';
2
+ /**
3
+ * The session state machine shared by the host and the fake transport, so their semantics cannot drift.
4
+ *
5
+ * @internal Building block for the host implementation and `createFakeBleHost`. Plugins never construct it:
6
+ * they receive a `PluginBleSession` from `context.ble`.
7
+ */
8
+ /** A connected peripheral, as the session engine sees it. Receives only validated, normalized primitives. */
9
+ export interface BleSessionLink {
10
+ read(request: {
11
+ serviceUuid: string;
12
+ characteristicUuid: string;
13
+ signal: AbortSignal;
14
+ }): Promise<Uint8Array>;
15
+ write(request: {
16
+ serviceUuid: string;
17
+ characteristicUuid: string;
18
+ value: Uint8Array;
19
+ withoutResponse: boolean;
20
+ signal: AbortSignal;
21
+ }): Promise<void>;
22
+ subscribe(request: {
23
+ serviceUuid: string;
24
+ characteristicUuid: string;
25
+ indicate: boolean;
26
+ signal: AbortSignal;
27
+ onNotification(value: Uint8Array): void;
28
+ }): Promise<void>;
29
+ unsubscribe(request: {
30
+ serviceUuid: string;
31
+ characteristicUuid: string;
32
+ }): Promise<void>;
33
+ disconnect(): Promise<void>;
34
+ /** Registers the callback for a lost link. A link already lost must call it right away. */
35
+ onDisconnected?(listener: () => void): void;
36
+ }
37
+ export interface BleSessionClock {
38
+ now(): number;
39
+ setTimeout(fn: () => void, ms: number): () => void;
40
+ }
41
+ export interface BleSessionEngineDeps {
42
+ id: string;
43
+ endpointId: string;
44
+ profile: PluginBleProfile;
45
+ clock: BleSessionClock;
46
+ connect(signal: AbortSignal, timeoutMs: number): Promise<BleSessionLink>;
47
+ /** Called once the session has fully closed (after the link disconnected or its deadline passed). */
48
+ onClosed(engine: BleSessionEngine): void;
49
+ /** Debug logging of operation, outcome and duration. Never receives payload bytes. */
50
+ log?(operation: string, outcome: string, durationMs: number): void;
51
+ }
52
+ /**
53
+ * Session semantics of the contract: one operation at a time in call order, per-call deadlines, a lease,
54
+ * cancellation, bounded notification queues and exactly-once settlement of every pending promise.
55
+ */
56
+ export declare class BleSessionEngine {
57
+ private readonly deps;
58
+ readonly id: string;
59
+ readonly endpointId: string;
60
+ /** Resolves once the session has fully closed: pending calls are settled and the link is released. */
61
+ readonly closed: Promise<{
62
+ reason: PluginBleCloseReason;
63
+ }>;
64
+ /** What plugins get: a frozen object without the link, the dependencies or any other internals. */
65
+ readonly handle: PluginBleSession;
66
+ private state;
67
+ private link;
68
+ private tail;
69
+ private readonly active;
70
+ private readonly subscriptions;
71
+ /** Keys with a queued or running subscribe, so two concurrent subscribes cannot both succeed. */
72
+ private readonly subscribing;
73
+ /** Streams whose subscribe is still running on the link; they already receive notifications. */
74
+ private readonly starting;
75
+ private resolveClosed;
76
+ private readonly connectController;
77
+ private rejectConnect;
78
+ private cancelLease;
79
+ private detachOpenSignal;
80
+ private closing;
81
+ private closeReason;
82
+ /** Settles once `open()` adopted or released its link. */
83
+ private openSettled;
84
+ /** Settles once the transport's connect call returned and a link that arrived too late was released. */
85
+ private connectWork;
86
+ private connectPending;
87
+ constructor(deps: BleSessionEngineDeps);
88
+ get isClosed(): boolean;
89
+ /** Connects the link. Rejects, with the session closed, when the connect fails, times out or is cancelled. */
90
+ open(options: {
91
+ signal?: AbortSignal;
92
+ leaseMs: number;
93
+ connectTimeoutMs: number;
94
+ }): Promise<void>;
95
+ private connect;
96
+ /** Disconnects a link; never rejects and never waits longer than the disconnect deadline. */
97
+ private release;
98
+ /** Waits for `work`, but never longer than the disconnect deadline. */
99
+ private bounded;
100
+ /** Called by the transport when the link was lost. */
101
+ linkLost(): void;
102
+ close(reason?: PluginBleCloseReason): Promise<void>;
103
+ read(key: string, options?: PluginBleCallOptions): Promise<Uint8Array>;
104
+ write(key: string, value: Uint8Array, options?: PluginBleWriteOptions): Promise<void>;
105
+ subscribe(key: string, options?: {
106
+ signal?: AbortSignal;
107
+ }): Promise<PluginBleNotificationStream>;
108
+ private startSubscription;
109
+ unsubscribe(key: string): Promise<void>;
110
+ private unsubscribeStream;
111
+ /** Removes the subscription of `key` (only if it is `only`, when given) and tells the link. */
112
+ private teardown;
113
+ private deliver;
114
+ /**
115
+ * Queues one operation. `prepare` validates synchronously (before anything is queued or reaches the link) and
116
+ * returns the function that talks to the link.
117
+ */
118
+ private run;
119
+ }
@@ -0,0 +1,410 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BleSessionEngine = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const plugin_ble_1 = require("./plugin-ble");
6
+ const plugin_ble_validation_1 = require("./plugin-ble-validation");
7
+ const plugin_ble_notification_stream_1 = require("./plugin-ble-notification-stream");
8
+ /** How long a close waits for the link to disconnect before the session counts as closed anyway. */
9
+ const DISCONNECT_DEADLINE_MS = plugin_ble_1.PLUGIN_BLE_LIMITS.opTimeoutDefaultMs;
10
+ function closeError(reason, operation) {
11
+ if (reason === 'disconnected')
12
+ return new plugin_ble_1.PluginBleError('DISCONNECTED', 'The device disconnected', { operation });
13
+ if (reason === 'cancelled')
14
+ return new plugin_ble_1.PluginBleError('CANCELLED', 'The session was cancelled', { operation });
15
+ return new plugin_ble_1.PluginBleError('SESSION_CLOSED', 'The session is closed', { operation, detail: reason });
16
+ }
17
+ const cancelled = plugin_ble_notification_stream_1.cancelledBleCall;
18
+ /** Calls `fn` so that a synchronous throw becomes a rejection. */
19
+ function attempt(fn) {
20
+ return Promise.resolve().then(fn);
21
+ }
22
+ /**
23
+ * Session semantics of the contract: one operation at a time in call order, per-call deadlines, a lease,
24
+ * cancellation, bounded notification queues and exactly-once settlement of every pending promise.
25
+ */
26
+ class BleSessionEngine {
27
+ constructor(deps) {
28
+ this.deps = deps;
29
+ this.state = 'connecting';
30
+ this.link = null;
31
+ this.tail = Promise.resolve();
32
+ this.active = new Set();
33
+ this.subscriptions = new Map();
34
+ /** Keys with a queued or running subscribe, so two concurrent subscribes cannot both succeed. */
35
+ this.subscribing = new Set();
36
+ /** Streams whose subscribe is still running on the link; they already receive notifications. */
37
+ this.starting = new Map();
38
+ this.connectController = new AbortController();
39
+ this.rejectConnect = null;
40
+ this.cancelLease = null;
41
+ this.detachOpenSignal = null;
42
+ this.closing = null;
43
+ this.closeReason = 'closed';
44
+ /** Settles once `open()` adopted or released its link. */
45
+ this.openSettled = Promise.resolve();
46
+ /** Settles once the transport's connect call returned and a link that arrived too late was released. */
47
+ this.connectWork = Promise.resolve();
48
+ this.connectPending = false;
49
+ this.id = deps.id;
50
+ this.endpointId = deps.endpointId;
51
+ this.closed = new Promise((resolve) => (this.resolveClosed = resolve));
52
+ this.handle = Object.freeze({
53
+ id: this.id,
54
+ endpointId: this.endpointId,
55
+ closed: this.closed,
56
+ read: (key, options) => this.read(key, options),
57
+ write: (key, value, options) => this.write(key, value, options),
58
+ subscribe: (key, options) => this.subscribe(key, options),
59
+ unsubscribe: (key) => this.unsubscribe(key),
60
+ close: () => this.close(),
61
+ });
62
+ }
63
+ get isClosed() {
64
+ return this.state === 'closed';
65
+ }
66
+ /** Connects the link. Rejects, with the session closed, when the connect fails, times out or is cancelled. */
67
+ open(options) {
68
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
69
+ var _a;
70
+ const { signal } = options;
71
+ if (signal) {
72
+ const onAbort = () => void this.close('cancelled');
73
+ signal.addEventListener('abort', onAbort, { once: true });
74
+ this.detachOpenSignal = () => signal.removeEventListener('abort', onAbort);
75
+ if (signal.aborted)
76
+ onAbort();
77
+ }
78
+ let markOpenSettled;
79
+ this.openSettled = new Promise((resolve) => (markOpenSettled = resolve));
80
+ let link;
81
+ try {
82
+ link = yield this.connect(options.connectTimeoutMs);
83
+ }
84
+ catch (error) {
85
+ const failure = (0, plugin_ble_validation_1.toBleError)(error, 'connect');
86
+ markOpenSettled();
87
+ const closing = this.close(this.state === 'closed' ? this.closeReason : 'error');
88
+ // Report the failure at once. The endpoint stays busy (retryable) until a still-running transport connect
89
+ // returned and its late link was released; otherwise the session is fully closed before we reject.
90
+ if (!this.connectPending)
91
+ yield closing;
92
+ throw failure;
93
+ }
94
+ finally {
95
+ this.rejectConnect = null;
96
+ }
97
+ if (this.state === 'closed') {
98
+ yield this.release(link);
99
+ markOpenSettled();
100
+ yield this.closing;
101
+ throw closeError(this.closeReason, 'connect');
102
+ }
103
+ this.link = link;
104
+ this.state = 'open';
105
+ this.cancelLease = this.deps.clock.setTimeout(() => void this.close('lease-expired'), options.leaseMs);
106
+ try {
107
+ (_a = link.onDisconnected) === null || _a === void 0 ? void 0 : _a.call(link, () => this.linkLost());
108
+ }
109
+ catch (error) {
110
+ markOpenSettled();
111
+ yield this.close('error');
112
+ throw (0, plugin_ble_validation_1.toBleError)(error, 'connect');
113
+ }
114
+ markOpenSettled();
115
+ });
116
+ }
117
+ connect(timeoutMs) {
118
+ return new Promise((resolve, reject) => {
119
+ if (this.state === 'closed')
120
+ return reject(closeError(this.closeReason, 'connect'));
121
+ let settled = false;
122
+ const fail = (error) => {
123
+ settled = true;
124
+ reject(error);
125
+ };
126
+ this.rejectConnect = fail;
127
+ const cancelTimer = this.deps.clock.setTimeout(() => {
128
+ this.connectController.abort();
129
+ fail(new plugin_ble_1.PluginBleError('TIMEOUT', 'Connecting timed out', { operation: 'connect' }));
130
+ }, timeoutMs);
131
+ this.connectPending = true;
132
+ this.connectWork = attempt(() => this.deps.connect(this.connectController.signal, timeoutMs))
133
+ .then((value) => {
134
+ cancelTimer();
135
+ // A link that arrives after the connect already failed must not be leaked.
136
+ if (settled)
137
+ return this.release(value).then(() => void (this.connectPending = false));
138
+ this.connectPending = false;
139
+ settled = true;
140
+ resolve(value);
141
+ }, (error) => {
142
+ cancelTimer();
143
+ this.connectPending = false;
144
+ if (!settled)
145
+ fail((0, plugin_ble_validation_1.toBleError)(error, 'connect'));
146
+ })
147
+ .catch(() => undefined);
148
+ });
149
+ }
150
+ /** Disconnects a link; never rejects and never waits longer than the disconnect deadline. */
151
+ release(link) {
152
+ return new Promise((resolve) => {
153
+ const cancelTimer = this.deps.clock.setTimeout(resolve, DISCONNECT_DEADLINE_MS);
154
+ attempt(() => link.disconnect())
155
+ .catch(() => undefined)
156
+ .then(() => (cancelTimer(), resolve()));
157
+ });
158
+ }
159
+ /** Waits for `work`, but never longer than the disconnect deadline. */
160
+ bounded(work) {
161
+ return new Promise((resolve) => {
162
+ const cancelTimer = this.deps.clock.setTimeout(resolve, DISCONNECT_DEADLINE_MS);
163
+ void work.then(() => (cancelTimer(), resolve()));
164
+ });
165
+ }
166
+ /** Called by the transport when the link was lost. */
167
+ linkLost() {
168
+ void this.close('disconnected');
169
+ }
170
+ close(reason = 'closed') {
171
+ var _a, _b, _c;
172
+ if (this.closing)
173
+ return this.closing;
174
+ let finish;
175
+ // Set first, so a close triggered from inside this one (e.g. a driver reporting the link lost) is a no-op.
176
+ this.closing = new Promise((resolve) => (finish = resolve));
177
+ this.state = 'closed';
178
+ this.closeReason = reason;
179
+ (_a = this.cancelLease) === null || _a === void 0 ? void 0 : _a.call(this);
180
+ (_b = this.detachOpenSignal) === null || _b === void 0 ? void 0 : _b.call(this);
181
+ this.connectController.abort();
182
+ (_c = this.rejectConnect) === null || _c === void 0 ? void 0 : _c.call(this, closeError(reason, 'connect'));
183
+ for (const operation of [...this.active]) {
184
+ operation.controller.abort();
185
+ operation.fail(closeError(reason, 'operation'));
186
+ }
187
+ for (const { stream } of this.subscriptions.values())
188
+ stream.end(closeError(reason, 'subscribe'));
189
+ for (const stream of this.starting.values())
190
+ stream.end(closeError(reason, 'subscribe'));
191
+ this.subscriptions.clear();
192
+ this.starting.clear();
193
+ this.subscribing.clear();
194
+ const link = this.link;
195
+ this.link = null;
196
+ void (() => tslib_1.__awaiter(this, void 0, void 0, function* () {
197
+ if (link && reason !== 'disconnected')
198
+ yield this.release(link);
199
+ yield this.openSettled;
200
+ yield this.bounded(this.connectWork);
201
+ }))().then(() => {
202
+ // Only now is the endpoint free again, so a reopen never overlaps the old link's disconnect.
203
+ try {
204
+ this.deps.onClosed(this);
205
+ }
206
+ catch (_a) {
207
+ // A failing callback must not keep the session from closing.
208
+ }
209
+ finally {
210
+ this.resolveClosed({ reason });
211
+ finish();
212
+ }
213
+ });
214
+ return this.closing;
215
+ }
216
+ read(key, options = {}) {
217
+ return this.run('read', options, () => {
218
+ const target = (0, plugin_ble_validation_1.prepareBleRead)(this.deps.profile, key);
219
+ return (signal, link) => tslib_1.__awaiter(this, void 0, void 0, function* () {
220
+ const value = yield link.read({
221
+ serviceUuid: target.serviceUuid,
222
+ characteristicUuid: target.characteristic.uuid,
223
+ signal,
224
+ });
225
+ return (0, plugin_ble_validation_1.checkBleInboundPayload)(target.characteristic, 'read', value);
226
+ });
227
+ });
228
+ }
229
+ write(key, value, options = {}) {
230
+ return this.run('write', options, () => {
231
+ const prepared = (0, plugin_ble_validation_1.prepareBleWrite)(this.deps.profile, key, value, options.withoutResponse === true);
232
+ return (signal, link) => link.write({
233
+ serviceUuid: prepared.serviceUuid,
234
+ characteristicUuid: prepared.characteristic.uuid,
235
+ value: prepared.value,
236
+ withoutResponse: prepared.withoutResponse,
237
+ signal,
238
+ });
239
+ });
240
+ }
241
+ subscribe(key, options = {}) {
242
+ let reserved = false;
243
+ const result = this.run('subscribe', options, () => {
244
+ const target = (0, plugin_ble_validation_1.prepareBleSubscribe)(this.deps.profile, key);
245
+ if (this.subscriptions.has(key) || this.subscribing.has(key)) {
246
+ throw new plugin_ble_1.PluginBleError('INVALID_ARGUMENT', `"${key}" is already subscribed`, { operation: 'subscribe' });
247
+ }
248
+ this.subscribing.add(key);
249
+ reserved = true;
250
+ return (signal, link) => this.startSubscription(key, target, signal, link);
251
+ });
252
+ if (reserved) {
253
+ const release = () => void this.subscribing.delete(key);
254
+ result.then(release, release);
255
+ }
256
+ return result;
257
+ }
258
+ startSubscription(key, target, signal, link) {
259
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
260
+ const stream = new plugin_ble_notification_stream_1.BleNotificationStream(key, this.deps.profile.limits.maxNotificationQueue, this.deps.clock, () => this.unsubscribeStream(key, stream));
261
+ const ref = { serviceUuid: target.serviceUuid, characteristicUuid: target.characteristic.uuid };
262
+ // Registered only once the link confirmed it, so a timed-out subscribe never blocks a retry.
263
+ this.starting.set(key, stream);
264
+ try {
265
+ yield link.subscribe(Object.assign(Object.assign({}, ref), { indicate: target.indicate, signal, onNotification: (bytes) => this.deliver(key, stream, target, bytes) }));
266
+ }
267
+ finally {
268
+ if (this.starting.get(key) === stream)
269
+ this.starting.delete(key);
270
+ }
271
+ if (signal.aborted || this.state !== 'open') {
272
+ // Timed out or cancelled while the link call ran: nobody holds this stream. Undo it on the link unless a
273
+ // newer subscribe for the key already took over.
274
+ stream.end(cancelled('next'));
275
+ if (this.link === link && !this.subscriptions.has(key) && !this.starting.has(key))
276
+ yield link.unsubscribe(ref);
277
+ throw cancelled('subscribe');
278
+ }
279
+ // A stream that already failed (overflow during the start) was torn down on the link by `deliver`.
280
+ if (!stream.ended)
281
+ this.subscriptions.set(key, { stream, target });
282
+ return stream.handle;
283
+ });
284
+ }
285
+ unsubscribe(key) {
286
+ return this.run('unsubscribe', {}, () => {
287
+ // Unknown or non-notifying keys are rejected; a valid key without a subscription is a no-op.
288
+ (0, plugin_ble_validation_1.prepareBleSubscribe)(this.deps.profile, key);
289
+ // Looked up when the operation runs, so it applies to a subscribe queued before it.
290
+ return (_signal, link) => this.teardown(key, null, link);
291
+ });
292
+ }
293
+ unsubscribeStream(key, stream) {
294
+ if (this.state !== 'open')
295
+ return Promise.resolve();
296
+ return this.run('unsubscribe', {}, () => (_signal, link) => this.teardown(key, stream, link));
297
+ }
298
+ /** Removes the subscription of `key` (only if it is `only`, when given) and tells the link. */
299
+ teardown(key, only, link) {
300
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
301
+ const subscription = this.subscriptions.get(key);
302
+ if (!subscription || (only && subscription.stream !== only))
303
+ return;
304
+ this.subscriptions.delete(key);
305
+ subscription.stream.end(cancelled('next'));
306
+ yield (link === null || link === void 0 ? void 0 : link.unsubscribe({
307
+ serviceUuid: subscription.target.serviceUuid,
308
+ characteristicUuid: subscription.target.characteristic.uuid,
309
+ }));
310
+ });
311
+ }
312
+ deliver(key, stream, target, bytes) {
313
+ var _a;
314
+ if (((_a = this.subscriptions.get(key)) === null || _a === void 0 ? void 0 : _a.stream) !== stream && this.starting.get(key) !== stream)
315
+ return;
316
+ try {
317
+ stream.push((0, plugin_ble_validation_1.checkBleInboundPayload)(target.characteristic, 'notification', bytes));
318
+ }
319
+ catch (error) {
320
+ stream.end((0, plugin_ble_validation_1.toBleError)(error, 'subscribe'));
321
+ }
322
+ if (stream.ended) {
323
+ this.subscriptions.delete(key);
324
+ // Overflow tears the subscription down on the link as well.
325
+ const link = this.link;
326
+ if (link) {
327
+ const ref = { serviceUuid: target.serviceUuid, characteristicUuid: target.characteristic.uuid };
328
+ void attempt(() => link.unsubscribe(ref)).catch(() => undefined);
329
+ }
330
+ }
331
+ }
332
+ /**
333
+ * Queues one operation. `prepare` validates synchronously (before anything is queued or reaches the link) and
334
+ * returns the function that talks to the link.
335
+ */
336
+ run(operation, options, prepare) {
337
+ var _a;
338
+ let execute;
339
+ let timeoutMs;
340
+ try {
341
+ if (this.state !== 'open') {
342
+ throw new plugin_ble_1.PluginBleError('SESSION_CLOSED', 'The session is closed', { operation, detail: this.closeReason });
343
+ }
344
+ timeoutMs = (0, plugin_ble_validation_1.resolveBleTimeout)(options.timeoutMs, plugin_ble_1.PLUGIN_BLE_LIMITS.opTimeoutDefaultMs, plugin_ble_1.PLUGIN_BLE_LIMITS.opTimeoutMaxMs);
345
+ execute = prepare();
346
+ if ((_a = options.signal) === null || _a === void 0 ? void 0 : _a.aborted)
347
+ throw cancelled(operation);
348
+ }
349
+ catch (error) {
350
+ return Promise.reject(error);
351
+ }
352
+ return new Promise((resolve, reject) => {
353
+ var _a;
354
+ let cancelTimer;
355
+ let finished;
356
+ const done = new Promise((resolveDone) => (finished = resolveDone));
357
+ const startedAt = this.deps.clock.now();
358
+ const op = {
359
+ settled: false,
360
+ controller: new AbortController(),
361
+ fail: (error) => settle(() => reject(error), error.code),
362
+ };
363
+ const settle = (complete, outcome) => {
364
+ var _a, _b, _c;
365
+ if (op.settled)
366
+ return;
367
+ op.settled = true;
368
+ cancelTimer === null || cancelTimer === void 0 ? void 0 : cancelTimer();
369
+ (_a = options.signal) === null || _a === void 0 ? void 0 : _a.removeEventListener('abort', onAbort);
370
+ this.active.delete(op);
371
+ finished();
372
+ complete();
373
+ try {
374
+ (_c = (_b = this.deps).log) === null || _c === void 0 ? void 0 : _c.call(_b, operation, outcome, this.deps.clock.now() - startedAt);
375
+ }
376
+ catch (_d) {
377
+ // Logging never changes the outcome of an operation.
378
+ }
379
+ };
380
+ const onAbort = () => {
381
+ op.controller.abort();
382
+ op.fail(cancelled(operation));
383
+ };
384
+ (_a = options.signal) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', onAbort, { once: true });
385
+ this.active.add(op);
386
+ const step = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
387
+ const link = this.link;
388
+ if (op.settled || !link)
389
+ return;
390
+ cancelTimer = this.deps.clock.setTimeout(() => {
391
+ op.controller.abort();
392
+ op.fail(new plugin_ble_1.PluginBleError('TIMEOUT', `${operation} timed out`, { operation }));
393
+ }, timeoutMs);
394
+ const call = attempt(() => execute(op.controller.signal, link)).then((value) => settle(() => resolve(value), 'ok'), (error) => {
395
+ const failure = (0, plugin_ble_validation_1.toBleError)(error, operation);
396
+ // The driver reporting a lost link ends the session; timeouts and cancellations do not.
397
+ if (failure.code === 'DISCONNECTED')
398
+ void this.close('disconnected');
399
+ op.fail(failure);
400
+ });
401
+ // A timed-out or cancelled operation must not block the queue behind a hung link call.
402
+ yield Promise.race([call, done]);
403
+ });
404
+ // The catch keeps the chain usable whatever happens in a step.
405
+ this.tail = this.tail.then(step).catch(() => undefined);
406
+ });
407
+ }
408
+ }
409
+ exports.BleSessionEngine = BleSessionEngine;
410
+ //# sourceMappingURL=plugin-ble-session-engine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin-ble-session-engine.js","sourceRoot":"","sources":["../../../../../libs/plugins-backend-sdk/src/lib/plugin-ble-session-engine.ts"],"names":[],"mappings":";;;;AAAA,6CASsB;AACtB,mEAOiC;AACjC,qFAA2F;AAiD3F,oGAAoG;AACpG,MAAM,sBAAsB,GAAG,8BAAiB,CAAC,kBAAkB,CAAC;AAWpE,SAAS,UAAU,CAAC,MAA4B,EAAE,SAAiB;IACjE,IAAI,MAAM,KAAK,cAAc;QAAE,OAAO,IAAI,2BAAc,CAAC,cAAc,EAAE,yBAAyB,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;IACnH,IAAI,MAAM,KAAK,WAAW;QAAE,OAAO,IAAI,2BAAc,CAAC,WAAW,EAAE,2BAA2B,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;IAC/G,OAAO,IAAI,2BAAc,CAAC,gBAAgB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Y,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;gBAClF,CAAC,EAAE,SAAS,CAAC,CAAC;gBACd,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAClE,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,EAC7C,CAAC,KAAK,EAAE,EAAE;oBACR,MAAM,OAAO,GAAG,IAAA,kCAAU,EAAC,KAAK,EAAE,SAAS,CAAC,CAAC;oBAC7C,wFAAwF;oBACxF,IAAI,OAAO,CAAC,IAAI,KAAK,cAAc;wBAAE,KAAK,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;oBACrE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBACnB,CAAC,CACF,CAAC;gBACF,uFAAuF;gBACvF,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;YACnC,CAAC,CAAA,CAAC;YACF,+DAA+D;YAC/D,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;QAC1D,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA5YD,4CA4YC"}
@@ -0,0 +1,71 @@
1
+ import { PluginBleAddressType, PluginBleAdvertisement, PluginBleCharacteristic, PluginBleEndpointInput, PluginBleEndpointUpdate, PluginBleError, PluginBleErrorCode, PluginBleProfile, PluginBleProfileDefinition } from './plugin-ble';
2
+ type ErrorCode = typeof PluginBleErrorCode.INVALID_PROFILE | typeof PluginBleErrorCode.INVALID_ARGUMENT;
3
+ export declare function isBleBytes(value: unknown): value is Uint8Array;
4
+ /**
5
+ * Returns a private copy of real `Uint8Array` bytes, or `null` for anything else. The copy is taken through the
6
+ * typed-array constructor, which reads the internal slots and never calls plugin-overridable getters or
7
+ * iterators, so bounds are always checked on exactly the bytes that are passed on.
8
+ */
9
+ export declare function copyBleBytes(value: unknown): Uint8Array | null;
10
+ /** Normalizes a 16-, 32- or 128-bit UUID to lowercase 128-bit with dashes. */
11
+ export declare function normalizeBleUuid(value: unknown, field?: string, code?: ErrorCode): string;
12
+ /** Normalizes `xx:xx:xx:xx:xx:xx` to uppercase. */
13
+ export declare function normalizeBleAddress(value: unknown, field?: string): string;
14
+ export declare function normalizeBleAddressType(value: unknown, field?: string): PluginBleAddressType;
15
+ export declare function validateBleReaderId(value: unknown, field?: string): number;
16
+ export declare function validateBleProfileId(value: unknown, field?: string, code?: ErrorCode): string;
17
+ export declare function deepFreezeBle<T>(value: T): T;
18
+ /** Validates and normalizes a profile definition; the result is deep-frozen. */
19
+ export declare function normalizeBleProfile(definition: PluginBleProfileDefinition): PluginBleProfile;
20
+ export declare function bleProfilesEqual(a: PluginBleProfile, b: PluginBleProfile): boolean;
21
+ export declare function findBleCharacteristic(profile: PluginBleProfile, key: string): {
22
+ serviceUuid: string;
23
+ characteristic: PluginBleCharacteristic;
24
+ } | undefined;
25
+ export interface BleCharacteristicTarget {
26
+ serviceUuid: string;
27
+ characteristic: PluginBleCharacteristic;
28
+ }
29
+ export declare function prepareBleRead(profile: PluginBleProfile, key: unknown): BleCharacteristicTarget;
30
+ export declare function prepareBleWrite(profile: PluginBleProfile, key: unknown, value: unknown, withoutResponse?: boolean): BleCharacteristicTarget & {
31
+ value: Uint8Array;
32
+ withoutResponse: boolean;
33
+ };
34
+ export declare function prepareBleSubscribe(profile: PluginBleProfile, key: unknown): BleCharacteristicTarget & {
35
+ indicate: boolean;
36
+ };
37
+ /** Rejects a read result or notification above the profile bound. Returns a private copy of the bytes. */
38
+ export declare function checkBleInboundPayload(characteristic: PluginBleCharacteristic, kind: 'read' | 'notification', value: Uint8Array): Uint8Array;
39
+ export declare function resolveBleTimeout(value: unknown, defaultMs: number, maxMs: number, field?: string): number;
40
+ export declare function resolveBleScanDuration(value: unknown): number;
41
+ export declare function resolveBleSessionOptions(profile: PluginBleProfile, options: {
42
+ leaseMs?: unknown;
43
+ connectTimeoutMs?: unknown;
44
+ } | undefined): {
45
+ leaseMs: number;
46
+ connectTimeoutMs: number;
47
+ };
48
+ export declare function normalizeBleEndpointInput(input: PluginBleEndpointInput): {
49
+ readerId: number;
50
+ address: string;
51
+ addressType: PluginBleAddressType;
52
+ profileId: string;
53
+ label: string;
54
+ };
55
+ export declare function normalizeBleEndpointUpdate(update: PluginBleEndpointUpdate): {
56
+ readerId?: number;
57
+ address?: string;
58
+ addressType?: PluginBleAddressType;
59
+ label?: string | null;
60
+ };
61
+ export declare function validateBleEndpointId(value: unknown): string;
62
+ /** Normalizes a raw advertisement coming from a driver (address, UUIDs, private byte copies). */
63
+ export declare function normalizeBleAdvertisement(raw: PluginBleAdvertisement): PluginBleAdvertisement;
64
+ /**
65
+ * True when the advertisement matches every criterion the profile filter sets: any of its service UUIDs, any of
66
+ * its manufacturer ids, and the name prefix. A profile without a filter matches nothing.
67
+ */
68
+ export declare function matchesBleAdvertisement(profile: PluginBleProfile, advertisement: PluginBleAdvertisement): boolean;
69
+ /** Wraps anything that is not a `PluginBleError` in `INTERNAL` without copying its message (it may carry data). */
70
+ export declare function toBleError(error: unknown, operation: string): PluginBleError;
71
+ export {};