-
Notifications
You must be signed in to change notification settings - Fork 22
chore: set target web/nodejs for crisp zk-inputs #1006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
94e07ca
chore: set target web/nodejs for crisp zk-inputs
cedoor d7d8566
docs: update crisp-zk-inputs README.md
cedoor 2925170
refactor: rename initializeWasm to init
cedoor 0f93294
refactor: parallel promises in build script
cedoor 1bce676
docs: add README headings
cedoor f7ac63c
Merge branch 'main' into chore/crisp-wasm
cedoor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Wasm bundle for crisp-zk-inputs | ||
|
|
||
| Here we export wasm functionality for consumption in TypeScript to enable us to share code between Rust and TypeScript. | ||
|
|
||
| ## Usage | ||
|
|
||
| This package exposes an `init` subpackage default function which should be used to universally load the wasm module instead of exporting the default loader. | ||
|
|
||
| This is because in modern node there is no need for preloading however in the browser we still need to load the wasm bundle. | ||
|
|
||
| ### ❌ DONT USE THE DEFAULT INIT | ||
|
|
||
| ```ts | ||
| // Bad! Because this uses the raw loader which doesn't exist in node contexts | ||
| import init, { bfvEncryptNumber } from "@crisp-e3/zk-inputs"; | ||
| ``` | ||
|
|
||
| ### ✅ DO USE THE EXPORTED SUBMODULE | ||
|
|
||
| ```ts | ||
| // Good! Use the universal loader | ||
| import init from "@crisp-e3/zk-inputs/init"; | ||
|
|
||
| await init(); | ||
| // other package imports here | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // 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. | ||
|
|
||
| declare function init(): Promise<void>; | ||
| export default init; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // 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. | ||
|
|
||
| module.exports = async function init() { | ||
| // Node does not need to be loaded async | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // 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. | ||
|
|
||
| export default async function init() { | ||
| // Node does not need to be loaded async | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // 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 * as bindgen from "./dist/web/index.js"; | ||
|
|
||
| let promise; | ||
|
|
||
| export default async function init() { | ||
| promise ??= (async () => { | ||
| const { default: base64 } = await import("./dist/web/index_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; | ||
| } | ||
|
cedoor marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // 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 { resolve } from "path"; | ||
| import replaceInFile from "replace-in-file"; | ||
|
|
||
| try { | ||
| // Build WASM with web and node target - generates index.js and index_bg.wasm. | ||
| const distWeb = resolve(process.cwd(), "dist/web"); | ||
| const distNode = resolve(process.cwd(), "dist/node"); | ||
|
|
||
| await execa("wasm-pack", [ | ||
| "build", | ||
| "../../crates/zk-inputs-wasm", | ||
| "--target=web", | ||
| `--out-dir=${distWeb}`, | ||
| "--no-pack", | ||
| "--out-name=index", | ||
| ]); | ||
| await execa("wasm-pack", [ | ||
| "build", | ||
| "../../crates/zk-inputs-wasm", | ||
| "--target=nodejs", | ||
| `--out-dir=${distNode}`, | ||
| "--no-pack", | ||
| "--out-name=index", | ||
| ]); | ||
|
|
||
| // Convert WASM binary to base64 for bundler compatibility. | ||
| const wasmBinary = await readFile("./dist/web/index_bg.wasm"); | ||
| const base64Src = `export default '${wasmBinary.toString("base64")}';\n`; | ||
|
|
||
| // Parallel cleanup and JS modification to prevent Next.js and other bundlers static analysis issues. | ||
| await Promise.all([ | ||
| rm("./dist/web/index_bg.wasm", { force: true }), | ||
| rm("./dist/web/index_bg.wasm.d.ts", { force: true }), | ||
| rm("./dist/web/.gitignore", { force: true }), | ||
| rm("./dist/node/.gitignore", { force: true }), | ||
| replaceInFile({ | ||
| files: "./dist/web/index.js", | ||
| from: /module_or_path\s*=\s*new URL\(['"]index_bg\.wasm['"],\s*import\.meta\.url\);\s*/g, | ||
| to: "/* wasm URL disabled: load via @crisp-e3/zk-inputs/init */\n", | ||
| }), | ||
| writeFile("./dist/web/index_base64.js", base64Src), | ||
| ]); | ||
|
cedoor marked this conversation as resolved.
|
||
| } catch (error) { | ||
| console.error(error); | ||
| process.exit(1); | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.