-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: simplified bb.js bundling (#11622)
Attempt to be clever about wasm compression. In production, it's highly likely servers will provide the correct `Content-Encoding` and `Content-type` headers so that the browser can decompress on the fly. However, that's a big assumption in development, and configuring webpack-server/vite/web-test-runner or any other of the 1000s of options that can be used creates friction with consumers. Also, initial attempt at providing an external URL for wasm resolution, so a version with debug symbols (for example) can be hot-swapped without recompilation.
- Loading branch information
Showing
16 changed files
with
70 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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 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 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
22 changes: 19 additions & 3 deletions
22
barretenberg/ts/src/barretenberg_wasm/fetch_code/browser/index.ts
This file contains 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 |
---|---|---|
@@ -1,9 +1,25 @@ | ||
import barretenbergModule from '../../barretenberg.wasm.gz'; | ||
import barretenbergThreadsModule from '../../barretenberg-threads.wasm.gz'; | ||
import pako from 'pako'; | ||
|
||
// Annoyingly the wasm declares if it's memory is shared or not. So now we need two wasms if we want to be | ||
// able to fallback on "non shared memory" situations. | ||
export async function fetchCode(multithreaded: boolean) { | ||
const res = await fetch(multithreaded ? barretenbergThreadsModule : barretenbergModule); | ||
return res.arrayBuffer(); | ||
export async function fetchCode(multithreaded: boolean, wasmPath?: string) { | ||
let url = multithreaded ? barretenbergThreadsModule : barretenbergModule; | ||
url = wasmPath ? `${wasmPath}/${/[^/]+(?=\/$|$)/.exec(url)?.[0]}` : url; | ||
const res = await fetch(url); | ||
const maybeCompressedData = await res.arrayBuffer(); | ||
const buffer = new Uint8Array(maybeCompressedData); | ||
const isGzip = | ||
// Check magic number | ||
buffer[0] === 0x1f && | ||
buffer[1] === 0x8b && | ||
// Check compression method: | ||
buffer[2] === 0x08; | ||
if (isGzip) { | ||
const decompressedData = pako.ungzip(buffer); | ||
return decompressedData.buffer; | ||
} else { | ||
return buffer; | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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 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 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