@opfs-vfs/effect 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,1031 @@
1
+ import { d as e, f as t, l as n, t as r } from "./volume-BIqpe5Vt.js";
2
+ import { EncryptionError as i, MountError as a, RemoteErrorDetails as o, SubscriptionError as s, VolumeError as c, mountError as l, remoteDetails as u } from "./errors.js";
3
+ import { ByteSize as d, Cause as f, Context as p, Deferred as m, Effect as h, Exit as g, FileSystem as _, Layer as v, Option as y, PlatformError as b, Queue as x, Schema as S, Scope as C, Semaphore as w, Stream as T } from "effect";
4
+ import { OpenFlags as E } from "@opfs-vfs/opfs-vfs";
5
+ import { VfsCommandError as D } from "@opfs-vfs/opfs-vfs/worker";
6
+ import { subscribe as ee } from "@opfs-vfs/plugin-subscriptions/client";
7
+ //#region src/subscriptions-internal.ts
8
+ var te = 16, ne = Symbol("retry subscription on captured generation"), O = /* @__PURE__ */ new Set([
9
+ "SUBSCRIPTION_OVERFLOW",
10
+ "SUBSCRIPTION_INTERRUPTED",
11
+ "SUBSCRIPTION_CALLBACK_FAILED",
12
+ "SUBSCRIPTION_RESYNC_REQUIRED",
13
+ "SUBSCRIPTION_RETIREMENT_UNKNOWN",
14
+ "SUBSCRIPTION_SETUP_FAILED",
15
+ "EINVAL",
16
+ "EBADF"
17
+ ]), k = (e, t, n) => {
18
+ let r, i;
19
+ try {
20
+ S.is(s)(e) ? r = e : S.is(c)(e) && (i = e);
21
+ } catch {}
22
+ let a = r?.details ?? u(e), o = r?.code ?? a.code, l = r?.sourceCode ?? (o === "SUBSCRIPTION_SETUP_FAILED" ? void 0 : o), d = o && O.has(o) ? o : l && O.has(l) ? l : "SUBSCRIPTION_SETUP_FAILED";
23
+ return new s({
24
+ code: d,
25
+ fileName: t,
26
+ path: n,
27
+ ...l && l !== d ? { sourceCode: l } : {},
28
+ details: a,
29
+ ...i === void 0 ? {} : { cause: i }
30
+ });
31
+ }, re = (e, t, n) => {
32
+ let r;
33
+ try {
34
+ S.is(s)(e) && (r = e);
35
+ } catch {}
36
+ let i = r?.details ?? u(e);
37
+ return new s({
38
+ code: "SUBSCRIPTION_RETIREMENT_UNKNOWN",
39
+ fileName: t,
40
+ path: n,
41
+ ...r?.sourceCode ?? r?.code ?? i.code ? { sourceCode: r?.sourceCode ?? r?.code ?? i.code } : {},
42
+ details: i
43
+ });
44
+ }, ie = (e, t, n) => e.status === "released" ? e : {
45
+ status: "unknown",
46
+ error: k(e.error, t, n)
47
+ }, ae = (e) => new c({
48
+ kind: "unsupported",
49
+ fileName: e,
50
+ operation: "subscribe",
51
+ code: "ENOTSUP",
52
+ outcome: "not-applied",
53
+ details: null
54
+ }), oe = (e, t) => {
55
+ for (let [n, r] of e.subscriptionSetups) r.generation !== t && e.subscriptionSetups.delete(n);
56
+ for (let n of e.subscriptionUnknown.keys()) n !== t && e.subscriptionUnknown.delete(n);
57
+ }, A = (e, t, n) => {
58
+ let r = e.terminal();
59
+ if (r) return k(r, e.fileName, n);
60
+ let i = e.currentGeneration();
61
+ if (e.isClosed() || i !== t) return k(Object.assign(/* @__PURE__ */ Error("Owner changed before subscription registration"), { code: "VFS_ATTACHMENT_LOST" }), e.fileName, n);
62
+ }, se = (e, t, n, r, i) => h.suspend(() => {
63
+ let a = A(e, t, i);
64
+ if (a) return h.fail(a);
65
+ oe(e, t);
66
+ let o = e.subscriptionUnknown.get(t);
67
+ if (o !== void 0) return h.fail(re(o, e.fileName, i));
68
+ let c = [...e.subscriptionSetups.values()].filter((e) => e.generation === t && e.state === "retiring").map((e) => e.closed);
69
+ if (!c.length) return h.void;
70
+ let l = r.currentTimeMillisUnsafe(), d = () => new s({
71
+ code: "SUBSCRIPTION_SETUP_FAILED",
72
+ fileName: e.fileName,
73
+ path: i,
74
+ sourceCode: "VFS_SUBSCRIPTION_RETIREMENT_TIMEOUT",
75
+ details: u(Object.assign(/* @__PURE__ */ Error("Subscription retirement wait timed out"), { code: "VFS_SUBSCRIPTION_RETIREMENT_TIMEOUT" }))
76
+ }), f = h.tryPromise({
77
+ try: () => Promise.all(c).then(() => void 0),
78
+ catch: (t) => k(t, e.fileName, i)
79
+ });
80
+ return h.raceFirst(f, h.sleep(Math.max(0, n.remaining)).pipe(h.andThen(h.fail(d())))).pipe(h.onExit(() => h.sync(() => {
81
+ let e = Math.max(0, r.currentTimeMillisUnsafe() - l);
82
+ n.remaining = Math.max(0, n.remaining - e);
83
+ })), h.andThen(se(e, t, n, r, i)));
84
+ }), j = (e, t, n) => h.uninterruptibleMask((r) => h.gen(function* () {
85
+ let i = yield* h.scope;
86
+ if (i.state._tag === "Closed") return yield* h.fail(k(Object.assign(/* @__PURE__ */ Error("Subscription scope is closed"), { code: "EBADF" }), e.fileName, ""));
87
+ let a = yield* C.fork(i, "sequential"), o = typeof t?.path == "string" ? t.path : "", c = new AbortController(), l = !1, d = !1, f, p, _, v, y, b = yield* x.bounded(te), S = m.makeUnsafe(), w = (t) => {
88
+ f || (f = k(t, e.fileName, o), m.doneUnsafe(S, h.succeed(f)), x.shutdownUnsafe(b));
89
+ }, E, D, O, j = h.uninterruptibleMask(() => h.gen(function* () {
90
+ if (l = !0, c.abort(), _ && (_.state = "retiring"), y) {
91
+ let e = yield* h.tryPromise({
92
+ try: () => y,
93
+ catch: (e) => e
94
+ });
95
+ e.ok && (v = e.handle, _ && (_.state = "retiring"), v.unsubscribe());
96
+ }
97
+ x.shutdownUnsafe(b), m.doneUnsafe(S, h.succeed(void 0));
98
+ }).pipe(h.ensuring(h.sync(() => e.files.delete(O))), h.orDie));
99
+ return E = yield* h.cached(j), D = yield* h.cached(h.uninterruptible(C.close(a, g.void).pipe(h.andThen(E)))), O = () => D, e.files.add(O), yield* C.addFinalizer(a, E), a.state._tag === "Closed" || e.isClosed() ? (yield* O(), yield* h.fail(k(Object.assign(/* @__PURE__ */ Error("Subscription scope is closed"), { code: "EBADF" }), e.fileName, ""))) : yield* h.gen(function* () {
100
+ if (!e.subscriptionsAvailable) return yield* h.fail(k(ae(e.fileName), e.fileName, o));
101
+ if (typeof o != "string" || !o.startsWith("/")) return yield* h.fail(k(Object.assign(/* @__PURE__ */ Error("path must be absolute"), { code: "EINVAL" }), e.fileName, o));
102
+ if (e.isClosed()) return yield* h.fail(k(e.terminal(), e.fileName, o));
103
+ let i = yield* h.clockWith((e) => h.succeed(e)), a = n?.budget ?? { remaining: e.readinessTimeout }, g = n?.generation;
104
+ g === void 0 && (g = yield* r(e.awaitReady(a, "subscribe").pipe(h.mapError((t) => k(t, e.fileName, o)))));
105
+ let C = A(e, g, o);
106
+ if (C) return yield* h.fail(C);
107
+ if (oe(e, g), yield* r(se(e, g, a, i, o)), (yield* r(e.awaitReady(a, "subscribe").pipe(h.mapError((t) => k(t, e.fileName, o))))) !== g) return yield* h.fail(A(e, g, o) ?? k(Object.assign(/* @__PURE__ */ Error("Owner changed during subscription setup"), { code: "VFS_ATTACHMENT_LOST" }), e.fileName, o));
108
+ let E = e.backend;
109
+ for (;;) {
110
+ let n = m.makeUnsafe(), d = m.makeUnsafe(), S = {
111
+ awaitSetupRetirements: async (t) => {
112
+ let r = A(e, g, o);
113
+ if (r) throw r;
114
+ let s = i.currentTimeMillisUnsafe();
115
+ m.doneUnsafe(n, h.succeed(s));
116
+ try {
117
+ await t(c.signal);
118
+ } finally {
119
+ let e = i.currentTimeMillisUnsafe();
120
+ a.remaining = Math.max(0, a.remaining - Math.max(0, e - s)), m.doneUnsafe(d, h.succeed(e));
121
+ }
122
+ },
123
+ registering: (t) => {
124
+ let n = A(e, g, o);
125
+ if (n) throw n;
126
+ if (g !== "direct" && t.generation !== g) throw Object.assign(/* @__PURE__ */ Error("Subscription channel generation changed"), { code: "VFS_ATTACHMENT_LOST" });
127
+ oe(e, g);
128
+ let r = e.subscriptionUnknown.get(g);
129
+ if (r !== void 0) throw re(r, e.fileName, o);
130
+ if ([...e.subscriptionSetups.values()].some((e) => e.generation === g && e.state === "retiring")) throw ne;
131
+ p = t, _ = {
132
+ generation: g,
133
+ closed: t.closed,
134
+ state: "active"
135
+ }, e.subscriptionSetups.set(t.closed, _), t.closed.then((n) => {
136
+ if (e.subscriptionSetups.get(t.closed) === _ && e.subscriptionSetups.delete(t.closed), n.status === "unknown") {
137
+ let t = k(n.error, e.fileName, o);
138
+ e.currentGeneration() === g && e.subscriptionUnknown.set(g, t);
139
+ try {
140
+ console.warn("OPFS VFS subscription retirement is unknown", {
141
+ fileName: e.fileName,
142
+ path: o,
143
+ generation: g,
144
+ code: t.code
145
+ });
146
+ } catch {}
147
+ }
148
+ }, (n) => {
149
+ e.subscriptionSetups.get(t.closed) === _ && e.subscriptionSetups.delete(t.closed);
150
+ let r = k(n, e.fileName, o);
151
+ e.currentGeneration() === g && e.subscriptionUnknown.set(g, r);
152
+ });
153
+ },
154
+ retiring: (t) => {
155
+ let n = e.subscriptionSetups.get(t.closed);
156
+ n && (n.state = "retiring");
157
+ }
158
+ }, C = m.await(n).pipe(h.flatMap((t) => {
159
+ let n = Math.max(0, i.currentTimeMillisUnsafe() - t);
160
+ return h.raceFirst(m.await(d).pipe(h.as("finished")), h.sleep(Math.max(0, a.remaining - n)).pipe(h.as("timeout"))).pipe(h.flatMap((t) => t === "finished" ? h.never : h.fail(new s({
161
+ code: "SUBSCRIPTION_SETUP_FAILED",
162
+ fileName: e.fileName,
163
+ path: o,
164
+ sourceCode: "VFS_SUBSCRIPTION_RETIREMENT_TIMEOUT",
165
+ details: u(Object.assign(/* @__PURE__ */ Error("Subscription retirement wait timed out"), { code: "VFS_SUBSCRIPTION_RETIREMENT_TIMEOUT" }))
166
+ }))));
167
+ })), T = h.tryPromise({
168
+ try: (e) => {
169
+ let n = () => c.abort();
170
+ return e.addEventListener("abort", n, { once: !0 }), e.aborted && c.abort(), y = ee(E, {
171
+ ...t,
172
+ signal: c.signal,
173
+ onError: w
174
+ }, async (e) => {
175
+ !l && !f && await h.runPromise(x.offer(b, e));
176
+ }, S).then((e) => (v = e, l && (_ && (_.state = "retiring"), e.unsubscribe()), {
177
+ ok: !0,
178
+ handle: e
179
+ }), (e) => ({
180
+ ok: !1,
181
+ cause: e
182
+ })), y.then(() => e.removeEventListener("abort", n)), y;
183
+ },
184
+ catch: (t) => t === ne ? t : k(t, e.fileName, o)
185
+ }), D = yield* h.result(r(h.raceFirst(T, C)));
186
+ if (D._tag === "Success" && D.success.ok) {
187
+ if (v = D.success.handle, l) return v.unsubscribe(), yield* h.fail(k(Object.assign(/* @__PURE__ */ Error("Subscription scope closed"), { code: "EBADF" }), e.fileName, o));
188
+ break;
189
+ }
190
+ let te = D._tag === "Failure" ? D.failure : "cause" in D.success ? D.success.cause : void 0;
191
+ if (te === ne) {
192
+ if ((yield* r(e.awaitReady(a, "subscribe").pipe(h.mapError((t) => k(t, e.fileName, o))))) !== g) return yield* h.fail(A(e, g, o) ?? k(Object.assign(/* @__PURE__ */ Error("Owner changed during subscription setup"), { code: "VFS_ATTACHMENT_LOST" }), e.fileName, o));
193
+ yield* r(se(e, g, a, i, o));
194
+ continue;
195
+ }
196
+ return yield* h.fail(k(te, e.fileName, o));
197
+ }
198
+ let D = p.closed.then((t) => (t.status === "released" && setTimeout(() => {
199
+ l || f || (x.endUnsafe(b), m.doneUnsafe(S, h.succeed(void 0)));
200
+ }, 0), ie(t, e.fileName, o)));
201
+ return {
202
+ changes: T.suspend(() => d ? T.die(/* @__PURE__ */ Error("A subscription stream can only be consumed once")) : (d = !0, T.concat(T.fromEffectRepeat(x.take(b)).pipe(T.flatMap((e) => f ? T.fail(f) : T.succeed(e)), T.catchCause((e) => f ? T.fail(f) : l ? T.empty : T.failCause(e))), T.unwrap(m.await(S).pipe(h.map((e) => e ? T.fail(e) : T.empty)))).pipe(T.ensuring(O())))),
203
+ retired: h.promise(() => D)
204
+ };
205
+ }).pipe(h.onExit((e) => g.isFailure(e) ? C.close(a, e) : h.void));
206
+ })), ce = /* @__PURE__ */ t({
207
+ Subscriptions: () => le,
208
+ layer: () => ue
209
+ }), le = class extends p.Service()("@opfs-vfs/effect/Subscriptions") {}, ue = v.effect(le, h.gen(function* () {
210
+ let t = yield* r, n = e(t);
211
+ return !n || !n.subscriptionsAvailable ? yield* h.fail(new c({
212
+ kind: "unsupported",
213
+ fileName: n?.fileName ?? t.fileName,
214
+ operation: "subscribe",
215
+ code: "ENOTSUP",
216
+ outcome: "not-applied",
217
+ details: null
218
+ })) : { subscribe: (e) => j(n, e) };
219
+ })), M = "FileSystem", de = 16777216, fe = (e, t) => e instanceof D && e.dispatch === "refused" ? "not-applied" : e instanceof D && e.dispatch === "sent" && t ? "possibly-applied" : "unknown", pe = (e, t, n) => l(e instanceof D ? e.cause : e, t, n, void 0, fe(e, !1)), N = (e, t, n, r) => new c({
220
+ kind: "lifecycle",
221
+ fileName: t,
222
+ operation: n,
223
+ ...e.code === void 0 ? {} : { code: e.code },
224
+ outcome: r,
225
+ details: e.details,
226
+ ...e.cause === void 0 ? {} : { cause: e.cause }
227
+ }), me = (e, t) => {
228
+ if (t instanceof c && t.kind === "lifecycle" && t.cause !== void 0) return "Unknown";
229
+ let n = e.code ?? "";
230
+ return n === "ENOENT" ? "NotFound" : n === "EEXIST" ? "AlreadyExists" : n === "EACCES" || n === "EPERM" ? "PermissionDenied" : n === "EISDIR" || n === "ENOTDIR" || n === "ELOOP" || n === "EBADF" ? "BadResource" : n === "EBUSY" ? "Busy" : n === "VFS_OWNER_READY_TIMEOUT" || n === "LEADER_RESPONSE_TIMEOUT" ? "TimedOut" : t instanceof c && t.kind === "corruption" || "reason" in t && (t.reason === "IntegrityFailure" || t.reason === "VaultCorrupt" || t.reason === "SidecarCorrupt") ? "InvalidData" : "Unknown";
231
+ }, P = (e, t, n, r = null, i = !1) => {
232
+ if (b.isPlatformError(e)) return e;
233
+ let a = (e instanceof D ? e : void 0)?.cause ?? e, o = u(a), s = fe(e, i), d = e instanceof c ? e : l(a, r, t, void 0, s);
234
+ return b.systemError({
235
+ _tag: me(o, d),
236
+ module: M,
237
+ method: t,
238
+ ...n === void 0 ? {} : { pathOrDescriptor: n },
239
+ ...o.message ? { description: o.message } : {},
240
+ cause: d
241
+ });
242
+ }, F = (e, t) => typeof t != "string" || !t.startsWith("/") ? b.badArgument({
243
+ module: M,
244
+ method: e,
245
+ description: "path must be absolute"
246
+ }) : void 0, he = (e, t, n = null) => b.systemError({
247
+ _tag: "Unknown",
248
+ module: M,
249
+ method: e,
250
+ ...t === void 0 ? {} : { pathOrDescriptor: t },
251
+ description: "Operation is not supported by this adapter slice",
252
+ cause: new c({
253
+ kind: "unsupported",
254
+ fileName: n,
255
+ operation: e,
256
+ ...t === void 0 ? {} : { path: t },
257
+ code: "ENOTSUP",
258
+ outcome: "not-applied",
259
+ details: null
260
+ })
261
+ }), ge = (e, t, n) => {
262
+ let r = new s({
263
+ code: "SUBSCRIPTION_INTERRUPTED",
264
+ fileName: e.fileName,
265
+ path: t,
266
+ sourceCode: "VFS_ATTACHMENT_LOST",
267
+ details: u(n)
268
+ });
269
+ return b.systemError({
270
+ _tag: "Unknown",
271
+ module: M,
272
+ method: "watch",
273
+ pathOrDescriptor: t,
274
+ cause: r
275
+ });
276
+ }, I = (e, t, n) => {
277
+ if (b.isPlatformError(n)) {
278
+ let r = n.reason.cause;
279
+ return S.is(c)(r) && r.code === "VFS_ATTACHMENT_LOST" ? ge(e, t, r) : n;
280
+ }
281
+ return S.is(s)(n) ? S.is(c)(n.cause) && n.cause.kind === "unsupported" ? P(n.cause, "watch", t, e.fileName) : n.code === "SUBSCRIPTION_INTERRUPTED" ? b.systemError({
282
+ _tag: "Unknown",
283
+ module: M,
284
+ method: "watch",
285
+ pathOrDescriptor: t,
286
+ cause: n
287
+ }) : n.sourceCode === "VFS_ATTACHMENT_LOST" ? ge(e, t, n) : n.sourceCode === "VFS_OWNER_READY_TIMEOUT" || n.sourceCode === "VFS_SUBSCRIPTION_RETIREMENT_TIMEOUT" ? b.systemError({
288
+ _tag: "TimedOut",
289
+ module: M,
290
+ method: "watch",
291
+ pathOrDescriptor: t,
292
+ cause: n
293
+ }) : b.systemError({
294
+ _tag: "Unknown",
295
+ module: M,
296
+ method: "watch",
297
+ pathOrDescriptor: t,
298
+ cause: n
299
+ }) : P(n, "watch", t, e.fileName);
300
+ }, L = (e, t, n, r, i = !1, a = !0, o, s, l = !0) => {
301
+ let d = F(t, n);
302
+ return d ? h.fail(d) : h.gen(function* () {
303
+ let d = s ?? { remaining: e.readinessTimeout }, p = 0, _, v;
304
+ for (;;) {
305
+ let s = yield* h.result(e.awaitReady(d, t));
306
+ if (s._tag === "Failure") return yield* h.fail(P(_ && u(s.failure).code === "VFS_OWNER_READY_TIMEOUT" ? _ : e.terminal() ? N(s.failure, e.fileName, t, "not-applied") : s.failure, t, n, e.fileName, i));
307
+ let y = s.success;
308
+ if (o !== void 0 && y !== o) return yield* h.fail(P(_e(), t, n, e.fileName, i));
309
+ if (v === y) return yield* h.fail(P(_, t, n, e.fileName, i));
310
+ let b = () => h.scoped(h.gen(function* () {
311
+ if (i) {
312
+ let n = h.asVoid(h.raceFirst(h.acquireRelease(w.take(e.gate, 1), () => w.release(e.gate, 1), { interruptible: !0 }), m.await(e.terminalSignal).pipe(h.flatMap((n) => h.fail(N(n, e.fileName, t, "not-applied"))))));
313
+ yield* l ? h.interruptible(n) : h.uninterruptible(n);
314
+ }
315
+ let n = e.terminal(), a = e.currentGeneration();
316
+ if (e.isClosed() || n) return yield* h.fail(n ? N(n, e.fileName, t, "not-applied") : new c({
317
+ kind: "lifecycle",
318
+ fileName: e.fileName,
319
+ operation: t,
320
+ outcome: "not-applied",
321
+ details: null
322
+ }));
323
+ if (a !== y) return yield* h.succeed({ retry: !0 });
324
+ let o = e.continuity;
325
+ i && (o._tag === "lost" || o._tag === "pending" && o.generation !== y ? e.continuity = {
326
+ _tag: "lost",
327
+ generation: o.generation
328
+ } : o._tag === "clean" && (e.continuity = {
329
+ _tag: "pending",
330
+ generation: y
331
+ }));
332
+ let s = h.suspend(() => {
333
+ try {
334
+ let t = r(e.backend, y);
335
+ return h.isEffect(t) ? t : h.tryPromise({
336
+ try: () => Promise.resolve(t),
337
+ catch: (e) => e
338
+ });
339
+ } catch (e) {
340
+ return h.fail(e);
341
+ }
342
+ }).pipe(h.map((e) => ({
343
+ retry: !1,
344
+ result: e
345
+ })));
346
+ return yield* i ? s.pipe(h.catchCause((t) => {
347
+ let n = t.reasons.length === 1 && t.reasons[0]._tag === "Fail" ? t.reasons[0].error : void 0;
348
+ return n instanceof D && n.dispatch === "refused" && (e.continuity = o), h.failCause(t);
349
+ })) : s;
350
+ })), x = i ? h.uninterruptibleMask(() => b()) : b(), S = yield* h.exit(x);
351
+ if (g.isFailure(S)) {
352
+ let r = S.cause.reasons.length === 1 && S.cause.reasons[0]._tag === "Fail" ? S.cause.reasons[0].error : void 0;
353
+ if (a && p === 0 && e.canRecapture() && r instanceof D && r.dispatch === "refused" && u(r.cause).code === "VFS_ATTACHMENT_LOST" && e.currentGeneration() !== y && !e.terminal()) {
354
+ p++, _ = r, v = y;
355
+ continue;
356
+ }
357
+ return yield* h.failCause(f.map(S.cause, (r) => {
358
+ let a = r instanceof D ? e.terminal() : void 0;
359
+ return P(a ? N(a, e.fileName, t, fe(r, i)) : r, t, n, e.fileName, i);
360
+ }));
361
+ }
362
+ let C = S.value;
363
+ if (C.retry) {
364
+ let r = _e();
365
+ if (o !== void 0 || !a || p++ >= 1 || !e.canRecapture() || e.terminal()) return yield* h.fail(P(r, t, n, e.fileName, i));
366
+ _ = r, v = void 0;
367
+ continue;
368
+ }
369
+ return C.result;
370
+ }
371
+ });
372
+ }, _e = () => new D(Object.assign(/* @__PURE__ */ Error("Owner changed before dispatch"), { code: "VFS_ATTACHMENT_LOST" }), "refused"), R = (e, t) => b.systemError({
373
+ _tag: "BadResource",
374
+ module: M,
375
+ method: t,
376
+ cause: new c({
377
+ kind: "lifecycle",
378
+ fileName: e.fileName,
379
+ operation: t,
380
+ code: "VFS_FILE_GENERATION_CHANGED",
381
+ outcome: "not-applied",
382
+ details: null
383
+ })
384
+ }), z = (e, t, n, r = !1) => P(Object.assign(/* @__PURE__ */ Error("Invalid result from VFS"), { code: "EIO" }), t, n, e.fileName, r), B = (e, t, n, r = !1, i = !1) => h.uninterruptibleMask((a) => h.gen(function* () {
385
+ let o = e.state, s = o.terminal();
386
+ return s ? yield* h.fail(P(s, t, e.path, o.fileName, r)) : e.closed || o.isClosed() || o.currentGeneration() !== e.generation ? yield* h.fail(R(o, t)) : yield* h.scoped(h.gen(function* () {
387
+ r && (yield* a(h.interruptible(h.asVoid(h.raceFirst(h.acquireRelease(w.take(o.gate, 1), () => w.release(o.gate, 1), { interruptible: !0 }), m.await(o.terminalSignal).pipe(h.flatMap((n) => h.fail(P(n, t, e.path, o.fileName, r)))))))));
388
+ let s = o.terminal();
389
+ if (s) return yield* h.fail(P(s, t, e.path, o.fileName, r));
390
+ if (e.closed || o.isClosed() || o.currentGeneration() !== e.generation) return yield* h.fail(R(o, t));
391
+ let c = o.continuity;
392
+ r && !i && (c._tag === "lost" || c._tag === "pending" && c.generation !== e.generation ? o.continuity = {
393
+ _tag: "lost",
394
+ generation: c.generation
395
+ } : c._tag === "clean" && (o.continuity = {
396
+ _tag: "pending",
397
+ generation: e.generation
398
+ }));
399
+ let l = yield* h.tryPromise({
400
+ try: () => Promise.resolve(n()),
401
+ catch: (e) => e
402
+ }).pipe(h.catchCause((n) => {
403
+ let a = n.reasons.length === 1 && n.reasons[0]._tag === "Fail" ? n.reasons[0].error : void 0;
404
+ return r && !i && a instanceof D && a.dispatch === "refused" && (o.continuity = c), h.failCause(f.map(n, (n) => P(n, t, e.path, o.fileName, r)));
405
+ }));
406
+ return i && o.continuity._tag === "pending" && o.continuity.generation === e.generation && (o.continuity = { _tag: "clean" }), l;
407
+ }));
408
+ })), ve = (e, t, n) => "forGeneration" in e ? e.forGeneration(t).stat(n) : e.statSync(n), ye = (e) => (e.closed = !0, e.closing ??= (async () => {
409
+ if (!e.openLaunched || (e.openingDone && await e.openingDone, e.fd === void 0)) return;
410
+ let t = e.state.backend;
411
+ "forGeneration" in t ? await t.forGeneration(e.generation).close(e.fd) : t.closeSync(e.fd);
412
+ })().finally(() => {
413
+ e.release && e.state.files.delete(e.release);
414
+ }), e.closing), be = (e, t, n, r = !1, i = !1) => $(e, t, () => B(e, t, () => n(e.state.backend), r, i)), xe = (e, t, n, r, i, a, o) => {
415
+ if (!Object.hasOwn(Y, n)) return h.fail(Z("open", "unsupported file flag"));
416
+ if (r !== void 0 && !X(r)) return h.fail(Z("open", "mode must be an unsigned 32-bit integer"));
417
+ let s = a ?? Y[n], c = (s & (E.O_CREAT | E.O_TRUNC)) !== 0;
418
+ return h.uninterruptibleMask((n) => h.gen(function* () {
419
+ let a = F("open", t);
420
+ if (a) return yield* h.fail(a);
421
+ let l = yield* h.scope, u = yield* C.fork(l, "sequential"), d, p = {
422
+ state: e,
423
+ fd: void 0,
424
+ generation: "",
425
+ path: t,
426
+ append: (s & E.O_APPEND) !== 0,
427
+ cursor: 0,
428
+ cursorGate: w.makeUnsafe(1),
429
+ closed: !1
430
+ };
431
+ p.release = () => h.tryPromise({
432
+ try: () => ye(p),
433
+ catch: (t) => pe(t, e.fileName, "close")
434
+ }).pipe(h.orDie), e.files.add(p.release), yield* C.addFinalizer(u, p.release());
435
+ let m = () => p.closed || e.isClosed() || e.scope?.state._tag === "Closed" || l.state._tag === "Closed" || u.state._tag === "Closed", _ = () => new D(Object.assign(/* @__PURE__ */ Error("File scope is closed"), { code: "EBADF" }), "refused"), v = (e) => h.gen(function* () {
436
+ let t = yield* h.exit(p.release());
437
+ return yield* h.exit(C.close(u, e)), g.isFailure(t) ? yield* h.failCause(f.combine(e.cause, t.cause)) : yield* h.failCause(e.cause);
438
+ }), y = (e) => g.fail(e);
439
+ if (m()) return yield* v(y(R(e, "open")));
440
+ let x = "", S = yield* h.exit(n(L(e, "open", t, (e, n) => {
441
+ if (m()) return h.fail(_());
442
+ x = n, p.openLaunched = !0, p.openingDone = new Promise((e) => {
443
+ d = e;
444
+ });
445
+ try {
446
+ return Promise.resolve("forGeneration" in e ? e.forGeneration(n).open(t, s, r) : e.openSync(t, s, r)).then((e) => (p.fd = e, p.generation = n, e)).finally(d);
447
+ } catch (e) {
448
+ throw d(), e;
449
+ }
450
+ }, c, !1, i, o)));
451
+ return g.isSuccess(S) && (p.fd = S.value, p.generation = x), g.isFailure(S) ? yield* v(S) : m() ? yield* v(y(b.systemError({
452
+ _tag: "BadResource",
453
+ module: M,
454
+ method: "open"
455
+ }))) : p;
456
+ }));
457
+ }, V = (e, t, n, r, i = !1, a, o, s) => h.flatMap(xe(e, t, n, r, a, o, s), (n) => n.closed ? h.fail(b.systemError({
458
+ _tag: "BadResource",
459
+ module: M,
460
+ method: "open"
461
+ })) : h.succeed({
462
+ [_.FileTypeId]: _.FileTypeId,
463
+ stat: be(n, "fstat", (e) => "forGeneration" in n.state.backend ? n.state.backend.forGeneration(n.generation).fstat(n.fd).then(Q) : Q(n.state.backend.fstatSync(n.fd))),
464
+ seek: (e, t) => $(n, "seek", () => h.gen(function* () {
465
+ let r = BigInt(n.cursor), i = Be(t === "start" ? e : r + e, "seek");
466
+ return typeof i == "number" ? !Number.isSafeInteger(i) || i < 0 ? yield* h.fail(Z("seek", "seek position must be a non-negative safe integer")) : (n.cursor = i, BigInt(i)) : yield* h.fail(i);
467
+ })),
468
+ sync: be(n, "fsync", (e) => "forGeneration" in n.state.backend ? n.state.backend.forGeneration(n.generation).fsync(n.fd) : n.state.backend.fsyncSync(n.fd), !0, !0),
469
+ read: (e) => e instanceof Uint8Array ? $(n, "read", () => h.gen(function* () {
470
+ let t = yield* B(n, "read", () => {
471
+ let t = n.state.backend;
472
+ return "forGeneration" in t ? t.forGeneration(n.generation).read(n.fd, e.byteLength, n.cursor) : t.readSync(n.fd, e.byteLength, n.cursor);
473
+ });
474
+ return !Number.isSafeInteger(t.read) || t.read < 0 || t.read > e.byteLength || t.buffer.byteLength < t.read ? yield* h.fail(z(n.state, "read", n.path)) : (e.set(t.buffer.subarray(0, t.read)), n.cursor += t.read, t.read);
475
+ })) : h.fail(Z("read", "buffer must be a Uint8Array")),
476
+ readAlloc: (e) => !Number.isSafeInteger(e) || e < 0 ? h.fail(Z("readAlloc", "size must be a non-negative safe integer")) : e === 0 ? $(n, "readAlloc", () => h.succeed(y.none())) : Se(n, e),
477
+ truncate: (e = 0) => !Number.isSafeInteger(e) || e < 0 ? h.fail(Z("truncate", "length must be a non-negative safe integer")) : $(n, "ftruncate", () => h.gen(function* () {
478
+ yield* B(n, "ftruncate", () => {
479
+ let t = n.state.backend;
480
+ return "forGeneration" in t ? t.forGeneration(n.generation).ftruncate(n.fd, e) : t.ftruncateSync(n.fd, e);
481
+ }, !0), n.append || (n.cursor = Math.min(n.cursor, e));
482
+ })),
483
+ write: (r) => r instanceof Uint8Array ? h.suspend(() => {
484
+ let i = Uint8Array.from(r), a = i.byteLength;
485
+ return $(n, "write", () => h.gen(function* () {
486
+ let r = yield* B(n, "write", () => {
487
+ let e = n.state.backend;
488
+ return "forGeneration" in e ? e.forGeneration(n.generation).write(n.fd, i, n.append ? void 0 : n.cursor) : e.writeSync(n.fd, i, n.append ? void 0 : n.cursor);
489
+ }, !0, !1);
490
+ return !Number.isSafeInteger(r) || r < 0 || r > a ? yield* h.fail(z(e, "write", t, !0)) : (n.append || (n.cursor += r), r);
491
+ }));
492
+ }) : h.fail(Z("write", "buffer must be a Uint8Array")),
493
+ writeAll: (r) => r instanceof Uint8Array ? h.suspend(() => {
494
+ let a = Uint8Array.from(r);
495
+ return $(n, "writeAll", () => {
496
+ let r = 0;
497
+ return h.gen(function* () {
498
+ for (; r < a.byteLength;) {
499
+ let i = Uint8Array.from(a.subarray(r, r + 65536)), o = i.byteLength, s = yield* B(n, "write", () => {
500
+ let e = n.state.backend;
501
+ return "forGeneration" in e ? e.forGeneration(n.generation).write(n.fd, i, n.append ? void 0 : n.cursor) : e.writeSync(n.fd, i, n.append ? void 0 : n.cursor);
502
+ }, !0, !1);
503
+ if (!Number.isSafeInteger(s) || s < 0 || s > o) return yield* h.fail(z(e, "writeAll", t, !0));
504
+ if (r += s, n.append || (n.cursor += s), s === 0) return yield* h.fail(b.systemError({
505
+ _tag: "WriteZero",
506
+ module: M,
507
+ method: "writeAll",
508
+ pathOrDescriptor: t
509
+ }));
510
+ }
511
+ }).pipe(h.catchCause((n) => r > 0 || i ? h.failCause(f.map(n, (n) => H(e, t, n))) : h.failCause(n)));
512
+ });
513
+ }) : h.fail(Z("writeAll", "buffer must be a Uint8Array"))
514
+ })), Se = (e, t) => $(e, "readAlloc", () => h.gen(function* () {
515
+ let n = yield* B(e, "readAlloc", () => {
516
+ let n = e.state.backend;
517
+ return "forGeneration" in n ? n.forGeneration(e.generation).read(e.fd, t, e.cursor) : n.readSync(e.fd, t, e.cursor);
518
+ });
519
+ return !Number.isSafeInteger(n.read) || n.read < 0 || n.read > t || n.buffer.byteLength < n.read ? yield* h.fail(z(e.state, "readAlloc", e.path)) : n.read === 0 ? y.none() : (e.cursor += n.read, y.some(Uint8Array.from(n.buffer.subarray(0, n.read))));
520
+ })), H = (e, t, n, r = "writeAll") => {
521
+ let a = b.isPlatformError(n) ? n.reason.cause : void 0, o = S.is(c)(a) ? new c({
522
+ kind: a.kind,
523
+ fileName: a.fileName,
524
+ operation: r,
525
+ path: t,
526
+ ...a.code === void 0 ? {} : { code: a.code },
527
+ outcome: "possibly-applied",
528
+ details: a.details,
529
+ ...a.cause === void 0 ? {} : { cause: a.cause }
530
+ }) : S.is(i)(a) ? new i({
531
+ reason: a.reason,
532
+ fileName: a.fileName,
533
+ operation: r,
534
+ path: t,
535
+ ...a.code === void 0 ? {} : { code: a.code },
536
+ outcome: "possibly-applied",
537
+ details: a.details,
538
+ ...a.cause === void 0 ? {} : { cause: a.cause }
539
+ }) : new c({
540
+ kind: "filesystem",
541
+ fileName: e.fileName,
542
+ operation: r,
543
+ path: t,
544
+ outcome: "possibly-applied",
545
+ details: u(n),
546
+ cause: n
547
+ }), s = o.details ?? u(n), l = b.isPlatformError(n) && n.reason._tag !== "BadArgument" ? n.reason._tag : me(s, o);
548
+ return b.systemError({
549
+ _tag: l,
550
+ module: M,
551
+ method: r,
552
+ pathOrDescriptor: t,
553
+ ...s.message ? { description: s.message } : {},
554
+ cause: o
555
+ });
556
+ }, U = (e, t) => e === "/" ? `/${t}` : `${e.replace(/\/$/, "")}/${t}`, W = (e) => {
557
+ let t = e.replace(/\/$/, ""), n = t.lastIndexOf("/");
558
+ return n <= 0 ? "/" : t.slice(0, n);
559
+ }, Ce = (e) => e.slice(e.lastIndexOf("/") + 1), G = (e) => (e.mode & 61440) == 40960, K = (e, t, n, r, i, a = !1, o, s = !0) => L(e, n, r, (e) => i(e), a, !1, t, o, s), q = (e, t, n, r) => K(e, t, "lstat", n, (e) => "forGeneration" in e ? e.forGeneration(t).lstat(n) : e.lstatSync(n), !1, r), J = (e, t, n, r) => K(e, t, "realPath", n, (e) => "forGeneration" in e ? e.forGeneration(t).realpath(n) : e.realpathSync(n), !1, r), we = (e, t, n, r) => K(e, t, "stat", n, (e) => "forGeneration" in e ? e.forGeneration(t).stat(n) : e.statSync(n), !1, r), Te = (e, t, n, r) => K(e, t, "remove", n, (e) => "forGeneration" in e ? e.forGeneration(t).remove(n) : e.removeSync(n), !0, r, !1), Ee = (e, t, n, r) => h.result(q(e, t, n, r)).pipe(h.flatMap((e) => e._tag === "Success" ? h.succeed(y.some(e.success)) : e.failure.reason._tag === "NotFound" ? h.succeed(y.none()) : h.fail(e.failure))), De = (e, t, n, r) => h.gen(function* () {
560
+ let i = n, a = [];
561
+ for (;;) {
562
+ let n = yield* Ee(e, t, i, r);
563
+ if (y.isSome(n)) {
564
+ let n = yield* J(e, t, i, r);
565
+ return (yield* we(e, t, n, r)).is_dir ? {
566
+ realPath: n,
567
+ missing: a.reverse()
568
+ } : yield* h.fail(Z("copy", "destination parent is not a directory"));
569
+ }
570
+ if (i === "/") return yield* h.fail(Z("copy", "destination parent does not exist"));
571
+ a.push(Ce(i)), i = W(i);
572
+ }
573
+ }), Oe = (e, t, n, r, i) => {
574
+ let a = !1;
575
+ return h.scoped(h.gen(function* () {
576
+ let o = yield* V(e, n, "r", void 0, !1, t, void 0, i), s = yield* V(e, r, "w", void 0, !1, t, E.O_WRONLY | E.O_CREAT, i);
577
+ a = !0;
578
+ let [c, l] = yield* h.all([o.stat, s.stat]), u = c.ino._tag === "Some" ? c.ino.value : void 0, d = l.ino._tag === "Some" ? l.ino.value : void 0;
579
+ if (u === void 0 || d === void 0) return yield* h.fail(z(e, "copyFile", n));
580
+ if (u === d) return a = !1, yield* h.fail(Z("copyFile", "source and destination refer to the same file"));
581
+ for (yield* s.truncate(0);;) {
582
+ let e = yield* o.readAlloc(65536);
583
+ if (y.isNone(e)) return;
584
+ yield* s.writeAll(e.value);
585
+ }
586
+ }).pipe(h.catchCause((t) => a ? h.failCause(f.map(t, (t) => H(e, r, t, "copyFile"))) : h.failCause(t))));
587
+ }, ke = (e, t, n, r, i, a, o) => {
588
+ let s = !1;
589
+ return h.gen(function* () {
590
+ let c = yield* q(e, t, n, o), l = c.is_dir ? yield* J(e, t, n, o) : U(yield* J(e, t, W(n), o), Ce(n)), u = yield* De(e, t, W(r), o), d = yield* Ee(e, t, r, o), f = y.isSome(d) && d.value.is_dir && !G(d.value) ? yield* J(e, t, r, o) : U(u.missing.reduce(U, u.realPath), Ce(r));
591
+ if (f === l || c.is_dir && f.startsWith(`${l.replace(/\/$/, "")}/`)) return yield* h.fail(Z("copy", "destination is the source or is inside it"));
592
+ u.missing.length > 0 && (yield* K(e, t, "makeDirectory", W(r), (e) => "forGeneration" in e ? e.forGeneration(t).mkdir(W(r), { recursive: !0 }) : e.mkdirSync(W(r), { recursive: !0 }), !0, o), s = !0);
593
+ let p = (n, r) => {
594
+ if (!a) return h.void;
595
+ let i = n.atimeMs ?? n.timestampMs, c = n.mtimeMs ?? n.timestampMs;
596
+ return i === void 0 || c === void 0 ? h.void : K(e, t, "utimes", r, (e) => "forGeneration" in e ? e.forGeneration(t).utimes(r, i, c) : e.utimesSync(r, i, c), !0, o).pipe(h.tap(() => h.sync(() => s = !0)));
597
+ }, m = (n, r) => h.gen(function* () {
598
+ let a = yield* q(e, t, n, o), c = yield* Ee(e, t, r, o), l = y.getOrUndefined(c), u = G(a), d = l !== void 0 && (a.is_dir && l.is_dir || a.is_file && (l.is_file || G(l)) || u && G(l));
599
+ if (l && !d && u && !G(l)) return yield* h.fail(P(Object.assign(/* @__PURE__ */ Error("Destination already exists"), { code: "EEXIST" }), "copy", r, e.fileName));
600
+ if (l && !d) return yield* h.fail(Z("copy", "source and destination have incompatible types"));
601
+ if (!l || i || a.is_dir) {
602
+ if (u) {
603
+ if (l && i && (yield* K(e, t, "remove", r, (e) => "forGeneration" in e ? e.forGeneration(t).unlink(r) : e.unlinkSync(r), !0, o), s = !0), !l || i) {
604
+ let i = yield* K(e, t, "readLink", n, (e) => "forGeneration" in e ? e.forGeneration(t).readlink(n) : e.readlinkSync(n), !1, o);
605
+ yield* K(e, t, "symlink", r, (e) => "forGeneration" in e ? e.forGeneration(t).symlink(i, r) : e.symlinkSync(i, r), !0, o), s = !0;
606
+ }
607
+ return;
608
+ }
609
+ if (a.is_dir) {
610
+ l || (yield* K(e, t, "makeDirectory", r, (e) => "forGeneration" in e ? e.forGeneration(t).mkdir(r, { mode: a.mode & 4095 | 448 }) : e.mkdirSync(r, { mode: a.mode & 4095 | 448 }), !0, o)), l || (s = !0);
611
+ let c = yield* K(e, t, "readDirectory", n, (e) => "forGeneration" in e ? e.forGeneration(t).readdirEntries(n) : e.readdirEntriesSync(n), !1, o);
612
+ for (let e of [...c].sort((e, t) => e.name.localeCompare(t.name))) yield* m(U(n, e.name), U(r, e.name));
613
+ (!l || i) && (yield* K(e, t, "chmod", r, (e) => "forGeneration" in e ? e.forGeneration(t).chmod(r, a.mode & 4095) : e.chmodSync(r, a.mode & 4095), !0, o), s = !0, yield* p(a, r));
614
+ return;
615
+ }
616
+ if (a.is_file) {
617
+ if (!l || i) {
618
+ if (l && l.is_file && a.ino === l.ino) return yield* h.fail(Z("copy", "source and destination refer to the same file"));
619
+ l && (yield* K(e, t, "remove", r, (e) => "forGeneration" in e ? e.forGeneration(t).unlink(r) : e.unlinkSync(r), !0, o), s = !0), yield* Oe(e, t, n, r, o), s = !0, yield* K(e, t, "chmod", r, (e) => "forGeneration" in e ? e.forGeneration(t).chmod(r, a.mode & 4095) : e.chmodSync(r, a.mode & 4095), !0, o), s = !0, yield* p(a, r);
620
+ }
621
+ return;
622
+ }
623
+ return yield* h.fail(Z("copy", "unsupported source entry type"));
624
+ }
625
+ });
626
+ if (c.is_dir && y.isSome(d) && d.value.is_dir) {
627
+ for (let i of yield* K(e, t, "readDirectory", n, (e) => "forGeneration" in e ? e.forGeneration(t).readdirEntries(n) : e.readdirEntriesSync(n), !1, o)) yield* m(U(n, i.name), U(r, i.name));
628
+ i && (yield* K(e, t, "chmod", r, (e) => "forGeneration" in e ? e.forGeneration(t).chmod(r, c.mode & 4095) : e.chmodSync(r, c.mode & 4095), !0, o), s = !0, yield* p(c, r));
629
+ } else yield* m(n, r);
630
+ }).pipe(h.catchCause((t) => s ? h.failCause(f.map(t, (t) => H(e, r, t, "copy"))) : h.failCause(t)));
631
+ }, Ae = (e) => typeof e == "string" && !e.includes("/") && !e.includes("\0"), je = () => {
632
+ let e = /* @__PURE__ */ new Uint8Array(8);
633
+ return globalThis.crypto.getRandomValues(e), Array.from(e, (e) => e.toString(16).padStart(2, "0")).join("");
634
+ }, Me = (e) => e.reason._tag === "AlreadyExists", Ne = (e, t, n, r) => h.gen(function* () {
635
+ if (n.state._tag === "Closed") return yield* h.fail(R(e, "makeTemp"));
636
+ let i = yield* C.fork(n, "sequential"), a, o = new Promise((e) => {
637
+ a = e;
638
+ }), s, c, l, u = h.uninterruptibleMask(() => h.gen(function* () {
639
+ s.closing = !0, s.started && (yield* h.tryPromise({
640
+ try: () => o,
641
+ catch: (e) => e
642
+ })), s.created && s.root && !s.detached && (yield* Te(e, t, s.root, s.cleanupBudget ?? { remaining: e.readinessTimeout }));
643
+ }).pipe(h.ensuring(h.sync(() => {
644
+ e.files.delete(l);
645
+ })), h.orDie));
646
+ l = () => c, c = yield* h.cached(u);
647
+ let d = h.uninterruptible(h.gen(function* () {
648
+ if (i.state._tag === "Closed" || s.closing) return yield* h.fail(R(e, "makeTemp"));
649
+ s.detached = !0, s.created = !1, s.root = void 0;
650
+ let t = C.closeUnsafe(i, g.succeed(void 0));
651
+ t && (yield* t);
652
+ }));
653
+ return s = {
654
+ child: i,
655
+ state: e,
656
+ generation: t,
657
+ created: !1,
658
+ mutated: !1,
659
+ started: !1,
660
+ detached: !1,
661
+ closing: !1,
662
+ cleanupBudget: r,
663
+ get allocationDone() {
664
+ return o;
665
+ },
666
+ finishAllocation: () => a(),
667
+ release: l,
668
+ detach: d
669
+ }, e.files.add(l), yield* C.addFinalizer(i, l()), i.state._tag === "Closed" ? yield* h.fail(R(e, "makeTemp")) : s;
670
+ }), Pe = (e, t, n, r, i, a, o) => h.suspend(() => L(e, t, n, (s, c) => h.uninterruptibleMask((s) => h.gen(function* () {
671
+ let l = yield* Ne(e, c, r, a);
672
+ l.generation = c, l.started = !0;
673
+ let u = yield* h.exit(s(o(c, l)).pipe(h.ensuring(h.sync(l.finishAllocation))));
674
+ if (g.isFailure(u)) {
675
+ let r = yield* h.exit(l.release());
676
+ yield* h.exit(C.close(l.child, u));
677
+ let i = l.mutated ? f.map(u.cause, (r) => H(e, l.root ?? n, r, t)) : u.cause;
678
+ return g.isFailure(r) ? yield* h.failCause(f.combine(i, r.cause)) : yield* h.failCause(i);
679
+ }
680
+ if (e.isClosed() || l.child.state._tag === "Closed" || l.closing) {
681
+ let r = f.fail(R(e, t)), i = yield* h.exit(l.release());
682
+ yield* h.exit(C.close(l.child, g.failCause(r)));
683
+ let a = l.mutated ? f.map(r, (r) => H(e, l.root ?? n, r, t)) : r;
684
+ return g.isFailure(i) ? yield* h.failCause(f.combine(a, i.cause)) : yield* h.failCause(a);
685
+ }
686
+ return i ? l.cleanupBudget = void 0 : yield* l.detach, u.value;
687
+ })), !1, !1, void 0, a)), Fe = (e, t, n, r, i) => {
688
+ let a = n?.directory ?? "/tmp", o = n?.prefix ?? "", s = F(t, a) ?? (Ae(o) ? void 0 : Z(t, "prefix must be a single path component"));
689
+ return s ? h.fail(s) : h.suspend(() => {
690
+ let n = { remaining: e.readinessTimeout };
691
+ return Pe(e, t, a, r, i, n, (r, i) => h.gen(function* () {
692
+ yield* K(e, r, "makeDirectory", a, (e) => "forGeneration" in e ? e.forGeneration(r).mkdir(a, { recursive: !0 }) : e.mkdirSync(a, { recursive: !0 }), !0, n), i.mutated = !0;
693
+ let s = yield* J(e, r, a, n), c;
694
+ for (let a = 0; a < 128; a++) {
695
+ let a = U(s, `${o}${je()}`), l = yield* h.result(K(e, r, t, a, (e) => {
696
+ if ("forGeneration" in e) return e.forGeneration(r).mkdir(a).then(() => {
697
+ i.root = a, i.created = !0;
698
+ });
699
+ e.mkdirSync(a), i.root = a, i.created = !0;
700
+ }, !0, n));
701
+ if (l._tag === "Success") return i.root = a, i.created = !0, i.mutated = !0, {
702
+ path: a,
703
+ root: a,
704
+ generation: r,
705
+ owner: i
706
+ };
707
+ if (c = l.failure, !Me(c)) return yield* h.fail(c);
708
+ }
709
+ return yield* h.fail(c);
710
+ }));
711
+ });
712
+ }, Ie = (e, t, n, r, i) => {
713
+ let a = n?.directory ?? "/tmp", o = n?.prefix ?? "", s = n?.suffix ?? "", c = F(t, a) ?? (!Ae(o) || !Ae(s) ? Z(t, "prefix and suffix must be single path components") : void 0);
714
+ return c ? h.fail(c) : h.suspend(() => {
715
+ let n = { remaining: e.readinessTimeout };
716
+ return Pe(e, t, a, r, i, n, (r, i) => h.gen(function* () {
717
+ yield* K(e, r, "makeDirectory", a, (e) => "forGeneration" in e ? e.forGeneration(r).mkdir(a, { recursive: !0 }) : e.mkdirSync(a, { recursive: !0 }), !0, n), i.mutated = !0;
718
+ let c = yield* J(e, r, a, n), l;
719
+ for (let a = 0; a < 128; a++) {
720
+ let a = U(c, `${o}${je()}`), u = yield* h.result(K(e, r, t, a, (e) => {
721
+ if ("forGeneration" in e) return e.forGeneration(r).mkdir(a).then(() => {
722
+ i.root = a, i.created = !0;
723
+ });
724
+ e.mkdirSync(a), i.root = a, i.created = !0;
725
+ }, !0, n));
726
+ if (u._tag === "Failure") {
727
+ if (l = u.failure, Me(l)) continue;
728
+ return yield* h.fail(l);
729
+ }
730
+ i.root = a, i.created = !0, i.mutated = !0;
731
+ let d = U(a, `${je()}${s}`), p = yield* h.exit(h.scoped(h.asVoid(V(e, d, "wx", void 0, !1, r, E.O_WRONLY | E.O_CREAT | E.O_EXCL, n))));
732
+ if (g.isSuccess(p)) return {
733
+ path: d,
734
+ root: a,
735
+ generation: r,
736
+ owner: i
737
+ };
738
+ let m = p.cause.reasons.length === 1 && p.cause.reasons[0]._tag === "Fail" ? p.cause.reasons[0].error : void 0;
739
+ if (!b.isPlatformError(m) || !Me(m)) return yield* h.failCause(p.cause);
740
+ let _ = yield* h.exit(Te(e, r, a, n));
741
+ if (g.isFailure(_)) return yield* h.failCause(f.combine(p.cause, _.cause));
742
+ i.root = void 0, i.created = !1, l = m;
743
+ }
744
+ return yield* h.fail(l);
745
+ }));
746
+ });
747
+ }, Le = (e, t, n, r) => h.scoped(h.gen(function* () {
748
+ let i = yield* h.acquireRelease(h.tryPromise({
749
+ try: async () => "forGeneration" in t ? await t.forGeneration(n).open(r, E.O_RDONLY) : t.openSync(r, E.O_RDONLY),
750
+ catch: (e) => e
751
+ }), (r) => ("forGeneration" in t ? h.tryPromise({
752
+ try: () => t.forGeneration(n).close(r),
753
+ catch: (t) => pe(t, e.fileName, "readFile")
754
+ }) : h.try({
755
+ try: () => t.closeSync(r),
756
+ catch: (t) => pe(t, e.fileName, "readFile")
757
+ })).pipe(h.orDie)), a = yield* h.tryPromise({
758
+ try: async () => "forGeneration" in t ? await t.forGeneration(n).fstat(i) : t.fstatSync(i),
759
+ catch: (e) => e
760
+ });
761
+ if (!Number.isSafeInteger(a.size) || a.size < 0) return yield* h.fail(l(Object.assign(/* @__PURE__ */ Error("File size is outside the supported range"), { code: "EFBIG" }), e.fileName, "readFile"));
762
+ let o = yield* h.try({
763
+ try: () => new Uint8Array(a.size),
764
+ catch: (e) => e
765
+ }), s = 0;
766
+ for (; s < o.byteLength;) {
767
+ let r = Math.min(65536, o.byteLength - s), a = yield* h.tryPromise({
768
+ try: async () => "forGeneration" in t ? await t.forGeneration(n).read(i, r, s) : t.readSync(i, r, s),
769
+ catch: (e) => e
770
+ });
771
+ if (!Number.isSafeInteger(a.read) || a.read <= 0 || a.read > r || a.buffer.byteLength < a.read) return yield* h.fail(l(Object.assign(/* @__PURE__ */ Error("Incomplete whole-file read"), { code: "EIO" }), e.fileName, "readFile"));
772
+ o.set(a.buffer.subarray(0, a.read), s), s += a.read;
773
+ }
774
+ return o;
775
+ })), Y = {
776
+ r: E.O_RDONLY,
777
+ "r+": E.O_RDWR,
778
+ w: E.O_WRONLY | E.O_CREAT | E.O_TRUNC,
779
+ wx: E.O_WRONLY | E.O_CREAT | E.O_TRUNC | E.O_EXCL,
780
+ "w+": E.O_RDWR | E.O_CREAT | E.O_TRUNC,
781
+ "wx+": E.O_RDWR | E.O_CREAT | E.O_TRUNC | E.O_EXCL,
782
+ a: E.O_WRONLY | E.O_CREAT | E.O_APPEND,
783
+ ax: E.O_WRONLY | E.O_CREAT | E.O_APPEND | E.O_EXCL,
784
+ "a+": E.O_RDWR | E.O_CREAT | E.O_APPEND,
785
+ "ax+": E.O_RDWR | E.O_CREAT | E.O_APPEND | E.O_EXCL
786
+ }, X = (e) => typeof e == "number" && Number.isFinite(e) && Number.isInteger(e) && e >= 0 && e <= 4294967295, Re = (e) => {
787
+ if (!(e instanceof Date) && typeof e != "number") return Z("utimes", "time must be a Date or number");
788
+ let t;
789
+ try {
790
+ t = e instanceof Date ? Date.prototype.getTime.call(e) : e * 1e3;
791
+ } catch {
792
+ return Z("utimes", "time must be a valid Date or number");
793
+ }
794
+ return Number.isFinite(t) && Math.abs(t) <= 864e13 ? t : Z("utimes", "time must be a finite value within the supported date range");
795
+ }, ze = (e) => u(e instanceof D ? e.cause : e).code === "ENOENT", Z = (e, t) => b.badArgument({
796
+ module: M,
797
+ method: e,
798
+ description: t
799
+ }), Q = (e) => {
800
+ if (!X(e.mode) || !Number.isSafeInteger(e.size) || e.size < 0) throw Z("stat", "file metadata is outside the supported range");
801
+ let t = (e) => e !== void 0 && Number.isSafeInteger(e) && e >= 0 ? y.some(e) : y.none(), n = (e) => {
802
+ let t = e === void 0 ? void 0 : new Date(e);
803
+ return t && Number.isFinite(t.getTime()) ? y.some(t) : y.none();
804
+ }, r = e.mode & 61440;
805
+ return {
806
+ type: e.is_file ? "File" : e.is_dir ? "Directory" : r === 40960 ? "SymbolicLink" : "Unknown",
807
+ mtime: n(e.mtimeMs ?? e.timestampMs),
808
+ atime: n(e.atimeMs ?? e.timestampMs),
809
+ birthtime: y.none(),
810
+ dev: 0,
811
+ ino: t(e.ino),
812
+ mode: e.mode,
813
+ nlink: t(e.nlink),
814
+ uid: y.none(),
815
+ gid: y.none(),
816
+ rdev: y.none(),
817
+ size: d.bytes(BigInt(e.size)),
818
+ blksize: t(e.blksize).pipe(y.map(d.bytes)),
819
+ blocks: t(e.blocks)
820
+ };
821
+ }, Be = (e, t) => {
822
+ let n = Number(e);
823
+ return Number.isSafeInteger(n) && n >= 0 ? n : Z(t, "offset is outside the supported range");
824
+ }, Ve = (e) => e.state.isClosed(), $ = (e, t, n) => h.scoped(h.gen(function* () {
825
+ yield* h.acquireRelease(w.take(e.cursorGate, 1), () => w.release(e.cursorGate, 1), { interruptible: !0 });
826
+ let r = e.state.terminal();
827
+ return r ? yield* h.fail(P(r, t, e.path, e.state.fileName)) : e.closed || Ve(e) || e.state.currentGeneration() !== e.generation ? yield* h.fail(b.systemError({
828
+ _tag: "BadResource",
829
+ module: M,
830
+ method: t
831
+ })) : yield* n();
832
+ })), He = (t) => {
833
+ let n = e(t);
834
+ if (!n) throw TypeError("Volume service was not created by this adapter");
835
+ return _.make({
836
+ access: (e, t) => t?.ok !== void 0 && typeof t.ok != "boolean" || t?.readable !== void 0 && typeof t.readable != "boolean" || t?.writable !== void 0 && typeof t.writable != "boolean" ? h.fail(Z("access", "ok, readable, and writable must be booleans")) : L(n, "access", e, async (n, r) => {
837
+ let i = await ve(n, r, e), a = !!(i.mode & 292), o = !!(i.mode & 146);
838
+ if (t?.readable && !a || t?.writable && !o) throw Object.assign(/* @__PURE__ */ Error("Permission denied"), { code: "EACCES" });
839
+ }),
840
+ chmod: (e, t) => X(t) ? L(n, "chmod", e, (n, r) => "forGeneration" in n ? n.forGeneration(r).chmod(e, t) : n.chmodSync(e, t), !0) : h.fail(Z("chmod", "mode must be an unsigned 32-bit integer")),
841
+ chown: (e) => h.fail(F("chown", e) ?? he("chown", e, n.fileName)),
842
+ glob: (e, t) => {
843
+ let r = t?.root === void 0 ? void 0 : F("glob", t.root);
844
+ return h.fail(r ?? he("glob", e, n.fileName));
845
+ },
846
+ link: (e, t) => {
847
+ let r = F("link", e), i = F("link", t);
848
+ return r || i ? h.fail(r ?? i) : L(n, "link", e, (n, r) => "forGeneration" in n ? n.forGeneration(r).link(e, t) : n.linkSync(e, t), !0);
849
+ },
850
+ makeDirectory: (e, t) => {
851
+ if (t?.recursive !== void 0 && typeof t.recursive != "boolean") return h.fail(Z("makeDirectory", "recursive must be a boolean"));
852
+ if (t?.mode !== void 0 && !X(t.mode)) return h.fail(Z("makeDirectory", "mode must be an unsigned 32-bit integer"));
853
+ let r = t?.mode, i = t?.recursive ?? !1;
854
+ return L(n, "makeDirectory", e, (t, n) => "forGeneration" in t ? t.forGeneration(n).mkdir(e, {
855
+ mode: r,
856
+ recursive: i
857
+ }) : t.mkdirSync(e, {
858
+ mode: r,
859
+ recursive: i
860
+ }), !0);
861
+ },
862
+ makeTempDirectory: (e) => h.scoped(h.flatMap(h.scope, (t) => h.map(Fe(n, "makeTempDirectory", e, t, !1), (e) => e.path))),
863
+ makeTempDirectoryScoped: (e) => h.flatMap(h.scope, (t) => h.map(Fe(n, "makeTempDirectoryScoped", e, t, !0), (e) => e.path)),
864
+ makeTempFile: (e) => h.scoped(h.flatMap(h.scope, (t) => h.map(Ie(n, "makeTempFile", e, t, !1), (e) => e.path))),
865
+ makeTempFileScoped: (e) => h.flatMap(h.scope, (t) => h.map(Ie(n, "makeTempFileScoped", e, t, !0), (e) => e.path)),
866
+ readFile: (e) => L(n, "readFile", e, (t, r) => {
867
+ let i = () => Le(n, t, r, e);
868
+ return "forGeneration" in t ? h.tryPromise({
869
+ try: () => t.forGeneration(r).readFileBuffer(e, de),
870
+ catch: (e) => e
871
+ }).pipe(h.catch((e) => u(e instanceof D ? e.cause : e).code === "EFBIG" ? i() : h.fail(e))) : i();
872
+ }, !1, !1),
873
+ writeFile: (e, t, r) => {
874
+ let i = F("writeFile", e);
875
+ if (i) return h.fail(i);
876
+ if (!(t instanceof Uint8Array)) return h.fail(b.badArgument({
877
+ module: M,
878
+ method: "writeFile",
879
+ description: "data must be a Uint8Array"
880
+ }));
881
+ let a = r?.flag ?? "w";
882
+ return Object.hasOwn(Y, a) ? r?.mode !== void 0 && !X(r.mode) ? h.fail(Z("writeFile", "mode must be an unsigned 32-bit integer")) : r?.mode !== void 0 || t.byteLength > de || ![
883
+ "w",
884
+ "wx",
885
+ "ax"
886
+ ].includes(a) ? h.scoped(h.gen(function* () {
887
+ yield* (yield* V(n, e, a, r?.mode, (Y[a] & (E.O_CREAT | E.O_TRUNC)) !== 0)).writeAll(t);
888
+ })) : L(n, "writeFile", e, (n, r) => {
889
+ let i = {
890
+ exclusive: a === "wx" || a === "ax",
891
+ append: a === "ax"
892
+ };
893
+ if ("forGeneration" in n) return n.forGeneration(r).writeFileBuffer(e, t, i);
894
+ n.writeFileBufferSync(e, t, i);
895
+ }, !0) : h.fail(Z("writeFile", "unsupported file flag"));
896
+ },
897
+ copy: (e, t, r) => {
898
+ let i = F("copy", e), a = F("copy", t);
899
+ return i || a ? h.fail(i ?? a) : r?.overwrite !== void 0 && typeof r.overwrite != "boolean" ? h.fail(Z("copy", "overwrite must be a boolean")) : r?.preserveTimestamps !== void 0 && typeof r.preserveTimestamps != "boolean" ? h.fail(Z("copy", "preserveTimestamps must be a boolean")) : h.suspend(() => {
900
+ let i = { remaining: n.readinessTimeout };
901
+ return L(n, "copy", e, (a, o) => ke(n, o, e, t, r?.overwrite ?? !1, r?.preserveTimestamps ?? !1, i), !1, !1, void 0, i);
902
+ });
903
+ },
904
+ copyFile: (e, t) => {
905
+ let r = F("copyFile", e), i = F("copyFile", t);
906
+ return r || i ? h.fail(r ?? i) : h.suspend(() => {
907
+ let r = { remaining: n.readinessTimeout };
908
+ return L(n, "copyFile", e, (i, a) => Oe(n, a, e, t, r), !1, !1, void 0, r);
909
+ });
910
+ },
911
+ open: (e, t) => V(n, e, t?.flag ?? "r", t?.mode),
912
+ readDirectory: (e, t) => {
913
+ if (t?.recursive !== void 0 && typeof t.recursive != "boolean") return h.fail(Z("readDirectory", "recursive must be a boolean"));
914
+ let r = t?.recursive ?? !1;
915
+ return h.suspend(() => {
916
+ let t = { remaining: n.readinessTimeout };
917
+ return L(n, "readDirectory", e, (i, a) => {
918
+ let o = (e) => L(n, "readDirectory", e, (t, n) => "forGeneration" in t ? t.forGeneration(n).readdirEntries(e) : t.readdirEntriesSync(e), !1, !1, a, t), s = [], c = (e, t) => h.gen(function* () {
919
+ let n = [...yield* o(e)].sort((e, t) => e.name < t.name ? -1 : +(e.name > t.name));
920
+ for (let i of n) {
921
+ let n = t ? `${t}/${i.name}` : i.name;
922
+ s.push(n), r && i.is_dir && (yield* c(e === "/" ? `/${i.name}` : `${e.replace(/\/$/, "")}/${i.name}`, n));
923
+ }
924
+ });
925
+ return h.gen(function* () {
926
+ return yield* c(e, ""), s;
927
+ });
928
+ }, !1, !1, void 0, t);
929
+ });
930
+ },
931
+ readLink: (e) => L(n, "readLink", e, (t, n) => "forGeneration" in t ? t.forGeneration(n).readlink(e) : t.readlinkSync(e)),
932
+ realPath: (e) => L(n, "realPath", e, (t, n) => "forGeneration" in t ? t.forGeneration(n).realpath(e) : t.realpathSync(e)),
933
+ remove: (e, t) => {
934
+ if (t?.recursive !== void 0 && typeof t.recursive != "boolean") return h.fail(Z("remove", "recursive must be a boolean"));
935
+ if (t?.force !== void 0 && typeof t.force != "boolean") return h.fail(Z("remove", "force must be a boolean"));
936
+ let r = t?.recursive ?? !1, i = t?.force ?? !1;
937
+ return r ? L(n, "remove", e, (t, n) => "forGeneration" in t ? t.forGeneration(n).remove(e) : t.removeSync(e), !0).pipe(h.catchTag("PlatformError", (e) => i && e.reason._tag === "NotFound" ? h.void : h.fail(e))) : L(n, "remove", e, (t, r) => {
938
+ let a = () => "forGeneration" in t ? t.forGeneration(r).lstat(e) : t.lstatSync(e);
939
+ return h.gen(function* () {
940
+ let o = yield* h.tryPromise({
941
+ try: () => Promise.resolve(a()),
942
+ catch: (e) => e
943
+ }).pipe(h.catch((e) => i && ze(e) ? h.succeed(void 0) : h.fail(e)));
944
+ o && (yield* L(n, "remove", e, () => o.is_dir ? "forGeneration" in t ? t.forGeneration(r).rmdir(e) : t.rmdirSync(e) : "forGeneration" in t ? t.forGeneration(r).unlink(e) : t.unlinkSync(e), !0, !1, r).pipe(h.catchTag("PlatformError", (e) => i && e.reason._tag === "NotFound" ? h.void : h.fail(e))));
945
+ });
946
+ }, !1, !1);
947
+ },
948
+ rename: (e, t) => {
949
+ let r = F("rename", e), i = F("rename", t);
950
+ return r || i ? h.fail(r ?? i) : L(n, "rename", e, (n, r) => "forGeneration" in n ? n.forGeneration(r).rename(e, t) : n.renameSync(e, t), !0);
951
+ },
952
+ stat: (e) => L(n, "stat", e, (t, n) => "forGeneration" in t ? t.forGeneration(n).stat(e).then(Q) : Q(t.statSync(e))),
953
+ symlink: (e, t) => {
954
+ let r = F("symlink", t);
955
+ return r ? h.fail(r) : typeof e == "string" ? L(n, "symlink", t, (n, r) => "forGeneration" in n ? n.forGeneration(r).symlink(e, t) : n.symlinkSync(e, t), !0) : h.fail(Z("symlink", "target must be a string"));
956
+ },
957
+ truncate: (e, t = 0) => !Number.isSafeInteger(t) || t < 0 ? h.fail(Z("truncate", "length must be a non-negative safe integer")) : L(n, "truncate", e, (n, r) => "forGeneration" in n ? n.forGeneration(r).truncate(e, t) : n.truncateSync(e, t), !0),
958
+ utimes: (e, t, r) => {
959
+ let i = Re(t), a = Re(r);
960
+ return typeof i == "number" ? typeof a == "number" ? L(n, "utimes", e, (t, n) => "forGeneration" in t ? t.forGeneration(n).utimes(e, i, a) : t.utimesSync(e, i, a), !0) : h.fail(a) : h.fail(i);
961
+ },
962
+ watch: (e, t) => T.suspend(() => {
963
+ let r = F("watch", e);
964
+ if (r) return T.fail(r);
965
+ let i = h.gen(function* () {
966
+ let r = { remaining: n.readinessTimeout }, i = yield* n.awaitReady(r, "watch").pipe(h.mapError((t) => I(n, e, t))), a = yield* q(n, i, e, r).pipe(h.mapError((t) => I(n, e, t)));
967
+ if (G(a)) return yield* h.fail(b.systemError({
968
+ _tag: "BadResource",
969
+ module: M,
970
+ method: "watch",
971
+ pathOrDescriptor: e,
972
+ cause: new c({
973
+ kind: "filesystem",
974
+ fileName: n.fileName,
975
+ operation: "watch",
976
+ path: e,
977
+ code: "ELOOP",
978
+ outcome: "not-applied",
979
+ details: u(Object.assign(/* @__PURE__ */ Error("Cannot watch a symlink target"), { code: "ELOOP" }))
980
+ })
981
+ }));
982
+ if (!a.is_dir && !a.is_file) return yield* h.fail(b.systemError({
983
+ _tag: "BadResource",
984
+ module: M,
985
+ method: "watch",
986
+ pathOrDescriptor: e,
987
+ cause: new c({
988
+ kind: "filesystem",
989
+ fileName: n.fileName,
990
+ operation: "watch",
991
+ path: e,
992
+ code: "EINVAL",
993
+ outcome: "not-applied",
994
+ details: u(Object.assign(/* @__PURE__ */ Error("Target is not a regular file or directory"), { code: "EINVAL" }))
995
+ })
996
+ }));
997
+ let o = {
998
+ generation: i,
999
+ budget: r
1000
+ };
1001
+ return (yield* j(n, {
1002
+ path: e,
1003
+ scope: a.is_dir ? "directory" : "file",
1004
+ recursive: a.is_dir && (t?.recursive ?? !1),
1005
+ content: !1
1006
+ }, o).pipe(h.catchCause((t) => h.failCause(f.map(t, (t) => I(n, e, t)))))).changes.pipe(T.catchCause((t) => T.failCause(f.map(t, (t) => I(n, e, t)))), T.map((e) => {
1007
+ switch (e.type) {
1008
+ case "create": return {
1009
+ _tag: "Create",
1010
+ path: e.path
1011
+ };
1012
+ case "update": return {
1013
+ _tag: "Update",
1014
+ path: e.path
1015
+ };
1016
+ case "delete": return {
1017
+ _tag: "Remove",
1018
+ path: e.path
1019
+ };
1020
+ }
1021
+ }));
1022
+ });
1023
+ return T.scoped(T.unwrap(i.pipe(h.catchCause((t) => h.failCause(f.map(t, (t) => I(n, e, t)))))));
1024
+ })
1025
+ });
1026
+ }, Ue = {
1027
+ make: He,
1028
+ layer: v.effect(_.FileSystem, h.map(r, He))
1029
+ };
1030
+ //#endregion
1031
+ export { i as EncryptionError, a as MountError, Ue as OpfsFileSystem, o as RemoteErrorDetails, s as SubscriptionError, ce as Subscriptions, n as Volume, c as VolumeError };