@mikestools/usefilesystem 0.0.1 → 0.0.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.
@@ -1,60 +1,60 @@
1
- import { shallowRef as ue, computed as N, ref as _ } from "vue";
2
- function fe() {
3
- const n = {
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
- }, o = /* @__PURE__ */ new Set();
10
- function s(e, t, i) {
11
- for (const r of o)
12
- r(e, t, i);
9
+ }, p = /* @__PURE__ */ new Set();
10
+ function b(e, t, r) {
11
+ for (const n of p)
12
+ n(e, t, r);
13
13
  }
14
- function c(e) {
15
- const t = e.split(/[/\\]/).filter((r) => r.length > 0 && r !== "."), i = [];
16
- for (const r of t)
17
- r === ".." ? i.pop() : i.push(r);
18
- return "/" + i.join("/");
14
+ function u(e) {
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
19
  }
20
20
  function l(e) {
21
- const t = c(e);
22
- return { segments: t.split("/").filter((r) => r.length > 0), normalized: t };
21
+ const t = u(e);
22
+ return { segments: t.split("/").filter((n) => n.length > 0), normalized: t };
23
23
  }
24
- function f(e) {
24
+ function y(e) {
25
25
  const { segments: t } = l(e);
26
26
  if (t.length === 0)
27
27
  return { parentPath: "/", name: "" };
28
- const i = t[t.length - 1] ?? "";
29
- return { parentPath: "/" + t.slice(0, -1).join("/"), name: i };
30
- }
31
- function u(e, t = !1) {
32
- const { segments: i } = l(e);
33
- let r = n;
34
- for (const a of i) {
35
- let d = r.directories.get(a);
36
- if (!d) {
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
- d = {
40
- name: a,
41
- path: r.path === "/" ? `/${a}` : `${r.path}/${a}`,
39
+ c = {
40
+ name: 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(a, d), s("create", d.path, "directory");
45
+ }, n.directories.set(o, c), b("create", c.path, "directory");
46
46
  }
47
- r = d;
47
+ n = c;
48
48
  }
49
- return r;
49
+ return n;
50
50
  }
51
- function w(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 h(e) {
57
+ function C(e) {
58
58
  const t = e.split(".").pop()?.toLowerCase() ?? "";
59
59
  return {
60
60
  txt: "text/plain",
@@ -92,482 +92,652 @@ function fe() {
92
92
  webm: "video/webm"
93
93
  }[t] ?? "application/octet-stream";
94
94
  }
95
- function x(e) {
95
+ function k(e) {
96
96
  return new Uint8Array(e);
97
97
  }
98
- function p(e, t, i = {}) {
99
- const { parentPath: r, name: a } = f(e), { normalized: d } = l(e);
100
- if (!a)
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);
119
+ if (!o)
101
120
  return { success: !1, error: "Invalid file path: no filename specified" };
102
- const S = u(r, !0);
103
- if (!S)
104
- return { success: !1, error: `Failed to create parent directory: ${r}` };
105
- const E = S.files.get(a);
106
- if (E && i.overwrite === !1)
107
- return { success: !1, error: `File already exists: ${d}` };
108
- const $ = typeof t == "string" ? w(t) : x(t), H = Date.now(), ae = {
109
- name: a,
110
- path: d,
111
- content: $,
112
- size: $.length,
113
- mimeType: i.mimeType ?? h(a),
114
- createdAt: E?.createdAt ?? H,
115
- modifiedAt: H
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 = {
128
+ name: o,
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 S.files.set(a, ae), s(E ? "modify" : "create", d, "file"), { success: !0 };
136
+ return m.files.set(o, te), b(w ? "modify" : "create", c, "file"), { success: !0 };
118
137
  }
119
- function m(e, t) {
120
- const i = y(e, { encoding: "binary" }), r = typeof t == "string" ? w(t) : t;
121
- if (i === void 0)
122
- return p(e, r);
123
- const a = i, d = new Uint8Array(a.length + r.length);
124
- return d.set(a, 0), d.set(r, a.length), p(e, d);
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 y(e, t = {}) {
127
- const { parentPath: i, name: r } = f(e), d = u(i)?.files.get(r);
128
- return d ? (t.encoding ?? "utf8") === "binary" ? x(d.content) : A(d.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
149
  function T(e) {
131
- const { parentPath: t, name: i } = f(e), a = u(t)?.files.get(i);
132
- if (a)
133
- return {
134
- name: a.name,
135
- path: a.path,
136
- size: a.size,
137
- mimeType: a.mimeType,
138
- createdAt: a.createdAt,
139
- modifiedAt: a.modifiedAt
140
- };
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
+ };
141
167
  }
142
- function U(e) {
143
- return T(e) !== void 0 || b(e);
168
+ function B(e) {
169
+ return T(e) !== void 0 || z(e);
144
170
  }
145
- function v(e) {
171
+ function K(e) {
146
172
  return T(e) !== void 0;
147
173
  }
148
- function b(e) {
149
- return u(e) !== void 0;
174
+ function z(e) {
175
+ return d(e) !== void 0;
150
176
  }
151
- function O(e) {
152
- const { parentPath: t, name: i } = f(e), { normalized: r } = l(e), a = u(t);
153
- return a ? a.files.has(i) ? (a.files.delete(i), s("delete", r, "file"), { success: !0 }) : { success: !1, error: `File not found: ${r}` } : { success: !1, error: `Parent directory not found: ${t}` };
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}` };
154
180
  }
155
- function z(e, t, i = {}) {
156
- const r = y(e, { encoding: "binary" });
157
- if (r === void 0)
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 a = T(e);
160
- return p(t, r, {
161
- mimeType: a?.mimeType,
162
- overwrite: i.overwrite
185
+ const o = T(e);
186
+ return x(t, n, {
187
+ mimeType: o?.mimeType,
188
+ overwrite: r.overwrite
163
189
  });
164
190
  }
165
- function B(e, t, i = {}) {
166
- const r = z(e, t, { overwrite: i.overwrite });
167
- return r.success ? O(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 C(e, t) {
170
- return B(e, t, { overwrite: !1 });
195
+ function Q(e, t) {
196
+ return J(e, t, { overwrite: !1 });
171
197
  }
172
- function R(e) {
173
- return u(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 L(e) {
176
- const { parentPath: t, name: i } = f(e), { normalized: r } = l(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 a = u(t);
180
- if (!a)
205
+ const o = d(t);
206
+ if (!o)
181
207
  return { success: !1, error: `Parent directory not found: ${t}` };
182
- const d = a.directories.get(i);
183
- return d ? d.files.size > 0 || d.directories.size > 0 ? { success: !1, error: `Directory not empty: ${r}` } : (a.directories.delete(i), s("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 D(e) {
186
- const { parentPath: t, name: i } = f(e), { normalized: r } = l(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 a = u(t);
190
- return a ? a.directories.get(i) ? (a.directories.delete(i), s("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 F(e, t = {}) {
193
- const { recursive: i = !1, filesOnly: r = !1, directoriesOnly: a = !1 } = t, d = e === "/" || e === "" ? n : u(e);
194
- if (!d)
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 S = [];
197
- if (!a)
198
- for (const E of d.files.values())
199
- S.push({
200
- name: E.name,
201
- path: E.path,
222
+ const m = [];
223
+ if (!o)
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: E.size
229
+ size: w.size
204
230
  });
205
- if (!r) {
206
- for (const E of d.directories.values())
207
- if (S.push({
208
- name: E.name,
209
- path: E.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
- }), i) {
212
- const $ = F(E.path, t);
213
- S.push(...$);
237
+ }), r) {
238
+ const S = j(w.path, t);
239
+ m.push(...S);
214
240
  }
215
241
  }
216
- return S;
242
+ return m;
217
243
  }
218
- function g(e) {
219
- const t = u(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(i) {
230
- for (const r of i.files.values())
231
- e.push(r);
232
- for (const r of i.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(n), e;
236
- }
237
- function I() {
238
- return P().map((e) => e.path);
239
- }
240
- function ne() {
241
- let e = 0, t = 0, i = 0;
242
- function r(a) {
243
- for (const d of a.files.values())
244
- e++, i += d.size;
245
- for (const d of a.directories.values())
246
- t++, r(d);
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(n), { totalFiles: e, totalDirectories: t, totalSize: i };
281
+ return n(h), { totalFiles: e, totalDirectories: t, totalSize: r };
249
282
  }
250
- function W() {
251
- n.files.clear(), n.directories.clear(), s("delete", "/", "directory");
283
+ function _() {
284
+ h.files.clear(), h.directories.clear(), b("delete", "/", "directory");
252
285
  }
253
- function re(e) {
254
- const t = I(), i = e.replace(/\./g, "\\.").replace(/\*\*/g, "<<<GLOBSTAR>>>").replace(/\*/g, "[^/]*").replace(/<<<GLOBSTAR>>>/g, ".*"), r = new RegExp(`^${i}$`);
255
- return t.filter((a) => r.test(a));
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 oe(e) {
290
+ function ee(e) {
258
291
  const t = e.startsWith(".") ? e : `.${e}`;
259
- return I().filter((i) => i.endsWith(t));
292
+ return P().filter((r) => r.endsWith(t));
260
293
  }
261
- function se(e) {
262
- return o.add(e), () => {
263
- o.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 ie() {
351
+ function s() {
267
352
  const e = {};
268
- for (const t of P()) {
269
- const i = 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: i,
358
+ content: r,
274
359
  mimeType: t.mimeType
275
360
  };
276
361
  }
277
362
  return e;
278
363
  }
279
- function ce(e) {
280
- W();
281
- for (const [t, { content: i, mimeType: r }] of Object.entries(e)) {
282
- const a = atob(i), d = Uint8Array.from(a, (S) => S.charCodeAt(0));
283
- p(t, d, { 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: p,
289
- appendFile: m,
290
- readFile: y,
373
+ writeFile: x,
374
+ appendFile: q,
375
+ readFile: $,
291
376
  stat: T,
292
- exists: U,
293
- isFile: v,
294
- isDirectory: b,
295
- remove: O,
296
- copy: z,
297
- move: B,
298
- rename: C,
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: L,
302
- rmdirRecursive: D,
303
- list: F,
304
- statDirectory: g,
385
+ mkdir: G,
386
+ rmdir: X,
387
+ rmdirRecursive: Z,
388
+ list: j,
389
+ statDirectory: I,
305
390
  // Bulk operations
306
- getAllFiles: P,
307
- getAllPaths: I,
308
- getStats: ne,
309
- clear: W,
391
+ getAllFiles: D,
392
+ getAllPaths: P,
393
+ getStats: V,
394
+ clear: _,
310
395
  // Search
311
- find: re,
312
- findByExtension: oe,
396
+ find: Y,
397
+ findByExtension: ee,
313
398
  // Watchers
314
- watch: se,
399
+ watch: i,
315
400
  // Serialization
316
- toJSON: ie,
317
- fromJSON: ce
401
+ toJSON: s,
402
+ fromJSON: a
318
403
  };
319
404
  }
320
- function he(n = {}) {
321
- const { adapter: o, autoPersist: s = !1 } = n, c = fe(), l = ue(0);
322
- function f() {
323
- l.value++, s && o && h();
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
- c.watch(() => {
326
- f();
430
+ f.watch(() => {
431
+ k();
327
432
  });
328
- const u = N(() => (l.value, c.getStats())), w = N(() => u.value.totalFiles), A = N(() => u.value.totalSize);
329
- async function h() {
330
- if (!o)
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 p = c.toJSON(), m = new TextEncoder().encode(JSON.stringify(p));
333
- await o.save("filesystem", m);
437
+ const i = f.toJSON(), s = new TextEncoder().encode(JSON.stringify(i));
438
+ await p.save("filesystem", s);
334
439
  }
335
- async function x() {
336
- if (!o)
440
+ async function T() {
441
+ if (!p)
337
442
  throw new Error("No storage adapter configured");
338
- const p = await o.load("filesystem");
339
- if (p) {
340
- const m = JSON.parse(new TextDecoder().decode(p));
341
- c.fromJSON(m), f();
443
+ const i = await p.load("filesystem");
444
+ if (i) {
445
+ const s = JSON.parse(new TextDecoder().decode(i));
446
+ f.fromJSON(s), k();
342
447
  }
343
448
  }
344
- return {
345
- ...c,
346
- stats: u,
347
- fileCount: w,
348
- totalSize: A,
349
- persist: h,
350
- restore: x
351
- };
352
- }
353
- const Y = 67324752, G = 33639248, q = 101010256, le = 0, M = 8, de = (function() {
354
- const o = new Uint32Array(256);
355
- for (let s = 0; s < 256; s++) {
356
- let c = s;
357
- for (let l = 0; l < 8; l++)
358
- c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
359
- o[s] = c;
360
- }
361
- return o;
362
- })();
363
- function K(n) {
364
- let o = 4294967295;
365
- for (let s = 0; s < n.length; s++) {
366
- const c = n[s] ?? 0, l = (o ^ c) & 255;
367
- o = o >>> 8 ^ (de[l] ?? 0);
368
- }
369
- return ~o >>> 0;
370
- }
371
- function j(n) {
372
- const o = new ArrayBuffer(n.byteLength);
373
- return new Uint8Array(o).set(n), o;
374
- }
375
- function V(n) {
376
- return new TextEncoder().encode(n);
377
- }
378
- function Q(n) {
379
- return new TextDecoder().decode(n);
380
- }
381
- async function X(n) {
382
- const s = new Blob([j(n)]).stream().pipeThrough(new CompressionStream("deflate-raw")), c = await new Response(s).blob();
383
- return new Uint8Array(await c.arrayBuffer());
384
- }
385
- async function ee(n) {
386
- const s = new Blob([j(n)]).stream().pipeThrough(new DecompressionStream("deflate-raw")), c = await new Response(s).blob();
387
- return new Uint8Array(await c.arrayBuffer());
388
- }
389
- function me(n) {
390
- const o = n.getHours(), s = n.getMinutes(), c = Math.floor(n.getSeconds() / 2), l = o << 11 | s << 5 | c, f = n.getFullYear() - 1980, u = n.getMonth() + 1, w = n.getDate(), A = f << 9 | u << 5 | w;
391
- return { time: l, date: A };
392
- }
393
- function Z(n, o) {
394
- const s = (n & 31) * 2, c = n >> 5 & 63, l = n >> 11 & 31, f = o & 31, u = (o >> 5 & 15) - 1, w = (o >> 9 & 127) + 1980;
395
- return new Date(w, u, f, l, c, s);
396
- }
397
- async function J(n, o = {}) {
398
- const { level: s = "default", comment: c = "" } = o, l = s !== "none", f = [], u = [];
399
- let w = 0;
400
- const h = me(/* @__PURE__ */ new Date());
401
- for (const U of n) {
402
- const v = V(U.path), b = typeof U.content == "string" ? V(U.content) : U.content, O = K(b), z = b.length, B = l ? await X(b) : b, C = B.length, R = l ? M : le, L = new ArrayBuffer(30), D = new DataView(L);
403
- D.setUint32(0, Y, !0), D.setUint16(4, 20, !0), D.setUint16(6, 0, !0), D.setUint16(8, R, !0), D.setUint16(10, h.time, !0), D.setUint16(12, h.date, !0), D.setUint32(14, O, !0), D.setUint32(18, C, !0), D.setUint32(22, z, !0), D.setUint16(26, v.length, !0), D.setUint16(28, 0, !0), f.push(new Uint8Array(L), v, B);
404
- const F = new ArrayBuffer(46), g = new DataView(F);
405
- g.setUint32(0, G, !0), g.setUint16(4, 20, !0), g.setUint16(6, 20, !0), g.setUint16(8, 0, !0), g.setUint16(10, R, !0), g.setUint16(12, h.time, !0), g.setUint16(14, h.date, !0), g.setUint32(16, O, !0), g.setUint32(20, C, !0), g.setUint32(24, z, !0), g.setUint16(28, v.length, !0), g.setUint16(30, 0, !0), g.setUint16(32, 0, !0), g.setUint16(34, 0, !0), g.setUint16(36, 0, !0), g.setUint32(38, 0, !0), g.setUint32(42, w, !0);
406
- const P = new Uint8Array(46 + v.length);
407
- P.set(new Uint8Array(F), 0), P.set(v, 46), u.push(P), w += 30 + v.length + C;
408
- }
409
- const x = u.reduce((U, v) => U + v.length, 0), p = V(c), m = new ArrayBuffer(22), y = new DataView(m);
410
- y.setUint32(0, q, !0), y.setUint16(4, 0, !0), y.setUint16(6, 0, !0), y.setUint16(8, n.length, !0), y.setUint16(10, n.length, !0), y.setUint32(12, x, !0), y.setUint32(16, w, !0), y.setUint16(20, p.length, !0);
411
- const T = [
412
- ...f.map((U) => j(U)),
413
- ...u.map((U) => j(U)),
414
- m,
415
- j(p)
416
- ];
417
- return new Blob(T, { type: "application/zip" });
418
- }
419
- async function pe(n) {
420
- const o = n instanceof Blob ? new Uint8Array(await n.arrayBuffer()) : n, s = [], c = ge(o);
421
- if (!c)
422
- throw new Error("Invalid ZIP file: end of central directory not found");
423
- const l = new DataView(o.buffer, o.byteOffset, o.byteLength);
424
- let f = c.centralDirectoryOffset;
425
- for (let u = 0; u < c.entryCount; u++) {
426
- if (l.getUint32(f, !0) !== G)
427
- throw new Error("Invalid ZIP file: invalid central directory entry");
428
- const A = l.getUint16(f + 10, !0), h = l.getUint16(f + 12, !0), x = l.getUint16(f + 14, !0), p = l.getUint32(f + 20, !0), m = l.getUint32(f + 24, !0), y = l.getUint16(f + 28, !0), T = l.getUint16(f + 30, !0), U = l.getUint16(f + 32, !0), v = o.slice(f + 46, f + 46 + y), b = Q(v);
429
- s.push({
430
- path: b,
431
- compressedSize: p,
432
- uncompressedSize: m,
433
- compressionMethod: A === M ? "deflate" : "store",
434
- isDirectory: b.endsWith("/"),
435
- modifiedAt: Z(h, x)
436
- }), f += 46 + y + T + U;
437
- }
438
- return s;
439
- }
440
- async function te(n, o = {}) {
441
- const { filter: s } = o, c = n instanceof Blob ? new Uint8Array(await n.arrayBuffer()) : n, l = [], f = new DataView(c.buffer, c.byteOffset, c.byteLength);
442
- let u = 0;
443
- for (; u < c.length - 4; ) {
444
- const w = f.getUint32(u, !0);
445
- if (w === Y) {
446
- const A = f.getUint16(u + 8, !0), h = f.getUint16(u + 10, !0), x = f.getUint16(u + 12, !0), p = f.getUint32(u + 14, !0), m = f.getUint32(u + 18, !0), y = f.getUint32(u + 22, !0), T = f.getUint16(u + 26, !0), U = f.getUint16(u + 28, !0), v = c.slice(u + 30, u + 30 + T), b = Q(v), O = u + 30 + T + U, z = c.slice(O, O + m), B = b.endsWith("/"), C = A === M ? "deflate" : "store";
447
- if (s) {
448
- const L = {
449
- path: b,
450
- compressedSize: m,
451
- uncompressedSize: y,
452
- compressionMethod: C,
453
- isDirectory: B,
454
- modifiedAt: Z(h, x)
455
- };
456
- if (!s(L)) {
457
- u = O + m;
458
- continue;
459
- }
460
- }
461
- let R;
462
- B ? R = new Uint8Array(0) : A === M ? R = await ee(z) : R = new Uint8Array(z), l.push({
463
- path: b,
464
- content: R,
465
- compressedSize: m,
466
- uncompressedSize: y,
467
- compressionMethod: C,
468
- crc32: p,
469
- modifiedAt: Z(h, x),
470
- isDirectory: B
471
- }), u = O + m;
472
- } else {
473
- if (w === G)
474
- break;
475
- u++;
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
+ };
476
461
  }
477
462
  }
478
- return l;
479
- }
480
- async function ye(n, o) {
481
- const s = await te(n, {
482
- filter: (c) => c.path === o
483
- });
484
- return s.length > 0 ? s[0]?.content : void 0;
485
- }
486
- function ge(n) {
487
- const o = new DataView(n.buffer, n.byteOffset, n.byteLength);
488
- for (let s = n.length - 22; s >= 0; s--)
489
- if (o.getUint32(s, !0) === q)
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;
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) {
490
568
  return {
491
- entryCount: o.getUint16(s + 10, !0),
492
- centralDirectorySize: o.getUint32(s + 12, !0),
493
- centralDirectoryOffset: o.getUint32(s + 16, !0)
569
+ success: !1,
570
+ error: r instanceof Error ? r.message : "Stream read failed"
494
571
  };
495
- }
496
- function k(n, o) {
497
- const s = URL.createObjectURL(n), c = document.createElement("a");
498
- c.href = s, c.download = o, document.body.appendChild(c), c.click(), document.body.removeChild(c), URL.revokeObjectURL(s);
499
- }
500
- async function Ue(n, o, s = {}) {
501
- const c = await J(n, s);
502
- k(c, o);
503
- }
504
- function ve() {
505
- const n = _(!1), o = _(0), s = _(void 0), c = N(() => ({
506
- isProcessing: n.value,
507
- progress: o.value,
508
- error: s.value
509
- }));
510
- async function l(p) {
511
- n.value = !0, o.value = 0, s.value = void 0;
512
- try {
513
- const m = await p();
514
- return o.value = 100, m;
515
- } catch (m) {
516
- const y = m instanceof Error ? m.message : "Unknown error";
517
- throw s.value = y, m;
518
- } finally {
519
- n.value = !1;
520
572
  }
521
573
  }
522
- async function f(p, m = {}) {
523
- return l(() => J(p, m));
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
+ });
524
582
  }
525
- async function u(p, m = {}) {
526
- return l(() => te(p, m));
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("");
527
588
  }
528
- async function w(p, m) {
529
- return l(() => ye(p, m));
589
+ function Y(i) {
590
+ return C.add(i), () => {
591
+ C.delete(i);
592
+ };
530
593
  }
531
- async function A(p) {
532
- return l(() => pe(p));
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;
533
663
  }
534
- async function h(p, m, y = {}) {
535
- return l(async () => {
536
- const T = await J(p, y);
537
- k(T, m);
538
- });
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;
539
701
  }
540
- function x() {
541
- s.value = void 0;
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);
542
704
  }
543
705
  return {
544
- state: c,
545
- isProcessing: n,
546
- progress: o,
547
- error: s,
548
- create: f,
549
- extract: u,
550
- extractSingle: w,
551
- list: A,
552
- download: h,
553
- downloadBlob: k,
554
- compress: X,
555
- decompress: ee,
556
- computeCrc32: K,
557
- clearError: x
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
+ }
558
737
  };
559
738
  }
560
739
  export {
561
- X as compress,
562
- K as computeCrc32,
563
- fe as createFileSystem,
564
- J as createZip,
565
- ee as decompress,
566
- Ue as downloadAsZip,
567
- k as downloadBlob,
568
- ye as extractFile,
569
- te as extractZip,
570
- pe as listZip,
571
- he as useFileSystem,
572
- ve as useZip
740
+ ce as createFileSystem,
741
+ le as createOPFSAdapter,
742
+ ue as useFileSystem
573
743
  };