2020-01-10 02:50:09 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
export type TransportCreator = {
|
|
|
|
create: () => Promise<Transport>;
|
|
|
|
};
|
|
|
|
|
2020-04-24 18:22:06 -04: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 03:46:46 -04: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-24 18:22:06 -04:00
|
|
|
hidCache = import("@ledgerhq/hw-transport-node-hid").then((hid) => {
|
|
|
|
if (hid.create == null) { return hid["default"]; }
|
|
|
|
return hid;
|
|
|
|
});
|
2020-04-25 03:46:46 -04:00
|
|
|
*/
|
2020-04-24 18:22:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return hidCache.then((hid) => {
|
|
|
|
return hid.create()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export const transports: { [ name: string ]: TransportCreator } = Object.freeze({
|
|
|
|
"hid": hidWrapper,
|
|
|
|
"default": hidWrapper
|
|
|
|
});
|