@zenfs/core 0.12.1 → 0.12.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/src/stats.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type * as Node from 'fs';
2
2
  import { Cred } from './cred.js';
3
- import { S_IFDIR, S_IFLNK, S_IFMT, S_IFREG, S_IRWXG, S_IRWXO, S_IRWXU } from './emulation/constants.js';
3
+ import { S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK, S_IRWXG, S_IRWXO, S_IRWXU } from './emulation/constants.js';
4
+ import { size_max } from './inode.js';
4
5
 
5
6
  /**
6
7
  * Indicates the type of the given file. Applied to 'mode'.
@@ -60,21 +61,15 @@ export interface StatsLike<T extends number | bigint = number | bigint> {
60
61
  * Common code used by both Stats and BigIntStats.
61
62
  */
62
63
  export abstract class StatsCommon<T extends number | bigint> implements Node.StatsBase<T>, StatsLike {
63
- protected abstract _isBigint: boolean;
64
-
65
- protected get _typename(): string {
66
- return this._isBigint ? 'bigint' : 'number';
67
- }
68
-
69
- protected get _typename_inverse(): string {
70
- return this._isBigint ? 'number' : 'bigint';
71
- }
64
+ protected abstract _isBigint: T extends bigint ? true : false;
72
65
 
73
66
  protected _convert(arg: number | bigint | string | boolean): T {
74
67
  return (this._isBigint ? BigInt(arg) : Number(arg)) as T;
75
68
  }
76
69
 
77
- public blocks: T;
70
+ public get blocks(): T {
71
+ return this._convert(Math.ceil(Number(this.size) / 512));
72
+ }
78
73
 
79
74
  /**
80
75
  * Unix-style file mode (e.g. 0o644) that includes the type of the item.
@@ -184,17 +179,15 @@ export abstract class StatsCommon<T extends number | bigint> implements Node.Sta
184
179
  * Creates a new stats instance from a stats-like object. Can be used to copy stats (note)
185
180
  */
186
181
  constructor({ atimeMs, mtimeMs, ctimeMs, birthtimeMs, uid, gid, size, mode, ino }: Partial<StatsLike> = {}) {
187
- const currentTime = Date.now();
188
- const resolveT = (val: number | bigint | undefined, _default: number) =>
189
- typeof val == this._typename ? (val as T) : this._convert(typeof val == this._typename_inverse ? val! : _default);
190
- this.atimeMs = resolveT(atimeMs, currentTime);
191
- this.mtimeMs = resolveT(mtimeMs, currentTime);
192
- this.ctimeMs = resolveT(ctimeMs, currentTime);
193
- this.birthtimeMs = resolveT(birthtimeMs, currentTime);
194
- this.uid = resolveT(uid, 0);
195
- this.gid = resolveT(gid, 0);
196
- this.size = resolveT(size, 0);
197
- this.ino = resolveT(ino, 0);
182
+ const now = Date.now();
183
+ this.atimeMs = this._convert(atimeMs ?? now);
184
+ this.mtimeMs = this._convert(mtimeMs ?? now);
185
+ this.ctimeMs = this._convert(ctimeMs ?? now);
186
+ this.birthtimeMs = this._convert(birthtimeMs ?? now);
187
+ this.uid = this._convert(uid ?? 0);
188
+ this.gid = this._convert(gid ?? 0);
189
+ this.size = this._convert(size ?? 0);
190
+ this.ino = this._convert(ino ?? 0);
198
191
  const itemType: FileType = Number(mode) & S_IFMT || FileType.FILE;
199
192
 
200
193
  if (mode) {
@@ -209,8 +202,6 @@ export abstract class StatsCommon<T extends number | bigint> implements Node.Sta
209
202
  this.mode = this._convert(0o777);
210
203
  }
211
204
  }
212
- // number of 512B blocks allocated
213
- this.blocks = this._convert(Math.ceil(Number(size) / 512));
214
205
  // Check if mode also includes top-most bits, which indicate the file's type.
215
206
  if ((this.mode & S_IFMT) == 0) {
216
207
  this.mode = (this.mode | this._convert(itemType)) as T;
@@ -241,19 +232,19 @@ export abstract class StatsCommon<T extends number | bigint> implements Node.Sta
241
232
  // Currently unsupported
242
233
 
243
234
  public isSocket(): boolean {
244
- return false;
235
+ return (this.mode & S_IFMT) === S_IFSOCK;
245
236
  }
246
237
 
247
238
  public isBlockDevice(): boolean {
248
- return false;
239
+ return (this.mode & S_IFMT) === S_IFBLK;
249
240
  }
250
241
 
251
242
  public isCharacterDevice(): boolean {
252
- return false;
243
+ return (this.mode & S_IFMT) === S_IFCHR;
253
244
  }
254
245
 
255
246
  public isFIFO(): boolean {
256
- return false;
247
+ return (this.mode & S_IFMT) === S_IFIFO;
257
248
  }
258
249
 
259
250
  /**
@@ -315,16 +306,16 @@ export abstract class StatsCommon<T extends number | bigint> implements Node.Sta
315
306
  }
316
307
 
317
308
  public get atimeNs(): bigint {
318
- return BigInt(this.atimeMs);
309
+ return BigInt(this.atimeMs) * 1000n;
319
310
  }
320
311
  public get mtimeNs(): bigint {
321
- return BigInt(this.mtimeMs);
312
+ return BigInt(this.mtimeMs) * 1000n;
322
313
  }
323
314
  public get ctimeNs(): bigint {
324
- return BigInt(this.ctimeMs);
315
+ return BigInt(this.ctimeMs) * 1000n;
325
316
  }
326
317
  public get birthtimeNs(): bigint {
327
- return BigInt(this.birthtimeMs);
318
+ return BigInt(this.birthtimeMs) * 1000n;
328
319
  }
329
320
  }
330
321
 
@@ -334,41 +325,35 @@ export abstract class StatsCommon<T extends number | bigint> implements Node.Sta
334
325
  * Attribute descriptions are from `man 2 stat'
335
326
  * @see http://nodejs.org/api/fs.html#fs_class_fs_stats
336
327
  * @see http://man7.org/linux/man-pages/man2/stat.2.html
328
+ * @internal
337
329
  */
338
330
  export class Stats extends StatsCommon<number> implements Node.Stats, StatsLike {
339
- protected _isBigint = false;
340
-
341
- /**
342
- * Clones the stats object.
343
- * @deprecated use `new Stats(stats)`
344
- */
345
- public static clone(stats: Stats): Stats {
346
- return new Stats(stats);
347
- }
331
+ protected _isBigint = false as const;
348
332
  }
349
333
  Stats satisfies typeof Node.Stats;
350
334
 
351
335
  /**
352
336
  * Stats with bigint
353
337
  * @todo Implement with bigint instead of wrapping Stats
338
+ * @internal
354
339
  */
355
340
  export class BigIntStats extends StatsCommon<bigint> implements Node.BigIntStats, StatsLike {
356
- protected _isBigint = true;
357
-
358
- /**
359
- * Clone a stats object.
360
- * @deprecated use `new BigIntStats(stats)`
361
- */
362
- public static clone(stats: BigIntStats | Stats): BigIntStats {
363
- return new BigIntStats(stats);
364
- }
341
+ protected _isBigint = true as const;
365
342
  }
366
343
 
344
+ /**
345
+ * @internal
346
+ */
347
+ export const ZenFsType = 0x7a656e6673; // 'z' 'e' 'n' 'f' 's'
348
+
349
+ /**
350
+ * @hidden
351
+ */
367
352
  export class StatsFs implements Node.StatsFsBase<number> {
368
353
  /** Type of file system. */
369
- public type: number = 0;
354
+ public type: number = 0x7a656e6673;
370
355
  /** Optimal transfer block size. */
371
- public bsize: number = 0;
356
+ public bsize: number = 4096;
372
357
  /** Total data blocks in file system. */
373
358
  public blocks: number = 0;
374
359
  /** Free blocks in file system. */
@@ -376,16 +361,19 @@ export class StatsFs implements Node.StatsFsBase<number> {
376
361
  /** Available blocks for unprivileged users */
377
362
  public bavail: number = 0;
378
363
  /** Total file nodes in file system. */
379
- public files: number = 0;
364
+ public files: number = size_max;
380
365
  /** Free file nodes in file system. */
381
- public ffree: number = 0;
366
+ public ffree: number = size_max;
382
367
  }
383
368
 
369
+ /**
370
+ * @hidden
371
+ */
384
372
  export class BigIntStatsFs implements Node.StatsFsBase<bigint> {
385
373
  /** Type of file system. */
386
- public type: bigint = 0n;
374
+ public type: bigint = 0x7a656e6673n;
387
375
  /** Optimal transfer block size. */
388
- public bsize: bigint = 0n;
376
+ public bsize: bigint = 4096n;
389
377
  /** Total data blocks in file system. */
390
378
  public blocks: bigint = 0n;
391
379
  /** Free blocks in file system. */
@@ -393,7 +381,7 @@ export class BigIntStatsFs implements Node.StatsFsBase<bigint> {
393
381
  /** Available blocks for unprivileged users */
394
382
  public bavail: bigint = 0n;
395
383
  /** Total file nodes in file system. */
396
- public files: bigint = 0n;
384
+ public files: bigint = BigInt(size_max);
397
385
  /** Free file nodes in file system. */
398
- public ffree: bigint = 0n;
386
+ public ffree: bigint = BigInt(size_max);
399
387
  }