@zenfs/core 1.1.1 → 1.1.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.
package/dist/devices.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { FileReadResult } from 'fs/promises';
1
+ import type { FileReadResult } from 'node:fs/promises';
2
2
  import { InMemoryStore } from './backends/memory.js';
3
3
  import { StoreFS } from './backends/store/fs.js';
4
4
  import { File } from './file.js';
@@ -1,4 +1,4 @@
1
- import type { Dir as _Dir, Dirent as _Dirent } from 'fs';
1
+ import type { Dir as _Dir, Dirent as _Dirent } from 'node:fs';
2
2
  import type { Stats } from '../stats.js';
3
3
  import type { Callback } from '../utils.js';
4
4
  export declare class Dirent implements _Dirent {
@@ -1,7 +1,6 @@
1
1
  import { Buffer } from 'buffer';
2
2
  import type * as fs from 'node:fs';
3
3
  import type * as promises from 'node:fs/promises';
4
- import type { CreateReadStreamOptions, CreateWriteStreamOptions, FileChangeInfo, FileReadResult, FlagAndOpenMode } from 'node:fs/promises';
5
4
  import type { Stream } from 'node:stream';
6
5
  import type { ReadableStream as TReadableStream } from 'node:stream/web';
7
6
  import type { Interface as ReadlineInterface } from 'readline';
@@ -60,7 +59,7 @@ export declare class FileHandle implements promises.FileHandle {
60
59
  * - `mode` defaults to `0o666`.
61
60
  * - `flag` defaults to `'a'`.
62
61
  */
63
- appendFile(data: string | Uint8Array, _options?: (fs.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding): Promise<void>;
62
+ appendFile(data: string | Uint8Array, _options?: (fs.ObjectEncodingOptions & promises.FlagAndOpenMode) | BufferEncoding): Promise<void>;
64
63
  /**
65
64
  * Asynchronously reads data from the file.
66
65
  * The `FileHandle` must have been opened for reading.
@@ -69,7 +68,7 @@ export declare class FileHandle implements promises.FileHandle {
69
68
  * @param length The number of bytes to read.
70
69
  * @param position The offset from the beginning of the file from which data should be read. If `null`, data will be read from the current position.
71
70
  */
72
- read<TBuffer extends NodeJS.ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number | null): Promise<FileReadResult<TBuffer>>;
71
+ read<TBuffer extends NodeJS.ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number | null): Promise<promises.FileReadResult<TBuffer>>;
73
72
  /**
74
73
  * Asynchronously reads the entire contents of a file. The underlying file will _not_ be closed automatically.
75
74
  * The `FileHandle` must have been opened for reading.
@@ -79,7 +78,7 @@ export declare class FileHandle implements promises.FileHandle {
79
78
  readFile(_options?: {
80
79
  flag?: fs.OpenMode;
81
80
  }): Promise<Buffer>;
82
- readFile(_options: (fs.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding): Promise<string>;
81
+ readFile(_options: (fs.ObjectEncodingOptions & promises.FlagAndOpenMode) | BufferEncoding): Promise<string>;
83
82
  /**
84
83
  * Returns a `ReadableStream` that may be used to read the files data.
85
84
  *
@@ -153,12 +152,12 @@ export declare class FileHandle implements promises.FileHandle {
153
152
  * Creates a stream for reading from the file.
154
153
  * @param options Options for the readable stream
155
154
  */
156
- createReadStream(options?: CreateReadStreamOptions): ReadStream;
155
+ createReadStream(options?: promises.CreateReadStreamOptions): ReadStream;
157
156
  /**
158
157
  * Creates a stream for writing to the file.
159
158
  * @param options Options for the writeable stream.
160
159
  */
161
- createWriteStream(options?: CreateWriteStreamOptions): WriteStream;
160
+ createWriteStream(options?: promises.CreateWriteStreamOptions): WriteStream;
162
161
  }
163
162
  export declare function rename(oldPath: fs.PathLike, newPath: fs.PathLike): Promise<void>;
164
163
  /**
@@ -300,9 +299,9 @@ export declare function lutimes(path: fs.PathLike, atime: fs.TimeLike, mtime: fs
300
299
  */
301
300
  export declare function realpath(path: fs.PathLike, options: fs.BufferEncodingOption): Promise<Buffer>;
302
301
  export declare function realpath(path: fs.PathLike, options?: fs.EncodingOption | BufferEncoding): Promise<string>;
303
- export declare function watch(filename: fs.PathLike, options?: fs.WatchOptions | BufferEncoding): AsyncIterable<FileChangeInfo<string>>;
304
- export declare function watch(filename: fs.PathLike, options: fs.WatchOptions | fs.BufferEncodingOption): AsyncIterable<FileChangeInfo<Buffer>>;
305
- export declare function watch(filename: fs.PathLike, options?: fs.WatchOptions | string): AsyncIterable<FileChangeInfo<string>> | AsyncIterable<FileChangeInfo<Buffer>>;
302
+ export declare function watch(filename: fs.PathLike, options?: fs.WatchOptions | BufferEncoding): AsyncIterable<promises.FileChangeInfo<string>>;
303
+ export declare function watch(filename: fs.PathLike, options: fs.WatchOptions | fs.BufferEncodingOption): AsyncIterable<promises.FileChangeInfo<Buffer>>;
304
+ export declare function watch(filename: fs.PathLike, options?: fs.WatchOptions | string): AsyncIterable<promises.FileChangeInfo<string>> | AsyncIterable<promises.FileChangeInfo<Buffer>>;
306
305
  export declare function access(path: fs.PathLike, mode?: number): Promise<void>;
307
306
  /**
308
307
  * Asynchronous `rm`. Removes files or directories (recursively).
@@ -871,20 +871,28 @@ realpath;
871
871
  export function watch(filename, options = {}) {
872
872
  return {
873
873
  [Symbol.asyncIterator]() {
874
- const watcher = new FSWatcher(filename.toString(), typeof options != 'string' ? options : { encoding: options });
875
- function withDone(done) {
876
- return function () {
877
- const event = Promise.withResolvers();
878
- watcher.on('change', (eventType, filename) => {
879
- event.resolve({ value: { eventType, filename }, done });
880
- });
881
- return event.promise;
882
- };
874
+ const watcher = new FSWatcher(filename.toString(), typeof options !== 'string' ? options : { encoding: options });
875
+ // A queue to hold change events, since we need to resolve them in the async iterator
876
+ const eventQueue = [];
877
+ watcher.on('change', (eventType, filename) => {
878
+ eventQueue.shift()?.({ value: { eventType, filename }, done: false });
879
+ });
880
+ function cleanup() {
881
+ watcher.close();
882
+ for (const resolve of eventQueue) {
883
+ resolve({ value: null, done: true });
884
+ }
885
+ eventQueue.length = 0; // Clear the queue
886
+ return Promise.resolve({ value: null, done: true });
883
887
  }
884
888
  return {
885
- next: withDone(false),
886
- return: withDone(true),
887
- throw: withDone(true),
889
+ async next() {
890
+ const { promise, resolve } = Promise.withResolvers();
891
+ eventQueue.push(resolve);
892
+ return promise;
893
+ },
894
+ return: cleanup,
895
+ throw: cleanup,
888
896
  };
889
897
  },
890
898
  };
@@ -1,4 +1,4 @@
1
- import type * as Node from 'fs';
1
+ import type * as Node from 'node:fs';
2
2
  import { Readable, Writable } from 'readable-stream';
3
3
  import type { Callback } from '../utils.js';
4
4
  export declare class ReadStream extends Readable implements Node.ReadStream {
package/dist/stats.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type * as Node from 'fs';
1
+ import type * as Node from 'node:fs';
2
2
  import { type Credentials } from './credentials.js';
3
3
  import { S_IFDIR, S_IFLNK, S_IFREG } from './emulation/constants.js';
4
4
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenfs/core",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "A filesystem, anywhere",
5
5
  "funding": {
6
6
  "type": "individual",
package/src/devices.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { FileReadResult } from 'fs/promises';
1
+ import type { FileReadResult } from 'node:fs/promises';
2
2
  import { InMemoryStore } from './backends/memory.js';
3
3
  import { StoreFS } from './backends/store/fs.js';
4
4
  import { S_IFBLK, S_IFCHR } from './emulation/constants.js';
@@ -1,4 +1,4 @@
1
- import type { Dir as _Dir, Dirent as _Dirent } from 'fs';
1
+ import type { Dir as _Dir, Dirent as _Dirent } from 'node:fs';
2
2
  import { Errno, ErrnoError } from '../error.js';
3
3
  import type { Stats } from '../stats.js';
4
4
  import type { Callback } from '../utils.js';
@@ -2,11 +2,9 @@
2
2
  import { Buffer } from 'buffer';
3
3
  import type * as fs from 'node:fs';
4
4
  import type * as promises from 'node:fs/promises';
5
- import type { CreateReadStreamOptions, CreateWriteStreamOptions, FileChangeInfo, FileReadResult, FlagAndOpenMode } from 'node:fs/promises';
6
5
  import type { Stream } from 'node:stream';
7
- import type { ReadableStream as TReadableStream } from 'node:stream/web';
6
+ import type { ReadableStreamController, ReadableStream as TReadableStream } from 'node:stream/web';
8
7
  import type { Interface as ReadlineInterface } from 'readline';
9
- import type { ReadableStreamController } from 'stream/web';
10
8
  import { Errno, ErrnoError } from '../error.js';
11
9
  import type { File } from '../file.js';
12
10
  import { flagToMode, isAppendable, isExclusive, isReadable, isTruncating, isWriteable, parseFlag } from '../file.js';
@@ -107,7 +105,7 @@ export class FileHandle implements promises.FileHandle {
107
105
  * - `mode` defaults to `0o666`.
108
106
  * - `flag` defaults to `'a'`.
109
107
  */
110
- public async appendFile(data: string | Uint8Array, _options: (fs.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding = {}): Promise<void> {
108
+ public async appendFile(data: string | Uint8Array, _options: (fs.ObjectEncodingOptions & promises.FlagAndOpenMode) | BufferEncoding = {}): Promise<void> {
111
109
  const options = normalizeOptions(_options, 'utf8', 'a', 0o644);
112
110
  const flag = parseFlag(options.flag);
113
111
  if (!isAppendable(flag)) {
@@ -129,7 +127,7 @@ export class FileHandle implements promises.FileHandle {
129
127
  * @param length The number of bytes to read.
130
128
  * @param position The offset from the beginning of the file from which data should be read. If `null`, data will be read from the current position.
131
129
  */
132
- public read<TBuffer extends NodeJS.ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number | null): Promise<FileReadResult<TBuffer>> {
130
+ public read<TBuffer extends NodeJS.ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number | null): Promise<promises.FileReadResult<TBuffer>> {
133
131
  if (isNaN(+position!)) {
134
132
  position = this.file.position;
135
133
  }
@@ -143,8 +141,8 @@ export class FileHandle implements promises.FileHandle {
143
141
  * If a flag is not provided, it defaults to `'r'`.
144
142
  */
145
143
  public async readFile(_options?: { flag?: fs.OpenMode }): Promise<Buffer>;
146
- public async readFile(_options: (fs.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding): Promise<string>;
147
- public async readFile(_options?: (fs.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding): Promise<string | Buffer> {
144
+ public async readFile(_options: (fs.ObjectEncodingOptions & promises.FlagAndOpenMode) | BufferEncoding): Promise<string>;
145
+ public async readFile(_options?: (fs.ObjectEncodingOptions & promises.FlagAndOpenMode) | BufferEncoding): Promise<string | Buffer> {
148
146
  const options = normalizeOptions(_options, null, 'r', 0o444);
149
147
  const flag = parseFlag(options.flag);
150
148
  if (!isReadable(flag)) {
@@ -334,7 +332,7 @@ export class FileHandle implements promises.FileHandle {
334
332
  * Creates a stream for reading from the file.
335
333
  * @param options Options for the readable stream
336
334
  */
337
- public createReadStream(options?: CreateReadStreamOptions): ReadStream {
335
+ public createReadStream(options?: promises.CreateReadStreamOptions): ReadStream {
338
336
  const stream = new ReadStream({
339
337
  highWaterMark: options?.highWaterMark || 64 * 1024,
340
338
  encoding: options!.encoding!,
@@ -359,7 +357,7 @@ export class FileHandle implements promises.FileHandle {
359
357
  * Creates a stream for writing to the file.
360
358
  * @param options Options for the writeable stream.
361
359
  */
362
- public createWriteStream(options?: CreateWriteStreamOptions): WriteStream {
360
+ public createWriteStream(options?: promises.CreateWriteStreamOptions): WriteStream {
363
361
  const streamOptions = {
364
362
  highWaterMark: options?.highWaterMark,
365
363
  encoding: options?.encoding,
@@ -887,28 +885,38 @@ export async function realpath(path: fs.PathLike, options?: fs.EncodingOption |
887
885
  }
888
886
  realpath satisfies typeof promises.realpath;
889
887
 
890
- export function watch(filename: fs.PathLike, options?: fs.WatchOptions | BufferEncoding): AsyncIterable<FileChangeInfo<string>>;
891
- export function watch(filename: fs.PathLike, options: fs.WatchOptions | fs.BufferEncodingOption): AsyncIterable<FileChangeInfo<Buffer>>;
892
- export function watch(filename: fs.PathLike, options?: fs.WatchOptions | string): AsyncIterable<FileChangeInfo<string>> | AsyncIterable<FileChangeInfo<Buffer>>;
893
- export function watch<T extends string | Buffer>(filename: fs.PathLike, options: fs.WatchOptions | string = {}): AsyncIterable<FileChangeInfo<T>> {
888
+ export function watch(filename: fs.PathLike, options?: fs.WatchOptions | BufferEncoding): AsyncIterable<promises.FileChangeInfo<string>>;
889
+ export function watch(filename: fs.PathLike, options: fs.WatchOptions | fs.BufferEncodingOption): AsyncIterable<promises.FileChangeInfo<Buffer>>;
890
+ export function watch(filename: fs.PathLike, options?: fs.WatchOptions | string): AsyncIterable<promises.FileChangeInfo<string>> | AsyncIterable<promises.FileChangeInfo<Buffer>>;
891
+ export function watch<T extends string | Buffer>(filename: fs.PathLike, options: fs.WatchOptions | string = {}): AsyncIterable<promises.FileChangeInfo<T>> {
894
892
  return {
895
- [Symbol.asyncIterator](): AsyncIterator<FileChangeInfo<T>> {
896
- const watcher = new FSWatcher<T>(filename.toString(), typeof options != 'string' ? options : { encoding: options as BufferEncoding | 'buffer' });
897
-
898
- function withDone(done: boolean) {
899
- return function () {
900
- const event = Promise.withResolvers<IteratorResult<FileChangeInfo<T>>>();
901
- watcher.on('change', (eventType, filename) => {
902
- event.resolve({ value: { eventType, filename }, done });
903
- });
904
- return event.promise;
905
- };
893
+ [Symbol.asyncIterator](): AsyncIterator<promises.FileChangeInfo<T>> {
894
+ const watcher = new FSWatcher<T>(filename.toString(), typeof options !== 'string' ? options : { encoding: options as BufferEncoding | 'buffer' });
895
+
896
+ // A queue to hold change events, since we need to resolve them in the async iterator
897
+ const eventQueue: ((value: IteratorResult<promises.FileChangeInfo<T>>) => void)[] = [];
898
+
899
+ watcher.on('change', (eventType: promises.FileChangeInfo<T>['eventType'], filename: T) => {
900
+ eventQueue.shift()?.({ value: { eventType, filename }, done: false });
901
+ });
902
+
903
+ function cleanup() {
904
+ watcher.close();
905
+ for (const resolve of eventQueue) {
906
+ resolve({ value: null, done: true });
907
+ }
908
+ eventQueue.length = 0; // Clear the queue
909
+ return Promise.resolve({ value: null, done: true as const });
906
910
  }
907
911
 
908
912
  return {
909
- next: withDone(false),
910
- return: withDone(true),
911
- throw: withDone(true),
913
+ async next() {
914
+ const { promise, resolve } = Promise.withResolvers<IteratorResult<promises.FileChangeInfo<T>>>();
915
+ eventQueue.push(resolve);
916
+ return promise;
917
+ },
918
+ return: cleanup,
919
+ throw: cleanup,
912
920
  };
913
921
  },
914
922
  };
@@ -1,4 +1,4 @@
1
- import type * as Node from 'fs';
1
+ import type * as Node from 'node:fs';
2
2
  import { Readable, Writable } from 'readable-stream';
3
3
  import type { Callback } from '../utils.js';
4
4
  import { ErrnoError, Errno } from '../error.js';
package/src/stats.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type * as Node from 'fs';
1
+ import type * as Node from 'node:fs';
2
2
  import { credentials, type Credentials } from './credentials.js';
3
3
  import { S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK, S_IRWXG, S_IRWXO, S_IRWXU, size_max } from './emulation/constants.js';
4
4