simplejsble 1.2.1-dev100
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/LICENSE.md +104 -0
- package/README.md +168 -0
- package/dist/adapter.d.ts +65 -0
- package/dist/adapter.js +139 -0
- package/dist/backend.d.ts +16 -0
- package/dist/backend.js +45 -0
- package/dist/config.d.ts +36 -0
- package/dist/config.js +43 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.js +17 -0
- package/dist/handlers.d.ts +1 -0
- package/dist/handlers.js +58 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +49 -0
- package/dist/local.d.ts +78 -0
- package/dist/local.js +182 -0
- package/dist/logging.d.ts +22 -0
- package/dist/logging.js +38 -0
- package/dist/native.d.ts +1 -0
- package/dist/native.js +15 -0
- package/dist/peripheral.d.ts +77 -0
- package/dist/peripheral.js +235 -0
- package/dist/registry.d.ts +1 -0
- package/dist/registry.js +110 -0
- package/package.json +59 -0
- package/prebuilds/darwin-arm64/simplejsble.node +0 -0
- package/prebuilds/darwin-x64/simplejsble.node +0 -0
- package/prebuilds/linux-arm64/simplejsble.node +0 -0
- package/prebuilds/linux-x64/simplejsble.node +0 -0
- package/prebuilds/win32-arm64/simplejsble.node +0 -0
- package/prebuilds/win32-x64/simplejsble.node +0 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Peripheral = void 0;
|
|
4
|
+
exports.deliver = deliver;
|
|
5
|
+
const node_events_1 = require("node:events");
|
|
6
|
+
const handlers_1 = require("./handlers");
|
|
7
|
+
const registry_1 = require("./registry");
|
|
8
|
+
const EVENTS = ['connected', 'disconnected'];
|
|
9
|
+
const construct = Symbol('construct');
|
|
10
|
+
// Notification and indication callbacks by subscription id. The addon passes
|
|
11
|
+
// only the id with each value.
|
|
12
|
+
const callbacks = new Map();
|
|
13
|
+
let nextId = 1;
|
|
14
|
+
// NOTE: Some backends, like CoreBluetooth, match UUIDs regardless of case, and
|
|
15
|
+
// the others match only lowercase ones. Lowercase keys name the characteristic
|
|
16
|
+
// the backend uses either way.
|
|
17
|
+
function subscriptionKey(service, characteristic) {
|
|
18
|
+
return `${service}\n${characteristic}`.toLowerCase();
|
|
19
|
+
}
|
|
20
|
+
/** @internal */
|
|
21
|
+
function deliver(id, value) {
|
|
22
|
+
callbacks.get(id)?.(value);
|
|
23
|
+
}
|
|
24
|
+
function freeze(services) {
|
|
25
|
+
for (const service of services) {
|
|
26
|
+
for (const characteristic of service.characteristics) {
|
|
27
|
+
for (const descriptor of characteristic.descriptors)
|
|
28
|
+
Object.freeze(descriptor);
|
|
29
|
+
Object.freeze(characteristic.descriptors);
|
|
30
|
+
Object.freeze(characteristic.capabilities);
|
|
31
|
+
Object.freeze(characteristic);
|
|
32
|
+
}
|
|
33
|
+
Object.freeze(service.characteristics);
|
|
34
|
+
Object.freeze(service);
|
|
35
|
+
}
|
|
36
|
+
return services;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A remote device. There is one Peripheral object per native peripheral object,
|
|
40
|
+
* so an Adapter's events, scan results and peripheral lists return the same
|
|
41
|
+
* object for the same device.
|
|
42
|
+
*
|
|
43
|
+
* A peripheral with listeners or subscriptions stays alive, even if nothing
|
|
44
|
+
* else references it. An open connection keeps Node.js running until it ends.
|
|
45
|
+
*/
|
|
46
|
+
class Peripheral extends node_events_1.EventEmitter {
|
|
47
|
+
#handle;
|
|
48
|
+
#rehook;
|
|
49
|
+
// Subscriptions by subscriptionKey().
|
|
50
|
+
#subscriptions = new Map();
|
|
51
|
+
#passkeyRequest = new handlers_1.HandlerSlot();
|
|
52
|
+
#passkeyDisplay = new handlers_1.HandlerSlot();
|
|
53
|
+
#numericComparison = new handlers_1.HandlerSlot();
|
|
54
|
+
constructor(token, handle) {
|
|
55
|
+
if (token !== construct)
|
|
56
|
+
throw new TypeError('Peripherals come from an Adapter');
|
|
57
|
+
super();
|
|
58
|
+
this.#handle = handle;
|
|
59
|
+
this.#rehook = (0, registry_1.retainWhileListening)(this, EVENTS);
|
|
60
|
+
handle.subscribe();
|
|
61
|
+
}
|
|
62
|
+
/** @internal */
|
|
63
|
+
static wrap(handle) {
|
|
64
|
+
return (0, registry_1.intern)(handle.key(), () => new Peripheral(construct, handle), () => handle.release());
|
|
65
|
+
}
|
|
66
|
+
identifier() {
|
|
67
|
+
return this.#handle.identifier();
|
|
68
|
+
}
|
|
69
|
+
address() {
|
|
70
|
+
return this.#handle.address();
|
|
71
|
+
}
|
|
72
|
+
addressType() {
|
|
73
|
+
return this.#handle.addressType();
|
|
74
|
+
}
|
|
75
|
+
/** The last received signal strength in dBm, if any. */
|
|
76
|
+
rssi() {
|
|
77
|
+
return this.#handle.rssi();
|
|
78
|
+
}
|
|
79
|
+
/** The advertised transmit power in dBm, if any. */
|
|
80
|
+
txPower() {
|
|
81
|
+
return this.#handle.txPower();
|
|
82
|
+
}
|
|
83
|
+
mtu() {
|
|
84
|
+
return this.#handle.mtu();
|
|
85
|
+
}
|
|
86
|
+
isConnected() {
|
|
87
|
+
return this.#handle.isConnected();
|
|
88
|
+
}
|
|
89
|
+
isConnectable() {
|
|
90
|
+
return this.#handle.isConnectable();
|
|
91
|
+
}
|
|
92
|
+
isPaired() {
|
|
93
|
+
return this.#handle.isPaired();
|
|
94
|
+
}
|
|
95
|
+
/** Advertised manufacturer data by company identifier. The buffers are copies. */
|
|
96
|
+
manufacturerData() {
|
|
97
|
+
return new Map(this.#handle.manufacturerData());
|
|
98
|
+
}
|
|
99
|
+
connect() {
|
|
100
|
+
return this.#handle.connect();
|
|
101
|
+
}
|
|
102
|
+
async disconnect() {
|
|
103
|
+
// Calls run in call order, so this ends the subscriptions made before it,
|
|
104
|
+
// and not those of calls made while it runs.
|
|
105
|
+
const ended = [...this.#subscriptions];
|
|
106
|
+
await this.#handle.disconnect();
|
|
107
|
+
for (const [key, subscription] of ended)
|
|
108
|
+
this.#forget(key, subscription);
|
|
109
|
+
}
|
|
110
|
+
unpair() {
|
|
111
|
+
return this.#handle.unpair();
|
|
112
|
+
}
|
|
113
|
+
/** The services of a connected peripheral, or the advertised ones before connecting. */
|
|
114
|
+
services() {
|
|
115
|
+
return freeze(this.#handle.services());
|
|
116
|
+
}
|
|
117
|
+
async read(service, characteristic) {
|
|
118
|
+
return this.#handle.read(service, characteristic);
|
|
119
|
+
}
|
|
120
|
+
async writeRequest(service, characteristic, data) {
|
|
121
|
+
return this.#handle.writeRequest(service, characteristic, data);
|
|
122
|
+
}
|
|
123
|
+
async writeCommand(service, characteristic, data) {
|
|
124
|
+
return this.#handle.writeCommand(service, characteristic, data);
|
|
125
|
+
}
|
|
126
|
+
async readDescriptor(service, characteristic, descriptor) {
|
|
127
|
+
return this.#handle.readDescriptor(service, characteristic, descriptor);
|
|
128
|
+
}
|
|
129
|
+
async writeDescriptor(service, characteristic, descriptor, data) {
|
|
130
|
+
return this.#handle.writeDescriptor(service, characteristic, descriptor, data);
|
|
131
|
+
}
|
|
132
|
+
/** Calls `callback` with each notified value until unsubscribe() or a disconnection. */
|
|
133
|
+
notify(service, characteristic, callback) {
|
|
134
|
+
return this.#subscribe(id => this.#handle.notify(service, characteristic, id), service, characteristic, callback);
|
|
135
|
+
}
|
|
136
|
+
/** Calls `callback` with each indicated value until unsubscribe() or a disconnection. */
|
|
137
|
+
indicate(service, characteristic, callback) {
|
|
138
|
+
return this.#subscribe(id => this.#handle.indicate(service, characteristic, id), service, characteristic, callback);
|
|
139
|
+
}
|
|
140
|
+
async unsubscribe(service, characteristic) {
|
|
141
|
+
// Like disconnect(), this ends only the subscription made before it.
|
|
142
|
+
const key = subscriptionKey(service, characteristic);
|
|
143
|
+
const ended = this.#subscriptions.get(key);
|
|
144
|
+
await this.#handle.unsubscribe(service, characteristic);
|
|
145
|
+
if (ended)
|
|
146
|
+
this.#forget(key, ended);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Answers a Dongl's request for the passkey the peer shows, with six digits,
|
|
150
|
+
* or undefined to reject pairing. Set it before connect(), as pairing can
|
|
151
|
+
* start as soon as the link is up. The handler can return a promise, e.g. of
|
|
152
|
+
* the user's input, and the Dongl waits for it.
|
|
153
|
+
*/
|
|
154
|
+
setPasskeyRequestHandler(handler) {
|
|
155
|
+
return this.#passkeyRequest.set(handler, id => this.#handle.setPasskeyRequestHandler(id));
|
|
156
|
+
}
|
|
157
|
+
/** Shows a Dongl's six-digit passkey, which the user enters on the peer. Set it before connect(). */
|
|
158
|
+
setPasskeyDisplayHandler(handler) {
|
|
159
|
+
return this.#passkeyDisplay.set(handler, id => this.#handle.setPasskeyDisplayHandler(id));
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Answers whether the peer shows the same six-digit number as a Dongl,
|
|
163
|
+
* accepting or rejecting pairing. Set it before connect(). The handler can
|
|
164
|
+
* return a promise.
|
|
165
|
+
*/
|
|
166
|
+
setNumericComparisonHandler(handler) {
|
|
167
|
+
return this.#numericComparison.set(handler, id => this.#handle.setNumericComparisonHandler(id));
|
|
168
|
+
}
|
|
169
|
+
removeAllListeners(...args) {
|
|
170
|
+
super.removeAllListeners(...args);
|
|
171
|
+
this.#rehook();
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
/** @internal */
|
|
175
|
+
[registry_1.releaseFrom](adapterKey) {
|
|
176
|
+
if (this.#handle.owner() !== adapterKey)
|
|
177
|
+
return;
|
|
178
|
+
const key = this.#handle.key();
|
|
179
|
+
for (const [subscriptionKey, subscription] of [...this.#subscriptions])
|
|
180
|
+
this.#forget(subscriptionKey, subscription);
|
|
181
|
+
for (const slot of [this.#passkeyRequest, this.#passkeyDisplay, this.#numericComparison])
|
|
182
|
+
slot.drop();
|
|
183
|
+
this.removeAllListeners();
|
|
184
|
+
this.#handle.release();
|
|
185
|
+
(0, registry_1.unregister)(key, this);
|
|
186
|
+
}
|
|
187
|
+
/** @internal */
|
|
188
|
+
[registry_1.onNativeEvent](event) {
|
|
189
|
+
if (event !== 'disconnected')
|
|
190
|
+
return;
|
|
191
|
+
// A disconnection ends the subscriptions made before it. One still being
|
|
192
|
+
// made either started after it or fails, which forgets it.
|
|
193
|
+
for (const [key, subscription] of [...this.#subscriptions]) {
|
|
194
|
+
if (subscription.settled)
|
|
195
|
+
this.#forget(key, subscription);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
async #subscribe(start, service, characteristic, callback) {
|
|
199
|
+
if (typeof callback !== 'function')
|
|
200
|
+
throw new TypeError('callback must be a function');
|
|
201
|
+
// The callback is in place before the first value can arrive. A new
|
|
202
|
+
// subscription to the same characteristic replaces the previous one.
|
|
203
|
+
const key = subscriptionKey(service, characteristic);
|
|
204
|
+
const subscription = { id: nextId++, settled: false };
|
|
205
|
+
this.#remember(key, subscription, callback);
|
|
206
|
+
try {
|
|
207
|
+
await start(subscription.id);
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
this.#forget(key, subscription);
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
subscription.settled = true;
|
|
214
|
+
}
|
|
215
|
+
#remember(key, subscription, callback) {
|
|
216
|
+
const previous = this.#subscriptions.get(key);
|
|
217
|
+
if (previous) {
|
|
218
|
+
callbacks.delete(previous.id);
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
(0, registry_1.retain)(this);
|
|
222
|
+
}
|
|
223
|
+
this.#subscriptions.set(key, subscription);
|
|
224
|
+
callbacks.set(subscription.id, callback);
|
|
225
|
+
}
|
|
226
|
+
// Forgets `subscription` unless a later one has replaced it.
|
|
227
|
+
#forget(key, subscription) {
|
|
228
|
+
if (this.#subscriptions.get(key) !== subscription)
|
|
229
|
+
return;
|
|
230
|
+
this.#subscriptions.delete(key);
|
|
231
|
+
callbacks.delete(subscription.id);
|
|
232
|
+
(0, registry_1.release)(this);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
exports.Peripheral = Peripheral;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.releaseFrom = exports.onNativeEvent = void 0;
|
|
4
|
+
exports.retain = retain;
|
|
5
|
+
exports.release = release;
|
|
6
|
+
exports.lookup = lookup;
|
|
7
|
+
exports.registered = registered;
|
|
8
|
+
exports.unregister = unregister;
|
|
9
|
+
exports.intern = intern;
|
|
10
|
+
exports.retainWhileListening = retainWhileListening;
|
|
11
|
+
// One JavaScript object per native object, keyed by the native object's address.
|
|
12
|
+
const objects = new Map();
|
|
13
|
+
const finalizer = new FinalizationRegistry(({ key, ref }) => {
|
|
14
|
+
if (objects.get(key) === ref)
|
|
15
|
+
objects.delete(key);
|
|
16
|
+
});
|
|
17
|
+
// Objects with listeners for native events or with subscriptions are kept
|
|
18
|
+
// alive, so they keep working after the caller drops its own reference.
|
|
19
|
+
const retained = new Map();
|
|
20
|
+
/** @internal */
|
|
21
|
+
function retain(object) {
|
|
22
|
+
retained.set(object, (retained.get(object) ?? 0) + 1);
|
|
23
|
+
}
|
|
24
|
+
/** @internal */
|
|
25
|
+
function release(object) {
|
|
26
|
+
const count = (retained.get(object) ?? 0) - 1;
|
|
27
|
+
if (count > 0) {
|
|
28
|
+
retained.set(object, count);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
retained.delete(object);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Lets an object see a native event before its listeners do, even without
|
|
36
|
+
* listeners, e.g. to forget subscriptions on a disconnection.
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
exports.onNativeEvent = Symbol('onNativeEvent');
|
|
40
|
+
/**
|
|
41
|
+
* Releases an object that came from the adapter with the given key, which is
|
|
42
|
+
* closing. Objects of other adapters ignore it.
|
|
43
|
+
* @internal
|
|
44
|
+
*/
|
|
45
|
+
exports.releaseFrom = Symbol('releaseFrom');
|
|
46
|
+
/** @internal */
|
|
47
|
+
function lookup(key) {
|
|
48
|
+
return objects.get(key)?.deref();
|
|
49
|
+
}
|
|
50
|
+
/** Every object that still stands for a native object. @internal */
|
|
51
|
+
function registered() {
|
|
52
|
+
return [...objects.values()].map(ref => ref.deref()).filter(object => object !== undefined);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Forgets `object`, which no longer stands for a native object, so the next
|
|
56
|
+
* object for `key` is a new one.
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
function unregister(key, object) {
|
|
60
|
+
if (lookup(key) === object)
|
|
61
|
+
objects.delete(key);
|
|
62
|
+
retained.delete(object);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Returns the object for `key`, created if there is none. `discard` releases
|
|
66
|
+
* the new handle when an object exists, as an unused handle would otherwise
|
|
67
|
+
* keep the native object until garbage collection.
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
function intern(key, create, discard) {
|
|
71
|
+
const existing = lookup(key);
|
|
72
|
+
if (existing) {
|
|
73
|
+
discard?.();
|
|
74
|
+
return existing;
|
|
75
|
+
}
|
|
76
|
+
const object = create();
|
|
77
|
+
const ref = new WeakRef(object);
|
|
78
|
+
objects.set(key, ref);
|
|
79
|
+
finalizer.register(object, { key, ref });
|
|
80
|
+
return object;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Keeps `emitter` alive while it has listeners for any of `events`. Returns a
|
|
84
|
+
* function that reinstalls the hooks, which removeAllListeners() also removes.
|
|
85
|
+
* @internal
|
|
86
|
+
*/
|
|
87
|
+
function retainWhileListening(emitter, events) {
|
|
88
|
+
const listening = () => events.some(event => emitter.listenerCount(event) > 0);
|
|
89
|
+
let held = false;
|
|
90
|
+
// NOTE: 'newListener' fires before the listener is added, 'removeListener'
|
|
91
|
+
// after it is removed.
|
|
92
|
+
const onNewListener = (event) => {
|
|
93
|
+
if (held || typeof event !== 'string' || !events.includes(event))
|
|
94
|
+
return;
|
|
95
|
+
held = true;
|
|
96
|
+
retain(emitter);
|
|
97
|
+
};
|
|
98
|
+
const onRemoveListener = (event) => {
|
|
99
|
+
if (!held || typeof event !== 'string' || !events.includes(event) || listening())
|
|
100
|
+
return;
|
|
101
|
+
held = false;
|
|
102
|
+
release(emitter);
|
|
103
|
+
};
|
|
104
|
+
const hook = () => {
|
|
105
|
+
emitter.off('newListener', onNewListener).off('removeListener', onRemoveListener);
|
|
106
|
+
emitter.on('newListener', onNewListener).on('removeListener', onRemoveListener);
|
|
107
|
+
};
|
|
108
|
+
hook();
|
|
109
|
+
return hook;
|
|
110
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "simplejsble",
|
|
3
|
+
"version": "1.2.1-dev100",
|
|
4
|
+
"description": "SimpleBLE for Node.js and Electron: a cross-platform Bluetooth Low Energy library",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bluetooth",
|
|
7
|
+
"ble",
|
|
8
|
+
"bluetooth-low-energy",
|
|
9
|
+
"simpleble",
|
|
10
|
+
"electron"
|
|
11
|
+
],
|
|
12
|
+
"license": "BUSL-1.1",
|
|
13
|
+
"homepage": "https://simpleble.org",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/simpleble/simpleble.git",
|
|
17
|
+
"directory": "simplejsble"
|
|
18
|
+
},
|
|
19
|
+
"type": "commonjs",
|
|
20
|
+
"main": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22"
|
|
24
|
+
},
|
|
25
|
+
"os": [
|
|
26
|
+
"darwin",
|
|
27
|
+
"linux",
|
|
28
|
+
"win32"
|
|
29
|
+
],
|
|
30
|
+
"cpu": [
|
|
31
|
+
"x64",
|
|
32
|
+
"arm64"
|
|
33
|
+
],
|
|
34
|
+
"binary": {
|
|
35
|
+
"napi_versions": [
|
|
36
|
+
8
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"prebuilds",
|
|
42
|
+
"LICENSE.md"
|
|
43
|
+
],
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "node scripts/build.mjs",
|
|
46
|
+
"build:plain": "node scripts/build.mjs --plain",
|
|
47
|
+
"build:ts": "tsc -p tsconfig.json",
|
|
48
|
+
"test": "node --test --test-concurrency=1 --test-reporter=spec \"test/*.test.mjs\"",
|
|
49
|
+
"test:types": "tsc -p test/types",
|
|
50
|
+
"check:prebuild": "node scripts/check-prebuild.mjs",
|
|
51
|
+
"prepack": "node scripts/prepack.mjs"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@types/node": "^24.0.0",
|
|
55
|
+
"cmake-js": "^8.0.0",
|
|
56
|
+
"node-addon-api": "^8.9.2",
|
|
57
|
+
"typescript": "^5.9.0"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|