Skip to content

Commit 8c81462

Browse files
committed
feat: updating wasm loader
1 parent e945ca3 commit 8c81462

File tree

11 files changed

+156
-7
lines changed

11 files changed

+156
-7
lines changed

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,12 @@ export { PhraseSize } from "./mnemonic";
6363
export type { Mnemonic } from "./mnemonic";
6464
export type { Signing } from "./signing";
6565
export type { Tx } from "./tx";
66+
67+
// Export SDK initializer and types
68+
export { initSdk } from "wasm";
69+
export type {
70+
WasmTarget,
71+
SdkBuildOptions,
72+
SdkWasmOptions,
73+
InitProps,
74+
} from "wasm";

src/wasm/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Proposal, Proposals } from "./borsh-schemas";
22
// TODO: How to handle these imports?
3-
import { Query as RustQuery } from "./web/sdk-multicore";
4-
export * from "./web/sdk-multicore";
3+
import { Query as RustQuery } from "./web/sdk";
4+
export * from "./web/sdk";
55
export * from "./types";
66
export * from "./utils";
7+
export * from "./loader";
78

89
type TimeoutOpts = {
910
// Timeout in milliseconds
@@ -60,3 +61,4 @@ export class Query extends RustQuery {
6061

6162
export * from "./types";
6263
export { Proposal, Proposals };
64+
export { initSdk } from "wasm/loader";

src/wasm/init-inline.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// TODO: How to handle these imports?
2-
import initWasm, { InitOutput } from "./web/sdk-multicore";
2+
import initWasm, { InitOutput } from "./web/sdk";
33
// @ts-ignore
4-
import wasm from "./web/sdk-multicore/sdk_bg.wasm?url";
4+
import wasm from "./web/sdk/sdk_bg.wasm?url";
55

66
export const init: () => Promise<InitOutput> = async () => await initWasm(wasm);

src/wasm/init-thread-pool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// TODO: Fix this export
2-
export { initThreadPool } from "./web/sdk-multicore";
2+
export { initThreadPool } from "./web/sdk";

src/wasm/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import initWasm, { InitInput, InitOutput } from "./web/sdk-multicore";
1+
import initWasm, { InitInput, InitOutput } from "./web/sdk";
22

33
export const init: (wasm: InitInput) => Promise<InitOutput> = async (wasm) => {
44
return await initWasm(wasm);

src/wasm/loader/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./web";
2+
export * from "./node";
3+
export * from "./types";
4+
export * from "./init";

src/wasm/loader/init.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Sdk } from "sdk";
2+
import {
3+
InitializedWasm,
4+
InitProps,
5+
SdkBuildOptions,
6+
SdkWasmOptions,
7+
} from "./types";
8+
import { initializeWeb } from "./web";
9+
import { initializeNode } from "./node";
10+
11+
// Load and initialize SDK according to options
12+
const initializeSdk = async (
13+
buildOpts: SdkBuildOptions,
14+
sdkOpts: SdkWasmOptions,
15+
): Promise<InitializedWasm> => {
16+
const { target } = buildOpts;
17+
18+
if (target === "web") {
19+
return initializeWeb(buildOpts, sdkOpts);
20+
} else {
21+
return initializeNode(buildOpts, sdkOpts);
22+
}
23+
};
24+
25+
/**
26+
* Public initializer for SDK
27+
*/
28+
export const initSdk = async (props: InitProps) => {
29+
const { rpcUrl, token, maspIndexerUrl, dbName = "", options } = props;
30+
const target = options?.target ?? "web";
31+
const multicore = options?.multicore ?? false;
32+
const inline = options?.inline ?? false;
33+
34+
// Import and initialize wasm
35+
const { cryptoMemory, query, sdk } = await initializeSdk(
36+
{ target, multicore, inline },
37+
{
38+
rpcUrl,
39+
token,
40+
maspIndexerUrl,
41+
dbName,
42+
},
43+
);
44+
45+
return new Sdk(sdk, query, cryptoMemory, rpcUrl, token);
46+
};

src/wasm/loader/node.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { InitializedWasm, SdkBuildOptions, SdkWasmOptions } from "./types";
2+
3+
export const initializeNode = async (
4+
buildOpts: SdkBuildOptions,
5+
sdkOpts: SdkWasmOptions,
6+
): Promise<InitializedWasm> => {
7+
const { target, multicore } = buildOpts;
8+
const { rpcUrl, maspIndexerUrl, dbName, token } = sdkOpts;
9+
const sdkPath = `./${target}/sdk${multicore ? "-multicore" : ""}`;
10+
const wasmPath = `${sdkPath}/sdk_bg.wasm`;
11+
const wasm = await import(wasmPath);
12+
13+
if (multicore) {
14+
const {
15+
default: initWasmSync,
16+
Sdk: SdkWasm,
17+
Query: QueryWasm,
18+
initThreadPool,
19+
} = await import(sdkPath);
20+
const { cryptoMemory } = initWasmSync(wasm);
21+
await initThreadPool(navigator.hardwareConcurrency);
22+
const sdk = new SdkWasm(rpcUrl, token, dbName);
23+
const query = new QueryWasm(rpcUrl, maspIndexerUrl);
24+
return { cryptoMemory, query, sdk };
25+
} else {
26+
const {
27+
default: initWasmSync,
28+
Sdk: SdkWasm,
29+
Query: QueryWasm,
30+
} = await import(sdkPath);
31+
const cryptoMemory = (crypto as any).__wasm.memory;
32+
initWasmSync(wasm);
33+
const sdk = new SdkWasm(rpcUrl, token, dbName);
34+
const query = new QueryWasm(rpcUrl, maspIndexerUrl);
35+
return { cryptoMemory, query, sdk };
36+
}
37+
};

src/wasm/loader/types.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { Sdk as SdkWasmType, Query as QueryWasmType } from "wasm/web/sdk";
2+
3+
export type WasmTarget = "web" | "nodejs";
4+
export type SdkBuildOptions = {
5+
target?: WasmTarget;
6+
multicore?: boolean;
7+
inline?: boolean;
8+
};
9+
10+
export type SdkWasmOptions = {
11+
rpcUrl: string;
12+
token: string;
13+
maspIndexerUrl?: string;
14+
dbName?: string;
15+
};
16+
export type InitProps = SdkWasmOptions & {
17+
options?: SdkBuildOptions;
18+
};
19+
20+
export type InitializedWasm = {
21+
sdk: SdkWasmType;
22+
query: QueryWasmType;
23+
cryptoMemory: WebAssembly.Memory;
24+
};

src/wasm/loader/web.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { InitializedWasm, SdkBuildOptions, SdkWasmOptions } from "./types";
2+
3+
export const initializeWeb = async (
4+
buildOpts: SdkBuildOptions,
5+
sdkOpts: SdkWasmOptions,
6+
): Promise<InitializedWasm> => {
7+
const { target, multicore, inline } = buildOpts;
8+
const { rpcUrl, maspIndexerUrl, dbName, token } = sdkOpts;
9+
const sdkPath = `./${target}/sdk${multicore ? "-multicore" : ""}`;
10+
const wasmPath = `${sdkPath}/sdk_bg.wasm${inline ? "?url" : ""}`;
11+
const wasm = await import(wasmPath);
12+
13+
const {
14+
default: initWasm,
15+
Sdk: SdkWasm,
16+
Query: QueryWasm,
17+
} = await import(sdkPath);
18+
19+
const { cryptoMemory } = initWasm(wasm);
20+
const sdk = new SdkWasm(rpcUrl, token, dbName);
21+
const query = new QueryWasm(rpcUrl, maspIndexerUrl);
22+
return {
23+
cryptoMemory,
24+
query,
25+
sdk,
26+
};
27+
};

src/wasm/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TxType } from "./web/sdk-multicore";
1+
import { TxType } from "./web/sdk";
22

33
export type SupportedTx = Extract<
44
TxType,

0 commit comments

Comments
 (0)