@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,497 +0,0 @@
1
- // Path utilities (since we can't use node:path in a way that works in isolate)
2
- function dirname(p) {
3
- const lastSlash = p.lastIndexOf("/");
4
- if (lastSlash === -1)
5
- return ".";
6
- if (lastSlash === 0)
7
- return "/";
8
- return p.slice(0, lastSlash);
9
- }
10
- function join(...parts) {
11
- const segments = [];
12
- for (const part of parts) {
13
- if (part.startsWith("/")) {
14
- segments.length = 0;
15
- }
16
- for (const seg of part.split("/")) {
17
- if (seg === "..") {
18
- segments.pop();
19
- }
20
- else if (seg && seg !== ".") {
21
- segments.push(seg);
22
- }
23
- }
24
- }
25
- return `/${segments.join("/")}`;
26
- }
27
- const FILE_EXTENSIONS = [".js", ".json", ".mjs", ".cjs"];
28
- export function createResolutionCache() {
29
- return {
30
- resolveResults: new Map(),
31
- packageJsonResults: new Map(),
32
- existsResults: new Map(),
33
- statResults: new Map(),
34
- };
35
- }
36
- /**
37
- * Resolve a module request to an absolute path in the virtual filesystem
38
- */
39
- export async function resolveModule(request, fromDir, fs, mode = "require", cache) {
40
- // Check top-level cache
41
- if (cache) {
42
- const cacheKey = `${request}\0${fromDir}\0${mode}`;
43
- if (cache.resolveResults.has(cacheKey)) {
44
- return cache.resolveResults.get(cacheKey);
45
- }
46
- }
47
- let result;
48
- // Absolute paths - resolve directly
49
- if (request.startsWith("/")) {
50
- result = await resolveAbsolute(request, fs, mode, cache);
51
- }
52
- else if (
53
- // Relative imports (including bare '.' and '..')
54
- request.startsWith("./") ||
55
- request.startsWith("../") ||
56
- request === "." ||
57
- request === "..") {
58
- result = await resolveRelative(request, fromDir, fs, mode, cache);
59
- }
60
- else if (request.startsWith("#")) {
61
- // Package import maps, e.g. "#dev"
62
- result = await resolvePackageImports(request, fromDir, fs, mode, cache);
63
- }
64
- else {
65
- // Bare imports - walk up node_modules
66
- result = await resolveNodeModules(request, fromDir, fs, mode, cache);
67
- }
68
- // Store in top-level cache
69
- if (cache) {
70
- const cacheKey = `${request}\0${fromDir}\0${mode}`;
71
- cache.resolveResults.set(cacheKey, result);
72
- }
73
- return result;
74
- }
75
- /** Resolve `#`-prefixed import-map specifiers by walking up to find the nearest package.json with `imports`. */
76
- async function resolvePackageImports(request, fromDir, fs, mode, cache) {
77
- let dir = fromDir;
78
- while (dir !== "" && dir !== ".") {
79
- const pkgJsonPath = join(dir, "package.json");
80
- const pkgJson = await readPackageJson(fs, pkgJsonPath, cache);
81
- if (pkgJson?.imports !== undefined) {
82
- const target = resolveImportsTarget(pkgJson.imports, request, mode);
83
- if (!target) {
84
- return null;
85
- }
86
- if (target.startsWith("#")) {
87
- // Avoid recursive import-map loops.
88
- return null;
89
- }
90
- const targetPath = target.startsWith("/")
91
- ? target
92
- : join(dir, normalizePackagePath(target));
93
- return resolvePath(targetPath, fs, mode, cache);
94
- }
95
- if (dir === "/") {
96
- break;
97
- }
98
- dir = dirname(dir);
99
- }
100
- return null;
101
- }
102
- /**
103
- * Resolve an absolute path
104
- */
105
- async function resolveAbsolute(request, fs, mode, cache) {
106
- return resolvePath(request, fs, mode, cache);
107
- }
108
- /**
109
- * Resolve a relative import
110
- */
111
- async function resolveRelative(request, fromDir, fs, mode, cache) {
112
- const basePath = join(fromDir, request);
113
- return resolvePath(basePath, fs, mode, cache);
114
- }
115
- /**
116
- * Resolve a bare module import by walking up node_modules
117
- */
118
- /** Walk up from `fromDir` checking `node_modules/` (including pnpm virtual-store layouts) for the package. */
119
- async function resolveNodeModules(request, fromDir, fs, mode, cache) {
120
- // Handle scoped packages: @scope/package
121
- let packageName;
122
- let subpath;
123
- if (request.startsWith("@")) {
124
- // Scoped package: @scope/package or @scope/package/subpath
125
- const parts = request.split("/");
126
- if (parts.length >= 2) {
127
- packageName = `${parts[0]}/${parts[1]}`;
128
- subpath = parts.slice(2).join("/");
129
- }
130
- else {
131
- return null;
132
- }
133
- }
134
- else {
135
- // Regular package: package or package/subpath
136
- const slashIndex = request.indexOf("/");
137
- if (slashIndex === -1) {
138
- packageName = request;
139
- subpath = "";
140
- }
141
- else {
142
- packageName = request.slice(0, slashIndex);
143
- subpath = request.slice(slashIndex + 1);
144
- }
145
- }
146
- let dir = fromDir;
147
- while (dir !== "" && dir !== ".") {
148
- const candidatePackageDirs = getNodeModulesCandidatePackageDirs(dir, packageName);
149
- for (const packageDir of candidatePackageDirs) {
150
- let entry;
151
- try {
152
- entry = await resolvePackageEntryFromDir(packageDir, subpath, fs, mode, cache);
153
- }
154
- catch (error) {
155
- if (isPermissionProbeError(error)) {
156
- continue;
157
- }
158
- throw error;
159
- }
160
- if (entry) {
161
- return entry;
162
- }
163
- }
164
- if (dir === "/")
165
- break;
166
- dir = dirname(dir);
167
- }
168
- // Also check root node_modules
169
- const rootPackageDir = join("/node_modules", packageName);
170
- let rootEntry;
171
- try {
172
- rootEntry = await resolvePackageEntryFromDir(rootPackageDir, subpath, fs, mode, cache);
173
- }
174
- catch (error) {
175
- if (isPermissionProbeError(error)) {
176
- rootEntry = null;
177
- }
178
- else {
179
- throw error;
180
- }
181
- }
182
- if (rootEntry) {
183
- return rootEntry;
184
- }
185
- return null;
186
- }
187
- function getNodeModulesCandidatePackageDirs(dir, packageName) {
188
- const candidates = new Set();
189
- candidates.add(join(dir, "node_modules", packageName));
190
- candidates.add(join(dir, "node_modules", ".pnpm", "node_modules", packageName));
191
- // Match Node's "parent node_modules" lookup when the current directory is
192
- // already a node_modules folder.
193
- if (dir === "/node_modules" || dir.endsWith("/node_modules")) {
194
- candidates.add(join(dir, packageName));
195
- }
196
- // Support pnpm virtual-store layouts where transitive dependencies are linked
197
- // under <root>/node_modules/.pnpm/node_modules.
198
- const nodeModulesSegment = "/node_modules/";
199
- const nodeModulesIndex = dir.lastIndexOf(nodeModulesSegment);
200
- if (nodeModulesIndex !== -1) {
201
- const nodeModulesRoot = dir.slice(0, nodeModulesIndex + nodeModulesSegment.length - 1);
202
- candidates.add(join(nodeModulesRoot, ".pnpm", "node_modules", packageName));
203
- }
204
- return Array.from(candidates);
205
- }
206
- /**
207
- * Given a package directory and optional subpath, resolve the entry file using
208
- * `exports` map (if present), then `main`, then `index.js` fallback. When
209
- * `exports` is defined, no fallback to `main` occurs (Node.js semantics).
210
- */
211
- async function resolvePackageEntryFromDir(packageDir, subpath, fs, mode, cache) {
212
- const pkgJsonPath = join(packageDir, "package.json");
213
- const pkgJson = await readPackageJson(fs, pkgJsonPath, cache);
214
- if (!pkgJson && !(await cachedSafeExists(fs, packageDir, cache))) {
215
- return null;
216
- }
217
- // If package uses "exports", follow it and do not fall back to main/subpath
218
- if (pkgJson?.exports !== undefined) {
219
- const exportsTarget = resolveExportsTarget(pkgJson.exports, subpath ? `./${subpath}` : ".", mode);
220
- if (!exportsTarget) {
221
- return null;
222
- }
223
- const targetPath = join(packageDir, normalizePackagePath(exportsTarget));
224
- const resolvedTarget = await resolvePath(targetPath, fs, mode, cache);
225
- return resolvedTarget ?? targetPath;
226
- }
227
- // Bare subpath import without exports map: package/sub/path
228
- if (subpath) {
229
- return resolvePath(join(packageDir, subpath), fs, mode, cache);
230
- }
231
- // Root package import
232
- const entryField = getPackageEntryField(pkgJson, mode);
233
- if (entryField) {
234
- const entryPath = join(packageDir, normalizePackagePath(entryField));
235
- const resolved = await resolvePath(entryPath, fs, mode, cache);
236
- if (resolved)
237
- return resolved;
238
- if (pkgJson) {
239
- return entryPath;
240
- }
241
- }
242
- // Default fallback
243
- return resolvePath(join(packageDir, "index"), fs, mode, cache);
244
- }
245
- async function resolvePath(basePath, fs, mode, cache) {
246
- let isDirectory = false;
247
- // Use cached stat when available
248
- const statResult = await cachedStat(fs, basePath, cache);
249
- if (statResult !== null) {
250
- if (!statResult.isDirectory) {
251
- return basePath;
252
- }
253
- isDirectory = true;
254
- }
255
- // For extensionless specifiers, try files before directory resolution.
256
- for (const ext of FILE_EXTENSIONS) {
257
- const withExt = `${basePath}${ext}`;
258
- if (await cachedSafeExists(fs, withExt, cache)) {
259
- return withExt;
260
- }
261
- }
262
- if (isDirectory) {
263
- const pkgJsonPath = join(basePath, "package.json");
264
- const pkgJson = await readPackageJson(fs, pkgJsonPath, cache);
265
- const entryField = getPackageEntryField(pkgJson, mode);
266
- if (entryField) {
267
- const entryPath = join(basePath, normalizePackagePath(entryField));
268
- // Avoid directory self-reference loops like "main": "."
269
- if (entryPath !== basePath) {
270
- const entry = await resolvePath(entryPath, fs, mode, cache);
271
- if (entry)
272
- return entry;
273
- }
274
- }
275
- for (const ext of FILE_EXTENSIONS) {
276
- const indexPath = join(basePath, `index${ext}`);
277
- if (await cachedSafeExists(fs, indexPath, cache)) {
278
- return indexPath;
279
- }
280
- }
281
- }
282
- return null;
283
- }
284
- async function readPackageJson(fs, pkgJsonPath, cache) {
285
- if (cache?.packageJsonResults.has(pkgJsonPath)) {
286
- return cache.packageJsonResults.get(pkgJsonPath);
287
- }
288
- if (!(await cachedSafeExists(fs, pkgJsonPath, cache))) {
289
- cache?.packageJsonResults.set(pkgJsonPath, null);
290
- return null;
291
- }
292
- try {
293
- const result = JSON.parse(await fs.readTextFile(pkgJsonPath));
294
- cache?.packageJsonResults.set(pkgJsonPath, result);
295
- return result;
296
- }
297
- catch {
298
- cache?.packageJsonResults.set(pkgJsonPath, null);
299
- return null;
300
- }
301
- }
302
- /** Treat EACCES/EPERM as "path not available" during resolution probing. */
303
- function isPermissionProbeError(error) {
304
- const err = error;
305
- return err?.code === "EACCES" || err?.code === "EPERM";
306
- }
307
- async function safeExists(fs, path) {
308
- try {
309
- return await fs.exists(path);
310
- }
311
- catch (error) {
312
- if (isPermissionProbeError(error)) {
313
- return false;
314
- }
315
- throw error;
316
- }
317
- }
318
- /** Cached wrapper around safeExists — avoids repeated VFS probes for the same path. */
319
- async function cachedSafeExists(fs, path, cache) {
320
- if (cache?.existsResults.has(path)) {
321
- return cache.existsResults.get(path);
322
- }
323
- const result = await safeExists(fs, path);
324
- cache?.existsResults.set(path, result);
325
- return result;
326
- }
327
- /** Cached stat — returns { isDirectory } or null for ENOENT. */
328
- async function cachedStat(fs, path, cache) {
329
- if (cache?.statResults.has(path)) {
330
- return cache.statResults.get(path);
331
- }
332
- try {
333
- const statInfo = await fs.stat(path);
334
- const result = { isDirectory: statInfo.isDirectory };
335
- cache?.statResults.set(path, result);
336
- return result;
337
- }
338
- catch (error) {
339
- const err = error;
340
- if (err?.code && err.code !== "ENOENT") {
341
- throw err;
342
- }
343
- cache?.statResults.set(path, null);
344
- return null;
345
- }
346
- }
347
- function normalizePackagePath(value) {
348
- return value.replace(/^\.\//, "").replace(/\/$/, "");
349
- }
350
- function getPackageEntryField(pkgJson, _mode) {
351
- if (!pkgJson)
352
- return "index.js";
353
- // Match Node's package entrypoint precedence when exports is absent.
354
- if (typeof pkgJson.main === "string")
355
- return pkgJson.main;
356
- return "index.js";
357
- }
358
- /**
359
- * Implement Node.js `package.json` "exports" resolution. Handles string, array,
360
- * conditions-object, subpath keys, and wildcard `*` patterns.
361
- */
362
- function resolveExportsTarget(exportsField, subpath, mode) {
363
- // "exports": "./dist/index.js"
364
- if (typeof exportsField === "string") {
365
- return subpath === "." ? exportsField : null;
366
- }
367
- // "exports": ["./a.js", "./b.js"]
368
- if (Array.isArray(exportsField)) {
369
- for (const item of exportsField) {
370
- const resolved = resolveExportsTarget(item, subpath, mode);
371
- if (resolved)
372
- return resolved;
373
- }
374
- return null;
375
- }
376
- if (!exportsField || typeof exportsField !== "object") {
377
- return null;
378
- }
379
- const record = exportsField;
380
- // Root conditions object (no "./" keys)
381
- if (subpath === "." && !Object.keys(record).some((key) => key.startsWith("./"))) {
382
- return resolveConditionalTarget(record, mode);
383
- }
384
- // Exact subpath key first
385
- if (subpath in record) {
386
- return resolveExportsTarget(record[subpath], ".", mode);
387
- }
388
- // Pattern keys like "./*"
389
- for (const [key, value] of Object.entries(record)) {
390
- if (!key.includes("*"))
391
- continue;
392
- const [prefix, suffix] = key.split("*");
393
- if (!subpath.startsWith(prefix) || !subpath.endsWith(suffix))
394
- continue;
395
- const wildcard = subpath.slice(prefix.length, subpath.length - suffix.length);
396
- const resolved = resolveExportsTarget(value, ".", mode);
397
- if (!resolved)
398
- continue;
399
- return resolved.replaceAll("*", wildcard);
400
- }
401
- // Root key may still be present in object with subpaths
402
- if (subpath === "." && "." in record) {
403
- return resolveExportsTarget(record["."], ".", mode);
404
- }
405
- return null;
406
- }
407
- /** Pick the first matching condition key (import/require/node/default) from an exports conditions object. */
408
- function resolveConditionalTarget(record, mode) {
409
- const order = mode === "import"
410
- ? ["import", "node", "module", "default", "require"]
411
- : ["require", "node", "default", "import", "module"];
412
- for (const key of order) {
413
- if (!(key in record))
414
- continue;
415
- const resolved = resolveExportsTarget(record[key], ".", mode);
416
- if (resolved)
417
- return resolved;
418
- }
419
- // Last resort: first key that resolves
420
- for (const value of Object.values(record)) {
421
- const resolved = resolveExportsTarget(value, ".", mode);
422
- if (resolved)
423
- return resolved;
424
- }
425
- return null;
426
- }
427
- /** Resolve a `#`-prefixed specifier against a package.json `imports` field, including wildcard patterns. */
428
- function resolveImportsTarget(importsField, specifier, mode) {
429
- if (typeof importsField === "string") {
430
- return importsField;
431
- }
432
- if (Array.isArray(importsField)) {
433
- for (const item of importsField) {
434
- const resolved = resolveImportsTarget(item, specifier, mode);
435
- if (resolved) {
436
- return resolved;
437
- }
438
- }
439
- return null;
440
- }
441
- if (!importsField || typeof importsField !== "object") {
442
- return null;
443
- }
444
- const record = importsField;
445
- if (specifier in record) {
446
- return resolveExportsTarget(record[specifier], ".", mode);
447
- }
448
- for (const [key, value] of Object.entries(record)) {
449
- if (!key.includes("*"))
450
- continue;
451
- const [prefix, suffix] = key.split("*");
452
- if (!specifier.startsWith(prefix) || !specifier.endsWith(suffix))
453
- continue;
454
- const wildcard = specifier.slice(prefix.length, specifier.length - suffix.length);
455
- const resolved = resolveExportsTarget(value, ".", mode);
456
- if (!resolved)
457
- continue;
458
- return resolved.replaceAll("*", wildcard);
459
- }
460
- return null;
461
- }
462
- /**
463
- * Load a file's content from the virtual filesystem
464
- */
465
- export async function loadFile(path, fs) {
466
- try {
467
- return await fs.readTextFile(path);
468
- }
469
- catch {
470
- return null;
471
- }
472
- }
473
- /**
474
- * Legacy function - bundle a package from node_modules (simple approach)
475
- * This is kept for backwards compatibility but the new dynamic resolution is preferred
476
- */
477
- export async function bundlePackage(packageName, fs) {
478
- // Resolve the package entry point
479
- const entryPath = await resolveNodeModules(packageName, "/", fs, "require");
480
- if (!entryPath) {
481
- return null;
482
- }
483
- try {
484
- const entryCode = await fs.readTextFile(entryPath);
485
- // Wrap the code in an IIFE that sets up module.exports
486
- const wrappedCode = `(function() {
487
- var module = { exports: {} };
488
- var exports = module.exports;
489
- ${entryCode}
490
- return module.exports;
491
- })()`;
492
- return wrappedCode;
493
- }
494
- catch {
495
- return null;
496
- }
497
- }
@@ -1,66 +0,0 @@
1
- import type { StdioHook, ExecOptions, ExecResult, OSConfig, PythonRunOptions, PythonRunResult, ProcessConfig, RunResult, TimingMitigation } from "./shared/api-types.js";
2
- import type { Permissions } from "./kernel/types.js";
3
- import type { VirtualFileSystem } from "./kernel/vfs.js";
4
- import type { CommandExecutor, NetworkAdapter } from "./types.js";
5
- export interface DriverRuntimeConfig {
6
- process: ProcessConfig;
7
- os: OSConfig;
8
- }
9
- export interface ResourceBudgets {
10
- /** Maximum total stdout/stderr bytes before subsequent writes are silently dropped. */
11
- maxOutputBytes?: number;
12
- /** Maximum total bridge calls (fs, network, timers, child_process) before errors are returned. */
13
- maxBridgeCalls?: number;
14
- /** Maximum concurrent host-side timers (setTimeout/setInterval with delay > 0). */
15
- maxTimers?: number;
16
- /** Maximum child_process.spawn() invocations per execution. */
17
- maxChildProcesses?: number;
18
- /** Maximum concurrent active handles (child processes, timers, servers) in the bridge handle map. */
19
- maxHandles?: number;
20
- }
21
- export interface RuntimeDriverOptions {
22
- system: SystemDriver;
23
- runtime: DriverRuntimeConfig;
24
- memoryLimit?: number;
25
- cpuTimeLimitMs?: number;
26
- timingMitigation?: TimingMitigation;
27
- onStdio?: StdioHook;
28
- payloadLimits?: {
29
- base64TransferBytes?: number;
30
- jsonPayloadBytes?: number;
31
- };
32
- resourceBudgets?: ResourceBudgets;
33
- }
34
- export interface SharedRuntimeDriver {
35
- exec(code: string, options?: ExecOptions): Promise<ExecResult>;
36
- dispose(): void;
37
- terminate?(): Promise<void>;
38
- }
39
- export interface NodeRuntimeDriver extends SharedRuntimeDriver {
40
- run<T = unknown>(code: string, filePath?: string): Promise<RunResult<T>>;
41
- readonly network?: Pick<NetworkAdapter, "fetch" | "dnsLookup" | "httpRequest">;
42
- unsafeIsolate?: unknown;
43
- createUnsafeContext?(options?: {
44
- env?: Record<string, string>;
45
- cwd?: string;
46
- filePath?: string;
47
- }): Promise<unknown>;
48
- }
49
- export interface PythonRuntimeDriver extends SharedRuntimeDriver {
50
- run<T = unknown>(code: string, options?: PythonRunOptions): Promise<PythonRunResult<T>>;
51
- }
52
- export interface NodeRuntimeDriverFactory {
53
- createRuntimeDriver(options: RuntimeDriverOptions): NodeRuntimeDriver;
54
- }
55
- export interface PythonRuntimeDriverFactory {
56
- createRuntimeDriver(options: RuntimeDriverOptions): PythonRuntimeDriver;
57
- }
58
- export interface SystemDriver {
59
- filesystem?: VirtualFileSystem;
60
- network?: NetworkAdapter;
61
- commandExecutor?: CommandExecutor;
62
- permissions?: Permissions;
63
- runtime: DriverRuntimeConfig;
64
- }
65
- export type RuntimeDriver = NodeRuntimeDriver;
66
- export type RuntimeDriverFactory = NodeRuntimeDriverFactory;
@@ -1,83 +0,0 @@
1
- export type TimingMitigation = "off" | "freeze";
2
- export type StdioChannel = "stdout" | "stderr";
3
- export interface StdioEvent {
4
- channel: StdioChannel;
5
- message: string;
6
- }
7
- export type StdioHook = (event: StdioEvent) => void;
8
- export interface ProcessConfig {
9
- platform?: string;
10
- arch?: string;
11
- version?: string;
12
- cwd?: string;
13
- env?: Record<string, string>;
14
- argv?: string[];
15
- execPath?: string;
16
- pid?: number;
17
- ppid?: number;
18
- uid?: number;
19
- gid?: number;
20
- /** Stdin data to provide to the script */
21
- stdin?: string;
22
- /** Internal execution timing policy for bridge/process polyfills */
23
- timingMitigation?: TimingMitigation;
24
- /** Internal frozen clock source used when timing mitigation is enabled */
25
- frozenTimeMs?: number;
26
- /** Whether stdin is a TTY (PTY slave attached) */
27
- stdinIsTTY?: boolean;
28
- /** Whether stdout is a TTY (PTY slave attached) */
29
- stdoutIsTTY?: boolean;
30
- /** Whether stderr is a TTY (PTY slave attached) */
31
- stderrIsTTY?: boolean;
32
- /** Terminal columns (from PTY dimensions). */
33
- cols?: number;
34
- /** Terminal rows (from PTY dimensions). */
35
- rows?: number;
36
- }
37
- export interface OSConfig {
38
- platform?: string;
39
- arch?: string;
40
- type?: string;
41
- release?: string;
42
- version?: string;
43
- homedir?: string;
44
- tmpdir?: string;
45
- hostname?: string;
46
- }
47
- export interface ExecutionStatus {
48
- code: number;
49
- errorMessage?: string;
50
- }
51
- export interface RunResult<T = unknown> extends ExecutionStatus {
52
- exports?: T;
53
- }
54
- export interface PythonRunOptions {
55
- filePath?: string;
56
- globals?: string[];
57
- env?: Record<string, string>;
58
- cwd?: string;
59
- stdin?: string;
60
- cpuTimeLimitMs?: number;
61
- onStdio?: StdioHook;
62
- }
63
- export interface PythonRunResult<T = unknown> extends ExecutionStatus {
64
- value?: T;
65
- globals?: Record<string, unknown>;
66
- }
67
- export interface ExecOptions {
68
- filePath?: string;
69
- env?: Record<string, string>;
70
- cwd?: string;
71
- /** Stdin data to pass to the script */
72
- stdin?: string;
73
- /** Maximum CPU time budget in milliseconds */
74
- cpuTimeLimitMs?: number;
75
- /** Timing side-channel mitigation mode */
76
- timingMitigation?: TimingMitigation;
77
- /** Optional streaming hook for console output events */
78
- onStdio?: StdioHook;
79
- /** Override execution mode. 'run' mode processes async operations (timers, network). */
80
- mode?: "exec" | "run";
81
- }
82
- export interface ExecResult extends ExecutionStatus {
83
- }