-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
58 lines (48 loc) · 1.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var usbspyBinding = require("bindings")("usbspy.node");
var events = require("events");
require("es6-promise/auto");
var index = require("./package.json");
if (global[index.name] && global[index.name].version === index.version) {
module.exports = global[index.name];
} else {
var usbspy = new events.EventEmitter();
var ready = false;
usbspy.spyOn = function(callback) {
var promise = new Promise(function(resolve, reject) {
if (!ready) {
usbspyBinding.spyOn(
function(data) {
usbspy.emit("change", data);
},
function(data) {
usbspy.emit("end", data);
},
function() {
resolve(true);
callback(true);
}
);
ready = true;
} else {
reject();
callback(false);
}
});
return promise;
};
usbspy.spyOff = function() {
if (!ready) {
usbspyBinding.spyOff();
ready = false;
}
};
usbspy.getAvailableUSBStorageDevices = function() {
return usbspyBinding.getAvailableUSBStorageDevices();
};
usbspy.getUSBStorageDeviceByPropertyName = function(propertyName, value) {
return usbspyBinding.getUSBStorageDeviceByPropertyName(propertyName, value);
};
usbspy.version = index.version;
global[index.name] = usbspy;
module.exports = usbspy;
}