@ledgerhq/hw-transport-node-hid
Advanced tools
Comparing version 4.21.0 to 4.22.0-beta.ae6f5c51
@@ -19,2 +19,10 @@ "use strict"; | ||
var _usb = require("usb"); | ||
var _usb2 = _interopRequireDefault(_usb); | ||
var _debounce = require("lodash/debounce"); | ||
var _debounce2 = _interopRequireDefault(_debounce); | ||
var _getDevices = require("./getDevices"); | ||
@@ -26,7 +34,6 @@ | ||
exports.default = function (delay, listenDevicesPollingSkip) { | ||
exports.default = function (delay, listenDevicesPollingSkip, debug) { | ||
var events = new _events2.default(); | ||
events.setMaxListeners(0); | ||
var timeoutDetection = void 0; | ||
var listDevices = (0, _getDevices2.default)(); | ||
@@ -52,19 +59,30 @@ | ||
var checkDevices = function checkDevices() { | ||
var poll = function poll() { | ||
if (!listenDevicesPollingSkip()) { | ||
debug("Polling for added or removed devices"); | ||
var changeFound = false; | ||
var currentDevices = getFlatDevices(); | ||
var newDevices = currentDevices.filter(function (d) { | ||
return !lastDevices.includes(d); | ||
}); | ||
var removeDevices = lastDevices.filter(function (d) { | ||
return !currentDevices.includes(d); | ||
}); | ||
if (newDevices.length > 0) { | ||
debug("New device found:", newDevices); | ||
listDevices = (0, _getDevices2.default)(); | ||
events.emit("add", getDeviceByPaths(newDevices)); | ||
changeFound = true; | ||
} else { | ||
debug("No new device found"); | ||
} | ||
var removeDevices = lastDevices.filter(function (d) { | ||
return !currentDevices.includes(d); | ||
}); | ||
if (removeDevices.length > 0) { | ||
debug("Removed device found:", removeDevices); | ||
events.emit("remove", getDeviceByPaths(removeDevices)); | ||
@@ -74,14 +92,41 @@ listDevices = listDevices.filter(function (d) { | ||
}); | ||
changeFound = true; | ||
} else { | ||
debug("No removed device found"); | ||
} | ||
lastDevices = currentDevices; | ||
if (changeFound) { | ||
lastDevices = currentDevices; | ||
} | ||
} else { | ||
debug("Polling skipped, re-debouncing"); | ||
debouncedPoll(); | ||
} | ||
setTimeout(checkDevices, delay); | ||
}; | ||
timeoutDetection = setTimeout(checkDevices, delay); | ||
var debouncedPoll = (0, _debounce2.default)(poll, delay); | ||
var attachDetected = function attachDetected(device) { | ||
debug("Device add detected:", device); | ||
debouncedPoll(); | ||
}; | ||
_usb2.default.on("attach", attachDetected); | ||
debug("attach listener added"); | ||
var detachDetected = function detachDetected(device) { | ||
debug("Device removal detected:", device); | ||
debouncedPoll(); | ||
}; | ||
_usb2.default.on("detach", detachDetected); | ||
debug("detach listener added"); | ||
return { | ||
stop: function stop() { | ||
clearTimeout(timeoutDetection); | ||
debug("Stop received, removing listeners and cancelling pending debounced polls"); | ||
debouncedPoll.cancel(); | ||
_usb2.default.removeListener("attach", attachDetected); | ||
_usb2.default.removeListener("detach", detachDetected); | ||
}, | ||
@@ -88,0 +133,0 @@ events: events |
@@ -77,2 +77,3 @@ "use strict"; | ||
}; | ||
var listenDevicesDebug = function listenDevicesDebug() {}; | ||
@@ -423,2 +424,14 @@ /** | ||
TransportNodeHid.setListenDevicesDebug = function (debug) { | ||
listenDevicesDebug = typeof debug === "function" ? debug : debug ? function () { | ||
var _console; | ||
for (var _len = arguments.length, log = Array(_len), _key = 0; _key < _len; _key++) { | ||
log[_key] = arguments[_key]; | ||
} | ||
return (_console = console).log.apply(_console, ["[listenDevices]"].concat(log)); | ||
} : function () {}; | ||
}; | ||
TransportNodeHid.listen = function (observer) { | ||
@@ -457,3 +470,3 @@ var unsubscribed = false; | ||
var _listenDevices = (0, _listenDevices3.default)(listenDevicesPollingInterval, listenDevicesPollingSkip), | ||
var _listenDevices = (0, _listenDevices3.default)(listenDevicesPollingInterval, listenDevicesPollingSkip, listenDevicesDebug), | ||
events = _listenDevices.events, | ||
@@ -460,0 +473,0 @@ stop = _listenDevices.stop; |
{ | ||
"name": "@ledgerhq/hw-transport-node-hid", | ||
"version": "4.21.0", | ||
"version": "4.22.0-beta.ae6f5c51", | ||
"description": "Ledger Hardware Wallet Node implementation of the communication layer, using node-hid", | ||
@@ -28,4 +28,6 @@ "keywords": [ | ||
"dependencies": { | ||
"@ledgerhq/hw-transport": "^4.21.0", | ||
"node-hid": "^0.7.2" | ||
"@ledgerhq/hw-transport": "^4.22.0-beta.ae6f5c51", | ||
"lodash": "^4.17.10", | ||
"node-hid": "^0.7.2", | ||
"usb": "^1.3.2" | ||
}, | ||
@@ -32,0 +34,0 @@ "devDependencies": { |
// @flow | ||
import EventEmitter from "events"; | ||
import usb from "usb"; | ||
import debounce from "lodash/debounce"; | ||
import getDevices from "./getDevices"; | ||
@@ -8,3 +10,4 @@ | ||
delay: number, | ||
listenDevicesPollingSkip: () => boolean | ||
listenDevicesPollingSkip: () => boolean, | ||
debug: (...any) => void | ||
): { | ||
@@ -17,3 +20,2 @@ events: EventEmitter, | ||
let timeoutDetection; | ||
let listDevices = getDevices(); | ||
@@ -32,17 +34,28 @@ | ||
const checkDevices = () => { | ||
const poll = () => { | ||
if (!listenDevicesPollingSkip()) { | ||
debug("Polling for added or removed devices"); | ||
let changeFound = false; | ||
const currentDevices = getFlatDevices(); | ||
const newDevices = currentDevices.filter(d => !lastDevices.includes(d)); | ||
const removeDevices = lastDevices.filter( | ||
d => !currentDevices.includes(d) | ||
); | ||
if (newDevices.length > 0) { | ||
debug("New device found:", newDevices); | ||
listDevices = getDevices(); | ||
events.emit("add", getDeviceByPaths(newDevices)); | ||
changeFound = true; | ||
} else { | ||
debug("No new device found"); | ||
} | ||
const removeDevices = lastDevices.filter( | ||
d => !currentDevices.includes(d) | ||
); | ||
if (removeDevices.length > 0) { | ||
debug("Removed device found:", removeDevices); | ||
events.emit("remove", getDeviceByPaths(removeDevices)); | ||
@@ -52,14 +65,43 @@ listDevices = listDevices.filter( | ||
); | ||
changeFound = true; | ||
} else { | ||
debug("No removed device found"); | ||
} | ||
lastDevices = currentDevices; | ||
if (changeFound) { | ||
lastDevices = currentDevices; | ||
} | ||
} else { | ||
debug("Polling skipped, re-debouncing"); | ||
debouncedPoll(); | ||
} | ||
setTimeout(checkDevices, delay); | ||
}; | ||
timeoutDetection = setTimeout(checkDevices, delay); | ||
const debouncedPoll = debounce(poll, delay); | ||
const attachDetected = device => { | ||
debug("Device add detected:", device); | ||
debouncedPoll(); | ||
}; | ||
usb.on("attach", attachDetected); | ||
debug("attach listener added"); | ||
const detachDetected = device => { | ||
debug("Device removal detected:", device); | ||
debouncedPoll(); | ||
}; | ||
usb.on("detach", detachDetected); | ||
debug("detach listener added"); | ||
return { | ||
stop: () => { | ||
clearTimeout(timeoutDetection); | ||
debug( | ||
"Stop received, removing listeners and cancelling pending debounced polls" | ||
); | ||
debouncedPoll.cancel(); | ||
usb.removeListener("attach", attachDetected); | ||
usb.removeListener("detach", detachDetected); | ||
}, | ||
@@ -66,0 +108,0 @@ events |
@@ -31,2 +31,3 @@ //@flow | ||
let listenDevicesPollingSkip = () => false; | ||
let listenDevicesDebug = () => {}; | ||
@@ -72,2 +73,11 @@ /** | ||
static setListenDevicesDebug = (debug: boolean | ((log: string) => void)) => { | ||
listenDevicesDebug = | ||
typeof debug === "function" | ||
? debug | ||
: debug | ||
? (...log) => console.log("[listenDevices]", ...log) | ||
: () => {}; | ||
}; | ||
/** | ||
@@ -90,3 +100,4 @@ */ | ||
listenDevicesPollingInterval, | ||
listenDevicesPollingSkip | ||
listenDevicesPollingSkip, | ||
listenDevicesDebug | ||
); | ||
@@ -93,0 +104,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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
84306
1014
4
1
1
+ Addedlodash@^4.17.10
+ Addedusb@^1.3.2
+ Addedlodash@4.17.21(transitive)
+ Addednode-addon-api@4.3.0(transitive)
+ Addednode-gyp-build@4.8.2(transitive)
+ Addedusb@1.9.2(transitive)