@zenfs/core 0.16.4 → 0.17.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.
Files changed (74) hide show
  1. package/dist/backends/backend.d.ts +3 -4
  2. package/dist/backends/fetch.d.ts +8 -3
  3. package/dist/backends/fetch.js +3 -2
  4. package/dist/backends/{index/fs.d.ts → file_index.d.ts} +49 -10
  5. package/dist/backends/{index/fs.js → file_index.js} +84 -5
  6. package/dist/backends/memory.d.ts +6 -1
  7. package/dist/backends/memory.js +2 -1
  8. package/dist/backends/overlay.d.ts +16 -16
  9. package/dist/backends/overlay.js +59 -82
  10. package/dist/backends/port/fs.d.ts +6 -2
  11. package/dist/backends/port/fs.js +4 -2
  12. package/dist/backends/store/fs.js +484 -304
  13. package/dist/backends/store/simple.js +5 -1
  14. package/dist/backends/store/store.d.ts +4 -1
  15. package/dist/backends/store/store.js +9 -5
  16. package/dist/browser.min.js +4 -4
  17. package/dist/browser.min.js.map +4 -4
  18. package/dist/config.d.ts +3 -3
  19. package/dist/emulation/async.d.ts +0 -3
  20. package/dist/emulation/async.js +6 -2
  21. package/dist/emulation/dir.d.ts +4 -0
  22. package/dist/emulation/dir.js +8 -6
  23. package/dist/emulation/promises.d.ts +1 -3
  24. package/dist/emulation/promises.js +25 -2
  25. package/dist/emulation/sync.js +0 -1
  26. package/dist/emulation/watchers.d.ts +9 -4
  27. package/dist/emulation/watchers.js +7 -0
  28. package/dist/file.d.ts +17 -1
  29. package/dist/file.js +86 -1
  30. package/dist/filesystem.d.ts +0 -63
  31. package/dist/filesystem.js +0 -311
  32. package/dist/index.d.ts +1 -2
  33. package/dist/index.js +1 -2
  34. package/dist/mixins/async.d.ts +39 -0
  35. package/dist/mixins/async.js +216 -0
  36. package/dist/mixins/mutexed.d.ts +33 -0
  37. package/dist/mixins/mutexed.js +465 -0
  38. package/dist/mixins/readonly.d.ts +25 -0
  39. package/dist/mixins/readonly.js +57 -0
  40. package/dist/mixins/shared.d.ts +12 -0
  41. package/dist/mixins/shared.js +4 -0
  42. package/dist/mixins/sync.d.ts +6 -0
  43. package/dist/mixins/sync.js +43 -0
  44. package/package.json +1 -1
  45. package/src/backends/backend.ts +3 -4
  46. package/src/backends/fetch.ts +7 -3
  47. package/src/backends/{index/fs.ts → file_index.ts} +106 -8
  48. package/src/backends/memory.ts +5 -1
  49. package/src/backends/overlay.ts +64 -90
  50. package/src/backends/port/fs.ts +7 -2
  51. package/src/backends/{index/readme.md → readme.md} +1 -1
  52. package/src/backends/store/fs.ts +97 -155
  53. package/src/backends/store/simple.ts +5 -1
  54. package/src/backends/store/store.ts +10 -5
  55. package/src/config.ts +3 -1
  56. package/src/emulation/async.ts +15 -6
  57. package/src/emulation/dir.ts +19 -16
  58. package/src/emulation/promises.ts +28 -6
  59. package/src/emulation/sync.ts +1 -2
  60. package/src/emulation/watchers.ts +10 -4
  61. package/src/file.ts +94 -1
  62. package/src/filesystem.ts +3 -366
  63. package/src/index.ts +1 -2
  64. package/src/mixins/async.ts +211 -0
  65. package/src/mixins/mutexed.ts +245 -0
  66. package/src/mixins/readonly.ts +97 -0
  67. package/src/mixins/shared.ts +20 -0
  68. package/src/mixins/sync.ts +59 -0
  69. package/dist/backends/index/index.d.ts +0 -43
  70. package/dist/backends/index/index.js +0 -83
  71. package/dist/backends/locked.d.ts +0 -92
  72. package/dist/backends/locked.js +0 -487
  73. package/src/backends/index/index.ts +0 -104
  74. package/src/backends/locked.ts +0 -264
@@ -1,487 +0,0 @@
1
- var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
- if (value !== null && value !== void 0) {
3
- if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
- var dispose;
5
- if (async) {
6
- if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
- dispose = value[Symbol.asyncDispose];
8
- }
9
- if (dispose === void 0) {
10
- if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
- dispose = value[Symbol.dispose];
12
- }
13
- if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
14
- env.stack.push({ value: value, dispose: dispose, async: async });
15
- }
16
- else if (async) {
17
- env.stack.push({ async: true });
18
- }
19
- return value;
20
- };
21
- var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
22
- return function (env) {
23
- function fail(e) {
24
- env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
25
- env.hasError = true;
26
- }
27
- function next() {
28
- while (env.stack.length) {
29
- var rec = env.stack.pop();
30
- try {
31
- var result = rec.dispose && rec.dispose.call(rec.value);
32
- if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
33
- }
34
- catch (e) {
35
- fail(e);
36
- }
37
- }
38
- if (env.hasError) throw env.error;
39
- }
40
- return next();
41
- };
42
- })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
43
- var e = new Error(message);
44
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
45
- });
46
- import { Errno, ErrnoError } from '../error.js';
47
- import { FileSystem } from '../filesystem.js';
48
- import '../polyfills.js';
49
- /**
50
- * This class serializes access to an underlying async filesystem.
51
- * For example, on an OverlayFS instance with an async lower
52
- * directory operations like rename and rmdir may involve multiple
53
- * requests involving both the upper and lower filesystems -- they
54
- * are not executed in a single atomic step. OverlayFS uses this
55
- * LockedFS to avoid having to reason about the correctness of
56
- * multiple requests interleaving.
57
- * @internal
58
- */
59
- export class LockedFS {
60
- constructor(fs) {
61
- this.fs = fs;
62
- /**
63
- * The current locks
64
- */
65
- this.locks = new Map();
66
- }
67
- addLock(path) {
68
- const lock = {
69
- ...Promise.withResolvers(),
70
- [Symbol.dispose]: () => {
71
- this.unlock(path);
72
- },
73
- };
74
- this.locks.set(path, lock);
75
- return lock;
76
- }
77
- /**
78
- * Locks `path` asynchronously.
79
- * If the path is currently locked, waits for it to be unlocked.
80
- * @internal
81
- */
82
- async lock(path) {
83
- if (this.locks.has(path)) {
84
- // Non-null assertion: we already checked locks has path
85
- await this.locks.get(path).promise;
86
- }
87
- return this.addLock(path);
88
- }
89
- /**
90
- * Locks `path` asynchronously.
91
- * If the path is currently locked, an error will be thrown
92
- * @internal
93
- */
94
- lockSync(path) {
95
- if (this.locks.has(path)) {
96
- // Non-null assertion: we already checked locks has path
97
- throw ErrnoError.With('EBUSY', path, 'lockSync');
98
- }
99
- return this.addLock(path);
100
- }
101
- /**
102
- * Unlocks a path
103
- * @param path The path to lock
104
- * @param noThrow If true, an error will not be thrown if the path is already unlocked
105
- * @returns Whether the path was unlocked
106
- * @internal
107
- */
108
- unlock(path, noThrow = false) {
109
- if (!this.locks.has(path)) {
110
- if (noThrow) {
111
- return false;
112
- }
113
- throw new ErrnoError(Errno.EPERM, 'Can not unlock an already unlocked path', path);
114
- }
115
- // Non-null assertion: we already checked locks has path
116
- this.locks.get(path).resolve();
117
- this.locks.delete(path);
118
- return true;
119
- }
120
- /**
121
- * Whether `path` is locked
122
- * @internal
123
- */
124
- isLocked(path) {
125
- return this.locks.has(path);
126
- }
127
- async ready() {
128
- await this.fs.ready();
129
- }
130
- metadata() {
131
- return {
132
- ...this.fs.metadata(),
133
- name: 'Locked<' + this.fs.metadata().name + '>',
134
- };
135
- }
136
- async rename(oldPath, newPath, cred) {
137
- const env_1 = { stack: [], error: void 0, hasError: false };
138
- try {
139
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
140
- const _ = __addDisposableResource(env_1, await this.lock(oldPath), false);
141
- await this.fs.rename(oldPath, newPath, cred);
142
- }
143
- catch (e_1) {
144
- env_1.error = e_1;
145
- env_1.hasError = true;
146
- }
147
- finally {
148
- __disposeResources(env_1);
149
- }
150
- }
151
- renameSync(oldPath, newPath, cred) {
152
- const env_2 = { stack: [], error: void 0, hasError: false };
153
- try {
154
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
155
- const _ = __addDisposableResource(env_2, this.lockSync(oldPath), false);
156
- return this.fs.renameSync(oldPath, newPath, cred);
157
- }
158
- catch (e_2) {
159
- env_2.error = e_2;
160
- env_2.hasError = true;
161
- }
162
- finally {
163
- __disposeResources(env_2);
164
- }
165
- }
166
- async stat(path, cred) {
167
- const env_3 = { stack: [], error: void 0, hasError: false };
168
- try {
169
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
170
- const _ = __addDisposableResource(env_3, await this.lock(path), false);
171
- return await this.fs.stat(path, cred);
172
- }
173
- catch (e_3) {
174
- env_3.error = e_3;
175
- env_3.hasError = true;
176
- }
177
- finally {
178
- __disposeResources(env_3);
179
- }
180
- }
181
- statSync(path, cred) {
182
- const env_4 = { stack: [], error: void 0, hasError: false };
183
- try {
184
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
185
- const _ = __addDisposableResource(env_4, this.lockSync(path), false);
186
- return this.fs.statSync(path, cred);
187
- }
188
- catch (e_4) {
189
- env_4.error = e_4;
190
- env_4.hasError = true;
191
- }
192
- finally {
193
- __disposeResources(env_4);
194
- }
195
- }
196
- async openFile(path, flag, cred) {
197
- const env_5 = { stack: [], error: void 0, hasError: false };
198
- try {
199
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
200
- const _ = __addDisposableResource(env_5, await this.lock(path), false);
201
- return await this.fs.openFile(path, flag, cred);
202
- }
203
- catch (e_5) {
204
- env_5.error = e_5;
205
- env_5.hasError = true;
206
- }
207
- finally {
208
- __disposeResources(env_5);
209
- }
210
- }
211
- openFileSync(path, flag, cred) {
212
- const env_6 = { stack: [], error: void 0, hasError: false };
213
- try {
214
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
215
- const _ = __addDisposableResource(env_6, this.lockSync(path), false);
216
- return this.fs.openFileSync(path, flag, cred);
217
- }
218
- catch (e_6) {
219
- env_6.error = e_6;
220
- env_6.hasError = true;
221
- }
222
- finally {
223
- __disposeResources(env_6);
224
- }
225
- }
226
- async createFile(path, flag, mode, cred) {
227
- const env_7 = { stack: [], error: void 0, hasError: false };
228
- try {
229
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
230
- const _ = __addDisposableResource(env_7, await this.lock(path), false);
231
- return await this.fs.createFile(path, flag, mode, cred);
232
- }
233
- catch (e_7) {
234
- env_7.error = e_7;
235
- env_7.hasError = true;
236
- }
237
- finally {
238
- __disposeResources(env_7);
239
- }
240
- }
241
- createFileSync(path, flag, mode, cred) {
242
- const env_8 = { stack: [], error: void 0, hasError: false };
243
- try {
244
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
245
- const _ = __addDisposableResource(env_8, this.lockSync(path), false);
246
- return this.fs.createFileSync(path, flag, mode, cred);
247
- }
248
- catch (e_8) {
249
- env_8.error = e_8;
250
- env_8.hasError = true;
251
- }
252
- finally {
253
- __disposeResources(env_8);
254
- }
255
- }
256
- async unlink(path, cred) {
257
- const env_9 = { stack: [], error: void 0, hasError: false };
258
- try {
259
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
260
- const _ = __addDisposableResource(env_9, await this.lock(path), false);
261
- await this.fs.unlink(path, cred);
262
- }
263
- catch (e_9) {
264
- env_9.error = e_9;
265
- env_9.hasError = true;
266
- }
267
- finally {
268
- __disposeResources(env_9);
269
- }
270
- }
271
- unlinkSync(path, cred) {
272
- const env_10 = { stack: [], error: void 0, hasError: false };
273
- try {
274
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
275
- const _ = __addDisposableResource(env_10, this.lockSync(path), false);
276
- return this.fs.unlinkSync(path, cred);
277
- }
278
- catch (e_10) {
279
- env_10.error = e_10;
280
- env_10.hasError = true;
281
- }
282
- finally {
283
- __disposeResources(env_10);
284
- }
285
- }
286
- async rmdir(path, cred) {
287
- const env_11 = { stack: [], error: void 0, hasError: false };
288
- try {
289
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
290
- const _ = __addDisposableResource(env_11, await this.lock(path), false);
291
- await this.fs.rmdir(path, cred);
292
- }
293
- catch (e_11) {
294
- env_11.error = e_11;
295
- env_11.hasError = true;
296
- }
297
- finally {
298
- __disposeResources(env_11);
299
- }
300
- }
301
- rmdirSync(path, cred) {
302
- const env_12 = { stack: [], error: void 0, hasError: false };
303
- try {
304
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
305
- const _ = __addDisposableResource(env_12, this.lockSync(path), false);
306
- return this.fs.rmdirSync(path, cred);
307
- }
308
- catch (e_12) {
309
- env_12.error = e_12;
310
- env_12.hasError = true;
311
- }
312
- finally {
313
- __disposeResources(env_12);
314
- }
315
- }
316
- async mkdir(path, mode, cred) {
317
- const env_13 = { stack: [], error: void 0, hasError: false };
318
- try {
319
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
320
- const _ = __addDisposableResource(env_13, await this.lock(path), false);
321
- await this.fs.mkdir(path, mode, cred);
322
- }
323
- catch (e_13) {
324
- env_13.error = e_13;
325
- env_13.hasError = true;
326
- }
327
- finally {
328
- __disposeResources(env_13);
329
- }
330
- }
331
- mkdirSync(path, mode, cred) {
332
- const env_14 = { stack: [], error: void 0, hasError: false };
333
- try {
334
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
335
- const _ = __addDisposableResource(env_14, this.lockSync(path), false);
336
- return this.fs.mkdirSync(path, mode, cred);
337
- }
338
- catch (e_14) {
339
- env_14.error = e_14;
340
- env_14.hasError = true;
341
- }
342
- finally {
343
- __disposeResources(env_14);
344
- }
345
- }
346
- async readdir(path, cred) {
347
- const env_15 = { stack: [], error: void 0, hasError: false };
348
- try {
349
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
350
- const _ = __addDisposableResource(env_15, await this.lock(path), false);
351
- return await this.fs.readdir(path, cred);
352
- }
353
- catch (e_15) {
354
- env_15.error = e_15;
355
- env_15.hasError = true;
356
- }
357
- finally {
358
- __disposeResources(env_15);
359
- }
360
- }
361
- readdirSync(path, cred) {
362
- const env_16 = { stack: [], error: void 0, hasError: false };
363
- try {
364
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
365
- const _ = __addDisposableResource(env_16, this.lockSync(path), false);
366
- return this.fs.readdirSync(path, cred);
367
- }
368
- catch (e_16) {
369
- env_16.error = e_16;
370
- env_16.hasError = true;
371
- }
372
- finally {
373
- __disposeResources(env_16);
374
- }
375
- }
376
- async exists(path, cred) {
377
- const env_17 = { stack: [], error: void 0, hasError: false };
378
- try {
379
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
380
- const _ = __addDisposableResource(env_17, await this.lock(path), false);
381
- return await this.fs.exists(path, cred);
382
- }
383
- catch (e_17) {
384
- env_17.error = e_17;
385
- env_17.hasError = true;
386
- }
387
- finally {
388
- __disposeResources(env_17);
389
- }
390
- }
391
- existsSync(path, cred) {
392
- const env_18 = { stack: [], error: void 0, hasError: false };
393
- try {
394
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
395
- const _ = __addDisposableResource(env_18, this.lockSync(path), false);
396
- return this.fs.existsSync(path, cred);
397
- }
398
- catch (e_18) {
399
- env_18.error = e_18;
400
- env_18.hasError = true;
401
- }
402
- finally {
403
- __disposeResources(env_18);
404
- }
405
- }
406
- async link(srcpath, dstpath, cred) {
407
- const env_19 = { stack: [], error: void 0, hasError: false };
408
- try {
409
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
410
- const _ = __addDisposableResource(env_19, await this.lock(srcpath), false);
411
- await this.fs.link(srcpath, dstpath, cred);
412
- }
413
- catch (e_19) {
414
- env_19.error = e_19;
415
- env_19.hasError = true;
416
- }
417
- finally {
418
- __disposeResources(env_19);
419
- }
420
- }
421
- linkSync(srcpath, dstpath, cred) {
422
- const env_20 = { stack: [], error: void 0, hasError: false };
423
- try {
424
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
425
- const _ = __addDisposableResource(env_20, this.lockSync(srcpath), false);
426
- return this.fs.linkSync(srcpath, dstpath, cred);
427
- }
428
- catch (e_20) {
429
- env_20.error = e_20;
430
- env_20.hasError = true;
431
- }
432
- finally {
433
- __disposeResources(env_20);
434
- }
435
- }
436
- async sync(path, data, stats) {
437
- const env_21 = { stack: [], error: void 0, hasError: false };
438
- try {
439
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
440
- const _ = __addDisposableResource(env_21, await this.lock(path), false);
441
- await this.fs.sync(path, data, stats);
442
- }
443
- catch (e_21) {
444
- env_21.error = e_21;
445
- env_21.hasError = true;
446
- }
447
- finally {
448
- __disposeResources(env_21);
449
- }
450
- }
451
- syncSync(path, data, stats) {
452
- const env_22 = { stack: [], error: void 0, hasError: false };
453
- try {
454
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
455
- const _ = __addDisposableResource(env_22, this.lockSync(path), false);
456
- return this.fs.syncSync(path, data, stats);
457
- }
458
- catch (e_22) {
459
- env_22.error = e_22;
460
- env_22.hasError = true;
461
- }
462
- finally {
463
- __disposeResources(env_22);
464
- }
465
- }
466
- }
467
- export const Locked = {
468
- name: 'Locked',
469
- options: {
470
- fs: {
471
- type: 'object',
472
- required: true,
473
- description: '',
474
- validator(fs) {
475
- if (!(fs instanceof FileSystem)) {
476
- throw new ErrnoError(Errno.EINVAL, 'fs passed to LockedFS must be a FileSystem');
477
- }
478
- },
479
- },
480
- },
481
- isAvailable() {
482
- return true;
483
- },
484
- create({ fs }) {
485
- return new LockedFS(fs);
486
- },
487
- };
@@ -1,104 +0,0 @@
1
- import { isJSON } from 'utilium';
2
- import { Errno, ErrnoError } from '../../error.js';
3
- import type { StatsLike } from '../../stats.js';
4
- import { Stats } from '../../stats.js';
5
- import { encode } from '../../utils.js';
6
- import { basename, dirname } from '../../emulation/path.js';
7
-
8
- /**
9
- * An Index in JSON form
10
- * @internal
11
- */
12
- export interface IndexData {
13
- version: 1;
14
- entries: Record<string, StatsLike<number>>;
15
- }
16
-
17
- export const version = 1;
18
-
19
- /**
20
- * An index of files
21
- * @internal
22
- */
23
- export class Index extends Map<string, Stats> {
24
- public constructor() {
25
- super();
26
- }
27
-
28
- /**
29
- * Convience method
30
- */
31
- public files(): Map<string, Stats> {
32
- const files = new Map<string, Stats>();
33
- for (const [path, stats] of this) {
34
- if (stats.isFile()) {
35
- files.set(path, stats);
36
- }
37
- }
38
- return files;
39
- }
40
-
41
- /**
42
- * Converts the index to JSON
43
- */
44
- public toJSON(): IndexData {
45
- return {
46
- version,
47
- entries: Object.fromEntries(this),
48
- };
49
- }
50
-
51
- /**
52
- * Converts the index to a string
53
- */
54
- public toString(): string {
55
- return JSON.stringify(this.toJSON());
56
- }
57
-
58
- /**
59
- * Returns the files in the directory `dir`.
60
- * This is expensive so it is only called once per directory.
61
- */
62
- protected dirEntries(dir: string): string[] {
63
- const entries = [];
64
- for (const entry of this.keys()) {
65
- if (dirname(entry) == dir) {
66
- entries.push(basename(entry));
67
- }
68
- }
69
- return entries;
70
- }
71
-
72
- /**
73
- * Loads the index from JSON data
74
- */
75
- public fromJSON(json: IndexData): void {
76
- if (json.version != version) {
77
- throw new ErrnoError(Errno.EINVAL, 'Index version mismatch');
78
- }
79
-
80
- this.clear();
81
-
82
- for (const [path, data] of Object.entries(json.entries)) {
83
- const stats = new Stats(data);
84
- if (stats.isDirectory()) {
85
- stats.fileData = encode(JSON.stringify(this.dirEntries(path)));
86
- }
87
- this.set(path, stats);
88
- }
89
- }
90
-
91
- /**
92
- * Parses an index from a string
93
- */
94
- public static parse(data: string): Index {
95
- if (!isJSON(data)) {
96
- throw new ErrnoError(Errno.EINVAL, 'Invalid JSON');
97
- }
98
-
99
- const json = JSON.parse(data) as IndexData;
100
- const index = new Index();
101
- index.fromJSON(json);
102
- return index;
103
- }
104
- }