@mikestools/usefilesystem 0.0.2 → 0.0.4

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.
@@ -1,60 +1,60 @@
1
- import { shallowRef as Q, computed as b } from "vue";
2
- function V() {
3
- const m = {
1
+ import { shallowRef as se, computed as U } from "vue";
2
+ function ce() {
3
+ const h = {
4
4
  name: "",
5
5
  path: "/",
6
6
  createdAt: Date.now(),
7
7
  files: /* @__PURE__ */ new Map(),
8
8
  directories: /* @__PURE__ */ new Map()
9
- }, l = /* @__PURE__ */ new Set();
10
- function p(e, t, n) {
11
- for (const r of l)
12
- r(e, t, n);
13
- }
14
- function y(e) {
15
- const t = e.split(/[/\\]/).filter((r) => r.length > 0 && r !== "."), n = [];
16
- for (const r of t)
17
- r === ".." ? n.pop() : n.push(r);
18
- return "/" + n.join("/");
9
+ }, p = /* @__PURE__ */ new Set();
10
+ function b(e, t, r) {
11
+ for (const n of p)
12
+ n(e, t, r);
19
13
  }
20
14
  function u(e) {
21
- const t = y(e);
22
- return { segments: t.split("/").filter((r) => r.length > 0), normalized: t };
15
+ const t = e.split(/[/\\]/).filter((n) => n.length > 0 && n !== "."), r = [];
16
+ for (const n of t)
17
+ n === ".." ? r.pop() : r.push(n);
18
+ return "/" + r.join("/");
19
+ }
20
+ function l(e) {
21
+ const t = u(e);
22
+ return { segments: t.split("/").filter((n) => n.length > 0), normalized: t };
23
23
  }
24
- function d(e) {
25
- const { segments: t } = u(e);
24
+ function y(e) {
25
+ const { segments: t } = l(e);
26
26
  if (t.length === 0)
27
27
  return { parentPath: "/", name: "" };
28
- const n = t[t.length - 1] ?? "";
29
- return { parentPath: "/" + t.slice(0, -1).join("/"), name: n };
30
- }
31
- function s(e, t = !1) {
32
- const { segments: n } = u(e);
33
- let r = m;
34
- for (const o of n) {
35
- let i = r.directories.get(o);
36
- if (!i) {
28
+ const r = t[t.length - 1] ?? "";
29
+ return { parentPath: "/" + t.slice(0, -1).join("/"), name: r };
30
+ }
31
+ function d(e, t = !1) {
32
+ const { segments: r } = l(e);
33
+ let n = h;
34
+ for (const o of r) {
35
+ let c = n.directories.get(o);
36
+ if (!c) {
37
37
  if (!t)
38
38
  return;
39
- i = {
39
+ c = {
40
40
  name: o,
41
- path: r.path === "/" ? `/${o}` : `${r.path}/${o}`,
41
+ path: n.path === "/" ? `/${o}` : `${n.path}/${o}`,
42
42
  createdAt: Date.now(),
43
43
  files: /* @__PURE__ */ new Map(),
44
44
  directories: /* @__PURE__ */ new Map()
45
- }, r.directories.set(o, i), p("create", i.path, "directory");
45
+ }, n.directories.set(o, c), b("create", c.path, "directory");
46
46
  }
47
- r = i;
47
+ n = c;
48
48
  }
49
- return r;
49
+ return n;
50
50
  }
51
- function h(e) {
51
+ function f(e) {
52
52
  return new TextEncoder().encode(e);
53
53
  }
54
- function A(e) {
54
+ function R(e) {
55
55
  return new TextDecoder().decode(e);
56
56
  }
57
- function v(e) {
57
+ function C(e) {
58
58
  const t = e.split(".").pop()?.toLowerCase() ?? "";
59
59
  return {
60
60
  txt: "text/plain",
@@ -92,265 +92,652 @@ function V() {
92
92
  webm: "video/webm"
93
93
  }[t] ?? "application/octet-stream";
94
94
  }
95
- function w(e) {
95
+ function k(e) {
96
96
  return new Uint8Array(e);
97
97
  }
98
- function f(e, t, n = {}) {
99
- const { parentPath: r, name: o } = d(e), { normalized: i } = u(e);
98
+ function W(e) {
99
+ const t = e.lastIndexOf(".");
100
+ return t <= 0 || t === e.length - 1 ? "" : e.slice(t);
101
+ }
102
+ function L(e) {
103
+ return e.startsWith("text/") || e === "application/json" || e === "application/xml" || e === "application/javascript" || e === "application/typescript" || e.endsWith("+xml") || e.endsWith("+json");
104
+ }
105
+ function N(e) {
106
+ const { segments: t } = l(e);
107
+ return t.length;
108
+ }
109
+ function M(e) {
110
+ let t = 0;
111
+ for (const r of e.files.values())
112
+ t += r.size;
113
+ for (const r of e.directories.values())
114
+ t += M(r);
115
+ return t;
116
+ }
117
+ function x(e, t, r = {}) {
118
+ const { parentPath: n, name: o } = y(e), { normalized: c } = l(e);
100
119
  if (!o)
101
120
  return { success: !1, error: "Invalid file path: no filename specified" };
102
- const c = s(r, !0);
103
- if (!c)
104
- return { success: !1, error: `Failed to create parent directory: ${r}` };
105
- const a = c.files.get(o);
106
- if (a && n.overwrite === !1)
107
- return { success: !1, error: `File already exists: ${i}` };
108
- const z = typeof t == "string" ? h(t) : w(t), E = Date.now(), K = {
121
+ const m = d(n, !0);
122
+ if (!m)
123
+ return { success: !1, error: `Failed to create parent directory: ${n}` };
124
+ const w = m.files.get(o);
125
+ if (w && r.overwrite === !1)
126
+ return { success: !1, error: `File already exists: ${c}` };
127
+ const S = typeof t == "string" ? f(t) : k(t), A = Date.now(), te = {
109
128
  name: o,
110
- path: i,
111
- content: z,
112
- size: z.length,
113
- mimeType: n.mimeType ?? v(o),
114
- createdAt: a?.createdAt ?? E,
115
- modifiedAt: E
129
+ path: c,
130
+ content: S,
131
+ size: S.length,
132
+ mimeType: r.mimeType ?? C(o),
133
+ createdAt: w?.createdAt ?? A,
134
+ modifiedAt: A
116
135
  };
117
- return c.files.set(o, K), p(a ? "modify" : "create", i, "file"), { success: !0 };
136
+ return m.files.set(o, te), b(w ? "modify" : "create", c, "file"), { success: !0 };
118
137
  }
119
- function g(e, t) {
120
- const n = T(e, { encoding: "binary" }), r = typeof t == "string" ? h(t) : t;
121
- if (n === void 0)
122
- return f(e, r);
123
- const o = n, i = new Uint8Array(o.length + r.length);
124
- return i.set(o, 0), i.set(r, o.length), f(e, i);
138
+ function q(e, t) {
139
+ const r = $(e, { encoding: "binary" }), n = typeof t == "string" ? f(t) : t;
140
+ if (r === void 0)
141
+ return x(e, n);
142
+ const o = r, c = new Uint8Array(o.length + n.length);
143
+ return c.set(o, 0), c.set(n, o.length), x(e, c);
125
144
  }
126
- function T(e, t = {}) {
127
- const { parentPath: n, name: r } = d(e), i = s(n)?.files.get(r);
128
- return i ? (t.encoding ?? "utf8") === "binary" ? w(i.content) : A(i.content) : void 0;
145
+ function $(e, t = {}) {
146
+ const { parentPath: r, name: n } = y(e), c = d(r)?.files.get(n);
147
+ return c ? (t.encoding ?? "utf8") === "binary" ? k(c.content) : R(c.content) : void 0;
129
148
  }
130
- function x(e) {
131
- const { parentPath: t, name: n } = d(e), o = s(t)?.files.get(n);
132
- if (o)
133
- return {
134
- name: o.name,
135
- path: o.path,
136
- size: o.size,
137
- mimeType: o.mimeType,
138
- createdAt: o.createdAt,
139
- modifiedAt: o.modifiedAt
140
- };
141
- }
142
- function N(e) {
143
- return x(e) !== void 0 || $(e);
149
+ function T(e) {
150
+ const { parentPath: t, name: r } = y(e), o = d(t)?.files.get(r);
151
+ if (!o)
152
+ return;
153
+ const c = W(o.name), m = L(o.mimeType);
154
+ return {
155
+ name: o.name,
156
+ path: o.path,
157
+ size: o.size,
158
+ mimeType: o.mimeType,
159
+ createdAt: o.createdAt,
160
+ modifiedAt: o.modifiedAt,
161
+ extension: c,
162
+ isText: m,
163
+ isBinary: !m,
164
+ parentPath: t,
165
+ depth: N(o.path)
166
+ };
144
167
  }
145
168
  function B(e) {
146
- return x(e) !== void 0;
169
+ return T(e) !== void 0 || z(e);
147
170
  }
148
- function $(e) {
149
- return s(e) !== void 0;
171
+ function K(e) {
172
+ return T(e) !== void 0;
150
173
  }
151
- function D(e) {
152
- const { parentPath: t, name: n } = d(e), { normalized: r } = u(e), o = s(t);
153
- return o ? o.files.has(n) ? (o.files.delete(n), p("delete", r, "file"), { success: !0 }) : { success: !1, error: `File not found: ${r}` } : { success: !1, error: `Parent directory not found: ${t}` };
174
+ function z(e) {
175
+ return d(e) !== void 0;
154
176
  }
155
- function j(e, t, n = {}) {
156
- const r = T(e, { encoding: "binary" });
157
- if (r === void 0)
177
+ function E(e) {
178
+ const { parentPath: t, name: r } = y(e), { normalized: n } = l(e), o = d(t);
179
+ return o ? o.files.has(r) ? (o.files.delete(r), b("delete", n, "file"), { success: !0 }) : { success: !1, error: `File not found: ${n}` } : { success: !1, error: `Parent directory not found: ${t}` };
180
+ }
181
+ function H(e, t, r = {}) {
182
+ const n = $(e, { encoding: "binary" });
183
+ if (n === void 0)
158
184
  return { success: !1, error: `Source file not found: ${e}` };
159
- const o = x(e);
160
- return f(t, r, {
185
+ const o = T(e);
186
+ return x(t, n, {
161
187
  mimeType: o?.mimeType,
162
- overwrite: n.overwrite
188
+ overwrite: r.overwrite
163
189
  });
164
190
  }
165
- function F(e, t, n = {}) {
166
- const r = j(e, t, { overwrite: n.overwrite });
167
- return r.success ? D(e) : r;
191
+ function J(e, t, r = {}) {
192
+ const n = H(e, t, { overwrite: r.overwrite });
193
+ return n.success ? E(e) : n;
168
194
  }
169
- function J(e, t) {
170
- return F(e, t, { overwrite: !1 });
195
+ function Q(e, t) {
196
+ return J(e, t, { overwrite: !1 });
171
197
  }
172
- function R(e) {
173
- return s(e, !0) ? { success: !0 } : { success: !1, error: `Failed to create directory: ${e}` };
198
+ function G(e) {
199
+ return d(e, !0) ? { success: !0 } : { success: !1, error: `Failed to create directory: ${e}` };
174
200
  }
175
- function M(e) {
176
- const { parentPath: t, name: n } = d(e), { normalized: r } = u(e);
177
- if (r === "/")
201
+ function X(e) {
202
+ const { parentPath: t, name: r } = y(e), { normalized: n } = l(e);
203
+ if (n === "/")
178
204
  return { success: !1, error: "Cannot remove root directory" };
179
- const o = s(t);
205
+ const o = d(t);
180
206
  if (!o)
181
207
  return { success: !1, error: `Parent directory not found: ${t}` };
182
- const i = o.directories.get(n);
183
- return i ? i.files.size > 0 || i.directories.size > 0 ? { success: !1, error: `Directory not empty: ${r}` } : (o.directories.delete(n), p("delete", r, "directory"), { success: !0 }) : { success: !1, error: `Directory not found: ${r}` };
208
+ const c = o.directories.get(r);
209
+ return c ? c.files.size > 0 || c.directories.size > 0 ? { success: !1, error: `Directory not empty: ${n}` } : (o.directories.delete(r), b("delete", n, "directory"), { success: !0 }) : { success: !1, error: `Directory not found: ${n}` };
184
210
  }
185
- function U(e) {
186
- const { parentPath: t, name: n } = d(e), { normalized: r } = u(e);
187
- if (r === "/")
211
+ function Z(e) {
212
+ const { parentPath: t, name: r } = y(e), { normalized: n } = l(e);
213
+ if (n === "/")
188
214
  return { success: !1, error: "Cannot remove root directory" };
189
- const o = s(t);
190
- return o ? o.directories.get(n) ? (o.directories.delete(n), p("delete", r, "directory"), { success: !0 }) : { success: !1, error: `Directory not found: ${r}` } : { success: !1, error: `Parent directory not found: ${t}` };
215
+ const o = d(t);
216
+ return o ? o.directories.get(r) ? (o.directories.delete(r), b("delete", n, "directory"), { success: !0 }) : { success: !1, error: `Directory not found: ${n}` } : { success: !1, error: `Parent directory not found: ${t}` };
191
217
  }
192
- function O(e, t = {}) {
193
- const { recursive: n = !1, filesOnly: r = !1, directoriesOnly: o = !1 } = t, i = e === "/" || e === "" ? m : s(e);
194
- if (!i)
218
+ function j(e, t = {}) {
219
+ const { recursive: r = !1, filesOnly: n = !1, directoriesOnly: o = !1 } = t, c = e === "/" || e === "" ? h : d(e);
220
+ if (!c)
195
221
  return [];
196
- const c = [];
222
+ const m = [];
197
223
  if (!o)
198
- for (const a of i.files.values())
199
- c.push({
200
- name: a.name,
201
- path: a.path,
224
+ for (const w of c.files.values())
225
+ m.push({
226
+ name: w.name,
227
+ path: w.path,
202
228
  type: "file",
203
- size: a.size
229
+ size: w.size
204
230
  });
205
- if (!r) {
206
- for (const a of i.directories.values())
207
- if (c.push({
208
- name: a.name,
209
- path: a.path,
231
+ if (!n) {
232
+ for (const w of c.directories.values())
233
+ if (m.push({
234
+ name: w.name,
235
+ path: w.path,
210
236
  type: "directory"
211
- }), n) {
212
- const z = O(a.path, t);
213
- c.push(...z);
237
+ }), r) {
238
+ const S = j(w.path, t);
239
+ m.push(...S);
214
240
  }
215
241
  }
216
- return c;
242
+ return m;
217
243
  }
218
- function L(e) {
219
- const t = s(e);
220
- if (t)
221
- return {
222
- name: t.name,
223
- path: t.path,
224
- createdAt: t.createdAt
225
- };
244
+ function I(e) {
245
+ const { normalized: t } = l(e), r = t === "/" ? h : d(e);
246
+ if (!r)
247
+ return;
248
+ const { parentPath: n } = y(t);
249
+ return {
250
+ name: r.name || "/",
251
+ path: r.path,
252
+ createdAt: r.createdAt,
253
+ parentPath: t === "/" ? "/" : n,
254
+ depth: N(r.path),
255
+ fileCount: r.files.size,
256
+ directoryCount: r.directories.size,
257
+ totalSize: M(r)
258
+ };
226
259
  }
227
- function P() {
260
+ function D() {
228
261
  const e = [];
229
- function t(n) {
230
- for (const r of n.files.values())
231
- e.push(r);
232
- for (const r of n.directories.values())
233
- t(r);
262
+ function t(r) {
263
+ for (const n of r.files.values())
264
+ e.push(n);
265
+ for (const n of r.directories.values())
266
+ t(n);
234
267
  }
235
- return t(m), e;
236
- }
237
- function S() {
238
- return P().map((e) => e.path);
239
- }
240
- function G() {
241
- let e = 0, t = 0, n = 0;
242
- function r(o) {
243
- for (const i of o.files.values())
244
- e++, n += i.size;
245
- for (const i of o.directories.values())
246
- t++, r(i);
268
+ return t(h), e;
269
+ }
270
+ function P() {
271
+ return D().map((e) => e.path);
272
+ }
273
+ function V() {
274
+ let e = 0, t = 0, r = 0;
275
+ function n(o) {
276
+ for (const c of o.files.values())
277
+ e++, r += c.size;
278
+ for (const c of o.directories.values())
279
+ t++, n(c);
247
280
  }
248
- return r(m), { totalFiles: e, totalDirectories: t, totalSize: n };
281
+ return n(h), { totalFiles: e, totalDirectories: t, totalSize: r };
249
282
  }
250
- function C() {
251
- m.files.clear(), m.directories.clear(), p("delete", "/", "directory");
283
+ function _() {
284
+ h.files.clear(), h.directories.clear(), b("delete", "/", "directory");
252
285
  }
253
- function W(e) {
254
- const t = S(), n = e.replace(/\./g, "\\.").replace(/\*\*/g, "<<<GLOBSTAR>>>").replace(/\*/g, "[^/]*").replace(/<<<GLOBSTAR>>>/g, ".*"), r = new RegExp(`^${n}$`);
255
- return t.filter((o) => r.test(o));
286
+ function Y(e) {
287
+ const t = P(), r = e.replace(/\./g, "\\.").replace(/\*\*/g, "<<<GLOBSTAR>>>").replace(/\*/g, "[^/]*").replace(/<<<GLOBSTAR>>>/g, ".*"), n = new RegExp(`^${r}$`);
288
+ return t.filter((o) => n.test(o));
256
289
  }
257
- function k(e) {
290
+ function ee(e) {
258
291
  const t = e.startsWith(".") ? e : `.${e}`;
259
- return S().filter((n) => n.endsWith(t));
292
+ return P().filter((r) => r.endsWith(t));
260
293
  }
261
- function I(e) {
262
- return l.add(e), () => {
263
- l.delete(e);
294
+ function i(e, t) {
295
+ if (!t) {
296
+ const v = e;
297
+ return p.add(v), () => {
298
+ p.delete(v);
299
+ };
300
+ }
301
+ const {
302
+ pattern: r,
303
+ events: n,
304
+ type: o = "all",
305
+ debounce: c,
306
+ path: m,
307
+ recursive: w = !1
308
+ } = t;
309
+ let S, A = [];
310
+ function te(v, g) {
311
+ const O = g.replace(/\./g, "\\.").replace(/\*\*/g, "<<<GLOBSTAR>>>").replace(/\*/g, "[^/]*").replace(/<<<GLOBSTAR>>>/g, ".*");
312
+ return new RegExp(`^${O}$`).test(v);
313
+ }
314
+ function oe(v, g, O) {
315
+ if (n && !n.includes(v) || o !== "all" && o !== O || r && !te(g, r))
316
+ return !1;
317
+ if (m) {
318
+ const F = m.endsWith("/") ? m.slice(0, -1) : m;
319
+ if (w) {
320
+ if (!g.startsWith(F + "/") && g !== F)
321
+ return !1;
322
+ } else if ((g.substring(0, g.lastIndexOf("/")) || "/") !== F)
323
+ return !1;
324
+ }
325
+ return !0;
326
+ }
327
+ function ie() {
328
+ const v = e;
329
+ for (const g of A)
330
+ v(g);
331
+ A = [];
332
+ }
333
+ const ne = (v, g, O) => {
334
+ if (!oe(v, g, O))
335
+ return;
336
+ let F;
337
+ v !== "delete" && (O === "file" ? F = T(g) : F = I(g));
338
+ const re = {
339
+ event: v,
340
+ path: g,
341
+ type: O,
342
+ timestamp: Date.now(),
343
+ metadata: F
344
+ };
345
+ c !== void 0 && c > 0 ? (A.push(re), S && clearTimeout(S), S = setTimeout(ie, c)) : e(re);
346
+ };
347
+ return p.add(ne), () => {
348
+ p.delete(ne), S && clearTimeout(S), A = [];
264
349
  };
265
350
  }
266
- function q() {
351
+ function s() {
267
352
  const e = {};
268
- for (const t of P()) {
269
- const n = btoa(
270
- Array.from(t.content, (r) => String.fromCharCode(r)).join("")
353
+ for (const t of D()) {
354
+ const r = btoa(
355
+ Array.from(t.content, (n) => String.fromCharCode(n)).join("")
271
356
  );
272
357
  e[t.path] = {
273
- content: n,
358
+ content: r,
274
359
  mimeType: t.mimeType
275
360
  };
276
361
  }
277
362
  return e;
278
363
  }
279
- function H(e) {
280
- C();
281
- for (const [t, { content: n, mimeType: r }] of Object.entries(e)) {
282
- const o = atob(n), i = Uint8Array.from(o, (c) => c.charCodeAt(0));
283
- f(t, i, { mimeType: r });
364
+ function a(e) {
365
+ _();
366
+ for (const [t, { content: r, mimeType: n }] of Object.entries(e)) {
367
+ const o = atob(r), c = Uint8Array.from(o, (m) => m.charCodeAt(0));
368
+ x(t, c, { mimeType: n });
284
369
  }
285
370
  }
286
371
  return {
287
372
  // File operations
288
- writeFile: f,
289
- appendFile: g,
290
- readFile: T,
291
- stat: x,
292
- exists: N,
293
- isFile: B,
294
- isDirectory: $,
295
- remove: D,
296
- copy: j,
297
- move: F,
298
- rename: J,
373
+ writeFile: x,
374
+ appendFile: q,
375
+ readFile: $,
376
+ stat: T,
377
+ exists: B,
378
+ isFile: K,
379
+ isDirectory: z,
380
+ remove: E,
381
+ copy: H,
382
+ move: J,
383
+ rename: Q,
299
384
  // Directory operations
300
- mkdir: R,
301
- rmdir: M,
302
- rmdirRecursive: U,
303
- list: O,
304
- statDirectory: L,
385
+ mkdir: G,
386
+ rmdir: X,
387
+ rmdirRecursive: Z,
388
+ list: j,
389
+ statDirectory: I,
305
390
  // Bulk operations
306
- getAllFiles: P,
307
- getAllPaths: S,
308
- getStats: G,
309
- clear: C,
391
+ getAllFiles: D,
392
+ getAllPaths: P,
393
+ getStats: V,
394
+ clear: _,
310
395
  // Search
311
- find: W,
312
- findByExtension: k,
396
+ find: Y,
397
+ findByExtension: ee,
313
398
  // Watchers
314
- watch: I,
399
+ watch: i,
315
400
  // Serialization
316
- toJSON: q,
317
- fromJSON: H
401
+ toJSON: s,
402
+ fromJSON: a
318
403
  };
319
404
  }
320
- function Y(m = {}) {
321
- const { adapter: l, autoPersist: p = !1 } = m, y = V(), u = Q(0);
322
- function d() {
323
- u.value++, p && l && v();
405
+ function ue(h = {}) {
406
+ const {
407
+ adapter: p,
408
+ autoPersist: b = !1,
409
+ maxSize: u,
410
+ maxFiles: l,
411
+ maxFileSize: y,
412
+ warnAtPercentage: d = 90
413
+ } = h, f = ce(), R = se(0), C = /* @__PURE__ */ new Set();
414
+ function k() {
415
+ R.value++, b && p && $();
416
+ const i = W();
417
+ if (i.percentage >= d)
418
+ for (const s of C)
419
+ s(i);
420
+ }
421
+ function W() {
422
+ const s = f.getStats().totalSize, a = u ?? Number.MAX_SAFE_INTEGER, e = Math.max(0, a - s), t = u ? s / u * 100 : 0;
423
+ return {
424
+ quota: a,
425
+ usage: s,
426
+ available: e,
427
+ percentage: Math.min(100, t)
428
+ };
324
429
  }
325
- y.watch(() => {
326
- d();
430
+ f.watch(() => {
431
+ k();
327
432
  });
328
- const s = b(() => (u.value, y.getStats())), h = b(() => s.value.totalFiles), A = b(() => s.value.totalSize);
329
- async function v() {
330
- if (!l)
433
+ const L = U(() => (R.value, f.getStats())), N = U(() => L.value.totalFiles), M = U(() => L.value.totalSize), x = U(() => (R.value, W())), q = U(() => x.value.percentage >= d);
434
+ async function $() {
435
+ if (!p)
331
436
  throw new Error("No storage adapter configured");
332
- const f = y.toJSON(), g = new TextEncoder().encode(JSON.stringify(f));
333
- await l.save("filesystem", g);
437
+ const i = f.toJSON(), s = new TextEncoder().encode(JSON.stringify(i));
438
+ await p.save("filesystem", s);
334
439
  }
335
- async function w() {
336
- if (!l)
440
+ async function T() {
441
+ if (!p)
337
442
  throw new Error("No storage adapter configured");
338
- const f = await l.load("filesystem");
339
- if (f) {
340
- const g = JSON.parse(new TextDecoder().decode(f));
341
- y.fromJSON(g), d();
443
+ const i = await p.load("filesystem");
444
+ if (i) {
445
+ const s = JSON.parse(new TextDecoder().decode(i));
446
+ f.fromJSON(s), k();
447
+ }
448
+ }
449
+ async function B(i, s) {
450
+ const a = s ?? `/${i.name}`;
451
+ try {
452
+ const e = await i.arrayBuffer(), t = new Uint8Array(e);
453
+ return f.writeFile(a, t, {
454
+ mimeType: i.type || void 0
455
+ });
456
+ } catch (e) {
457
+ return {
458
+ success: !1,
459
+ error: e instanceof Error ? e.message : "Failed to import file"
460
+ };
461
+ }
462
+ }
463
+ async function K(i, s = "/") {
464
+ const e = (s.endsWith("/") ? s.slice(0, -1) : s) || "";
465
+ for (const t of Array.from(i)) {
466
+ const r = e === "" ? `/${t.name}` : `${e}/${t.name}`, n = await B(t, r);
467
+ if (!n.success)
468
+ return n;
342
469
  }
470
+ return { success: !0 };
471
+ }
472
+ function z(i) {
473
+ return i.byteOffset === 0 && i.byteLength === i.buffer.byteLength ? i.buffer : i.buffer.slice(i.byteOffset, i.byteOffset + i.byteLength);
474
+ }
475
+ function E(i) {
476
+ const s = f.readFile(i, { encoding: "binary" });
477
+ if (s === void 0) return;
478
+ const a = f.stat(i);
479
+ return new Blob([z(s)], { type: a?.mimeType });
480
+ }
481
+ function H(i) {
482
+ const s = f.readFile(i, { encoding: "binary" });
483
+ if (s === void 0) return;
484
+ const a = f.stat(i);
485
+ if (a)
486
+ return new File([z(s)], a.name, {
487
+ type: a.mimeType,
488
+ lastModified: a.modifiedAt
489
+ });
490
+ }
491
+ function J(i) {
492
+ const s = E(i);
493
+ if (s)
494
+ return URL.createObjectURL(s);
495
+ }
496
+ function Q(i) {
497
+ URL.revokeObjectURL(i);
498
+ }
499
+ function G(i, s) {
500
+ const a = E(i);
501
+ if (!a) return !1;
502
+ const e = f.stat(i), t = s ?? e?.name ?? "download", r = URL.createObjectURL(a), n = document.createElement("a");
503
+ return n.href = r, n.download = t, n.style.display = "none", document.body.appendChild(n), n.click(), document.body.removeChild(n), setTimeout(() => URL.revokeObjectURL(r), 1e3), !0;
504
+ }
505
+ function X(i) {
506
+ i.forEach((s, a) => {
507
+ setTimeout(() => G(s), a * 100);
508
+ });
509
+ }
510
+ const Z = 64 * 1024, j = 3;
511
+ function I(i, s = {}) {
512
+ const a = f.readFile(i, { encoding: "binary" });
513
+ if (a === void 0) return;
514
+ const e = s.chunkSize ?? Z, t = s.highWaterMark ?? j;
515
+ let r = 0;
516
+ return new ReadableStream(
517
+ {
518
+ pull(n) {
519
+ if (r >= a.length) {
520
+ n.close();
521
+ return;
522
+ }
523
+ const o = a.slice(r, r + e);
524
+ n.enqueue(o), r += e;
525
+ }
526
+ },
527
+ new CountQueuingStrategy({ highWaterMark: t })
528
+ );
529
+ }
530
+ function D(i, s = {}) {
531
+ const a = [], e = s.highWaterMark ?? j;
532
+ return new WritableStream(
533
+ {
534
+ write(t) {
535
+ a.push(t);
536
+ },
537
+ close() {
538
+ const t = a.reduce((o, c) => o + c.length, 0), r = new Uint8Array(t);
539
+ let n = 0;
540
+ for (const o of a)
541
+ r.set(o, n), n += o.length;
542
+ f.writeFile(i, r, {
543
+ mimeType: s.mimeType,
544
+ overwrite: s.overwrite
545
+ });
546
+ },
547
+ abort() {
548
+ a.length = 0;
549
+ }
550
+ },
551
+ new CountQueuingStrategy({ highWaterMark: e })
552
+ );
553
+ }
554
+ async function P(i, s, a = {}) {
555
+ const e = [], t = new WritableStream({
556
+ write(r) {
557
+ e.push(r);
558
+ }
559
+ });
560
+ try {
561
+ await s.pipeTo(t);
562
+ const r = e.reduce((c, m) => c + m.length, 0), n = new Uint8Array(r);
563
+ let o = 0;
564
+ for (const c of e)
565
+ n.set(c, o), o += c.length;
566
+ return f.writeFile(i, n, a);
567
+ } catch (r) {
568
+ return {
569
+ success: !1,
570
+ error: r instanceof Error ? r.message : "Stream read failed"
571
+ };
572
+ }
573
+ }
574
+ function V(i, s = {}) {
575
+ const a = f.readFile(i, { encoding: "binary" });
576
+ if (a === void 0) return;
577
+ const e = f.stat(i), t = new Headers(s.headers);
578
+ return e?.mimeType && !t.has("Content-Type") && t.set("Content-Type", e.mimeType), t.set("Content-Length", String(a.length)), new Response(z(a), {
579
+ ...s,
580
+ headers: t
581
+ });
582
+ }
583
+ async function _(i) {
584
+ const s = f.readFile(i, { encoding: "binary" });
585
+ if (s === void 0) return;
586
+ const a = await crypto.subtle.digest("SHA-256", z(s));
587
+ return Array.from(new Uint8Array(a)).map((t) => t.toString(16).padStart(2, "0")).join("");
588
+ }
589
+ function Y(i) {
590
+ return C.add(i), () => {
591
+ C.delete(i);
592
+ };
593
+ }
594
+ function ee() {
595
+ const i = [];
596
+ let s = !1, a = !1;
597
+ const e = {
598
+ writeFile(t, r, n) {
599
+ return s || a || i.push({ type: "writeFile", path: t, content: r, options: n }), e;
600
+ },
601
+ remove(t) {
602
+ return s || a || i.push({ type: "remove", path: t }), e;
603
+ },
604
+ mkdir(t) {
605
+ return s || a || i.push({ type: "mkdir", path: t }), e;
606
+ },
607
+ copy(t, r) {
608
+ return s || a || i.push({ type: "copy", source: t, destination: r }), e;
609
+ },
610
+ move(t, r) {
611
+ return s || a || i.push({ type: "move", source: t, destination: r }), e;
612
+ },
613
+ commit() {
614
+ if (s)
615
+ return { success: !1, error: "Transaction already committed" };
616
+ if (a)
617
+ return { success: !1, error: "Transaction was rolled back" };
618
+ const t = f.toJSON();
619
+ for (const r of i) {
620
+ let n;
621
+ switch (r.type) {
622
+ case "writeFile":
623
+ if (y !== void 0) {
624
+ const o = typeof r.content == "string" ? new TextEncoder().encode(r.content).length : r.content.length;
625
+ if (o > y)
626
+ return f.fromJSON(t), { success: !1, error: `File size ${o} exceeds maximum ${y}` };
627
+ }
628
+ if (l !== void 0) {
629
+ const o = f.getStats().totalFiles;
630
+ if ((!f.exists(r.path) || !f.isFile(r.path)) && o >= l)
631
+ return f.fromJSON(t), { success: !1, error: `Maximum file count ${l} reached` };
632
+ }
633
+ n = f.writeFile(r.path, r.content, r.options);
634
+ break;
635
+ case "remove":
636
+ n = f.remove(r.path);
637
+ break;
638
+ case "mkdir":
639
+ n = f.mkdir(r.path);
640
+ break;
641
+ case "copy":
642
+ n = f.copy(r.source, r.destination);
643
+ break;
644
+ case "move":
645
+ n = f.move(r.source, r.destination);
646
+ break;
647
+ }
648
+ if (!n.success)
649
+ return f.fromJSON(t), { success: !1, error: `Transaction failed at ${r.type}: ${n.error}` };
650
+ if (u !== void 0) {
651
+ const o = f.getStats().totalSize;
652
+ if (o > u)
653
+ return f.fromJSON(t), { success: !1, error: `Total size ${o} exceeds maximum ${u}` };
654
+ }
655
+ }
656
+ return s = !0, k(), { success: !0 };
657
+ },
658
+ rollback() {
659
+ !s && !a && (a = !0, i.length = 0);
660
+ }
661
+ };
662
+ return e;
663
+ }
664
+ return {
665
+ ...f,
666
+ stats: L,
667
+ fileCount: N,
668
+ totalSize: M,
669
+ persist: $,
670
+ restore: T,
671
+ // Upload methods
672
+ upload: B,
673
+ uploads: K,
674
+ // Export/conversion methods
675
+ toBlob: E,
676
+ toFile: H,
677
+ createObjectURL: J,
678
+ revokeObjectURL: Q,
679
+ // Download methods
680
+ download: G,
681
+ downloads: X,
682
+ // Web Streams API methods
683
+ createReadStream: I,
684
+ createWriteStream: D,
685
+ writeFromStream: P,
686
+ toResponse: V,
687
+ // Utility methods
688
+ computeChecksum: _,
689
+ // Storage quota management
690
+ storageEstimate: x,
691
+ isNearCapacity: q,
692
+ onStorageWarning: Y,
693
+ // Transactions
694
+ beginTransaction: ee
695
+ };
696
+ }
697
+ function le() {
698
+ let h;
699
+ async function p() {
700
+ return h || (h = await navigator.storage.getDirectory()), h;
701
+ }
702
+ function b(u) {
703
+ return u.byteOffset === 0 && u.byteLength === u.buffer.byteLength ? u.buffer : u.buffer.slice(u.byteOffset, u.byteOffset + u.byteLength);
343
704
  }
344
705
  return {
345
- ...y,
346
- stats: s,
347
- fileCount: h,
348
- totalSize: A,
349
- persist: v,
350
- restore: w
706
+ async save(u, l) {
707
+ const f = await (await (await p()).getFileHandle(u, { create: !0 })).createWritable();
708
+ await f.write(b(l)), await f.close();
709
+ },
710
+ async load(u) {
711
+ try {
712
+ const f = await (await (await (await p()).getFileHandle(u)).getFile()).arrayBuffer();
713
+ return new Uint8Array(f);
714
+ } catch {
715
+ return;
716
+ }
717
+ },
718
+ async remove(u) {
719
+ try {
720
+ await (await p()).removeEntry(u);
721
+ } catch {
722
+ }
723
+ },
724
+ async list() {
725
+ const u = await p(), l = [], y = u;
726
+ for await (const [d] of y)
727
+ l.push(d);
728
+ return l;
729
+ },
730
+ async clear() {
731
+ const u = await p(), l = [], y = u;
732
+ for await (const [d] of y)
733
+ l.push(d);
734
+ for (const d of l)
735
+ await u.removeEntry(d, { recursive: !0 });
736
+ }
351
737
  };
352
738
  }
353
739
  export {
354
- V as createFileSystem,
355
- Y as useFileSystem
740
+ ce as createFileSystem,
741
+ le as createOPFSAdapter,
742
+ ue as useFileSystem
356
743
  };