Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return Promise.resolve().then(() => {
if (transportInstance) {
log("hid-verbose", "reusing opened transport instance");
return transportInstance;
}
const device = getDevices()[0];
if (!device) throw new CantOpenDevice("no device found");
log("hid-verbose", "new HID transport");
transportInstance = new TransportNodeHidSingleton(
new HID.HID(device.path)
);
const unlisten = listenDevices(
() => {},
() => {
// assume any ledger disconnection concerns current transport
if (transportInstance) {
transportInstance.emit("disconnect");
}
}
);
const onDisconnect = () => {
if (!transportInstance) return;
log("hid-verbose", "transport instance was disconnected");
log("ble-verbose", `connectToDevice(${deviceOrId})`);
try {
device = await bleManager.connectToDevice(deviceOrId, connectOptions);
} catch (e) {
if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed) {
// eslint-disable-next-line require-atomic-updates
connectOptions = {};
device = await bleManager.connectToDevice(deviceOrId);
} else {
throw e;
}
}
}
if (!device) {
throw new CantOpenDevice();
}
} else {
device = deviceOrId;
}
if (!(await device.isConnected())) {
log("ble-verbose", "not connected. connecting...");
try {
await device.connect(connectOptions);
} catch (e) {
if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed) {
// eslint-disable-next-line require-atomic-updates
connectOptions = {};
await device.connect();
} else {
throw e;
async function open(deviceOrId: Device | string, needsReconnect: boolean) {
let device;
if (typeof deviceOrId === "string") {
if (transportsCache[deviceOrId]) {
log("ble-verbose", "Transport in cache, using that.");
return transportsCache[deviceOrId];
}
} else {
device = deviceOrId;
}
if (!device) {
throw new CantOpenDevice();
}
await availability.pipe(first(enabled => enabled)).toPromise();
if (isDeviceDisconnected(device)) {
log("ble-verbose", "not connected. connecting...");
await connectDevice(device);
}
const {
notifyC,
writeC,
deviceModel
} = await retrieveServiceAndCharacteristics(device);
const [observable, monitoringReady] = monitorCharacteristic(notifyC);
.catch(e => {
finish();
if (e instanceof BluetoothRequired) throw e;
if (e instanceof TransportWebUSBGestureRequired) throw e;
if (e instanceof TransportInterfaceNotAvailable) throw e;
throw new CantOpenDevice(e.message);
})
.then(transport => {