Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/wasm/.npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
src/
scripts/
*.ts
tsconfig.json
.gitignore
node_modules/
23 changes: 21 additions & 2 deletions crates/wasm/init_web.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,26 @@
// without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.

import init from "./dist/web/e3_wasm.js";
import * as bindgen from "./dist/web/e3_wasm.js";

let promise;

export default async function initializeWasm(initParams) {
return await init(initParams);
promise ??= (async () => {
const { default: base64 } = await import("./dist/web/e3_wasm_base64.js");

const binaryString = atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);

for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}

bindgen.initSync(bytes);

return bindgen;
})();

return promise;
}
16 changes: 10 additions & 6 deletions crates/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "@enclave-e3/wasm",
"version": "0.0.13-test",
"description": "Wasm modules for enclave.",
"main": "dist/nodejs/e3_wasm.js",
"main": "dist/node/e3_wasm.js",
"module": "dist/web/e3_wasm.js",
"types": "dist/web/e3_wasm.d.ts",
"files": [
"dist/",
"dist/nodejs/**",
"dist/node/**",
"dist/web/**",
"init_node.js",
"init_node.cjs",
Expand All @@ -18,15 +18,15 @@
"access": "public"
},
"scripts": {
"build": "./scripts/build.sh",
"build": "rm -rf dist && node scripts/build.js",
"prerelease": "pnpm build",
"release": "pnpm publish --access=public"
},
"exports": {
".": {
"node": {
"types": "./dist/nodejs/e3_wasm.d.ts",
"default": "./dist/nodejs/e3_wasm.js"
"types": "./dist/node/e3_wasm.d.ts",
"default": "./dist/node/e3_wasm.js"
},
"browser": {
"types": "./dist/web/e3_wasm.d.ts",
Expand Down Expand Up @@ -61,5 +61,9 @@
"url": "https://github.com/gnosisguild"
},
"license": "LGPL-3.0-only",
"packageManager": "pnpm@10.7.1+sha512.2d92c86b7928dc8284f53494fb4201f983da65f0fb4f0d40baafa5cf628fa31dae3e5968f12466f17df7e97310e30f343a648baea1b9b350685dafafffdf5808"
"packageManager": "pnpm@10.7.1+sha512.2d92c86b7928dc8284f53494fb4201f983da65f0fb4f0d40baafa5cf628fa31dae3e5968f12466f17df7e97310e30f343a648baea1b9b350685dafafffdf5808",
"devDependencies": {
"execa": "^8.0.1",
"replace-in-file": "^7.2.0"
}
}
48 changes: 48 additions & 0 deletions crates/wasm/scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: LGPL-3.0-only
//
// This file is provided WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.

import { execa } from "execa";
import { readFile, writeFile, rm } from "fs/promises";
import replaceInFile from "replace-in-file";

try {
// Build WASM with web and node target - generates e3_wasm.js and e3_wasm_bg.wasm.
await execa("wasm-pack", [
"build",
"--target=web",
"--out-dir=dist/web",
"--no-pack",
]);
await execa("wasm-pack", [
"build",
"--target=nodejs",
"--out-dir=dist/node",
"--no-pack",
]);

// Convert WASM binary to base64 for bundler compatibility.
const wasmBinary = await readFile("./dist/web/e3_wasm_bg.wasm");
const base64Src = `export default '${wasmBinary.toString("base64")}';\n`;

Comment thread
cedoor marked this conversation as resolved.
// Parallel cleanup and JS modification to prevent Next.js and other bundlers static analysis issues.
await Promise.all([
await Promise.all([
rm("./dist/web/e3_wasm_bg.wasm", { force: true }),
rm("./dist/web/e3_wasm_bg.wasm.d.ts", { force: true }),
rm("./dist/web/.gitignore", { force: true }),
rm("./dist/node/.gitignore", { force: true }),
]),
replaceInFile({
files: "./dist/web/e3_wasm.js",
from: /module_or_path\s*=\s*new URL\(['"]e3_wasm_bg\.wasm['"],\s*import\.meta\.url\);\s*/g,
to: "/* wasm URL disabled: load via @enclave-e3/wasm/init */\n",
}),
writeFile("./dist/web/e3_wasm_base64.js", base64Src),
]);
} catch (error) {
console.error(error);
process.exit(1);
}
4 changes: 0 additions & 4 deletions crates/wasm/scripts/build.sh

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@
},
"packageManager": "pnpm@10.7.1+sha512.2d92c86b7928dc8284f53494fb4201f983da65f0fb4f0d40baafa5cf628fa31dae3e5968f12466f17df7e97310e30f343a648baea1b9b350685dafafffdf5808",
"devDependencies": {
"husky": "^9.1.7",
"@changesets/cli": "^2.27.1"
"@changesets/cli": "^2.27.1",
"@types/node": "22.7.5",
"husky": "^9.1.7"
},
"pnpm": {
"overrides": {
Expand Down
23 changes: 22 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading