@wasm-oj/server 0.2.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.
- package/LICENSE +21 -0
- package/README.md +6 -0
- package/THIRD_PARTY_NOTICES.md +302 -0
- package/crates/runtime-core/Cargo.lock +5099 -0
- package/crates/runtime-core/Cargo.toml +66 -0
- package/crates/runtime-core/README.md +47 -0
- package/crates/runtime-core/src/bin/wasm-oj-compiler.rs +418 -0
- package/crates/runtime-core/src/bin/wasm-oj-runner.rs +294 -0
- package/crates/runtime-core/src/capabilities.rs +118 -0
- package/crates/runtime-core/src/compiler.rs +658 -0
- package/crates/runtime-core/src/contract.rs +5 -0
- package/crates/runtime-core/src/deterministic.rs +1051 -0
- package/crates/runtime-core/src/error.rs +58 -0
- package/crates/runtime-core/src/filesystem.rs +547 -0
- package/crates/runtime-core/src/filesystem_quota.rs +167 -0
- package/crates/runtime-core/src/go_compiler_session.rs +297 -0
- package/crates/runtime-core/src/interactive.rs +1019 -0
- package/crates/runtime-core/src/judge_package.rs +1539 -0
- package/crates/runtime-core/src/lib.rs +98 -0
- package/crates/runtime-core/src/memory.rs +84 -0
- package/crates/runtime-core/src/meter.rs +549 -0
- package/crates/runtime-core/src/module_imports.rs +149 -0
- package/crates/runtime-core/src/module_policy.rs +714 -0
- package/crates/runtime-core/src/output.rs +204 -0
- package/crates/runtime-core/src/run/mod.rs +208 -0
- package/crates/runtime-core/src/run/native.rs +260 -0
- package/crates/runtime-core/src/run/web.rs +229 -0
- package/crates/runtime-core/src/run/web_runtime.rs +109 -0
- package/crates/runtime-core/src/types.rs +268 -0
- package/crates/runtime-core/src/web.rs +83 -0
- package/dist/chunks/go-toolchain-Dbt-lp2L.js +426 -0
- package/dist/chunks/java-toolchain-DajoRCHu.js +44 -0
- package/dist/chunks/python-toolchain-Dx834o2A.js +4 -0
- package/dist/chunks/rust-toolchain-CJ3sMxPE.js +252 -0
- package/dist/chunks/toolchains-C6KuA1yM.js +224 -0
- package/dist/go-stage.mjs +193 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.js +4393 -0
- package/dist/java-stage.mjs +111 -0
- package/dist/python-stage.mjs +90 -0
- package/dist/rustc-stage.mjs +301 -0
- package/dist/server-build-stage.mjs +2564 -0
- package/dist/server-runner-stage.mjs +155 -0
- package/licenses/fflate-MIT.txt +21 -0
- package/licenses/runtime-core-dependencies.html +6253 -0
- package/licenses/runtime-core-dependencies.json +3041 -0
- package/licenses/wasmer-sdk-MIT.txt +21 -0
- package/licenses/wasmer-sdk-dependencies.html +6901 -0
- package/licenses/wasmer-sdk-dependencies.json +3013 -0
- package/package.json +70 -0
- package/rust-toolchain.toml +5 -0
- package/testdata/wojjdg02-v2-text.hex +1 -0
- package/vendor/shared-buffer/Cargo.toml +22 -0
- package/vendor/shared-buffer/LICENSE_APACHE.md +176 -0
- package/vendor/shared-buffer/LICENSE_MIT.md +25 -0
- package/vendor/shared-buffer/README.md +34 -0
- package/vendor/shared-buffer/src/lib.rs +58 -0
- package/vendor/shared-buffer/src/mmap.rs +250 -0
- package/vendor/shared-buffer/src/owned.rs +389 -0
- package/vendor/virtual-fs/Cargo.toml +181 -0
- package/vendor/virtual-fs/LICENSE +25 -0
- package/vendor/virtual-fs/src/arc_box_file.rs +142 -0
- package/vendor/virtual-fs/src/arc_file.rs +182 -0
- package/vendor/virtual-fs/src/arc_fs.rs +68 -0
- package/vendor/virtual-fs/src/buffer_file.rs +103 -0
- package/vendor/virtual-fs/src/builder.rs +232 -0
- package/vendor/virtual-fs/src/combine_file.rs +101 -0
- package/vendor/virtual-fs/src/cow_file.rs +345 -0
- package/vendor/virtual-fs/src/dual_write_file.rs +113 -0
- package/vendor/virtual-fs/src/empty_fs.rs +81 -0
- package/vendor/virtual-fs/src/filesystems.rs +108 -0
- package/vendor/virtual-fs/src/host_fs.rs +1390 -0
- package/vendor/virtual-fs/src/lib.rs +782 -0
- package/vendor/virtual-fs/src/limiter.rs +252 -0
- package/vendor/virtual-fs/src/mem_fs/file.rs +1799 -0
- package/vendor/virtual-fs/src/mem_fs/file_opener.rs +941 -0
- package/vendor/virtual-fs/src/mem_fs/filesystem.rs +2134 -0
- package/vendor/virtual-fs/src/mem_fs/mod.rs +245 -0
- package/vendor/virtual-fs/src/mem_fs/offloaded_file.rs +474 -0
- package/vendor/virtual-fs/src/mem_fs/stdio.rs +318 -0
- package/vendor/virtual-fs/src/mount_fs.rs +2225 -0
- package/vendor/virtual-fs/src/null_file.rs +87 -0
- package/vendor/virtual-fs/src/ops.rs +364 -0
- package/vendor/virtual-fs/src/overlay_fs.rs +2216 -0
- package/vendor/virtual-fs/src/passthru_fs.rs +119 -0
- package/vendor/virtual-fs/src/pipe.rs +603 -0
- package/vendor/virtual-fs/src/random_file.rs +88 -0
- package/vendor/virtual-fs/src/special_file.rs +108 -0
- package/vendor/virtual-fs/src/static_file.rs +133 -0
- package/vendor/virtual-fs/src/static_fs.rs +460 -0
- package/vendor/virtual-fs/src/tmp_fs.rs +95 -0
- package/vendor/virtual-fs/src/trace_fs.rs +258 -0
- package/vendor/virtual-fs/src/webc_volume_fs.rs +829 -0
- package/vendor/virtual-fs/src/zero_file.rs +90 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { O as PYTHON_COMPRESSED_PACKAGE_SHA256, j as PYTHON_PACKAGE_SHA256, k as PYTHON_PACKAGE } from "./chunks/toolchains-C6KuA1yM.js";
|
|
2
|
+
import { writeFileSync } from "node:fs";
|
|
3
|
+
import { serialize } from "node:v8";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { gunzipSync } from "node:zlib";
|
|
8
|
+
import { Runtime, Wasmer, init } from "@wasmer/sdk/node";
|
|
9
|
+
//#region src/server/server-runner-stage.mjs
|
|
10
|
+
var MAX_INPUT_BYTES = 1048576;
|
|
11
|
+
var MAX_RESULT_BYTES = 268435456;
|
|
12
|
+
var MAX_ERROR_CHARACTERS = 65536;
|
|
13
|
+
var runtime;
|
|
14
|
+
var pkg;
|
|
15
|
+
var commands = [];
|
|
16
|
+
var response;
|
|
17
|
+
var exitCode = 0;
|
|
18
|
+
try {
|
|
19
|
+
const input = parseInput(JSON.parse(await readStdin()));
|
|
20
|
+
await init({ log: "warn" });
|
|
21
|
+
runtime = new Runtime({ registry: null });
|
|
22
|
+
const packagePath = input.toolchainAsset;
|
|
23
|
+
const compressed = await readFile(packagePath);
|
|
24
|
+
if (!input.verifiedToolchain) verifyDigest(packagePath, compressed, PYTHON_COMPRESSED_PACKAGE_SHA256);
|
|
25
|
+
const expanded = uint8View(gunzipSync(compressed));
|
|
26
|
+
if (!input.verifiedToolchain) verifyDigest(packagePath, expanded, PYTHON_PACKAGE_SHA256);
|
|
27
|
+
pkg = await Wasmer.fromFile(expanded, runtime);
|
|
28
|
+
const commandMap = pkg.commands;
|
|
29
|
+
commands = uniqueCommands(commandMap);
|
|
30
|
+
const command = commandMap[input.request.command];
|
|
31
|
+
if (!command) throw new Error(`Package '${input.request.packageSpecifier}' does not expose '${input.request.command}'.`);
|
|
32
|
+
let bytes;
|
|
33
|
+
if (input.request.operation === "command-binary") {
|
|
34
|
+
bytes = command.binary().slice();
|
|
35
|
+
if (!WebAssembly.validate(bytes)) throw new Error(`Package '${input.request.packageSpecifier}' command '${input.request.command}' returned invalid WebAssembly.`);
|
|
36
|
+
} else {
|
|
37
|
+
const output = await (await command.run({
|
|
38
|
+
args: input.request.args,
|
|
39
|
+
env: {
|
|
40
|
+
PYTHONHOME: "/usr/local",
|
|
41
|
+
PYTHONHASHSEED: "0",
|
|
42
|
+
PYTHONDONTWRITEBYTECODE: "1"
|
|
43
|
+
}
|
|
44
|
+
})).wait();
|
|
45
|
+
if (!output.ok) throw new Error(`Unable to export runtime files from ${input.request.packageSpecifier}: exit ${output.code}: ${boundedText(output.stderr)}`);
|
|
46
|
+
bytes = output.stdoutBytes.slice();
|
|
47
|
+
}
|
|
48
|
+
if (bytes.byteLength > MAX_RESULT_BYTES) throw new Error(`Runner stage result is ${bytes.byteLength} bytes; the limit is ${MAX_RESULT_BYTES} bytes.`);
|
|
49
|
+
response = {
|
|
50
|
+
ok: true,
|
|
51
|
+
result: {
|
|
52
|
+
operation: input.request.operation,
|
|
53
|
+
bytes
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
} catch (error) {
|
|
57
|
+
response = {
|
|
58
|
+
ok: false,
|
|
59
|
+
error: boundedText(error instanceof Error ? error.message : String(error))
|
|
60
|
+
};
|
|
61
|
+
exitCode = 1;
|
|
62
|
+
} finally {
|
|
63
|
+
const cleanupErrors = [];
|
|
64
|
+
for (const command of commands) try {
|
|
65
|
+
command.free();
|
|
66
|
+
} catch (error) {
|
|
67
|
+
cleanupErrors.push(error);
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
pkg?.free();
|
|
71
|
+
} catch (error) {
|
|
72
|
+
cleanupErrors.push(error);
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
runtime?.free();
|
|
76
|
+
} catch (error) {
|
|
77
|
+
cleanupErrors.push(error);
|
|
78
|
+
}
|
|
79
|
+
if (cleanupErrors.length > 0) {
|
|
80
|
+
response = {
|
|
81
|
+
ok: false,
|
|
82
|
+
error: boundedText(`Unable to release isolated Wasmer resources: ${cleanupErrors.map(errorText).join("; ")}`)
|
|
83
|
+
};
|
|
84
|
+
exitCode = 1;
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
writeFileSync(requiredResponsePath(), serialize(response), {
|
|
88
|
+
flag: "wx",
|
|
89
|
+
mode: 384
|
|
90
|
+
});
|
|
91
|
+
} catch (error) {
|
|
92
|
+
process.stderr.write(`Unable to write the runner-stage response: ${errorText(error)}\n`);
|
|
93
|
+
exitCode = 1;
|
|
94
|
+
}
|
|
95
|
+
setTimeout(() => process.exit(exitCode), 10);
|
|
96
|
+
}
|
|
97
|
+
function parseInput(value) {
|
|
98
|
+
if (!isRecord(value) || typeof value.toolchainAsset !== "string" || !path.isAbsolute(value.toolchainAsset) || !isRecord(value.request)) throw new Error("The runner stage received an invalid request envelope.");
|
|
99
|
+
const request = value.request;
|
|
100
|
+
if (request.packageSpecifier !== PYTHON_PACKAGE || request.command !== "python" || request.operation !== "command-binary" && request.operation !== "runtime-files") throw new Error("The runner stage request does not name a pinned WASM-OJ runtime command.");
|
|
101
|
+
if (request.operation === "runtime-files" && (!Array.isArray(request.args) || request.args.some((argument) => typeof argument !== "string"))) throw new Error("The runner stage runtime-files arguments are invalid.");
|
|
102
|
+
return {
|
|
103
|
+
toolchainAsset: value.toolchainAsset,
|
|
104
|
+
verifiedToolchain: value.verifiedToolchain === true,
|
|
105
|
+
request: request.operation === "command-binary" ? {
|
|
106
|
+
operation: request.operation,
|
|
107
|
+
packageSpecifier: request.packageSpecifier,
|
|
108
|
+
command: request.command
|
|
109
|
+
} : {
|
|
110
|
+
operation: request.operation,
|
|
111
|
+
packageSpecifier: request.packageSpecifier,
|
|
112
|
+
command: request.command,
|
|
113
|
+
args: [...request.args]
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function verifyDigest(filename, bytes, expected) {
|
|
118
|
+
const actual = createHash("sha256").update(bytes).digest("hex");
|
|
119
|
+
if (actual !== expected) throw new Error(`Pinned package '${filename}' has digest ${actual}; expected ${expected}.`);
|
|
120
|
+
}
|
|
121
|
+
function uint8View(bytes) {
|
|
122
|
+
return new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
123
|
+
}
|
|
124
|
+
function uniqueCommands(commandMap) {
|
|
125
|
+
const unique = /* @__PURE__ */ new Set();
|
|
126
|
+
for (const command of Object.values(commandMap)) if (command && typeof command.free === "function") unique.add(command);
|
|
127
|
+
return [...unique];
|
|
128
|
+
}
|
|
129
|
+
function requiredResponsePath() {
|
|
130
|
+
const value = process.env.WASM_OJ_RUNNER_STAGE_RESPONSE;
|
|
131
|
+
if (!value) throw new Error("WASM_OJ_RUNNER_STAGE_RESPONSE is required.");
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
async function readStdin() {
|
|
135
|
+
const chunks = [];
|
|
136
|
+
let bytes = 0;
|
|
137
|
+
for await (const chunk of process.stdin) {
|
|
138
|
+
bytes += chunk.byteLength;
|
|
139
|
+
if (bytes > MAX_INPUT_BYTES) throw new Error(`Runner stage input exceeds ${MAX_INPUT_BYTES} bytes.`);
|
|
140
|
+
chunks.push(chunk);
|
|
141
|
+
}
|
|
142
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
143
|
+
}
|
|
144
|
+
function boundedText(value) {
|
|
145
|
+
const text = String(value);
|
|
146
|
+
return text.length <= MAX_ERROR_CHARACTERS ? text : `${text.slice(0, MAX_ERROR_CHARACTERS)}…`;
|
|
147
|
+
}
|
|
148
|
+
function errorText(error) {
|
|
149
|
+
return error instanceof Error ? error.message : String(error);
|
|
150
|
+
}
|
|
151
|
+
function isRecord(value) {
|
|
152
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Arjun Barrett
|
|
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.
|