wasm-sandbox-cli 1.0.0

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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +88 -0
  3. package/cli.js +117 -0
  4. package/package.json +32 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 N-API for Rust
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,88 @@
1
+ # wasm-sandbox
2
+
3
+ Node.js WebAssembly sandbox CLI tool built on [@wasm-sandbox/runtime](https://www.npmjs.com/package/@wasm-sandbox/runtime).
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install -g wasmsand-box
9
+ # or use npx
10
+ npx wasmsand-box <command>
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Run WebAssembly Module
16
+
17
+ ```bash
18
+ # Basic run
19
+ wasmsand-box run example.wasm
20
+
21
+ # Pass arguments
22
+ wasmsand-box run example.wasm arg1 arg2 arg3
23
+
24
+ # Invoke specific function
25
+ wasmsand-box run --invoke add example.wasm 1 2
26
+
27
+ # Set working directory
28
+ wasmsand-box run --work-dir ./ example.wasm
29
+
30
+ # Map directory
31
+ wasmsand-box run --map-dir ./host/path::/guest/path example.wasm
32
+
33
+ # Set environment variables
34
+ wasmsand-box run --env FOO=BAR --env BAZ=QUX example.wasm
35
+
36
+ # Limit resources
37
+ wasmsand-box run --wasm-timeout 10 --wasm-max-memory-size 1073741824 example.wasm
38
+ ```
39
+
40
+ ### Start HTTP Server
41
+
42
+ ```bash
43
+ # Basic server
44
+ wasmsand-box serve example.wasm
45
+
46
+ # Specify port and IP
47
+ wasmsand-box serve -p 3000 -i 127.0.0.1 example.wasm
48
+
49
+ # Configure network access
50
+ wasmsand-box serve --allowed-outbound-hosts https://*.github.com example.wasm
51
+
52
+ # Set config variables
53
+ wasmsand-box serve --config-var KEY=VALUE --keyvalue-var DATA=VALUE example.wasm
54
+ ```
55
+
56
+ ## Command Options
57
+
58
+ ### run Command
59
+
60
+ | Option | Description |
61
+ |--------|-------------|
62
+ | `--program-name <name>` | Override argv[0] value |
63
+ | `--invoke <function>` | Invoke specific function |
64
+ | `--work-dir <dir>` | Grant host directory access |
65
+ | `--map-dir <mapping>` | Map host directory to guest, format: `host::guest` |
66
+ | `--env <var>` | Pass environment variable, format: `NAME=VALUE` |
67
+ | `--allowed-outbound-hosts <host>` | Allowed network destinations |
68
+ | `--block-networks <network>` | Blocked IP networks |
69
+ | `--wasm-timeout <seconds>` | Maximum execution time (seconds) |
70
+ | `--wasm-max-memory-size <bytes>` | Maximum memory size (bytes) |
71
+ | `--wasm-max-stack <bytes>` | Maximum stack size (bytes) |
72
+ | `--wasm-fuel <units>` | Execution fuel units |
73
+ | `--wasm-cache-dir <dir>` | Precompiled module cache directory |
74
+
75
+ ### serve Command
76
+
77
+ | Option | Description |
78
+ |--------|-------------|
79
+ | `-i, --ip <ip>` | Bind IP address |
80
+ | `-p, --port <port>` | Bind port |
81
+ | `--config-var <var>` | WASI config variable, format: `NAME=VALUE` |
82
+ | `--keyvalue-var <var>` | WASI key-value API preset data |
83
+
84
+ Other options are same as `run` command.
85
+
86
+ ## License
87
+
88
+ MIT
package/cli.js ADDED
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { readFileSync } from 'fs'
4
+ import { fileURLToPath } from 'url'
5
+ import { dirname, join } from 'path'
6
+
7
+ import { Command } from 'commander';
8
+ import wasm_sandbox from '@wasm-sandbox/runtime';
9
+
10
+ const __dirname = dirname(fileURLToPath(import.meta.url))
11
+ const { name, version, description } = JSON.parse(readFileSync(join(__dirname, 'package.json'), 'utf8'))
12
+
13
+ const program = new Command();
14
+
15
+ program
16
+ .name(name)
17
+ .version(version)
18
+ .description(description);
19
+
20
+ program
21
+ .command('run <wasm> [args...]')
22
+ .option('--program-name <name>')
23
+ .option('--invoke <function>')
24
+ .option('--work-dir <dir>')
25
+ .option('--map-dir <mapping...>')
26
+ .option('--env <var...>')
27
+ .option('--allowed-outbound-hosts <host...>')
28
+ .option('--block-networks <network...>')
29
+ .option('--wasm-timeout <seconds>', '', parseInt)
30
+ .option('--wasm-max-memory-size <bytes>', '', parseInt)
31
+ .option('--wasm-max-stack <bytes>', '', parseInt)
32
+ .option('--wasm-fuel <units>', '', parseInt)
33
+ .option('--wasm-cache-dir <dir>')
34
+ .action((wasm, args, opts) => {
35
+ wasm_sandbox.run({
36
+ wasmFile: wasm,
37
+ args,
38
+ programName: opts.programName,
39
+ invoke: opts.invoke,
40
+ workDir: opts.workDir,
41
+ mapDirs: opts.mapDir?.map(m => {
42
+ const [key, value] = m.split('::');
43
+ return { key, value: value || key };
44
+ }),
45
+ envVars: opts.env?.map(e => {
46
+ const [key, value] = e.split('=');
47
+ return { key, value };
48
+ }),
49
+ allowedOutboundHosts: opts.allowedOutboundHosts,
50
+ blockNetworks: opts.blockNetworks,
51
+ wasmTimeout: opts.wasmTimeout,
52
+ wasmMaxMemorySize: opts.wasmMaxMemorySize,
53
+ wasmMaxWasmStack: opts.wasmMaxStack,
54
+ wasmFuel: opts.wasmFuel,
55
+ wasmCacheDir: opts.wasmCacheDir
56
+ });
57
+ });
58
+
59
+ program
60
+ .command('serve <wasm>')
61
+ .option('-i, --ip <ip>')
62
+ .option('-p, --port <port>', '', parseInt)
63
+ .option('--work-dir <dir>')
64
+ .option('--map-dir <mapping...>')
65
+ .option('--env <var...>')
66
+ .option('--allowed-outbound-hosts <host...>')
67
+ .option('--block-networks <network...>')
68
+ .option('--config-var <var...>')
69
+ .option('--keyvalue-var <var...>')
70
+ .option('--wasm-timeout <seconds>', '', parseInt)
71
+ .option('--wasm-max-memory-size <bytes>', '', parseInt)
72
+ .option('--wasm-max-stack <bytes>', '', parseInt)
73
+ .option('--wasm-fuel <units>', '', parseInt)
74
+ .option('--wasm-cache-dir <dir>')
75
+ .action((wasm, opts) => {
76
+ const mapDirs = {};
77
+ opts.mapDir?.forEach(m => {
78
+ const [key, value] = m.split('::');
79
+ mapDirs[key] = value || key;
80
+ });
81
+
82
+ const envVars = {};
83
+ opts.env?.forEach(e => {
84
+ const [key, value] = e.split('=');
85
+ envVars[key] = value;
86
+ });
87
+
88
+ const configVars = opts.configVar?.map(v => {
89
+ const [key, value] = v.split('=');
90
+ return { key, value };
91
+ });
92
+
93
+ const keyvalueVars = opts.keyvalueVar?.map(v => {
94
+ const [key, value] = v.split('=');
95
+ return { key, value };
96
+ });
97
+
98
+ wasm_sandbox.serve({
99
+ wasmFile: wasm,
100
+ ip: opts.ip,
101
+ port: opts.port,
102
+ workDir: opts.workDir,
103
+ mapDirs,
104
+ envVars,
105
+ allowedOutboundHosts: opts.allowedOutboundHosts,
106
+ blockNetworks: opts.blockNetworks,
107
+ configVars,
108
+ keyvalueVars,
109
+ wasmTimeout: opts.wasmTimeout,
110
+ wasmMaxMemorySize: opts.wasmMaxMemorySize,
111
+ wasmMaxWasmStack: opts.wasmMaxStack,
112
+ wasmFuel: opts.wasmFuel,
113
+ wasmCacheDir: opts.wasmCacheDir
114
+ });
115
+ });
116
+
117
+ program.parse();
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "wasm-sandbox-cli",
3
+ "version": "1.0.0",
4
+ "description": "wasm-sandbox cli tool",
5
+ "author": "guyoung",
6
+ "homepage": "https://github.com/guyoung/wasm-sandbox-node/tree/main/cli",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/guyoung/wasm-sandbox-node.git"
10
+ },
11
+ "license": "MIT",
12
+ "keywords": [
13
+ "wasm",
14
+ "webassembly",
15
+ "wasi",
16
+ "sandbox"
17
+ ],
18
+ "files": [
19
+ "cli.js",
20
+ "README.md",
21
+ "README_zh-CN.md"
22
+ ],
23
+ "type": "module",
24
+ "bin": {
25
+ "wasmsand-box": "cli.js",
26
+ "wasmsand-box-cli": "cli.js"
27
+ },
28
+ "dependencies": {
29
+ "@wasm-sandbox/runtime": "^1.0.5",
30
+ "commander": "^14.0.3"
31
+ }
32
+ }