@ricsam/isolate 0.0.1 → 0.1.1

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 (299) hide show
  1. package/README.md +243 -34
  2. package/dist/cjs/bridge/diagnostics.cjs +58 -0
  3. package/dist/cjs/bridge/diagnostics.cjs.map +10 -0
  4. package/dist/cjs/bridge/legacy-adapters.cjs +242 -0
  5. package/dist/cjs/bridge/legacy-adapters.cjs.map +10 -0
  6. package/dist/cjs/bridge/request-context.cjs +59 -0
  7. package/dist/cjs/bridge/request-context.cjs.map +10 -0
  8. package/dist/cjs/bridge/runtime-bindings.cjs +367 -0
  9. package/dist/cjs/bridge/runtime-bindings.cjs.map +10 -0
  10. package/dist/cjs/browser/browser-runtime.cjs +157 -0
  11. package/dist/cjs/browser/browser-runtime.cjs.map +10 -0
  12. package/dist/cjs/daemon.cjs +91 -0
  13. package/dist/cjs/daemon.cjs.map +10 -0
  14. package/dist/cjs/files/index.cjs +140 -0
  15. package/dist/cjs/files/index.cjs.map +10 -0
  16. package/dist/cjs/host/create-isolate-host.cjs +235 -0
  17. package/dist/cjs/host/create-isolate-host.cjs.map +10 -0
  18. package/dist/cjs/host/index.cjs +47 -0
  19. package/dist/cjs/host/index.cjs.map +10 -0
  20. package/dist/cjs/index.cjs +55 -0
  21. package/dist/cjs/index.cjs.map +10 -0
  22. package/dist/cjs/internal/client/connection.cjs +1919 -0
  23. package/dist/cjs/internal/client/connection.cjs.map +10 -0
  24. package/dist/cjs/internal/client/index.cjs +48 -0
  25. package/dist/cjs/internal/client/index.cjs.map +10 -0
  26. package/dist/cjs/internal/client/types.cjs +30 -0
  27. package/dist/cjs/internal/client/types.cjs.map +9 -0
  28. package/dist/cjs/internal/console/index.cjs +506 -0
  29. package/dist/cjs/internal/console/index.cjs.map +10 -0
  30. package/dist/cjs/internal/console/utils.cjs +70 -0
  31. package/dist/cjs/internal/console/utils.cjs.map +10 -0
  32. package/dist/cjs/internal/core/index.cjs +2745 -0
  33. package/dist/cjs/internal/core/index.cjs.map +10 -0
  34. package/dist/cjs/internal/crypto/index.cjs +470 -0
  35. package/dist/cjs/internal/crypto/index.cjs.map +10 -0
  36. package/dist/cjs/internal/daemon/callback-fs-handler.cjs +355 -0
  37. package/dist/cjs/internal/daemon/callback-fs-handler.cjs.map +10 -0
  38. package/dist/cjs/internal/daemon/connection.cjs +1952 -0
  39. package/dist/cjs/internal/daemon/connection.cjs.map +10 -0
  40. package/dist/cjs/internal/daemon/daemon.cjs +98 -0
  41. package/dist/cjs/internal/daemon/daemon.cjs.map +10 -0
  42. package/dist/cjs/internal/daemon/index.cjs +145 -0
  43. package/dist/cjs/internal/daemon/index.cjs.map +10 -0
  44. package/dist/cjs/internal/daemon/runtime-pool.cjs +106 -0
  45. package/dist/cjs/internal/daemon/runtime-pool.cjs.map +10 -0
  46. package/dist/cjs/internal/daemon/types.cjs +30 -0
  47. package/dist/cjs/internal/daemon/types.cjs.map +9 -0
  48. package/dist/cjs/internal/encoding/index.cjs +419 -0
  49. package/dist/cjs/internal/encoding/index.cjs.map +10 -0
  50. package/dist/cjs/internal/fetch/consistency/origins.cjs +598 -0
  51. package/dist/cjs/internal/fetch/consistency/origins.cjs.map +10 -0
  52. package/dist/cjs/internal/fetch/index.cjs +2640 -0
  53. package/dist/cjs/internal/fetch/index.cjs.map +10 -0
  54. package/dist/cjs/internal/fetch/stream-state.cjs +256 -0
  55. package/dist/cjs/internal/fetch/stream-state.cjs.map +10 -0
  56. package/dist/cjs/internal/fs/index.cjs +847 -0
  57. package/dist/cjs/internal/fs/index.cjs.map +10 -0
  58. package/dist/cjs/internal/fs/node-adapter.cjs +254 -0
  59. package/dist/cjs/internal/fs/node-adapter.cjs.map +10 -0
  60. package/dist/cjs/internal/module-loader/bundle.cjs +482 -0
  61. package/dist/cjs/internal/module-loader/bundle.cjs.map +10 -0
  62. package/dist/cjs/internal/module-loader/index.cjs +240 -0
  63. package/dist/cjs/internal/module-loader/index.cjs.map +10 -0
  64. package/dist/cjs/internal/module-loader/mappings.cjs +120 -0
  65. package/dist/cjs/internal/module-loader/mappings.cjs.map +10 -0
  66. package/dist/cjs/internal/module-loader/resolve.cjs +177 -0
  67. package/dist/cjs/internal/module-loader/resolve.cjs.map +10 -0
  68. package/dist/cjs/internal/module-loader/strip-types.cjs +236 -0
  69. package/dist/cjs/internal/module-loader/strip-types.cjs.map +10 -0
  70. package/dist/cjs/internal/path/index.cjs +503 -0
  71. package/dist/cjs/internal/path/index.cjs.map +10 -0
  72. package/dist/cjs/internal/playwright/client.cjs +49 -0
  73. package/dist/cjs/internal/playwright/client.cjs.map +10 -0
  74. package/dist/cjs/internal/playwright/handler.cjs +1416 -0
  75. package/dist/cjs/internal/playwright/handler.cjs.map +10 -0
  76. package/dist/cjs/internal/playwright/index.cjs +1289 -0
  77. package/dist/cjs/internal/playwright/index.cjs.map +10 -0
  78. package/dist/cjs/internal/playwright/types.cjs +47 -0
  79. package/dist/cjs/internal/playwright/types.cjs.map +10 -0
  80. package/dist/cjs/internal/protocol/codec.cjs +510 -0
  81. package/dist/cjs/internal/protocol/codec.cjs.map +10 -0
  82. package/dist/cjs/internal/protocol/framing.cjs +141 -0
  83. package/dist/cjs/internal/protocol/framing.cjs.map +10 -0
  84. package/dist/cjs/internal/protocol/index.cjs +110 -0
  85. package/dist/cjs/internal/protocol/index.cjs.map +10 -0
  86. package/dist/cjs/internal/protocol/marshalValue.cjs +518 -0
  87. package/dist/cjs/internal/protocol/marshalValue.cjs.map +10 -0
  88. package/dist/cjs/internal/protocol/serialization.cjs +109 -0
  89. package/dist/cjs/internal/protocol/serialization.cjs.map +10 -0
  90. package/dist/cjs/internal/protocol/types.cjs +181 -0
  91. package/dist/cjs/internal/protocol/types.cjs.map +10 -0
  92. package/dist/cjs/internal/runtime/index.cjs +1235 -0
  93. package/dist/cjs/internal/runtime/index.cjs.map +10 -0
  94. package/dist/cjs/internal/server/index.cjs +223 -0
  95. package/dist/cjs/internal/server/index.cjs.map +10 -0
  96. package/dist/cjs/internal/test-environment/index.cjs +1415 -0
  97. package/dist/cjs/internal/test-environment/index.cjs.map +10 -0
  98. package/dist/cjs/internal/timers/index.cjs +200 -0
  99. package/dist/cjs/internal/timers/index.cjs.map +10 -0
  100. package/dist/cjs/internal/transform/index.cjs +361 -0
  101. package/dist/cjs/internal/transform/index.cjs.map +10 -0
  102. package/dist/cjs/internal/typecheck/index.cjs +60 -0
  103. package/dist/cjs/internal/typecheck/index.cjs.map +10 -0
  104. package/dist/cjs/internal/typecheck/isolate-types.cjs +2614 -0
  105. package/dist/cjs/internal/typecheck/isolate-types.cjs.map +10 -0
  106. package/dist/cjs/internal/typecheck/typecheck.cjs +131 -0
  107. package/dist/cjs/internal/typecheck/typecheck.cjs.map +10 -0
  108. package/dist/cjs/modules/index.cjs +160 -0
  109. package/dist/cjs/modules/index.cjs.map +10 -0
  110. package/dist/cjs/package.json +5 -0
  111. package/dist/cjs/runtime/script-runtime.cjs +97 -0
  112. package/dist/cjs/runtime/script-runtime.cjs.map +10 -0
  113. package/dist/cjs/server/app-server.cjs +158 -0
  114. package/dist/cjs/server/app-server.cjs.map +10 -0
  115. package/dist/cjs/testing/integration-helpers.cjs +127 -0
  116. package/dist/cjs/testing/integration-helpers.cjs.map +10 -0
  117. package/dist/cjs/typecheck/index.cjs +96 -0
  118. package/dist/cjs/typecheck/index.cjs.map +10 -0
  119. package/dist/cjs/types.cjs +30 -0
  120. package/dist/cjs/types.cjs.map +9 -0
  121. package/dist/mjs/bridge/diagnostics.mjs +18 -0
  122. package/dist/mjs/bridge/diagnostics.mjs.map +10 -0
  123. package/dist/mjs/bridge/legacy-adapters.mjs +178 -0
  124. package/dist/mjs/bridge/legacy-adapters.mjs.map +10 -0
  125. package/dist/mjs/bridge/request-context.mjs +19 -0
  126. package/dist/mjs/bridge/request-context.mjs.map +10 -0
  127. package/dist/mjs/bridge/runtime-bindings.mjs +303 -0
  128. package/dist/mjs/bridge/runtime-bindings.mjs.map +10 -0
  129. package/dist/mjs/browser/browser-runtime.mjs +93 -0
  130. package/dist/mjs/browser/browser-runtime.mjs.map +10 -0
  131. package/dist/mjs/daemon.mjs +91 -0
  132. package/dist/mjs/daemon.mjs.map +10 -0
  133. package/dist/mjs/files/index.mjs +76 -0
  134. package/dist/mjs/files/index.mjs.map +10 -0
  135. package/dist/mjs/host/create-isolate-host.mjs +171 -0
  136. package/dist/mjs/host/create-isolate-host.mjs.map +10 -0
  137. package/dist/mjs/host/index.mjs +7 -0
  138. package/dist/mjs/host/index.mjs.map +10 -0
  139. package/dist/mjs/index.mjs +15 -0
  140. package/dist/mjs/index.mjs.map +10 -0
  141. package/dist/mjs/internal/client/connection.mjs +1872 -0
  142. package/dist/mjs/internal/client/connection.mjs.map +10 -0
  143. package/dist/mjs/internal/client/index.mjs +8 -0
  144. package/dist/mjs/internal/client/index.mjs.map +10 -0
  145. package/dist/mjs/internal/client/types.mjs +2 -0
  146. package/dist/mjs/internal/client/types.mjs.map +9 -0
  147. package/dist/mjs/internal/console/index.mjs +442 -0
  148. package/dist/mjs/internal/console/index.mjs.map +10 -0
  149. package/dist/mjs/internal/console/utils.mjs +30 -0
  150. package/dist/mjs/internal/console/utils.mjs.map +10 -0
  151. package/dist/mjs/internal/core/index.mjs +2681 -0
  152. package/dist/mjs/internal/core/index.mjs.map +10 -0
  153. package/dist/mjs/internal/crypto/index.mjs +406 -0
  154. package/dist/mjs/internal/crypto/index.mjs.map +10 -0
  155. package/dist/mjs/internal/daemon/callback-fs-handler.mjs +315 -0
  156. package/dist/mjs/internal/daemon/callback-fs-handler.mjs.map +10 -0
  157. package/dist/mjs/internal/daemon/connection.mjs +1931 -0
  158. package/dist/mjs/internal/daemon/connection.mjs.map +10 -0
  159. package/dist/mjs/internal/daemon/daemon.mjs +98 -0
  160. package/dist/mjs/internal/daemon/daemon.mjs.map +10 -0
  161. package/dist/mjs/internal/daemon/index.mjs +105 -0
  162. package/dist/mjs/internal/daemon/index.mjs.map +10 -0
  163. package/dist/mjs/internal/daemon/runtime-pool.mjs +66 -0
  164. package/dist/mjs/internal/daemon/runtime-pool.mjs.map +10 -0
  165. package/dist/mjs/internal/daemon/types.mjs +2 -0
  166. package/dist/mjs/internal/daemon/types.mjs.map +9 -0
  167. package/dist/mjs/internal/encoding/index.mjs +379 -0
  168. package/dist/mjs/internal/encoding/index.mjs.map +10 -0
  169. package/dist/mjs/internal/fetch/consistency/origins.mjs +558 -0
  170. package/dist/mjs/internal/fetch/consistency/origins.mjs.map +10 -0
  171. package/dist/mjs/internal/fetch/index.mjs +2580 -0
  172. package/dist/mjs/internal/fetch/index.mjs.map +10 -0
  173. package/dist/mjs/internal/fetch/stream-state.mjs +216 -0
  174. package/dist/mjs/internal/fetch/stream-state.mjs.map +10 -0
  175. package/dist/mjs/internal/fs/index.mjs +783 -0
  176. package/dist/mjs/internal/fs/index.mjs.map +10 -0
  177. package/dist/mjs/internal/fs/node-adapter.mjs +190 -0
  178. package/dist/mjs/internal/fs/node-adapter.mjs.map +10 -0
  179. package/dist/mjs/internal/module-loader/bundle.mjs +418 -0
  180. package/dist/mjs/internal/module-loader/bundle.mjs.map +10 -0
  181. package/dist/mjs/internal/module-loader/index.mjs +185 -0
  182. package/dist/mjs/internal/module-loader/index.mjs.map +10 -0
  183. package/dist/mjs/internal/module-loader/mappings.mjs +80 -0
  184. package/dist/mjs/internal/module-loader/mappings.mjs.map +10 -0
  185. package/dist/mjs/internal/module-loader/resolve.mjs +113 -0
  186. package/dist/mjs/internal/module-loader/resolve.mjs.map +10 -0
  187. package/dist/mjs/internal/module-loader/strip-types.mjs +172 -0
  188. package/dist/mjs/internal/module-loader/strip-types.mjs.map +10 -0
  189. package/dist/mjs/internal/path/index.mjs +463 -0
  190. package/dist/mjs/internal/path/index.mjs.map +10 -0
  191. package/dist/mjs/internal/playwright/client.mjs +13 -0
  192. package/dist/mjs/internal/playwright/client.mjs.map +10 -0
  193. package/dist/mjs/internal/playwright/handler.mjs +1378 -0
  194. package/dist/mjs/internal/playwright/handler.mjs.map +10 -0
  195. package/dist/mjs/internal/playwright/index.mjs +1234 -0
  196. package/dist/mjs/internal/playwright/index.mjs.map +10 -0
  197. package/dist/mjs/internal/playwright/types.mjs +7 -0
  198. package/dist/mjs/internal/playwright/types.mjs.map +10 -0
  199. package/dist/mjs/internal/protocol/codec.mjs +470 -0
  200. package/dist/mjs/internal/protocol/codec.mjs.map +10 -0
  201. package/dist/mjs/internal/protocol/framing.mjs +101 -0
  202. package/dist/mjs/internal/protocol/framing.mjs.map +10 -0
  203. package/dist/mjs/internal/protocol/index.mjs +98 -0
  204. package/dist/mjs/internal/protocol/index.mjs.map +10 -0
  205. package/dist/mjs/internal/protocol/marshalValue.mjs +494 -0
  206. package/dist/mjs/internal/protocol/marshalValue.mjs.map +10 -0
  207. package/dist/mjs/internal/protocol/serialization.mjs +69 -0
  208. package/dist/mjs/internal/protocol/serialization.mjs.map +10 -0
  209. package/dist/mjs/internal/protocol/types.mjs +141 -0
  210. package/dist/mjs/internal/protocol/types.mjs.map +10 -0
  211. package/dist/mjs/internal/runtime/index.mjs +1198 -0
  212. package/dist/mjs/internal/runtime/index.mjs.map +10 -0
  213. package/dist/mjs/internal/server/index.mjs +183 -0
  214. package/dist/mjs/internal/server/index.mjs.map +10 -0
  215. package/dist/mjs/internal/test-environment/index.mjs +1351 -0
  216. package/dist/mjs/internal/test-environment/index.mjs.map +10 -0
  217. package/dist/mjs/internal/timers/index.mjs +136 -0
  218. package/dist/mjs/internal/timers/index.mjs.map +10 -0
  219. package/dist/mjs/internal/transform/index.mjs +321 -0
  220. package/dist/mjs/internal/transform/index.mjs.map +10 -0
  221. package/dist/mjs/internal/typecheck/index.mjs +35 -0
  222. package/dist/mjs/internal/typecheck/index.mjs.map +10 -0
  223. package/dist/mjs/internal/typecheck/isolate-types.mjs +2574 -0
  224. package/dist/mjs/internal/typecheck/isolate-types.mjs.map +10 -0
  225. package/dist/mjs/internal/typecheck/typecheck.mjs +91 -0
  226. package/dist/mjs/internal/typecheck/typecheck.mjs.map +10 -0
  227. package/dist/mjs/modules/index.mjs +96 -0
  228. package/dist/mjs/modules/index.mjs.map +10 -0
  229. package/dist/mjs/package.json +5 -0
  230. package/dist/mjs/runtime/script-runtime.mjs +57 -0
  231. package/dist/mjs/runtime/script-runtime.mjs.map +10 -0
  232. package/dist/mjs/server/app-server.mjs +118 -0
  233. package/dist/mjs/server/app-server.mjs.map +10 -0
  234. package/dist/mjs/testing/integration-helpers.mjs +63 -0
  235. package/dist/mjs/testing/integration-helpers.mjs.map +10 -0
  236. package/dist/mjs/typecheck/index.mjs +56 -0
  237. package/dist/mjs/typecheck/index.mjs.map +10 -0
  238. package/dist/mjs/types.mjs +2 -0
  239. package/dist/mjs/types.mjs.map +9 -0
  240. package/dist/types/bridge/diagnostics.d.ts +12 -0
  241. package/dist/types/bridge/legacy-adapters.d.ts +14 -0
  242. package/dist/types/bridge/request-context.d.ts +10 -0
  243. package/dist/types/bridge/runtime-bindings.d.ts +14 -0
  244. package/dist/types/browser/browser-runtime.d.ts +3 -0
  245. package/dist/types/daemon.d.ts +2 -0
  246. package/dist/types/files/index.d.ts +5 -0
  247. package/dist/types/host/create-isolate-host.d.ts +2 -0
  248. package/dist/types/host/index.d.ts +1 -0
  249. package/dist/types/index.d.ts +5 -0
  250. package/dist/types/internal/client/connection.d.ts +9 -0
  251. package/dist/types/internal/client/index.d.ts +8 -0
  252. package/dist/types/internal/client/types.d.ts +198 -0
  253. package/dist/types/internal/console/index.d.ts +108 -0
  254. package/dist/types/internal/console/utils.d.ts +27 -0
  255. package/dist/types/internal/core/index.d.ts +119 -0
  256. package/dist/types/internal/crypto/index.d.ts +18 -0
  257. package/dist/types/internal/daemon/callback-fs-handler.d.ts +28 -0
  258. package/dist/types/internal/daemon/connection.d.ts +9 -0
  259. package/dist/types/internal/daemon/daemon.d.ts +2 -0
  260. package/dist/types/internal/daemon/index.d.ts +14 -0
  261. package/dist/types/internal/daemon/runtime-pool.d.ts +16 -0
  262. package/dist/types/internal/daemon/types.d.ts +211 -0
  263. package/dist/types/internal/encoding/index.d.ts +21 -0
  264. package/dist/types/internal/fetch/consistency/origins.d.ts +179 -0
  265. package/dist/types/internal/fetch/index.d.ts +93 -0
  266. package/dist/types/internal/fetch/stream-state.d.ts +65 -0
  267. package/dist/types/internal/fs/index.d.ts +70 -0
  268. package/dist/types/internal/fs/node-adapter.d.ts +24 -0
  269. package/dist/types/internal/module-loader/bundle.d.ts +33 -0
  270. package/dist/types/internal/module-loader/index.d.ts +30 -0
  271. package/dist/types/internal/module-loader/mappings.d.ts +47 -0
  272. package/dist/types/internal/module-loader/resolve.d.ts +26 -0
  273. package/dist/types/internal/module-loader/strip-types.d.ts +19 -0
  274. package/dist/types/internal/path/index.d.ts +23 -0
  275. package/dist/types/internal/playwright/client.d.ts +7 -0
  276. package/dist/types/internal/playwright/handler.d.ts +44 -0
  277. package/dist/types/internal/playwright/index.d.ts +14 -0
  278. package/dist/types/internal/playwright/types.d.ts +145 -0
  279. package/dist/types/internal/protocol/codec.d.ts +242 -0
  280. package/dist/types/internal/protocol/framing.d.ts +89 -0
  281. package/dist/types/internal/protocol/index.d.ts +10 -0
  282. package/dist/types/internal/protocol/marshalValue.d.ts +79 -0
  283. package/dist/types/internal/protocol/serialization.d.ts +23 -0
  284. package/dist/types/internal/protocol/types.d.ts +996 -0
  285. package/dist/types/internal/runtime/index.d.ts +200 -0
  286. package/dist/types/internal/server/index.d.ts +42 -0
  287. package/dist/types/internal/test-environment/index.d.ts +112 -0
  288. package/dist/types/internal/timers/index.d.ts +22 -0
  289. package/dist/types/internal/transform/index.d.ts +36 -0
  290. package/dist/types/internal/typecheck/index.d.ts +7 -0
  291. package/dist/types/internal/typecheck/isolate-types.d.ts +94 -0
  292. package/dist/types/internal/typecheck/typecheck.d.ts +148 -0
  293. package/dist/types/modules/index.d.ts +2 -0
  294. package/dist/types/runtime/script-runtime.d.ts +6 -0
  295. package/dist/types/server/app-server.d.ts +3 -0
  296. package/dist/types/testing/integration-helpers.d.ts +9 -0
  297. package/dist/types/typecheck/index.d.ts +8 -0
  298. package/dist/types/types.d.ts +233 -0
  299. package/package.json +82 -6
@@ -0,0 +1,783 @@
1
+ // src/internal/fs/index.ts
2
+ import ivm from "isolated-vm";
3
+ import { setupCore, clearAllInstanceState } from "../core/index.mjs";
4
+ import { createNodeFileSystemHandler } from "./node-adapter.mjs";
5
+ var instanceStateMap = new WeakMap;
6
+ var nextInstanceId = 1;
7
+ function getInstanceStateMapForContext(context) {
8
+ let map = instanceStateMap.get(context);
9
+ if (!map) {
10
+ map = new Map;
11
+ instanceStateMap.set(context, map);
12
+ }
13
+ return map;
14
+ }
15
+ function setupFileSystemDirectoryHandle(context, stateMap) {
16
+ const global = context.global;
17
+ global.setSync("__FileSystemDirectoryHandle_get_name", new ivm.Callback((instanceId) => {
18
+ const state = stateMap.get(instanceId);
19
+ return state?.name ?? "";
20
+ }));
21
+ global.setSync("__FileSystemDirectoryHandle_get_path", new ivm.Callback((instanceId) => {
22
+ const state = stateMap.get(instanceId);
23
+ return state?.path ?? "/";
24
+ }));
25
+ const getFileHandleRef = new ivm.Reference(async (instanceId, name, optionsJson) => {
26
+ const state = stateMap.get(instanceId);
27
+ if (!state) {
28
+ throw new Error("[NotFoundError]Directory handle not found");
29
+ }
30
+ const options = JSON.parse(optionsJson);
31
+ const childPath = state.path === "/" ? `/${name}` : `${state.path}/${name}`;
32
+ try {
33
+ await state.handler.getFileHandle(childPath, options);
34
+ } catch (err) {
35
+ if (err instanceof Error) {
36
+ throw new Error(err.message);
37
+ }
38
+ throw err;
39
+ }
40
+ const fileInstanceId = nextInstanceId++;
41
+ const fileState = {
42
+ instanceId: fileInstanceId,
43
+ path: childPath,
44
+ name,
45
+ handler: state.handler
46
+ };
47
+ stateMap.set(fileInstanceId, fileState);
48
+ return JSON.stringify({ instanceId: fileInstanceId });
49
+ });
50
+ global.setSync("__FileSystemDirectoryHandle_getFileHandle_ref", getFileHandleRef);
51
+ const getDirectoryHandleRef = new ivm.Reference(async (instanceId, name, optionsJson) => {
52
+ const state = stateMap.get(instanceId);
53
+ if (!state) {
54
+ throw new Error("[NotFoundError]Directory handle not found");
55
+ }
56
+ const options = JSON.parse(optionsJson);
57
+ const childPath = state.path === "/" ? `/${name}` : `${state.path}/${name}`;
58
+ try {
59
+ await state.handler.getDirectoryHandle(childPath, options);
60
+ } catch (err) {
61
+ if (err instanceof Error) {
62
+ throw new Error(err.message);
63
+ }
64
+ throw err;
65
+ }
66
+ const dirInstanceId = nextInstanceId++;
67
+ const dirState = {
68
+ instanceId: dirInstanceId,
69
+ path: childPath,
70
+ name,
71
+ handler: state.handler
72
+ };
73
+ stateMap.set(dirInstanceId, dirState);
74
+ return JSON.stringify({ instanceId: dirInstanceId });
75
+ });
76
+ global.setSync("__FileSystemDirectoryHandle_getDirectoryHandle_ref", getDirectoryHandleRef);
77
+ const removeEntryRef = new ivm.Reference(async (instanceId, name, optionsJson) => {
78
+ const state = stateMap.get(instanceId);
79
+ if (!state) {
80
+ throw new Error("[NotFoundError]Directory handle not found");
81
+ }
82
+ const options = JSON.parse(optionsJson);
83
+ const childPath = state.path === "/" ? `/${name}` : `${state.path}/${name}`;
84
+ try {
85
+ await state.handler.removeEntry(childPath, options);
86
+ } catch (err) {
87
+ if (err instanceof Error) {
88
+ throw new Error(err.message);
89
+ }
90
+ throw err;
91
+ }
92
+ });
93
+ global.setSync("__FileSystemDirectoryHandle_removeEntry_ref", removeEntryRef);
94
+ const readDirectoryRef = new ivm.Reference(async (instanceId) => {
95
+ const state = stateMap.get(instanceId);
96
+ if (!state) {
97
+ throw new Error("[NotFoundError]Directory handle not found");
98
+ }
99
+ try {
100
+ const entries = await state.handler.readDirectory(state.path);
101
+ const result = entries.map((entry) => {
102
+ const entryId = nextInstanceId++;
103
+ const entryPath = state.path === "/" ? `/${entry.name}` : `${state.path}/${entry.name}`;
104
+ if (entry.kind === "file") {
105
+ const fileState = {
106
+ instanceId: entryId,
107
+ path: entryPath,
108
+ name: entry.name,
109
+ handler: state.handler
110
+ };
111
+ stateMap.set(entryId, fileState);
112
+ } else {
113
+ const dirState = {
114
+ instanceId: entryId,
115
+ path: entryPath,
116
+ name: entry.name,
117
+ handler: state.handler
118
+ };
119
+ stateMap.set(entryId, dirState);
120
+ }
121
+ return {
122
+ name: entry.name,
123
+ kind: entry.kind,
124
+ instanceId: entryId
125
+ };
126
+ });
127
+ return JSON.stringify(result);
128
+ } catch (err) {
129
+ if (err instanceof Error) {
130
+ throw new Error(err.message);
131
+ }
132
+ throw err;
133
+ }
134
+ });
135
+ global.setSync("__FileSystemDirectoryHandle_readDirectory_ref", readDirectoryRef);
136
+ global.setSync("__FileSystemDirectoryHandle_isSameEntry", new ivm.Callback((id1, id2) => {
137
+ const state1 = stateMap.get(id1);
138
+ const state2 = stateMap.get(id2);
139
+ if (!state1 || !state2)
140
+ return false;
141
+ return state1.path === state2.path;
142
+ }));
143
+ const resolveRef = new ivm.Reference(async (instanceId, descendantId) => {
144
+ const state = stateMap.get(instanceId);
145
+ const descendantState = stateMap.get(descendantId);
146
+ if (!state || !descendantState) {
147
+ return "null";
148
+ }
149
+ const basePath = state.path === "/" ? "" : state.path;
150
+ if (!descendantState.path.startsWith(basePath + "/") && descendantState.path !== state.path) {
151
+ return "null";
152
+ }
153
+ const relativePath = descendantState.path.slice(basePath.length);
154
+ const components = relativePath.split("/").filter((c) => c.length > 0);
155
+ return JSON.stringify(components);
156
+ });
157
+ global.setSync("__FileSystemDirectoryHandle_resolve_ref", resolveRef);
158
+ const directoryHandleCode = `
159
+ (function() {
160
+ const _directoryHandleInstanceIds = new WeakMap();
161
+
162
+ function __decodeError(err) {
163
+ if (!(err instanceof Error)) return err;
164
+ const match = err.message.match(/^\\[(TypeError|RangeError|NotFoundError|TypeMismatchError|InvalidModificationError|Error)\\](.*)$/);
165
+ if (match) {
166
+ if (['NotFoundError', 'TypeMismatchError', 'InvalidModificationError'].includes(match[1])) {
167
+ return new DOMException(match[2], match[1]);
168
+ }
169
+ const ErrorType = globalThis[match[1]] || Error;
170
+ return new ErrorType(match[2]);
171
+ }
172
+ return err;
173
+ }
174
+
175
+ class FileSystemDirectoryHandle {
176
+ constructor(path, name) {
177
+ // Internal construction from instance ID
178
+ if (typeof path === 'number' && name === null) {
179
+ _directoryHandleInstanceIds.set(this, path);
180
+ return;
181
+ }
182
+ const instanceId = __FileSystemDirectoryHandle_construct(path, name);
183
+ _directoryHandleInstanceIds.set(this, instanceId);
184
+ }
185
+
186
+ static _fromInstanceId(instanceId) {
187
+ return new FileSystemDirectoryHandle(instanceId, null);
188
+ }
189
+
190
+ _getInstanceId() {
191
+ return _directoryHandleInstanceIds.get(this);
192
+ }
193
+
194
+ get kind() {
195
+ return 'directory';
196
+ }
197
+
198
+ get name() {
199
+ return __FileSystemDirectoryHandle_get_name(this._getInstanceId());
200
+ }
201
+
202
+ async getFileHandle(name, options = {}) {
203
+ try {
204
+ const resultJson = await __FileSystemDirectoryHandle_getFileHandle_ref.apply(
205
+ undefined,
206
+ [this._getInstanceId(), name, JSON.stringify(options)],
207
+ { result: { promise: true, copy: true } }
208
+ );
209
+ const result = JSON.parse(resultJson);
210
+ return FileSystemFileHandle._fromInstanceId(result.instanceId);
211
+ } catch (err) {
212
+ throw __decodeError(err);
213
+ }
214
+ }
215
+
216
+ async getDirectoryHandle(name, options = {}) {
217
+ try {
218
+ const resultJson = await __FileSystemDirectoryHandle_getDirectoryHandle_ref.apply(
219
+ undefined,
220
+ [this._getInstanceId(), name, JSON.stringify(options)],
221
+ { result: { promise: true, copy: true } }
222
+ );
223
+ const result = JSON.parse(resultJson);
224
+ return FileSystemDirectoryHandle._fromInstanceId(result.instanceId);
225
+ } catch (err) {
226
+ throw __decodeError(err);
227
+ }
228
+ }
229
+
230
+ async removeEntry(name, options = {}) {
231
+ try {
232
+ await __FileSystemDirectoryHandle_removeEntry_ref.apply(
233
+ undefined,
234
+ [this._getInstanceId(), name, JSON.stringify(options)],
235
+ { result: { promise: true, copy: true } }
236
+ );
237
+ } catch (err) {
238
+ throw __decodeError(err);
239
+ }
240
+ }
241
+
242
+ async *entries() {
243
+ let entriesJson;
244
+ try {
245
+ entriesJson = await __FileSystemDirectoryHandle_readDirectory_ref.apply(
246
+ undefined,
247
+ [this._getInstanceId()],
248
+ { result: { promise: true, copy: true } }
249
+ );
250
+ } catch (err) {
251
+ throw __decodeError(err);
252
+ }
253
+ const entries = JSON.parse(entriesJson);
254
+ for (const entry of entries) {
255
+ if (entry.kind === 'file') {
256
+ yield [entry.name, FileSystemFileHandle._fromInstanceId(entry.instanceId)];
257
+ } else {
258
+ yield [entry.name, FileSystemDirectoryHandle._fromInstanceId(entry.instanceId)];
259
+ }
260
+ }
261
+ }
262
+
263
+ async *keys() {
264
+ for await (const [name] of this.entries()) {
265
+ yield name;
266
+ }
267
+ }
268
+
269
+ async *values() {
270
+ for await (const [, handle] of this.entries()) {
271
+ yield handle;
272
+ }
273
+ }
274
+
275
+ [Symbol.asyncIterator]() {
276
+ return this.entries();
277
+ }
278
+
279
+ async isSameEntry(other) {
280
+ if (!(other instanceof FileSystemDirectoryHandle)) {
281
+ return false;
282
+ }
283
+ return __FileSystemDirectoryHandle_isSameEntry(
284
+ this._getInstanceId(),
285
+ other._getInstanceId()
286
+ );
287
+ }
288
+
289
+ async resolve(possibleDescendant) {
290
+ try {
291
+ const resultJson = await __FileSystemDirectoryHandle_resolve_ref.apply(
292
+ undefined,
293
+ [this._getInstanceId(), possibleDescendant._getInstanceId()],
294
+ { result: { promise: true, copy: true } }
295
+ );
296
+ return resultJson === 'null' ? null : JSON.parse(resultJson);
297
+ } catch (err) {
298
+ throw __decodeError(err);
299
+ }
300
+ }
301
+ }
302
+
303
+ globalThis.FileSystemDirectoryHandle = FileSystemDirectoryHandle;
304
+ })();
305
+ `;
306
+ context.evalSync(directoryHandleCode);
307
+ }
308
+ function setupFileSystemFileHandle(context, stateMap) {
309
+ const global = context.global;
310
+ global.setSync("__FileSystemFileHandle_get_name", new ivm.Callback((instanceId) => {
311
+ const state = stateMap.get(instanceId);
312
+ return state?.name ?? "";
313
+ }));
314
+ global.setSync("__FileSystemFileHandle_get_path", new ivm.Callback((instanceId) => {
315
+ const state = stateMap.get(instanceId);
316
+ return state?.path ?? "";
317
+ }));
318
+ const getFileRef = new ivm.Reference(async (instanceId) => {
319
+ const state = stateMap.get(instanceId);
320
+ if (!state) {
321
+ throw new Error("[NotFoundError]File handle not found");
322
+ }
323
+ try {
324
+ const fileData = await state.handler.readFile(state.path);
325
+ return JSON.stringify({
326
+ name: state.name,
327
+ data: Array.from(fileData.data),
328
+ size: fileData.size,
329
+ lastModified: fileData.lastModified,
330
+ type: fileData.type
331
+ });
332
+ } catch (err) {
333
+ if (err instanceof Error) {
334
+ throw new Error(err.message);
335
+ }
336
+ throw err;
337
+ }
338
+ });
339
+ global.setSync("__FileSystemFileHandle_getFile_ref", getFileRef);
340
+ const createWritableRef = new ivm.Reference(async (instanceId, _optionsJson) => {
341
+ const state = stateMap.get(instanceId);
342
+ if (!state) {
343
+ throw new Error("[NotFoundError]File handle not found");
344
+ }
345
+ const streamInstanceId = nextInstanceId++;
346
+ const streamState = {
347
+ instanceId: streamInstanceId,
348
+ filePath: state.path,
349
+ position: 0,
350
+ buffer: [],
351
+ closed: false,
352
+ handler: state.handler
353
+ };
354
+ stateMap.set(streamInstanceId, streamState);
355
+ return streamInstanceId;
356
+ });
357
+ global.setSync("__FileSystemFileHandle_createWritable_ref", createWritableRef);
358
+ global.setSync("__FileSystemFileHandle_isSameEntry", new ivm.Callback((id1, id2) => {
359
+ const state1 = stateMap.get(id1);
360
+ const state2 = stateMap.get(id2);
361
+ if (!state1 || !state2)
362
+ return false;
363
+ return state1.path === state2.path;
364
+ }));
365
+ const fileHandleCode = `
366
+ (function() {
367
+ const _fileHandleInstanceIds = new WeakMap();
368
+
369
+ function __decodeError(err) {
370
+ if (!(err instanceof Error)) return err;
371
+ const match = err.message.match(/^\\[(TypeError|RangeError|NotFoundError|TypeMismatchError|InvalidModificationError|Error)\\](.*)$/);
372
+ if (match) {
373
+ if (['NotFoundError', 'TypeMismatchError', 'InvalidModificationError'].includes(match[1])) {
374
+ return new DOMException(match[2], match[1]);
375
+ }
376
+ const ErrorType = globalThis[match[1]] || Error;
377
+ return new ErrorType(match[2]);
378
+ }
379
+ return err;
380
+ }
381
+
382
+ class FileSystemFileHandle {
383
+ constructor(path, name) {
384
+ // Internal construction from instance ID
385
+ if (typeof path === 'number' && name === null) {
386
+ _fileHandleInstanceIds.set(this, path);
387
+ return;
388
+ }
389
+ const instanceId = __FileSystemFileHandle_construct(path, name);
390
+ _fileHandleInstanceIds.set(this, instanceId);
391
+ }
392
+
393
+ static _fromInstanceId(instanceId) {
394
+ return new FileSystemFileHandle(instanceId, null);
395
+ }
396
+
397
+ _getInstanceId() {
398
+ return _fileHandleInstanceIds.get(this);
399
+ }
400
+
401
+ get kind() {
402
+ return 'file';
403
+ }
404
+
405
+ get name() {
406
+ return __FileSystemFileHandle_get_name(this._getInstanceId());
407
+ }
408
+
409
+ async getFile() {
410
+ try {
411
+ const metadataJson = await __FileSystemFileHandle_getFile_ref.apply(
412
+ undefined,
413
+ [this._getInstanceId()],
414
+ { result: { promise: true, copy: true } }
415
+ );
416
+ const metadata = JSON.parse(metadataJson);
417
+ // Create File object from metadata and content
418
+ const content = new Uint8Array(metadata.data);
419
+ return new File([content], metadata.name, {
420
+ type: metadata.type,
421
+ lastModified: metadata.lastModified
422
+ });
423
+ } catch (err) {
424
+ throw __decodeError(err);
425
+ }
426
+ }
427
+
428
+ async createWritable(options = {}) {
429
+ try {
430
+ const streamId = await __FileSystemFileHandle_createWritable_ref.apply(
431
+ undefined,
432
+ [this._getInstanceId(), JSON.stringify(options)],
433
+ { result: { promise: true, copy: true } }
434
+ );
435
+ return FileSystemWritableFileStream._fromInstanceId(streamId);
436
+ } catch (err) {
437
+ throw __decodeError(err);
438
+ }
439
+ }
440
+
441
+ async isSameEntry(other) {
442
+ if (!(other instanceof FileSystemFileHandle)) {
443
+ return false;
444
+ }
445
+ return __FileSystemFileHandle_isSameEntry(
446
+ this._getInstanceId(),
447
+ other._getInstanceId()
448
+ );
449
+ }
450
+ }
451
+
452
+ globalThis.FileSystemFileHandle = FileSystemFileHandle;
453
+ })();
454
+ `;
455
+ context.evalSync(fileHandleCode);
456
+ }
457
+ function setupFileSystemWritableFileStream(context, stateMap) {
458
+ const global = context.global;
459
+ const writeRef = new ivm.Reference(async (instanceId, bytesJson, position) => {
460
+ const state = stateMap.get(instanceId);
461
+ if (!state) {
462
+ throw new Error("[InvalidStateError]Stream not found");
463
+ }
464
+ if (state.closed) {
465
+ throw new Error("[InvalidStateError]Stream is closed");
466
+ }
467
+ const bytes = JSON.parse(bytesJson);
468
+ const data = new Uint8Array(bytes);
469
+ if (position !== null) {
470
+ state.position = position;
471
+ }
472
+ try {
473
+ await state.handler.writeFile(state.filePath, data, state.position);
474
+ state.position += data.length;
475
+ } catch (err) {
476
+ if (err instanceof Error) {
477
+ throw new Error(err.message);
478
+ }
479
+ throw err;
480
+ }
481
+ });
482
+ global.setSync("__FileSystemWritableFileStream_write_ref", writeRef);
483
+ global.setSync("__FileSystemWritableFileStream_seek", new ivm.Callback((instanceId, position) => {
484
+ const state = stateMap.get(instanceId);
485
+ if (!state) {
486
+ throw new Error("[InvalidStateError]Stream not found");
487
+ }
488
+ if (state.closed) {
489
+ throw new Error("[InvalidStateError]Stream is closed");
490
+ }
491
+ state.position = position;
492
+ }));
493
+ const truncateRef = new ivm.Reference(async (instanceId, size) => {
494
+ const state = stateMap.get(instanceId);
495
+ if (!state) {
496
+ throw new Error("[InvalidStateError]Stream not found");
497
+ }
498
+ if (state.closed) {
499
+ throw new Error("[InvalidStateError]Stream is closed");
500
+ }
501
+ try {
502
+ await state.handler.truncateFile(state.filePath, size);
503
+ if (state.position > size) {
504
+ state.position = size;
505
+ }
506
+ } catch (err) {
507
+ if (err instanceof Error) {
508
+ throw new Error(err.message);
509
+ }
510
+ throw err;
511
+ }
512
+ });
513
+ global.setSync("__FileSystemWritableFileStream_truncate_ref", truncateRef);
514
+ const closeRef = new ivm.Reference(async (instanceId) => {
515
+ const state = stateMap.get(instanceId);
516
+ if (!state) {
517
+ throw new Error("[InvalidStateError]Stream not found");
518
+ }
519
+ if (state.closed) {
520
+ throw new Error("[InvalidStateError]Stream is already closed");
521
+ }
522
+ state.closed = true;
523
+ });
524
+ global.setSync("__FileSystemWritableFileStream_close_ref", closeRef);
525
+ const abortRef = new ivm.Reference(async (instanceId, _reason) => {
526
+ const state = stateMap.get(instanceId);
527
+ if (!state) {
528
+ throw new Error("[InvalidStateError]Stream not found");
529
+ }
530
+ state.closed = true;
531
+ state.buffer = [];
532
+ });
533
+ global.setSync("__FileSystemWritableFileStream_abort_ref", abortRef);
534
+ global.setSync("__FileSystemWritableFileStream_get_locked", new ivm.Callback((instanceId) => {
535
+ const state = stateMap.get(instanceId);
536
+ return state ? !state.closed : false;
537
+ }));
538
+ const writableStreamCode = `
539
+ (function() {
540
+ const _writableStreamInstanceIds = new WeakMap();
541
+
542
+ function __decodeError(err) {
543
+ if (!(err instanceof Error)) return err;
544
+ const match = err.message.match(/^\\[(TypeError|RangeError|InvalidStateError|NotFoundError|Error)\\](.*)$/);
545
+ if (match) {
546
+ if (['InvalidStateError', 'NotFoundError'].includes(match[1])) {
547
+ return new DOMException(match[2], match[1]);
548
+ }
549
+ const ErrorType = globalThis[match[1]] || Error;
550
+ return new ErrorType(match[2]);
551
+ }
552
+ return err;
553
+ }
554
+
555
+ class FileSystemWritableFileStream {
556
+ constructor(instanceId) {
557
+ _writableStreamInstanceIds.set(this, instanceId);
558
+ }
559
+
560
+ static _fromInstanceId(instanceId) {
561
+ return new FileSystemWritableFileStream(instanceId);
562
+ }
563
+
564
+ _getInstanceId() {
565
+ return _writableStreamInstanceIds.get(this);
566
+ }
567
+
568
+ async write(data) {
569
+ try {
570
+ // Handle different data types
571
+ let writeData;
572
+ let position = null;
573
+ let type = 'write';
574
+
575
+ if (data && typeof data === 'object' && !ArrayBuffer.isView(data) &&
576
+ !(data instanceof Blob) && !(data instanceof ArrayBuffer) &&
577
+ !Array.isArray(data) && typeof data.type === 'string') {
578
+ // WriteParams object: { type, data, position, size }
579
+ type = data.type || 'write';
580
+ if (type === 'seek') {
581
+ return this.seek(data.position);
582
+ }
583
+ if (type === 'truncate') {
584
+ return this.truncate(data.size);
585
+ }
586
+ writeData = data.data;
587
+ position = data.position ?? null;
588
+ } else {
589
+ writeData = data;
590
+ }
591
+
592
+ // Convert data to bytes array for transfer
593
+ let bytes;
594
+ if (typeof writeData === 'string') {
595
+ bytes = Array.from(new TextEncoder().encode(writeData));
596
+ } else if (writeData instanceof Blob) {
597
+ const buffer = await writeData.arrayBuffer();
598
+ bytes = Array.from(new Uint8Array(buffer));
599
+ } else if (writeData instanceof ArrayBuffer) {
600
+ bytes = Array.from(new Uint8Array(writeData));
601
+ } else if (ArrayBuffer.isView(writeData)) {
602
+ bytes = Array.from(new Uint8Array(writeData.buffer, writeData.byteOffset, writeData.byteLength));
603
+ } else if (Array.isArray(writeData)) {
604
+ bytes = writeData;
605
+ } else {
606
+ throw new TypeError('Invalid data type for write');
607
+ }
608
+
609
+ await __FileSystemWritableFileStream_write_ref.apply(
610
+ undefined,
611
+ [this._getInstanceId(), JSON.stringify(bytes), position],
612
+ { result: { promise: true, copy: true } }
613
+ );
614
+ } catch (err) {
615
+ throw __decodeError(err);
616
+ }
617
+ }
618
+
619
+ seek(position) {
620
+ try {
621
+ __FileSystemWritableFileStream_seek(this._getInstanceId(), position);
622
+ } catch (err) {
623
+ throw __decodeError(err);
624
+ }
625
+ }
626
+
627
+ async truncate(size) {
628
+ try {
629
+ await __FileSystemWritableFileStream_truncate_ref.apply(
630
+ undefined,
631
+ [this._getInstanceId(), size],
632
+ { result: { promise: true, copy: true } }
633
+ );
634
+ } catch (err) {
635
+ throw __decodeError(err);
636
+ }
637
+ }
638
+
639
+ async close() {
640
+ try {
641
+ await __FileSystemWritableFileStream_close_ref.apply(
642
+ undefined,
643
+ [this._getInstanceId()],
644
+ { result: { promise: true, copy: true } }
645
+ );
646
+ } catch (err) {
647
+ throw __decodeError(err);
648
+ }
649
+ }
650
+
651
+ async abort(reason) {
652
+ try {
653
+ await __FileSystemWritableFileStream_abort_ref.apply(
654
+ undefined,
655
+ [this._getInstanceId(), reason ? String(reason) : null],
656
+ { result: { promise: true, copy: true } }
657
+ );
658
+ } catch (err) {
659
+ throw __decodeError(err);
660
+ }
661
+ }
662
+
663
+ get locked() {
664
+ return __FileSystemWritableFileStream_get_locked(this._getInstanceId());
665
+ }
666
+
667
+ getWriter() {
668
+ const stream = this;
669
+ let released = false;
670
+ let closedResolve;
671
+ let closedReject;
672
+ const closedPromise = new Promise((resolve, reject) => {
673
+ closedResolve = resolve;
674
+ closedReject = reject;
675
+ });
676
+
677
+ return {
678
+ get closed() {
679
+ return closedPromise;
680
+ },
681
+ get desiredSize() {
682
+ return 1;
683
+ },
684
+ get ready() {
685
+ return Promise.resolve();
686
+ },
687
+ async write(chunk) {
688
+ if (released) {
689
+ return Promise.reject(new TypeError('Writer has been released'));
690
+ }
691
+ try {
692
+ await stream.write(chunk);
693
+ return Promise.resolve();
694
+ } catch (err) {
695
+ return Promise.reject(err);
696
+ }
697
+ },
698
+ async close() {
699
+ if (released) {
700
+ return Promise.reject(new TypeError('Writer has been released'));
701
+ }
702
+ try {
703
+ await stream.close();
704
+ closedResolve();
705
+ return Promise.resolve();
706
+ } catch (err) {
707
+ closedReject(err);
708
+ return Promise.reject(err);
709
+ }
710
+ },
711
+ async abort(reason) {
712
+ if (released) {
713
+ return Promise.reject(new TypeError('Writer has been released'));
714
+ }
715
+ try {
716
+ await stream.abort(reason);
717
+ closedReject(reason || new Error('Stream aborted'));
718
+ return Promise.resolve();
719
+ } catch (err) {
720
+ return Promise.reject(err);
721
+ }
722
+ },
723
+ releaseLock() {
724
+ released = true;
725
+ }
726
+ };
727
+ }
728
+ }
729
+
730
+ globalThis.FileSystemWritableFileStream = FileSystemWritableFileStream;
731
+ })();
732
+ `;
733
+ context.evalSync(writableStreamCode);
734
+ }
735
+ function setupGetDirectoryGlobal(context, stateMap, options) {
736
+ const global = context.global;
737
+ const getDirectoryRef = new ivm.Reference(async (path) => {
738
+ const handler = await options.getDirectory(path);
739
+ const instanceId = nextInstanceId++;
740
+ const state = {
741
+ instanceId,
742
+ path: "/",
743
+ name: path.split("/").filter(Boolean).pop() || "",
744
+ handler
745
+ };
746
+ stateMap.set(instanceId, state);
747
+ return instanceId;
748
+ });
749
+ global.setSync("__getDirectory_ref", getDirectoryRef);
750
+ const getDirectoryCode = `
751
+ (function() {
752
+ globalThis.getDirectory = async function(path) {
753
+ const instanceId = await __getDirectory_ref.apply(
754
+ undefined,
755
+ [path],
756
+ { result: { promise: true, copy: true } }
757
+ );
758
+ return FileSystemDirectoryHandle._fromInstanceId(instanceId);
759
+ };
760
+ })();
761
+ `;
762
+ context.evalSync(getDirectoryCode);
763
+ }
764
+ async function setupFs(context, options) {
765
+ await setupCore(context);
766
+ const stateMap = getInstanceStateMapForContext(context);
767
+ setupFileSystemDirectoryHandle(context, stateMap);
768
+ setupFileSystemFileHandle(context, stateMap);
769
+ setupFileSystemWritableFileStream(context, stateMap);
770
+ setupGetDirectoryGlobal(context, stateMap, options);
771
+ return {
772
+ dispose() {
773
+ stateMap.clear();
774
+ }
775
+ };
776
+ }
777
+ export {
778
+ setupFs,
779
+ createNodeFileSystemHandler,
780
+ clearAllInstanceState
781
+ };
782
+
783
+ //# debugId=88FA3A7BCFD718C864756E2164756E21