Add default export

This commit is contained in:
Cayman 2022-04-13 11:59:25 -05:00
parent 8881334330
commit 9a851a095a
No known key found for this signature in database
GPG Key ID: 54B21AEC3C53E1F5
5 changed files with 38 additions and 25 deletions

View File

@ -13,6 +13,9 @@
"./types": {
"import": "./lib/types.js"
},
"./default": {
"import": "./lib/default.js"
},
"./blst-native": {
"import": "./lib/blst-native/index.js"
},

8
src/default.ts Normal file
View File

@ -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";

24
src/getImplementation.ts Normal file
View File

@ -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<IBls> {
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}`);
}
}

View File

@ -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<IBls> {
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<void> {
// Using Object.assign instead of just bls = getImplementation()
// because otherwise the default import breaks. The reference is lost

View File

@ -3,7 +3,7 @@
"compilerOptions": {
"outDir": "lib",
"target": "es2019",
"module": "ES2020",
"module": "es2022",
"moduleResolution": "Node",
"pretty": true,
"lib": ["esnext.bigint", "DOM"],