diff --git a/package.json b/package.json index dd3ca12..3abf4d2 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,9 @@ "./types": { "import": "./lib/types.js" }, + "./default": { + "import": "./lib/default.js" + }, "./blst-native": { "import": "./lib/blst-native/index.js" }, diff --git a/src/default.ts b/src/default.ts new file mode 100644 index 0000000..5c89239 --- /dev/null +++ b/src/default.ts @@ -0,0 +1,8 @@ +import {getImplementation} from "./getImplementation.js"; + +// Thanks https://github.com/iliakan/detect-node/blob/master/index.esm.js +const isNode = Object.prototype.toString.call(typeof process !== "undefined" ? process : 0) === "[object process]"; + +export const bls = await getImplementation(isNode ? "blst-native" : "herumi"); +export default bls; +export * from "./interface.js"; diff --git a/src/getImplementation.ts b/src/getImplementation.ts new file mode 100644 index 0000000..bd80dd2 --- /dev/null +++ b/src/getImplementation.ts @@ -0,0 +1,24 @@ +import type {IBls, Implementation} from "./interface.js"; + +// Thanks https://github.com/iliakan/detect-node/blob/master/index.esm.js +const isNode = Object.prototype.toString.call(typeof process !== "undefined" ? process : 0) === "[object process]"; + +export async function getImplementation(impl: Implementation = "herumi"): Promise { + switch (impl) { + case "herumi": { + const blsHerumi = (await import("./herumi/index.js")).bls; + await blsHerumi.init(); + return blsHerumi; + } + + case "blst-native": + // Lazy import native bindings to prevent automatically importing binding.node files + if (!isNode) { + throw Error("blst-native is only supported in NodeJS"); + } + return (await import("./blst-native/index.js")).bls; + + default: + throw new Error(`Unsupported implementation - ${impl}`); + } +} diff --git a/src/index.ts b/src/index.ts index b479a04..5cb3084 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ -import {IBls, Implementation} from "./interface.js"; +import type {IBls, Implementation} from "./interface.js"; +import {getImplementation} from "./getImplementation.js"; export {IBls, Implementation, CoordType, PointFormat} from "./interface.js"; @@ -6,29 +7,6 @@ export {IBls, Implementation, CoordType, PointFormat} from "./interface.js"; export const bls: IBls = {} as IBls; export default bls; -// Thanks https://github.com/iliakan/detect-node/blob/master/index.esm.js -const isNode = Object.prototype.toString.call(typeof process !== "undefined" ? process : 0) === "[object process]"; - -async function getImplementation(impl: Implementation = "herumi"): Promise { - switch (impl) { - case "herumi": { - const blsHerumi = (await import("./herumi/index.js")).bls; - await blsHerumi.init(); - return blsHerumi; - } - - case "blst-native": - // Lazy import native bindings to prevent automatically importing binding.node files - if (!isNode) { - throw Error("blst-native is only supported in NodeJS"); - } - return (await import("./blst-native/index.js")).bls; - - default: - throw new Error(`Unsupported implementation - ${impl}`); - } -} - export async function init(impl: Implementation): Promise { // Using Object.assign instead of just bls = getImplementation() // because otherwise the default import breaks. The reference is lost diff --git a/tsconfig.json b/tsconfig.json index f1b3836..d6ea2b8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ "compilerOptions": { "outDir": "lib", "target": "es2019", - "module": "ES2020", + "module": "es2022", "moduleResolution": "Node", "pretty": true, "lib": ["esnext.bigint", "DOM"],