2020-01-10 10:50:09 +03:00
|
|
|
"use strict";
|
|
|
|
|
2021-10-05 22:48:14 +03:00
|
|
|
import type { Transport } from "@ledgerhq/hw-transport-node-hid";
|
|
|
|
|
2020-01-10 10:50:09 +03:00
|
|
|
export type TransportCreator = {
|
|
|
|
create: () => Promise<Transport>;
|
|
|
|
};
|
|
|
|
|
2020-04-25 01:22:06 +03:00
|
|
|
let hidCache: Promise<typeof import("@ledgerhq/hw-transport-node-hid")> = null;
|
|
|
|
|
|
|
|
const hidWrapper = Object.freeze({
|
|
|
|
create: function(): Promise<Transport> {
|
|
|
|
// Load the library if not loaded
|
|
|
|
if (hidCache == null) {
|
2020-04-25 10:46:46 +03:00
|
|
|
hidCache = new Promise((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
let hid = require("@ledgerhq/hw-transport-node-hid");
|
|
|
|
if (hid.create == null) { resolve(hid["default"]); }
|
|
|
|
resolve(hid);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
/*
|
2020-04-25 01:22:06 +03:00
|
|
|
hidCache = import("@ledgerhq/hw-transport-node-hid").then((hid) => {
|
|
|
|
if (hid.create == null) { return hid["default"]; }
|
|
|
|
return hid;
|
|
|
|
});
|
2020-04-25 10:46:46 +03:00
|
|
|
*/
|
2020-04-25 01:22:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return hidCache.then((hid) => {
|
|
|
|
return hid.create()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export const transports: { [ name: string ]: TransportCreator } = Object.freeze({
|
|
|
|
"hid": hidWrapper,
|
|
|
|
"default": hidWrapper
|
|
|
|
});
|