Add default export
This commit is contained in:
parent
8881334330
commit
9a851a095a
|
@ -13,6 +13,9 @@
|
|||
"./types": {
|
||||
"import": "./lib/types.js"
|
||||
},
|
||||
"./default": {
|
||||
"import": "./lib/default.js"
|
||||
},
|
||||
"./blst-native": {
|
||||
"import": "./lib/blst-native/index.js"
|
||||
},
|
||||
|
|
|
@ -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";
|
|
@ -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}`);
|
||||
}
|
||||
}
|
26
src/index.ts
26
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<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
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
"compilerOptions": {
|
||||
"outDir": "lib",
|
||||
"target": "es2019",
|
||||
"module": "ES2020",
|
||||
"module": "es2022",
|
||||
"moduleResolution": "Node",
|
||||
"pretty": true,
|
||||
"lib": ["esnext.bigint", "DOM"],
|
||||
|
|
Reference in New Issue