@zenfs/core 1.11.2 → 1.11.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.
@@ -458,8 +458,7 @@ export class CopyOnWriteFS extends FileSystem {
458
458
  return;
459
459
  }
460
460
  const data = new Uint8Array(stats.size);
461
- const readable = __addDisposableResource(env_2, await this.readable.openFile(path, 'r'), true);
462
- await readable.read(data);
461
+ await this.readable.read(path, data, 0, stats.size);
463
462
  const writable = __addDisposableResource(env_2, await this.writable.createFile(path, 'w', stats.mode, stats), true);
464
463
  await writable.write(data);
465
464
  }
@@ -211,10 +211,9 @@ export function Async(FS) {
211
211
  if (!stats.isDirectory()) {
212
212
  const env_1 = { stack: [], error: void 0, hasError: false };
213
213
  try {
214
- const asyncFile = __addDisposableResource(env_1, await this.openFile(path, parseFlag('r')), true);
215
214
  const syncFile = __addDisposableResource(env_1, this._sync.createFileSync(path, parseFlag('w'), stats.mode, stats), false);
216
215
  const buffer = new Uint8Array(stats.size);
217
- await asyncFile.read(buffer);
216
+ await this.read(path, buffer, 0, stats.size);
218
217
  syncFile.writeSync(buffer, 0, stats.size);
219
218
  return;
220
219
  }
@@ -223,9 +222,7 @@ export function Async(FS) {
223
222
  env_1.hasError = true;
224
223
  }
225
224
  finally {
226
- const result_1 = __disposeResources(env_1);
227
- if (result_1)
228
- await result_1;
225
+ __disposeResources(env_1);
229
226
  }
230
227
  }
231
228
  if (path !== '/') {
@@ -113,9 +113,8 @@ export class FileHandle {
113
113
  */
114
114
  async truncate(length) {
115
115
  length || (length = 0);
116
- if (length < 0) {
116
+ if (length < 0)
117
117
  throw new ErrnoError(Errno.EINVAL);
118
- }
119
118
  await this.file.truncate(length);
120
119
  this._emitChange();
121
120
  }
@@ -162,11 +161,10 @@ export class FileHandle {
162
161
  offset = buffer.offset;
163
162
  buffer = buffer.buffer;
164
163
  }
165
- if (!Number.isSafeInteger(position)) {
166
- position = this.file.position;
167
- }
164
+ const pos = Number.isSafeInteger(position) ? position : this.file.position;
168
165
  buffer || (buffer = new Uint8Array((await this.file.stat()).size));
169
- return this.file.read(buffer, offset !== null && offset !== void 0 ? offset : undefined, length !== null && length !== void 0 ? length : undefined, position !== null && position !== void 0 ? position : undefined);
166
+ offset !== null && offset !== void 0 ? offset : (offset = 0);
167
+ return this.file.read(buffer, offset, length !== null && length !== void 0 ? length : buffer.byteLength - offset, pos);
170
168
  }
171
169
  async readFile(_options) {
172
170
  const options = normalizeOptions(_options, null, 'r', 0o444);
@@ -501,11 +499,7 @@ export async function writeFile(path, data, _options) {
501
499
  try {
502
500
  const options = normalizeOptions(_options, 'utf8', 'w+', 0o644);
503
501
  const handle = __addDisposableResource(env_3, path instanceof FileHandle ? path : await open.call(this, path.toString(), options.flag, options.mode), true);
504
- const _data = typeof data == 'string'
505
- ? data
506
- : data instanceof DataView
507
- ? new Uint8Array(data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength))
508
- : data;
502
+ const _data = typeof data == 'string' ? data : data instanceof DataView ? new Uint8Array(data.buffer, data.byteOffset, data.byteLength) : data;
509
503
  if (typeof _data != 'string' && !(_data instanceof Uint8Array)) {
510
504
  throw new ErrnoError(Errno.EINVAL, 'The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received ' + typeof data, handle.file.path, 'writeFile');
511
505
  }
@@ -2,7 +2,7 @@ import { EventEmitter } from 'eventemitter3';
2
2
  import { ErrnoError } from '../internal/error.js';
3
3
  import { isStatsEqual } from '../stats.js';
4
4
  import { normalizePath } from '../utils.js';
5
- import { dirname, join, relative } from './path.js';
5
+ import { basename, dirname, join, relative } from './path.js';
6
6
  import { statSync } from './sync.js';
7
7
  /**
8
8
  * Base class for file system watchers.
@@ -143,16 +143,13 @@ export function emitChange(context, eventType, filename) {
143
143
  if (context)
144
144
  filename = join((_a = context.root) !== null && _a !== void 0 ? _a : '/', filename);
145
145
  filename = normalizePath(filename);
146
- // Notify watchers on parent directories if they are watching recursively
147
- let parent = filename, normalizedFilename;
148
- while (normalizedFilename != '/') {
149
- normalizedFilename = parent;
150
- parent = dirname(parent);
151
- const parentWatchers = watchers.get(parent);
152
- if (!parentWatchers)
146
+ // Notify watchers, including ones on parent directories if they are watching recursively
147
+ for (let path = filename; path != '/'; path = dirname(path)) {
148
+ const watchersForPath = watchers.get(path);
149
+ if (!watchersForPath)
153
150
  continue;
154
- for (const watcher of parentWatchers) {
155
- watcher.emit('change', eventType, relative(parent, filename));
151
+ for (const watcher of watchersForPath) {
152
+ watcher.emit('change', eventType, relative(path, filename) || basename(filename));
156
153
  }
157
154
  }
158
155
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenfs/core",
3
- "version": "1.11.2",
3
+ "version": "1.11.3",
4
4
  "description": "A filesystem, anywhere",
5
5
  "funding": {
6
6
  "type": "individual",
@@ -13,13 +13,18 @@ await fs.promises.writeFile(testFile, 'Initial content');
13
13
  */
14
14
  await suite('Watch Features', () => {
15
15
  test('fs.watch should emit events on file change', async () => {
16
+ const { promise, resolve } = Promise.withResolvers<[string, string]>();
17
+
16
18
  using watcher = fs.watch(testFile, (eventType, filename) => {
17
- assert.equal(eventType, 'change');
18
- assert.equal(filename, 'test.txt');
19
+ resolve([eventType, filename]);
19
20
  });
20
21
 
21
22
  // Modify the file to trigger the event
22
23
  await fs.promises.writeFile(testFile, 'Updated content');
24
+
25
+ const [eventType, filename] = await promise;
26
+ assert.equal(eventType, 'change');
27
+ assert.equal(filename, 'test.txt');
23
28
  });
24
29
 
25
30
  test('fs.watch should emit events on file rename (delete)', async () => {