UNPKG

7.33 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 if (typeof b !== "function" && b !== null)
11 throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12 extendStatics(d, b);
13 function __() { this.constructor = d; }
14 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 };
16})();
17var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18 if (k2 === undefined) k2 = k;
19 var desc = Object.getOwnPropertyDescriptor(m, k);
20 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21 desc = { enumerable: true, get: function() { return m[k]; } };
22 }
23 Object.defineProperty(o, k2, desc);
24}) : (function(o, m, k, k2) {
25 if (k2 === undefined) k2 = k;
26 o[k2] = m[k];
27}));
28var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29 Object.defineProperty(o, "default", { enumerable: true, value: v });
30}) : function(o, v) {
31 o["default"] = v;
32});
33var __importStar = (this && this.__importStar) || function (mod) {
34 if (mod && mod.__esModule) return mod;
35 var result = {};
36 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
37 __setModuleDefault(result, mod);
38 return result;
39};
40var __values = (this && this.__values) || function(o) {
41 var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
42 if (m) return m.call(o);
43 if (o && typeof o.length === "number") return {
44 next: function () {
45 if (o && i >= o.length) o = void 0;
46 return { value: o && o[i++], done: !o };
47 }
48 };
49 throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
50};
51var __importDefault = (this && this.__importDefault) || function (mod) {
52 return (mod && mod.__esModule) ? mod : { "default": mod };
53};
54exports.__esModule = true;
55var node_hid_1 = __importDefault(require("node-hid"));
56var hw_transport_node_hid_noevents_1 = __importStar(require("@ledgerhq/hw-transport-node-hid-noevents"));
57var devices_1 = require("@ledgerhq/devices");
58var errors_1 = require("@ledgerhq/errors");
59var listenDevices_1 = __importDefault(require("./listenDevices"));
60var listenDevicesDebounce = 500;
61var listenDevicesPollingSkip = function () { return false; };
62/**
63 * node-hid Transport implementation
64 * @example
65 * import TransportNodeHid from "@ledgerhq/hw-transport-node-hid";
66 * ...
67 * TransportNodeHid.create().then(transport => ...)
68 */
69var TransportNodeHid = /** @class */ (function (_super) {
70 __extends(TransportNodeHid, _super);
71 function TransportNodeHid() {
72 return _super !== null && _super.apply(this, arguments) || this;
73 }
74 /**
75 * if path="" is not provided, the library will take the first device
76 */
77 TransportNodeHid.open = function (path) {
78 return Promise.resolve().then(function () {
79 if (path) {
80 return new TransportNodeHid(new node_hid_1["default"].HID(path));
81 }
82 var device = (0, hw_transport_node_hid_noevents_1.getDevices)()[0];
83 if (!device)
84 throw new errors_1.TransportError("NoDevice", "NoDevice");
85 return new TransportNodeHid(new node_hid_1["default"].HID(device.path));
86 });
87 };
88 /**
89 *
90 */
91 TransportNodeHid.isSupported = hw_transport_node_hid_noevents_1["default"].isSupported;
92 /**
93 *
94 */
95 TransportNodeHid.list = hw_transport_node_hid_noevents_1["default"].list;
96 /**
97 *
98 */
99 TransportNodeHid.setListenDevicesDebounce = function (delay) {
100 listenDevicesDebounce = delay;
101 };
102 /**
103 *
104 */
105 TransportNodeHid.setListenDevicesPollingSkip = function (conditionToSkip) {
106 listenDevicesPollingSkip = conditionToSkip;
107 };
108 /**
109 *
110 */
111 TransportNodeHid.setListenDevicesDebug = function () {
112 console.warn("setListenDevicesDebug is deprecated. Use @ledgerhq/logs instead. No logs will get emitted there anymore.");
113 };
114 /**
115 */
116 TransportNodeHid.listen = function (observer) {
117 var unsubscribed = false;
118 Promise.resolve((0, hw_transport_node_hid_noevents_1.getDevices)()).then(function (devices) {
119 var e_1, _a;
120 try {
121 // this needs to run asynchronously so the subscription is defined during this phase
122 for (var devices_2 = __values(devices), devices_2_1 = devices_2.next(); !devices_2_1.done; devices_2_1 = devices_2.next()) {
123 var device = devices_2_1.value;
124 if (!unsubscribed) {
125 var descriptor = device.path;
126 var deviceModel = (0, devices_1.identifyUSBProductId)(device.productId);
127 observer.next({
128 type: "add",
129 descriptor: descriptor,
130 device: device,
131 deviceModel: deviceModel
132 });
133 }
134 }
135 }
136 catch (e_1_1) { e_1 = { error: e_1_1 }; }
137 finally {
138 try {
139 if (devices_2_1 && !devices_2_1.done && (_a = devices_2["return"])) _a.call(devices_2);
140 }
141 finally { if (e_1) throw e_1.error; }
142 }
143 });
144 var _a = (0, listenDevices_1["default"])(listenDevicesDebounce, listenDevicesPollingSkip), events = _a.events, stop = _a.stop;
145 var onAdd = function (device) {
146 if (unsubscribed || !device)
147 return;
148 var deviceModel = (0, devices_1.identifyUSBProductId)(device.productId);
149 observer.next({
150 type: "add",
151 descriptor: device.path,
152 deviceModel: deviceModel,
153 device: device
154 });
155 };
156 var onRemove = function (device) {
157 if (unsubscribed || !device)
158 return;
159 var deviceModel = (0, devices_1.identifyUSBProductId)(device.productId);
160 observer.next({
161 type: "remove",
162 descriptor: device.path,
163 deviceModel: deviceModel,
164 device: device
165 });
166 };
167 events.on("add", onAdd);
168 events.on("remove", onRemove);
169 function unsubscribe() {
170 unsubscribed = true;
171 events.removeListener("add", onAdd);
172 events.removeListener("remove", onRemove);
173 stop();
174 }
175 return {
176 unsubscribe: unsubscribe
177 };
178 };
179 return TransportNodeHid;
180}(hw_transport_node_hid_noevents_1["default"]));
181exports["default"] = TransportNodeHid;
182//# sourceMappingURL=TransportNodeHid.js.map
\No newline at end of file