@zenfs/core 0.16.0 → 0.16.2

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 (46) hide show
  1. package/dist/backends/fetch.js +5 -4
  2. package/dist/backends/index/fs.js +12 -5
  3. package/dist/backends/overlay.d.ts +1 -1
  4. package/dist/backends/overlay.js +9 -9
  5. package/dist/backends/port/fs.d.ts +22 -0
  6. package/dist/backends/port/fs.js +12 -1
  7. package/dist/backends/port/rpc.d.ts +2 -0
  8. package/dist/backends/port/rpc.js +13 -1
  9. package/dist/backends/store/fs.js +1 -1
  10. package/dist/browser.min.js +4 -4
  11. package/dist/browser.min.js.map +4 -4
  12. package/dist/config.d.ts +1 -2
  13. package/dist/config.js +2 -2
  14. package/dist/emulation/async.d.ts +4 -5
  15. package/dist/emulation/async.js +9 -9
  16. package/dist/emulation/dir.js +1 -1
  17. package/dist/emulation/promises.js +10 -5
  18. package/dist/emulation/sync.d.ts +1 -0
  19. package/dist/emulation/sync.js +1 -1
  20. package/dist/file.js +6 -6
  21. package/dist/filesystem.js +1 -1
  22. package/dist/inode.d.ts +1 -1
  23. package/dist/inode.js +54 -37
  24. package/dist/polyfills.js +1 -0
  25. package/dist/utils.d.ts +1 -1
  26. package/dist/utils.js +1 -1
  27. package/eslint.shared.js +53 -0
  28. package/package.json +16 -6
  29. package/src/backends/fetch.ts +6 -5
  30. package/src/backends/index/fs.ts +12 -5
  31. package/src/backends/index/index.ts +1 -1
  32. package/src/backends/overlay.ts +9 -9
  33. package/src/backends/port/fs.ts +18 -3
  34. package/src/backends/port/readme.md +7 -12
  35. package/src/backends/port/rpc.ts +16 -1
  36. package/src/backends/store/fs.ts +2 -2
  37. package/src/config.ts +3 -3
  38. package/src/emulation/async.ts +31 -18
  39. package/src/emulation/dir.ts +1 -1
  40. package/src/emulation/promises.ts +11 -6
  41. package/src/emulation/sync.ts +5 -4
  42. package/src/file.ts +6 -6
  43. package/src/filesystem.ts +2 -1
  44. package/src/inode.ts +56 -37
  45. package/src/polyfills.ts +1 -0
  46. package/src/utils.ts +5 -2
@@ -1,20 +1,21 @@
1
1
  import { Errno, ErrnoError } from '../error.js';
2
2
  import { IndexFS } from './index/fs.js';
3
3
  async function fetchFile(path, type) {
4
- const response = await fetch(path).catch(e => {
4
+ const response = await fetch(path).catch((e) => {
5
5
  throw new ErrnoError(Errno.EIO, e.message);
6
6
  });
7
7
  if (!response.ok) {
8
8
  throw new ErrnoError(Errno.EIO, 'fetch failed: response returned code ' + response.status);
9
9
  }
10
10
  switch (type) {
11
- case 'buffer':
12
- const arrayBuffer = await response.arrayBuffer().catch(e => {
11
+ case 'buffer': {
12
+ const arrayBuffer = await response.arrayBuffer().catch((e) => {
13
13
  throw new ErrnoError(Errno.EIO, e.message);
14
14
  });
15
15
  return new Uint8Array(arrayBuffer);
16
+ }
16
17
  case 'json':
17
- return response.json().catch(e => {
18
+ return response.json().catch((e) => {
18
19
  throw new ErrnoError(Errno.EIO, e.message);
19
20
  });
20
21
  default:
@@ -30,8 +30,8 @@ export class IndexFS extends Readonly(FileSystem) {
30
30
  stats.fileData = this.getDataSync(path, stats);
31
31
  }
32
32
  }
33
- async stat(path) {
34
- return this.statSync(path);
33
+ stat(path) {
34
+ return Promise.resolve(this.statSync(path));
35
35
  }
36
36
  statSync(path) {
37
37
  if (!this.index.has(path)) {
@@ -69,8 +69,8 @@ export class IndexFS extends Readonly(FileSystem) {
69
69
  }
70
70
  return new NoSyncFile(this, path, flag, stats, stats.isDirectory() ? stats.fileData : this.getDataSync(path, stats));
71
71
  }
72
- async readdir(path) {
73
- return this.readdirSync(path);
72
+ readdir(path) {
73
+ return Promise.resolve(this.readdirSync(path));
74
74
  }
75
75
  readdirSync(path) {
76
76
  // Check if it exists.
@@ -81,6 +81,13 @@ export class IndexFS extends Readonly(FileSystem) {
81
81
  if (!stats.isDirectory()) {
82
82
  throw ErrnoError.With('ENOTDIR', path, 'readdir');
83
83
  }
84
- return JSON.parse(decode(stats.fileData));
84
+ const content = JSON.parse(decode(stats.fileData));
85
+ if (!Array.isArray(content)) {
86
+ throw ErrnoError.With('ENODATA', path, 'readdir');
87
+ }
88
+ if (!content.every(item => typeof item == 'string')) {
89
+ throw ErrnoError.With('ENODATA', path, 'readdir');
90
+ }
91
+ return content;
85
92
  }
86
93
  }
@@ -47,7 +47,7 @@ export declare class UnlockedOverlayFS extends FileSystem {
47
47
  */
48
48
  _initialize(): Promise<void>;
49
49
  getDeletionLog(): string;
50
- restoreDeletionLog(log: string, cred: Cred): void;
50
+ restoreDeletionLog(log: string, cred: Cred): Promise<void>;
51
51
  rename(oldPath: string, newPath: string, cred: Cred): Promise<void>;
52
52
  renameSync(oldPath: string, newPath: string, cred: Cred): void;
53
53
  stat(path: string, cred: Cred): Promise<Stats>;
@@ -92,10 +92,10 @@ export class UnlockedOverlayFS extends FileSystem {
92
92
  getDeletionLog() {
93
93
  return this._deleteLog;
94
94
  }
95
- restoreDeletionLog(log, cred) {
95
+ async restoreDeletionLog(log, cred) {
96
96
  this._deleteLog = log;
97
97
  this._reparseDeletionLog();
98
- this.updateLog('', cred);
98
+ await this.updateLog('', cred);
99
99
  }
100
100
  async rename(oldPath, newPath, cred) {
101
101
  this.checkInitialized();
@@ -203,7 +203,7 @@ export class UnlockedOverlayFS extends FileSystem {
203
203
  }
204
204
  // if it still exists add to the delete log
205
205
  if (await this.exists(path, cred)) {
206
- this.deletePath(path, cred);
206
+ await this.deletePath(path, cred);
207
207
  }
208
208
  }
209
209
  unlinkSync(path, cred) {
@@ -217,7 +217,7 @@ export class UnlockedOverlayFS extends FileSystem {
217
217
  }
218
218
  // if it still exists add to the delete log
219
219
  if (this.existsSync(path, cred)) {
220
- this.deletePath(path, cred);
220
+ void this.deletePath(path, cred);
221
221
  }
222
222
  }
223
223
  async rmdir(path, cred) {
@@ -234,7 +234,7 @@ export class UnlockedOverlayFS extends FileSystem {
234
234
  throw ErrnoError.With('ENOTEMPTY', path, 'rmdir');
235
235
  }
236
236
  else {
237
- this.deletePath(path, cred);
237
+ await this.deletePath(path, cred);
238
238
  }
239
239
  }
240
240
  }
@@ -252,7 +252,7 @@ export class UnlockedOverlayFS extends FileSystem {
252
252
  throw ErrnoError.With('ENOTEMPTY', path, 'rmdir');
253
253
  }
254
254
  else {
255
- this.deletePath(path, cred);
255
+ void this.deletePath(path, cred);
256
256
  }
257
257
  }
258
258
  }
@@ -328,9 +328,9 @@ export class UnlockedOverlayFS extends FileSystem {
328
328
  return result;
329
329
  });
330
330
  }
331
- deletePath(path, cred) {
331
+ async deletePath(path, cred) {
332
332
  this._deletedFiles.add(path);
333
- this.updateLog(`d${path}\n`, cred);
333
+ await this.updateLog(`d${path}\n`, cred);
334
334
  }
335
335
  async updateLog(addition, cred) {
336
336
  this._deleteLog += addition;
@@ -344,7 +344,7 @@ export class UnlockedOverlayFS extends FileSystem {
344
344
  await log.write(encode(this._deleteLog));
345
345
  if (this._deleteLogUpdateNeeded) {
346
346
  this._deleteLogUpdateNeeded = false;
347
- this.updateLog('', cred);
347
+ await this.updateLog('', cred);
348
348
  }
349
349
  }
350
350
  catch (e) {
@@ -6,9 +6,17 @@ import type { Cred } from '../../cred.js';
6
6
  import { File } from '../../file.js';
7
7
  import { FileSystem, type FileSystemMetadata } from '../../filesystem.js';
8
8
  import { Stats, type FileType } from '../../stats.js';
9
+ import type { Backend, FilesystemOf } from '../backend.js';
9
10
  import * as RPC from './rpc.js';
11
+ import { type MountConfiguration } from '../../config.js';
10
12
  type FileMethods = Omit<ExtractProperties<File, (...args: any[]) => Promise<any>>, typeof Symbol.asyncDispose>;
11
13
  type FileMethod = keyof FileMethods;
14
+ interface FileRequest<TMethod extends FileMethod = FileMethod> extends RPC.Request {
15
+ fd: number;
16
+ scope: 'file';
17
+ method: TMethod;
18
+ args: Parameters<FileMethods[TMethod]>;
19
+ }
12
20
  export declare class PortFile extends File {
13
21
  readonly fs: PortFS;
14
22
  readonly fd: number;
@@ -40,6 +48,11 @@ export declare class PortFile extends File {
40
48
  }
41
49
  type FSMethods = ExtractProperties<FileSystem, (...args: any[]) => Promise<any> | FileSystemMetadata>;
42
50
  type FSMethod = keyof FSMethods;
51
+ interface FSRequest<TMethod extends FSMethod = FSMethod> extends RPC.Request {
52
+ scope: 'fs';
53
+ method: TMethod;
54
+ args: Parameters<FSMethods[TMethod]>;
55
+ }
43
56
  declare const PortFS_base: import("../../filesystem.js").Mixin<typeof FileSystem, {
44
57
  _sync?: FileSystem | undefined;
45
58
  queueDone(): Promise<void>;
@@ -88,6 +101,14 @@ export declare class PortFS extends PortFS_base {
88
101
  exists(path: string, cred: Cred): Promise<boolean>;
89
102
  link(srcpath: string, dstpath: string, cred: Cred): Promise<void>;
90
103
  }
104
+ /**
105
+ * @internal
106
+ */
107
+ export type FileOrFSRequest = FSRequest | FileRequest;
108
+ /**
109
+ * @internal
110
+ */
111
+ export declare function handleRequest(port: RPC.Port, fs: FileSystem, request: FileOrFSRequest): Promise<void>;
91
112
  export declare function attachFS(port: RPC.Port, fs: FileSystem): void;
92
113
  export declare function detachFS(port: RPC.Port, fs: FileSystem): void;
93
114
  export declare const Port: {
@@ -108,4 +129,5 @@ export declare const Port: {
108
129
  isAvailable(): Promise<boolean>;
109
130
  create(options: RPC.Options): PortFS;
110
131
  };
132
+ export declare function resolveRemoteMount<T extends Backend>(port: RPC.Port, config: MountConfiguration<T>, _depth?: number): Promise<FilesystemOf<T>>;
111
133
  export {};
@@ -4,6 +4,7 @@ import { Async, FileSystem } from '../../filesystem.js';
4
4
  import { Stats } from '../../stats.js';
5
5
  import { InMemory } from '../memory.js';
6
6
  import * as RPC from './rpc.js';
7
+ import { resolveMountConfig } from '../../config.js';
7
8
  export class PortFile extends File {
8
9
  constructor(fs, fd, path, position) {
9
10
  super();
@@ -159,7 +160,10 @@ export class PortFS extends Async(FileSystem) {
159
160
  }
160
161
  let nextFd = 0;
161
162
  const descriptors = new Map();
162
- async function handleRequest(port, fs, request) {
163
+ /**
164
+ * @internal
165
+ */
166
+ export async function handleRequest(port, fs, request) {
163
167
  if (!RPC.isMessage(request)) {
164
168
  return;
165
169
  }
@@ -233,3 +237,10 @@ export const Port = {
233
237
  return new PortFS(options);
234
238
  },
235
239
  };
240
+ export async function resolveRemoteMount(port, config, _depth = 0) {
241
+ const stopAndReplay = RPC.catchMessages(port);
242
+ const fs = await resolveMountConfig(config, _depth);
243
+ attachFS(port, fs);
244
+ stopAndReplay(fs);
245
+ return fs;
246
+ }
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  import { type ErrnoErrorJSON } from '../../error.js';
3
+ import type { Backend, FilesystemOf } from '../backend.js';
3
4
  import { type PortFS } from './fs.js';
4
5
  type _MessageEvent<T = any> = T | {
5
6
  data: T;
@@ -62,3 +63,4 @@ export declare function request<const TRequest extends Request, TValue>(request:
62
63
  export declare function handleResponse<const TResponse extends Response>(response: TResponse): void;
63
64
  export declare function attach<T extends Message>(port: Port, handler: (message: T) => unknown): void;
64
65
  export declare function detach<T extends Message>(port: Port, handler: (message: T) => unknown): void;
66
+ export declare function catchMessages<T extends Backend>(port: Port): (fs: FilesystemOf<T>) => void;
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  import { Errno, ErrnoError } from '../../error.js';
3
- import { PortFile } from './fs.js';
3
+ import { handleRequest, PortFile } from './fs.js';
4
4
  function isFileData(value) {
5
5
  return typeof value == 'object' && value != null && 'fd' in value && 'path' in value && 'position' in value;
6
6
  }
@@ -72,3 +72,15 @@ export function detach(port, handler) {
72
72
  handler('data' in message ? message.data : message);
73
73
  });
74
74
  }
75
+ export function catchMessages(port) {
76
+ const events = [];
77
+ const handler = events.push.bind(events);
78
+ attach(port, handler);
79
+ return function (fs) {
80
+ detach(port, handler);
81
+ for (const event of events) {
82
+ const request = 'data' in event ? event.data : event;
83
+ void handleRequest(port, fs, request);
84
+ }
85
+ };
86
+ }
@@ -598,7 +598,7 @@ export class StoreFS extends FileSystem {
598
598
  return inode;
599
599
  }
600
600
  catch (e) {
601
- tx.abort();
601
+ await tx.abort();
602
602
  throw e;
603
603
  }
604
604
  }