@componentor/fs 3.0.30 → 3.0.31

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/index.d.mts CHANGED
@@ -2,7 +2,7 @@
2
2
  * Type definitions for the VFS-based filesystem.
3
3
  * Mirrors Node.js fs module interfaces.
4
4
  */
5
- type Encoding = 'utf8' | 'utf-8' | 'ascii' | 'base64' | 'hex' | 'binary' | 'latin1' | 'ucs2' | 'ucs-2' | 'utf16le' | 'utf-16le';
5
+ type Encoding = 'utf8' | 'utf-8' | 'ascii' | 'base64' | 'hex' | 'binary' | 'latin1' | 'ucs2' | 'ucs-2' | 'utf16le' | 'utf-16le' | 'buffer';
6
6
  interface ReadOptions {
7
7
  encoding?: Encoding | null;
8
8
  flag?: string;
@@ -486,6 +486,7 @@ declare class VFSFileSystem {
486
486
  *
487
487
  * Returns a Promise that resolves when the new mode is ready. */
488
488
  setMode(newMode: FSMode): Promise<void>;
489
+ private _validateCb;
489
490
  readFile(filePath: string, callback: (err: Error | null, data?: Uint8Array | string) => void): void;
490
491
  readFile(filePath: string, options: ReadOptions | Encoding | null, callback: (err: Error | null, data?: Uint8Array | string) => void): void;
491
492
  writeFile(filePath: string, data: string | Uint8Array, callback: (err: Error | null) => void): void;
package/dist/index.js CHANGED
@@ -1775,7 +1775,7 @@ function toPathString(p) {
1775
1775
  }
1776
1776
  return decodeURIComponent(p.pathname);
1777
1777
  }
1778
- return String(p);
1778
+ throw new TypeError('The "path" argument must be of type string, Uint8Array, or URL. Received ' + typeof p);
1779
1779
  }
1780
1780
  var sep = "/";
1781
1781
  var delimiter = ":";
@@ -3124,7 +3124,7 @@ var VFSFileSystem = class {
3124
3124
  statfs(path, callback) {
3125
3125
  const result = this.statfsSync(path);
3126
3126
  if (callback) {
3127
- callback(null, result);
3127
+ setTimeout(() => callback(null, result), 0);
3128
3128
  return;
3129
3129
  }
3130
3130
  return Promise.resolve(result);
@@ -3155,18 +3155,24 @@ var VFSFileSystem = class {
3155
3155
  let handle = null;
3156
3156
  let finished = false;
3157
3157
  const cleanup = async () => {
3158
- if (handle) {
3158
+ if (handle && ownsHandle) {
3159
3159
  try {
3160
3160
  await handle.close();
3161
3161
  } catch {
3162
3162
  }
3163
- handle = null;
3164
3163
  }
3164
+ handle = null;
3165
3165
  };
3166
+ const providedFd = opts?.fd;
3167
+ let ownsHandle = providedFd == null;
3166
3168
  const readFn = async () => {
3167
3169
  if (finished) return { done: true };
3168
3170
  if (!handle) {
3169
- handle = await this.promises.open(toPathString(filePath), opts?.flags ?? "r");
3171
+ if (providedFd != null) {
3172
+ handle = createFileHandle(providedFd, this._async);
3173
+ } else {
3174
+ handle = await this.promises.open(toPathString(filePath), opts?.flags ?? "r");
3175
+ }
3170
3176
  }
3171
3177
  const readLen = end !== void 0 ? Math.min(highWaterMark, end - position + 1) : highWaterMark;
3172
3178
  if (readLen <= 0) {
@@ -3197,9 +3203,15 @@ var VFSFileSystem = class {
3197
3203
  const opts = typeof options === "string" ? { } : options;
3198
3204
  let position = opts?.start ?? 0;
3199
3205
  let handle = null;
3206
+ const providedWFd = opts?.fd;
3207
+ const ownsWHandle = providedWFd == null;
3200
3208
  const writeFn = async (chunk) => {
3201
3209
  if (!handle) {
3202
- handle = await this.promises.open(toPathString(filePath), opts?.flags ?? "w");
3210
+ if (providedWFd != null) {
3211
+ handle = createFileHandle(providedWFd, this._async);
3212
+ } else {
3213
+ handle = await this.promises.open(toPathString(filePath), opts?.flags ?? "w");
3214
+ }
3203
3215
  }
3204
3216
  const { bytesWritten } = await handle.write(chunk, 0, chunk.byteLength, position);
3205
3217
  position += bytesWritten;
@@ -3209,7 +3221,9 @@ var VFSFileSystem = class {
3209
3221
  if (opts?.flush) {
3210
3222
  await handle.sync();
3211
3223
  }
3212
- await handle.close();
3224
+ if (ownsWHandle) {
3225
+ await handle.close();
3226
+ }
3213
3227
  handle = null;
3214
3228
  }
3215
3229
  };
@@ -3319,8 +3333,17 @@ var VFSFileSystem = class {
3319
3333
  }
3320
3334
  return this.readyPromise;
3321
3335
  }
3336
+ // ========== Callback API ==========
3337
+ // Node.js-style callback overloads for all async operations.
3338
+ // These delegate to this.promises.* and adapt the result to (err, result) callbacks.
3339
+ _validateCb(cb) {
3340
+ if (typeof cb !== "function") {
3341
+ throw new TypeError('The "cb" argument must be of type function. Received ' + typeof cb);
3342
+ }
3343
+ }
3322
3344
  readFile(filePath, optionsOrCallback, callback) {
3323
3345
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3346
+ this._validateCb(cb);
3324
3347
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3325
3348
  this.promises.readFile(filePath, opts).then(
3326
3349
  (result) => setTimeout(() => cb(null, result), 0),
@@ -3329,6 +3352,7 @@ var VFSFileSystem = class {
3329
3352
  }
3330
3353
  writeFile(filePath, data, optionsOrCallback, callback) {
3331
3354
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3355
+ this._validateCb(cb);
3332
3356
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3333
3357
  this.promises.writeFile(filePath, data, opts).then(
3334
3358
  () => setTimeout(() => cb(null), 0),
@@ -3337,6 +3361,7 @@ var VFSFileSystem = class {
3337
3361
  }
3338
3362
  appendFile(filePath, data, optionsOrCallback, callback) {
3339
3363
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3364
+ this._validateCb(cb);
3340
3365
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3341
3366
  this.promises.appendFile(filePath, data, opts).then(
3342
3367
  () => setTimeout(() => cb(null), 0),
@@ -3345,6 +3370,7 @@ var VFSFileSystem = class {
3345
3370
  }
3346
3371
  mkdir(filePath, optionsOrCallback, callback) {
3347
3372
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3373
+ this._validateCb(cb);
3348
3374
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3349
3375
  this.promises.mkdir(filePath, opts).then(
3350
3376
  (result) => setTimeout(() => cb(null, result), 0),
@@ -3353,6 +3379,7 @@ var VFSFileSystem = class {
3353
3379
  }
3354
3380
  rmdir(filePath, optionsOrCallback, callback) {
3355
3381
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3382
+ this._validateCb(cb);
3356
3383
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3357
3384
  this.promises.rmdir(filePath, opts).then(
3358
3385
  () => setTimeout(() => cb(null), 0),
@@ -3361,6 +3388,7 @@ var VFSFileSystem = class {
3361
3388
  }
3362
3389
  rm(filePath, optionsOrCallback, callback) {
3363
3390
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3391
+ this._validateCb(cb);
3364
3392
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3365
3393
  this.promises.rm(filePath, opts).then(
3366
3394
  () => setTimeout(() => cb(null), 0),
@@ -3368,6 +3396,7 @@ var VFSFileSystem = class {
3368
3396
  );
3369
3397
  }
3370
3398
  unlink(filePath, callback) {
3399
+ this._validateCb(callback);
3371
3400
  this.promises.unlink(filePath).then(
3372
3401
  () => setTimeout(() => callback(null), 0),
3373
3402
  (err) => setTimeout(() => callback(err), 0)
@@ -3375,6 +3404,7 @@ var VFSFileSystem = class {
3375
3404
  }
3376
3405
  readdir(filePath, optionsOrCallback, callback) {
3377
3406
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3407
+ this._validateCb(cb);
3378
3408
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3379
3409
  this.promises.readdir(filePath, opts).then(
3380
3410
  (result) => setTimeout(() => cb(null, result), 0),
@@ -3383,6 +3413,7 @@ var VFSFileSystem = class {
3383
3413
  }
3384
3414
  stat(filePath, optionsOrCallback, callback) {
3385
3415
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3416
+ this._validateCb(cb);
3386
3417
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3387
3418
  this.promises.stat(filePath, opts).then(
3388
3419
  (result) => setTimeout(() => cb(null, result), 0),
@@ -3391,6 +3422,7 @@ var VFSFileSystem = class {
3391
3422
  }
3392
3423
  lstat(filePath, optionsOrCallback, callback) {
3393
3424
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3425
+ this._validateCb(cb);
3394
3426
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3395
3427
  this.promises.lstat(filePath, opts).then(
3396
3428
  (result) => setTimeout(() => cb(null, result), 0),
@@ -3399,6 +3431,7 @@ var VFSFileSystem = class {
3399
3431
  }
3400
3432
  access(filePath, modeOrCallback, callback) {
3401
3433
  const cb = typeof modeOrCallback === "function" ? modeOrCallback : callback;
3434
+ this._validateCb(cb);
3402
3435
  const mode = typeof modeOrCallback === "function" ? void 0 : modeOrCallback;
3403
3436
  this.promises.access(filePath, mode).then(
3404
3437
  () => setTimeout(() => cb(null), 0),
@@ -3406,6 +3439,7 @@ var VFSFileSystem = class {
3406
3439
  );
3407
3440
  }
3408
3441
  rename(oldPath, newPath, callback) {
3442
+ this._validateCb(callback);
3409
3443
  this.promises.rename(oldPath, newPath).then(
3410
3444
  () => setTimeout(() => callback(null), 0),
3411
3445
  (err) => setTimeout(() => callback(err), 0)
@@ -3413,6 +3447,7 @@ var VFSFileSystem = class {
3413
3447
  }
3414
3448
  copyFile(src, dest, modeOrCallback, callback) {
3415
3449
  const cb = typeof modeOrCallback === "function" ? modeOrCallback : callback;
3450
+ this._validateCb(cb);
3416
3451
  const mode = typeof modeOrCallback === "function" ? void 0 : modeOrCallback;
3417
3452
  this.promises.copyFile(src, dest, mode).then(
3418
3453
  () => setTimeout(() => cb(null), 0),
@@ -3421,6 +3456,7 @@ var VFSFileSystem = class {
3421
3456
  }
3422
3457
  truncate(filePath, lenOrCallback, callback) {
3423
3458
  const cb = typeof lenOrCallback === "function" ? lenOrCallback : callback;
3459
+ this._validateCb(cb);
3424
3460
  const len = typeof lenOrCallback === "function" ? void 0 : lenOrCallback;
3425
3461
  this.promises.truncate(filePath, len).then(
3426
3462
  () => setTimeout(() => cb(null), 0),
@@ -3428,24 +3464,28 @@ var VFSFileSystem = class {
3428
3464
  );
3429
3465
  }
3430
3466
  realpath(filePath, callback) {
3467
+ this._validateCb(callback);
3431
3468
  this.promises.realpath(filePath).then(
3432
3469
  (result) => setTimeout(() => callback(null, result), 0),
3433
3470
  (err) => setTimeout(() => callback(err), 0)
3434
3471
  );
3435
3472
  }
3436
3473
  chmod(filePath, mode, callback) {
3474
+ this._validateCb(callback);
3437
3475
  this.promises.chmod(filePath, mode).then(
3438
3476
  () => setTimeout(() => callback(null), 0),
3439
3477
  (err) => setTimeout(() => callback(err), 0)
3440
3478
  );
3441
3479
  }
3442
3480
  chown(filePath, uid, gid, callback) {
3481
+ this._validateCb(callback);
3443
3482
  this.promises.chown(filePath, uid, gid).then(
3444
3483
  () => setTimeout(() => callback(null), 0),
3445
3484
  (err) => setTimeout(() => callback(err), 0)
3446
3485
  );
3447
3486
  }
3448
3487
  utimes(filePath, atime, mtime, callback) {
3488
+ this._validateCb(callback);
3449
3489
  this.promises.utimes(filePath, atime, mtime).then(
3450
3490
  () => setTimeout(() => callback(null), 0),
3451
3491
  (err) => setTimeout(() => callback(err), 0)
@@ -3453,6 +3493,7 @@ var VFSFileSystem = class {
3453
3493
  }
3454
3494
  symlink(target, linkPath, typeOrCallback, callback) {
3455
3495
  const cb = typeof typeOrCallback === "function" ? typeOrCallback : callback;
3496
+ this._validateCb(cb);
3456
3497
  const type = typeof typeOrCallback === "function" ? void 0 : typeOrCallback;
3457
3498
  this.promises.symlink(target, linkPath, type).then(
3458
3499
  () => setTimeout(() => cb(null), 0),
@@ -3461,6 +3502,7 @@ var VFSFileSystem = class {
3461
3502
  }
3462
3503
  readlink(filePath, optionsOrCallback, callback) {
3463
3504
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3505
+ this._validateCb(cb);
3464
3506
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3465
3507
  this.promises.readlink(filePath, opts).then(
3466
3508
  (result) => setTimeout(() => cb(null, result), 0),
@@ -3468,6 +3510,7 @@ var VFSFileSystem = class {
3468
3510
  );
3469
3511
  }
3470
3512
  link(existingPath, newPath, callback) {
3513
+ this._validateCb(callback);
3471
3514
  this.promises.link(existingPath, newPath).then(
3472
3515
  () => setTimeout(() => callback(null), 0),
3473
3516
  (err) => setTimeout(() => callback(err), 0)
@@ -3475,6 +3518,7 @@ var VFSFileSystem = class {
3475
3518
  }
3476
3519
  open(filePath, flags, modeOrCallback, callback) {
3477
3520
  const cb = typeof modeOrCallback === "function" ? modeOrCallback : callback;
3521
+ this._validateCb(cb);
3478
3522
  const mode = typeof modeOrCallback === "function" ? void 0 : modeOrCallback;
3479
3523
  this.promises.open(filePath, flags, mode).then(
3480
3524
  (handle) => setTimeout(() => cb(null, handle.fd), 0),
@@ -3482,6 +3526,7 @@ var VFSFileSystem = class {
3482
3526
  );
3483
3527
  }
3484
3528
  mkdtemp(prefix, callback) {
3529
+ this._validateCb(callback);
3485
3530
  this.promises.mkdtemp(prefix).then(
3486
3531
  (result) => setTimeout(() => callback(null, result), 0),
3487
3532
  (err) => setTimeout(() => callback(err), 0)
@@ -3500,6 +3545,7 @@ var VFSFileSystem = class {
3500
3545
  return this._cpAsync(src, dest, opts);
3501
3546
  }
3502
3547
  fdatasync(fd, callback) {
3548
+ this._validateCb(callback);
3503
3549
  try {
3504
3550
  this.fdatasyncSync(fd);
3505
3551
  setTimeout(() => callback(null), 0);
@@ -3508,6 +3554,7 @@ var VFSFileSystem = class {
3508
3554
  }
3509
3555
  }
3510
3556
  fsync(fd, callback) {
3557
+ this._validateCb(callback);
3511
3558
  try {
3512
3559
  this.fsyncSync(fd);
3513
3560
  setTimeout(() => callback(null), 0);
@@ -3517,6 +3564,7 @@ var VFSFileSystem = class {
3517
3564
  }
3518
3565
  fstat(fd, optionsOrCallback, callback) {
3519
3566
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3567
+ this._validateCb(cb);
3520
3568
  try {
3521
3569
  const result = this.fstatSync(fd);
3522
3570
  setTimeout(() => cb(null, result), 0);
@@ -3526,6 +3574,7 @@ var VFSFileSystem = class {
3526
3574
  }
3527
3575
  ftruncate(fd, lenOrCallback, callback) {
3528
3576
  const cb = typeof lenOrCallback === "function" ? lenOrCallback : callback;
3577
+ this._validateCb(cb);
3529
3578
  const len = typeof lenOrCallback === "function" ? 0 : lenOrCallback;
3530
3579
  try {
3531
3580
  this.ftruncateSync(fd, len);
@@ -3535,6 +3584,7 @@ var VFSFileSystem = class {
3535
3584
  }
3536
3585
  }
3537
3586
  read(fd, buffer, offset, length, position, callback) {
3587
+ this._validateCb(callback);
3538
3588
  try {
3539
3589
  const bytesRead = this.readSync(fd, buffer, offset, length, position);
3540
3590
  setTimeout(() => callback(null, bytesRead, buffer), 0);
@@ -3544,6 +3594,7 @@ var VFSFileSystem = class {
3544
3594
  }
3545
3595
  write(fd, bufferOrString, offsetOrPosition, lengthOrEncoding, position, callback) {
3546
3596
  const cb = [offsetOrPosition, lengthOrEncoding, position, callback].find((a) => typeof a === "function");
3597
+ this._validateCb(cb);
3547
3598
  try {
3548
3599
  let bytesWritten;
3549
3600
  if (typeof bufferOrString === "string") {
@@ -3577,6 +3628,7 @@ var VFSFileSystem = class {
3577
3628
  );
3578
3629
  }
3579
3630
  opendir(filePath, callback) {
3631
+ this._validateCb(callback);
3580
3632
  this.promises.opendir(filePath).then(
3581
3633
  (dir) => setTimeout(() => callback(null, dir), 0),
3582
3634
  (err) => setTimeout(() => callback(err), 0)
@@ -3584,6 +3636,7 @@ var VFSFileSystem = class {
3584
3636
  }
3585
3637
  glob(pattern, optionsOrCallback, callback) {
3586
3638
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3639
+ this._validateCb(cb);
3587
3640
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3588
3641
  this.promises.glob(pattern, opts).then(
3589
3642
  (result) => setTimeout(() => cb(null, result), 0),
@@ -3591,27 +3644,33 @@ var VFSFileSystem = class {
3591
3644
  );
3592
3645
  }
3593
3646
  futimes(fd, atime, mtime, callback) {
3647
+ this._validateCb(callback);
3594
3648
  setTimeout(() => callback(null), 0);
3595
3649
  }
3596
3650
  fchmod(fd, mode, callback) {
3651
+ this._validateCb(callback);
3597
3652
  setTimeout(() => callback(null), 0);
3598
3653
  }
3599
3654
  fchown(fd, uid, gid, callback) {
3655
+ this._validateCb(callback);
3600
3656
  setTimeout(() => callback(null), 0);
3601
3657
  }
3602
3658
  lchmod(filePath, mode, callback) {
3659
+ this._validateCb(callback);
3603
3660
  this.promises.lchmod(filePath, mode).then(
3604
3661
  () => setTimeout(() => callback(null), 0),
3605
3662
  (err) => setTimeout(() => callback(err), 0)
3606
3663
  );
3607
3664
  }
3608
3665
  lchown(filePath, uid, gid, callback) {
3666
+ this._validateCb(callback);
3609
3667
  this.promises.lchown(filePath, uid, gid).then(
3610
3668
  () => setTimeout(() => callback(null), 0),
3611
3669
  (err) => setTimeout(() => callback(err), 0)
3612
3670
  );
3613
3671
  }
3614
3672
  lutimes(filePath, atime, mtime, callback) {
3673
+ this._validateCb(callback);
3615
3674
  this.promises.lutimes(filePath, atime, mtime).then(
3616
3675
  () => setTimeout(() => callback(null), 0),
3617
3676
  (err) => setTimeout(() => callback(err), 0)