@componentor/fs 3.0.30 → 3.0.33

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.js CHANGED
@@ -59,6 +59,25 @@ var SimpleEventEmitter = class {
59
59
  listenerCount(event) {
60
60
  return this._listeners.get(event)?.length ?? 0;
61
61
  }
62
+ rawListeners(event) {
63
+ return [...this._listeners.get(event) ?? []];
64
+ }
65
+ prependListener(event, fn) {
66
+ const arr = this._listeners.get(event) ?? [];
67
+ arr.unshift(fn);
68
+ this._listeners.set(event, arr);
69
+ return this;
70
+ }
71
+ prependOnceListener(event, fn) {
72
+ const wrapper = (...args) => {
73
+ this.off(event, wrapper);
74
+ fn(...args);
75
+ };
76
+ return this.prependListener(event, wrapper);
77
+ }
78
+ eventNames() {
79
+ return [...this._listeners.keys()].filter((k) => (this._listeners.get(k)?.length ?? 0) > 0);
80
+ }
62
81
  };
63
82
  var NodeReadable = class extends SimpleEventEmitter {
64
83
  constructor(_readFn, destroyFn) {
@@ -852,13 +871,13 @@ function writeSyncFd(syncRequest, fd, bufferOrString, offsetOrPositionOrOptions,
852
871
  if (status !== 0) throw statusToError(status, "write", String(fd));
853
872
  return data ? new DataView(data.buffer, data.byteOffset, data.byteLength).getUint32(0, true) : 0;
854
873
  }
855
- function fstatSync(syncRequest, fd) {
874
+ function fstatSync(syncRequest, fd, options) {
856
875
  const fdBuf = new Uint8Array(4);
857
876
  new DataView(fdBuf.buffer).setUint32(0, fd, true);
858
877
  const buf = encodeRequest(OP.FSTAT, "", 0, fdBuf);
859
878
  const { status, data } = syncRequest(buf);
860
879
  if (status !== 0) throw statusToError(status, "fstat", String(fd));
861
- return decodeStats(data);
880
+ return options?.bigint ? decodeStatsBigInt(data) : decodeStats(data);
862
881
  }
863
882
  function ftruncateSync(syncRequest, fd, len = 0) {
864
883
  const fdBuf = new Uint8Array(12);
@@ -1775,7 +1794,7 @@ function toPathString(p) {
1775
1794
  }
1776
1795
  return decodeURIComponent(p.pathname);
1777
1796
  }
1778
- return String(p);
1797
+ throw new TypeError('The "path" argument must be of type string, Uint8Array, or URL. Received ' + typeof p);
1779
1798
  }
1780
1799
  var sep = "/";
1781
1800
  var delimiter = ":";
@@ -3036,8 +3055,8 @@ var VFSFileSystem = class {
3036
3055
  writeSync(fd, bufferOrString, offsetOrPositionOrOptions, lengthOrEncoding, position) {
3037
3056
  return writeSyncFd(this._sync, fd, bufferOrString, offsetOrPositionOrOptions, lengthOrEncoding, position);
3038
3057
  }
3039
- fstatSync(fd) {
3040
- return fstatSync(this._sync, fd);
3058
+ fstatSync(fd, options) {
3059
+ return fstatSync(this._sync, fd, options);
3041
3060
  }
3042
3061
  ftruncateSync(fd, len) {
3043
3062
  ftruncateSync(this._sync, fd, len);
@@ -3080,11 +3099,13 @@ var VFSFileSystem = class {
3080
3099
  pos = positionOrCallback;
3081
3100
  cb = callback;
3082
3101
  }
3102
+ this._validateCb(cb);
3083
3103
  try {
3084
3104
  const bytesRead = this.readvSync(fd, buffers, pos);
3085
- setTimeout(() => cb(null, bytesRead, buffers), 0);
3105
+ if (cb) setTimeout(() => cb(null, bytesRead, buffers), 0);
3086
3106
  } catch (err) {
3087
- setTimeout(() => cb(err), 0);
3107
+ if (cb) setTimeout(() => cb(err), 0);
3108
+ else throw err;
3088
3109
  }
3089
3110
  }
3090
3111
  writev(fd, buffers, positionOrCallback, callback) {
@@ -3097,11 +3118,13 @@ var VFSFileSystem = class {
3097
3118
  pos = positionOrCallback;
3098
3119
  cb = callback;
3099
3120
  }
3121
+ this._validateCb(cb);
3100
3122
  try {
3101
3123
  const bytesWritten = this.writevSync(fd, buffers, pos);
3102
- setTimeout(() => cb(null, bytesWritten, buffers), 0);
3124
+ if (cb) setTimeout(() => cb(null, bytesWritten, buffers), 0);
3103
3125
  } catch (err) {
3104
- setTimeout(() => cb(err), 0);
3126
+ if (cb) setTimeout(() => cb(err), 0);
3127
+ else throw err;
3105
3128
  }
3106
3129
  }
3107
3130
  // ---- statfs methods ----
@@ -3124,7 +3147,8 @@ var VFSFileSystem = class {
3124
3147
  statfs(path, callback) {
3125
3148
  const result = this.statfsSync(path);
3126
3149
  if (callback) {
3127
- callback(null, result);
3150
+ this._validateCb(callback);
3151
+ setTimeout(() => callback(null, result), 0);
3128
3152
  return;
3129
3153
  }
3130
3154
  return Promise.resolve(result);
@@ -3155,18 +3179,24 @@ var VFSFileSystem = class {
3155
3179
  let handle = null;
3156
3180
  let finished = false;
3157
3181
  const cleanup = async () => {
3158
- if (handle) {
3182
+ if (handle && ownsHandle) {
3159
3183
  try {
3160
3184
  await handle.close();
3161
3185
  } catch {
3162
3186
  }
3163
- handle = null;
3164
3187
  }
3188
+ handle = null;
3165
3189
  };
3190
+ const providedFd = opts?.fd;
3191
+ let ownsHandle = providedFd == null;
3166
3192
  const readFn = async () => {
3167
3193
  if (finished) return { done: true };
3168
3194
  if (!handle) {
3169
- handle = await this.promises.open(toPathString(filePath), opts?.flags ?? "r");
3195
+ if (providedFd != null) {
3196
+ handle = createFileHandle(providedFd, this._async);
3197
+ } else {
3198
+ handle = await this.promises.open(toPathString(filePath), opts?.flags ?? "r");
3199
+ }
3170
3200
  }
3171
3201
  const readLen = end !== void 0 ? Math.min(highWaterMark, end - position + 1) : highWaterMark;
3172
3202
  if (readLen <= 0) {
@@ -3197,9 +3227,15 @@ var VFSFileSystem = class {
3197
3227
  const opts = typeof options === "string" ? { } : options;
3198
3228
  let position = opts?.start ?? 0;
3199
3229
  let handle = null;
3230
+ const providedWFd = opts?.fd;
3231
+ const ownsWHandle = providedWFd == null;
3200
3232
  const writeFn = async (chunk) => {
3201
3233
  if (!handle) {
3202
- handle = await this.promises.open(toPathString(filePath), opts?.flags ?? "w");
3234
+ if (providedWFd != null) {
3235
+ handle = createFileHandle(providedWFd, this._async);
3236
+ } else {
3237
+ handle = await this.promises.open(toPathString(filePath), opts?.flags ?? "w");
3238
+ }
3203
3239
  }
3204
3240
  const { bytesWritten } = await handle.write(chunk, 0, chunk.byteLength, position);
3205
3241
  position += bytesWritten;
@@ -3209,7 +3245,9 @@ var VFSFileSystem = class {
3209
3245
  if (opts?.flush) {
3210
3246
  await handle.sync();
3211
3247
  }
3212
- await handle.close();
3248
+ if (ownsWHandle) {
3249
+ await handle.close();
3250
+ }
3213
3251
  handle = null;
3214
3252
  }
3215
3253
  };
@@ -3319,178 +3357,165 @@ var VFSFileSystem = class {
3319
3357
  }
3320
3358
  return this.readyPromise;
3321
3359
  }
3360
+ // ========== Callback API ==========
3361
+ // Node.js-style callback overloads for all async operations.
3362
+ // These delegate to this.promises.* and adapt the result to (err, result) callbacks.
3363
+ _validateCb(cb) {
3364
+ if (cb !== void 0 && cb !== null && typeof cb !== "function") {
3365
+ throw new TypeError('The "cb" argument must be of type function. Received ' + typeof cb);
3366
+ }
3367
+ }
3368
+ /** Adapt a promise to optional Node.js callback style.
3369
+ * If cb is a function: calls cb(err, result) via setTimeout. Returns void.
3370
+ * If cb is missing: returns the promise (allows .then() or await). */
3371
+ _cb(promise, cb, mapResult) {
3372
+ if (typeof cb === "function") {
3373
+ promise.then(
3374
+ (val) => setTimeout(() => cb(null, ...mapResult ? mapResult(val) : [val]), 0),
3375
+ (err) => setTimeout(() => cb(err), 0)
3376
+ );
3377
+ return;
3378
+ }
3379
+ return promise;
3380
+ }
3381
+ /** Like _cb but for void-returning promises (no result value). */
3382
+ _cbVoid(promise, cb) {
3383
+ if (typeof cb === "function") {
3384
+ promise.then(
3385
+ () => setTimeout(() => cb(null), 0),
3386
+ (err) => setTimeout(() => cb(err), 0)
3387
+ );
3388
+ return;
3389
+ }
3390
+ return promise;
3391
+ }
3322
3392
  readFile(filePath, optionsOrCallback, callback) {
3323
3393
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3394
+ this._validateCb(cb);
3324
3395
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3325
- this.promises.readFile(filePath, opts).then(
3326
- (result) => setTimeout(() => cb(null, result), 0),
3327
- (err) => setTimeout(() => cb(err), 0)
3328
- );
3396
+ return this._cb(this.promises.readFile(filePath, opts), cb);
3329
3397
  }
3330
3398
  writeFile(filePath, data, optionsOrCallback, callback) {
3331
3399
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3400
+ this._validateCb(cb);
3332
3401
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3333
- this.promises.writeFile(filePath, data, opts).then(
3334
- () => setTimeout(() => cb(null), 0),
3335
- (err) => setTimeout(() => cb(err), 0)
3336
- );
3402
+ return this._cbVoid(this.promises.writeFile(filePath, data, opts), cb);
3337
3403
  }
3338
3404
  appendFile(filePath, data, optionsOrCallback, callback) {
3339
3405
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3406
+ this._validateCb(cb);
3340
3407
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3341
- this.promises.appendFile(filePath, data, opts).then(
3342
- () => setTimeout(() => cb(null), 0),
3343
- (err) => setTimeout(() => cb(err), 0)
3344
- );
3408
+ return this._cbVoid(this.promises.appendFile(filePath, data, opts), cb);
3345
3409
  }
3346
3410
  mkdir(filePath, optionsOrCallback, callback) {
3347
3411
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3412
+ this._validateCb(cb);
3348
3413
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3349
- this.promises.mkdir(filePath, opts).then(
3350
- (result) => setTimeout(() => cb(null, result), 0),
3351
- (err) => setTimeout(() => cb(err), 0)
3352
- );
3414
+ return this._cb(this.promises.mkdir(filePath, opts), cb);
3353
3415
  }
3354
3416
  rmdir(filePath, optionsOrCallback, callback) {
3355
3417
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3418
+ this._validateCb(cb);
3356
3419
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3357
- this.promises.rmdir(filePath, opts).then(
3358
- () => setTimeout(() => cb(null), 0),
3359
- (err) => setTimeout(() => cb(err), 0)
3360
- );
3420
+ return this._cbVoid(this.promises.rmdir(filePath, opts), cb);
3361
3421
  }
3362
3422
  rm(filePath, optionsOrCallback, callback) {
3363
3423
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3424
+ this._validateCb(cb);
3364
3425
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3365
- this.promises.rm(filePath, opts).then(
3366
- () => setTimeout(() => cb(null), 0),
3367
- (err) => setTimeout(() => cb(err), 0)
3368
- );
3426
+ return this._cbVoid(this.promises.rm(filePath, opts), cb);
3369
3427
  }
3370
3428
  unlink(filePath, callback) {
3371
- this.promises.unlink(filePath).then(
3372
- () => setTimeout(() => callback(null), 0),
3373
- (err) => setTimeout(() => callback(err), 0)
3374
- );
3429
+ this._validateCb(callback);
3430
+ return this._cbVoid(this.promises.unlink(filePath), callback);
3375
3431
  }
3376
3432
  readdir(filePath, optionsOrCallback, callback) {
3377
3433
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3434
+ this._validateCb(cb);
3378
3435
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3379
- this.promises.readdir(filePath, opts).then(
3380
- (result) => setTimeout(() => cb(null, result), 0),
3381
- (err) => setTimeout(() => cb(err), 0)
3382
- );
3436
+ return this._cb(this.promises.readdir(filePath, opts), cb);
3383
3437
  }
3384
3438
  stat(filePath, optionsOrCallback, callback) {
3385
3439
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3440
+ this._validateCb(cb);
3386
3441
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3387
- this.promises.stat(filePath, opts).then(
3388
- (result) => setTimeout(() => cb(null, result), 0),
3389
- (err) => setTimeout(() => cb(err), 0)
3390
- );
3442
+ return this._cb(this.promises.stat(filePath, opts), cb);
3391
3443
  }
3392
3444
  lstat(filePath, optionsOrCallback, callback) {
3393
3445
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3446
+ this._validateCb(cb);
3394
3447
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3395
- this.promises.lstat(filePath, opts).then(
3396
- (result) => setTimeout(() => cb(null, result), 0),
3397
- (err) => setTimeout(() => cb(err), 0)
3398
- );
3448
+ return this._cb(this.promises.lstat(filePath, opts), cb);
3399
3449
  }
3400
3450
  access(filePath, modeOrCallback, callback) {
3401
3451
  const cb = typeof modeOrCallback === "function" ? modeOrCallback : callback;
3452
+ this._validateCb(cb);
3402
3453
  const mode = typeof modeOrCallback === "function" ? void 0 : modeOrCallback;
3403
- this.promises.access(filePath, mode).then(
3404
- () => setTimeout(() => cb(null), 0),
3405
- (err) => setTimeout(() => cb(err), 0)
3406
- );
3454
+ return this._cbVoid(this.promises.access(filePath, mode), cb);
3407
3455
  }
3408
3456
  rename(oldPath, newPath, callback) {
3409
- this.promises.rename(oldPath, newPath).then(
3410
- () => setTimeout(() => callback(null), 0),
3411
- (err) => setTimeout(() => callback(err), 0)
3412
- );
3457
+ this._validateCb(callback);
3458
+ return this._cbVoid(this.promises.rename(oldPath, newPath), callback);
3413
3459
  }
3414
3460
  copyFile(src, dest, modeOrCallback, callback) {
3415
3461
  const cb = typeof modeOrCallback === "function" ? modeOrCallback : callback;
3462
+ this._validateCb(cb);
3416
3463
  const mode = typeof modeOrCallback === "function" ? void 0 : modeOrCallback;
3417
- this.promises.copyFile(src, dest, mode).then(
3418
- () => setTimeout(() => cb(null), 0),
3419
- (err) => setTimeout(() => cb(err), 0)
3420
- );
3464
+ return this._cbVoid(this.promises.copyFile(src, dest, mode), cb);
3421
3465
  }
3422
3466
  truncate(filePath, lenOrCallback, callback) {
3423
3467
  const cb = typeof lenOrCallback === "function" ? lenOrCallback : callback;
3468
+ this._validateCb(cb);
3424
3469
  const len = typeof lenOrCallback === "function" ? void 0 : lenOrCallback;
3425
- this.promises.truncate(filePath, len).then(
3426
- () => setTimeout(() => cb(null), 0),
3427
- (err) => setTimeout(() => cb(err), 0)
3428
- );
3470
+ return this._cbVoid(this.promises.truncate(filePath, len), cb);
3429
3471
  }
3430
3472
  realpath(filePath, callback) {
3431
- this.promises.realpath(filePath).then(
3432
- (result) => setTimeout(() => callback(null, result), 0),
3433
- (err) => setTimeout(() => callback(err), 0)
3434
- );
3473
+ this._validateCb(callback);
3474
+ return this._cb(this.promises.realpath(filePath), callback);
3435
3475
  }
3436
3476
  chmod(filePath, mode, callback) {
3437
- this.promises.chmod(filePath, mode).then(
3438
- () => setTimeout(() => callback(null), 0),
3439
- (err) => setTimeout(() => callback(err), 0)
3440
- );
3477
+ this._validateCb(callback);
3478
+ return this._cbVoid(this.promises.chmod(filePath, mode), callback);
3441
3479
  }
3442
3480
  chown(filePath, uid, gid, callback) {
3443
- this.promises.chown(filePath, uid, gid).then(
3444
- () => setTimeout(() => callback(null), 0),
3445
- (err) => setTimeout(() => callback(err), 0)
3446
- );
3481
+ this._validateCb(callback);
3482
+ return this._cbVoid(this.promises.chown(filePath, uid, gid), callback);
3447
3483
  }
3448
3484
  utimes(filePath, atime, mtime, callback) {
3449
- this.promises.utimes(filePath, atime, mtime).then(
3450
- () => setTimeout(() => callback(null), 0),
3451
- (err) => setTimeout(() => callback(err), 0)
3452
- );
3485
+ this._validateCb(callback);
3486
+ return this._cbVoid(this.promises.utimes(filePath, atime, mtime), callback);
3453
3487
  }
3454
3488
  symlink(target, linkPath, typeOrCallback, callback) {
3455
3489
  const cb = typeof typeOrCallback === "function" ? typeOrCallback : callback;
3490
+ this._validateCb(cb);
3456
3491
  const type = typeof typeOrCallback === "function" ? void 0 : typeOrCallback;
3457
- this.promises.symlink(target, linkPath, type).then(
3458
- () => setTimeout(() => cb(null), 0),
3459
- (err) => setTimeout(() => cb(err), 0)
3460
- );
3492
+ return this._cbVoid(this.promises.symlink(target, linkPath, type), cb);
3461
3493
  }
3462
3494
  readlink(filePath, optionsOrCallback, callback) {
3463
3495
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3496
+ this._validateCb(cb);
3464
3497
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3465
- this.promises.readlink(filePath, opts).then(
3466
- (result) => setTimeout(() => cb(null, result), 0),
3467
- (err) => setTimeout(() => cb(err), 0)
3468
- );
3498
+ return this._cb(this.promises.readlink(filePath, opts), cb);
3469
3499
  }
3470
3500
  link(existingPath, newPath, callback) {
3471
- this.promises.link(existingPath, newPath).then(
3472
- () => setTimeout(() => callback(null), 0),
3473
- (err) => setTimeout(() => callback(err), 0)
3474
- );
3501
+ this._validateCb(callback);
3502
+ return this._cbVoid(this.promises.link(existingPath, newPath), callback);
3475
3503
  }
3476
3504
  open(filePath, flags, modeOrCallback, callback) {
3477
3505
  const cb = typeof modeOrCallback === "function" ? modeOrCallback : callback;
3506
+ this._validateCb(cb);
3478
3507
  const mode = typeof modeOrCallback === "function" ? void 0 : modeOrCallback;
3479
- this.promises.open(filePath, flags, mode).then(
3480
- (handle) => setTimeout(() => cb(null, handle.fd), 0),
3481
- (err) => setTimeout(() => cb(err), 0)
3482
- );
3508
+ return this._cb(this.promises.open(filePath, flags, mode), cb, (handle) => [handle.fd]);
3483
3509
  }
3484
3510
  mkdtemp(prefix, callback) {
3485
- this.promises.mkdtemp(prefix).then(
3486
- (result) => setTimeout(() => callback(null, result), 0),
3487
- (err) => setTimeout(() => callback(err), 0)
3488
- );
3511
+ this._validateCb(callback);
3512
+ return this._cb(this.promises.mkdtemp(prefix), callback);
3489
3513
  }
3490
3514
  cp(src, dest, optionsOrCallback, callback) {
3491
3515
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3492
3516
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3493
3517
  if (cb) {
3518
+ this._validateCb(cb);
3494
3519
  this._cpAsync(src, dest, opts).then(
3495
3520
  () => setTimeout(() => cb(null), 0),
3496
3521
  (err) => setTimeout(() => cb(err), 0)
@@ -3500,50 +3525,80 @@ var VFSFileSystem = class {
3500
3525
  return this._cpAsync(src, dest, opts);
3501
3526
  }
3502
3527
  fdatasync(fd, callback) {
3528
+ this._validateCb(callback);
3503
3529
  try {
3504
3530
  this.fdatasyncSync(fd);
3505
- setTimeout(() => callback(null), 0);
3531
+ if (callback) setTimeout(() => callback(null), 0);
3506
3532
  } catch (err) {
3507
- setTimeout(() => callback(err), 0);
3533
+ if (callback) setTimeout(() => callback(err), 0);
3534
+ else throw err;
3508
3535
  }
3509
3536
  }
3510
3537
  fsync(fd, callback) {
3538
+ this._validateCb(callback);
3511
3539
  try {
3512
3540
  this.fsyncSync(fd);
3513
- setTimeout(() => callback(null), 0);
3541
+ if (callback) setTimeout(() => callback(null), 0);
3514
3542
  } catch (err) {
3515
- setTimeout(() => callback(err), 0);
3543
+ if (callback) setTimeout(() => callback(err), 0);
3544
+ else throw err;
3516
3545
  }
3517
3546
  }
3518
3547
  fstat(fd, optionsOrCallback, callback) {
3519
3548
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3549
+ this._validateCb(cb);
3550
+ const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3520
3551
  try {
3521
- const result = this.fstatSync(fd);
3522
- setTimeout(() => cb(null, result), 0);
3552
+ const result = this.fstatSync(fd, opts);
3553
+ if (cb) setTimeout(() => cb(null, result), 0);
3523
3554
  } catch (err) {
3524
- setTimeout(() => cb(err), 0);
3555
+ if (cb) setTimeout(() => cb(err), 0);
3556
+ else throw err;
3525
3557
  }
3526
3558
  }
3527
3559
  ftruncate(fd, lenOrCallback, callback) {
3528
3560
  const cb = typeof lenOrCallback === "function" ? lenOrCallback : callback;
3561
+ this._validateCb(cb);
3529
3562
  const len = typeof lenOrCallback === "function" ? 0 : lenOrCallback;
3530
3563
  try {
3531
3564
  this.ftruncateSync(fd, len);
3532
- setTimeout(() => cb(null), 0);
3565
+ if (cb) setTimeout(() => cb(null), 0);
3533
3566
  } catch (err) {
3534
- setTimeout(() => cb(err), 0);
3567
+ if (cb) setTimeout(() => cb(err), 0);
3568
+ else throw err;
3535
3569
  }
3536
3570
  }
3537
3571
  read(fd, buffer, offset, length, position, callback) {
3572
+ let cb;
3573
+ let buf;
3574
+ let off;
3575
+ let len;
3576
+ let pos;
3577
+ if (typeof buffer === "object" && !(buffer instanceof Uint8Array) && buffer !== null && "buffer" in buffer) {
3578
+ cb = offset;
3579
+ buf = buffer.buffer;
3580
+ off = buffer.offset ?? 0;
3581
+ len = buffer.length ?? buf.byteLength;
3582
+ pos = buffer.position ?? null;
3583
+ } else {
3584
+ cb = callback;
3585
+ buf = buffer;
3586
+ off = offset;
3587
+ len = length;
3588
+ pos = position;
3589
+ }
3590
+ this._validateCb(cb);
3538
3591
  try {
3539
- const bytesRead = this.readSync(fd, buffer, offset, length, position);
3540
- setTimeout(() => callback(null, bytesRead, buffer), 0);
3592
+ const bytesRead = this.readSync(fd, buf, off, len, pos);
3593
+ if (cb) setTimeout(() => cb(null, bytesRead, buf), 0);
3541
3594
  } catch (err) {
3542
- setTimeout(() => callback(err), 0);
3595
+ if (cb) setTimeout(() => cb(err), 0);
3596
+ else throw err;
3543
3597
  }
3544
3598
  }
3545
3599
  write(fd, bufferOrString, offsetOrPosition, lengthOrEncoding, position, callback) {
3546
3600
  const cb = [offsetOrPosition, lengthOrEncoding, position, callback].find((a) => typeof a === "function");
3601
+ this._validateCb(cb);
3547
3602
  try {
3548
3603
  let bytesWritten;
3549
3604
  if (typeof bufferOrString === "string") {
@@ -3556,9 +3611,10 @@ var VFSFileSystem = class {
3556
3611
  const pos = typeof position === "function" ? void 0 : position;
3557
3612
  bytesWritten = this.writeSync(fd, bufferOrString, off, len, pos);
3558
3613
  }
3559
- setTimeout(() => cb(null, bytesWritten, bufferOrString), 0);
3614
+ if (cb) setTimeout(() => cb(null, bytesWritten, bufferOrString), 0);
3560
3615
  } catch (err) {
3561
- setTimeout(() => cb(err), 0);
3616
+ if (cb) setTimeout(() => cb(err), 0);
3617
+ else throw err;
3562
3618
  }
3563
3619
  }
3564
3620
  close(fd, callback) {
@@ -3571,51 +3627,49 @@ var VFSFileSystem = class {
3571
3627
  }
3572
3628
  }
3573
3629
  exists(filePath, callback) {
3574
- this.promises.exists(filePath).then(
3575
- (result) => setTimeout(() => callback(result), 0),
3576
- () => setTimeout(() => callback(false), 0)
3577
- );
3630
+ const p = this.promises.exists(filePath);
3631
+ if (typeof callback === "function") {
3632
+ p.then(
3633
+ (result) => setTimeout(() => callback(result), 0),
3634
+ () => setTimeout(() => callback(false), 0)
3635
+ );
3636
+ return;
3637
+ }
3638
+ return p;
3578
3639
  }
3579
3640
  opendir(filePath, callback) {
3580
- this.promises.opendir(filePath).then(
3581
- (dir) => setTimeout(() => callback(null, dir), 0),
3582
- (err) => setTimeout(() => callback(err), 0)
3583
- );
3641
+ this._validateCb(callback);
3642
+ return this._cb(this.promises.opendir(filePath), callback);
3584
3643
  }
3585
3644
  glob(pattern, optionsOrCallback, callback) {
3586
3645
  const cb = typeof optionsOrCallback === "function" ? optionsOrCallback : callback;
3646
+ this._validateCb(cb);
3587
3647
  const opts = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
3588
- this.promises.glob(pattern, opts).then(
3589
- (result) => setTimeout(() => cb(null, result), 0),
3590
- (err) => setTimeout(() => cb(err), 0)
3591
- );
3648
+ return this._cb(this.promises.glob(pattern, opts), cb);
3592
3649
  }
3593
3650
  futimes(fd, atime, mtime, callback) {
3594
- setTimeout(() => callback(null), 0);
3651
+ this._validateCb(callback);
3652
+ if (callback) setTimeout(() => callback(null), 0);
3595
3653
  }
3596
3654
  fchmod(fd, mode, callback) {
3597
- setTimeout(() => callback(null), 0);
3655
+ this._validateCb(callback);
3656
+ if (callback) setTimeout(() => callback(null), 0);
3598
3657
  }
3599
3658
  fchown(fd, uid, gid, callback) {
3600
- setTimeout(() => callback(null), 0);
3659
+ this._validateCb(callback);
3660
+ if (callback) setTimeout(() => callback(null), 0);
3601
3661
  }
3602
3662
  lchmod(filePath, mode, callback) {
3603
- this.promises.lchmod(filePath, mode).then(
3604
- () => setTimeout(() => callback(null), 0),
3605
- (err) => setTimeout(() => callback(err), 0)
3606
- );
3663
+ this._validateCb(callback);
3664
+ return this._cbVoid(this.promises.lchmod(filePath, mode), callback);
3607
3665
  }
3608
3666
  lchown(filePath, uid, gid, callback) {
3609
- this.promises.lchown(filePath, uid, gid).then(
3610
- () => setTimeout(() => callback(null), 0),
3611
- (err) => setTimeout(() => callback(err), 0)
3612
- );
3667
+ this._validateCb(callback);
3668
+ return this._cbVoid(this.promises.lchown(filePath, uid, gid), callback);
3613
3669
  }
3614
3670
  lutimes(filePath, atime, mtime, callback) {
3615
- this.promises.lutimes(filePath, atime, mtime).then(
3616
- () => setTimeout(() => callback(null), 0),
3617
- (err) => setTimeout(() => callback(err), 0)
3618
- );
3671
+ this._validateCb(callback);
3672
+ return this._cbVoid(this.promises.lutimes(filePath, atime, mtime), callback);
3619
3673
  }
3620
3674
  };
3621
3675
  var VFSPromises = class {
@@ -3807,6 +3861,15 @@ var VFSPromises = class {
3807
3861
  async *watch(filePath, options) {
3808
3862
  yield* watchAsync(this._ns, this._async, filePath, options);
3809
3863
  }
3864
+ async fstat(fd, options) {
3865
+ const { status, data } = await this._async(OP.FSTAT, "", 0, null, void 0, { fd });
3866
+ if (status !== 0) throw statusToError(status, "fstat", String(fd));
3867
+ return options?.bigint ? decodeStatsBigInt(data) : decodeStats(data);
3868
+ }
3869
+ async ftruncate(fd, len = 0) {
3870
+ const { status } = await this._async(OP.FTRUNCATE, "", 0, null, void 0, { fd, length: len });
3871
+ if (status !== 0) throw statusToError(status, "ftruncate", String(fd));
3872
+ }
3810
3873
  async fsync(_fd) {
3811
3874
  await this._async(OP.FSYNC, "");
3812
3875
  }