wasm-idle 1.1.2 → 1.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/README.md +134 -82
- package/dist/container/bash-profile.d.ts +80 -0
- package/dist/container/bash-profile.js +328 -0
- package/dist/{playground/container → container}/binding.d.ts +21 -8
- package/dist/{playground/container → container}/binding.js +109 -25
- package/dist/{playground/container → container}/clang-runtime.d.ts +11 -7
- package/dist/container/clang-runtime.js +561 -0
- package/dist/container/client.d.ts +14 -0
- package/dist/{playground/container → container}/client.js +2 -6
- package/dist/{playground/container → container}/explorer.d.ts +1 -1
- package/dist/container/index.d.ts +11 -0
- package/dist/container/index.js +11 -0
- package/dist/container/owner-worker.d.ts +19 -0
- package/dist/container/owner-worker.js +464 -0
- package/dist/{playground/container → container}/protocol.d.ts +12 -1
- package/dist/{playground/container → container}/runtime-adapter.d.ts +13 -11
- package/dist/{playground/container → container}/runtime-adapter.js +104 -41
- package/dist/{playground/container → container}/shell-control.d.ts +2 -2
- package/dist/{playground/container → container}/shell-control.js +93 -36
- package/dist/{playground/container → container}/workspace.d.ts +5 -1
- package/dist/index.d.ts +1 -1
- package/dist/playground/assetBridge.js +5 -2
- package/dist/playground/assets.d.ts +8 -0
- package/dist/playground/clang.d.ts +5 -1
- package/dist/playground/clang.js +61 -5
- package/dist/playground/compiledLldb.d.ts +41 -0
- package/dist/playground/compiledLldb.js +167 -0
- package/dist/playground/d.d.ts +4 -1
- package/dist/playground/d.js +78 -9
- package/dist/playground/debugDeadline.d.ts +12 -0
- package/dist/playground/debugDeadline.js +34 -0
- package/dist/playground/debugMailbox.d.ts +12 -0
- package/dist/playground/debugMailbox.js +77 -0
- package/dist/playground/go.js +9 -4
- package/dist/playground/goDebugInstrumentation.js +24 -44
- package/dist/playground/java-debug-FMHDKUN6.js +124 -0
- package/dist/playground/java-debug-notices.txt +492 -0
- package/dist/playground/java.d.ts +17 -1
- package/dist/playground/java.js +159 -3
- package/dist/playground/javaDebug.d.ts +62 -0
- package/dist/playground/javaDebug.js +305 -0
- package/dist/playground/javaDebugBridge.d.ts +6 -0
- package/dist/playground/javaDebugBridge.js +250 -0
- package/dist/playground/javaDebugSource.d.ts +9 -0
- package/dist/playground/javaDebugSource.js +4 -0
- package/dist/playground/javaRuntimeErrors.js +34 -1
- package/dist/playground/lean4Assets.d.ts +1 -25
- package/dist/playground/lean4Assets.js +2 -123
- package/dist/playground/lfortranAssets.d.ts +2 -2
- package/dist/playground/lldbSession.d.ts +23 -2
- package/dist/playground/lldbSession.js +278 -58
- package/dist/playground/lldbSourcePath.d.ts +2 -0
- package/dist/playground/lldbSourcePath.js +9 -0
- package/dist/playground/lua.d.ts +20 -1
- package/dist/playground/lua.js +190 -8
- package/dist/playground/luaDebug.d.ts +1 -0
- package/dist/playground/luaDebug.js +1 -0
- package/dist/playground/nim.d.ts +1 -0
- package/dist/playground/nim.js +8 -0
- package/dist/playground/objectivec.d.ts +7 -6
- package/dist/playground/objectivec.js +75 -9
- package/dist/playground/options.d.ts +10 -4
- package/dist/playground/python.d.ts +8 -1
- package/dist/playground/python.js +81 -6
- package/dist/playground/ruby.d.ts +10 -1
- package/dist/playground/ruby.js +66 -1
- package/dist/playground/rubyDebug.d.ts +21 -0
- package/dist/playground/rubyDebug.js +128 -0
- package/dist/playground/rust.d.ts +5 -1
- package/dist/playground/rust.js +128 -46
- package/dist/playground/sandbox.d.ts +8 -1
- package/dist/playground/staticWorkerRuntime.js +8 -2
- package/dist/playground/stdinBuffer.d.ts +1 -1
- package/dist/playground/stdinBuffer.js +19 -2
- package/dist/playground/tinygo.js +7 -0
- package/dist/playground/typescript.js +8 -0
- package/dist/playground/v.d.ts +3 -2
- package/dist/playground/v.js +66 -12
- package/dist/playground/wasmC3Version.d.ts +2 -2
- package/dist/playground/wasmC3Version.js +2 -2
- package/dist/playground/wasmLean4Version.d.ts +1 -110
- package/dist/playground/wasmLean4Version.js +2 -111
- package/dist/playground/wasmLfortranVersion.d.ts +2 -2
- package/dist/playground/wasmLfortranVersion.js +2 -2
- package/dist/playground/wasmLuaVersion.d.ts +1 -1
- package/dist/playground/wasmLuaVersion.js +1 -1
- package/dist/playground/wasmRustVersion.d.ts +26 -38
- package/dist/playground/wasmRustVersion.js +25 -40
- package/dist/playground/worker/clang.js +1 -1
- package/dist/playground/worker/cobol.js +1 -1
- package/dist/playground/worker/d.js +51 -2
- package/dist/playground/worker/fortran.js +1 -1
- package/dist/playground/worker/java.js +188 -35
- package/dist/playground/worker/javaDebugEvaluator.d.ts +16 -0
- package/dist/playground/worker/javaDebugEvaluator.js +44 -0
- package/dist/playground/worker/javaDebugRuntime.d.ts +24 -0
- package/dist/playground/worker/javaDebugRuntime.js +315 -0
- package/dist/playground/worker/javaOutput.d.ts +11 -0
- package/dist/playground/worker/javaOutput.js +54 -0
- package/dist/playground/worker/lua.js +13 -0
- package/dist/playground/worker/python.js +57 -22
- package/dist/playground/worker/ruby.js +13 -0
- package/dist/playground/worker/rubyDebugPreview.d.ts +2 -0
- package/dist/playground/worker/rubyDebugPreview.js +80 -0
- package/dist/playground/worker/rubyTracepoint.d.ts +16 -0
- package/dist/playground/worker/rubyTracepoint.js +162 -0
- package/dist/playground/worker/rust.js +4 -1
- package/dist/playground/worker/v.js +30 -5
- package/dist/playground/zig.js +4 -0
- package/package.json +55 -11
- package/dist/playground/container/ContainerExplorer.svelte +0 -307
- package/dist/playground/container/ContainerExplorer.svelte.d.ts +0 -14
- package/dist/playground/container/clang-runtime.js +0 -343
- package/dist/playground/container/client.d.ts +0 -15
- package/dist/playground/container/owner.worker.d.ts +0 -1
- package/dist/playground/container/owner.worker.js +0 -390
- package/dist/playground/container/release.d.ts +0 -31
- package/dist/playground/container/release.js +0 -612
- /package/dist/{playground/container → container}/explorer.js +0 -0
- /package/dist/{playground/container → container}/protocol.js +0 -0
- /package/dist/{playground/container → container}/workspace.js +0 -0
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { createContainer } from '@wasm-idle/container';
|
|
2
|
+
import { installWasixWorkerGroup } from '@wasm-idle/container/adapters/wasix';
|
|
3
|
+
import { createBashProfile } from './bash-profile.js';
|
|
4
|
+
import { createShellControl } from './shell-control.js';
|
|
5
|
+
import { ContainerRuntimeAdapter } from './runtime-adapter.js';
|
|
6
|
+
import { WebsiteClangRuntime } from './clang-runtime.js';
|
|
7
|
+
import { CONTAINER_WORKER_PROTOCOL, serializeContainerWorkerError } from './protocol.js';
|
|
8
|
+
const servingScopes = new WeakSet();
|
|
9
|
+
/** Install the owner protocol in the application's dedicated module Worker. */
|
|
10
|
+
export function serveContainerWorker(options) {
|
|
11
|
+
if (typeof globalThis.document !== 'undefined' || typeof globalThis.Worker !== 'function')
|
|
12
|
+
throw new Error('The container owner requires a dedicated Worker realm');
|
|
13
|
+
if (servingScopes.has(globalThis))
|
|
14
|
+
throw new Error('The container owner is already serving');
|
|
15
|
+
servingScopes.add(globalThis);
|
|
16
|
+
const scope = globalThis;
|
|
17
|
+
let channel = '', initialized = false, retired = false;
|
|
18
|
+
let container, engines, clang;
|
|
19
|
+
let profile;
|
|
20
|
+
let shellControl;
|
|
21
|
+
let shell;
|
|
22
|
+
let shellStarting, shellClosed = false;
|
|
23
|
+
let shellWriter;
|
|
24
|
+
let shellWrites = Promise.resolve();
|
|
25
|
+
let watcher;
|
|
26
|
+
let disposal;
|
|
27
|
+
const lifetime = new AbortController();
|
|
28
|
+
const requests = new Map();
|
|
29
|
+
const executionMode = new Map();
|
|
30
|
+
const NativeWorker = globalThis.Worker, children = new Set();
|
|
31
|
+
let childrenClosed = false;
|
|
32
|
+
class ApplicationWorker extends NativeWorker {
|
|
33
|
+
constructor(url, options) {
|
|
34
|
+
if (childrenClosed || children.size >= 64)
|
|
35
|
+
throw new Error('Application worker admission limit reached');
|
|
36
|
+
super(url, options);
|
|
37
|
+
children.add(this);
|
|
38
|
+
}
|
|
39
|
+
terminate() {
|
|
40
|
+
if (!children.delete(this))
|
|
41
|
+
return;
|
|
42
|
+
super.terminate();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Every engine Worker remains owned even after the one-shot SDK shell realm retires.
|
|
46
|
+
globalThis.Worker = ApplicationWorker;
|
|
47
|
+
const respond = (id, result) => scope.postMessage({ version: CONTAINER_WORKER_PROTOCOL, channel, id, ...result });
|
|
48
|
+
const emit = (event) => {
|
|
49
|
+
if (!retired)
|
|
50
|
+
respond(event.requestId ?? 0, { type: 'event', event });
|
|
51
|
+
};
|
|
52
|
+
function absoluteAssets(value, root, key = '') {
|
|
53
|
+
if (typeof value === 'string')
|
|
54
|
+
return key === '' || /(?:urls?|URLs?)$/i.test(key) ? new URL(value, root).href : value;
|
|
55
|
+
if (Array.isArray(value))
|
|
56
|
+
return value.map((item) => absoluteAssets(item, root, key));
|
|
57
|
+
if (value && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype)
|
|
58
|
+
return Object.fromEntries(Object.entries(value).map(([name, item]) => [
|
|
59
|
+
name,
|
|
60
|
+
absoluteAssets(item, root, name)
|
|
61
|
+
]));
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
async function initialize(settings) {
|
|
65
|
+
lifetime.signal.throwIfAborted();
|
|
66
|
+
if (typeof settings.workspaceId !== 'string' || settings.workspaceId.length > 4096)
|
|
67
|
+
throw new Error('Invalid workspace identifier');
|
|
68
|
+
const root = new URL(settings.applicationRootURL);
|
|
69
|
+
if (root.origin !== location.origin || !root.pathname.endsWith('/'))
|
|
70
|
+
throw new Error('Invalid application asset root');
|
|
71
|
+
engines = new ContainerRuntimeAdapter({
|
|
72
|
+
runtimeAssets: absoluteAssets(settings.runtimeAssets, root.href),
|
|
73
|
+
emit,
|
|
74
|
+
createBinding: options.createBinding
|
|
75
|
+
});
|
|
76
|
+
if (crossOriginIsolated && typeof SharedArrayBuffer === 'function') {
|
|
77
|
+
const { config, sdk } = await options.createBashConfiguration(root.href);
|
|
78
|
+
lifetime.signal.throwIfAborted();
|
|
79
|
+
const sdkScope = { Worker: ApplicationWorker };
|
|
80
|
+
const group = installWasixWorkerGroup({ scope: sdkScope, maxWorkers: 32 });
|
|
81
|
+
const sdkWorkerURL = new URL(config.sdkWorkerUrl).href;
|
|
82
|
+
// Only the fixed SDK thread entry belongs to the SDK's irrevocable lifetime.
|
|
83
|
+
// Its late allocations keep hitting the closed constructor after shell exit.
|
|
84
|
+
const RoutedWorker = function (url, options) {
|
|
85
|
+
const Constructor = new URL(String(url), location.href).href === sdkWorkerURL
|
|
86
|
+
? sdkScope.Worker
|
|
87
|
+
: ApplicationWorker;
|
|
88
|
+
return new Constructor(url, options);
|
|
89
|
+
};
|
|
90
|
+
Object.setPrototypeOf(RoutedWorker, NativeWorker);
|
|
91
|
+
Object.defineProperty(RoutedWorker, 'prototype', { value: NativeWorker.prototype });
|
|
92
|
+
globalThis.Worker = RoutedWorker;
|
|
93
|
+
shellControl = createShellControl({ languages: options.languages });
|
|
94
|
+
try {
|
|
95
|
+
const created = await createBashProfile({
|
|
96
|
+
config,
|
|
97
|
+
sdk,
|
|
98
|
+
workerGroup: group,
|
|
99
|
+
shellCommands: options.shellCommands,
|
|
100
|
+
adapters: [engines.adapter, shellControl.adapter],
|
|
101
|
+
commands: { ...engines.commands, ...shellControl.commands },
|
|
102
|
+
shellRoutes: {
|
|
103
|
+
[shellControl.route.path]: { command: shellControl.route.command }
|
|
104
|
+
},
|
|
105
|
+
onInvocation: (invocation) => {
|
|
106
|
+
shellControl?.onInvocation(invocation);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
// Configuration and manifest validation can finish after retirement.
|
|
110
|
+
// A late profile must never become the live resource owner.
|
|
111
|
+
if (retired) {
|
|
112
|
+
await Promise.allSettled([created.dispose(), group.dispose()]);
|
|
113
|
+
lifetime.signal.throwIfAborted();
|
|
114
|
+
}
|
|
115
|
+
profile = created;
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
await group.dispose().catch(() => { });
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
container = profile.container;
|
|
122
|
+
clang = new WebsiteClangRuntime(profile, engines, emit);
|
|
123
|
+
}
|
|
124
|
+
else
|
|
125
|
+
container = createContainer({
|
|
126
|
+
adapters: [engines.adapter],
|
|
127
|
+
commands: engines.commands,
|
|
128
|
+
maxBytes: 256 * 1024 * 1024,
|
|
129
|
+
maxInodes: 16384,
|
|
130
|
+
maxProcesses: 8,
|
|
131
|
+
maxMemoryBytes: 2 ** 32
|
|
132
|
+
});
|
|
133
|
+
engines.attach(container);
|
|
134
|
+
watcher = container.fs.watch({ maxQueuedEvents: 32, signal: lifetime.signal }).getReader();
|
|
135
|
+
void (async () => {
|
|
136
|
+
try {
|
|
137
|
+
for (;;) {
|
|
138
|
+
const { value, done } = await watcher.read();
|
|
139
|
+
if (done)
|
|
140
|
+
break;
|
|
141
|
+
emit({ name: 'workspace', data: value });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
if (!lifetime.signal.aborted)
|
|
146
|
+
emit({ name: 'shell-output', data: { error: String(error) } });
|
|
147
|
+
}
|
|
148
|
+
})();
|
|
149
|
+
}
|
|
150
|
+
async function startShell(options = {}) {
|
|
151
|
+
if (!profile)
|
|
152
|
+
throw new Error('Interactive shell requires cross-origin isolation. Batch Run is still available.');
|
|
153
|
+
if (shellClosed)
|
|
154
|
+
throw new Error('Bash has exited. Reload this workspace to open a fresh shell.');
|
|
155
|
+
if (shellStarting)
|
|
156
|
+
return shellStarting;
|
|
157
|
+
shellStarting = (async () => {
|
|
158
|
+
const cwd = options.cwd ?? '/workspace';
|
|
159
|
+
if (!/^\/workspace(?:\/[A-Z][A-Z0-9_]*)?$/.test(cwd))
|
|
160
|
+
throw new Error('Invalid shell directory');
|
|
161
|
+
container.fs.mkdir(cwd, { recursive: true });
|
|
162
|
+
const control = shellControl.setup(container.fs);
|
|
163
|
+
const initialDirectory = cwd === '/workspace'
|
|
164
|
+
? Promise.resolve()
|
|
165
|
+
: shellControl.requestDirectory(cwd.slice('/workspace/'.length));
|
|
166
|
+
// Keep a rejection observed while the actual SDK startup is still pending.
|
|
167
|
+
void initialDirectory.catch(() => { });
|
|
168
|
+
try {
|
|
169
|
+
shell = await profile.startShell({
|
|
170
|
+
cwd,
|
|
171
|
+
args: [...control.args],
|
|
172
|
+
mounts: ['/workspace', '/tmp', '/home/user', control.mountPath],
|
|
173
|
+
env: { ...options.env, ...control.env }
|
|
174
|
+
}, lifetime.signal);
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
shellControl?.dispose();
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
shellWriter = shell.stdin.getWriter();
|
|
181
|
+
const drain = async (stream, name) => {
|
|
182
|
+
const reader = stream.getReader();
|
|
183
|
+
try {
|
|
184
|
+
for (;;) {
|
|
185
|
+
const { value, done } = await reader.read();
|
|
186
|
+
if (done)
|
|
187
|
+
break;
|
|
188
|
+
emit({ name: 'shell-output', data: { stream: name, bytes: value } });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
finally {
|
|
192
|
+
reader.releaseLock();
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
void Promise.all([
|
|
196
|
+
shell.exited,
|
|
197
|
+
drain(shell.stdout, 'stdout'),
|
|
198
|
+
drain(shell.stderr, 'stderr')
|
|
199
|
+
])
|
|
200
|
+
.then(([result]) => {
|
|
201
|
+
shellClosed = true;
|
|
202
|
+
shellControl?.dispose();
|
|
203
|
+
emit({
|
|
204
|
+
name: 'shell-output',
|
|
205
|
+
data: {
|
|
206
|
+
stream: 'stderr',
|
|
207
|
+
bytes: new TextEncoder().encode(`\nShell ${result.reason}; reload to open a new shell.\n`)
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
})
|
|
211
|
+
.catch((error) => {
|
|
212
|
+
shellClosed = true;
|
|
213
|
+
shellControl?.dispose();
|
|
214
|
+
emit({
|
|
215
|
+
name: 'shell-output',
|
|
216
|
+
data: { error: error instanceof Error ? error.message : String(error) }
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
let readyTimeout;
|
|
220
|
+
try {
|
|
221
|
+
await Promise.race([
|
|
222
|
+
initialDirectory,
|
|
223
|
+
new Promise((_, reject) => {
|
|
224
|
+
readyTimeout = setTimeout(() => reject(new Error('Bash workspace control did not become ready within 10 seconds')), 10_000);
|
|
225
|
+
})
|
|
226
|
+
]);
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
shellControl?.dispose();
|
|
230
|
+
await shell?.dispose();
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
finally {
|
|
234
|
+
clearTimeout(readyTimeout);
|
|
235
|
+
}
|
|
236
|
+
})();
|
|
237
|
+
return shellStarting;
|
|
238
|
+
}
|
|
239
|
+
async function shellWrite(text) {
|
|
240
|
+
if (!shellWriter || shellClosed)
|
|
241
|
+
throw new Error('Shell is not running');
|
|
242
|
+
const bytes = typeof text === 'string'
|
|
243
|
+
? new TextEncoder().encode(text.replaceAll('\r\n', '\n').replaceAll('\r', '\n'))
|
|
244
|
+
: text;
|
|
245
|
+
if (bytes.length > 65536)
|
|
246
|
+
throw new Error('Terminal input exceeds 64 KiB');
|
|
247
|
+
const next = shellWrites.catch(() => { }).then(() => shellWriter.write(bytes));
|
|
248
|
+
shellWrites = next;
|
|
249
|
+
await next;
|
|
250
|
+
}
|
|
251
|
+
function dispose() {
|
|
252
|
+
if (disposal)
|
|
253
|
+
return disposal;
|
|
254
|
+
retired = true;
|
|
255
|
+
scope.onmessage = null;
|
|
256
|
+
disposal = Promise.resolve().then(async () => {
|
|
257
|
+
shellControl?.dispose();
|
|
258
|
+
await Promise.allSettled([
|
|
259
|
+
shell?.dispose(),
|
|
260
|
+
clang?.dispose(),
|
|
261
|
+
engines?.dispose(),
|
|
262
|
+
profile?.dispose(),
|
|
263
|
+
watcher?.cancel()
|
|
264
|
+
]);
|
|
265
|
+
try {
|
|
266
|
+
await container?.dispose();
|
|
267
|
+
}
|
|
268
|
+
finally {
|
|
269
|
+
childrenClosed = true;
|
|
270
|
+
// Termination also removes children from the registry; close the captured group.
|
|
271
|
+
for (const child of [...children])
|
|
272
|
+
child.terminate();
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
lifetime.abort();
|
|
276
|
+
for (const request of requests.values())
|
|
277
|
+
request.abort();
|
|
278
|
+
return disposal;
|
|
279
|
+
}
|
|
280
|
+
const debugMethods = new Set([
|
|
281
|
+
'debugCommand',
|
|
282
|
+
'debugPause',
|
|
283
|
+
'setBreakpoints',
|
|
284
|
+
'setSourceBreakpoints',
|
|
285
|
+
'debugEvaluate',
|
|
286
|
+
'debugEvaluateValue',
|
|
287
|
+
'debugSetSteppingOptions',
|
|
288
|
+
'debugVariables',
|
|
289
|
+
'debugScopes',
|
|
290
|
+
'debugFrameScopes',
|
|
291
|
+
'debugFrameName',
|
|
292
|
+
'debugReadMemory',
|
|
293
|
+
'debugWriteMemory',
|
|
294
|
+
'debugDataBreakpointInfo',
|
|
295
|
+
'debugSetDataBreakpoints'
|
|
296
|
+
]);
|
|
297
|
+
async function call(method, args, signal, id) {
|
|
298
|
+
if (retired && method !== 'dispose')
|
|
299
|
+
throw new Error('Container owner has been disposed');
|
|
300
|
+
const language = args[0];
|
|
301
|
+
const progress = {
|
|
302
|
+
set: (value, stage) => respond(id, { type: 'progress', progress: { kind: 'set', value, stage } }),
|
|
303
|
+
report: (event) => respond(id, { type: 'progress', progress: { kind: 'report', event } })
|
|
304
|
+
};
|
|
305
|
+
if (['load', 'prepare', 'run'].includes(method)) {
|
|
306
|
+
const [, code = '', log = true, argv = [], rawOptions = {}] = args;
|
|
307
|
+
const options = rawOptions ?? {};
|
|
308
|
+
const useClang = !!clang &&
|
|
309
|
+
['C', 'CPP'].includes(language) &&
|
|
310
|
+
!options.debug &&
|
|
311
|
+
(!options.debugMode || options.debugMode === 'none');
|
|
312
|
+
executionMode.set(language, useClang ? 'clang' : 'engine');
|
|
313
|
+
if (useClang) {
|
|
314
|
+
if (method === 'load') {
|
|
315
|
+
engines.inputs(language, code, options);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
return await clang[method](language, code, argv, options, signal, progress, id);
|
|
319
|
+
}
|
|
320
|
+
return await engines.execute(method, language, code, log, argv, options, signal, progress, id);
|
|
321
|
+
}
|
|
322
|
+
if (debugMethods.has(method))
|
|
323
|
+
return await engines.debug(language, method, args.slice(1));
|
|
324
|
+
if (method === 'write' || method === 'eof') {
|
|
325
|
+
const target = executionMode.get(language) === 'clang' && clang ? clang : engines;
|
|
326
|
+
return method === 'write'
|
|
327
|
+
? await target.write(language, args[1])
|
|
328
|
+
: await target.eof(language);
|
|
329
|
+
}
|
|
330
|
+
if (method === 'stop') {
|
|
331
|
+
await Promise.all([clang?.stop(language), engines.stop(language)]);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (method === 'clear') {
|
|
335
|
+
clang?.clear(language);
|
|
336
|
+
if (executionMode.get(language) === 'engine')
|
|
337
|
+
await engines.clear(language);
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (method === 'restart') {
|
|
341
|
+
await clang?.stop(language);
|
|
342
|
+
clang?.clear(language);
|
|
343
|
+
await engines.restart(language);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
if (method === 'syncWorkspace')
|
|
347
|
+
return engines.syncWorkspace(language, args[1], args[2]);
|
|
348
|
+
if (method === 'syncWorkspaceDelta') {
|
|
349
|
+
if (!Array.isArray(args[2]) || !Array.isArray(args[3]))
|
|
350
|
+
throw new TypeError('Workspace delta requires a baseline and project metadata');
|
|
351
|
+
return engines.syncWorkspace(language, args[1], args[2], args[3]);
|
|
352
|
+
}
|
|
353
|
+
if (method === 'readWorkspace')
|
|
354
|
+
return engines.readWorkspace(language);
|
|
355
|
+
if (method === 'readWorkspaceDelta')
|
|
356
|
+
return engines.readWorkspaceDelta(language, args[1]);
|
|
357
|
+
if (method === 'listWorkspaceFiles')
|
|
358
|
+
return engines.listWorkspaceFiles();
|
|
359
|
+
if (method === 'inspect')
|
|
360
|
+
return {
|
|
361
|
+
...(await container.inspect()),
|
|
362
|
+
runtimeJobs: [...engines.inspect(), ...(clang?.inspect() ?? [])],
|
|
363
|
+
shell: {
|
|
364
|
+
running: !!shell && !shellClosed,
|
|
365
|
+
closed: shellClosed,
|
|
366
|
+
control: shellControl?.inspect()
|
|
367
|
+
},
|
|
368
|
+
ownedWorkers: children.size
|
|
369
|
+
};
|
|
370
|
+
if (method === 'shellStart')
|
|
371
|
+
return await startShell(args[0]);
|
|
372
|
+
if (method === 'shellSetDirectory') {
|
|
373
|
+
await shellStarting;
|
|
374
|
+
if (!shell || shellClosed || !shellControl)
|
|
375
|
+
throw new Error('Shell is not running');
|
|
376
|
+
return await shellControl.requestDirectory(language);
|
|
377
|
+
}
|
|
378
|
+
if (method === 'shellSuspend' || method === 'shellResume') {
|
|
379
|
+
await shellStarting;
|
|
380
|
+
if (!shell || shellClosed || !shellControl)
|
|
381
|
+
return;
|
|
382
|
+
// The native hook runs only after Readline's input queue is empty. Waiting
|
|
383
|
+
// for a blocked foreground stdin writer here would defeat the deadline.
|
|
384
|
+
try {
|
|
385
|
+
return await shellControl.requestPrompt(method === 'shellSuspend' ? 'suspend' : 'resume', AbortSignal.any([signal, lifetime.signal, AbortSignal.timeout(2_000)]));
|
|
386
|
+
}
|
|
387
|
+
catch (error) {
|
|
388
|
+
if (error?.name === 'TimeoutError')
|
|
389
|
+
throw new Error('Finish the current shell command before running a program.');
|
|
390
|
+
throw error;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
if (method === 'shellWrite')
|
|
394
|
+
return await shellWrite(args[0]);
|
|
395
|
+
if (method === 'shellEof') {
|
|
396
|
+
await shellWrites;
|
|
397
|
+
await shellWriter?.close();
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
if (method === 'shellInterrupt') {
|
|
401
|
+
const interrupted = shell?.interrupt() ?? 0;
|
|
402
|
+
if (!interrupted)
|
|
403
|
+
await shellWrite('\x03');
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
if (method === 'shellDispose') {
|
|
407
|
+
await shell?.dispose();
|
|
408
|
+
shellControl?.dispose();
|
|
409
|
+
shellClosed = true;
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
if (method === 'dispose')
|
|
413
|
+
return await dispose();
|
|
414
|
+
throw new Error('Unknown container operation');
|
|
415
|
+
}
|
|
416
|
+
scope.onmessage = (event) => {
|
|
417
|
+
if (retired)
|
|
418
|
+
return;
|
|
419
|
+
const request = event.data;
|
|
420
|
+
if (!request ||
|
|
421
|
+
request.version !== CONTAINER_WORKER_PROTOCOL ||
|
|
422
|
+
typeof request.channel !== 'string' ||
|
|
423
|
+
!Number.isSafeInteger(request.id))
|
|
424
|
+
return;
|
|
425
|
+
if (request.type === 'init') {
|
|
426
|
+
if (initialized)
|
|
427
|
+
return;
|
|
428
|
+
initialized = true;
|
|
429
|
+
channel = request.channel;
|
|
430
|
+
void initialize(request.settings).then(() => {
|
|
431
|
+
if (!retired)
|
|
432
|
+
respond(0, { type: 'result', value: undefined });
|
|
433
|
+
}, (error) => {
|
|
434
|
+
if (!retired)
|
|
435
|
+
respond(0, {
|
|
436
|
+
type: 'error',
|
|
437
|
+
error: serializeContainerWorkerError(error)
|
|
438
|
+
});
|
|
439
|
+
void dispose();
|
|
440
|
+
});
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
if (!initialized || request.channel !== channel)
|
|
444
|
+
return;
|
|
445
|
+
if (request.type === 'cancel') {
|
|
446
|
+
requests.get(request.id)?.abort(new DOMException('Operation cancelled', 'AbortError'));
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (request.type !== 'call' ||
|
|
450
|
+
typeof request.method !== 'string' ||
|
|
451
|
+
!Array.isArray(request.args) ||
|
|
452
|
+
requests.has(request.id))
|
|
453
|
+
return;
|
|
454
|
+
const controller = new AbortController();
|
|
455
|
+
requests.set(request.id, controller);
|
|
456
|
+
void call(request.method, request.args, controller.signal, request.id)
|
|
457
|
+
.then((value) => respond(request.id, { type: 'result', value }), (error) => respond(request.id, {
|
|
458
|
+
type: 'error',
|
|
459
|
+
error: serializeContainerWorkerError(error)
|
|
460
|
+
}))
|
|
461
|
+
.finally(() => requests.delete(request.id));
|
|
462
|
+
};
|
|
463
|
+
return { dispose };
|
|
464
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ProgressLike, RuntimeProgressEvent, WorkspaceFile } from '@wasm-idle/core';
|
|
2
|
-
import type { SandboxRuntimeAssets } from '../sandbox';
|
|
2
|
+
import type { SandboxRuntimeAssets } from '../playground/sandbox.js';
|
|
3
3
|
export declare const CONTAINER_WORKER_PROTOCOL = 1;
|
|
4
4
|
export declare const OWNER_CLEANUP_DEADLINE_MS = 2000;
|
|
5
5
|
export interface ContainerWorkerInitialization {
|
|
@@ -8,6 +8,17 @@ export interface ContainerWorkerInitialization {
|
|
|
8
8
|
workspaceId: string;
|
|
9
9
|
}
|
|
10
10
|
export type ContainerWorkspaceFile = WorkspaceFile;
|
|
11
|
+
export interface ContainerWorkspaceFileMetadata {
|
|
12
|
+
path: string;
|
|
13
|
+
bytes: number;
|
|
14
|
+
}
|
|
15
|
+
export interface ContainerWorkspaceDelta {
|
|
16
|
+
/** A cursor belongs to one owner's most recent read of this language project. */
|
|
17
|
+
revision: number;
|
|
18
|
+
reset: boolean;
|
|
19
|
+
files: ContainerWorkspaceFile[];
|
|
20
|
+
deleted: string[];
|
|
21
|
+
}
|
|
11
22
|
export interface ContainerWorkspaceEntry {
|
|
12
23
|
/** Path relative to /workspace, independent of the selected language. */
|
|
13
24
|
path: string;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { type ProgressLike, type WorkspaceFile } from '@wasm-idle/core';
|
|
2
2
|
import { type Container, type ContainerProcess, type LiveCommandAdapter, type MemoryFileSystem } from '@wasm-idle/container';
|
|
3
|
-
import {
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
import type { ContainerWorkerEvent, ContainerWorkspaceEntry } from './protocol';
|
|
3
|
+
import type { PlaygroundBinding, SandboxRuntimeAssets } from '../playground/sandbox.js';
|
|
4
|
+
import type { SandboxExecutionOptions } from '../playground/options.js';
|
|
5
|
+
import type { ContainerWorkerEvent, ContainerWorkspaceDelta, ContainerWorkspaceEntry, ContainerWorkspaceFileMetadata } from './protocol.js';
|
|
7
6
|
interface Job {
|
|
8
7
|
id: string;
|
|
9
8
|
language: string;
|
|
@@ -22,14 +21,15 @@ interface Job {
|
|
|
22
21
|
output?: (data: string) => void;
|
|
23
22
|
readyForInput?: boolean;
|
|
24
23
|
}
|
|
25
|
-
|
|
24
|
+
export type ContainerRuntimeBindingFactory = (runtimeAssets: SandboxRuntimeAssets) => PlaygroundBinding;
|
|
25
|
+
export interface ContainerRuntimeAdapterOptions {
|
|
26
26
|
runtimeAssets: SandboxRuntimeAssets;
|
|
27
27
|
emit(event: ContainerWorkerEvent): void;
|
|
28
|
-
/**
|
|
29
|
-
createBinding
|
|
28
|
+
/** The application chooses its real language registry; the host imports no engines. */
|
|
29
|
+
createBinding: ContainerRuntimeBindingFactory;
|
|
30
30
|
}
|
|
31
31
|
/** Existing real engines keep their private runtime FS; their source snapshot is the common VFS. */
|
|
32
|
-
export declare class
|
|
32
|
+
export declare class ContainerRuntimeAdapter {
|
|
33
33
|
#private;
|
|
34
34
|
private readonly settings;
|
|
35
35
|
readonly adapter: LiveCommandAdapter;
|
|
@@ -41,12 +41,14 @@ export declare class WebsiteRuntimeAdapter {
|
|
|
41
41
|
};
|
|
42
42
|
};
|
|
43
43
|
readonly filesystemProfile = "shared-source-snapshot; runtime-private-files-not-exported";
|
|
44
|
-
constructor(settings:
|
|
44
|
+
constructor(settings: ContainerRuntimeAdapterOptions);
|
|
45
45
|
attach(container: Container): void;
|
|
46
46
|
get fs(): MemoryFileSystem;
|
|
47
|
+
private directoryPath;
|
|
47
48
|
directory(language: string): string;
|
|
48
49
|
private session;
|
|
49
|
-
syncWorkspace(language: string, files: readonly WorkspaceFile[], expectedBaseFiles?: readonly WorkspaceFile[]): void;
|
|
50
|
+
syncWorkspace(language: string, files: readonly WorkspaceFile[], expectedBaseFiles?: readonly WorkspaceFile[], project?: readonly ContainerWorkspaceFileMetadata[]): void;
|
|
51
|
+
readWorkspaceDelta(language: string, since?: number): ContainerWorkspaceDelta;
|
|
50
52
|
readWorkspace(language: string): WorkspaceFile[];
|
|
51
53
|
/** Enumerate the common namespace without copying file bodies or following links. */
|
|
52
54
|
listWorkspaceFiles(): ContainerWorkspaceEntry[];
|
|
@@ -70,4 +72,4 @@ export declare class WebsiteRuntimeAdapter {
|
|
|
70
72
|
}[];
|
|
71
73
|
dispose(): Promise<void>;
|
|
72
74
|
}
|
|
73
|
-
export {};
|
|
75
|
+
export { ContainerRuntimeAdapter as WebsiteRuntimeAdapter };
|