@ai-ecoverse/slicc-jsh 0.3.5 → 0.3.7

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/node.mjs CHANGED
@@ -1539,7 +1539,7 @@ ${e3}`;
1539
1539
  // package.json
1540
1540
  var package_default = {
1541
1541
  name: "@ai-ecoverse/slicc-jsh",
1542
- version: "0.3.5",
1542
+ version: "0.3.7",
1543
1543
  description: "Node.js-compatible runtime for slicc-kernel: the node and jsh commands (abi: js)",
1544
1544
  type: "module",
1545
1545
  license: "Apache-2.0",
@@ -3144,10 +3144,10 @@ function createBuffer() {
3144
3144
  e.code = "ERR_INVALID_ARG_TYPE";
3145
3145
  throw e;
3146
3146
  }
3147
- if (value instanceof ArrayBuffer) {
3147
+ if (value instanceof ArrayBuffer || typeof SharedArrayBuffer === "function" && value instanceof SharedArrayBuffer) {
3148
3148
  const offset2 = encodingOrOffset ?? 0;
3149
3149
  const len = length ?? value.byteLength - offset2;
3150
- return Buffer2.fromBytes(new Uint8Array(value, offset2, len));
3150
+ return new Buffer2(value, offset2, len);
3151
3151
  }
3152
3152
  if (value instanceof Uint8Array || Array.isArray(value) || ArrayBuffer.isView(value)) {
3153
3153
  return Buffer2.fromBytes(asUint8(value));
@@ -3291,12 +3291,13 @@ function createBuffer() {
3291
3291
  if (!isBufView(target)) throwBufferArgType("target", target);
3292
3292
  return Buffer2.compare(this, target);
3293
3293
  }
3294
+ // Node: both are views on the same memory (slice is subarray's old name).
3294
3295
  slice(start, end) {
3295
- return Buffer2.fromBytes(Uint8Array.prototype.slice.call(this, start, end));
3296
+ return this.subarray(start, end);
3296
3297
  }
3297
3298
  subarray(start, end) {
3298
3299
  const view = Uint8Array.prototype.subarray.call(this, start, end);
3299
- return Buffer2.fromBytes(view);
3300
+ return new Buffer2(view.buffer, view.byteOffset, view.length);
3300
3301
  }
3301
3302
  fill(value, offset2 = 0, end = this.length, encoding = "utf8") {
3302
3303
  if (typeof offset2 === "string") {
@@ -11284,8 +11285,10 @@ var ERRNO = {
11284
11285
  EACCES: -13,
11285
11286
  EINVAL: -22,
11286
11287
  EIO: -5,
11287
- ENOTEMPTY: -66,
11288
- EBADF: -9
11288
+ ENOTEMPTY: -39,
11289
+ // Linux (the shim's process.platform)
11290
+ EBADF: -9,
11291
+ ENOTSUP: -45
11289
11292
  };
11290
11293
  var DESCRIPTIONS = {
11291
11294
  ENOENT: "no such file or directory",
@@ -11297,7 +11300,8 @@ var DESCRIPTIONS = {
11297
11300
  EINVAL: "invalid argument",
11298
11301
  EIO: "i/o error",
11299
11302
  ENOTEMPTY: "directory not empty",
11300
- EBADF: "bad file descriptor"
11303
+ EBADF: "bad file descriptor",
11304
+ ENOTSUP: "operation not supported"
11301
11305
  };
11302
11306
  function nodeFsError(code, syscall, pathArg, err, dest) {
11303
11307
  const c = typeof code === "string" ? code : err?.code || "EIO";
@@ -11318,37 +11322,90 @@ function nodeFsError(code, syscall, pathArg, err, dest) {
11318
11322
  if (dest !== void 0) e.dest = dest;
11319
11323
  return e;
11320
11324
  }
11325
+ var FS_READ_STREAM = /* @__PURE__ */ Symbol("fs.ReadStream");
11326
+ var FS_WRITE_STREAM = /* @__PURE__ */ Symbol("fs.WriteStream");
11327
+ function cpInvalid(message) {
11328
+ const e = new Error(message);
11329
+ e.code = "ERR_FS_CP_EINVAL";
11330
+ return e;
11331
+ }
11332
+ function rmIsDirectoryError(pathArg) {
11333
+ const e = new Error(`Path is a directory: rm returned EISDIR (is a directory) ${pathArg}`);
11334
+ e.name = "SystemError";
11335
+ e.code = "ERR_FS_EISDIR";
11336
+ e.errno = 21;
11337
+ e.syscall = "rm";
11338
+ e.path = String(pathArg);
11339
+ e.info = { code: "EISDIR", message: "is a directory", path: String(pathArg), syscall: "rm", errno: 21 };
11340
+ return e;
11341
+ }
11321
11342
  function toBytes(data2) {
11322
11343
  if (typeof data2 === "string") return encoder3.encode(data2);
11323
11344
  if (data2 instanceof Uint8Array) return data2;
11324
11345
  if (ArrayBuffer.isView(data2)) return new Uint8Array(data2.buffer, data2.byteOffset, data2.byteLength);
11325
11346
  return encoder3.encode(String(data2));
11326
11347
  }
11348
+ var S_IFMT = 61440;
11349
+ var S_IFREG = 32768;
11350
+ var S_IFDIR = 16384;
11351
+ var S_IFLNK = 40960;
11352
+ var Stats = class {
11353
+ isFile() {
11354
+ return (this.mode & S_IFMT) === S_IFREG;
11355
+ }
11356
+ isDirectory() {
11357
+ return (this.mode & S_IFMT) === S_IFDIR;
11358
+ }
11359
+ isSymbolicLink() {
11360
+ return (this.mode & S_IFMT) === S_IFLNK;
11361
+ }
11362
+ isBlockDevice() {
11363
+ return false;
11364
+ }
11365
+ isCharacterDevice() {
11366
+ return false;
11367
+ }
11368
+ isFIFO() {
11369
+ return false;
11370
+ }
11371
+ isSocket() {
11372
+ return false;
11373
+ }
11374
+ };
11327
11375
  function statView(st) {
11328
11376
  const mtimeMs = st.mtimeMs ?? (st.mtime ? Number(st.mtime) : 0);
11329
11377
  const atimeMs = st.atimeMs ?? mtimeMs;
11330
11378
  const ctimeMs = st.ctimeMs ?? mtimeMs;
11331
- return {
11332
- isFile: () => !!st.isFile,
11333
- isDirectory: () => !!st.isDirectory,
11334
- isSymbolicLink: () => !!st.isSymbolicLink,
11335
- size: st.size ?? 0,
11336
- mode: st.mode,
11337
- mtimeMs,
11379
+ const type = st.isDirectory ? S_IFDIR : st.isSymbolicLink ? S_IFLNK : S_IFREG;
11380
+ const perm = typeof st.mode === "number" ? st.mode & ~S_IFMT : st.isDirectory ? 493 : 420;
11381
+ const size = st.size ?? 0;
11382
+ return Object.assign(new Stats(), {
11383
+ dev: st.dev ?? 0,
11384
+ mode: type | perm,
11385
+ nlink: st.nlink ?? 1,
11386
+ uid: st.uid ?? 0,
11387
+ gid: st.gid ?? 0,
11388
+ rdev: 0,
11389
+ blksize: 4096,
11390
+ ino: st.ino ?? 0,
11391
+ size,
11392
+ blocks: Math.ceil(size / 512),
11338
11393
  atimeMs,
11394
+ mtimeMs,
11339
11395
  ctimeMs,
11340
- mtime: new Date(mtimeMs),
11396
+ birthtimeMs: ctimeMs,
11341
11397
  atime: new Date(atimeMs),
11398
+ mtime: new Date(mtimeMs),
11342
11399
  ctime: new Date(ctimeMs),
11343
- birthtime: new Date(ctimeMs),
11344
- ino: st.ino,
11345
- dev: st.dev
11346
- };
11400
+ birthtime: new Date(ctimeMs)
11401
+ });
11347
11402
  }
11348
11403
  var Dirent = class {
11349
- constructor(name, type) {
11404
+ constructor(name, type, parentPath) {
11350
11405
  this.name = name;
11351
- this._type = type;
11406
+ this.parentPath = parentPath;
11407
+ this.path = parentPath;
11408
+ Object.defineProperty(this, "_type", { value: type, enumerable: false });
11352
11409
  }
11353
11410
  isFile() {
11354
11411
  return this._type === "file";
@@ -11380,6 +11437,20 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11380
11437
  const syncFs = ctx.sync.fs;
11381
11438
  const asyncFs = ctx.fs;
11382
11439
  const wrapBuffer = (bytes) => Buffer2 ? Buffer2.from(bytes) : bytes;
11440
+ const optEncoding = (options) => typeof options === "string" ? options : options?.encoding ?? null;
11441
+ const optFlag = (options, fallback) => options && typeof options === "object" && options.flag || fallback;
11442
+ const decodeAs = (bytes, encoding) => {
11443
+ if (!encoding || encoding === "buffer") return wrapBuffer(bytes);
11444
+ if (encoding === "utf8" || encoding === "utf-8") return decoder3.decode(bytes);
11445
+ return Buffer2 ? Buffer2.from(bytes).toString(encoding) : decoder3.decode(bytes);
11446
+ };
11447
+ const encodeAs = (data2, encoding) => {
11448
+ if (typeof data2 === "string" && encoding && encoding !== "utf8" && encoding !== "utf-8" && Buffer2) {
11449
+ const b = Buffer2.from(data2, encoding);
11450
+ return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
11451
+ }
11452
+ return toBytes(data2);
11453
+ };
11383
11454
  const track = (p) => handles ? trackPromise(handles, p) : p;
11384
11455
  const fdTable = /* @__PURE__ */ new Map();
11385
11456
  let nextFd = 3;
@@ -11505,12 +11576,10 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11505
11576
  function readStdinSync(encoding) {
11506
11577
  try {
11507
11578
  if (typeof process2?._readStdinSync === "function") {
11508
- const out = process2._readStdinSync();
11509
- return encoding ? decoder3.decode(out) : wrapBuffer(out);
11579
+ return decodeAs(process2._readStdinSync(), encoding);
11510
11580
  }
11511
11581
  if (typeof process2?.stdin?._readAllSync === "function") {
11512
- const out = process2.stdin._readAllSync();
11513
- return encoding ? decoder3.decode(out) : wrapBuffer(out);
11582
+ return decodeAs(process2.stdin._readAllSync(), encoding);
11514
11583
  }
11515
11584
  if (typeof ctx.sync?.read === "function") {
11516
11585
  const chunks = [];
@@ -11527,11 +11596,11 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11527
11596
  out.set(c, at2);
11528
11597
  at2 += c.length;
11529
11598
  }
11530
- return encoding ? decoder3.decode(out) : wrapBuffer(out);
11599
+ return decodeAs(out, encoding);
11531
11600
  }
11532
11601
  } catch {
11533
11602
  }
11534
- return encoding ? "" : wrapBuffer(new Uint8Array(0));
11603
+ return decodeAs(new Uint8Array(0), encoding);
11535
11604
  }
11536
11605
  function mkdirSync(p, options = {}) {
11537
11606
  const pathArg = p;
@@ -11548,42 +11617,105 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11548
11617
  }
11549
11618
  const parts = String(target).split("/").filter(Boolean);
11550
11619
  let cur = "";
11620
+ let first;
11551
11621
  for (const part of parts) {
11552
11622
  cur += `/${part}`;
11553
- if (syncFs.exists(cur)) continue;
11623
+ if (syncFs.exists(cur)) {
11624
+ let st;
11625
+ try {
11626
+ st = syncFs.stat(cur);
11627
+ } catch {
11628
+ st = null;
11629
+ }
11630
+ if (st && !st.isDirectory) throw nodeFsError("ENOTDIR", "mkdir", pathArg);
11631
+ continue;
11632
+ }
11554
11633
  try {
11555
11634
  syncFs.mkdir(cur);
11635
+ first ??= path.isAbsolute(String(p)) ? cur : path.relative(process2.cwd(), cur);
11556
11636
  } catch (err) {
11557
11637
  if (err?.code === "EEXIST") continue;
11558
11638
  throw nodeFsError(err?.code || "EIO", "mkdir", pathArg, err);
11559
11639
  }
11560
11640
  }
11641
+ return first;
11642
+ }
11643
+ const typeAt = (full) => {
11644
+ try {
11645
+ const st = syncFs.lstat(full);
11646
+ return st.isDirectory ? "dir" : st.isSymbolicLink ? "link" : st.isFile ? "file" : "unknown";
11647
+ } catch {
11648
+ return "unknown";
11649
+ }
11650
+ };
11651
+ function mustExist(p, syscall) {
11652
+ let ok = false;
11653
+ try {
11654
+ ok = syncFs.exists(abs(p));
11655
+ } catch {
11656
+ ok = false;
11657
+ }
11658
+ if (!ok) throw nodeFsError("ENOENT", syscall, p);
11561
11659
  }
11562
11660
  function readdirSync(p, options) {
11563
11661
  const pathArg = p;
11564
11662
  const target = abs(p);
11663
+ const encoding = optEncoding(options);
11664
+ const asName = (name) => encoding === "buffer" ? wrapBuffer(encoder3.encode(name)) : name;
11665
+ let names;
11565
11666
  try {
11566
- const names = syncFs.readdir(target);
11667
+ names = syncFs.readdir(target);
11668
+ } catch (err) {
11669
+ throw nodeFsError(err?.code || "ENOENT", "scandir", pathArg, err);
11670
+ }
11671
+ if (!options?.recursive) {
11567
11672
  if (options?.withFileTypes) {
11568
- return names.map((name) => {
11569
- let type = "unknown";
11673
+ return names.map((name) => new Dirent(name, typeAt(path.join(target, name)), String(pathArg)));
11674
+ }
11675
+ return names.map(asName);
11676
+ }
11677
+ const out = [];
11678
+ const queue = [{ dir: target, rel: "", parent: String(pathArg), names }];
11679
+ while (queue.length) {
11680
+ const { dir, rel, parent, names: entries } = queue.shift();
11681
+ for (const name of entries) {
11682
+ const full = path.join(dir, name);
11683
+ const type = typeAt(full);
11684
+ out.push(options.withFileTypes ? new Dirent(name, type, parent) : asName(rel ? `${rel}/${name}` : name));
11685
+ if (type === "dir") {
11686
+ let children;
11570
11687
  try {
11571
- const st = syncFs.lstat(path.join(target, name));
11572
- type = st.isDirectory ? "dir" : st.isSymbolicLink ? "link" : st.isFile ? "file" : "unknown";
11688
+ children = syncFs.readdir(full);
11573
11689
  } catch {
11690
+ children = [];
11574
11691
  }
11575
- return new Dirent(name, type);
11576
- });
11692
+ queue.push({
11693
+ dir: full,
11694
+ rel: rel ? `${rel}/${name}` : name,
11695
+ parent: path.join(parent, name),
11696
+ names: children
11697
+ });
11698
+ }
11577
11699
  }
11578
- return names;
11579
- } catch (err) {
11580
- throw nodeFsError(err?.code || "ENOENT", "scandir", pathArg, err);
11581
11700
  }
11701
+ return out;
11582
11702
  }
11583
11703
  const syncApi = {
11584
11704
  readFileSync(p, options) {
11585
- const encoding = typeof options === "string" ? options : options?.encoding;
11705
+ const encoding = optEncoding(options);
11586
11706
  if (p === 0 || p === "0") return readStdinSync(encoding);
11707
+ if (typeof p === "number") {
11708
+ const ent = fdTable.get(p);
11709
+ if (!ent || !ent.readable) throw nodeFsError("EBADF", "read", void 0);
11710
+ try {
11711
+ const bytes = syncFs.readFile(ent.path);
11712
+ const rest = bytes.subarray(Math.min(ent.pos, bytes.length));
11713
+ ent.pos += rest.length;
11714
+ return decodeAs(rest, encoding);
11715
+ } catch (err) {
11716
+ throw nodeFsError(err?.code || "EIO", "read", void 0, err);
11717
+ }
11718
+ }
11587
11719
  const pathArg = p;
11588
11720
  if (pathArg === "/dev/stdin" || pathArg === "/dev/fd/0") {
11589
11721
  return readStdinSync(encoding);
@@ -11598,32 +11730,52 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11598
11730
  e.syscall = "read";
11599
11731
  throw e;
11600
11732
  }
11601
- const bytes = syncFs.readFile(target);
11602
- if (encoding) return decoder3.decode(bytes);
11603
- return wrapBuffer(bytes);
11733
+ return decodeAs(syncFs.readFile(target), encoding);
11604
11734
  } catch (err) {
11605
11735
  if (err?.code && err.syscall) throw err;
11606
11736
  throw nodeFsError(err?.code || "ENOENT", "open", pathArg, err);
11607
11737
  }
11608
11738
  },
11609
11739
  writeFileSync(p, data2, options) {
11740
+ const bytes = encodeAs(data2, optEncoding(options));
11741
+ const flag = optFlag(options, "w");
11742
+ if (typeof p === "number") {
11743
+ syncApi.writeSync(p, bytes);
11744
+ return;
11745
+ }
11746
+ if (flag.includes("a")) {
11747
+ syncApi.appendFileSync(p, bytes);
11748
+ return;
11749
+ }
11610
11750
  const pathArg = p;
11611
11751
  const target = abs(p);
11612
- void options;
11752
+ if (flag.includes("x") && syncFs.exists(target)) throw nodeFsError("EEXIST", "open", pathArg);
11613
11753
  try {
11614
- syncFs.writeFile(target, toBytes(data2));
11754
+ syncFs.writeFile(target, bytes);
11615
11755
  } catch (err) {
11616
11756
  throw nodeFsError(err?.code || "EIO", "open", pathArg, err);
11617
11757
  }
11618
11758
  },
11619
11759
  appendFileSync(p, data2, options) {
11760
+ const next = encodeAs(data2, optEncoding(options));
11761
+ if (typeof p === "number") {
11762
+ const ent = fdTable.get(p);
11763
+ let end = null;
11764
+ if (ent) {
11765
+ try {
11766
+ end = syncFs.stat(ent.path).size ?? 0;
11767
+ } catch {
11768
+ end = 0;
11769
+ }
11770
+ }
11771
+ syncApi.writeSync(p, next, 0, next.length, end);
11772
+ return;
11773
+ }
11620
11774
  const pathArg = p;
11621
11775
  const target = abs(p);
11622
- void options;
11623
11776
  try {
11624
11777
  let prev = new Uint8Array(0);
11625
11778
  if (syncFs.exists(target)) prev = syncFs.readFile(target);
11626
- const next = toBytes(data2);
11627
11779
  const merged = new Uint8Array(prev.length + next.length);
11628
11780
  merged.set(prev);
11629
11781
  merged.set(next, prev.length);
@@ -11650,20 +11802,24 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11650
11802
  throw nodeFsError(err?.code || "ENOENT", "access", pathArg, err);
11651
11803
  }
11652
11804
  },
11653
- statSync(p) {
11805
+ statSync(p, options) {
11654
11806
  const pathArg = p;
11655
11807
  try {
11656
11808
  return statView(syncFs.stat(abs(p)));
11657
11809
  } catch (err) {
11658
- throw nodeFsError(err?.code || "ENOENT", "stat", pathArg, err);
11810
+ const code = err?.code || "ENOENT";
11811
+ if (options?.throwIfNoEntry === false && (code === "ENOENT" || code === "ENOTDIR")) return void 0;
11812
+ throw nodeFsError(code, "stat", pathArg, err);
11659
11813
  }
11660
11814
  },
11661
- lstatSync(p) {
11815
+ lstatSync(p, options) {
11662
11816
  const pathArg = p;
11663
11817
  try {
11664
11818
  return statView(syncFs.lstat(abs(p)));
11665
11819
  } catch (err) {
11666
- throw nodeFsError(err?.code || "ENOENT", "lstat", pathArg, err);
11820
+ const code = err?.code || "ENOENT";
11821
+ if (options?.throwIfNoEntry === false && (code === "ENOENT" || code === "ENOTDIR")) return void 0;
11822
+ throw nodeFsError(code, "lstat", pathArg, err);
11667
11823
  }
11668
11824
  },
11669
11825
  /**
@@ -11683,37 +11839,57 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11683
11839
  },
11684
11840
  readdirSync,
11685
11841
  mkdirSync,
11842
+ // The kernel's rm is recursive: only ever hand it an empty directory.
11686
11843
  rmdirSync(p, options = {}) {
11687
11844
  const pathArg = p;
11688
11845
  const target = abs(p);
11846
+ let st;
11689
11847
  try {
11690
- if (options.recursive) {
11691
- syncApi.rmSync(p, { recursive: true, force: options.force });
11692
- return;
11693
- }
11694
- syncFs.rm(target);
11848
+ st = syncFs.lstat(target);
11695
11849
  } catch (err) {
11696
11850
  throw nodeFsError(err?.code || "ENOENT", "rmdir", pathArg, err);
11697
11851
  }
11852
+ if (!st.isDirectory) throw nodeFsError("ENOTDIR", "rmdir", pathArg);
11853
+ if (options?.recursive) {
11854
+ syncApi.rmSync(p, { recursive: true, force: options.force });
11855
+ return;
11856
+ }
11857
+ let entries;
11858
+ try {
11859
+ entries = syncFs.readdir(target);
11860
+ } catch (err) {
11861
+ throw nodeFsError(err?.code || "EIO", "rmdir", pathArg, err);
11862
+ }
11863
+ if (entries.length > 0) throw nodeFsError("ENOTEMPTY", "rmdir", pathArg);
11864
+ try {
11865
+ syncFs.rm(target);
11866
+ } catch (err) {
11867
+ throw nodeFsError(err?.code || "EIO", "rmdir", pathArg, err);
11868
+ }
11698
11869
  },
11699
11870
  rmSync(p, options = {}) {
11700
11871
  const pathArg = p;
11701
11872
  const target = abs(p);
11873
+ let st;
11874
+ try {
11875
+ st = syncFs.lstat(target);
11876
+ } catch (err) {
11877
+ if (options?.force && (err?.code === "ENOENT" || err?.code === "ENOTDIR")) return;
11878
+ throw nodeFsError(err?.code || "ENOENT", "lstat", pathArg, err);
11879
+ }
11880
+ if (st.isDirectory && !options?.recursive) throw rmIsDirectoryError(pathArg);
11702
11881
  try {
11703
- const st = syncFs.lstat(target);
11704
11882
  if (st.isDirectory) {
11705
- if (options.recursive) {
11706
- for (const name of syncFs.readdir(target)) {
11707
- syncApi.rmSync(path.join(target, name), { recursive: true, force: options.force });
11708
- }
11883
+ for (const name of syncFs.readdir(target)) {
11884
+ syncApi.rmSync(path.join(target, name), { recursive: true, force: options.force });
11709
11885
  }
11710
11886
  syncFs.rm(target);
11711
11887
  } else {
11712
11888
  syncFs.unlink(target);
11713
11889
  }
11714
11890
  } catch (err) {
11715
- if (options.force && (err?.code === "ENOENT" || !syncFs.exists(target))) return;
11716
- throw nodeFsError(err?.code || "ENOENT", "rm", pathArg, err);
11891
+ if (err?.syscall) throw err;
11892
+ throw nodeFsError(err?.code || "EIO", "rm", pathArg, err);
11717
11893
  }
11718
11894
  },
11719
11895
  unlinkSync(p) {
@@ -11788,7 +11964,7 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11788
11964
  return n;
11789
11965
  }
11790
11966
  const ent = fdTable.get(fd);
11791
- if (!ent) throw nodeFsError("EBADF", "read", void 0);
11967
+ if (!ent || !ent.readable) throw nodeFsError("EBADF", "read", void 0);
11792
11968
  try {
11793
11969
  const bytes = syncFs.readFile(ent.path);
11794
11970
  const pos = filePosition == null ? ent.pos : filePosition;
@@ -11807,7 +11983,7 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11807
11983
  return bytes2.byteLength;
11808
11984
  }
11809
11985
  const ent = fdTable.get(fd);
11810
- if (!ent) throw nodeFsError("EBADF", "write", void 0);
11986
+ if (!ent || !ent.writable) throw nodeFsError("EBADF", "write", void 0);
11811
11987
  const { bytes, filePosition } = parseWriteSyncArgs(data2, offset2, length, position);
11812
11988
  try {
11813
11989
  return writeBytesAtPath(ent, bytes, filePosition);
@@ -11859,8 +12035,176 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11859
12035
  } catch (err) {
11860
12036
  throw nodeFsError(err?.code || "ENOENT", "readlink", p, err);
11861
12037
  }
12038
+ },
12039
+ copyFileSync(src, dest, mode = 0) {
12040
+ const from = abs(src);
12041
+ const to = abs(dest);
12042
+ let st;
12043
+ try {
12044
+ st = syncFs.stat(from);
12045
+ } catch (err) {
12046
+ throw nodeFsError(err?.code || "ENOENT", "copyfile", src, err, dest);
12047
+ }
12048
+ if (st.isDirectory) throw nodeFsError("EISDIR", "copyfile", src, void 0, dest);
12049
+ if (mode & 1 && syncFs.exists(to)) throw nodeFsError("EEXIST", "copyfile", src, void 0, dest);
12050
+ try {
12051
+ syncFs.writeFile(to, syncFs.readFile(from));
12052
+ } catch (err) {
12053
+ throw nodeFsError(err?.code || "EIO", "copyfile", src, err, dest);
12054
+ }
12055
+ },
12056
+ cpSync(src, dest, options = {}) {
12057
+ const from = abs(src);
12058
+ const to = abs(dest);
12059
+ if (from === to) throw cpInvalid(`src and dest cannot be the same ${src}`);
12060
+ if (to.startsWith(`${from}/`)) {
12061
+ throw cpInvalid(`Cannot copy ${src}/ to a subdirectory of self ${dest}`);
12062
+ }
12063
+ let st;
12064
+ try {
12065
+ st = syncFs.stat(from);
12066
+ } catch (err) {
12067
+ throw nodeFsError(err?.code || "ENOENT", "cp", src, err);
12068
+ }
12069
+ if (st.isDirectory) {
12070
+ if (!options?.recursive) {
12071
+ const e = new Error(`Recursive option not enabled, cannot copy a directory: ${src}/`);
12072
+ e.code = "ERR_FS_EISDIR";
12073
+ throw e;
12074
+ }
12075
+ mkdirSync(to, { recursive: true });
12076
+ for (const name of syncFs.readdir(from)) {
12077
+ syncApi.cpSync(path.join(from, name), path.join(to, name), options);
12078
+ }
12079
+ return;
12080
+ }
12081
+ const exists = syncFs.exists(to);
12082
+ if (exists && options?.errorOnExist && options?.force === false) {
12083
+ const e = new Error(`Target already exists: cp returned EEXIST (${to} already exists)`);
12084
+ e.name = "SystemError";
12085
+ e.code = "ERR_FS_CP_EEXIST";
12086
+ throw e;
12087
+ }
12088
+ if (exists && options?.force === false) return;
12089
+ syncFs.writeFile(to, syncFs.readFile(from));
12090
+ },
12091
+ mkdtempSync(prefix, options) {
12092
+ const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
12093
+ const bytes = new Uint8Array(6);
12094
+ for (let attempt = 0; attempt < 100; attempt++) {
12095
+ crypto.getRandomValues(bytes);
12096
+ const name = `${prefix}${[...bytes].map((b) => chars[b % chars.length]).join("")}`;
12097
+ const target = abs(name);
12098
+ if (syncFs.exists(target)) continue;
12099
+ try {
12100
+ syncFs.mkdir(target);
12101
+ } catch (err) {
12102
+ throw nodeFsError(err?.code || "ENOENT", "mkdtemp", `${prefix}XXXXXX`, err);
12103
+ }
12104
+ return optEncoding(options) === "buffer" ? wrapBuffer(encoder3.encode(name)) : name;
12105
+ }
12106
+ throw nodeFsError("EEXIST", "mkdtemp", `${prefix}XXXXXX`);
12107
+ },
12108
+ truncateSync(p, len = 0) {
12109
+ if (typeof p === "number") {
12110
+ syncApi.ftruncateSync(p, len);
12111
+ return;
12112
+ }
12113
+ const target = abs(p);
12114
+ let cur;
12115
+ try {
12116
+ cur = syncFs.readFile(target);
12117
+ } catch (err) {
12118
+ throw nodeFsError(err?.code || "ENOENT", "open", p, err);
12119
+ }
12120
+ const out = new Uint8Array(Math.max(0, len));
12121
+ out.set(cur.subarray(0, Math.min(cur.length, out.length)), 0);
12122
+ syncFs.writeFile(target, out);
12123
+ },
12124
+ // The kernel's JS fs has no mode bits, owners or times to set: these
12125
+ // check that the path exists, as Node's do, and change nothing.
12126
+ chmodSync(p) {
12127
+ mustExist(p, "chmod");
12128
+ },
12129
+ lchmodSync(p) {
12130
+ mustExist(p, "lchmod");
12131
+ },
12132
+ fchmodSync(fd) {
12133
+ syncApi.fstatSync(fd);
12134
+ },
12135
+ chownSync(p) {
12136
+ mustExist(p, "chown");
12137
+ },
12138
+ lchownSync(p) {
12139
+ mustExist(p, "lchown");
12140
+ },
12141
+ fchownSync(fd) {
12142
+ syncApi.fstatSync(fd);
12143
+ },
12144
+ utimesSync(p) {
12145
+ mustExist(p, "utime");
12146
+ },
12147
+ lutimesSync(p) {
12148
+ mustExist(p, "lutime");
12149
+ },
12150
+ futimesSync(fd) {
12151
+ syncApi.fstatSync(fd);
12152
+ },
12153
+ fsyncSync(fd) {
12154
+ if (fd !== 0 && fd !== 1 && fd !== 2 && !fdTable.has(fd)) throw nodeFsError("EBADF", "fsync", void 0);
12155
+ },
12156
+ fdatasyncSync(fd) {
12157
+ if (fd !== 0 && fd !== 1 && fd !== 2 && !fdTable.has(fd)) throw nodeFsError("EBADF", "fdatasync", void 0);
12158
+ },
12159
+ opendirSync(p) {
12160
+ const entries = readdirSync(p, { withFileTypes: true });
12161
+ return createDir(String(p), entries);
11862
12162
  }
11863
12163
  };
12164
+ function createDir(dirPath, entries) {
12165
+ let closed = false;
12166
+ const next = () => {
12167
+ if (closed) {
12168
+ const e = new Error("Directory handle was closed");
12169
+ e.code = "ERR_DIR_CLOSED";
12170
+ throw e;
12171
+ }
12172
+ return entries.length ? entries.shift() : null;
12173
+ };
12174
+ return {
12175
+ path: dirPath,
12176
+ readSync: next,
12177
+ read(cb) {
12178
+ if (typeof cb === "function") {
12179
+ queueMicrotask(() => {
12180
+ try {
12181
+ cb(null, next());
12182
+ } catch (err) {
12183
+ cb(err);
12184
+ }
12185
+ });
12186
+ return void 0;
12187
+ }
12188
+ return Promise.resolve().then(next);
12189
+ },
12190
+ closeSync() {
12191
+ closed = true;
12192
+ },
12193
+ close(cb) {
12194
+ closed = true;
12195
+ if (typeof cb === "function") queueMicrotask(() => cb(null));
12196
+ else return Promise.resolve();
12197
+ return void 0;
12198
+ },
12199
+ async *[Symbol.asyncIterator]() {
12200
+ try {
12201
+ for (let d = next(); d; d = next()) yield d;
12202
+ } finally {
12203
+ closed = true;
12204
+ }
12205
+ }
12206
+ };
12207
+ }
11864
12208
  function requireCallback(cb) {
11865
12209
  if (typeof cb === "function") return cb;
11866
12210
  let received;
@@ -11902,11 +12246,9 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11902
12246
  readFile(p, options) {
11903
12247
  return track(
11904
12248
  (async () => {
11905
- const encoding = typeof options === "string" ? options : options?.encoding;
12249
+ const encoding = optEncoding(options);
11906
12250
  try {
11907
- const bytes = await asyncFs.readFile(abs(p));
11908
- if (encoding) return decoder3.decode(bytes);
11909
- return wrapBuffer(bytes);
12251
+ return decodeAs(await asyncFs.readFile(abs(p)), encoding);
11910
12252
  } catch (err) {
11911
12253
  throw nodeFsError(err?.code || "ENOENT", "open", p, err);
11912
12254
  }
@@ -11916,23 +12258,27 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11916
12258
  writeFile(p, data2, options) {
11917
12259
  return track(
11918
12260
  (async () => {
11919
- void options;
12261
+ const bytes = encodeAs(data2, optEncoding(options));
12262
+ const flag = optFlag(options, "w");
12263
+ if (flag.includes("a")) return promises.appendFile(p, bytes);
12264
+ const target = abs(p);
12265
+ if (flag.includes("x") && await asyncFs.exists(target)) throw nodeFsError("EEXIST", "open", p);
11920
12266
  try {
11921
- await asyncFs.writeFile(abs(p), toBytes(data2));
12267
+ await asyncFs.writeFile(target, bytes);
11922
12268
  } catch (err) {
11923
12269
  throw nodeFsError(err?.code || "EIO", "open", p, err);
11924
12270
  }
12271
+ return void 0;
11925
12272
  })()
11926
12273
  );
11927
12274
  },
11928
12275
  appendFile(p, data2, options) {
11929
12276
  return track(
11930
12277
  (async () => {
11931
- void options;
11932
12278
  const target = abs(p);
11933
12279
  let prev = new Uint8Array(0);
11934
12280
  if (await asyncFs.exists(target)) prev = await asyncFs.readFile(target);
11935
- const next = toBytes(data2);
12281
+ const next = encodeAs(data2, optEncoding(options));
11936
12282
  const merged = new Uint8Array(prev.length + next.length);
11937
12283
  merged.set(prev);
11938
12284
  merged.set(next, prev.length);
@@ -11963,75 +12309,52 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
11963
12309
  );
11964
12310
  },
11965
12311
  readdir(p, options) {
11966
- return track(
11967
- (async () => {
11968
- try {
11969
- const names = await asyncFs.readdir(abs(p));
11970
- if (options?.withFileTypes) {
11971
- const out = [];
11972
- for (const name of names) {
11973
- let type = "unknown";
11974
- try {
11975
- const st = await asyncFs.lstat(path.join(abs(p), name));
11976
- type = st.isDirectory ? "dir" : st.isSymbolicLink ? "link" : st.isFile ? "file" : "unknown";
11977
- } catch {
11978
- }
11979
- out.push(new Dirent(name, type));
11980
- }
11981
- return out;
11982
- }
11983
- return names;
11984
- } catch (err) {
11985
- throw nodeFsError(err?.code || "ENOENT", "scandir", p, err);
11986
- }
11987
- })()
11988
- );
12312
+ return track((async () => readdirSync(p, options))());
11989
12313
  },
11990
12314
  mkdir(p, options = {}) {
11991
- return track(
11992
- (async () => {
11993
- const target = abs(p);
11994
- if (options?.recursive) {
11995
- const parts = String(target).split("/").filter(Boolean);
11996
- let cur = "";
11997
- for (const part of parts) {
11998
- cur += `/${part}`;
11999
- if (await asyncFs.exists(cur)) continue;
12000
- try {
12001
- await asyncFs.mkdir(cur);
12002
- } catch (err) {
12003
- if (err?.code !== "EEXIST") throw nodeFsError(err?.code || "EIO", "mkdir", p, err);
12004
- }
12005
- }
12006
- return;
12007
- }
12008
- if (await asyncFs.exists(target)) throw nodeFsError("EEXIST", "mkdir", p);
12009
- await asyncFs.mkdir(target);
12010
- })()
12011
- );
12315
+ return track((async () => mkdirSync(p, options))());
12012
12316
  },
12013
12317
  rm(p, options = {}) {
12014
- return track(
12015
- (async () => {
12016
- const target = abs(p);
12017
- try {
12018
- const st = await asyncFs.lstat(target);
12019
- if (st.isDirectory) {
12020
- if (options.recursive) {
12021
- for (const name of await asyncFs.readdir(target)) {
12022
- await promises.rm(path.join(target, name), { recursive: true, force: options.force });
12023
- }
12024
- }
12025
- await asyncFs.rm(target);
12026
- } else {
12027
- await asyncFs.unlink(target);
12028
- }
12029
- } catch (err) {
12030
- if (options.force && err?.code === "ENOENT") return;
12031
- throw nodeFsError(err?.code || "ENOENT", "rm", p, err);
12032
- }
12033
- })()
12034
- );
12318
+ return track((async () => syncApi.rmSync(p, options))());
12319
+ },
12320
+ rmdir(p, options = {}) {
12321
+ return track((async () => syncApi.rmdirSync(p, options))());
12322
+ },
12323
+ copyFile(src, dest, mode) {
12324
+ return track((async () => syncApi.copyFileSync(src, dest, mode))());
12325
+ },
12326
+ cp(src, dest, options) {
12327
+ return track((async () => syncApi.cpSync(src, dest, options))());
12328
+ },
12329
+ mkdtemp(prefix, options) {
12330
+ return track((async () => syncApi.mkdtempSync(prefix, options))());
12331
+ },
12332
+ truncate(p, len) {
12333
+ return track((async () => syncApi.truncateSync(p, len))());
12334
+ },
12335
+ realpath(p) {
12336
+ return track((async () => syncApi.realpathSync(p))());
12337
+ },
12338
+ chmod(p) {
12339
+ return track((async () => syncApi.chmodSync(p))());
12340
+ },
12341
+ lchmod(p) {
12342
+ return track((async () => syncApi.lchmodSync(p))());
12343
+ },
12344
+ chown(p) {
12345
+ return track((async () => syncApi.chownSync(p))());
12346
+ },
12347
+ lchown(p) {
12348
+ return track((async () => syncApi.lchownSync(p))());
12349
+ },
12350
+ utimes(p) {
12351
+ return track((async () => syncApi.utimesSync(p))());
12352
+ },
12353
+ lutimes(p) {
12354
+ return track((async () => syncApi.lutimesSync(p))());
12355
+ },
12356
+ opendir(p) {
12357
+ return track((async () => syncApi.opendirSync(p))());
12035
12358
  },
12036
12359
  unlink(p) {
12037
12360
  return track(
@@ -12120,10 +12443,32 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
12120
12443
  ensureOpen("truncate");
12121
12444
  syncApi.ftruncateSync(fd, len);
12122
12445
  },
12446
+ async readFile(options) {
12447
+ ensureOpen("readFile");
12448
+ return syncApi.readFileSync(fd, options);
12449
+ },
12450
+ async sync() {
12451
+ ensureOpen("fsync");
12452
+ },
12453
+ async datasync() {
12454
+ ensureOpen("fdatasync");
12455
+ },
12456
+ async chmod() {
12457
+ ensureOpen("fchmod");
12458
+ },
12459
+ async chown() {
12460
+ ensureOpen("fchown");
12461
+ },
12462
+ async utimes() {
12463
+ ensureOpen("futimes");
12464
+ },
12123
12465
  async close() {
12124
12466
  ensureOpen("close");
12125
12467
  closed = true;
12126
12468
  syncApi.closeSync(fd);
12469
+ },
12470
+ async [Symbol.asyncDispose ?? /* @__PURE__ */ Symbol.for("nodejs.asyncDispose")]() {
12471
+ if (!closed) await handle.close();
12127
12472
  }
12128
12473
  };
12129
12474
  return handle;
@@ -12185,6 +12530,7 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
12185
12530
  });
12186
12531
  r.path = p;
12187
12532
  r.bytesRead = 0;
12533
+ Object.defineProperty(r, FS_READ_STREAM, { value: true });
12188
12534
  if (!openEmitted) {
12189
12535
  queueMicrotask(() => {
12190
12536
  if (openEmitted || r.destroyed) return;
@@ -12248,6 +12594,7 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
12248
12594
  });
12249
12595
  w.path = p;
12250
12596
  w.bytesWritten = 0;
12597
+ Object.defineProperty(w, FS_WRITE_STREAM, { value: true });
12251
12598
  if (!openEmitted) {
12252
12599
  queueMicrotask(() => {
12253
12600
  if (openEmitted || w.destroyed) return;
@@ -12270,10 +12617,20 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
12270
12617
  }
12271
12618
  return w;
12272
12619
  }
12620
+ function ReadStream(p, options) {
12621
+ return createReadStream(p, options);
12622
+ }
12623
+ Object.defineProperty(ReadStream, Symbol.hasInstance, { value: (x) => !!x?.[FS_READ_STREAM] });
12624
+ function WriteStream(p, options) {
12625
+ return createWriteStream(p, options);
12626
+ }
12627
+ Object.defineProperty(WriteStream, Symbol.hasInstance, { value: (x) => !!x?.[FS_WRITE_STREAM] });
12273
12628
  return {
12274
12629
  ...syncApi,
12275
12630
  promises,
12276
12631
  Dirent,
12632
+ ReadStream,
12633
+ WriteStream,
12277
12634
  createReadStream,
12278
12635
  createWriteStream,
12279
12636
  readFile: callbackify(syncApi.readFileSync),
@@ -12321,10 +12678,55 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
12321
12678
  lstat: callbackify(syncApi.lstatSync),
12322
12679
  realpath: callbackify(syncApi.realpathSync),
12323
12680
  readdir: callbackify(syncApi.readdirSync),
12324
- mkdir: callbackify(syncApi.mkdirSync),
12681
+ mkdir: callbackify(syncApi.mkdirSync, { minArgsBeforeCb: 2 }),
12325
12682
  rmdir: callbackify(syncApi.rmdirSync),
12326
12683
  unlink: callbackify(syncApi.unlinkSync),
12327
12684
  rename: callbackify(syncApi.renameSync, { minArgsBeforeCb: 2 }),
12685
+ // Node 22's rm does not check its callback up front: without one it does
12686
+ // the work, then calls the missing callback and throws from there.
12687
+ rm(p, options, callback) {
12688
+ const cb = typeof options === "function" ? options : callback;
12689
+ const opts = typeof options === "function" ? void 0 : options;
12690
+ if (typeof cb === "function") return callbackify(syncApi.rmSync)(p, opts ?? {}, cb);
12691
+ let err = null;
12692
+ try {
12693
+ syncApi.rmSync(p, opts ?? {});
12694
+ } catch (e) {
12695
+ err = e;
12696
+ }
12697
+ void err;
12698
+ setTimeout(() => {
12699
+ const e = new TypeError("callback is not a function");
12700
+ e._fsPretty = `node:fs:1225
12701
+ return callback(err);
12702
+ ^
12703
+
12704
+ ${e.stack}
12705
+ `;
12706
+ throw e;
12707
+ }, 0);
12708
+ return void 0;
12709
+ },
12710
+ copyFile: callbackify(syncApi.copyFileSync, { minArgsBeforeCb: 2 }),
12711
+ cp: callbackify(syncApi.cpSync, { minArgsBeforeCb: 2 }),
12712
+ mkdtemp: callbackify(syncApi.mkdtempSync),
12713
+ truncate: callbackify(syncApi.truncateSync),
12714
+ ftruncate: callbackify(syncApi.ftruncateSync),
12715
+ chmod: callbackify(syncApi.chmodSync, { minArgsBeforeCb: 2 }),
12716
+ lchmod: callbackify(syncApi.lchmodSync, { minArgsBeforeCb: 2 }),
12717
+ fchmod: callbackify(syncApi.fchmodSync, { minArgsBeforeCb: 2 }),
12718
+ chown: callbackify(syncApi.chownSync, { minArgsBeforeCb: 3 }),
12719
+ lchown: callbackify(syncApi.lchownSync, { minArgsBeforeCb: 3 }),
12720
+ fchown: callbackify(syncApi.fchownSync, { minArgsBeforeCb: 3 }),
12721
+ utimes: callbackify(syncApi.utimesSync, { minArgsBeforeCb: 3 }),
12722
+ lutimes: callbackify(syncApi.lutimesSync, { minArgsBeforeCb: 3 }),
12723
+ futimes: callbackify(syncApi.futimesSync, { minArgsBeforeCb: 3 }),
12724
+ fsync: callbackify(syncApi.fsyncSync),
12725
+ fdatasync: callbackify(syncApi.fdatasyncSync),
12726
+ opendir: callbackify(syncApi.opendirSync),
12727
+ symlink: callbackify(syncApi.symlinkSync, { minArgsBeforeCb: 2 }),
12728
+ readlink: callbackify(syncApi.readlinkSync),
12729
+ Stats,
12328
12730
  constants: {
12329
12731
  F_OK: 0,
12330
12732
  R_OK: 4,
@@ -12335,7 +12737,15 @@ function createFs({ ctx, path, process: process2, Buffer: Buffer2, handles, getS
12335
12737
  O_RDWR: 2,
12336
12738
  O_CREAT: 64,
12337
12739
  O_TRUNC: 512,
12338
- O_APPEND: 1024
12740
+ O_APPEND: 1024,
12741
+ O_EXCL: 128,
12742
+ S_IFMT,
12743
+ S_IFREG,
12744
+ S_IFDIR,
12745
+ S_IFLNK,
12746
+ COPYFILE_EXCL: 1,
12747
+ COPYFILE_FICLONE: 2,
12748
+ COPYFILE_FICLONE_FORCE: 4
12339
12749
  }
12340
12750
  };
12341
12751
  }
@@ -14320,6 +14730,94 @@ function notSupported3(id) {
14320
14730
  err.code = "ERR_NOT_SUPPORTED";
14321
14731
  throw err;
14322
14732
  }
14733
+ function describeReceived(value) {
14734
+ if (value === null) return "null";
14735
+ if (value === void 0) return "undefined";
14736
+ if (typeof value === "string") return `type string ('${value}')`;
14737
+ if (typeof value === "function") return `function ${value.name || "<anonymous>"}`;
14738
+ if (typeof value === "object") return `an instance of ${value.constructor?.name || "Object"}`;
14739
+ return `type ${typeof value} (${String(value)})`;
14740
+ }
14741
+ function invalidArgType(name, expected, value) {
14742
+ const err = new TypeError(
14743
+ `The "${name}" argument must be ${expected}. Received ${describeReceived(value)}`
14744
+ );
14745
+ err.code = "ERR_INVALID_ARG_TYPE";
14746
+ return err;
14747
+ }
14748
+ var TRANSCODE_ENCODINGS = {
14749
+ ascii: "ascii",
14750
+ latin1: "latin1",
14751
+ binary: "latin1",
14752
+ ucs2: "utf16le",
14753
+ "ucs-2": "utf16le",
14754
+ utf16le: "utf16le",
14755
+ "utf-16le": "utf16le",
14756
+ utf8: "utf8",
14757
+ "utf-8": "utf8"
14758
+ };
14759
+ function bufferModule(Buffer2) {
14760
+ const MAX_LENGTH = 2 ** 53 - 1;
14761
+ const MAX_STRING_LENGTH = 536870888;
14762
+ const fatal = new TextDecoder("utf-8", { fatal: true });
14763
+ const bytesOf = (input) => {
14764
+ if (input instanceof ArrayBuffer) return new Uint8Array(input);
14765
+ if (typeof SharedArrayBuffer === "function" && input instanceof SharedArrayBuffer) {
14766
+ return new Uint8Array(input);
14767
+ }
14768
+ if (ArrayBuffer.isView(input)) return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
14769
+ throw invalidArgType("input", "an instance of ArrayBuffer, Buffer, or TypedArray", input);
14770
+ };
14771
+ function SlowBuffer(size) {
14772
+ return Buffer2.allocUnsafeSlow(size);
14773
+ }
14774
+ return {
14775
+ Buffer: Buffer2,
14776
+ SlowBuffer,
14777
+ /**
14778
+ * Node's transcode (ICU): text a target cannot hold becomes '?', as ICU's
14779
+ * substitution does; invalid UTF-8 decodes to U+FFFD first.
14780
+ */
14781
+ transcode(source, fromEnc, toEnc) {
14782
+ if (!(source instanceof Uint8Array)) {
14783
+ throw invalidArgType("source", "an instance of Buffer or Uint8Array", source);
14784
+ }
14785
+ const from = TRANSCODE_ENCODINGS[String(fromEnc).toLowerCase()];
14786
+ const to = TRANSCODE_ENCODINGS[String(toEnc).toLowerCase()];
14787
+ if (!from || !to) {
14788
+ const err = new Error("Unable to transcode Buffer [U_ILLEGAL_ARGUMENT_ERROR]");
14789
+ err.code = "U_ILLEGAL_ARGUMENT_ERROR";
14790
+ err.errno = 1;
14791
+ throw err;
14792
+ }
14793
+ let text = Buffer2.from(source).toString(from);
14794
+ if (to === "latin1") text = text.replace(/[^\u0000-\u00ff]/gu, "?");
14795
+ else if (to === "ascii") text = text.replace(/[^\u0000-\u007f]/gu, "?");
14796
+ return Buffer2.from(text, to);
14797
+ },
14798
+ isUtf8(input) {
14799
+ const bytes = bytesOf(input);
14800
+ try {
14801
+ fatal.decode(bytes);
14802
+ return true;
14803
+ } catch {
14804
+ return false;
14805
+ }
14806
+ },
14807
+ isAscii(input) {
14808
+ return bytesOf(input).every((b) => b < 128);
14809
+ },
14810
+ kMaxLength: MAX_LENGTH,
14811
+ kStringMaxLength: MAX_STRING_LENGTH,
14812
+ btoa: globalThis.btoa,
14813
+ atob: globalThis.atob,
14814
+ constants: { MAX_LENGTH, MAX_STRING_LENGTH },
14815
+ INSPECT_MAX_BYTES: 50,
14816
+ Blob: globalThis.Blob,
14817
+ File: globalThis.File,
14818
+ resolveObjectURL: () => void 0
14819
+ };
14820
+ }
14323
14821
  function createBuiltins({ ctx, process: process2, Buffer: Buffer2, console: console2, path, handles, timers }) {
14324
14822
  const util2 = createUtil();
14325
14823
  const events = createEvents();
@@ -14368,10 +14866,11 @@ function createBuiltins({ ctx, process: process2, Buffer: Buffer2, console: cons
14368
14866
  }
14369
14867
  return childProcess;
14370
14868
  };
14869
+ let bufferExports;
14371
14870
  let ModuleExport = null;
14372
14871
  const implemented = {
14373
14872
  process: () => process2,
14374
- buffer: () => ({ Buffer: Buffer2, kMaxLength: 2147483647, INSPECT_MAX_BYTES: 50 }),
14873
+ buffer: () => bufferExports ??= bufferModule(Buffer2),
14375
14874
  console: () => console2,
14376
14875
  path: () => path,
14377
14876
  "path/posix": () => path,
@@ -16118,7 +16617,7 @@ function numericExitCode(value) {
16118
16617
  const n = Number(value);
16119
16618
  return Number.isFinite(n) ? n & 255 : 0;
16120
16619
  }
16121
- function describeReceived(value) {
16620
+ function describeReceived2(value) {
16122
16621
  if (typeof value === "string") return `type string ('${value}')`;
16123
16622
  if (typeof value === "boolean") return `type boolean (${value})`;
16124
16623
  if (typeof value === "bigint") return `type bigint (${value}n)`;
@@ -16132,7 +16631,7 @@ function assignProcessExitCode(value) {
16132
16631
  const n = Number(value);
16133
16632
  if (value !== "" && Number.isInteger(n)) return n === 0 ? 0 : n;
16134
16633
  const err2 = new TypeError(
16135
- `The "code" argument must be of type number. Received ${describeReceived(value)}`
16634
+ `The "code" argument must be of type number. Received ${describeReceived2(value)}`
16136
16635
  );
16137
16636
  err2.code = "ERR_INVALID_ARG_TYPE";
16138
16637
  throw err2;
@@ -16146,7 +16645,7 @@ function assignProcessExitCode(value) {
16146
16645
  throw err2;
16147
16646
  }
16148
16647
  const err = new TypeError(
16149
- `The "code" argument must be of type number. Received ${describeReceived(value)}`
16648
+ `The "code" argument must be of type number. Received ${describeReceived2(value)}`
16150
16649
  );
16151
16650
  err.code = "ERR_INVALID_ARG_TYPE";
16152
16651
  throw err;
@@ -16233,6 +16732,32 @@ function toBytes2(value) {
16233
16732
  if (value == null) return "";
16234
16733
  return String(value);
16235
16734
  }
16735
+ function createEnv(initial) {
16736
+ const target = { ...initial };
16737
+ const coerce = (value) => {
16738
+ if (typeof value === "symbol") {
16739
+ throw new TypeError("Cannot convert a Symbol value to a string");
16740
+ }
16741
+ return String(value);
16742
+ };
16743
+ return new Proxy(target, {
16744
+ set(t, key, value) {
16745
+ if (typeof key === "symbol") {
16746
+ t[key] = value;
16747
+ return true;
16748
+ }
16749
+ t[key] = coerce(value);
16750
+ return true;
16751
+ },
16752
+ defineProperty(t, key, desc) {
16753
+ if (typeof key !== "symbol" && "value" in desc) {
16754
+ t[key] = coerce(desc.value);
16755
+ return true;
16756
+ }
16757
+ return Reflect.defineProperty(t, key, desc);
16758
+ }
16759
+ });
16760
+ }
16236
16761
  function writableStream(fd, writeSync, isTTY) {
16237
16762
  const ee = new EventEmitter();
16238
16763
  return {
@@ -16513,7 +17038,7 @@ var Process = class extends EventEmitter {
16513
17038
  this._onExit = onExit;
16514
17039
  this.argv = argv.slice();
16515
17040
  this.argv0 = argv[0] ?? "node";
16516
- this.env = { ...ctx.env };
17041
+ this.env = createEnv(ctx.env);
16517
17042
  this.versions = { node: NODE_SHIM_VERSION };
16518
17043
  this.version = `v${NODE_SHIM_VERSION}`;
16519
17044
  this.platform = "linux";