@php-wasm/universal 3.0.5 → 3.0.8

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/index.js CHANGED
@@ -1,11 +1,11 @@
1
- var B = (t) => {
1
+ var z = (t) => {
2
2
  throw TypeError(t);
3
3
  };
4
- var O = (t, e, r) => e.has(t) || B("Cannot " + r);
5
- var u = (t, e, r) => (O(t, e, "read from private field"), r ? r.call(t) : e.get(t)), m = (t, e, r) => e.has(t) ? B("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, r), w = (t, e, r, s) => (O(t, e, "write to private field"), s ? s.call(t, r) : e.set(t, r), r), y = (t, e, r) => (O(t, e, "access private method"), r);
4
+ var O = (t, e, r) => e.has(t) || z("Cannot " + r);
5
+ var u = (t, e, r) => (O(t, e, "read from private field"), r ? r.call(t) : e.get(t)), m = (t, e, r) => e.has(t) ? z("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, r), w = (t, e, r, s) => (O(t, e, "write to private field"), s ? s.call(t, r) : e.set(t, r), r), y = (t, e, r) => (O(t, e, "access private method"), r);
6
6
  import "@php-wasm/node-polyfills";
7
7
  import { logger } from "@php-wasm/logger";
8
- import { dirname, joinPaths, Semaphore, createSpawnHandler, normalizePath, AcquireTimeoutError } from "@php-wasm/util";
8
+ import { dirname, joinPaths, Semaphore, createSpawnHandler, basename, normalizePath, AcquireTimeoutError } from "@php-wasm/util";
9
9
  import { parse, stringify } from "ini";
10
10
  import { StreamedFile } from "@php-wasm/stream-compression";
11
11
  class ErrnoError extends Error {
@@ -368,12 +368,12 @@ class PHPWorker {
368
368
  /** @inheritDoc */
369
369
  constructor(e, r) {
370
370
  m(this, P);
371
- this.absoluteUrl = "", this.documentRoot = "", w(this, P, /* @__PURE__ */ new Map()), this.onMessageListeners = [], _private.set(this, {
371
+ this.absoluteUrl = "", this.documentRoot = "", this.chroot = null, w(this, P, /* @__PURE__ */ new Map()), this.onMessageListeners = [], _private.set(this, {
372
372
  monitor: r
373
373
  }), e && this.__internal_setRequestHandler(e);
374
374
  }
375
375
  __internal_setRequestHandler(e) {
376
- this.absoluteUrl = e.absoluteUrl, this.documentRoot = e.documentRoot, _private.set(this, {
376
+ this.absoluteUrl = e.absoluteUrl, this.documentRoot = e.documentRoot, this.chroot = this.documentRoot, _private.set(this, {
377
377
  ..._private.get(this),
378
378
  requestHandler: e
379
379
  });
@@ -433,7 +433,7 @@ class PHPWorker {
433
433
  }
434
434
  /** @inheritDoc @php-wasm/universal!/PHP.run */
435
435
  async run(e) {
436
- const { php: r, reap: s } = await _private.get(this).requestHandler.processManager.acquirePHPInstance();
436
+ const { php: r, reap: s } = await this.acquirePHPInstance();
437
437
  try {
438
438
  return await r.run(e);
439
439
  } finally {
@@ -442,16 +442,29 @@ class PHPWorker {
442
442
  }
443
443
  /** @inheritDoc @php-wasm/universal!/PHP.cli */
444
444
  async cli(e, r) {
445
- const { php: s, reap: n } = await _private.get(this).requestHandler.processManager.acquirePHPInstance();
445
+ const { php: s, reap: n } = await this.acquirePHPInstance();
446
+ let i;
446
447
  try {
447
- return await s.cli(e, r);
448
- } finally {
449
- n();
448
+ i = await s.cli(e, r);
449
+ } catch (o) {
450
+ throw n(), o;
450
451
  }
452
+ return i.finished.finally(n), i;
451
453
  }
452
454
  /** @inheritDoc @php-wasm/universal!/PHP.chdir */
453
455
  chdir(e) {
454
- return _private.get(this).php.chdir(e);
456
+ return this.chroot = e, _private.get(this).php.chdir(e);
457
+ }
458
+ /** @inheritDoc @php-wasm/universal!/PHP.chdir */
459
+ cwd() {
460
+ return _private.get(this).php.cwd();
461
+ }
462
+ /**
463
+ * @returns A PHP instance with a consistent chroot.
464
+ */
465
+ async acquirePHPInstance() {
466
+ const { php: e, reap: r } = await _private.get(this).requestHandler.processManager.acquirePHPInstance();
467
+ return this.chroot !== null && e.chdir(this.chroot), this.registerWorkerListeners(e), { php: e, reap: r };
455
468
  }
456
469
  /** @inheritDoc @php-wasm/universal!/PHP.setSapiName */
457
470
  setSapiName(e) {
@@ -937,7 +950,7 @@ class PHPExecutionFailureError extends Error {
937
950
  }
938
951
  }
939
952
  const PHP_INI_PATH = "/internal/shared/php.ini", AUTO_PREPEND_SCRIPT = "/internal/shared/auto_prepend_file.php", OPCACHE_FILE_FOLDER = "/internal/shared/opcache";
940
- var C, b, T, E, F, M, _, h, z, $, V, G, J, Y, K, X, U, Q, j, q;
953
+ var C, H, T, E, F, M, _, h, B, $, V, G, J, Y, K, X, U, Q, q, D;
941
954
  class PHP {
942
955
  /**
943
956
  * Initializes a PHP runtime.
@@ -949,7 +962,7 @@ class PHP {
949
962
  constructor(t) {
950
963
  m(this, h);
951
964
  m(this, C);
952
- m(this, b, !1);
965
+ m(this, H, !1);
953
966
  m(this, T, null);
954
967
  m(this, E, /* @__PURE__ */ new Map([
955
968
  // Listen to all events
@@ -1163,6 +1176,14 @@ class PHP {
1163
1176
  chdir(t) {
1164
1177
  this[__private__dont__use].FS.chdir(t);
1165
1178
  }
1179
+ /**
1180
+ * Gets the current working directory in the PHP filesystem.
1181
+ *
1182
+ * @returns The current working directory.
1183
+ */
1184
+ cwd() {
1185
+ return this[__private__dont__use].FS.cwd();
1186
+ }
1166
1187
  /**
1167
1188
  * Changes the permissions of a file or directory.
1168
1189
  * @param path - The path to the file or directory.
@@ -1366,8 +1387,8 @@ class PHP {
1366
1387
  async runStream(t) {
1367
1388
  const e = await this.semaphore.acquire();
1368
1389
  let r;
1369
- const s = y(this, h, q).call(this, async () => {
1370
- if (u(this, b) || (await this[__private__dont__use].ccall(
1390
+ const s = y(this, h, D).call(this, async () => {
1391
+ if (u(this, H) || (await this[__private__dont__use].ccall(
1371
1392
  "php_wasm_init",
1372
1393
  null,
1373
1394
  [],
@@ -1375,7 +1396,7 @@ class PHP {
1375
1396
  {
1376
1397
  isAsync: !0
1377
1398
  }
1378
- ), w(this, b, !0)), t.scriptPath && !this.fileExists(t.scriptPath))
1399
+ ), w(this, H, !0)), t.scriptPath && !this.fileExists(t.scriptPath))
1379
1400
  throw new Error(
1380
1401
  `The script path "${t.scriptPath}" does not exist.`
1381
1402
  );
@@ -1389,12 +1410,12 @@ class PHP {
1389
1410
  throw new TypeError(
1390
1411
  "The request object must have either a `code` or a `scriptPath` property."
1391
1412
  );
1392
- const c = y(this, h, z).call(this, t.$_SERVER, i, a);
1393
- for (const p in c)
1394
- y(this, h, Q).call(this, p, c[p]);
1413
+ const c = y(this, h, B).call(this, t.$_SERVER, i, a);
1414
+ for (const d in c)
1415
+ y(this, h, Q).call(this, d, c[d]);
1395
1416
  const l = t.env || {};
1396
- for (const p in l)
1397
- y(this, h, j).call(this, p, l[p]);
1417
+ for (const d in l)
1418
+ y(this, h, q).call(this, d, l[d]);
1398
1419
  return await this[__private__dont__use].ccall(
1399
1420
  "wasm_sapi_handle_request",
1400
1421
  NUMBER,
@@ -1604,8 +1625,7 @@ class PHP {
1604
1625
  ...u(this, _),
1605
1626
  enabled: !0,
1606
1627
  recreateRuntime: t.recreateRuntime,
1607
- maxRequests: t.maxRequests ?? 400,
1608
- cwd: t.cwd
1628
+ maxRequests: t.maxRequests ?? 400
1609
1629
  });
1610
1630
  }
1611
1631
  async rotateRuntime() {
@@ -1624,19 +1644,31 @@ class PHP {
1624
1644
  * @param runtime
1625
1645
  */
1626
1646
  async hotSwapPHPRuntime(t) {
1627
- const e = this[__private__dont__use].FS, r = this.listFiles("/").map((o) => `/${o}`), s = this[__private__dont__use].spawnProcess, n = [];
1628
- for (const [o, a] of Object.entries(u(this, M)))
1629
- n.push({ mountHandler: a.mountHandler, vfsPath: o }), await a.unmount();
1647
+ const e = this[__private__dont__use].FS, r = this.listFiles("/").map((a) => `/${a}`), s = this[__private__dont__use].spawnProcess, n = e.cwd();
1648
+ e.chdir("/");
1649
+ const i = [];
1650
+ for (const [a, c] of Object.entries(u(this, M)))
1651
+ i.push({ mountHandler: c.mountHandler, vfsPath: a }), await c.unmount();
1630
1652
  try {
1631
1653
  this.exit();
1632
1654
  } catch {
1633
1655
  }
1634
1656
  this.initializeRuntime(t), s && (this[__private__dont__use].spawnProcess = s), u(this, C) && this.setSapiName(u(this, C));
1635
- const i = this[__private__dont__use].FS;
1636
- for (const o of r)
1637
- o && o !== "/request" && copyMEMFSNodes(e, i, o);
1638
- for (const { mountHandler: o, vfsPath: a } of n)
1639
- this.mkdir(a), await this.mount(a, o);
1657
+ const o = this[__private__dont__use].FS;
1658
+ for (const a of r)
1659
+ a && a !== "/request" && copyMEMFSNodes(e, o, a);
1660
+ for (const { mountHandler: a, vfsPath: c } of i)
1661
+ this.mkdir(c), await this.mount(c, a);
1662
+ try {
1663
+ o.chdir(n);
1664
+ } catch (a) {
1665
+ throw new Error(
1666
+ `Failed to restore CWD to ${n} after PHP runtime rotation.`,
1667
+ {
1668
+ cause: a
1669
+ }
1670
+ );
1671
+ }
1640
1672
  }
1641
1673
  /**
1642
1674
  * Mounts a filesystem to a given path in the PHP filesystem.
@@ -1674,12 +1706,14 @@ class PHP {
1674
1706
  * @returns The exit code of the CLI session.
1675
1707
  */
1676
1708
  async cli(t, e = {}) {
1677
- u(this, b) && (u(this, _).needsRotating = !0);
1709
+ if (basename(t[0] ?? "") !== "php")
1710
+ return this.subProcess(t, e);
1711
+ u(this, H) && (u(this, _).needsRotating = !0);
1678
1712
  const r = await this.semaphore.acquire();
1679
- return await y(this, h, q).call(this, () => {
1713
+ return await y(this, h, D).call(this, () => {
1680
1714
  const s = e.env || {};
1681
1715
  for (const [n, i] of Object.entries(s))
1682
- y(this, h, j).call(this, n, i);
1716
+ y(this, h, q).call(this, n, i);
1683
1717
  t = [t[0], "-c", PHP_INI_PATH, ...t.slice(1)];
1684
1718
  for (const n of t)
1685
1719
  this[__private__dont__use].ccall(
@@ -1695,6 +1729,59 @@ class PHP {
1695
1729
  u(this, _).needsRotating = !0;
1696
1730
  });
1697
1731
  }
1732
+ /**
1733
+ * Runs an arbitrary CLI command using the spawn handler associated
1734
+ * with this PHP instance.
1735
+ *
1736
+ * @param argv
1737
+ * @param options
1738
+ * @returns StreamedPHPResponse.
1739
+ */
1740
+ async subProcess(t, e = {}) {
1741
+ const r = this[__private__dont__use].spawnProcess(
1742
+ t[0],
1743
+ t.slice(1),
1744
+ {
1745
+ env: e.env,
1746
+ cwd: e.cwd ?? this.cwd()
1747
+ }
1748
+ ), s = await createInvertedReadableStream();
1749
+ r.on("error", (i) => {
1750
+ s.controller.error(i);
1751
+ }), r.stderr.on("data", (i) => {
1752
+ s.controller.enqueue(i);
1753
+ });
1754
+ const n = await createInvertedReadableStream();
1755
+ return r.stdout.on("data", (i) => {
1756
+ n.controller.enqueue(i);
1757
+ }), r.on("exit", () => {
1758
+ setTimeout(() => {
1759
+ try {
1760
+ s.controller.close();
1761
+ } catch {
1762
+ }
1763
+ try {
1764
+ n.controller.close();
1765
+ } catch {
1766
+ }
1767
+ }, 0);
1768
+ }), new StreamedPHPResponse(
1769
+ // Headers stream
1770
+ new ReadableStream({
1771
+ start(i) {
1772
+ i.close();
1773
+ }
1774
+ }),
1775
+ n.stream,
1776
+ s.stream,
1777
+ // Exit code
1778
+ new Promise((i) => {
1779
+ r.on("exit", (o) => {
1780
+ i(o);
1781
+ });
1782
+ })
1783
+ );
1784
+ }
1698
1785
  setSkipShebang(t) {
1699
1786
  this[__private__dont__use].ccall(
1700
1787
  "wasm_set_skip_shebang",
@@ -1711,13 +1798,13 @@ class PHP {
1711
1798
  this[__private__dont__use]._exit(t);
1712
1799
  } catch {
1713
1800
  }
1714
- w(this, b, !1), w(this, T, null), this[__private__dont__use] && (delete this[__private__dont__use].onMessage, delete this[__private__dont__use]);
1801
+ w(this, H, !1), w(this, T, null), this[__private__dont__use] && (delete this[__private__dont__use].onMessage, delete this[__private__dont__use]);
1715
1802
  }
1716
1803
  [Symbol.dispose]() {
1717
1804
  this.exit(0);
1718
1805
  }
1719
1806
  }
1720
- C = new WeakMap(), b = new WeakMap(), T = new WeakMap(), E = new WeakMap(), F = new WeakMap(), M = new WeakMap(), _ = new WeakMap(), h = new WeakSet(), /**
1807
+ C = new WeakMap(), H = new WeakMap(), T = new WeakMap(), E = new WeakMap(), F = new WeakMap(), M = new WeakMap(), _ = new WeakMap(), h = new WeakSet(), /**
1721
1808
  * Prepares the $_SERVER entries for the PHP runtime.
1722
1809
  *
1723
1810
  * @param defaults Default entries to include in $_SERVER.
@@ -1726,7 +1813,7 @@ C = new WeakMap(), b = new WeakMap(), T = new WeakMap(), E = new WeakMap(), F =
1726
1813
  * was provided.
1727
1814
  * @returns Computed $_SERVER entries.
1728
1815
  */
1729
- z = function(t, e, r) {
1816
+ B = function(t, e, r) {
1730
1817
  const s = {
1731
1818
  ...t || {}
1732
1819
  };
@@ -1832,41 +1919,41 @@ z = function(t, e, r) {
1832
1919
  [STRING, STRING],
1833
1920
  [t, e]
1834
1921
  );
1835
- }, j = function(t, e) {
1922
+ }, q = function(t, e) {
1836
1923
  this[__private__dont__use].ccall(
1837
1924
  "wasm_add_ENV_entry",
1838
1925
  null,
1839
1926
  [STRING, STRING],
1840
1927
  [t, e]
1841
1928
  );
1842
- }, q = async function(t) {
1929
+ }, D = async function(t) {
1843
1930
  u(this, _).enabled && u(this, _).needsRotating && await this.rotateRuntime(), ++u(this, _).requestsMade, u(this, _).requestsMade >= u(this, _).maxRequests && (u(this, _).needsRotating = !0);
1844
1931
  const e = this[__private__dont__use], r = await createInvertedReadableStream();
1845
- e.onHeaders = (d) => {
1846
- a || s || r.controller.enqueue(d.slice());
1932
+ e.onHeaders = (p) => {
1933
+ a || s || r.controller.enqueue(p.slice());
1847
1934
  };
1848
1935
  let s = !1;
1849
1936
  const n = () => {
1850
1937
  s || (s = !0, r.controller.close());
1851
1938
  }, i = await createInvertedReadableStream();
1852
- e.onStdout = (d) => {
1853
- n(), !a && i.controller.enqueue(d.slice());
1939
+ e.onStdout = (p) => {
1940
+ n(), !a && i.controller.enqueue(p.slice());
1854
1941
  };
1855
1942
  const o = await createInvertedReadableStream();
1856
- e.onStderr = (d) => {
1857
- a || o.controller.enqueue(d.slice());
1943
+ e.onStderr = (p) => {
1944
+ a || o.controller.enqueue(p.slice());
1858
1945
  };
1859
1946
  let a = !1, c;
1860
- const p = (async () => {
1861
- var d;
1947
+ const d = (async () => {
1948
+ var p;
1862
1949
  try {
1863
1950
  return await Promise.race([
1864
1951
  t(),
1865
- new Promise((g, H) => {
1866
- var D;
1952
+ new Promise((g, b) => {
1953
+ var j;
1867
1954
  c = (W) => {
1868
- isExitCode(W.error) || H(W.error);
1869
- }, (D = u(this, T)) == null || D.addEventListener(
1955
+ isExitCode(W.error) || b(W.error);
1956
+ }, (j = u(this, T)) == null || j.addEventListener(
1870
1957
  "error",
1871
1958
  c,
1872
1959
  { once: !0 }
@@ -1885,34 +1972,34 @@ z = function(t, e, r) {
1885
1972
  });
1886
1973
  throw this.functionsMaybeMissingFromAsyncify = getFunctionsMaybeMissingFromAsyncify(), f;
1887
1974
  } finally {
1888
- a || (i.controller.close(), o.controller.close(), n(), a = !0), (d = u(this, T)) == null || d.removeEventListener(
1975
+ a || (i.controller.close(), o.controller.close(), n(), a = !0), (p = u(this, T)) == null || p.removeEventListener(
1889
1976
  "error",
1890
1977
  c
1891
1978
  );
1892
1979
  }
1893
1980
  })().then(
1894
- (d) => (d !== 0 && this.dispatchEvent({
1981
+ (p) => (p !== 0 && this.dispatchEvent({
1895
1982
  type: "request.error",
1896
1983
  error: new Error(
1897
- `PHP.run() failed with exit code ${d}.`
1984
+ `PHP.run() failed with exit code ${p}.`
1898
1985
  ),
1899
1986
  // Distinguish between PHP request and PHP-wasm errors
1900
1987
  source: "php-wasm"
1901
- }), d),
1902
- (d) => {
1903
- const f = d.source ?? "php-wasm";
1988
+ }), p),
1989
+ (p) => {
1990
+ const f = p.source ?? "php-wasm";
1904
1991
  throw this.dispatchEvent({
1905
1992
  type: "request.error",
1906
- error: d,
1993
+ error: p,
1907
1994
  source: f
1908
- }), d;
1995
+ }), p;
1909
1996
  }
1910
1997
  );
1911
1998
  return new StreamedPHPResponse(
1912
1999
  r.stream,
1913
2000
  i.stream,
1914
2001
  o.stream,
1915
- p
2002
+ d
1916
2003
  );
1917
2004
  };
1918
2005
  function normalizeHeaders(t) {
@@ -2661,14 +2748,12 @@ function applyRewriteRules(t, e) {
2661
2748
  }
2662
2749
  function rotatePHPRuntime({
2663
2750
  php: t,
2664
- cwd: e,
2665
- recreateRuntime: r,
2666
- maxRequests: s = 400
2751
+ recreateRuntime: e,
2752
+ maxRequests: r = 400
2667
2753
  }) {
2668
2754
  return t.enableRuntimeRotation({
2669
- recreateRuntime: r,
2670
- maxRequests: s,
2671
- cwd: e
2755
+ recreateRuntime: e,
2756
+ maxRequests: r
2672
2757
  });
2673
2758
  }
2674
2759
  async function writeFiles(t, e, r, { rmRoot: s = !1 } = {}) {
@@ -2706,50 +2791,71 @@ function sandboxedSpawnHandlerFactory(t) {
2706
2791
  else if (n === "tput" && e[1] === "cols")
2707
2792
  r.stdout("140"), r.exit(0);
2708
2793
  else if (n === "less") {
2709
- r.on("stdin", (i) => {
2710
- r.stdout(i);
2711
- }), await new Promise((i) => {
2794
+ r.on("stdin", (a) => {
2795
+ r.stdout(a);
2796
+ }), await new Promise((a) => {
2712
2797
  r.childProcess.stdin.on("finish", () => {
2713
- i(!0);
2798
+ a(!0);
2714
2799
  });
2715
2800
  }), r.exit(0);
2716
2801
  return;
2717
- } else if (n === "php") {
2718
- const { php: i, reap: o } = await t.acquirePHPInstance({
2719
- considerPrimary: !1
2720
- });
2721
- i.chdir(s.cwd);
2722
- try {
2723
- const a = await i.cli(e, {
2724
- env: {
2725
- ...s.env,
2726
- SCRIPT_PATH: e[1],
2727
- // Set SHELL_PIPE to 0 to ensure WP-CLI formats
2728
- // the output as ASCII tables.
2729
- // @see https://github.com/wp-cli/wp-cli/issues/1102
2730
- SHELL_PIPE: "0"
2731
- }
2732
- });
2733
- a.stdout.pipeTo(
2734
- new WritableStream({
2735
- write(c) {
2736
- r.stdout(c);
2737
- }
2738
- })
2739
- ), a.stderr.pipeTo(
2740
- new WritableStream({
2741
- write(c) {
2742
- r.stderr(c);
2802
+ }
2803
+ if (!["php", "ls", "pwd"].includes(n ?? "")) {
2804
+ r.exit(127);
2805
+ return;
2806
+ }
2807
+ const { php: i, reap: o } = await t.acquirePHPInstance({
2808
+ considerPrimary: !1
2809
+ });
2810
+ try {
2811
+ s.cwd && i.chdir(s.cwd);
2812
+ const a = i.cwd();
2813
+ switch (n) {
2814
+ case "php": {
2815
+ const c = await i.cli(e, {
2816
+ env: {
2817
+ ...s.env,
2818
+ SCRIPT_PATH: e[1],
2819
+ // Set SHELL_PIPE to 0 to ensure WP-CLI formats
2820
+ // the output as ASCII tables.
2821
+ // @see https://github.com/wp-cli/wp-cli/issues/1102
2822
+ SHELL_PIPE: "0"
2743
2823
  }
2744
- })
2745
- ), r.exit(await a.exitCode);
2746
- } catch (a) {
2747
- throw r.exit(1), a;
2748
- } finally {
2749
- o();
2824
+ });
2825
+ c.stdout.pipeTo(
2826
+ new WritableStream({
2827
+ write(l) {
2828
+ r.stdout(l);
2829
+ }
2830
+ })
2831
+ ), c.stderr.pipeTo(
2832
+ new WritableStream({
2833
+ write(l) {
2834
+ r.stderr(l);
2835
+ }
2836
+ })
2837
+ ), r.exit(await c.exitCode);
2838
+ break;
2839
+ }
2840
+ case "ls": {
2841
+ const c = i.listFiles(e[1] ?? a);
2842
+ for (const l of c)
2843
+ r.stdout(l + `
2844
+ `);
2845
+ await new Promise((l) => setTimeout(l, 10)), r.exit(0);
2846
+ break;
2847
+ }
2848
+ case "pwd": {
2849
+ r.stdout(a + `
2850
+ `), await new Promise((c) => setTimeout(c, 10)), r.exit(0);
2851
+ break;
2852
+ }
2750
2853
  }
2751
- } else
2752
- r.exit(1);
2854
+ } catch (a) {
2855
+ throw r.exit(1), a;
2856
+ } finally {
2857
+ o();
2858
+ }
2753
2859
  });
2754
2860
  }
2755
2861
  function exposeSync(t, e, r, s = ["*"]) {
@@ -2838,10 +2944,10 @@ class NodeSABSyncReceiveMessageTransport {
2838
2944
  ), Atomics.wait(i, 0, 0, 5e3) === "timed-out")
2839
2945
  throw new Error("Timeout waiting for response");
2840
2946
  for (; ; ) {
2841
- const p = NodeSABSyncReceiveMessageTransport.receiveMessageOnPort(e);
2842
- if (((l = p.message) == null ? void 0 : l.id) === o)
2843
- return p.message;
2844
- if (!p)
2947
+ const d = NodeSABSyncReceiveMessageTransport.receiveMessageOnPort(e);
2948
+ if (((l = d.message) == null ? void 0 : l.id) === o)
2949
+ return d.message;
2950
+ if (!d)
2845
2951
  throw new Error("No response received");
2846
2952
  }
2847
2953
  }
@@ -2919,51 +3025,51 @@ function expose(t, e = globalThis, r = ["*"], s) {
2919
3025
  path: [],
2920
3026
  ...i.data
2921
3027
  }, l = (i.data.argumentList || []).map(fromWireValue);
2922
- let p;
3028
+ let d;
2923
3029
  try {
2924
- const d = c.slice(0, -1).reduce((g, H) => g[H], t), f = c.reduce((g, H) => g[H], t);
3030
+ const p = c.slice(0, -1).reduce((g, b) => g[b], t), f = c.reduce((g, b) => g[b], t);
2925
3031
  switch (a) {
2926
3032
  case MessageType.GET:
2927
- p = f;
3033
+ d = f;
2928
3034
  break;
2929
3035
  case MessageType.SET:
2930
- d[c.slice(-1)[0]] = fromWireValue(
3036
+ p[c.slice(-1)[0]] = fromWireValue(
2931
3037
  i.data.value
2932
- ), p = !0;
3038
+ ), d = !0;
2933
3039
  break;
2934
3040
  case MessageType.APPLY:
2935
- p = f.apply(d, l);
3041
+ d = f.apply(p, l);
2936
3042
  break;
2937
3043
  case MessageType.CONSTRUCT:
2938
3044
  {
2939
3045
  const g = new f(...l);
2940
- p = proxy(g);
3046
+ d = proxy(g);
2941
3047
  }
2942
3048
  break;
2943
3049
  case MessageType.ENDPOINT:
2944
3050
  {
2945
- const { port1: g, port2: H } = new MessageChannel();
2946
- expose(t, H), p = transfer(g, [g]);
3051
+ const { port1: g, port2: b } = new MessageChannel();
3052
+ expose(t, b), d = transfer(g, [g]);
2947
3053
  }
2948
3054
  break;
2949
3055
  case MessageType.RELEASE:
2950
- p = void 0;
3056
+ d = void 0;
2951
3057
  break;
2952
3058
  default:
2953
3059
  return;
2954
3060
  }
2955
- } catch (d) {
2956
- p = { value: d, [throwMarker]: 0 };
3061
+ } catch (p) {
3062
+ d = { value: p, [throwMarker]: 0 };
2957
3063
  }
2958
- Promise.resolve(p).catch((d) => ({ value: d, [throwMarker]: 0 })).then((d) => {
2959
- const [f, g] = toWireValue(d);
3064
+ Promise.resolve(d).catch((p) => ({ value: p, [throwMarker]: 0 })).then((p) => {
3065
+ const [f, g] = toWireValue(p);
2960
3066
  e.postMessage({ ...f, id: o }, g), a === MessageType.RELEASE && (e.removeEventListener("message", n), closeEndPoint(e), finalizer in t && typeof t[finalizer] == "function" && t[finalizer]());
2961
3067
  }).catch(() => {
2962
- const [d, f] = toWireValue({
3068
+ const [p, f] = toWireValue({
2963
3069
  value: new TypeError("Unserializable return value"),
2964
3070
  [throwMarker]: 0
2965
3071
  });
2966
- e.postMessage({ ...d, id: o }, f);
3072
+ e.postMessage({ ...p, id: o }, f);
2967
3073
  }).finally(() => {
2968
3074
  s == null || s(i);
2969
3075
  });
@@ -3034,16 +3140,16 @@ function createProxy(t, e, r = [], s = function() {
3034
3140
  },
3035
3141
  set(o, a, c) {
3036
3142
  throwIfProxyReleased(n);
3037
- const [l, p] = toWireValue(c);
3143
+ const [l, d] = toWireValue(c);
3038
3144
  return requestResponseMessage(
3039
3145
  t,
3040
3146
  e,
3041
3147
  {
3042
3148
  type: MessageType.SET,
3043
- path: [...r, a].map((d) => d.toString()),
3149
+ path: [...r, a].map((p) => p.toString()),
3044
3150
  value: l
3045
3151
  },
3046
- p
3152
+ d
3047
3153
  ).then(fromWireValue);
3048
3154
  },
3049
3155
  apply(o, a, c) {
@@ -3055,16 +3161,16 @@ function createProxy(t, e, r = [], s = function() {
3055
3161
  }).then(fromWireValue);
3056
3162
  if (l === "bind")
3057
3163
  return createProxy(t, e, r.slice(0, -1));
3058
- const [p, d] = processArguments(c);
3164
+ const [d, p] = processArguments(c);
3059
3165
  return requestResponseMessage(
3060
3166
  t,
3061
3167
  e,
3062
3168
  {
3063
3169
  type: MessageType.APPLY,
3064
3170
  path: r.map((f) => f.toString()),
3065
- argumentList: p
3171
+ argumentList: d
3066
3172
  },
3067
- d
3173
+ p
3068
3174
  ).then(fromWireValue);
3069
3175
  },
3070
3176
  construct(o, a) {
@@ -3075,7 +3181,7 @@ function createProxy(t, e, r = [], s = function() {
3075
3181
  e,
3076
3182
  {
3077
3183
  type: MessageType.CONSTRUCT,
3078
- path: r.map((p) => p.toString()),
3184
+ path: r.map((d) => d.toString()),
3079
3185
  argumentList: c
3080
3186
  },
3081
3187
  l
@@ -3246,19 +3352,19 @@ const errorProperties = [
3246
3352
  useToJSON: o,
3247
3353
  serialize: a
3248
3354
  });
3249
- for (const [l, p] of Object.entries(t)) {
3250
- if (p && p instanceof Uint8Array && p.constructor.name === "Buffer") {
3355
+ for (const [l, d] of Object.entries(t)) {
3356
+ if (d && d instanceof Uint8Array && d.constructor.name === "Buffer") {
3251
3357
  r[l] = "[object Buffer]";
3252
3358
  continue;
3253
3359
  }
3254
- if (p !== null && typeof p == "object" && typeof p.pipe == "function") {
3360
+ if (d !== null && typeof d == "object" && typeof d.pipe == "function") {
3255
3361
  r[l] = "[object Stream]";
3256
3362
  continue;
3257
3363
  }
3258
- if (typeof p != "function") {
3259
- if (!p || typeof p != "object") {
3364
+ if (typeof d != "function") {
3365
+ if (!d || typeof d != "object") {
3260
3366
  try {
3261
- r[l] = p;
3367
+ r[l] = d;
3262
3368
  } catch {
3263
3369
  }
3264
3370
  continue;
@@ -3271,10 +3377,10 @@ const errorProperties = [
3271
3377
  }
3272
3378
  }
3273
3379
  if (a || r instanceof Error)
3274
- for (const { property: l, enumerable: p } of errorProperties)
3380
+ for (const { property: l, enumerable: d } of errorProperties)
3275
3381
  t[l] !== void 0 && t[l] !== null && Object.defineProperty(r, l, {
3276
3382
  value: isErrorLike(t[l]) || Array.isArray(t[l]) ? c(t[l]) : t[l],
3277
- enumerable: s ? !0 : p,
3383
+ enumerable: s ? !0 : d,
3278
3384
  configurable: !0,
3279
3385
  writable: !0
3280
3386
  });
@@ -3403,7 +3509,198 @@ function setupTransferHandlers() {
3403
3509
  t.serialize = ({ value: r }) => {
3404
3510
  const s = e({ value: r });
3405
3511
  return r.response && (s[0].value.response = r.response), r.source && (s[0].value.source = r.source), s;
3406
- };
3512
+ }, transferHandlers.set("StreamedPHPResponse", {
3513
+ canHandle: (r) => r instanceof StreamedPHPResponse,
3514
+ serialize(r) {
3515
+ const s = supportsTransferableStreams(), n = promiseToPort(r.exitCode);
3516
+ if (s)
3517
+ return [{
3518
+ __type: "StreamedPHPResponse",
3519
+ headers: r.headersStream,
3520
+ stdout: r.stdout,
3521
+ stderr: r.stderr,
3522
+ exitCodePort: n
3523
+ }, [n]];
3524
+ const i = streamToPort(r.headersStream), o = streamToPort(r.stdout), a = streamToPort(r.stderr);
3525
+ return [
3526
+ {
3527
+ __type: "StreamedPHPResponse",
3528
+ headersPort: i,
3529
+ stdoutPort: o,
3530
+ stderrPort: a,
3531
+ exitCodePort: n
3532
+ },
3533
+ [i, o, a, n]
3534
+ ];
3535
+ },
3536
+ deserialize(r) {
3537
+ if (r.headers && r.stdout && r.stderr) {
3538
+ const a = portToPromise(
3539
+ r.exitCodePort
3540
+ );
3541
+ return new StreamedPHPResponse(
3542
+ r.headers,
3543
+ r.stdout,
3544
+ r.stderr,
3545
+ a
3546
+ );
3547
+ }
3548
+ const s = portToStream(r.headersPort), n = portToStream(r.stdoutPort), i = portToStream(r.stderrPort), o = portToPromise(r.exitCodePort);
3549
+ return new StreamedPHPResponse(s, n, i, o);
3550
+ }
3551
+ });
3552
+ }
3553
+ function supportsTransferableStreams() {
3554
+ try {
3555
+ if (typeof ReadableStream > "u") return !1;
3556
+ const { port1: t } = new MessageChannel(), e = new ReadableStream();
3557
+ t.postMessage(e);
3558
+ try {
3559
+ t.close();
3560
+ } catch {
3561
+ }
3562
+ return !0;
3563
+ } catch {
3564
+ return !1;
3565
+ }
3566
+ }
3567
+ function streamToPort(t) {
3568
+ const { port1: e, port2: r } = new MessageChannel();
3569
+ return (async () => {
3570
+ const s = t.getReader();
3571
+ try {
3572
+ for (; ; ) {
3573
+ const { done: n, value: i } = await s.read();
3574
+ if (n) {
3575
+ try {
3576
+ e.postMessage({ t: "close" });
3577
+ } catch {
3578
+ }
3579
+ try {
3580
+ e.close();
3581
+ } catch {
3582
+ }
3583
+ break;
3584
+ }
3585
+ if (i) {
3586
+ const o = i.byteOffset === 0 && i.byteLength === i.buffer.byteLength ? i : i.slice(), a = o.buffer;
3587
+ try {
3588
+ e.postMessage({ t: "chunk", b: a }, [
3589
+ a
3590
+ ]);
3591
+ } catch {
3592
+ e.postMessage({
3593
+ t: "chunk",
3594
+ b: o.buffer.slice(0)
3595
+ });
3596
+ }
3597
+ }
3598
+ }
3599
+ } catch (n) {
3600
+ try {
3601
+ e.postMessage({ t: "error", m: (n == null ? void 0 : n.message) || String(n) });
3602
+ } catch {
3603
+ }
3604
+ } finally {
3605
+ try {
3606
+ e.close();
3607
+ } catch {
3608
+ }
3609
+ }
3610
+ })(), r;
3611
+ }
3612
+ function portToStream(t) {
3613
+ return new ReadableStream({
3614
+ start(e) {
3615
+ const r = (n) => {
3616
+ const i = n.data;
3617
+ if (i)
3618
+ switch (i.t) {
3619
+ case "chunk":
3620
+ e.enqueue(new Uint8Array(i.b));
3621
+ break;
3622
+ case "close":
3623
+ e.close(), s();
3624
+ break;
3625
+ case "error":
3626
+ e.error(new Error(i.m || "Stream error")), s();
3627
+ break;
3628
+ }
3629
+ }, s = () => {
3630
+ var n;
3631
+ try {
3632
+ (n = t.removeEventListener) == null || n.call(t, "message", r);
3633
+ } catch {
3634
+ }
3635
+ try {
3636
+ t.onmessage = null;
3637
+ } catch {
3638
+ }
3639
+ try {
3640
+ t.close();
3641
+ } catch {
3642
+ }
3643
+ };
3644
+ t.addEventListener ? t.addEventListener("message", r) : t.on ? t.on(
3645
+ "message",
3646
+ (n) => r({ data: n })
3647
+ ) : t.onmessage = r, typeof t.start == "function" && t.start();
3648
+ },
3649
+ cancel() {
3650
+ try {
3651
+ t.close();
3652
+ } catch {
3653
+ }
3654
+ }
3655
+ });
3656
+ }
3657
+ function promiseToPort(t) {
3658
+ const { port1: e, port2: r } = new MessageChannel();
3659
+ return t.then((s) => {
3660
+ try {
3661
+ e.postMessage({ t: "resolve", v: s });
3662
+ } catch {
3663
+ }
3664
+ }).catch((s) => {
3665
+ try {
3666
+ e.postMessage({
3667
+ t: "reject",
3668
+ m: (s == null ? void 0 : s.message) || String(s)
3669
+ });
3670
+ } catch {
3671
+ }
3672
+ }).finally(() => {
3673
+ try {
3674
+ e.close();
3675
+ } catch {
3676
+ }
3677
+ }), r;
3678
+ }
3679
+ function portToPromise(t) {
3680
+ return new Promise((e, r) => {
3681
+ const s = (i) => {
3682
+ const o = i.data;
3683
+ o && (o.t === "resolve" ? (n(), e(o.v)) : o.t === "reject" && (n(), r(new Error(o.m || ""))));
3684
+ }, n = () => {
3685
+ var i;
3686
+ try {
3687
+ (i = t.removeEventListener) == null || i.call(t, "message", s);
3688
+ } catch {
3689
+ }
3690
+ try {
3691
+ t.onmessage = null;
3692
+ } catch {
3693
+ }
3694
+ try {
3695
+ t.close();
3696
+ } catch {
3697
+ }
3698
+ };
3699
+ t.addEventListener ? t.addEventListener("message", s) : t.on ? t.on(
3700
+ "message",
3701
+ (i) => s({ data: i })
3702
+ ) : t.onmessage = s, typeof t.start == "function" && t.start();
3703
+ });
3407
3704
  }
3408
3705
  const throwTransferHandler = transferHandlers.get(
3409
3706
  "throw"