@ricsam/isolate-fs 0.1.1 → 0.1.3

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