@ledgerhq/hw-transport-node-hid
Advanced tools
Comparing version 1.2.0-beta.5f7fbb68 to 1.2.0-beta.6cd06d6f
@@ -12,7 +12,11 @@ "use strict"; | ||
var _isLedgerDevice = require("./isLedgerDevice"); | ||
var _isLedgerDevice2 = _interopRequireDefault(_isLedgerDevice); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function getDevices() { | ||
return _nodeHid2.default.devices(); | ||
return _nodeHid2.default.devices().filter(_isLedgerDevice2.default); | ||
} | ||
//# sourceMappingURL=getDevices.js.map |
@@ -11,63 +11,53 @@ "use strict"; | ||
var _nodeHid = require("node-hid"); | ||
var _getDevices = require("./getDevices"); | ||
var _getDevices2 = _interopRequireDefault(_getDevices); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const myEE = new _events2.default(); | ||
myEE.setMaxListeners(0); | ||
exports.default = (delay = 100) => { | ||
const events = new _events2.default(); | ||
events.setMaxListeners(0); | ||
let timeoutDetection; | ||
let listDevices = (0, _getDevices2.default)(); | ||
let timeoutDetection = null; | ||
let isListenDevices = false; | ||
const flatDevice = device => device.path; | ||
exports.default = { | ||
start: (delay = 100) => { | ||
if (isListenDevices) { | ||
return; | ||
} | ||
const getFlatDevices = () => [...new Set((0, _getDevices2.default)().map(device => flatDevice(device)))]; | ||
const getDeviceByPath = ids => listDevices.find(device => flatDevice(device) === ids); | ||
isListenDevices = true; | ||
let lastDevices = getFlatDevices(); | ||
let listDevices = (0, _nodeHid.devices)(); | ||
const checkDevices = () => { | ||
timeoutDetection = setTimeout(() => { | ||
const currentDevices = getFlatDevices(); | ||
const flatDevice = device => device.path; | ||
const addDevice = currentDevices.find(device => !lastDevices.includes(device)); | ||
const removeDevice = lastDevices.find(device => !currentDevices.includes(device)); | ||
const getFlatDevices = () => [...new Set((0, _nodeHid.devices)().map(device => flatDevice(device)))]; | ||
const getDeviceByPath = ids => listDevices.find(device => flatDevice(device) === ids); | ||
if (addDevice) { | ||
listDevices = (0, _getDevices2.default)(); | ||
events.emit("add", getDeviceByPath(addDevice)); | ||
} | ||
let lastDevices = getFlatDevices(); | ||
if (removeDevice) { | ||
events.emit("remove", getDeviceByPath(removeDevice)); | ||
listDevices = listDevices.filter(device => flatDevice(device) !== removeDevice); | ||
} | ||
const checkDevices = () => { | ||
timeoutDetection = setTimeout(() => { | ||
const currentDevices = getFlatDevices(); | ||
lastDevices = currentDevices; | ||
const addDevice = currentDevices.find(device => !lastDevices.includes(device)); | ||
const removeDevice = lastDevices.find(device => !currentDevices.includes(device)); | ||
checkDevices(); | ||
}, delay); | ||
}; | ||
if (addDevice) { | ||
listDevices = (0, _nodeHid.devices)(); | ||
myEE.emit("add", getDeviceByPath(addDevice)); | ||
} | ||
checkDevices(); | ||
if (removeDevice) { | ||
myEE.emit("remove", getDeviceByPath(removeDevice)); | ||
listDevices = listDevices.filter(device => flatDevice(device) !== removeDevice); | ||
} | ||
lastDevices = currentDevices; | ||
checkDevices(); | ||
}, delay); | ||
}; | ||
checkDevices(); | ||
}, | ||
stop: () => { | ||
isListenDevices = false; | ||
if (timeoutDetection !== null) { | ||
return { | ||
stop: () => { | ||
clearTimeout(timeoutDetection); | ||
} | ||
}, | ||
events: myEE | ||
}, | ||
events | ||
}; | ||
}; | ||
//# sourceMappingURL=listenDevices.js.map |
@@ -19,2 +19,6 @@ "use strict"; | ||
var _listenDevices = require("./listenDevices"); | ||
var _listenDevices2 = _interopRequireDefault(_listenDevices); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -324,19 +328,32 @@ | ||
TransportNodeHid.list = () => Promise.resolve((0, _getDevices2.default)().filter(device => device.vendorId === 0x2581 && device.productId === 0x3b7c || device.vendorId === 0x2c97).map(d => d.path)); | ||
TransportNodeHid.list = () => Promise.resolve((0, _getDevices2.default)().map(d => d.path)); | ||
TransportNodeHid.discover = observer => { | ||
TransportNodeHid.listen = observer => { | ||
let unsubscribed = false; | ||
function unsubscribe() { | ||
unsubscribed = true; | ||
} | ||
undefined.list().then(paths => { | ||
TransportNodeHid.list().then(paths => { | ||
for (const path of paths) { | ||
if (!unsubscribed) { | ||
observer.next(path); | ||
observer.next({ type: "add", descriptor: path }); | ||
} | ||
} | ||
}); | ||
// TODO needs to also discover new plugged connection... | ||
const { events, stop } = (0, _listenDevices2.default)(); | ||
const onAdd = descriptor => { | ||
observer.next({ type: "add", descriptor }); | ||
}; | ||
const onRemove = descriptor => { | ||
observer.next({ type: "remove", descriptor }); | ||
}; | ||
events.on("add", onAdd); | ||
events.on("remove", onRemove); | ||
function unsubscribe() { | ||
unsubscribed = true; | ||
events.removeListener("add", onAdd); | ||
events.removeListener("remove", onRemove); | ||
stop(); | ||
} | ||
return { unsubscribe }; | ||
}; | ||
//# sourceMappingURL=TransportNodeHid.js.map |
{ | ||
"name": "@ledgerhq/hw-transport-node-hid", | ||
"version": "1.2.0-beta.5f7fbb68", | ||
"version": "1.2.0-beta.6cd06d6f", | ||
"description": "Ledger Hardware Wallet Node implementation of the communication layer, using node-hid", | ||
@@ -28,3 +28,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@ledgerhq/hw-transport": "^1.2.0-beta.5f7fbb68", | ||
"@ledgerhq/hw-transport": "^1.2.0-beta.6cd06d6f", | ||
"node-hid": "^0.7.2" | ||
@@ -31,0 +31,0 @@ }, |
// @flow | ||
import HID from "node-hid"; | ||
import isLedgerDevice from "./isLedgerDevice"; | ||
export default function getDevices(): Array<*> { | ||
return HID.devices(); | ||
return HID.devices().filter(isLedgerDevice); | ||
} |
// @flow | ||
import EventEmitter from "events"; | ||
import { devices } from "node-hid"; | ||
import getDevices from "./getDevices"; | ||
const myEE = new EventEmitter(); | ||
myEE.setMaxListeners(0); | ||
export default ( | ||
delay: number = 100 | ||
): { | ||
events: EventEmitter, | ||
stop: () => void | ||
} => { | ||
const events = new EventEmitter(); | ||
events.setMaxListeners(0); | ||
let timeoutDetection; | ||
let listDevices = getDevices(); | ||
let timeoutDetection = null; | ||
let isListenDevices = false; | ||
const flatDevice = device => device.path; | ||
export default { | ||
start: (delay: number = 100) => { | ||
if (isListenDevices) { | ||
return; | ||
} | ||
const getFlatDevices = () => [ | ||
...new Set(getDevices().map(device => flatDevice(device))) | ||
]; | ||
const getDeviceByPath = ids => | ||
listDevices.find(device => flatDevice(device) === ids); | ||
isListenDevices = true; | ||
let lastDevices = getFlatDevices(); | ||
let listDevices = devices(); | ||
const checkDevices = () => { | ||
timeoutDetection = setTimeout(() => { | ||
const currentDevices = getFlatDevices(); | ||
const flatDevice = device => device.path; | ||
const addDevice = currentDevices.find( | ||
device => !lastDevices.includes(device) | ||
); | ||
const removeDevice = lastDevices.find( | ||
device => !currentDevices.includes(device) | ||
); | ||
const getFlatDevices = () => [ | ||
...new Set(devices().map(device => flatDevice(device))) | ||
]; | ||
const getDeviceByPath = ids => | ||
listDevices.find(device => flatDevice(device) === ids); | ||
if (addDevice) { | ||
listDevices = getDevices(); | ||
events.emit("add", getDeviceByPath(addDevice)); | ||
} | ||
let lastDevices = getFlatDevices(); | ||
const checkDevices = () => { | ||
timeoutDetection = setTimeout(() => { | ||
const currentDevices = getFlatDevices(); | ||
const addDevice = currentDevices.find( | ||
device => !lastDevices.includes(device) | ||
if (removeDevice) { | ||
events.emit("remove", getDeviceByPath(removeDevice)); | ||
listDevices = listDevices.filter( | ||
device => flatDevice(device) !== removeDevice | ||
); | ||
const removeDevice = lastDevices.find( | ||
device => !currentDevices.includes(device) | ||
); | ||
} | ||
if (addDevice) { | ||
listDevices = devices(); | ||
myEE.emit("add", getDeviceByPath(addDevice)); | ||
} | ||
lastDevices = currentDevices; | ||
if (removeDevice) { | ||
myEE.emit("remove", getDeviceByPath(removeDevice)); | ||
listDevices = listDevices.filter( | ||
device => flatDevice(device) !== removeDevice | ||
); | ||
} | ||
checkDevices(); | ||
}, delay); | ||
}; | ||
lastDevices = currentDevices; | ||
checkDevices(); | ||
checkDevices(); | ||
}, delay); | ||
}; | ||
checkDevices(); | ||
}, | ||
stop: () => { | ||
isListenDevices = false; | ||
if (timeoutDetection !== null) { | ||
return { | ||
stop: () => { | ||
clearTimeout(timeoutDetection); | ||
} | ||
}, | ||
events: myEE | ||
}, | ||
events | ||
}; | ||
}; |
@@ -5,4 +5,4 @@ //@flow | ||
import Transport from "@ledgerhq/hw-transport"; | ||
import getDevices from "./getDevices"; | ||
import listenDevices from "./listenDevices"; | ||
@@ -53,26 +53,30 @@ // FIXME drop | ||
static list = () => | ||
Promise.resolve( | ||
getDevices() | ||
.filter( | ||
device => | ||
(device.vendorId === 0x2581 && device.productId === 0x3b7c) || | ||
device.vendorId === 0x2c97 | ||
) | ||
.map(d => d.path) | ||
); | ||
static list = () => Promise.resolve(getDevices().map(d => d.path)); | ||
static discover = (observer: *) => { | ||
static listen = (observer: *) => { | ||
let unsubscribed = false; | ||
function unsubscribe() { | ||
unsubscribed = true; | ||
} | ||
this.list().then(paths => { | ||
TransportNodeHid.list().then(paths => { | ||
for (const path of paths) { | ||
if (!unsubscribed) { | ||
observer.next(path); | ||
observer.next({ type: "add", descriptor: path }); | ||
} | ||
} | ||
}); | ||
// TODO needs to also discover new plugged connection... | ||
const { events, stop } = listenDevices(); | ||
const onAdd = descriptor => { | ||
observer.next({ type: "add", descriptor }); | ||
}; | ||
const onRemove = descriptor => { | ||
observer.next({ type: "remove", descriptor }); | ||
}; | ||
events.on("add", onAdd); | ||
events.on("remove", onRemove); | ||
function unsubscribe() { | ||
unsubscribed = true; | ||
events.removeListener("add", onAdd); | ||
events.removeListener("remove", onRemove); | ||
stop(); | ||
} | ||
return { unsubscribe }; | ||
@@ -79,0 +83,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75014
23
1037