Skip to content

Commit f6613b0

Browse files
committed
chore: async init wasm on browser
1 parent 829ec1a commit f6613b0

File tree

6 files changed

+41
-11
lines changed

6 files changed

+41
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"website"
1313
],
1414
"devDependencies": {
15-
"@napi-rs/cli": "^3.0.0-alpha.91",
15+
"@napi-rs/cli": "^3.0.0-alpha.92",
1616
"@taplo/cli": "^0.7.0",
1717
"@types/node": "^24.0.4",
1818
"@types/sharp": "^0.32.0",

packages/binding/image.wasi-browser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
createOnMessage as __wasmCreateOnMessageForFsProxy,
33
getDefaultContext as __emnapiGetDefaultContext,
4-
instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
4+
instantiateNapiModule as __emnapiInstantiateNapiModule,
55
WASI as __WASI,
66
} from '@napi-rs/wasm-runtime'
77

@@ -25,7 +25,7 @@ const {
2525
instance: __napiInstance,
2626
module: __wasiModule,
2727
napiModule: __napiModule,
28-
} = __emnapiInstantiateNapiModuleSync(__wasmFile, {
28+
} = await __emnapiInstantiateNapiModule(__wasmFile, {
2929
context: __emnapiContext,
3030
asyncWorkPoolSize: 4,
3131
wasi: __wasi,

packages/binding/image.wasi.cjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
6565
worker.onmessage = ({ data }) => {
6666
__wasmCreateOnMessageForFsProxy(__nodeFs)(data)
6767
}
68+
69+
// The main thread of Node.js waits for all the active handles before exiting.
70+
// But Rust threads are never waited without `thread::join`.
71+
// So here we hack the code of Node.js to prevent the workers from being referenced (active).
72+
// According to https://github.com/nodejs/node/blob/19e0d472728c79d418b74bddff588bea70a403d0/lib/internal/worker.js#L415,
73+
// a worker is consist of two handles: kPublicPort and kHandle.
74+
{
75+
const kPublicPort = Object.getOwnPropertySymbols(worker).find(s =>
76+
s.toString().includes("kPublicPort")
77+
);
78+
if (kPublicPort) {
79+
worker[kPublicPort].ref = () => {};
80+
}
81+
82+
const kHandle = Object.getOwnPropertySymbols(worker).find(s =>
83+
s.toString().includes("kHandle")
84+
);
85+
if (kPublicPort) {
86+
worker[kHandle].ref = () => {};
87+
}
88+
89+
worker.unref();
90+
}
6891
return worker
6992
},
7093
overwriteImports(importObject) {

packages/binding/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export declare class Transformer {
66
static fromSvg(input: string | Uint8Array, background?: string | undefined | null): Transformer
77
static fromRgbaPixels(input: Uint8Array | Uint8ClampedArray, width: number, height: number): Transformer
88
metadata(withExif?: boolean | undefined | null, signal?: AbortSignal | undefined | null): Promise<Metadata>
9+
metadataSync(withExif?: boolean | undefined | null): Metadata
910
/**
1011
* Rotate with exif orientation
1112
* If the orientation param is not null,

packages/binding/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@
4444
"x86_64-unknown-freebsd",
4545
"i686-pc-windows-msvc",
4646
"wasm32-wasi-preview1-threads"
47-
]
47+
],
48+
"wasm": {
49+
"browser": {
50+
"asyncInit": true,
51+
"fs": false
52+
}
53+
}
4854
},
4955
"license": "MIT",
5056
"engines": {
@@ -66,7 +72,7 @@
6672
},
6773
"repository": "[email protected]:Brooooooklyn/Image.git",
6874
"devDependencies": {
69-
"@napi-rs/cli": "^3.0.0-alpha.91",
75+
"@napi-rs/cli": "^3.0.0-alpha.92",
7076
"@napi-rs/wasm-runtime": "^0.2.11"
7177
}
7278
}

yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -953,9 +953,9 @@ __metadata:
953953
languageName: node
954954
linkType: hard
955955

956-
"@napi-rs/cli@npm:^3.0.0-alpha.91":
957-
version: 3.0.0-alpha.91
958-
resolution: "@napi-rs/cli@npm:3.0.0-alpha.91"
956+
"@napi-rs/cli@npm:^3.0.0-alpha.92":
957+
version: 3.0.0-alpha.92
958+
resolution: "@napi-rs/cli@npm:3.0.0-alpha.92"
959959
dependencies:
960960
"@inquirer/prompts": "npm:^7.4.0"
961961
"@napi-rs/cross-toolchain": "npm:^0.0.19"
@@ -982,7 +982,7 @@ __metadata:
982982
bin:
983983
napi: ./dist/cli.js
984984
napi-raw: ./cli.mjs
985-
checksum: 10c0/a1e8078b47b64551f41d1386a7f462a9d34728bbe7158e9d55348889a6100d6c3c970fc7e1d7e4898fce362074515e283cd4475275eea88a1614cab66f16b12b
985+
checksum: 10c0/58118e413e9f3d98b991ce1869475c06b6a837c0373f43bb74e2509d48d7d7281d1b4b9c3d2b5aa45c11957d08b5e758a547ae9757c8299ef72fb02751f03208
986986
languageName: node
987987
linkType: hard
988988

@@ -1027,7 +1027,7 @@ __metadata:
10271027
version: 0.0.0-use.local
10281028
resolution: "@napi-rs/image@workspace:packages/binding"
10291029
dependencies:
1030-
"@napi-rs/cli": "npm:^3.0.0-alpha.91"
1030+
"@napi-rs/cli": "npm:^3.0.0-alpha.92"
10311031
"@napi-rs/wasm-runtime": "npm:^0.2.11"
10321032
languageName: unknown
10331033
linkType: soft
@@ -6649,7 +6649,7 @@ __metadata:
66496649
version: 0.0.0-use.local
66506650
resolution: "image@workspace:."
66516651
dependencies:
6652-
"@napi-rs/cli": "npm:^3.0.0-alpha.91"
6652+
"@napi-rs/cli": "npm:^3.0.0-alpha.92"
66536653
"@taplo/cli": "npm:^0.7.0"
66546654
"@types/node": "npm:^24.0.4"
66556655
"@types/sharp": "npm:^0.32.0"

0 commit comments

Comments
 (0)