@waffle-os/runtime 0.0.156 → 0.0.157
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/browser/filesystem-backend.ts +11 -1
- package/browser/preview-protocol.ts +2 -0
- package/browser/preview.ts +128 -18
- package/browser/runtime-terminals.ts +65 -24
- package/browser/shared-fs.ts +141 -14
- package/browser/worker.ts +73 -6
- package/core/descriptors.ts +19 -2
- package/core/devices.ts +237 -2
- package/core/fd-reopen.ts +105 -20
- package/core/filesystem.ts +71 -0
- package/core/index.ts +4 -0
- package/core/open-file.ts +212 -18
- package/core/wasi.ts +397 -46
- package/dist/browser/filesystem-backend.js +4 -2
- package/dist/browser/preview.js +120 -17
- package/dist/browser/runtime-terminals.js +53 -10
- package/dist/browser/shared-fs.js +124 -11
- package/dist/browser/worker.js +59 -6
- package/dist/core/descriptors.js +9 -1
- package/dist/core/devices.js +211 -1
- package/dist/core/fd-reopen.js +79 -18
- package/dist/core/filesystem.js +13 -0
- package/dist/core/open-file.js +166 -13
- package/dist/core/wasi.js +353 -27
- package/dist/fs/backend.js +29 -2
- package/dist/fs/client.js +199 -6
- package/dist/fs/index.js +21 -1
- package/dist/fs/indexeddb.js +30 -2
- package/dist/fs/locks.js +1112 -0
- package/dist/fs/memory.js +109 -1
- package/dist/fs/multitab-backend.js +315 -195
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +13 -6
- package/dist/fs/protocol.js +9 -3
- package/dist/fs/service.js +386 -22
- package/dist/fs/shared.js +54 -1
- package/dist/fs/storage-geometry.js +2 -0
- package/dist/interrupt/rewriter-internal.js +4 -1
- package/dist/proc/abi.js +80 -6
- package/dist/proc/errors.js +1 -0
- package/dist/proc/filesystem-codec.js +40 -9
- package/dist/proc/index.js +10 -1
- package/dist/proc/pty.js +193 -0
- package/dist/proc/worker-client.js +11 -2
- package/dist/proc/worker-pipes.js +51 -0
- package/dist/runtime.js +11 -2
- package/dist/search/engine.js +53 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +33 -1
- package/fs/client.ts +225 -6
- package/fs/index.ts +22 -1
- package/fs/indexeddb.ts +36 -2
- package/fs/locks.ts +1406 -0
- package/fs/memory.ts +114 -1
- package/fs/multitab-backend.ts +561 -303
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +14 -3
- package/fs/protocol.ts +17 -5
- package/fs/service.ts +468 -23
- package/fs/shared.ts +60 -2
- package/fs/storage-geometry.ts +3 -0
- package/index.ts +2 -0
- package/interrupt/rewriter-internal.ts +4 -1
- package/package.json +2 -2
- package/proc/abi.ts +79 -6
- package/proc/errors.ts +1 -0
- package/proc/filesystem-codec.ts +39 -9
- package/proc/index.ts +11 -0
- package/proc/pty.ts +227 -0
- package/proc/worker-client.ts +28 -6
- package/proc/worker-pipes.ts +57 -0
- package/runtime.ts +12 -2
- package/search/engine.ts +49 -0
- package/testing/indexeddb-storage-browser.ts +173 -2
- package/tooling/program/program-transformer.json +1 -1
package/dist/fs/shared.js
CHANGED
|
@@ -38,6 +38,7 @@ import {
|
|
|
38
38
|
PAGE_CELLS,
|
|
39
39
|
SLOT_WORDS,
|
|
40
40
|
HEADER_WORDS,
|
|
41
|
+
MACHINE_ID_OFFSET,
|
|
41
42
|
SLOT_OFFSET,
|
|
42
43
|
TABLE_OWNER_OFFSET,
|
|
43
44
|
PAGE_CELL_OFFSET,
|
|
@@ -45,6 +46,7 @@ import {
|
|
|
45
46
|
DATA_OFFSET,
|
|
46
47
|
SHARED_ROOT_BYTES
|
|
47
48
|
} from "./storage-geometry.js";
|
|
49
|
+
import { validateMachineId } from "../core/devices.js";
|
|
48
50
|
import { SHARED_SEGMENT_BYTES } from "./shared-capacity.js";
|
|
49
51
|
export { SLOT_WORDS } from "./storage-geometry.js";
|
|
50
52
|
const MAGIC = 1113015891;
|
|
@@ -61,6 +63,7 @@ const FREED_REVISION_LOW = 7;
|
|
|
61
63
|
const FREED_REVISION_HIGH = 8;
|
|
62
64
|
const REGISTERED_SLOTS = 11;
|
|
63
65
|
const CHANGE_SEQUENCE = 12;
|
|
66
|
+
export const POSIX_LOCK_COUNT = 13;
|
|
64
67
|
const UNLINK_GENERATION = 31;
|
|
65
68
|
const PHYSICAL_HINT = 27;
|
|
66
69
|
const TABLE_HINT = 28;
|
|
@@ -84,6 +87,47 @@ const REVISION = 10;
|
|
|
84
87
|
const REVISION_STATE = 11;
|
|
85
88
|
const RESIDENT = 1;
|
|
86
89
|
const copyBytes = Uint8Array.prototype.set;
|
|
90
|
+
const HEX_DIGITS = "0123456789abcdef";
|
|
91
|
+
function hexWord(word) {
|
|
92
|
+
let out = "";
|
|
93
|
+
for (let shift = 28;shift >= 0; shift -= 4)
|
|
94
|
+
out += HEX_DIGITS[word >>> shift & 15];
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
export function machineIdFromWorkspace(workspaceId) {
|
|
98
|
+
return validateMachineId(workspaceId.replaceAll("-", "").toLowerCase());
|
|
99
|
+
}
|
|
100
|
+
export function writeSharedMachineId(cells, machineId) {
|
|
101
|
+
const valid = validateMachineId(machineId);
|
|
102
|
+
const seqWord = MACHINE_ID_OFFSET + 4;
|
|
103
|
+
const current = Atomics.load(cells, seqWord) >>> 0;
|
|
104
|
+
const base = current & ~1;
|
|
105
|
+
Atomics.store(cells, seqWord, base + 1 | 0);
|
|
106
|
+
for (let i = 0;i < 4; i++) {
|
|
107
|
+
const word = Number.parseInt(valid.slice(i * 8, i * 8 + 8), 16) | 0;
|
|
108
|
+
Atomics.store(cells, MACHINE_ID_OFFSET + i, word);
|
|
109
|
+
}
|
|
110
|
+
const nextEven = base + 2 | 0;
|
|
111
|
+
Atomics.store(cells, seqWord, nextEven === 0 ? 2 : nextEven);
|
|
112
|
+
}
|
|
113
|
+
export function readSharedMachineId(cells) {
|
|
114
|
+
const seqWord = MACHINE_ID_OFFSET + 4;
|
|
115
|
+
for (let attempt = 0;attempt < 64; attempt++) {
|
|
116
|
+
const seq1 = atomicsLoad(cells, seqWord);
|
|
117
|
+
if (seq1 === 0)
|
|
118
|
+
return;
|
|
119
|
+
if ((seq1 & 1) !== 0)
|
|
120
|
+
continue;
|
|
121
|
+
const w0 = atomicsLoad(cells, MACHINE_ID_OFFSET);
|
|
122
|
+
const w1 = atomicsLoad(cells, MACHINE_ID_OFFSET + 1);
|
|
123
|
+
const w2 = atomicsLoad(cells, MACHINE_ID_OFFSET + 2);
|
|
124
|
+
const w3 = atomicsLoad(cells, MACHINE_ID_OFFSET + 3);
|
|
125
|
+
const seq2 = atomicsLoad(cells, seqWord);
|
|
126
|
+
if (seq1 === seq2)
|
|
127
|
+
return hexWord(w0) + hexWord(w1) + hexWord(w2) + hexWord(w3);
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
87
131
|
|
|
88
132
|
export class MissingSegmentError extends Error {
|
|
89
133
|
}
|
|
@@ -217,6 +261,12 @@ export class SharedFilesystemState {
|
|
|
217
261
|
setClientMode(mode) {
|
|
218
262
|
Atomics.store(this.cells, CLIENT_MODE, mode | 0);
|
|
219
263
|
}
|
|
264
|
+
setMachineId(machineId) {
|
|
265
|
+
writeSharedMachineId(this.cells, machineId);
|
|
266
|
+
}
|
|
267
|
+
getMachineId() {
|
|
268
|
+
return readSharedMachineId(this.cells);
|
|
269
|
+
}
|
|
220
270
|
isDirectDisabled() {
|
|
221
271
|
return (atomicsLoad(this.cells, CLIENT_MODE) & CLIENT_MODE_NO_DIRECT_IO) !== 0;
|
|
222
272
|
}
|
|
@@ -234,7 +284,7 @@ export class SharedFilesystemState {
|
|
|
234
284
|
Atomics.store(this.cells, PHYSICAL_HINT, 0);
|
|
235
285
|
Atomics.store(this.cells, TABLE_HINT, 0);
|
|
236
286
|
Atomics.store(this.cells, PINNED_PAGES, 0);
|
|
237
|
-
for (let i = 32;i <
|
|
287
|
+
for (let i = 32;i < MACHINE_ID_OFFSET; i++)
|
|
238
288
|
Atomics.store(this.cells, i, 0);
|
|
239
289
|
this.owners.fill(0);
|
|
240
290
|
this.slots.fill(0);
|
|
@@ -1943,6 +1993,9 @@ export class SharedFilesystemState {
|
|
|
1943
1993
|
unlinkGeneration() {
|
|
1944
1994
|
return Atomics.load(this.cells, UNLINK_GENERATION);
|
|
1945
1995
|
}
|
|
1996
|
+
posixLockCount() {
|
|
1997
|
+
return atomicsLoad(this.cells, POSIX_LOCK_COUNT);
|
|
1998
|
+
}
|
|
1946
1999
|
continueUnlinks(previous) {
|
|
1947
2000
|
const floor = previous.unlinkGeneration() + 1;
|
|
1948
2001
|
for (;; ) {
|
|
@@ -17,6 +17,8 @@ export const ROOT_CELL_BASE = TABLE_BLOCKS * TABLE_ENTRIES;
|
|
|
17
17
|
export const PAGE_CELLS = ROOT_CELL_BASE + MAX_FILES * FILE_ROOT_WORDS;
|
|
18
18
|
export const SLOT_WORDS = 28;
|
|
19
19
|
const DIRTY_HEADER_WORDS = 48;
|
|
20
|
+
const DIRTY_LEDGER_WORDS = 40;
|
|
21
|
+
export const MACHINE_ID_OFFSET = 32 + DIRTY_LEDGER_WORDS;
|
|
20
22
|
export const HEADER_WORDS = 32 + DIRTY_HEADER_WORDS;
|
|
21
23
|
export const WATCH_CAPACITY = 128;
|
|
22
24
|
export const WATCH_RECORD_WORDS = 288;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const INTERRUPT_REWRITER_REVISION =
|
|
1
|
+
export const INTERRUPT_REWRITER_REVISION = 9;
|
|
2
2
|
const encoder = new TextEncoder;
|
|
3
3
|
const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
4
4
|
const HEADER = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0]);
|
|
@@ -17,6 +17,9 @@ const runtimeProcSignatures = new Map([
|
|
|
17
17
|
["spawn_error", { params: [127, 127, 127], results: [127] }],
|
|
18
18
|
["job_of", { params: [127], results: [127] }],
|
|
19
19
|
["pipe", { params: [127], results: [127] }],
|
|
20
|
+
["pty_open", { params: [127, 127, 127], results: [127] }],
|
|
21
|
+
["pty_resize", { params: [127, 127, 127], results: [127] }],
|
|
22
|
+
["pty_size", { params: [127, 127], results: [127] }],
|
|
20
23
|
["dup", { params: [127], results: [127] }],
|
|
21
24
|
["fd_kind", { params: [127], results: [127] }],
|
|
22
25
|
["inherited_fd", { params: [127], results: [127] }],
|
package/dist/proc/abi.js
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
arrayAppend,
|
|
5
5
|
atomicsAdd,
|
|
6
6
|
atomicsNotify,
|
|
7
|
+
isInstance,
|
|
7
8
|
isSharedArrayBuffer,
|
|
8
9
|
DataViewConstructor,
|
|
9
10
|
Uint8ArrayConstructor,
|
|
@@ -34,6 +35,7 @@ import {
|
|
|
34
35
|
withFileOffset
|
|
35
36
|
} from "../core/open-file.js";
|
|
36
37
|
import { PipeController } from "./pipes.js";
|
|
38
|
+
import { PtyEndpoint, allocatePtyGeometry, validatePtyDimensions } from "./pty.js";
|
|
37
39
|
import { readOutcome } from "./outcome.js";
|
|
38
40
|
import { WasiExit } from "../core/wasi.js";
|
|
39
41
|
import { waitStatus } from "./wait-status.js";
|
|
@@ -69,7 +71,8 @@ export class ProcessSignalExit extends ExecutionUnwind {
|
|
|
69
71
|
this.status = 128 + signal;
|
|
70
72
|
}
|
|
71
73
|
}
|
|
72
|
-
const READ = 1n << 1n, WRITE = 1n << 6n, FILESTAT_GET = 1n << 21n, POLL = 1n << 27n;
|
|
74
|
+
const READ = 1n << 1n, FDSTAT_SET_FLAGS = 1n << 3n, WRITE = 1n << 6n, FILESTAT_GET = 1n << 21n, POLL = 1n << 27n;
|
|
75
|
+
const PTY_RIGHTS = READ | WRITE | FILESTAT_GET | POLL | FDSTAT_SET_FLAGS;
|
|
73
76
|
const FAULT = 21, ISDIR = 31, NOSYS = 52, NOTCAPABLE = 76;
|
|
74
77
|
const decode = new TextDecoder("utf-8", { fatal: true });
|
|
75
78
|
const pathDecode = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
@@ -136,6 +139,9 @@ export class ProcessABI {
|
|
|
136
139
|
exec: (ptr, length) => this.result(() => this.exec(ptr, length)),
|
|
137
140
|
job_of: (pid) => this.result(() => options.service.jobOf(pid === 0 ? options.pid : pid)),
|
|
138
141
|
pipe: (ptr) => this.result(() => this.pipe(ptr)),
|
|
142
|
+
pty_open: (cols, rows, ptr) => this.result(() => this.ptyOpen(cols, rows, ptr)),
|
|
143
|
+
pty_resize: (fd, cols, rows) => this.result(() => this.ptyResize(fd, cols, rows)),
|
|
144
|
+
pty_size: (fd, ptr) => this.result(() => this.ptySize(fd, ptr)),
|
|
139
145
|
dup: (fd) => options.descriptors.dup(fd >>> 0),
|
|
140
146
|
fd_kind: (fd) => options.descriptors.kind(fd >>> 0),
|
|
141
147
|
fd_reopen: (fd, oflags, fdflags, base, out) => {
|
|
@@ -341,7 +347,7 @@ export class ProcessABI {
|
|
|
341
347
|
}
|
|
342
348
|
pipe(ptr) {
|
|
343
349
|
this.span(ptr, 8);
|
|
344
|
-
const endpoints = this.options.createPipe?.() ?? this.
|
|
350
|
+
const endpoints = this.options.createPipe?.() ?? this.localPipeEndpoints();
|
|
345
351
|
let read, write;
|
|
346
352
|
try {
|
|
347
353
|
read = this.options.descriptors.open(endpoints[0], READ | FILESTAT_GET | POLL, 0n);
|
|
@@ -362,7 +368,76 @@ export class ProcessABI {
|
|
|
362
368
|
throw error;
|
|
363
369
|
}
|
|
364
370
|
}
|
|
365
|
-
|
|
371
|
+
ptyOpen(cols, rows, ptr) {
|
|
372
|
+
validatePtyDimensions(cols, rows);
|
|
373
|
+
this.span(ptr, 8);
|
|
374
|
+
const endpoints = this.options.createPty?.(cols, rows) ?? this.localPty(cols, rows);
|
|
375
|
+
let master, slave;
|
|
376
|
+
try {
|
|
377
|
+
master = this.options.descriptors.open(endpoints[0], PTY_RIGHTS, 0n);
|
|
378
|
+
slave = this.options.descriptors.open(endpoints[1], PTY_RIGHTS, 0n);
|
|
379
|
+
const output = this.view(ptr, 8);
|
|
380
|
+
dataViewSetUint32(output, 0, master, true);
|
|
381
|
+
dataViewSetUint32(output, 4, slave, true);
|
|
382
|
+
return 0;
|
|
383
|
+
} catch (error) {
|
|
384
|
+
if (master !== undefined)
|
|
385
|
+
this.options.descriptors.close(master);
|
|
386
|
+
else
|
|
387
|
+
endpoints[0].close?.();
|
|
388
|
+
if (slave !== undefined)
|
|
389
|
+
this.options.descriptors.close(slave);
|
|
390
|
+
else
|
|
391
|
+
endpoints[1].close?.();
|
|
392
|
+
throw error;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
ptyEndpoint(fd) {
|
|
396
|
+
const entry = this.options.descriptors.get(fd >>> 0);
|
|
397
|
+
if (!entry)
|
|
398
|
+
throw new ProcessError(ProcessErrno.BADF, `missing descriptor ${fd}`);
|
|
399
|
+
if (!isInstance(entry.resource, PtyEndpoint))
|
|
400
|
+
throw new ProcessError(ProcessErrno.NOTTY, `descriptor ${fd} is not a pty`);
|
|
401
|
+
return entry.resource;
|
|
402
|
+
}
|
|
403
|
+
ptyResize(fd, cols, rows) {
|
|
404
|
+
validatePtyDimensions(cols, rows);
|
|
405
|
+
this.ptyEndpoint(fd).resize(cols, rows);
|
|
406
|
+
return 0;
|
|
407
|
+
}
|
|
408
|
+
ptySize(fd, ptr) {
|
|
409
|
+
this.span(ptr, 12);
|
|
410
|
+
const size = this.ptyEndpoint(fd).size();
|
|
411
|
+
const output = this.view(ptr, 12);
|
|
412
|
+
dataViewSetUint32(output, 0, size.cols, true);
|
|
413
|
+
dataViewSetUint32(output, 4, size.rows, true);
|
|
414
|
+
dataViewSetUint32(output, 8, size.seq, true);
|
|
415
|
+
return 0;
|
|
416
|
+
}
|
|
417
|
+
localPty(cols, rows) {
|
|
418
|
+
const geometry = allocatePtyGeometry(cols, rows);
|
|
419
|
+
const input = this.localPipeEndpoints();
|
|
420
|
+
const slaveIn = input[0];
|
|
421
|
+
const masterOut = input[1];
|
|
422
|
+
let masterIn;
|
|
423
|
+
let slaveOut;
|
|
424
|
+
try {
|
|
425
|
+
const output = this.localPipeEndpoints();
|
|
426
|
+
masterIn = output[0];
|
|
427
|
+
slaveOut = output[1];
|
|
428
|
+
return [
|
|
429
|
+
new PtyEndpoint("master", masterIn, masterOut, geometry),
|
|
430
|
+
new PtyEndpoint("slave", slaveIn, slaveOut, geometry)
|
|
431
|
+
];
|
|
432
|
+
} catch (error) {
|
|
433
|
+
slaveIn.close();
|
|
434
|
+
masterOut.close();
|
|
435
|
+
masterIn?.close();
|
|
436
|
+
slaveOut?.close();
|
|
437
|
+
throw error;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
localPipeEndpoints() {
|
|
366
441
|
const controller = new PipeController({
|
|
367
442
|
capacity: this.options.pipeCapacity,
|
|
368
443
|
wake: this.options.wake
|
|
@@ -372,7 +447,7 @@ export class ProcessABI {
|
|
|
372
447
|
try {
|
|
373
448
|
write = controller.retain(this.options.pid, "write");
|
|
374
449
|
} catch (error) {
|
|
375
|
-
read.close
|
|
450
|
+
read.close();
|
|
376
451
|
throw error;
|
|
377
452
|
}
|
|
378
453
|
const unregister = this.options.service.registerRevocation?.(this.options.pid, () => controller.revoke(this.options.pid));
|
|
@@ -388,8 +463,7 @@ export class ProcessABI {
|
|
|
388
463
|
return;
|
|
389
464
|
closed = true;
|
|
390
465
|
try {
|
|
391
|
-
|
|
392
|
-
reflectApply(close, endpoint, []);
|
|
466
|
+
reflectApply(close, endpoint, []);
|
|
393
467
|
} finally {
|
|
394
468
|
if (--remaining === 0)
|
|
395
469
|
unregister();
|
package/dist/proc/errors.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { isInstance, isSharedArrayBuffer } from "./intrinsics.js";
|
|
2
2
|
import { OpenDescription } from "../core/descriptors.js";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
readSharedOfdBuffer,
|
|
5
|
+
retainSharedOfdTransfer,
|
|
6
|
+
shareDirectoryOfd,
|
|
7
|
+
shareFileResource
|
|
8
|
+
} from "../core/open-file.js";
|
|
4
9
|
import { FilesystemClient } from "../fs/client.js";
|
|
5
10
|
import {
|
|
6
11
|
releaseFilesystemReservation
|
|
@@ -8,18 +13,33 @@ import {
|
|
|
8
13
|
export function isFilesystemDescriptorToken(token) {
|
|
9
14
|
return typeof token === "object" && token !== null && "kind" in token && token.kind === "filesystem";
|
|
10
15
|
}
|
|
16
|
+
function tokenOfdBuffer(token) {
|
|
17
|
+
const candidate = token.resource === "file" ? token.offset : token.ofd;
|
|
18
|
+
return isSharedArrayBuffer(candidate) ? candidate : undefined;
|
|
19
|
+
}
|
|
20
|
+
function releaseTokenOfd(client, token) {
|
|
21
|
+
const buffer = tokenOfdBuffer(token);
|
|
22
|
+
if (!buffer)
|
|
23
|
+
return;
|
|
24
|
+
const ofd = readSharedOfdBuffer(buffer);
|
|
25
|
+
if (!ofd?.locked)
|
|
26
|
+
return;
|
|
27
|
+
client.locks.releaseOfd(token.reservation.handle, ofd.id, ofd.pendingTransfers > 0);
|
|
28
|
+
}
|
|
11
29
|
export function createFilesystemDescriptorCodec(filesystem, target, sweep) {
|
|
12
30
|
const client = isInstance(filesystem, FilesystemClient) ? filesystem : undefined;
|
|
13
31
|
const identity = client?.connection ?? filesystem;
|
|
14
32
|
const discard = (token) => {
|
|
15
33
|
if (!isFilesystemDescriptorToken(token))
|
|
16
34
|
throw new TypeError("not a filesystem descriptor token");
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
35
|
+
const won = releaseFilesystemReservation(token.reservation);
|
|
36
|
+
const localOwner = client && token.reservation.targetOwner === client.connection.owner;
|
|
37
|
+
if (won && localOwner)
|
|
38
|
+
releaseTokenOfd(client, token);
|
|
39
|
+
if (localOwner)
|
|
40
|
+
client.finishReleasedReservation(token.reservation, won);
|
|
41
|
+
else if (won)
|
|
21
42
|
(sweep ?? (() => client?.sweep()))();
|
|
22
|
-
}
|
|
23
43
|
};
|
|
24
44
|
return {
|
|
25
45
|
export(descriptor) {
|
|
@@ -29,11 +49,19 @@ export function createFilesystemDescriptorCodec(filesystem, target, sweep) {
|
|
|
29
49
|
if (!client || !target)
|
|
30
50
|
throw new TypeError("filesystem export requires a client and child identity");
|
|
31
51
|
const offset = resource.kind === "file" ? shareFileResource(resource, client.connection) : undefined;
|
|
52
|
+
const dirOfd = resource.kind === "directory" ? shareDirectoryOfd(resource, client.connection) : undefined;
|
|
53
|
+
const reservation = client.reserve(resource.handle, target);
|
|
54
|
+
const ofd = offset ?? dirOfd;
|
|
55
|
+
if (ofd) {
|
|
56
|
+
reservation.ofd = ofd;
|
|
57
|
+
retainSharedOfdTransfer(ofd);
|
|
58
|
+
}
|
|
32
59
|
return {
|
|
33
60
|
kind: "filesystem",
|
|
34
61
|
resource: resource.kind,
|
|
35
|
-
reservation
|
|
36
|
-
offset
|
|
62
|
+
reservation,
|
|
63
|
+
offset,
|
|
64
|
+
...dirOfd ? { ofd: dirOfd } : {}
|
|
37
65
|
};
|
|
38
66
|
},
|
|
39
67
|
import(token) {
|
|
@@ -50,8 +78,11 @@ export function createFilesystemDescriptorCodec(filesystem, target, sweep) {
|
|
|
50
78
|
throw new TypeError("file offset is missing");
|
|
51
79
|
resource = { kind: "file", handle, offset: 0, append: false };
|
|
52
80
|
shareFileResource(resource, identity, token.offset);
|
|
53
|
-
} else
|
|
81
|
+
} else {
|
|
54
82
|
resource = { kind: "directory", handle };
|
|
83
|
+
if (isSharedArrayBuffer(token.ofd))
|
|
84
|
+
shareDirectoryOfd(resource, identity, token.ofd);
|
|
85
|
+
}
|
|
55
86
|
client?.adopt(token.reservation);
|
|
56
87
|
return new OpenDescription(resource, () => discard(token));
|
|
57
88
|
},
|
package/dist/proc/index.js
CHANGED
|
@@ -54,6 +54,15 @@ export {
|
|
|
54
54
|
WorkerPipeHost,
|
|
55
55
|
WorkerPipeBroker,
|
|
56
56
|
createWorkerPipeCodec,
|
|
57
|
-
isWorkerPipeDescriptorToken
|
|
57
|
+
isWorkerPipeDescriptorToken,
|
|
58
|
+
isWorkerPtyDescriptorToken
|
|
58
59
|
} from "./worker-pipes.js";
|
|
60
|
+
export {
|
|
61
|
+
PtyEndpoint,
|
|
62
|
+
PTY_GEOMETRY_BYTES,
|
|
63
|
+
MAX_PTY_DIMENSION,
|
|
64
|
+
isValidPtyDimension,
|
|
65
|
+
allocatePtyGeometry,
|
|
66
|
+
attachPtyEndpoint
|
|
67
|
+
} from "./pty.js";
|
|
59
68
|
export { ProcessABI, ProcessSignalExit } from "./abi.js";
|
package/dist/proc/pty.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { allocateSharedControl } from "../memory/control.js";
|
|
2
|
+
import { ProcessErrno, ProcessError } from "./errors.js";
|
|
3
|
+
import {
|
|
4
|
+
BigInt64ArrayConstructor,
|
|
5
|
+
Int32ArrayConstructor,
|
|
6
|
+
atomicsAdd,
|
|
7
|
+
atomicsCompareExchange,
|
|
8
|
+
atomicsLoad,
|
|
9
|
+
atomicsNotify,
|
|
10
|
+
atomicsStore,
|
|
11
|
+
isSharedArrayBuffer,
|
|
12
|
+
typedArrayLength
|
|
13
|
+
} from "./intrinsics.js";
|
|
14
|
+
const bigintConstructor = BigInt;
|
|
15
|
+
const bigIntAsIntN = BigInt.asIntN;
|
|
16
|
+
const numberConstructor = Number;
|
|
17
|
+
import {
|
|
18
|
+
PipeEndpoint,
|
|
19
|
+
attachPipeEndpoint
|
|
20
|
+
} from "./pipes.js";
|
|
21
|
+
export const PTY_GEOMETRY_BYTES = 16;
|
|
22
|
+
export const MAX_PTY_DIMENSION = 65535;
|
|
23
|
+
export function isValidPtyDimension(value) {
|
|
24
|
+
return Number.isInteger(value) && value >= 1 && value <= MAX_PTY_DIMENSION;
|
|
25
|
+
}
|
|
26
|
+
export function validatePtyDimensions(cols, rows) {
|
|
27
|
+
if (!isValidPtyDimension(cols) || !isValidPtyDimension(rows))
|
|
28
|
+
throw new ProcessError(ProcessErrno.INVAL, "invalid pty dimensions");
|
|
29
|
+
}
|
|
30
|
+
function isValidPtyGeometryBuffer(buffer) {
|
|
31
|
+
if (!isSharedArrayBuffer(buffer))
|
|
32
|
+
return false;
|
|
33
|
+
return typedArrayLength(new Int32ArrayConstructor(buffer)) >= 4;
|
|
34
|
+
}
|
|
35
|
+
function packPtySize(cols, rows) {
|
|
36
|
+
return ((rows & 65535) << 16 | cols & 65535) >>> 0;
|
|
37
|
+
}
|
|
38
|
+
function packGeometryWord(cols, rows, seq) {
|
|
39
|
+
return bigIntAsIntN(64, bigintConstructor(seq >>> 0) << 32n | bigintConstructor(packPtySize(cols, rows)));
|
|
40
|
+
}
|
|
41
|
+
export function allocatePtyGeometry(cols, rows, allocateShared) {
|
|
42
|
+
validatePtyDimensions(cols, rows);
|
|
43
|
+
const buffer = allocateShared ? allocateShared(PTY_GEOMETRY_BYTES) : allocateSharedControl(PTY_GEOMETRY_BYTES);
|
|
44
|
+
const words = new BigInt64ArrayConstructor(buffer, 0, 2);
|
|
45
|
+
atomicsStore(words, 0, packGeometryWord(cols, rows, 0));
|
|
46
|
+
atomicsStore(words, 1, 0n);
|
|
47
|
+
return buffer;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class PtyEndpoint {
|
|
51
|
+
role;
|
|
52
|
+
readEnd;
|
|
53
|
+
writeEnd;
|
|
54
|
+
geometryBuffer;
|
|
55
|
+
kind;
|
|
56
|
+
notification;
|
|
57
|
+
geometry;
|
|
58
|
+
closed = false;
|
|
59
|
+
constructor(role, readEnd, writeEnd, geometryBuffer) {
|
|
60
|
+
this.role = role;
|
|
61
|
+
this.readEnd = readEnd;
|
|
62
|
+
this.writeEnd = writeEnd;
|
|
63
|
+
this.geometryBuffer = geometryBuffer;
|
|
64
|
+
if (!isValidPtyGeometryBuffer(geometryBuffer))
|
|
65
|
+
throw new TypeError("invalid pty geometry buffer");
|
|
66
|
+
this.kind = role === "slave" ? "terminal" : "pipe";
|
|
67
|
+
this.notification = readEnd.notification;
|
|
68
|
+
this.geometry = new BigInt64ArrayConstructor(geometryBuffer, 0, 2);
|
|
69
|
+
}
|
|
70
|
+
get pid() {
|
|
71
|
+
return this.readEnd.pid;
|
|
72
|
+
}
|
|
73
|
+
get live() {
|
|
74
|
+
return !this.closed && this.readEnd.live && this.writeEnd.live;
|
|
75
|
+
}
|
|
76
|
+
get fdflags() {
|
|
77
|
+
return this.readEnd.fdflags;
|
|
78
|
+
}
|
|
79
|
+
set fdflags(value) {
|
|
80
|
+
this.readEnd.fdflags = value;
|
|
81
|
+
this.writeEnd.fdflags = value;
|
|
82
|
+
}
|
|
83
|
+
read(chunks) {
|
|
84
|
+
return this.readEnd.read(chunks);
|
|
85
|
+
}
|
|
86
|
+
tryRead(chunks) {
|
|
87
|
+
return this.readEnd.tryRead(chunks);
|
|
88
|
+
}
|
|
89
|
+
write(chunks) {
|
|
90
|
+
return this.writeEnd.write(chunks);
|
|
91
|
+
}
|
|
92
|
+
tryWrite(chunks) {
|
|
93
|
+
return this.writeEnd.tryWrite(chunks);
|
|
94
|
+
}
|
|
95
|
+
readiness() {
|
|
96
|
+
const readState = this.readEnd.readiness();
|
|
97
|
+
const writeState = this.writeEnd.readiness();
|
|
98
|
+
return {
|
|
99
|
+
readable: readState.readable,
|
|
100
|
+
writable: writeState.writable,
|
|
101
|
+
hangup: readState.hangup || writeState.hangup,
|
|
102
|
+
nbytes: readState.nbytes
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
size() {
|
|
106
|
+
const word = atomicsLoad(this.geometry, 0);
|
|
107
|
+
const packed = numberConstructor(word & 0xffffffffn);
|
|
108
|
+
const seq = numberConstructor(word >> 32n & 0xffffffffn);
|
|
109
|
+
return {
|
|
110
|
+
cols: packed & 65535,
|
|
111
|
+
rows: packed >>> 16 & 65535,
|
|
112
|
+
seq
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
resize(cols, rows) {
|
|
116
|
+
validatePtyDimensions(cols, rows);
|
|
117
|
+
const dims = bigintConstructor(packPtySize(cols, rows));
|
|
118
|
+
for (;; ) {
|
|
119
|
+
const prev = atomicsLoad(this.geometry, 0);
|
|
120
|
+
const nextSeq = (prev >> 32n & 0xffffffffn) + 1n & 0xffffffffn;
|
|
121
|
+
const next = bigIntAsIntN(64, nextSeq << 32n | dims);
|
|
122
|
+
if (atomicsCompareExchange(this.geometry, 0, prev, next) === prev)
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
atomicsAdd(this.notification, 0, 1);
|
|
126
|
+
atomicsNotify(this.notification, 0);
|
|
127
|
+
}
|
|
128
|
+
handle() {
|
|
129
|
+
return {
|
|
130
|
+
role: this.role,
|
|
131
|
+
read: this.readEnd.handle(),
|
|
132
|
+
write: this.writeEnd.handle(),
|
|
133
|
+
geometry: this.geometryBuffer
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
forProcess(pid) {
|
|
137
|
+
if (!this.live)
|
|
138
|
+
throw new Error("pty endpoint is closed");
|
|
139
|
+
if (pid === this.pid)
|
|
140
|
+
return this;
|
|
141
|
+
const nextRead = this.readEnd.forProcess(pid);
|
|
142
|
+
try {
|
|
143
|
+
const nextWrite = this.writeEnd.forProcess(pid);
|
|
144
|
+
return new PtyEndpoint(this.role, nextRead, nextWrite, this.geometryBuffer);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
nextRead.close();
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
retainSuccessor() {
|
|
151
|
+
if (!this.live)
|
|
152
|
+
throw new Error("pty endpoint is closed");
|
|
153
|
+
const nextRead = this.readEnd.retainSuccessor();
|
|
154
|
+
try {
|
|
155
|
+
const nextWrite = this.writeEnd.retainSuccessor();
|
|
156
|
+
return new PtyEndpoint(this.role, nextRead, nextWrite, this.geometryBuffer);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
nextRead.close();
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
revoke() {
|
|
163
|
+
try {
|
|
164
|
+
this.readEnd.revoke();
|
|
165
|
+
} finally {
|
|
166
|
+
this.writeEnd.revoke();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
close() {
|
|
170
|
+
if (this.closed)
|
|
171
|
+
return;
|
|
172
|
+
this.closed = true;
|
|
173
|
+
try {
|
|
174
|
+
this.readEnd.close();
|
|
175
|
+
} finally {
|
|
176
|
+
this.writeEnd.close();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
export function attachPtyEndpoint(handle, options) {
|
|
181
|
+
if (handle.role !== "master" && handle.role !== "slave")
|
|
182
|
+
throw new TypeError("invalid pty endpoint role");
|
|
183
|
+
if (!isValidPtyGeometryBuffer(handle.geometry))
|
|
184
|
+
throw new TypeError("invalid pty geometry buffer");
|
|
185
|
+
const readEnd = attachPipeEndpoint(handle.read, options);
|
|
186
|
+
try {
|
|
187
|
+
const writeEnd = attachPipeEndpoint(handle.write, options);
|
|
188
|
+
return new PtyEndpoint(handle.role, readEnd, writeEnd, handle.geometry);
|
|
189
|
+
} catch (error) {
|
|
190
|
+
readEnd.close();
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -223,8 +223,12 @@ export class WorkerProcessClient {
|
|
|
223
223
|
};
|
|
224
224
|
const handoff = commandExecTransfer(staged);
|
|
225
225
|
let published = false;
|
|
226
|
+
const keptFds = [];
|
|
227
|
+
for (let index = 0;index < snapshot.fds.length; index++) {
|
|
228
|
+
arrayAppend(keptFds, snapshot.fds[index].parent);
|
|
229
|
+
}
|
|
226
230
|
try {
|
|
227
|
-
transfer = this.options.prepareExec(pid, (error) => recordCommandTransferFailure(this.options.transport, "exec", staged.generation, error));
|
|
231
|
+
transfer = this.options.prepareExec(pid, (error) => recordCommandTransferFailure(this.options.transport, "exec", staged.generation, error), keptFds);
|
|
228
232
|
codec = transfer.codec ?? codec;
|
|
229
233
|
staged.data = transfer.data;
|
|
230
234
|
this.exportBindings(staged, snapshot, sources, references, codec);
|
|
@@ -442,7 +446,7 @@ export class WorkerProcessClient {
|
|
|
442
446
|
let codec = this.options.codec;
|
|
443
447
|
let publishing = false;
|
|
444
448
|
try {
|
|
445
|
-
transfer = this.options.prepareSpawn?.(pid, (error) => recordCommandTransferFailure(this.options.transport, "spawn", pid, error));
|
|
449
|
+
transfer = this.options.prepareSpawn?.(pid, (error) => recordCommandTransferFailure(this.options.transport, "spawn", pid, error), processKind);
|
|
446
450
|
codec = transfer?.codec ?? codec;
|
|
447
451
|
child = reserveRecord(this.options.transport, pid, this.pid, job, existingJob?.ownerPid ?? this.pid, existingJob?.slot, selector === 0, jobLifetime, cwd, processKind);
|
|
448
452
|
child.data = transfer?.data;
|
|
@@ -818,6 +822,11 @@ export class WorkerProcessClient {
|
|
|
818
822
|
throw new ProcessError(58, "worker pipe factory is unavailable");
|
|
819
823
|
return this.options.createPipe();
|
|
820
824
|
}
|
|
825
|
+
createPty(cols, rows) {
|
|
826
|
+
if (!this.options.createPty)
|
|
827
|
+
throw new ProcessError(58, "worker pty factory is unavailable");
|
|
828
|
+
return this.options.createPty(cols, rows);
|
|
829
|
+
}
|
|
821
830
|
report(error) {
|
|
822
831
|
try {
|
|
823
832
|
this.options.onError?.(error);
|
|
@@ -8,15 +8,27 @@ import {
|
|
|
8
8
|
attachPipeEndpoint,
|
|
9
9
|
attachPipePublicationRegistry
|
|
10
10
|
} from "./pipes.js";
|
|
11
|
+
import {
|
|
12
|
+
PtyEndpoint,
|
|
13
|
+
allocatePtyGeometry,
|
|
14
|
+
attachPtyEndpoint
|
|
15
|
+
} from "./pty.js";
|
|
11
16
|
import {
|
|
12
17
|
workerProcessState
|
|
13
18
|
} from "./worker-client.js";
|
|
14
19
|
export function isWorkerPipeDescriptorToken(token) {
|
|
15
20
|
return typeof token === "object" && token !== null && "kind" in token && token.kind === "pipe";
|
|
16
21
|
}
|
|
22
|
+
export function isWorkerPtyDescriptorToken(token) {
|
|
23
|
+
return typeof token === "object" && token !== null && "kind" in token && token.kind === "pty";
|
|
24
|
+
}
|
|
17
25
|
export function createWorkerPipeCodec(options = {}) {
|
|
18
26
|
return {
|
|
19
27
|
export(descriptor, pid) {
|
|
28
|
+
if (isInstance(descriptor.resource, PtyEndpoint)) {
|
|
29
|
+
const endpoint = options.successor ? descriptor.resource.retainSuccessor() : descriptor.resource.forProcess(pid);
|
|
30
|
+
return { kind: "pty", ...endpoint.handle() };
|
|
31
|
+
}
|
|
20
32
|
if (!isInstance(descriptor.resource, PipeEndpoint)) {
|
|
21
33
|
if (options.fallback)
|
|
22
34
|
return options.fallback.export(descriptor, pid);
|
|
@@ -26,6 +38,14 @@ export function createWorkerPipeCodec(options = {}) {
|
|
|
26
38
|
return { kind: "pipe", endpoint: endpoint.handle() };
|
|
27
39
|
},
|
|
28
40
|
import(token, pid) {
|
|
41
|
+
if (isWorkerPtyDescriptorToken(token)) {
|
|
42
|
+
if (token.read.pid !== pid || token.write.pid !== pid)
|
|
43
|
+
throw new TypeError("pty descriptor belongs to another process");
|
|
44
|
+
options.onPipe?.(token.read);
|
|
45
|
+
options.onPipe?.(token.write);
|
|
46
|
+
const endpoint = attachPtyEndpoint(token, options);
|
|
47
|
+
return new OpenDescription(endpoint, () => endpoint.close());
|
|
48
|
+
}
|
|
29
49
|
if (!isWorkerPipeDescriptorToken(token)) {
|
|
30
50
|
if (options.fallback)
|
|
31
51
|
return options.fallback.import(token, pid);
|
|
@@ -38,6 +58,14 @@ export function createWorkerPipeCodec(options = {}) {
|
|
|
38
58
|
return new OpenDescription(endpoint, () => endpoint.close());
|
|
39
59
|
},
|
|
40
60
|
discard(token) {
|
|
61
|
+
if (isWorkerPtyDescriptorToken(token)) {
|
|
62
|
+
try {
|
|
63
|
+
attachPipeEndpoint(token.read, options).close();
|
|
64
|
+
} finally {
|
|
65
|
+
attachPipeEndpoint(token.write, options).close();
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
41
69
|
if (isWorkerPipeDescriptorToken(token))
|
|
42
70
|
attachPipeEndpoint(token.endpoint, options).close();
|
|
43
71
|
else if (options.fallback)
|
|
@@ -96,6 +124,29 @@ export class WorkerPipeHost {
|
|
|
96
124
|
throw error;
|
|
97
125
|
}
|
|
98
126
|
}
|
|
127
|
+
createPty(cols, rows) {
|
|
128
|
+
const geometry = allocatePtyGeometry(cols, rows, this.options.allocateShared);
|
|
129
|
+
const input = this.createPipe();
|
|
130
|
+
const slaveIn = input[0];
|
|
131
|
+
const masterOut = input[1];
|
|
132
|
+
let masterIn;
|
|
133
|
+
let slaveOut;
|
|
134
|
+
try {
|
|
135
|
+
const output = this.createPipe();
|
|
136
|
+
masterIn = output[0];
|
|
137
|
+
slaveOut = output[1];
|
|
138
|
+
return [
|
|
139
|
+
new PtyEndpoint("master", masterIn, masterOut, geometry),
|
|
140
|
+
new PtyEndpoint("slave", slaveIn, slaveOut, geometry)
|
|
141
|
+
];
|
|
142
|
+
} catch (error) {
|
|
143
|
+
slaveIn.close();
|
|
144
|
+
masterOut.close();
|
|
145
|
+
masterIn?.close();
|
|
146
|
+
slaveOut?.close();
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
99
150
|
revokeProcess(pid) {
|
|
100
151
|
this.registry.revoke(pid);
|
|
101
152
|
}
|