@ledgerhq/hw-transport-node-hid-singleton 6.20.0 → 6.25.1-alpha.3

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
@@ -17,10 +17,13 @@ Allows to communicate with Ledger Hardware Wallets.
17
17
 
18
18
  * [TransportNodeHidSingleton](#transportnodehidsingleton)
19
19
  * [Examples](#examples)
20
+ * [exchange](#exchange)
21
+ * [Parameters](#parameters)
20
22
  * [isSupported](#issupported)
21
23
  * [list](#list)
22
24
  * [listen](#listen)
23
- * [Parameters](#parameters)
25
+ * [Parameters](#parameters-1)
26
+ * [autoDisconnect](#autodisconnect)
24
27
  * [disconnect](#disconnect)
25
28
  * [open](#open)
26
29
 
@@ -38,6 +41,16 @@ import TransportNodeHid from "@ledgerhq/hw-transport-node-hid-singleton";
38
41
  TransportNodeHid.create().then(transport => ...)
39
42
  ```
40
43
 
44
+ #### exchange
45
+
46
+ Exchange with the device using APDU protocol.
47
+
48
+ ##### Parameters
49
+
50
+ * `apdu` **[Buffer](https://nodejs.org/api/buffer.html)**
51
+
52
+ Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Buffer](https://nodejs.org/api/buffer.html)>** a promise of apdu response
53
+
41
54
  #### isSupported
42
55
 
43
56
  #### list
@@ -50,6 +63,12 @@ TransportNodeHid.create().then(transport => ...)
50
63
 
51
64
  Returns **Subscription**
52
65
 
66
+ #### autoDisconnect
67
+
68
+ convenience wrapper for auto-disconnect logic
69
+
70
+ Returns **void**
71
+
53
72
  #### disconnect
54
73
 
55
74
  globally disconnect the transport singleton
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  import TransportNodeHidNoEvents from "@ledgerhq/hw-transport-node-hid-noevents";
2
3
  import type { Observer, DescriptorEvent, Subscription } from "@ledgerhq/hw-transport";
3
4
  /**
@@ -8,6 +9,7 @@ import type { Observer, DescriptorEvent, Subscription } from "@ledgerhq/hw-trans
8
9
  * TransportNodeHid.create().then(transport => ...)
9
10
  */
10
11
  export default class TransportNodeHidSingleton extends TransportNodeHidNoEvents {
12
+ preventAutoDisconnect: boolean;
11
13
  /**
12
14
  *
13
15
  */
@@ -19,6 +21,10 @@ export default class TransportNodeHidSingleton extends TransportNodeHidNoEvents
19
21
  /**
20
22
  */
21
23
  static listen: (observer: Observer<DescriptorEvent<any>>) => Subscription;
24
+ /**
25
+ * convenience wrapper for auto-disconnect logic
26
+ */
27
+ static autoDisconnect(): void;
22
28
  /**
23
29
  * globally disconnect the transport singleton
24
30
  */
@@ -27,6 +33,13 @@ export default class TransportNodeHidSingleton extends TransportNodeHidNoEvents
27
33
  * if path="" is not provided, the library will take the first device
28
34
  */
29
35
  static open(): Promise<TransportNodeHidSingleton>;
36
+ setAllowAutoDisconnect(allow: boolean): void;
37
+ /**
38
+ * Exchange with the device using APDU protocol.
39
+ * @param apdu
40
+ * @returns a promise of apdu response
41
+ */
42
+ exchange(apdu: Buffer): Promise<Buffer>;
30
43
  close(): Promise<void>;
31
44
  }
32
45
  //# sourceMappingURL=TransportNodeHid.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"TransportNodeHid.d.ts","sourceRoot":"","sources":["../src/TransportNodeHid.ts"],"names":[],"mappings":"AACA,OAAO,wBAEN,MAAM,0CAA0C,CAAC;AAClD,OAAO,KAAK,EACV,QAAQ,EACR,eAAe,EACf,YAAY,EACb,MAAM,wBAAwB,CAAC;AAMhC;;;;;;GAMG;AAEH,MAAM,CAAC,OAAO,OAAO,yBAA0B,SAAQ,wBAAwB;IAC7E;;OAEG;IACH,MAAM,CAAC,WAAW,yBAAwC;IAE1D;;OAEG;IACH,MAAM,CAAC,IAAI,qBAAiC;IAE5C;OACG;IACH,MAAM,CAAC,MAAM,aAAc,SAAS,gBAAgB,GAAG,CAAC,CAAC,KAAG,YAAY,CAqDtE;IAEF;;OAEG;WACU,UAAU;IAQvB;;OAEG;IACH,MAAM,CAAC,IAAI,IAAI,OAAO,CAAC,yBAAyB,CAAC;IAoCjD,KAAK;CAIN"}
1
+ {"version":3,"file":"TransportNodeHid.d.ts","sourceRoot":"","sources":["../src/TransportNodeHid.ts"],"names":[],"mappings":";AACA,OAAO,wBAEN,MAAM,0CAA0C,CAAC;AAClD,OAAO,KAAK,EACV,QAAQ,EACR,eAAe,EACf,YAAY,EACb,MAAM,wBAAwB,CAAC;AAuBhC;;;;;;GAMG;AAEH,MAAM,CAAC,OAAO,OAAO,yBAA0B,SAAQ,wBAAwB;IAC7E,qBAAqB,UAAS;IAC9B;;OAEG;IACH,MAAM,CAAC,WAAW,yBAAwC;IAE1D;;OAEG;IACH,MAAM,CAAC,IAAI,qBAAiC;IAE5C;OACG;IACH,MAAM,CAAC,MAAM,aAAc,SAAS,gBAAgB,GAAG,CAAC,CAAC,KAAG,YAAY,CAqDtE;IAEF;;OAEG;WACU,cAAc,IAAI,IAAI;IAWnC;;OAEG;WACU,UAAU;IASvB;;OAEG;IACH,MAAM,CAAC,IAAI,IAAI,OAAO,CAAC,yBAAyB,CAAC;IAqCjD,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI;IAI5C;;;;OAIG;IACG,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAO7C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAMvB"}
@@ -91,6 +91,17 @@ var devices_1 = require("@ledgerhq/devices");
91
91
  var errors_1 = require("@ledgerhq/errors");
92
92
  var listenDevices_1 = require("./listenDevices");
93
93
  var transportInstance;
94
+ var DISCONNECT_TIMEOUT = 5000;
95
+ var disconnectTimeout;
96
+ var clearDisconnectTimeout = function () {
97
+ if (disconnectTimeout) {
98
+ clearTimeout(disconnectTimeout);
99
+ }
100
+ };
101
+ var setDisconnectTimeout = function () {
102
+ clearDisconnectTimeout();
103
+ disconnectTimeout = setTimeout(function () { return TransportNodeHidSingleton.autoDisconnect(); }, DISCONNECT_TIMEOUT);
104
+ };
94
105
  /**
95
106
  * node-hid Transport implementation
96
107
  * @example
@@ -101,8 +112,29 @@ var transportInstance;
101
112
  var TransportNodeHidSingleton = /** @class */ (function (_super) {
102
113
  __extends(TransportNodeHidSingleton, _super);
103
114
  function TransportNodeHidSingleton() {
104
- return _super !== null && _super.apply(this, arguments) || this;
115
+ var _this = _super !== null && _super.apply(this, arguments) || this;
116
+ _this.preventAutoDisconnect = false;
117
+ return _this;
105
118
  }
119
+ /**
120
+ * convenience wrapper for auto-disconnect logic
121
+ */
122
+ TransportNodeHidSingleton.autoDisconnect = function () {
123
+ return __awaiter(this, void 0, void 0, function () {
124
+ return __generator(this, function (_a) {
125
+ if (transportInstance && !transportInstance.preventAutoDisconnect) {
126
+ (0, logs_1.log)("hid-verbose", "triggering auto disconnect");
127
+ TransportNodeHidSingleton.disconnect();
128
+ }
129
+ else if (transportInstance) {
130
+ // If we have disabled the auto-disconnect, try again in DISCONNECT_TIMEOUT
131
+ clearDisconnectTimeout();
132
+ setDisconnectTimeout();
133
+ }
134
+ return [2 /*return*/];
135
+ });
136
+ });
137
+ };
106
138
  /**
107
139
  * globally disconnect the transport singleton
108
140
  */
@@ -114,6 +146,7 @@ var TransportNodeHidSingleton = /** @class */ (function (_super) {
114
146
  transportInstance.emit("disconnect");
115
147
  transportInstance = null;
116
148
  }
149
+ clearDisconnectTimeout();
117
150
  return [2 /*return*/];
118
151
  });
119
152
  });
@@ -122,6 +155,7 @@ var TransportNodeHidSingleton = /** @class */ (function (_super) {
122
155
  * if path="" is not provided, the library will take the first device
123
156
  */
124
157
  TransportNodeHidSingleton.open = function () {
158
+ clearDisconnectTimeout();
125
159
  return Promise.resolve().then(function () {
126
160
  if (transportInstance) {
127
161
  (0, logs_1.log)("hid-verbose", "reusing opened transport instance");
@@ -150,8 +184,34 @@ var TransportNodeHidSingleton = /** @class */ (function (_super) {
150
184
  return transportInstance;
151
185
  });
152
186
  };
187
+ TransportNodeHidSingleton.prototype.setAllowAutoDisconnect = function (allow) {
188
+ this.preventAutoDisconnect = allow;
189
+ };
190
+ /**
191
+ * Exchange with the device using APDU protocol.
192
+ * @param apdu
193
+ * @returns a promise of apdu response
194
+ */
195
+ TransportNodeHidSingleton.prototype.exchange = function (apdu) {
196
+ return __awaiter(this, void 0, void 0, function () {
197
+ var result;
198
+ return __generator(this, function (_a) {
199
+ switch (_a.label) {
200
+ case 0:
201
+ clearDisconnectTimeout();
202
+ return [4 /*yield*/, _super.prototype.exchange.call(this, apdu)];
203
+ case 1:
204
+ result = _a.sent();
205
+ setDisconnectTimeout();
206
+ return [2 /*return*/, result];
207
+ }
208
+ });
209
+ });
210
+ };
153
211
  TransportNodeHidSingleton.prototype.close = function () {
154
- // intentionally, a close will not effectively close the hid connection
212
+ // intentionally, a close will not effectively close the hid connection but
213
+ // will allow an auto-disconnection after some inactivity
214
+ this.preventAutoDisconnect = false;
155
215
  return Promise.resolve();
156
216
  };
157
217
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"TransportNodeHid.js","sourceRoot":"","sources":["../src/TransportNodeHid.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sDAA2B;AAC3B,yGAEkD;AAMlD,uCAAqC;AACrC,6CAAyD;AACzD,2CAAkD;AAClD,iDAAgD;AAChD,IAAI,iBAAiB,CAAC;AACtB;;;;;;GAMG;AAEH;IAAuD,6CAAwB;IAA/E;;IA0HA,CAAC;IAtDC;;OAEG;IACU,oCAAU,GAAvB;;;gBACE,IAAI,iBAAiB,EAAE;oBACrB,iBAAiB,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;oBACjC,iBAAiB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;oBACrC,iBAAiB,GAAG,IAAI,CAAC;iBAC1B;;;;KACF;IAED;;OAEG;IACI,8BAAI,GAAX;QACE,OAAO,OAAO,CAAC,OAAO,EAAE,CA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,EAAE,KAAK;4BACX,UAAU,EAAE,EAAE;4BACd,MAAM,EAAE;gCACN,IAAI,EAAE,MAAM,CAAC,UAAU;6BACxB;4BACD,WAAW,aAAA;yBACZ,CAAC,CAAC;qBACJ;iBACF;;;;;;;;;QACH,CAAC,CAAC,CAAC;QAEH,IAAM,KAAK,GAAG,UAAC,MAAM;YACnB,IAAM,WAAW,GAAG,IAAA,8BAAoB,EAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YAC3D,QAAQ,CAAC,IAAI,CAAC;gBACZ,IAAI,EAAE,KAAK;gBACX,UAAU,EAAE,EAAE;gBACd,WAAW,aAAA;gBACX,MAAM,EAAE;oBACN,IAAI,EAAE,MAAM,CAAC,UAAU;iBACxB;aACF,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,IAAM,QAAQ,GAAG,UAAC,MAAM;YACtB,IAAM,WAAW,GAAG,IAAA,8BAAoB,EAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YAC3D,QAAQ,CAAC,IAAI,CAAC;gBACZ,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE,EAAE;gBACd,WAAW,aAAA;gBACX,MAAM,EAAE;oBACN,IAAI,EAAE,MAAM,CAAC,UAAU;iBACxB;aACF,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,IAAM,IAAI,GAAG,IAAA,6BAAa,EAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QAE5C,SAAS,WAAW;YAClB,IAAI,EAAE,CAAC;YACP,YAAY,GAAG,IAAI,CAAC;QACtB,CAAC;QAED,OAAO;YACL,WAAW,aAAA;SACZ,CAAC;IACJ,CAAC,CAAC;IA0FJ,gCAAC;CAAA,AA7JD,CAAuD,2CAAwB,GA6J9E;qBA7JoB,yBAAyB"}
@@ -0,0 +1,138 @@
1
+ //@flow
2
+
3
+ import HID from "node-hid";
4
+ import TransportNodeHidNoEvents, {
5
+ getDevices,
6
+ } from "@ledgerhq/hw-transport-node-hid-noevents";
7
+ import type {
8
+ Observer,
9
+ DescriptorEvent,
10
+ Subscription,
11
+ } from "@ledgerhq/hw-transport";
12
+ import { log } from "@ledgerhq/logs";
13
+ import { identifyUSBProductId } from "@ledgerhq/devices";
14
+ import { CantOpenDevice } from "@ledgerhq/errors";
15
+ import { listenDevices } from "./listenDevices";
16
+
17
+ let transportInstance;
18
+
19
+ /**
20
+ * node-hid Transport implementation
21
+ * @example
22
+ * import TransportNodeHid from "@ledgerhq/hw-transport-node-hid-singleton";
23
+ * ...
24
+ * TransportNodeHid.create().then(transport => ...)
25
+ */
26
+ export default class TransportNodeHidSingleton extends TransportNodeHidNoEvents {
27
+ /**
28
+ *
29
+ */
30
+ static isSupported = TransportNodeHidNoEvents.isSupported;
31
+
32
+ /**
33
+ *
34
+ */
35
+ static list = TransportNodeHidNoEvents.list;
36
+
37
+ /**
38
+ */
39
+ static listen = (observer: Observer<DescriptorEvent<*>>): Subscription => {
40
+ let unsubscribed;
41
+ Promise.resolve(getDevices()).then((devices) => {
42
+ // this needs to run asynchronously so the subscription is defined during this phase
43
+ for (const device of devices) {
44
+ if (!unsubscribed) {
45
+ const deviceModel = identifyUSBProductId(device.productId);
46
+ observer.next({
47
+ type: "add",
48
+ descriptor: "",
49
+ device: { name: device.deviceName },
50
+ deviceModel,
51
+ });
52
+ }
53
+ }
54
+ });
55
+
56
+ const onAdd = (device) => {
57
+ const deviceModel = identifyUSBProductId(device.productId);
58
+ observer.next({
59
+ type: "add",
60
+ descriptor: "",
61
+ deviceModel,
62
+ device: { name: device.deviceName },
63
+ });
64
+ };
65
+
66
+ const onRemove = (device) => {
67
+ const deviceModel = identifyUSBProductId(device.productId);
68
+ observer.next({
69
+ type: "remove",
70
+ descriptor: "",
71
+ deviceModel,
72
+ device: { name: device.deviceName },
73
+ });
74
+ };
75
+
76
+ const stop = listenDevices(onAdd, onRemove);
77
+
78
+ function unsubscribe() {
79
+ stop();
80
+ unsubscribed = true;
81
+ }
82
+ return { unsubscribe };
83
+ };
84
+
85
+ /**
86
+ * globally disconnect the transport singleton
87
+ */
88
+ static async disconnect() {
89
+ if (transportInstance) {
90
+ transportInstance.device.close();
91
+ transportInstance.emit("disconnect");
92
+ transportInstance = null;
93
+ }
94
+ }
95
+
96
+ /**
97
+ * if path="" is not provided, the library will take the first device
98
+ */
99
+ static open(): Promise<TransportNodeHidSingleton> {
100
+ return Promise.resolve().then(() => {
101
+ if (transportInstance) {
102
+ log("hid-verbose", "reusing opened transport instance");
103
+ return transportInstance;
104
+ }
105
+
106
+ const device = getDevices()[0];
107
+ if (!device) throw new CantOpenDevice("no device found");
108
+ log("hid-verbose", "new HID transport");
109
+ transportInstance = new TransportNodeHidSingleton(
110
+ new HID.HID(device.path)
111
+ );
112
+ const unlisten = listenDevices(
113
+ () => {},
114
+ () => {
115
+ // assume any ledger disconnection concerns current transport
116
+ if (transportInstance) {
117
+ transportInstance.emit("disconnect");
118
+ }
119
+ }
120
+ );
121
+ const onDisconnect = () => {
122
+ if (!transportInstance) return;
123
+ log("hid-verbose", "transport instance was disconnected");
124
+ transportInstance.off("disconnect", onDisconnect);
125
+ transportInstance = null;
126
+ unlisten();
127
+ };
128
+ transportInstance.on("disconnect", onDisconnect);
129
+
130
+ return transportInstance;
131
+ });
132
+ }
133
+
134
+ close() {
135
+ // intentionally, a close will not effectively close the hid connection
136
+ return Promise.resolve();
137
+ }
138
+ }
@@ -0,0 +1,84 @@
1
+ // @flow
2
+ import usbDetect from "usb-detection";
3
+ import { ledgerUSBVendorId } from "@ledgerhq/devices";
4
+ import { log } from "@ledgerhq/logs";
5
+
6
+ export type Device = {
7
+ locationId: number,
8
+ vendorId: number,
9
+ productId: number,
10
+ deviceName: string,
11
+ manufacturer: string,
12
+ serialNumber: string,
13
+ deviceAddress: number,
14
+ };
15
+
16
+ const deviceToLog = ({ productId, locationId, deviceAddress }) =>
17
+ `productId=${productId} locationId=${locationId} deviceAddress=${deviceAddress}`;
18
+
19
+ let usbDebounce = 1000;
20
+
21
+ export const setUsbDebounce = (n: number) => {
22
+ usbDebounce = n;
23
+ };
24
+
25
+ let monitoring = false;
26
+ const monitor = () => {
27
+ if (!monitoring) {
28
+ monitoring = true;
29
+ usbDetect.startMonitoring();
30
+ }
31
+ return () => {};
32
+ };
33
+
34
+ // No better way for now. see https://github.com/LedgerHQ/ledgerjs/issues/434
35
+ process.on("exit", () => {
36
+ if (monitoring) {
37
+ // redeem the monitoring so the process can be terminated.
38
+ usbDetect.stopMonitoring();
39
+ }
40
+ });
41
+
42
+ export const listenDevices = (
43
+ onAdd: (Device) => void,
44
+ onRemove: (Device) => void
45
+ ) => {
46
+ const unmonitor = monitor();
47
+
48
+ const addEvent = "add:" + ledgerUSBVendorId;
49
+ const removeEvent = "remove:" + ledgerUSBVendorId;
50
+
51
+ let timeout;
52
+
53
+ const add = (device) => {
54
+ log("usb-detection", "add: " + deviceToLog(device));
55
+ if (!timeout) {
56
+ // a time is needed for the device to actually be connectable over HID..
57
+ // we also take this time to not emit the device yet and potentially cancel it if a remove happens.
58
+ timeout = setTimeout(() => {
59
+ onAdd(device);
60
+ timeout = null;
61
+ }, usbDebounce);
62
+ }
63
+ };
64
+
65
+ const remove = (device) => {
66
+ log("usb-detection", "remove: " + deviceToLog(device));
67
+ if (timeout) {
68
+ clearTimeout(timeout);
69
+ timeout = null;
70
+ } else {
71
+ onRemove(device);
72
+ }
73
+ };
74
+
75
+ usbDetect.on(addEvent, add);
76
+ usbDetect.on(removeEvent, remove);
77
+
78
+ return () => {
79
+ if (timeout) clearTimeout(timeout);
80
+ usbDetect.off(addEvent, add);
81
+ usbDetect.off(removeEvent, remove);
82
+ unmonitor();
83
+ };
84
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ledgerhq/hw-transport-node-hid-singleton",
3
- "version": "6.20.0",
3
+ "version": "6.25.1-alpha.3+eb669e17",
4
4
  "description": "Ledger Hardware Wallet Node implementation of the communication layer, using node-hid and usb-detection",
5
5
  "keywords": [
6
6
  "Ledger",
@@ -27,10 +27,10 @@
27
27
  "types": "lib/TransportNodeHid.d.ts",
28
28
  "license": "Apache-2.0",
29
29
  "dependencies": {
30
- "@ledgerhq/devices": "^6.20.0",
30
+ "@ledgerhq/devices": "^6.24.1",
31
31
  "@ledgerhq/errors": "^6.10.0",
32
- "@ledgerhq/hw-transport": "^6.20.0",
33
- "@ledgerhq/hw-transport-node-hid-noevents": "^6.20.0",
32
+ "@ledgerhq/hw-transport": "^6.25.1-alpha.3+eb669e17",
33
+ "@ledgerhq/hw-transport-node-hid-noevents": "^6.25.1-alpha.3+eb669e17",
34
34
  "@ledgerhq/logs": "^6.10.0",
35
35
  "lodash": "^4.17.21",
36
36
  "node-hid": "2.1.1",
@@ -42,5 +42,5 @@
42
42
  "watch": "bash ../../script/watch.sh",
43
43
  "doc": "bash ../../script/doc.sh"
44
44
  },
45
- "gitHead": "814218180de877f1c4128af1b9e2dd0fb7dfb0c7"
45
+ "gitHead": "eb669e17dd87d3ab568beab1f9a5ddb1a2536e83"
46
46
  }
@@ -12,6 +12,23 @@ import { identifyUSBProductId } from "@ledgerhq/devices";
12
12
  import { CantOpenDevice } from "@ledgerhq/errors";
13
13
  import { listenDevices } from "./listenDevices";
14
14
  let transportInstance;
15
+
16
+ const DISCONNECT_TIMEOUT = 5000;
17
+ let disconnectTimeout;
18
+ const clearDisconnectTimeout = () => {
19
+ if (disconnectTimeout) {
20
+ clearTimeout(disconnectTimeout);
21
+ }
22
+ };
23
+
24
+ const setDisconnectTimeout = () => {
25
+ clearDisconnectTimeout();
26
+ disconnectTimeout = setTimeout(
27
+ () => TransportNodeHidSingleton.autoDisconnect(),
28
+ DISCONNECT_TIMEOUT
29
+ );
30
+ };
31
+
15
32
  /**
16
33
  * node-hid Transport implementation
17
34
  * @example
@@ -21,6 +38,7 @@ let transportInstance;
21
38
  */
22
39
 
23
40
  export default class TransportNodeHidSingleton extends TransportNodeHidNoEvents {
41
+ preventAutoDisconnect = false;
24
42
  /**
25
43
  *
26
44
  */
@@ -88,6 +106,20 @@ export default class TransportNodeHidSingleton extends TransportNodeHidNoEvents
88
106
  };
89
107
  };
90
108
 
109
+ /**
110
+ * convenience wrapper for auto-disconnect logic
111
+ */
112
+ static async autoDisconnect(): void {
113
+ if (transportInstance && !transportInstance.preventAutoDisconnect) {
114
+ log("hid-verbose", "triggering auto disconnect");
115
+ TransportNodeHidSingleton.disconnect();
116
+ } else if (transportInstance) {
117
+ // If we have disabled the auto-disconnect, try again in DISCONNECT_TIMEOUT
118
+ clearDisconnectTimeout();
119
+ setDisconnectTimeout();
120
+ }
121
+ }
122
+
91
123
  /**
92
124
  * globally disconnect the transport singleton
93
125
  */
@@ -97,12 +129,14 @@ export default class TransportNodeHidSingleton extends TransportNodeHidNoEvents
97
129
  transportInstance.emit("disconnect");
98
130
  transportInstance = null;
99
131
  }
132
+ clearDisconnectTimeout();
100
133
  }
101
134
 
102
135
  /**
103
136
  * if path="" is not provided, the library will take the first device
104
137
  */
105
138
  static open(): Promise<TransportNodeHidSingleton> {
139
+ clearDisconnectTimeout();
106
140
  return Promise.resolve().then(() => {
107
141
  if (transportInstance) {
108
142
  log("hid-verbose", "reusing opened transport instance");
@@ -138,8 +172,26 @@ export default class TransportNodeHidSingleton extends TransportNodeHidNoEvents
138
172
  });
139
173
  }
140
174
 
141
- close() {
142
- // intentionally, a close will not effectively close the hid connection
175
+ setAllowAutoDisconnect(allow: boolean): void {
176
+ this.preventAutoDisconnect = allow;
177
+ }
178
+
179
+ /**
180
+ * Exchange with the device using APDU protocol.
181
+ * @param apdu
182
+ * @returns a promise of apdu response
183
+ */
184
+ async exchange(apdu: Buffer): Promise<Buffer> {
185
+ clearDisconnectTimeout();
186
+ const result = await super.exchange(apdu);
187
+ setDisconnectTimeout();
188
+ return result;
189
+ }
190
+
191
+ close(): Promise<void> {
192
+ // intentionally, a close will not effectively close the hid connection but
193
+ // will allow an auto-disconnection after some inactivity
194
+ this.preventAutoDisconnect = false;
143
195
  return Promise.resolve();
144
196
  }
145
197
  }