From ef98683424d303a78a619c0cef98b84426ceaf27 Mon Sep 17 00:00:00 2001 From: dapplion Date: Sun, 29 Nov 2020 16:52:29 +0000 Subject: [PATCH 1/4] Use Object.assign to fix default import --- src/index.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index bf811a2..ff62669 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import {bls as blsHerumi} from "./herumi"; export type Implementation = "herumi" | "blst-native"; // TODO: Use a Proxy for example to throw an error if it's not initialized yet -export let bls: IBls; +export let bls: IBls = {} as IBls; async function getImplementation(impl: Implementation = "herumi"): Promise { switch (impl) { @@ -25,7 +25,10 @@ async function getImplementation(impl: Implementation = "herumi"): Promise } export async function init(impl: Implementation): Promise { - bls = await getImplementation(impl); + // Using Object.assign instead of just bls = getImplementation() + // because otherwise the default import breaks. The reference is lost + // and the imported object is still undefined after calling init() + Object.assign(bls, await getImplementation(impl)); } export default bls; From 2235e7be8933c2618c9c3a80e133fa518d0d87e7 Mon Sep 17 00:00:00 2001 From: dapplion Date: Sun, 29 Nov 2020 16:59:16 +0000 Subject: [PATCH 2/4] Import named export from blst --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index ff62669..b552a7a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,7 @@ async function getImplementation(impl: Implementation = "herumi"): Promise throw Error("blst-native is only supported in NodeJS"); } // eslint-disable-next-line @typescript-eslint/no-require-imports - return require("./blst"); + return require("./blst").bls; default: throw new Error(`Unsupported implementation - ${impl}`); From e51c1784a757f742fd8d6a3f6bcdccaaf711bb23 Mon Sep 17 00:00:00 2001 From: dapplion Date: Sun, 29 Nov 2020 16:59:24 +0000 Subject: [PATCH 3/4] Document lazy import --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index b552a7a..0e3f1de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ async function getImplementation(impl: Implementation = "herumi"): Promise return blsHerumi; case "blst-native": + // Lazy import native bindings to prevent automatically importing binding.node files if (typeof require !== "function") { throw Error("blst-native is only supported in NodeJS"); } From 61c1baea3f01ab9b533165e6e8690f19643e7d40 Mon Sep 17 00:00:00 2001 From: dapplion Date: Sun, 29 Nov 2020 17:10:20 +0000 Subject: [PATCH 4/4] Use const in main bls export --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 0e3f1de..4de9153 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import {bls as blsHerumi} from "./herumi"; export type Implementation = "herumi" | "blst-native"; // TODO: Use a Proxy for example to throw an error if it's not initialized yet -export let bls: IBls = {} as IBls; +export const bls: IBls = {} as IBls; async function getImplementation(impl: Implementation = "herumi"): Promise { switch (impl) {