simplejsble 1.3.1-dev.25 → 1.3.1-dev.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -141,7 +141,9 @@ Dongl for other programs. Adapters also support `await using`.
141
141
  first call that reaches a backend. Pairing through a Dongl happens without the
142
142
  operating system: set `setPasskeyRequestHandler`, `setPasskeyDisplayHandler`, or
143
143
  `setNumericComparisonHandler` on the peripheral, and await the promise it
144
- returns, before `connect()`.
144
+ returns, before `connect()`. On Linux, the same handlers on an adapter, plus
145
+ `setPairingAuthorizationHandler`, answer BlueZ's pairing requests; set
146
+ `Config.simpleBluez.interactivePairing` for passkeys and numeric comparison.
145
147
 
146
148
  ## Electron
147
149
 
package/dist/adapter.d.ts CHANGED
@@ -49,15 +49,37 @@ export declare class Adapter extends EventEmitter<AdapterEvents> {
49
49
  * reject with OPERATION_NOT_SUPPORTED.
50
50
  */
51
51
  retrieveCachedPeripherals(identifiers: readonly string[]): Promise<Peripheral[]>;
52
+ /**
53
+ * Answers a request for the passkey a peer shows while it pairs through this
54
+ * adapter on Linux, with six digits, or undefined to reject pairing. Peers are
55
+ * the peripherals this adapter connects to and the clients of its local
56
+ * peripherals, and each handler receives the peer's address. A handler can
57
+ * return a promise, e.g. of the user's input, and BlueZ waits for it.
58
+ * Requests reach it once Config.simpleBluez.interactivePairing is set. Other
59
+ * platforms and adapters reject with OPERATION_NOT_SUPPORTED.
60
+ */
61
+ setPasskeyRequestHandler(handler: ((peer: string) => string | undefined | Promise<string | undefined>) | null): Promise<void>;
62
+ /** Shows the six-digit passkey a peer pairing through this adapter on Linux asks the user to enter on it. */
63
+ setPasskeyDisplayHandler(handler: ((peer: string, passkey: string) => void) | null): Promise<void>;
64
+ /**
65
+ * Answers whether a peer pairing through this adapter on Linux shows the same
66
+ * six-digit number, accepting or rejecting pairing.
67
+ */
68
+ setNumericComparisonHandler(handler: ((peer: string, passkey: string) => boolean | Promise<boolean>) | null): Promise<void>;
69
+ /**
70
+ * Accepts or rejects Just Works pairing with a peer through this adapter on
71
+ * Linux. Without a handler, Just Works pairing is accepted.
72
+ */
73
+ setPairingAuthorizationHandler(handler: ((peer: string) => boolean | Promise<boolean>) | null): Promise<void>;
52
74
  /** A host for GATT services of this process, advertised by this adapter. */
53
75
  createLocalPeripheral(): LocalPeripheral;
54
76
  /**
55
77
  * Stops this adapter's scan and local peripherals, disconnects the
56
- * peripherals connected through it, and releases the adapter and every object
57
- * that came from it. Once the promise resolves, a Dongl is free for other
58
- * programs. Calls on the closed objects then fail with OBJECT_NOT_INITIALIZED,
59
- * they emit no more events, and getAdapters() returns a new Adapter for the
60
- * device.
78
+ * peripherals connected through it, removes its pairing handlers, and
79
+ * releases the adapter and every object that came from it. Once the promise
80
+ * resolves, a Dongl is free for other programs. Calls on the closed objects
81
+ * then fail with OBJECT_NOT_INITIALIZED, they emit no more events, and
82
+ * getAdapters() returns a new Adapter for the device.
61
83
  */
62
84
  close(): Promise<void>;
63
85
  [Symbol.asyncDispose](): Promise<void>;
package/dist/adapter.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Adapter = void 0;
4
4
  const node_events_1 = require("node:events");
5
+ const handlers_1 = require("./handlers");
5
6
  const local_1 = require("./local");
6
7
  const native_1 = require("./native");
7
8
  const peripheral_1 = require("./peripheral");
@@ -23,6 +24,10 @@ const construct = Symbol('construct');
23
24
  class Adapter extends node_events_1.EventEmitter {
24
25
  #handle;
25
26
  #rehook;
27
+ #passkeyRequest = new handlers_1.HandlerSlot();
28
+ #passkeyDisplay = new handlers_1.HandlerSlot();
29
+ #numericComparison = new handlers_1.HandlerSlot();
30
+ #pairingAuthorization = new handlers_1.HandlerSlot();
26
31
  #closing;
27
32
  constructor(token, handle) {
28
33
  if (token !== construct)
@@ -91,17 +96,47 @@ class Adapter extends node_events_1.EventEmitter {
91
96
  async retrieveCachedPeripherals(identifiers) {
92
97
  return (await this.#handle.retrieveCachedPeripherals(identifiers)).map(peripheral_1.Peripheral.wrap);
93
98
  }
99
+ /**
100
+ * Answers a request for the passkey a peer shows while it pairs through this
101
+ * adapter on Linux, with six digits, or undefined to reject pairing. Peers are
102
+ * the peripherals this adapter connects to and the clients of its local
103
+ * peripherals, and each handler receives the peer's address. A handler can
104
+ * return a promise, e.g. of the user's input, and BlueZ waits for it.
105
+ * Requests reach it once Config.simpleBluez.interactivePairing is set. Other
106
+ * platforms and adapters reject with OPERATION_NOT_SUPPORTED.
107
+ */
108
+ setPasskeyRequestHandler(handler) {
109
+ return this.#passkeyRequest.set(handler, id => this.#handle.setPasskeyRequestHandler(id));
110
+ }
111
+ /** Shows the six-digit passkey a peer pairing through this adapter on Linux asks the user to enter on it. */
112
+ setPasskeyDisplayHandler(handler) {
113
+ return this.#passkeyDisplay.set(handler, id => this.#handle.setPasskeyDisplayHandler(id));
114
+ }
115
+ /**
116
+ * Answers whether a peer pairing through this adapter on Linux shows the same
117
+ * six-digit number, accepting or rejecting pairing.
118
+ */
119
+ setNumericComparisonHandler(handler) {
120
+ return this.#numericComparison.set(handler, id => this.#handle.setNumericComparisonHandler(id));
121
+ }
122
+ /**
123
+ * Accepts or rejects Just Works pairing with a peer through this adapter on
124
+ * Linux. Without a handler, Just Works pairing is accepted.
125
+ */
126
+ setPairingAuthorizationHandler(handler) {
127
+ return this.#pairingAuthorization.set(handler, id => this.#handle.setPairingAuthorizationHandler(id));
128
+ }
94
129
  /** A host for GATT services of this process, advertised by this adapter. */
95
130
  createLocalPeripheral() {
96
131
  return local_1.LocalPeripheral.wrap(this.#handle.createLocalPeripheral());
97
132
  }
98
133
  /**
99
134
  * Stops this adapter's scan and local peripherals, disconnects the
100
- * peripherals connected through it, and releases the adapter and every object
101
- * that came from it. Once the promise resolves, a Dongl is free for other
102
- * programs. Calls on the closed objects then fail with OBJECT_NOT_INITIALIZED,
103
- * they emit no more events, and getAdapters() returns a new Adapter for the
104
- * device.
135
+ * peripherals connected through it, removes its pairing handlers, and
136
+ * releases the adapter and every object that came from it. Once the promise
137
+ * resolves, a Dongl is free for other programs. Calls on the closed objects
138
+ * then fail with OBJECT_NOT_INITIALIZED, they emit no more events, and
139
+ * getAdapters() returns a new Adapter for the device.
105
140
  */
106
141
  close() {
107
142
  this.#closing ??= this.#close();
@@ -128,6 +163,18 @@ class Adapter extends node_events_1.EventEmitter {
128
163
  const connections = this.#handle.connections().map(peripheral_1.Peripheral.wrap);
129
164
  const hosts = this.#handle.hosts().map(local_1.LocalPeripheral.wrap);
130
165
  await Promise.allSettled([...connections.map(peripheral => peripheral.disconnect()), ...hosts.map(host => host.stop())]);
166
+ // NOTE: BlueZ keeps its adapters after close(), so the pairing handlers are
167
+ // removed from it rather than left rejecting its pairing requests.
168
+ const pairing = [];
169
+ if (this.#passkeyRequest.isSet)
170
+ pairing.push(this.setPasskeyRequestHandler(null));
171
+ if (this.#passkeyDisplay.isSet)
172
+ pairing.push(this.setPasskeyDisplayHandler(null));
173
+ if (this.#numericComparison.isSet)
174
+ pairing.push(this.setNumericComparisonHandler(null));
175
+ if (this.#pairingAuthorization.isSet)
176
+ pairing.push(this.setPairingAuthorizationHandler(null));
177
+ await Promise.allSettled(pairing);
131
178
  for (const object of (0, registry_1.registered)()) {
132
179
  object[registry_1.releaseFrom]?.(key);
133
180
  }
package/dist/config.d.ts CHANGED
@@ -5,6 +5,8 @@ export interface SimpleBluezConfig {
5
5
  connectionTimeout: number;
6
6
  /** How long disconnect() waits for BlueZ, in milliseconds. Fractions round up. */
7
7
  disconnectionTimeout: number;
8
+ /** Pass BlueZ's passkey and numeric comparison requests to the adapters' pairing handlers. */
9
+ interactivePairing: boolean;
8
10
  }
9
11
  export interface WinRTConfig {
10
12
  experimentalUseOwnMtaApartment: boolean;
package/dist/config.js CHANGED
@@ -25,6 +25,7 @@ exports.Config = Object.freeze({
25
25
  useSystemBus: setting('simpleBluez.useSystemBus'),
26
26
  connectionTimeout: setting('simpleBluez.connectionTimeout'),
27
27
  disconnectionTimeout: setting('simpleBluez.disconnectionTimeout'),
28
+ interactivePairing: setting('simpleBluez.interactivePairing'),
28
29
  }),
29
30
  winRT: group('winRT', {
30
31
  experimentalUseOwnMtaApartment: setting('winRT.experimentalUseOwnMtaApartment'),
package/dist/handlers.js CHANGED
@@ -1,19 +1,19 @@
1
1
  "use strict";
2
2
  // Handlers answer questions a native thread asks and waits for, such as a local
3
- // characteristic read or a Dongl pairing request. The addon passes only the
4
- // handler's id with each question.
3
+ // characteristic read or a pairing request. The addon passes only the handler's
4
+ // id with each question, and the question's arguments.
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.HandlerSlot = void 0;
7
7
  exports.answer = answer;
8
8
  const handlers = new Map();
9
9
  let nextId = 1;
10
10
  /** @internal */
11
- function answer(id, argument, respond) {
11
+ function answer(id, args, respond) {
12
12
  const handler = handlers.get(id);
13
13
  // NOTE: An error in a handler is unhandled, as one in a listener is uncaught,
14
14
  // and the native side then gets no answer.
15
15
  Promise.resolve()
16
- .then(() => handler?.(argument))
16
+ .then(() => handler?.(...args))
17
17
  .then(respond, error => {
18
18
  respond?.(undefined);
19
19
  throw error;
@@ -48,6 +48,10 @@ class HandlerSlot {
48
48
  if (previous !== undefined)
49
49
  handlers.delete(previous);
50
50
  }
51
+ /** Whether a handler is installed or being installed. */
52
+ get isSet() {
53
+ return this.#current !== undefined;
54
+ }
51
55
  /** Forgets the handler of a native object that is gone. */
52
56
  drop() {
53
57
  if (this.#current !== undefined)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simplejsble",
3
- "version": "1.3.1-dev.25",
3
+ "version": "1.3.1-dev.35",
4
4
  "description": "SimpleBLE for Node.js and Electron: a cross-platform Bluetooth Low Energy library",
5
5
  "keywords": [
6
6
  "bluetooth",