zen-fs-config 0.3.1 → 0.3.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.
Files changed (3) hide show
  1. package/dist/index.js +45 -108
  2. package/dist/index.mjs +45 -108
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -316,100 +316,74 @@ function hasBackend(type) {
316
316
  function listBackends() {
317
317
  return Array.from(registry.keys());
318
318
  }
319
- var inMemoryCounter = 0;
320
- registerBackend("InMemory", async (options) => {
319
+ async function wrapZenFSFileSystem(config) {
321
320
  const zenfs = await import("@zenfs/core");
322
- const { InMemory } = zenfs;
323
- const maxSize = options.maxSize ?? 100 * 1024 * 1024;
324
- const label = options.label ?? `zen-fs-config-${++inMemoryCounter}`;
325
- await zenfs.configureSingle({ backend: InMemory, maxSize, label });
326
- const pfs = zenfs.fs.promises;
327
- const backend = {
321
+ const isolatedFS = await zenfs.resolveMountConfig(config);
322
+ return {
328
323
  async readFile(path, ...args) {
329
- if (args.length > 0) {
330
- return pfs.readFile(path, ...args);
331
- }
332
- return pfs.readFile(path);
324
+ const st = await isolatedFS.stat(path);
325
+ const size = st.size;
326
+ const buf = new Uint8Array(size);
327
+ await isolatedFS.read(path, buf, 0, size);
328
+ if (args[0] === "utf-8") return new TextDecoder().decode(buf);
329
+ return buf;
333
330
  },
334
- async writeFile(path, data, options2) {
335
- return pfs.writeFile(path, data, options2);
331
+ async writeFile(path, data, _options) {
332
+ const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : data instanceof Uint8Array ? data : new TextEncoder().encode(data);
333
+ const parts = path.split("/").filter(Boolean);
334
+ parts.pop();
335
+ let dir = "";
336
+ for (const p of parts) {
337
+ dir += "/" + p;
338
+ if (!await isolatedFS.exists(dir)) {
339
+ await isolatedFS.mkdir(dir, { uid: 0, gid: 0, mode: 493 });
340
+ }
341
+ }
342
+ await isolatedFS.write(path, bytes, 0);
336
343
  },
337
344
  async readdir(path) {
338
- const entries = await pfs.readdir(path);
339
- return entries.map((e) => typeof e === "string" ? e : e.name);
345
+ return isolatedFS.readdir(path);
340
346
  },
341
- async stat(path, ...args) {
342
- return pfs.stat(path, ...args);
347
+ async stat(path, ..._args) {
348
+ const st = await isolatedFS.stat(path);
349
+ return {
350
+ isFile: () => st.isDirectory?.() === false || st.mode !== void 0 && (st.mode & 61440) === 32768,
351
+ isDirectory: () => st.isDirectory?.() === true || st.mode !== void 0 && (st.mode & 61440) === 16384,
352
+ size: st.size,
353
+ mtime: st.mtimeMs ?? st.mtime
354
+ };
343
355
  },
344
356
  async exists(path) {
345
- try {
346
- await pfs.stat(path);
347
- return true;
348
- } catch {
349
- return false;
350
- }
357
+ return isolatedFS.exists(path);
351
358
  },
352
- async mkdir(path, options2) {
353
- return pfs.mkdir(path, options2);
359
+ async mkdir(path, options) {
360
+ return isolatedFS.mkdir(path, options ?? { uid: 0, gid: 0, mode: 493 });
354
361
  },
355
362
  async unlink(path) {
356
- return pfs.unlink(path);
363
+ return isolatedFS.unlink(path);
357
364
  },
358
365
  async rmdir(path) {
359
- return pfs.rmdir(path);
366
+ return isolatedFS.rmdir(path);
360
367
  },
361
368
  async rename(oldPath, newPath) {
362
- return pfs.rename(oldPath, newPath);
369
+ return isolatedFS.rename(oldPath, newPath);
363
370
  }
364
371
  };
365
- return backend;
372
+ }
373
+ var inMemoryCounter = 0;
374
+ registerBackend("InMemory", async (options) => {
375
+ const { InMemory } = await import("@zenfs/core");
376
+ const maxSize = options.maxSize ?? 100 * 1024 * 1024;
377
+ const label = options.label ?? `zen-fs-config-${++inMemoryCounter}`;
378
+ return wrapZenFSFileSystem({ backend: InMemory, maxSize, label });
366
379
  });
367
380
  var idbCounter = 0;
368
381
  registerBackend("IndexedDB", async (options) => {
369
- const zenfs = await import("@zenfs/core");
370
382
  const { IndexedDB } = await import("@zenfs/dom");
371
383
  const storeName = options.storeName ?? `zen-fs-config-${++idbCounter}`;
372
- await zenfs.configureSingle({ backend: IndexedDB, storeName });
373
- const pfs = zenfs.fs.promises;
374
- const backend = {
375
- async readFile(path, ...args) {
376
- return args.length > 0 ? pfs.readFile(path, ...args) : pfs.readFile(path);
377
- },
378
- async writeFile(path, data, options2) {
379
- return pfs.writeFile(path, data, options2);
380
- },
381
- async readdir(path) {
382
- const entries = await pfs.readdir(path);
383
- return entries.map((e) => typeof e === "string" ? e : e.name);
384
- },
385
- async stat(path, ...args) {
386
- return pfs.stat(path, ...args);
387
- },
388
- async exists(path) {
389
- try {
390
- await pfs.stat(path);
391
- return true;
392
- } catch {
393
- return false;
394
- }
395
- },
396
- async mkdir(path, options2) {
397
- return pfs.mkdir(path, options2);
398
- },
399
- async unlink(path) {
400
- return pfs.unlink(path);
401
- },
402
- async rmdir(path) {
403
- return pfs.rmdir(path);
404
- },
405
- async rename(oldPath, newPath) {
406
- return pfs.rename(oldPath, newPath);
407
- }
408
- };
409
- return backend;
384
+ return wrapZenFSFileSystem({ backend: IndexedDB, storeName });
410
385
  });
411
386
  registerBackend("WebStorage", async (options) => {
412
- const zenfs = await import("@zenfs/core");
413
387
  const { WebStorage } = await import("@zenfs/dom");
414
388
  const storageType = options.storageType ?? "localStorage";
415
389
  let storage;
@@ -418,44 +392,7 @@ registerBackend("WebStorage", async (options) => {
418
392
  } else {
419
393
  storage = localStorage;
420
394
  }
421
- await zenfs.configureSingle({ backend: WebStorage, storage });
422
- const pfs = zenfs.fs.promises;
423
- const backend = {
424
- async readFile(path, ...args) {
425
- return args.length > 0 ? pfs.readFile(path, ...args) : pfs.readFile(path);
426
- },
427
- async writeFile(path, data, options2) {
428
- return pfs.writeFile(path, data, options2);
429
- },
430
- async readdir(path) {
431
- const entries = await pfs.readdir(path);
432
- return entries.map((e) => typeof e === "string" ? e : e.name);
433
- },
434
- async stat(path, ...args) {
435
- return pfs.stat(path, ...args);
436
- },
437
- async exists(path) {
438
- try {
439
- await pfs.stat(path);
440
- return true;
441
- } catch {
442
- return false;
443
- }
444
- },
445
- async mkdir(path, options2) {
446
- return pfs.mkdir(path, options2);
447
- },
448
- async unlink(path) {
449
- return pfs.unlink(path);
450
- },
451
- async rmdir(path) {
452
- return pfs.rmdir(path);
453
- },
454
- async rename(oldPath, newPath) {
455
- return pfs.rename(oldPath, newPath);
456
- }
457
- };
458
- return backend;
395
+ return wrapZenFSFileSystem({ backend: WebStorage, storage });
459
396
  });
460
397
  registerBackend("GitHub", async (options) => {
461
398
  const token = options.token ?? "";
package/dist/index.mjs CHANGED
@@ -269,100 +269,74 @@ function hasBackend(type) {
269
269
  function listBackends() {
270
270
  return Array.from(registry.keys());
271
271
  }
272
- var inMemoryCounter = 0;
273
- registerBackend("InMemory", async (options) => {
272
+ async function wrapZenFSFileSystem(config) {
274
273
  const zenfs = await import("@zenfs/core");
275
- const { InMemory } = zenfs;
276
- const maxSize = options.maxSize ?? 100 * 1024 * 1024;
277
- const label = options.label ?? `zen-fs-config-${++inMemoryCounter}`;
278
- await zenfs.configureSingle({ backend: InMemory, maxSize, label });
279
- const pfs = zenfs.fs.promises;
280
- const backend = {
274
+ const isolatedFS = await zenfs.resolveMountConfig(config);
275
+ return {
281
276
  async readFile(path, ...args) {
282
- if (args.length > 0) {
283
- return pfs.readFile(path, ...args);
284
- }
285
- return pfs.readFile(path);
277
+ const st = await isolatedFS.stat(path);
278
+ const size = st.size;
279
+ const buf = new Uint8Array(size);
280
+ await isolatedFS.read(path, buf, 0, size);
281
+ if (args[0] === "utf-8") return new TextDecoder().decode(buf);
282
+ return buf;
286
283
  },
287
- async writeFile(path, data, options2) {
288
- return pfs.writeFile(path, data, options2);
284
+ async writeFile(path, data, _options) {
285
+ const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : data instanceof Uint8Array ? data : new TextEncoder().encode(data);
286
+ const parts = path.split("/").filter(Boolean);
287
+ parts.pop();
288
+ let dir = "";
289
+ for (const p of parts) {
290
+ dir += "/" + p;
291
+ if (!await isolatedFS.exists(dir)) {
292
+ await isolatedFS.mkdir(dir, { uid: 0, gid: 0, mode: 493 });
293
+ }
294
+ }
295
+ await isolatedFS.write(path, bytes, 0);
289
296
  },
290
297
  async readdir(path) {
291
- const entries = await pfs.readdir(path);
292
- return entries.map((e) => typeof e === "string" ? e : e.name);
298
+ return isolatedFS.readdir(path);
293
299
  },
294
- async stat(path, ...args) {
295
- return pfs.stat(path, ...args);
300
+ async stat(path, ..._args) {
301
+ const st = await isolatedFS.stat(path);
302
+ return {
303
+ isFile: () => st.isDirectory?.() === false || st.mode !== void 0 && (st.mode & 61440) === 32768,
304
+ isDirectory: () => st.isDirectory?.() === true || st.mode !== void 0 && (st.mode & 61440) === 16384,
305
+ size: st.size,
306
+ mtime: st.mtimeMs ?? st.mtime
307
+ };
296
308
  },
297
309
  async exists(path) {
298
- try {
299
- await pfs.stat(path);
300
- return true;
301
- } catch {
302
- return false;
303
- }
310
+ return isolatedFS.exists(path);
304
311
  },
305
- async mkdir(path, options2) {
306
- return pfs.mkdir(path, options2);
312
+ async mkdir(path, options) {
313
+ return isolatedFS.mkdir(path, options ?? { uid: 0, gid: 0, mode: 493 });
307
314
  },
308
315
  async unlink(path) {
309
- return pfs.unlink(path);
316
+ return isolatedFS.unlink(path);
310
317
  },
311
318
  async rmdir(path) {
312
- return pfs.rmdir(path);
319
+ return isolatedFS.rmdir(path);
313
320
  },
314
321
  async rename(oldPath, newPath) {
315
- return pfs.rename(oldPath, newPath);
322
+ return isolatedFS.rename(oldPath, newPath);
316
323
  }
317
324
  };
318
- return backend;
325
+ }
326
+ var inMemoryCounter = 0;
327
+ registerBackend("InMemory", async (options) => {
328
+ const { InMemory } = await import("@zenfs/core");
329
+ const maxSize = options.maxSize ?? 100 * 1024 * 1024;
330
+ const label = options.label ?? `zen-fs-config-${++inMemoryCounter}`;
331
+ return wrapZenFSFileSystem({ backend: InMemory, maxSize, label });
319
332
  });
320
333
  var idbCounter = 0;
321
334
  registerBackend("IndexedDB", async (options) => {
322
- const zenfs = await import("@zenfs/core");
323
335
  const { IndexedDB } = await import("@zenfs/dom");
324
336
  const storeName = options.storeName ?? `zen-fs-config-${++idbCounter}`;
325
- await zenfs.configureSingle({ backend: IndexedDB, storeName });
326
- const pfs = zenfs.fs.promises;
327
- const backend = {
328
- async readFile(path, ...args) {
329
- return args.length > 0 ? pfs.readFile(path, ...args) : pfs.readFile(path);
330
- },
331
- async writeFile(path, data, options2) {
332
- return pfs.writeFile(path, data, options2);
333
- },
334
- async readdir(path) {
335
- const entries = await pfs.readdir(path);
336
- return entries.map((e) => typeof e === "string" ? e : e.name);
337
- },
338
- async stat(path, ...args) {
339
- return pfs.stat(path, ...args);
340
- },
341
- async exists(path) {
342
- try {
343
- await pfs.stat(path);
344
- return true;
345
- } catch {
346
- return false;
347
- }
348
- },
349
- async mkdir(path, options2) {
350
- return pfs.mkdir(path, options2);
351
- },
352
- async unlink(path) {
353
- return pfs.unlink(path);
354
- },
355
- async rmdir(path) {
356
- return pfs.rmdir(path);
357
- },
358
- async rename(oldPath, newPath) {
359
- return pfs.rename(oldPath, newPath);
360
- }
361
- };
362
- return backend;
337
+ return wrapZenFSFileSystem({ backend: IndexedDB, storeName });
363
338
  });
364
339
  registerBackend("WebStorage", async (options) => {
365
- const zenfs = await import("@zenfs/core");
366
340
  const { WebStorage } = await import("@zenfs/dom");
367
341
  const storageType = options.storageType ?? "localStorage";
368
342
  let storage;
@@ -371,44 +345,7 @@ registerBackend("WebStorage", async (options) => {
371
345
  } else {
372
346
  storage = localStorage;
373
347
  }
374
- await zenfs.configureSingle({ backend: WebStorage, storage });
375
- const pfs = zenfs.fs.promises;
376
- const backend = {
377
- async readFile(path, ...args) {
378
- return args.length > 0 ? pfs.readFile(path, ...args) : pfs.readFile(path);
379
- },
380
- async writeFile(path, data, options2) {
381
- return pfs.writeFile(path, data, options2);
382
- },
383
- async readdir(path) {
384
- const entries = await pfs.readdir(path);
385
- return entries.map((e) => typeof e === "string" ? e : e.name);
386
- },
387
- async stat(path, ...args) {
388
- return pfs.stat(path, ...args);
389
- },
390
- async exists(path) {
391
- try {
392
- await pfs.stat(path);
393
- return true;
394
- } catch {
395
- return false;
396
- }
397
- },
398
- async mkdir(path, options2) {
399
- return pfs.mkdir(path, options2);
400
- },
401
- async unlink(path) {
402
- return pfs.unlink(path);
403
- },
404
- async rmdir(path) {
405
- return pfs.rmdir(path);
406
- },
407
- async rename(oldPath, newPath) {
408
- return pfs.rename(oldPath, newPath);
409
- }
410
- };
411
- return backend;
348
+ return wrapZenFSFileSystem({ backend: WebStorage, storage });
412
349
  });
413
350
  registerBackend("GitHub", async (options) => {
414
351
  const token = options.token ?? "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zen-fs-config",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "Distributed config management library built on ZenFS, zen-fs-cache, and zen-fs-sync",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",