Skip to content

Commit ae27672

Browse files
authored
Merge pull request #1 from oligamiq/use-br
Use br
2 parents c5a6bcf + 71016d6 commit ae27672

21 files changed

+823
-322
lines changed

RELEASE.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Release notes
2+
# v1.1.0 (2024-12-8)
3+
- load time optimization
4+
18s -> 10s
5+
Finer-grained asynchronous fetch.
6+
File size reduction due to brotli compression.
7+
Change wasm compile method from compile to compileStreaming.
8+
- lazy loading monaco editor
9+
10+
## v1.0.0 (2024-11-26)
11+
- Initial release

bun.lockb

6.77 KB
Binary file not shown.

lib/package-lock.json

+56-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@oligami/rustc-browser-wasi_shim",
3-
"version": "0.1.0",
3+
"version": "1.1.0",
44
"type": "module",
55
"scripts": {
66
"dev": "vite",
@@ -12,8 +12,10 @@
1212
},
1313
"dependencies": {
1414
"@bjorn3/browser_wasi_shim": "^0.3.0",
15-
"@oligami/browser_wasi_shim-threads": "^0.1.0",
16-
"@oligami/rustc-browser-wasi_shim": "file:"
15+
"@oligami/browser_wasi_shim-threads": "^0.1.1",
16+
"@oligami/rustc-browser-wasi_shim": "file:",
17+
"brotli-dec-wasm": "^2.3.0",
18+
"nanotar": "^0.1.1"
1719
},
1820
"private": false,
1921
"publishConfig": {

lib/src/brotli_stream.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import init, { BrotliDecStream, BrotliStreamResultCode } from "brotli-dec-wasm/web"; // Import the default export
2+
// @ts-ignore
3+
import brotli_dec_wasm_bg from "brotli-dec-wasm/web/bg.wasm?wasm&url"; // Import the wasm file
4+
5+
const promise = init(brotli_dec_wasm_bg); // Import is async in browsers due to wasm requirements!
6+
7+
// 1MB output buffer
8+
const OUTPUT_SIZE = 1024 * 1024;
9+
10+
export const get_brotli_decompress_stream = async (): Promise<
11+
TransformStream<Uint8Array, Uint8Array>
12+
> => {
13+
await promise;
14+
15+
const decompressStream = new BrotliDecStream();
16+
const decompressionStream = new TransformStream({
17+
transform(chunk, controller) {
18+
let resultCode: number;
19+
let inputOffset = 0;
20+
21+
// Decompress this chunk, producing up to OUTPUT_SIZE output bytes at a time, until the
22+
// entire input has been decompressed.
23+
24+
do {
25+
const input = chunk.slice(inputOffset);
26+
const result = decompressStream.decompress(input, OUTPUT_SIZE);
27+
controller.enqueue(result.buf);
28+
resultCode = result.code;
29+
inputOffset += result.input_offset;
30+
} while (resultCode === BrotliStreamResultCode.NeedsMoreOutput);
31+
if (
32+
resultCode !== BrotliStreamResultCode.NeedsMoreInput &&
33+
resultCode !== BrotliStreamResultCode.ResultSuccess
34+
) {
35+
controller.error(`Brotli decompression failed with code ${resultCode}`);
36+
}
37+
},
38+
flush(controller) {
39+
controller.terminate();
40+
},
41+
});
42+
return decompressionStream;
43+
};
44+
45+
export const fetch_compressed_stream = async (
46+
url: string | URL | globalThis.Request,
47+
): Promise<ReadableStream<Uint8Array>> => {
48+
const compressed_stream = await fetch(
49+
url,
50+
);
51+
if (!compressed_stream.ok) {
52+
throw new Error("Failed to fetch wasm");
53+
}
54+
if (!compressed_stream.body) {
55+
throw new Error("No body in response");
56+
}
57+
58+
return compressed_stream.body.pipeThrough(await get_brotli_decompress_stream());
59+
}

lib/src/get_llvm_wasm.ts

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
import { parseTarGzip } from 'nanotar';
1+
import { get_wasm } from "./get_wasm";
22

3-
export const get_llvm_wasm = async (): Promise<WebAssembly.Module> => {
4-
const zipped_wasm = await fetch(
5-
// "https://oligamiq.github.io/rust_wasm/v0.1.0/wasm32-wasip1.tar.gz",
6-
"https://oligamiq.github.io/rust_wasm/v0.1.0/llvm_opt.wasm.tar.gz",
7-
);
8-
const files = await parseTarGzip(await zipped_wasm.arrayBuffer());
9-
const file = files[0];
10-
const wasmFile = file.data;
11-
if (!wasmFile) {
12-
throw new Error("Wasm file not found");
13-
}
14-
const wasm = await WebAssembly.compile(wasmFile);
15-
return wasm;
16-
}
3+
export const get_llvm_wasm = () => get_wasm(
4+
"https://oligamiq.github.io/rust_wasm/v0.2.0/llvm_opt.wasm.br");

lib/src/get_rustc_wasm.ts

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
import { parseTarGzip } from 'nanotar';
1+
import { get_wasm } from "./get_wasm";
22

3-
export const get_rustc_wasm = async (): Promise<WebAssembly.Module> => {
4-
const zipped_wasm = await fetch(
5-
// "https://oligamiq.github.io/rust_wasm/v0.1.0/wasm32-wasip1.tar.gz",
6-
"https://oligamiq.github.io/rust_wasm/v0.1.0/rustc_opt.wasm.tar.gz",
7-
);
8-
const files = await parseTarGzip(await zipped_wasm.arrayBuffer());
9-
const file = files[0];
10-
const wasmFile = file.data;
11-
if (!wasmFile) {
12-
throw new Error("Wasm file not found");
13-
}
14-
const wasm = await WebAssembly.compile(wasmFile);
15-
return wasm;
16-
}
3+
export const get_rustc_wasm = () => get_wasm(
4+
"https://oligamiq.github.io/rust_wasm/v0.2.0/rustc_opt.wasm.br");

lib/src/get_wasm.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { fetch_compressed_stream } from "./brotli_stream";
2+
3+
export const get_wasm = async (
4+
url: string | URL | globalThis.Request
5+
): Promise<WebAssembly.Module> => {
6+
const response = new Response(
7+
await fetch_compressed_stream(
8+
url,
9+
),
10+
{
11+
headers: { "Content-Type": "application/wasm" },
12+
},
13+
);
14+
15+
const wasm = await WebAssembly.compileStreaming(response);
16+
17+
return wasm;
18+
};
19+

lib/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import './sysroot';
2-
import './get_rustc_wasm';
3-
import './get_llvm_wasm';
1+
import "./sysroot";
2+
import "./get_rustc_wasm";
3+
import "./get_llvm_wasm";

0 commit comments

Comments
 (0)