@rspack/binding-wasm32-wasi 1.4.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022-present Bytedance Inc and its affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ <picture>
2
+ <img alt="Rspack Banner" src="https://assets.rspack.rs/rspack/rspack-banner.png">
3
+ </picture>
4
+
5
+ # @rspack/binding-wasm32-wasi
6
+
7
+ Private node binding crate for rspack.
8
+
9
+ This package does *NOT* follow [semantic versioning](https://semver.org/).
10
+
11
+ ## Documentation
12
+
13
+ See [https://rspack.rs](https://rspack.rs) for details.
14
+
15
+ ## License
16
+
17
+ Rspack is [MIT licensed](https://github.com/web-infra-dev/rspack/blob/main/LICENSE).
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@rspack/binding-wasm32-wasi",
3
+ "version": "1.4.0-beta.1",
4
+ "license": "MIT",
5
+ "description": "Node binding for rspack",
6
+ "main": "rspack.wasi.cjs",
7
+ "browser": "rspack.wasi-browser.js",
8
+ "homepage": "https://rspack.rs",
9
+ "bugs": "https://github.com/web-infra-dev/rspack/issues",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/web-infra-dev/rspack"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public",
16
+ "provenance": true
17
+ },
18
+ "cpu": [
19
+ "wasm32"
20
+ ],
21
+ "files": [
22
+ "rspack.wasm32-wasi.wasm",
23
+ "rspack.wasi.cjs",
24
+ "rspack.wasi-browser.js",
25
+ "wasi-worker.mjs",
26
+ "wasi-worker-browser.mjs"
27
+ ],
28
+ "dependencies": {
29
+ "@napi-rs/wasm-runtime": "^0.2.11"
30
+ }
31
+ }
@@ -0,0 +1,63 @@
1
+ import {
2
+ createOnMessage as __wasmCreateOnMessageForFsProxy,
3
+ getDefaultContext as __emnapiGetDefaultContext,
4
+ instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
5
+ WASI as __WASI,
6
+ } from '@napi-rs/wasm-runtime'
7
+ import { memfs } from '@napi-rs/wasm-runtime/fs'
8
+ import __wasmUrl from './rspack.wasm32-wasi.wasm?url'
9
+
10
+ export const { fs: __fs, vol: __volume } = memfs()
11
+
12
+ const __wasi = new __WASI({
13
+ version: 'preview1',
14
+ fs: __fs,
15
+ preopens: {
16
+ '/': '/',
17
+ },
18
+ })
19
+
20
+ const __emnapiContext = __emnapiGetDefaultContext()
21
+
22
+ const __sharedMemory = new WebAssembly.Memory({
23
+ initial: 16384,
24
+ maximum: 65536,
25
+ shared: true,
26
+ })
27
+
28
+ const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
29
+
30
+ const {
31
+ instance: __napiInstance,
32
+ module: __wasiModule,
33
+ napiModule: __napiModule,
34
+ } = __emnapiInstantiateNapiModuleSync(__wasmFile, {
35
+ context: __emnapiContext,
36
+ asyncWorkPoolSize: 4,
37
+ wasi: __wasi,
38
+ onCreateWorker() {
39
+ const worker = new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), {
40
+ type: 'module',
41
+ })
42
+ worker.addEventListener('message', __wasmCreateOnMessageForFsProxy(__fs))
43
+
44
+ return worker
45
+ },
46
+ overwriteImports(importObject) {
47
+ importObject.env = {
48
+ ...importObject.env,
49
+ ...importObject.napi,
50
+ ...importObject.emnapi,
51
+ memory: __sharedMemory,
52
+ }
53
+ return importObject
54
+ },
55
+ beforeInit({ instance }) {
56
+ for (const name of Object.keys(instance.exports)) {
57
+ if (name.startsWith('__napi_register__')) {
58
+ instance.exports[name]()
59
+ }
60
+ }
61
+ },
62
+ })
63
+ export default __napiModule.exports
@@ -0,0 +1,110 @@
1
+ /* eslint-disable */
2
+ /* prettier-ignore */
3
+
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ const __nodeFs = require('node:fs')
7
+ const __nodePath = require('node:path')
8
+ const { WASI: __nodeWASI } = require('node:wasi')
9
+ const { Worker } = require('node:worker_threads')
10
+
11
+ const {
12
+ createOnMessage: __wasmCreateOnMessageForFsProxy,
13
+ getDefaultContext: __emnapiGetDefaultContext,
14
+ instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync,
15
+ } = require('@napi-rs/wasm-runtime')
16
+
17
+ const __rootDir = __nodePath.parse(process.cwd()).root
18
+
19
+ const __wasi = new __nodeWASI({
20
+ version: 'preview1',
21
+ env: process.env,
22
+ preopens: {
23
+ [__rootDir]: __rootDir,
24
+ }
25
+ })
26
+
27
+ const __emnapiContext = __emnapiGetDefaultContext()
28
+
29
+ const __sharedMemory = new WebAssembly.Memory({
30
+ initial: 16384,
31
+ maximum: 65536,
32
+ shared: true,
33
+ })
34
+
35
+ let __wasmFilePath = __nodePath.join(__dirname, 'rspack.wasm32-wasi.wasm')
36
+ const __wasmDebugFilePath = __nodePath.join(__dirname, 'rspack.wasm32-wasi.debug.wasm')
37
+
38
+ if (__nodeFs.existsSync(__wasmDebugFilePath)) {
39
+ __wasmFilePath = __wasmDebugFilePath
40
+ } else if (!__nodeFs.existsSync(__wasmFilePath)) {
41
+ try {
42
+ __wasmFilePath = __nodePath.resolve('@rspack/binding-wasm32-wasi')
43
+ } catch {
44
+ throw new Error('Cannot find rspack.wasm32-wasi.wasm file, and @rspack/binding-wasm32-wasi package is not installed.')
45
+ }
46
+ }
47
+
48
+ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), {
49
+ context: __emnapiContext,
50
+ asyncWorkPoolSize: (function() {
51
+ const threadsSizeFromEnv = Number(process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ?? process.env.UV_THREADPOOL_SIZE)
52
+ // NaN > 0 is false
53
+ if (threadsSizeFromEnv > 0) {
54
+ return threadsSizeFromEnv
55
+ } else {
56
+ return 4
57
+ }
58
+ })(),
59
+ reuseWorker: true,
60
+ wasi: __wasi,
61
+ onCreateWorker() {
62
+ const worker = new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), {
63
+ env: process.env,
64
+ })
65
+ worker.onmessage = ({ data }) => {
66
+ __wasmCreateOnMessageForFsProxy(__nodeFs)(data)
67
+ }
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
+ }
91
+ return worker
92
+ },
93
+ overwriteImports(importObject) {
94
+ importObject.env = {
95
+ ...importObject.env,
96
+ ...importObject.napi,
97
+ ...importObject.emnapi,
98
+ memory: __sharedMemory,
99
+ }
100
+ return importObject
101
+ },
102
+ beforeInit({ instance }) {
103
+ for (const name of Object.keys(instance.exports)) {
104
+ if (name.startsWith('__napi_register__')) {
105
+ instance.exports[name]()
106
+ }
107
+ }
108
+ },
109
+ })
110
+ module.exports = __napiModule.exports
Binary file
@@ -0,0 +1,39 @@
1
+ import { instantiateNapiModuleSync, MessageHandler, WASI, createFsProxy } from '@napi-rs/wasm-runtime'
2
+ import { memfsExported as __memfsExported } from '@napi-rs/wasm-runtime/fs'
3
+
4
+ const fs = createFsProxy(__memfsExported)
5
+
6
+ const handler = new MessageHandler({
7
+ onLoad({ wasmModule, wasmMemory }) {
8
+ const wasi = new WASI({
9
+ fs,
10
+ preopens: {
11
+ '/': '/',
12
+ },
13
+ print: function () {
14
+ // eslint-disable-next-line no-console
15
+ console.log.apply(console, arguments)
16
+ },
17
+ printErr: function() {
18
+ // eslint-disable-next-line no-console
19
+ console.error.apply(console, arguments)
20
+ },
21
+ })
22
+ return instantiateNapiModuleSync(wasmModule, {
23
+ childThread: true,
24
+ wasi,
25
+ overwriteImports(importObject) {
26
+ importObject.env = {
27
+ ...importObject.env,
28
+ ...importObject.napi,
29
+ ...importObject.emnapi,
30
+ memory: wasmMemory,
31
+ }
32
+ },
33
+ })
34
+ },
35
+ })
36
+
37
+ globalThis.onmessage = function (e) {
38
+ handler.handle(e)
39
+ }
@@ -0,0 +1,63 @@
1
+ import fs from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import { parse } from "node:path";
4
+ import { WASI } from "node:wasi";
5
+ import { parentPort, Worker } from "node:worker_threads";
6
+
7
+ const require = createRequire(import.meta.url);
8
+
9
+ const { instantiateNapiModuleSync, MessageHandler, getDefaultContext } = require("@napi-rs/wasm-runtime");
10
+
11
+ if (parentPort) {
12
+ parentPort.on("message", (data) => {
13
+ globalThis.onmessage({ data });
14
+ });
15
+ }
16
+
17
+ Object.assign(globalThis, {
18
+ self: globalThis,
19
+ require,
20
+ Worker,
21
+ importScripts: function (f) {
22
+ ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
23
+ },
24
+ postMessage: function (msg) {
25
+ if (parentPort) {
26
+ parentPort.postMessage(msg);
27
+ }
28
+ },
29
+ });
30
+
31
+ const emnapiContext = getDefaultContext();
32
+
33
+ const __rootDir = parse(process.cwd()).root;
34
+
35
+ const handler = new MessageHandler({
36
+ onLoad({ wasmModule, wasmMemory }) {
37
+ const wasi = new WASI({
38
+ version: 'preview1',
39
+ env: process.env,
40
+ preopens: {
41
+ [__rootDir]: __rootDir,
42
+ },
43
+ });
44
+
45
+ return instantiateNapiModuleSync(wasmModule, {
46
+ childThread: true,
47
+ wasi,
48
+ context: emnapiContext,
49
+ overwriteImports(importObject) {
50
+ importObject.env = {
51
+ ...importObject.env,
52
+ ...importObject.napi,
53
+ ...importObject.emnapi,
54
+ memory: wasmMemory
55
+ };
56
+ },
57
+ });
58
+ },
59
+ });
60
+
61
+ globalThis.onmessage = function (e) {
62
+ handler.handle(e);
63
+ };