@zenfs/core 2.3.1 → 2.3.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.
@@ -11,6 +11,7 @@ import type { FileContents, ReaddirOptions } from './types.js';
11
11
  import { Buffer } from 'buffer';
12
12
  import '../polyfills.js';
13
13
  import { Dir, Dirent } from './dir.js';
14
+ import { SyncHandle } from './file.js';
14
15
  import { BigIntStats, Stats } from './stats.js';
15
16
  import { ReadStream, WriteStream } from './streams.js';
16
17
  export * as constants from './constants.js';
@@ -18,10 +19,6 @@ export declare class FileHandle implements promises.FileHandle {
18
19
  protected context: V_Context;
19
20
  readonly fd: number;
20
21
  protected _buffer?: Uint8Array;
21
- /**
22
- * Current position
23
- */
24
- protected _position: number;
25
22
  /**
26
23
  * Get the current file position.
27
24
  *
@@ -42,15 +39,16 @@ export declare class FileHandle implements promises.FileHandle {
42
39
  */
43
40
  protected closed: boolean;
44
41
  /** The path relative to the context's root */
45
- readonly path: string;
42
+ get path(): string;
46
43
  /** The internal FS associated with the handle */
47
- protected readonly fs: FileSystem;
44
+ protected get fs(): FileSystem;
48
45
  /** The path relative to the `FileSystem`'s root */
49
- readonly internalPath: string;
46
+ get internalPath(): string;
50
47
  /** The flag the handle was opened with */
51
- readonly flag: number;
48
+ get flag(): number;
52
49
  /** Stats for the handle */
53
- readonly inode: InodeLike;
50
+ get inode(): InodeLike;
51
+ protected _sync: SyncHandle;
54
52
  constructor(context: V_Context, fd: number);
55
53
  private get _isSync();
56
54
  private _emitChange;
@@ -52,7 +52,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
52
52
  });
53
53
  import { Buffer } from 'buffer';
54
54
  import { Exception, rethrow, setUVMessage, UV } from 'kerium';
55
- import { decodeUTF8, pick } from 'utilium';
55
+ import { decodeUTF8 } from 'utilium';
56
56
  import { defaultContext } from '../internal/contexts.js';
57
57
  import { hasAccess, InodeFlags, isBlockDevice, isCharacterDevice, isDirectory, isSymbolicLink } from '../internal/inode.js';
58
58
  import { dirname, join, matchesGlob, parse, resolve } from '../path.js';
@@ -73,10 +73,6 @@ export class FileHandle {
73
73
  context;
74
74
  fd;
75
75
  _buffer;
76
- /**
77
- * Current position
78
- */
79
- _position = 0;
80
76
  /**
81
77
  * Get the current file position.
82
78
  *
@@ -87,10 +83,10 @@ export class FileHandle {
87
83
  * @returns The current file position.
88
84
  */
89
85
  get position() {
90
- return this.flag & constants.O_APPEND ? this.inode.size : this._position;
86
+ return this._sync.position;
91
87
  }
92
88
  set position(value) {
93
- this._position = value;
89
+ this._sync.position = value;
94
90
  }
95
91
  /**
96
92
  * Whether the file has changes which have not been written to the FS
@@ -101,20 +97,30 @@ export class FileHandle {
101
97
  */
102
98
  closed = false;
103
99
  /** The path relative to the context's root */
104
- path;
100
+ get path() {
101
+ return this._sync.path;
102
+ }
105
103
  /** The internal FS associated with the handle */
106
- fs;
104
+ get fs() {
105
+ return this._sync.fs;
106
+ }
107
107
  /** The path relative to the `FileSystem`'s root */
108
- internalPath;
108
+ get internalPath() {
109
+ return this._sync.internalPath;
110
+ }
109
111
  /** The flag the handle was opened with */
110
- flag;
112
+ get flag() {
113
+ return this._sync.flag;
114
+ }
111
115
  /** Stats for the handle */
112
- inode;
116
+ get inode() {
117
+ return this._sync.inode;
118
+ }
119
+ _sync;
113
120
  constructor(context, fd) {
114
121
  this.context = context;
115
122
  this.fd = fd;
116
- const sync = fromFD(context, fd);
117
- Object.assign(this, pick(sync, 'path', 'fs', 'internalPath', 'flag', 'inode'));
123
+ this._sync = fromFD(context, fd);
118
124
  }
119
125
  get _isSync() {
120
126
  return !!(this.flag & constants.O_SYNC || this.inode.flags & InodeFlags.Sync);
@@ -246,7 +252,7 @@ export class FileHandle {
246
252
  if (!isCharacterDevice(this.inode) && !isBlockDevice(this.inode) && end > this.inode.size) {
247
253
  end = position + Math.max(this.inode.size - position, 0);
248
254
  }
249
- this._position = end;
255
+ this._sync.position = end;
250
256
  const uint8 = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
251
257
  await this.fs.read(this.internalPath, uint8.subarray(offset, offset + length), position, end);
252
258
  if (this._isSync)
@@ -348,7 +354,7 @@ export class FileHandle {
348
354
  this.inode.size = end;
349
355
  this.inode.mtimeMs = Date.now();
350
356
  this.inode.ctimeMs = Date.now();
351
- this._position = position + slice.byteLength;
357
+ this._sync.position = position + slice.byteLength;
352
358
  await this.fs.write(this.internalPath, slice, position);
353
359
  if (this._isSync)
354
360
  await this.sync();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenfs/core",
3
- "version": "2.3.1",
3
+ "version": "2.3.2",
4
4
  "description": "A filesystem, anywhere",
5
5
  "funding": {
6
6
  "type": "individual",
package/scripts/test.js CHANGED
@@ -40,13 +40,14 @@ if (options.help) {
40
40
  Paths: The setup files to run tests on
41
41
 
42
42
  Behavior:
43
- -a, --auto Automatically detect setup files
44
- -b, --build Run the npm build script prior to running tests
45
- -c, --common Also run tests not specific to any backend
46
- -e, --exit-on-fail If any tests suites fail, exit immediately
47
- -t, --test <glob> Which FS test suite(s) to run
48
- -f, --force Whether to use --test-force-exit
49
- -I, --inspect Use the inspector for debugging
43
+ -a, --auto Automatically detect setup files
44
+ -b, --build Run the npm build script prior to running tests
45
+ -c, --common Also run tests not specific to any backend
46
+ -e, --exit-on-fail If any tests suites fail, exit immediately
47
+ -t, --test <glob> Which FS test suite(s) to run
48
+ -f, --force Whether to use --test-force-exit
49
+ -I, --inspect Use the inspector for debugging
50
+ -s, --skip <pattern> Skip tests with names matching the given pattern.
50
51
 
51
52
  Output:
52
53
  -h, --help Outputs this help message
@@ -215,7 +216,7 @@ for (const setupFile of positionals) {
215
216
  '--test --experimental-test-coverage',
216
217
  options.force ? '--test-force-exit' : '',
217
218
  options.skip ? `--test-skip-pattern=${options.skip}` : '',
218
- testsGlob,
219
+ `'${testsGlob.replaceAll("'", "\\'")}'`,
219
220
  process.env.CMD,
220
221
  ].join(' '),
221
222
  {
@@ -1,7 +1,9 @@
1
1
  import { Buffer } from 'buffer';
2
2
  import assert from 'node:assert/strict';
3
+ import type { OpenMode, PathLike } from 'node:fs';
3
4
  import { suite, test } from 'node:test';
4
- import { fs } from '../common.js';
5
+ import { promisify } from 'node:util';
6
+ import { fs, type Callback } from '../common.js';
5
7
 
6
8
  const filepath = 'x.txt';
7
9
  const expected = 'xyz\n';
@@ -65,4 +67,25 @@ suite('read', () => {
65
67
  assert.equal(buffer.subarray(10, buffer.length).toString(), expected);
66
68
  assert.equal(bytesRead, expected.length);
67
69
  });
70
+
71
+ test('read using callback API', async () => {
72
+ // @zenfs/core#239
73
+ const path = '/text.txt';
74
+
75
+ fs.writeFileSync(path, 'hello world');
76
+ const fd: number = (await promisify<PathLike, OpenMode, number | string>(fs.open)(path, 0, 0)) as any;
77
+
78
+ const read = promisify(fs.read);
79
+
80
+ const buf = Buffer.alloc(1024);
81
+ const n0 = await read(fd, buf, 0, 1024, undefined);
82
+ assert.equal(n0, 11);
83
+ assert.equal(buf.subarray(0, n0).toString('utf8'), 'hello world');
84
+
85
+ const n1 = await read(fd, buf, 0, 1024, undefined);
86
+ assert.equal(n1, 0);
87
+ assert.equal(buf.subarray(0, n1).toString('utf8'), '');
88
+
89
+ await promisify(fs.close)(fd);
90
+ });
68
91
  });