@secure-exec/core 0.2.1 → 0.3.0-rc.2

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 (248) hide show
  1. package/README.md +5 -5
  2. package/dist/binary.d.ts +4 -0
  3. package/dist/binary.js +25 -0
  4. package/dist/bytes.d.ts +2 -0
  5. package/dist/bytes.js +6 -0
  6. package/dist/callbacks.d.ts +41 -0
  7. package/dist/callbacks.js +94 -0
  8. package/dist/cargo.d.ts +2 -0
  9. package/dist/cargo.js +142 -0
  10. package/dist/correlation.d.ts +10 -0
  11. package/dist/correlation.js +49 -0
  12. package/dist/descriptors.d.ts +34 -0
  13. package/dist/descriptors.js +37 -0
  14. package/dist/event-buffer.d.ts +90 -0
  15. package/dist/event-buffer.js +313 -0
  16. package/dist/ext.d.ts +7 -0
  17. package/dist/ext.js +13 -0
  18. package/dist/filesystem.d.ts +41 -0
  19. package/dist/filesystem.js +70 -0
  20. package/dist/frame-payload-codec.d.ts +8 -0
  21. package/dist/frame-payload-codec.js +14 -0
  22. package/dist/frame-rpc.d.ts +38 -0
  23. package/dist/frame-rpc.js +73 -0
  24. package/dist/frame-stream.d.ts +27 -0
  25. package/dist/frame-stream.js +99 -0
  26. package/dist/framing.d.ts +7 -0
  27. package/dist/framing.js +22 -0
  28. package/dist/generated/AcpLimitsConfig.d.ts +4 -0
  29. package/dist/generated/AcpLimitsConfig.js +2 -0
  30. package/dist/generated/CreateVmConfig.d.ts +19 -0
  31. package/dist/generated/FsPermissionRule.d.ts +6 -0
  32. package/dist/generated/FsPermissionRuleSet.d.ts +6 -0
  33. package/dist/generated/FsPermissionRuleSet.js +1 -0
  34. package/dist/generated/FsPermissionScope.d.ts +3 -0
  35. package/dist/generated/FsPermissionScope.js +1 -0
  36. package/dist/generated/HttpLimitsConfig.d.ts +3 -0
  37. package/dist/generated/HttpLimitsConfig.js +2 -0
  38. package/dist/generated/JsModuleResolution.d.ts +1 -0
  39. package/dist/generated/JsModuleResolution.js +2 -0
  40. package/dist/generated/JsRuntimeConfig.d.ts +26 -0
  41. package/dist/generated/JsRuntimeConfig.js +1 -0
  42. package/dist/generated/JsRuntimeLimitsConfig.d.ts +7 -0
  43. package/dist/generated/JsRuntimeLimitsConfig.js +2 -0
  44. package/dist/generated/JsRuntimePlatform.d.ts +1 -0
  45. package/dist/generated/JsRuntimePlatform.js +2 -0
  46. package/dist/generated/MountPluginDescriptor.d.ts +4 -0
  47. package/dist/generated/MountPluginDescriptor.js +2 -0
  48. package/dist/generated/NativeRootFilesystemConfig.d.ts +5 -0
  49. package/dist/generated/NativeRootFilesystemConfig.js +1 -0
  50. package/dist/generated/PatternPermissionRule.d.ts +6 -0
  51. package/dist/generated/PatternPermissionRule.js +1 -0
  52. package/dist/generated/PatternPermissionRuleSet.d.ts +6 -0
  53. package/dist/generated/PatternPermissionRuleSet.js +1 -0
  54. package/dist/generated/PatternPermissionScope.d.ts +3 -0
  55. package/dist/generated/PatternPermissionScope.js +1 -0
  56. package/dist/generated/PermissionMode.d.ts +1 -0
  57. package/dist/generated/PermissionMode.js +2 -0
  58. package/dist/generated/PermissionsPolicy.d.ts +10 -0
  59. package/dist/generated/PermissionsPolicy.js +1 -0
  60. package/dist/generated/PluginLimitsConfig.d.ts +4 -0
  61. package/dist/generated/PluginLimitsConfig.js +2 -0
  62. package/dist/generated/PythonLimitsConfig.d.ts +5 -0
  63. package/dist/generated/PythonLimitsConfig.js +2 -0
  64. package/dist/generated/ResourceLimitsConfig.d.ts +22 -0
  65. package/dist/generated/ResourceLimitsConfig.js +2 -0
  66. package/dist/generated/RootFilesystemConfig.d.ts +9 -0
  67. package/dist/generated/RootFilesystemConfig.js +1 -0
  68. package/dist/generated/RootFilesystemEntry.d.ts +13 -0
  69. package/dist/generated/RootFilesystemEntry.js +1 -0
  70. package/dist/generated/RootFilesystemEntryEncoding.d.ts +1 -0
  71. package/dist/generated/RootFilesystemEntryEncoding.js +2 -0
  72. package/dist/generated/RootFilesystemEntryKind.d.ts +1 -0
  73. package/dist/generated/RootFilesystemEntryKind.js +2 -0
  74. package/dist/generated/RootFilesystemLowerDescriptor.d.ts +7 -0
  75. package/dist/generated/RootFilesystemLowerDescriptor.js +1 -0
  76. package/dist/generated/RootFilesystemMode.d.ts +1 -0
  77. package/dist/generated/RootFilesystemMode.js +2 -0
  78. package/dist/generated/ToolLimitsConfig.d.ts +10 -0
  79. package/dist/generated/ToolLimitsConfig.js +2 -0
  80. package/dist/generated/VmDnsConfig.d.ts +6 -0
  81. package/dist/generated/VmDnsConfig.js +2 -0
  82. package/dist/generated/VmLimitsConfig.d.ts +18 -0
  83. package/dist/generated/VmLimitsConfig.js +1 -0
  84. package/dist/generated/VmListenPolicyConfig.d.ts +5 -0
  85. package/dist/generated/VmListenPolicyConfig.js +2 -0
  86. package/dist/generated/WasmLimitsConfig.d.ts +5 -0
  87. package/dist/generated/WasmLimitsConfig.js +2 -0
  88. package/dist/generated-protocol.d.ts +1037 -0
  89. package/dist/generated-protocol.js +2887 -0
  90. package/dist/index.d.ts +24 -62
  91. package/dist/index.js +24 -53
  92. package/dist/json.d.ts +2 -0
  93. package/dist/json.js +20 -0
  94. package/dist/kernel-proxy.d.ts +149 -0
  95. package/dist/kernel-proxy.js +1733 -0
  96. package/dist/native-client.d.ts +41 -0
  97. package/dist/native-client.js +124 -0
  98. package/dist/node-runtime.d.ts +490 -0
  99. package/dist/node-runtime.js +585 -0
  100. package/dist/numbers.d.ts +1 -0
  101. package/dist/numbers.js +8 -0
  102. package/dist/ownership.d.ts +18 -0
  103. package/dist/ownership.js +77 -0
  104. package/dist/permissions.d.ts +29 -0
  105. package/dist/permissions.js +68 -0
  106. package/dist/process.d.ts +35 -0
  107. package/dist/process.js +125 -0
  108. package/dist/protocol-client.d.ts +46 -0
  109. package/dist/protocol-client.js +180 -0
  110. package/dist/protocol-frames.d.ts +68 -0
  111. package/dist/protocol-frames.js +139 -0
  112. package/dist/protocol-maps.d.ts +28 -0
  113. package/dist/protocol-maps.js +217 -0
  114. package/dist/protocol-schema.d.ts +10 -0
  115. package/dist/protocol-schema.js +11 -0
  116. package/dist/request-payloads.d.ts +137 -0
  117. package/dist/request-payloads.js +210 -0
  118. package/dist/response-payloads.d.ts +107 -0
  119. package/dist/response-payloads.js +161 -0
  120. package/dist/sidecar-client.d.ts +242 -0
  121. package/dist/sidecar-client.js +797 -0
  122. package/dist/state.d.ts +40 -0
  123. package/dist/state.js +44 -0
  124. package/dist/test-runtime.d.ts +526 -0
  125. package/dist/test-runtime.js +2119 -0
  126. package/dist/vm-config.d.ts +31 -0
  127. package/dist/vm-config.js +1 -0
  128. package/fixtures/alpine-defaults.json +520 -0
  129. package/fixtures/base-filesystem.json +528 -0
  130. package/package.json +193 -115
  131. package/LICENSE +0 -191
  132. package/dist/bridge-setup.d.ts +0 -6
  133. package/dist/bridge-setup.js +0 -9
  134. package/dist/esm-compiler.d.ts +0 -18
  135. package/dist/esm-compiler.js +0 -72
  136. package/dist/fs-helpers.d.ts +0 -23
  137. package/dist/fs-helpers.js +0 -41
  138. package/dist/generated/isolate-runtime.d.ts +0 -19
  139. package/dist/generated/isolate-runtime.js +0 -21
  140. package/dist/generated/polyfills.d.ts +0 -82
  141. package/dist/generated/polyfills.js +0 -82
  142. package/dist/isolate-runtime/apply-custom-global-policy.js +0 -53
  143. package/dist/isolate-runtime/apply-timing-mitigation-freeze.js +0 -130
  144. package/dist/isolate-runtime/apply-timing-mitigation-off.js +0 -14
  145. package/dist/isolate-runtime/bridge-attach.js +0 -29
  146. package/dist/isolate-runtime/bridge-initial-globals.js +0 -385
  147. package/dist/isolate-runtime/eval-script-result.js +0 -8
  148. package/dist/isolate-runtime/global-exposure-helpers.js +0 -36
  149. package/dist/isolate-runtime/init-commonjs-module-globals.js +0 -28
  150. package/dist/isolate-runtime/override-process-cwd.js +0 -8
  151. package/dist/isolate-runtime/override-process-env.js +0 -8
  152. package/dist/isolate-runtime/require-setup.js +0 -4153
  153. package/dist/isolate-runtime/set-commonjs-file-globals.js +0 -36
  154. package/dist/isolate-runtime/set-stdin-data.js +0 -10
  155. package/dist/isolate-runtime/setup-dynamic-import.js +0 -123
  156. package/dist/isolate-runtime/setup-fs-facade.js +0 -87
  157. package/dist/kernel/command-registry.d.ts +0 -44
  158. package/dist/kernel/command-registry.js +0 -114
  159. package/dist/kernel/device-backend.d.ts +0 -14
  160. package/dist/kernel/device-backend.js +0 -251
  161. package/dist/kernel/device-layer.d.ts +0 -12
  162. package/dist/kernel/device-layer.js +0 -271
  163. package/dist/kernel/dns-cache.d.ts +0 -29
  164. package/dist/kernel/dns-cache.js +0 -52
  165. package/dist/kernel/fd-table.d.ts +0 -84
  166. package/dist/kernel/fd-table.js +0 -278
  167. package/dist/kernel/file-lock.d.ts +0 -34
  168. package/dist/kernel/file-lock.js +0 -122
  169. package/dist/kernel/host-adapter.d.ts +0 -50
  170. package/dist/kernel/host-adapter.js +0 -8
  171. package/dist/kernel/index.d.ts +0 -36
  172. package/dist/kernel/index.js +0 -34
  173. package/dist/kernel/kernel.d.ts +0 -9
  174. package/dist/kernel/kernel.js +0 -1415
  175. package/dist/kernel/mount-table.d.ts +0 -75
  176. package/dist/kernel/mount-table.js +0 -353
  177. package/dist/kernel/permissions.d.ts +0 -36
  178. package/dist/kernel/permissions.js +0 -150
  179. package/dist/kernel/pipe-manager.d.ts +0 -64
  180. package/dist/kernel/pipe-manager.js +0 -267
  181. package/dist/kernel/proc-backend.d.ts +0 -30
  182. package/dist/kernel/proc-backend.js +0 -428
  183. package/dist/kernel/proc-layer.d.ts +0 -11
  184. package/dist/kernel/proc-layer.js +0 -507
  185. package/dist/kernel/process-table.d.ts +0 -126
  186. package/dist/kernel/process-table.js +0 -651
  187. package/dist/kernel/pty.d.ts +0 -109
  188. package/dist/kernel/pty.js +0 -552
  189. package/dist/kernel/socket-table.d.ts +0 -312
  190. package/dist/kernel/socket-table.js +0 -1188
  191. package/dist/kernel/timer-table.d.ts +0 -54
  192. package/dist/kernel/timer-table.js +0 -108
  193. package/dist/kernel/types.d.ts +0 -541
  194. package/dist/kernel/types.js +0 -98
  195. package/dist/kernel/user.d.ts +0 -29
  196. package/dist/kernel/user.js +0 -35
  197. package/dist/kernel/vfs.d.ts +0 -82
  198. package/dist/kernel/vfs.js +0 -25
  199. package/dist/kernel/wait.d.ts +0 -45
  200. package/dist/kernel/wait.js +0 -112
  201. package/dist/kernel/wstatus.d.ts +0 -21
  202. package/dist/kernel/wstatus.js +0 -33
  203. package/dist/module-resolver.d.ts +0 -29
  204. package/dist/module-resolver.js +0 -314
  205. package/dist/package-bundler.d.ts +0 -41
  206. package/dist/package-bundler.js +0 -497
  207. package/dist/runtime-driver.d.ts +0 -66
  208. package/dist/shared/api-types.d.ts +0 -83
  209. package/dist/shared/bridge-contract.d.ts +0 -772
  210. package/dist/shared/bridge-contract.js +0 -169
  211. package/dist/shared/console-formatter.d.ts +0 -22
  212. package/dist/shared/console-formatter.js +0 -161
  213. package/dist/shared/constants.d.ts +0 -3
  214. package/dist/shared/constants.js +0 -3
  215. package/dist/shared/errors.d.ts +0 -16
  216. package/dist/shared/errors.js +0 -21
  217. package/dist/shared/esm-utils.d.ts +0 -28
  218. package/dist/shared/esm-utils.js +0 -97
  219. package/dist/shared/global-exposure.d.ts +0 -38
  220. package/dist/shared/global-exposure.js +0 -876
  221. package/dist/shared/in-memory-fs.d.ts +0 -16
  222. package/dist/shared/in-memory-fs.js +0 -115
  223. package/dist/shared/permissions.d.ts +0 -36
  224. package/dist/shared/permissions.js +0 -314
  225. package/dist/shared/require-setup.d.ts +0 -6
  226. package/dist/shared/require-setup.js +0 -9
  227. package/dist/test/block-store-conformance.d.ts +0 -34
  228. package/dist/test/block-store-conformance.js +0 -251
  229. package/dist/test/metadata-store-conformance.d.ts +0 -37
  230. package/dist/test/metadata-store-conformance.js +0 -646
  231. package/dist/test/vfs-conformance.d.ts +0 -65
  232. package/dist/test/vfs-conformance.js +0 -842
  233. package/dist/types.d.ts +0 -98
  234. package/dist/types.js +0 -6
  235. package/dist/vfs/chunked-vfs.d.ts +0 -66
  236. package/dist/vfs/chunked-vfs.js +0 -1290
  237. package/dist/vfs/host-block-store.d.ts +0 -19
  238. package/dist/vfs/host-block-store.js +0 -97
  239. package/dist/vfs/memory-block-store.d.ts +0 -16
  240. package/dist/vfs/memory-block-store.js +0 -45
  241. package/dist/vfs/memory-metadata.d.ts +0 -75
  242. package/dist/vfs/memory-metadata.js +0 -528
  243. package/dist/vfs/sqlite-metadata.d.ts +0 -91
  244. package/dist/vfs/sqlite-metadata.js +0 -582
  245. package/dist/vfs/types.d.ts +0 -210
  246. package/dist/vfs/types.js +0 -8
  247. /package/dist/{runtime-driver.js → generated/CreateVmConfig.js} +0 -0
  248. /package/dist/{shared/api-types.js → generated/FsPermissionRule.js} +0 -0
@@ -1,278 +0,0 @@
1
- /**
2
- * Per-PID file descriptor table.
3
- *
4
- * Each process gets its own FD number space. Multiple FDs can share the
5
- * same FileDescription (via dup/dup2), which shares the cursor position.
6
- * Standard FDs 0-2 are pre-allocated per process.
7
- */
8
- import { FILETYPE_REGULAR_FILE, FILETYPE_CHARACTER_DEVICE, O_RDONLY, O_WRONLY, O_CLOEXEC, KernelError, } from "./types.js";
9
- /** Maximum open FDs per process before allocations are rejected (EMFILE). */
10
- export const MAX_FDS_PER_PROCESS = 256;
11
- /**
12
- * FD table for a single process.
13
- *
14
- * Manages FD allocation, dup/dup2, and shared cursor via FileDescription.
15
- */
16
- export class ProcessFDTable {
17
- entries = new Map();
18
- nextFd = 3; // 0, 1, 2 reserved
19
- allocDesc;
20
- constructor(allocDesc) {
21
- this.allocDesc = allocDesc;
22
- }
23
- /** Pre-allocate stdin, stdout, stderr */
24
- initStdio(stdinDesc, stdoutDesc, stderrDesc) {
25
- this.entries.set(0, {
26
- fd: 0,
27
- description: stdinDesc,
28
- rights: 0n,
29
- filetype: FILETYPE_CHARACTER_DEVICE,
30
- cloexec: false,
31
- });
32
- this.entries.set(1, {
33
- fd: 1,
34
- description: stdoutDesc,
35
- rights: 0n,
36
- filetype: FILETYPE_CHARACTER_DEVICE,
37
- cloexec: false,
38
- });
39
- this.entries.set(2, {
40
- fd: 2,
41
- description: stderrDesc,
42
- rights: 0n,
43
- filetype: FILETYPE_CHARACTER_DEVICE,
44
- cloexec: false,
45
- });
46
- }
47
- /** Pre-allocate stdin, stdout, stderr with custom filetypes (for pipe wiring). */
48
- initStdioWithTypes(stdinDesc, stdinType, stdoutDesc, stdoutType, stderrDesc, stderrType) {
49
- // Shared descriptions (from pipes) get refCount bumped
50
- stdinDesc.refCount++;
51
- stdoutDesc.refCount++;
52
- stderrDesc.refCount++;
53
- this.entries.set(0, { fd: 0, description: stdinDesc, rights: 0n, filetype: stdinType, cloexec: false });
54
- this.entries.set(1, { fd: 1, description: stdoutDesc, rights: 0n, filetype: stdoutType, cloexec: false });
55
- this.entries.set(2, { fd: 2, description: stderrDesc, rights: 0n, filetype: stderrType, cloexec: false });
56
- }
57
- /** Open a new FD for the given path and flags */
58
- open(path, flags, filetype) {
59
- const fd = this.allocateFd();
60
- const cloexec = (flags & O_CLOEXEC) !== 0;
61
- const storedFlags = flags & ~O_CLOEXEC;
62
- const description = this.allocDesc(path, storedFlags);
63
- this.entries.set(fd, {
64
- fd,
65
- description,
66
- rights: 0n,
67
- filetype: filetype ?? FILETYPE_REGULAR_FILE,
68
- cloexec,
69
- });
70
- return fd;
71
- }
72
- /** Open a new FD pointing to an existing FileDescription (for pipes, inherited FDs) */
73
- openWith(description, filetype, targetFd) {
74
- const fd = targetFd ?? this.allocateFd();
75
- description.refCount++;
76
- this.entries.set(fd, {
77
- fd,
78
- description,
79
- rights: 0n,
80
- filetype,
81
- cloexec: false,
82
- });
83
- return fd;
84
- }
85
- get(fd) {
86
- return this.entries.get(fd);
87
- }
88
- /** Close an FD. Decrements the refcount on the shared FileDescription. */
89
- close(fd) {
90
- const entry = this.entries.get(fd);
91
- if (!entry)
92
- return false;
93
- entry.description.refCount--;
94
- this.entries.delete(fd);
95
- return true;
96
- }
97
- /** Duplicate an FD — new FD shares the same FileDescription (cursor). cloexec cleared on new FD (POSIX). */
98
- dup(fd) {
99
- const entry = this.entries.get(fd);
100
- if (!entry)
101
- throw new KernelError("EBADF", `bad file descriptor ${fd}`);
102
- const newFd = this.allocateFd();
103
- entry.description.refCount++;
104
- this.entries.set(newFd, {
105
- fd: newFd,
106
- description: entry.description,
107
- rights: entry.rights,
108
- filetype: entry.filetype,
109
- cloexec: false,
110
- });
111
- return newFd;
112
- }
113
- /** Duplicate FD to lowest available >= minFd (F_DUPFD). cloexec cleared on new FD. */
114
- dupMinFd(fd, minFd) {
115
- const entry = this.entries.get(fd);
116
- if (!entry)
117
- throw new KernelError("EBADF", `bad file descriptor ${fd}`);
118
- if (this.entries.size >= MAX_FDS_PER_PROCESS) {
119
- throw new KernelError("EMFILE", "too many open files");
120
- }
121
- let newFd = minFd;
122
- while (this.entries.has(newFd))
123
- newFd++;
124
- entry.description.refCount++;
125
- this.entries.set(newFd, {
126
- fd: newFd,
127
- description: entry.description,
128
- rights: entry.rights,
129
- filetype: entry.filetype,
130
- cloexec: false,
131
- });
132
- return newFd;
133
- }
134
- /** Duplicate oldFd to newFd. Closes newFd first if open. cloexec cleared on new FD (POSIX). */
135
- dup2(oldFd, newFd) {
136
- const entry = this.entries.get(oldFd);
137
- if (!entry)
138
- throw new KernelError("EBADF", `bad file descriptor ${oldFd}`);
139
- if (oldFd === newFd)
140
- return;
141
- // Close newFd if already open
142
- if (this.entries.has(newFd)) {
143
- this.close(newFd);
144
- }
145
- entry.description.refCount++;
146
- this.entries.set(newFd, {
147
- fd: newFd,
148
- description: entry.description,
149
- rights: entry.rights,
150
- filetype: entry.filetype,
151
- cloexec: false,
152
- });
153
- }
154
- stat(fd) {
155
- const entry = this.entries.get(fd);
156
- if (!entry)
157
- throw new KernelError("EBADF", `bad file descriptor ${fd}`);
158
- return {
159
- filetype: entry.filetype,
160
- flags: entry.description.flags,
161
- rights: entry.rights,
162
- };
163
- }
164
- /** Create a copy of this table for a child process (FD inheritance). Skips cloexec FDs. */
165
- fork() {
166
- const child = new ProcessFDTable(this.allocDesc);
167
- child.nextFd = this.nextFd;
168
- for (const [fd, entry] of this.entries) {
169
- if (entry.cloexec)
170
- continue;
171
- entry.description.refCount++;
172
- child.entries.set(fd, {
173
- fd,
174
- description: entry.description,
175
- rights: entry.rights,
176
- filetype: entry.filetype,
177
- cloexec: false,
178
- });
179
- }
180
- return child;
181
- }
182
- /** Close all FDs, decrementing all refcounts. */
183
- closeAll() {
184
- for (const [fd] of this.entries) {
185
- this.close(fd);
186
- }
187
- }
188
- /** Iterate all FD entries (for cleanup inspection). */
189
- *[Symbol.iterator]() {
190
- yield* this.entries.values();
191
- }
192
- allocateFd() {
193
- // Enforce per-process FD limit
194
- if (this.entries.size >= MAX_FDS_PER_PROCESS) {
195
- throw new KernelError("EMFILE", "too many open files");
196
- }
197
- // Find lowest available FD >= nextFd hint
198
- while (this.entries.has(this.nextFd)) {
199
- this.nextFd++;
200
- }
201
- return this.nextFd++;
202
- }
203
- }
204
- /**
205
- * Kernel-level FD table manager.
206
- * Owns per-PID FD tables and coordinates shared FileDescriptions.
207
- */
208
- export class FDTableManager {
209
- tables = new Map();
210
- nextDescriptionId = 1;
211
- /** Per-instance allocator bound to this manager's ID counter. */
212
- allocDesc = (path, flags) => ({
213
- id: this.nextDescriptionId++,
214
- path,
215
- cursor: 0n,
216
- flags,
217
- refCount: 1,
218
- });
219
- /** Create a new FD table for a process with standard FDs. */
220
- create(pid) {
221
- const table = new ProcessFDTable(this.allocDesc);
222
- table.initStdio(this.allocDesc("/dev/stdin", O_RDONLY), this.allocDesc("/dev/stdout", O_WRONLY), this.allocDesc("/dev/stderr", O_WRONLY));
223
- this.tables.set(pid, table);
224
- return table;
225
- }
226
- /**
227
- * Create a new FD table with custom stdio FileDescriptions.
228
- * Used for pipe wiring: pass a pipe read/write end as stdin/stdout/stderr.
229
- * Null entries fall back to default device nodes.
230
- */
231
- createWithStdio(pid, stdinOverride, stdoutOverride, stderrOverride) {
232
- const table = new ProcessFDTable(this.allocDesc);
233
- const stdinDesc = stdinOverride
234
- ? stdinOverride.description
235
- : this.allocDesc("/dev/stdin", O_RDONLY);
236
- const stdinType = stdinOverride?.filetype ?? FILETYPE_CHARACTER_DEVICE;
237
- const stdoutDesc = stdoutOverride
238
- ? stdoutOverride.description
239
- : this.allocDesc("/dev/stdout", O_WRONLY);
240
- const stdoutType = stdoutOverride?.filetype ?? FILETYPE_CHARACTER_DEVICE;
241
- const stderrDesc = stderrOverride
242
- ? stderrOverride.description
243
- : this.allocDesc("/dev/stderr", O_WRONLY);
244
- const stderrType = stderrOverride?.filetype ?? FILETYPE_CHARACTER_DEVICE;
245
- table.initStdioWithTypes(stdinDesc, stdinType, stdoutDesc, stdoutType, stderrDesc, stderrType);
246
- this.tables.set(pid, table);
247
- return table;
248
- }
249
- /** Create a child FD table by forking the parent's. */
250
- fork(parentPid, childPid) {
251
- const parentTable = this.tables.get(parentPid);
252
- if (!parentTable) {
253
- return this.create(childPid);
254
- }
255
- const childTable = parentTable.fork();
256
- this.tables.set(childPid, childTable);
257
- return childTable;
258
- }
259
- get(pid) {
260
- return this.tables.get(pid);
261
- }
262
- /** Check whether a PID has an FD table. */
263
- has(pid) {
264
- return this.tables.has(pid);
265
- }
266
- /** Number of active FD tables. */
267
- get size() {
268
- return this.tables.size;
269
- }
270
- /** Remove and close all FDs for a process. */
271
- remove(pid) {
272
- const table = this.tables.get(pid);
273
- if (table) {
274
- table.closeAll();
275
- this.tables.delete(pid);
276
- }
277
- }
278
- }
@@ -1,34 +0,0 @@
1
- /**
2
- * Advisory file lock manager (flock semantics).
3
- *
4
- * Locks are per-path (inode proxy). Multiple FDs sharing the same
5
- * FileDescription (via dup) share the same lock. Locks are released
6
- * when the description's refCount drops to zero (all FDs closed).
7
- */
8
- export declare const LOCK_SH = 1;
9
- export declare const LOCK_EX = 2;
10
- export declare const LOCK_UN = 8;
11
- export declare const LOCK_NB = 4;
12
- export declare class FileLockManager {
13
- /** path -> lock state */
14
- private locks;
15
- /** descriptionId -> path (for cleanup) */
16
- private descToPath;
17
- /**
18
- * Acquire, upgrade/downgrade, or release a lock.
19
- *
20
- * @param path Resolved file path (inode proxy)
21
- * @param descId FileDescription id (shared across dup'd FDs)
22
- * @param operation LOCK_SH | LOCK_EX | LOCK_UN, optionally | LOCK_NB
23
- */
24
- flock(path: string, descId: number, operation: number): Promise<void>;
25
- /** Release the lock held by a specific description on a path. */
26
- private unlock;
27
- /** Release all locks held by a specific description (called on FD close when refCount drops to 0). */
28
- releaseByDescription(descId: number): void;
29
- /** Check if a description holds any lock. */
30
- hasLock(descId: number): boolean;
31
- private getOrCreate;
32
- private tryAcquire;
33
- private cleanupState;
34
- }
@@ -1,122 +0,0 @@
1
- /**
2
- * Advisory file lock manager (flock semantics).
3
- *
4
- * Locks are per-path (inode proxy). Multiple FDs sharing the same
5
- * FileDescription (via dup) share the same lock. Locks are released
6
- * when the description's refCount drops to zero (all FDs closed).
7
- */
8
- import { KernelError } from "./types.js";
9
- import { WaitQueue } from "./wait.js";
10
- // flock operation flags (POSIX)
11
- export const LOCK_SH = 1;
12
- export const LOCK_EX = 2;
13
- export const LOCK_UN = 8;
14
- export const LOCK_NB = 4;
15
- export class FileLockManager {
16
- /** path -> lock state */
17
- locks = new Map();
18
- /** descriptionId -> path (for cleanup) */
19
- descToPath = new Map();
20
- /**
21
- * Acquire, upgrade/downgrade, or release a lock.
22
- *
23
- * @param path Resolved file path (inode proxy)
24
- * @param descId FileDescription id (shared across dup'd FDs)
25
- * @param operation LOCK_SH | LOCK_EX | LOCK_UN, optionally | LOCK_NB
26
- */
27
- async flock(path, descId, operation) {
28
- const op = operation & ~LOCK_NB;
29
- const nonBlocking = (operation & LOCK_NB) !== 0;
30
- if (op === LOCK_UN) {
31
- this.unlock(path, descId);
32
- return;
33
- }
34
- while (true) {
35
- const state = this.getOrCreate(path);
36
- if (this.tryAcquire(path, state, descId, op)) {
37
- return;
38
- }
39
- if (nonBlocking) {
40
- throw new KernelError("EAGAIN", "resource temporarily unavailable");
41
- }
42
- // Wait indefinitely until an unlock wakes this waiter.
43
- const handle = state.waiters.enqueue();
44
- try {
45
- await handle.wait();
46
- }
47
- finally {
48
- state.waiters.remove(handle);
49
- this.cleanupState(path, state);
50
- }
51
- }
52
- }
53
- /** Release the lock held by a specific description on a path. */
54
- unlock(path, descId) {
55
- const state = this.locks.get(path);
56
- if (!state)
57
- return;
58
- const idx = state.holders.findIndex(h => h.descriptionId === descId);
59
- if (idx >= 0) {
60
- state.holders.splice(idx, 1);
61
- this.descToPath.delete(descId);
62
- state.waiters.wakeOne();
63
- }
64
- this.cleanupState(path, state);
65
- }
66
- /** Release all locks held by a specific description (called on FD close when refCount drops to 0). */
67
- releaseByDescription(descId) {
68
- const path = this.descToPath.get(descId);
69
- if (path === undefined)
70
- return;
71
- this.unlock(path, descId);
72
- }
73
- /** Check if a description holds any lock. */
74
- hasLock(descId) {
75
- return this.descToPath.has(descId);
76
- }
77
- getOrCreate(path) {
78
- let state = this.locks.get(path);
79
- if (!state) {
80
- state = { holders: [], waiters: new WaitQueue() };
81
- this.locks.set(path, state);
82
- }
83
- return state;
84
- }
85
- tryAcquire(path, state, descId, op) {
86
- const existingIdx = state.holders.findIndex(h => h.descriptionId === descId);
87
- if (op === LOCK_SH) {
88
- const conflict = state.holders.some(h => h.type === "ex" && h.descriptionId !== descId);
89
- if (conflict) {
90
- return false;
91
- }
92
- if (existingIdx >= 0) {
93
- state.holders[existingIdx].type = "sh";
94
- }
95
- else {
96
- state.holders.push({ descriptionId: descId, type: "sh" });
97
- this.descToPath.set(descId, path);
98
- }
99
- return true;
100
- }
101
- if (op === LOCK_EX) {
102
- const conflict = state.holders.some(h => h.descriptionId !== descId);
103
- if (conflict) {
104
- return false;
105
- }
106
- if (existingIdx >= 0) {
107
- state.holders[existingIdx].type = "ex";
108
- }
109
- else {
110
- state.holders.push({ descriptionId: descId, type: "ex" });
111
- this.descToPath.set(descId, path);
112
- }
113
- return true;
114
- }
115
- throw new KernelError("EINVAL", `unsupported flock operation ${op}`);
116
- }
117
- cleanupState(path, state) {
118
- if (state.holders.length === 0 && state.waiters.pending === 0) {
119
- this.locks.delete(path);
120
- }
121
- }
122
- }
@@ -1,50 +0,0 @@
1
- /**
2
- * Host adapter interfaces for kernel network delegation.
3
- *
4
- * The kernel uses these interfaces to delegate external I/O to the host
5
- * without knowing the host implementation. Node.js driver implements
6
- * using node:net / node:dgram; browser driver may use WebSocket proxy.
7
- */
8
- /** A connected TCP socket on the host. */
9
- export interface HostSocket {
10
- write(data: Uint8Array): Promise<void>;
11
- /** Returns data or null for EOF. */
12
- read(): Promise<Uint8Array | null>;
13
- close(): Promise<void>;
14
- /** Forward kernel socket options to host socket. */
15
- setOption(level: number, optname: number, optval: number): void;
16
- /** TCP half-close / full shutdown. */
17
- shutdown(how: "read" | "write" | "both"): void;
18
- }
19
- /** A TCP listener on the host. */
20
- export interface HostListener {
21
- /** Accept the next incoming connection. */
22
- accept(): Promise<HostSocket>;
23
- close(): Promise<void>;
24
- /** Actual bound port (useful when binding port 0 for ephemeral ports). */
25
- readonly port: number;
26
- }
27
- /** A UDP socket on the host. */
28
- export interface HostUdpSocket {
29
- recv(): Promise<{
30
- data: Uint8Array;
31
- remoteAddr: {
32
- host: string;
33
- port: number;
34
- };
35
- }>;
36
- close(): Promise<void>;
37
- }
38
- /** DNS lookup result. */
39
- export interface DnsResult {
40
- address: string;
41
- family: 4 | 6;
42
- }
43
- /** Host adapter that the kernel delegates external network I/O to. */
44
- export interface HostNetworkAdapter {
45
- tcpConnect(host: string, port: number): Promise<HostSocket>;
46
- tcpListen(host: string, port: number): Promise<HostListener>;
47
- udpBind(host: string, port: number): Promise<HostUdpSocket>;
48
- udpSend(socket: HostUdpSocket, data: Uint8Array, host: string, port: number): Promise<void>;
49
- dnsLookup(hostname: string, rrtype: string): Promise<DnsResult>;
50
- }
@@ -1,8 +0,0 @@
1
- /**
2
- * Host adapter interfaces for kernel network delegation.
3
- *
4
- * The kernel uses these interfaces to delegate external I/O to the host
5
- * without knowing the host implementation. Node.js driver implements
6
- * using node:net / node:dgram; browser driver may use WebSocket proxy.
7
- */
8
- export {};
@@ -1,36 +0,0 @@
1
- /**
2
- * @secure-exec/kernel
3
- *
4
- * OS kernel providing VFS, FD table, process table, device layer,
5
- * pipes, command registry, and permissions. All runtimes share the
6
- * same kernel instance.
7
- */
8
- export { createKernel } from "./kernel.js";
9
- export type { FsMount, Kernel, KernelOptions, KernelInterface, KernelLogger, ExecOptions, ExecResult, SpawnOptions, ManagedProcess, RuntimeDriver, ProcessContext, DriverProcess, ProcessEntry, ProcessInfo, FDStat, FileDescription, FDEntry, Pipe, Permissions, PermissionDecision, PermissionCheck, FsAccessRequest, NetworkAccessRequest, ChildProcessAccessRequest, EnvAccessRequest, KernelErrorCode, SignalDisposition, SignalHandler, ProcessSignalState, Termios, TermiosCC, OpenShellOptions, ShellHandle, ConnectTerminalOptions, } from "./types.js";
10
- export { KernelError, defaultTermios, noopKernelLogger } from "./types.js";
11
- export type { VirtualFileSystem, VirtualDirEntry, VirtualStat, } from "./vfs.js";
12
- export { FDTableManager, ProcessFDTable } from "./fd-table.js";
13
- export { ProcessTable } from "./process-table.js";
14
- export { createDeviceLayer } from "./device-layer.js";
15
- export { createProcLayer, createProcessScopedFileSystem, resolveProcSelfPath, } from "./proc-layer.js";
16
- export { createProcBackend } from "./proc-backend.js";
17
- export type { ProcBackendOptions } from "./proc-backend.js";
18
- export { PipeManager } from "./pipe-manager.js";
19
- export { PtyManager } from "./pty.js";
20
- export type { LineDisciplineConfig } from "./pty.js";
21
- export { CommandRegistry } from "./command-registry.js";
22
- export { FileLockManager, LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB } from "./file-lock.js";
23
- export { WaitHandle, WaitQueue } from "./wait.js";
24
- export { TimerTable } from "./timer-table.js";
25
- export type { KernelTimer, TimerTableOptions } from "./timer-table.js";
26
- export { DnsCache } from "./dns-cache.js";
27
- export type { DnsCacheOptions } from "./dns-cache.js";
28
- export { UserManager } from "./user.js";
29
- export type { UserConfig } from "./user.js";
30
- export { SocketTable } from "./socket-table.js";
31
- export type { KernelSocket, SocketState, SockAddr, InetAddr, UnixAddr, UdpDatagram, } from "./socket-table.js";
32
- export { AF_INET, AF_INET6, AF_UNIX, SOCK_STREAM, SOCK_DGRAM, SOL_SOCKET, IPPROTO_TCP, SO_REUSEADDR, SO_KEEPALIVE, SO_RCVBUF, SO_SNDBUF, TCP_NODELAY, MSG_PEEK, MSG_DONTWAIT, MSG_NOSIGNAL, MAX_DATAGRAM_SIZE, MAX_UDP_QUEUE_DEPTH, S_IFSOCK, isInetAddr, isUnixAddr, addrKey, optKey, } from "./socket-table.js";
33
- export type { HostNetworkAdapter, HostSocket, HostListener, HostUdpSocket, DnsResult, } from "./host-adapter.js";
34
- export { wrapFileSystem, filterEnv, checkChildProcess, allowAll, allowAllFs, allowAllNetwork, allowAllChildProcess, allowAllEnv, } from "./permissions.js";
35
- export { O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_TRUNC, O_APPEND, O_CLOEXEC, F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_DUPFD_CLOEXEC, FD_CLOEXEC, SEEK_SET, SEEK_CUR, SEEK_END, FILETYPE_UNKNOWN, FILETYPE_CHARACTER_DEVICE, FILETYPE_DIRECTORY, FILETYPE_REGULAR_FILE, FILETYPE_SYMBOLIC_LINK, FILETYPE_PIPE, SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP, SIGWINCH, SA_RESTART, SA_RESETHAND, SA_NOCLDSTOP, SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK, WNOHANG, } from "./types.js";
36
- export { encodeExitStatus, encodeSignalStatus, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, } from "./wstatus.js";
@@ -1,34 +0,0 @@
1
- /**
2
- * @secure-exec/kernel
3
- *
4
- * OS kernel providing VFS, FD table, process table, device layer,
5
- * pipes, command registry, and permissions. All runtimes share the
6
- * same kernel instance.
7
- */
8
- // Kernel factory
9
- export { createKernel } from "./kernel.js";
10
- // Structured kernel error, termios defaults, and no-op logger
11
- export { KernelError, defaultTermios, noopKernelLogger } from "./types.js";
12
- // Kernel components (for direct use / testing)
13
- export { FDTableManager, ProcessFDTable } from "./fd-table.js";
14
- export { ProcessTable } from "./process-table.js";
15
- export { createDeviceLayer } from "./device-layer.js";
16
- export { createProcLayer, createProcessScopedFileSystem, resolveProcSelfPath, } from "./proc-layer.js";
17
- export { createProcBackend } from "./proc-backend.js";
18
- export { PipeManager } from "./pipe-manager.js";
19
- export { PtyManager } from "./pty.js";
20
- export { CommandRegistry } from "./command-registry.js";
21
- export { FileLockManager, LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB } from "./file-lock.js";
22
- export { WaitHandle, WaitQueue } from "./wait.js";
23
- export { TimerTable } from "./timer-table.js";
24
- export { DnsCache } from "./dns-cache.js";
25
- export { UserManager } from "./user.js";
26
- // Socket table
27
- export { SocketTable } from "./socket-table.js";
28
- export { AF_INET, AF_INET6, AF_UNIX, SOCK_STREAM, SOCK_DGRAM, SOL_SOCKET, IPPROTO_TCP, SO_REUSEADDR, SO_KEEPALIVE, SO_RCVBUF, SO_SNDBUF, TCP_NODELAY, MSG_PEEK, MSG_DONTWAIT, MSG_NOSIGNAL, MAX_DATAGRAM_SIZE, MAX_UDP_QUEUE_DEPTH, S_IFSOCK, isInetAddr, isUnixAddr, addrKey, optKey, } from "./socket-table.js";
29
- // Permissions
30
- export { wrapFileSystem, filterEnv, checkChildProcess, allowAll, allowAllFs, allowAllNetwork, allowAllChildProcess, allowAllEnv, } from "./permissions.js";
31
- // Constants
32
- export { O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_TRUNC, O_APPEND, O_CLOEXEC, F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_DUPFD_CLOEXEC, FD_CLOEXEC, SEEK_SET, SEEK_CUR, SEEK_END, FILETYPE_UNKNOWN, FILETYPE_CHARACTER_DEVICE, FILETYPE_DIRECTORY, FILETYPE_REGULAR_FILE, FILETYPE_SYMBOLIC_LINK, FILETYPE_PIPE, SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP, SIGWINCH, SA_RESTART, SA_RESETHAND, SA_NOCLDSTOP, SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK, WNOHANG, } from "./types.js";
33
- // POSIX wstatus encoding/decoding
34
- export { encodeExitStatus, encodeSignalStatus, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, } from "./wstatus.js";
@@ -1,9 +0,0 @@
1
- /**
2
- * Kernel implementation.
3
- *
4
- * The kernel is the OS. It owns VFS, FD table, process table, device layer,
5
- * pipe manager, command registry, and permissions. Runtimes are execution
6
- * engines that make "syscalls" to the kernel.
7
- */
8
- import type { Kernel, KernelOptions } from "./types.js";
9
- export declare function createKernel(options: KernelOptions): Kernel;