@php-wasm/web 0.6.15 → 0.7.0

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.
Files changed (39) hide show
  1. package/index.d.ts +100 -201
  2. package/index.js +395 -325
  3. package/kitchen-sink/7_0_33/php_7_0.wasm +0 -0
  4. package/kitchen-sink/7_1_30/php_7_1.wasm +0 -0
  5. package/kitchen-sink/7_2_34/php_7_2.wasm +0 -0
  6. package/kitchen-sink/7_3_33/php_7_3.wasm +0 -0
  7. package/kitchen-sink/7_4_33/php_7_4.wasm +0 -0
  8. package/kitchen-sink/8_0_30/php_8_0.wasm +0 -0
  9. package/kitchen-sink/8_1_23/php_8_1.wasm +0 -0
  10. package/kitchen-sink/8_2_10/php_8_2.wasm +0 -0
  11. package/kitchen-sink/8_3_0/php_8_3.wasm +0 -0
  12. package/kitchen-sink/php_7_0.js +1 -1
  13. package/kitchen-sink/php_7_1.js +1 -1
  14. package/kitchen-sink/php_7_2.js +1 -1
  15. package/kitchen-sink/php_7_3.js +1 -1
  16. package/kitchen-sink/php_7_4.js +1 -1
  17. package/kitchen-sink/php_8_0.js +1 -1
  18. package/kitchen-sink/php_8_1.js +1 -1
  19. package/kitchen-sink/php_8_2.js +1 -1
  20. package/kitchen-sink/php_8_3.js +1 -1
  21. package/light/7_0_33/php_7_0.wasm +0 -0
  22. package/light/7_1_30/php_7_1.wasm +0 -0
  23. package/light/7_2_34/php_7_2.wasm +0 -0
  24. package/light/7_3_33/php_7_3.wasm +0 -0
  25. package/light/7_4_33/php_7_4.wasm +0 -0
  26. package/light/8_0_30/php_8_0.wasm +0 -0
  27. package/light/8_1_23/php_8_1.wasm +0 -0
  28. package/light/8_2_10/php_8_2.wasm +0 -0
  29. package/light/8_3_0/php_8_3.wasm +0 -0
  30. package/light/php_7_0.js +1 -1
  31. package/light/php_7_1.js +1 -1
  32. package/light/php_7_2.js +1 -1
  33. package/light/php_7_3.js +1 -1
  34. package/light/php_7_4.js +1 -1
  35. package/light/php_8_0.js +1 -1
  36. package/light/php_8_1.js +1 -1
  37. package/light/php_8_2.js +1 -1
  38. package/light/php_8_3.js +1 -1
  39. package/package.json +6 -6
package/index.js CHANGED
@@ -1,13 +1,13 @@
1
- var K = (e, t, r) => {
1
+ var Z = (e, t, r) => {
2
2
  if (!t.has(e))
3
3
  throw TypeError("Cannot " + r);
4
4
  };
5
- var c = (e, t, r) => (K(e, t, "read from private field"), r ? r.call(e) : t.get(e)), d = (e, t, r) => {
5
+ var u = (e, t, r) => (Z(e, t, "read from private field"), r ? r.call(e) : t.get(e)), d = (e, t, r) => {
6
6
  if (t.has(e))
7
7
  throw TypeError("Cannot add the same private member more than once");
8
8
  t instanceof WeakSet ? t.add(e) : t.set(e, r);
9
- }, p = (e, t, r, s) => (K(e, t, "write to private field"), s ? s.call(e, r) : t.set(e, r), r);
10
- var f = (e, t, r) => (K(e, t, "access private method"), r);
9
+ }, p = (e, t, r, s) => (Z(e, t, "write to private field"), s ? s.call(e, r) : t.set(e, r), r);
10
+ var f = (e, t, r) => (Z(e, t, "access private method"), r);
11
11
  const currentJsRuntime$1 = function() {
12
12
  var e;
13
13
  return typeof process < "u" && ((e = process.release) == null ? void 0 : e.name) === "node" ? "NODE" : typeof window < "u" ? "WEB" : (
@@ -59,10 +59,10 @@ if (currentJsRuntime$1 === "NODE") {
59
59
  const o = n.byobRequest.view, a = await s.slice(
60
60
  r,
61
61
  r + o.byteLength
62
- ).arrayBuffer(), l = new Uint8Array(a);
63
- new Uint8Array(o.buffer).set(l);
64
- const u = l.byteLength;
65
- n.byobRequest.respond(u), r += u, r >= s.size && n.close();
62
+ ).arrayBuffer(), c = new Uint8Array(a);
63
+ new Uint8Array(o.buffer).set(c);
64
+ const l = c.byteLength;
65
+ n.byobRequest.respond(l), r += l, r >= s.size && n.close();
66
66
  }
67
67
  });
68
68
  });
@@ -226,18 +226,40 @@ function extractPHPFunctionsFromStack(e) {
226
226
  return [];
227
227
  }
228
228
  }
229
+ const SleepFinished = Symbol("SleepFinished");
230
+ function sleep(e) {
231
+ return new Promise((t) => {
232
+ setTimeout(() => t(SleepFinished), e);
233
+ });
234
+ }
235
+ class AcquireTimeoutError extends Error {
236
+ constructor() {
237
+ super("Acquiring lock timed out");
238
+ }
239
+ }
229
240
  class Semaphore {
230
- constructor({ concurrency: t }) {
231
- this._running = 0, this.concurrency = t, this.queue = [];
241
+ constructor({ concurrency: t, timeout: r }) {
242
+ this._running = 0, this.concurrency = t, this.timeout = r, this.queue = [];
243
+ }
244
+ get remaining() {
245
+ return this.concurrency - this.running;
232
246
  }
233
247
  get running() {
234
248
  return this._running;
235
249
  }
236
250
  async acquire() {
237
251
  for (; ; )
238
- if (this._running >= this.concurrency)
239
- await new Promise((t) => this.queue.push(t));
240
- else {
252
+ if (this._running >= this.concurrency) {
253
+ const t = new Promise((r) => {
254
+ this.queue.push(r);
255
+ });
256
+ this.timeout !== void 0 ? await Promise.race([t, sleep(this.timeout)]).then(
257
+ (r) => {
258
+ if (r === SleepFinished)
259
+ throw new AcquireTimeoutError();
260
+ }
261
+ ) : await t;
262
+ } else {
241
263
  this._running++;
242
264
  let t = !1;
243
265
  return () => {
@@ -287,8 +309,8 @@ function splitShellCommand(e) {
287
309
  const o = [];
288
310
  let i = "";
289
311
  for (let a = 0; a < e.length; a++) {
290
- const l = e[a];
291
- l === "\\" ? ((e[a + 1] === '"' || e[a + 1] === "'") && a++, i += e[a]) : s === 0 ? l === '"' || l === "'" ? (s = 1, n = l) : l.match(/\s/) ? (i.trim().length && o.push(i.trim()), i = l) : o.length && !i ? i = o.pop() + l : i += l : s === 1 && (l === n ? (s = 0, n = "") : i += l);
312
+ const c = e[a];
313
+ c === "\\" ? ((e[a + 1] === '"' || e[a + 1] === "'") && a++, i += e[a]) : s === 0 ? c === '"' || c === "'" ? (s = 1, n = c) : c.match(/\s/) ? (i.trim().length && o.push(i.trim()), i = c) : o.length && !i ? i = o.pop() + c : i += c : s === 1 && (c === n ? (s = 0, n = "") : i += c);
292
314
  }
293
315
  return i && o.push(i.trim()), o;
294
316
  }
@@ -421,96 +443,7 @@ const SupportedPHPVersions = [
421
443
  "7.2",
422
444
  "7.1",
423
445
  "7.0"
424
- ], LatestSupportedPHPVersion = SupportedPHPVersions[0];
425
- var R, T;
426
- class PHPBrowser {
427
- /**
428
- * @param server - The PHP server to browse.
429
- * @param config - The browser configuration.
430
- */
431
- constructor(t, r = {}) {
432
- d(this, R, void 0);
433
- d(this, T, void 0);
434
- this.requestHandler = t, p(this, R, {}), p(this, T, {
435
- handleRedirects: !1,
436
- maxRedirects: 4,
437
- ...r
438
- });
439
- }
440
- /**
441
- * Sends the request to the server.
442
- *
443
- * When cookies are present in the response, this method stores
444
- * them and sends them with any subsequent requests.
445
- *
446
- * When a redirection is present in the response, this method
447
- * follows it by discarding a response and sending a subsequent
448
- * request.
449
- *
450
- * @param request - The request.
451
- * @param redirects - Internal. The number of redirects handled so far.
452
- * @returns PHPRequestHandler response.
453
- */
454
- async request(t, r = 0) {
455
- const s = await this.requestHandler.request({
456
- ...t,
457
- headers: {
458
- ...t.headers,
459
- cookie: this.serializeCookies()
460
- }
461
- });
462
- if (s.headers["set-cookie"] && this.setCookies(s.headers["set-cookie"]), c(this, T).handleRedirects && s.headers.location && r < c(this, T).maxRedirects) {
463
- const n = new URL(
464
- s.headers.location[0],
465
- this.requestHandler.absoluteUrl
466
- );
467
- return this.request(
468
- {
469
- url: n.toString(),
470
- method: "GET",
471
- headers: {}
472
- },
473
- r + 1
474
- );
475
- }
476
- return s;
477
- }
478
- /** @inheritDoc */
479
- pathToInternalUrl(t) {
480
- return this.requestHandler.pathToInternalUrl(t);
481
- }
482
- /** @inheritDoc */
483
- internalUrlToPath(t) {
484
- return this.requestHandler.internalUrlToPath(t);
485
- }
486
- /** @inheritDoc */
487
- get absoluteUrl() {
488
- return this.requestHandler.absoluteUrl;
489
- }
490
- /** @inheritDoc */
491
- get documentRoot() {
492
- return this.requestHandler.documentRoot;
493
- }
494
- setCookies(t) {
495
- for (const r of t)
496
- try {
497
- if (!r.includes("="))
498
- continue;
499
- const s = r.indexOf("="), n = r.substring(0, s), o = r.substring(s + 1).split(";")[0];
500
- c(this, R)[n] = o;
501
- } catch (s) {
502
- console.error(s);
503
- }
504
- }
505
- serializeCookies() {
506
- const t = [];
507
- for (const r in c(this, R))
508
- t.push(`${r}=${c(this, R)[r]}`);
509
- return t.join("; ");
510
- }
511
- }
512
- R = new WeakMap(), T = new WeakMap();
513
- const DEFAULT_BASE_URL = "http://example.com";
446
+ ], LatestSupportedPHPVersion = SupportedPHPVersions[0], DEFAULT_BASE_URL = "http://example.com";
514
447
  function toRelativeUrl(e) {
515
448
  return e.toString().substring(e.origin.length);
516
449
  }
@@ -522,22 +455,22 @@ function ensurePathPrefix(e, t) {
522
455
  }
523
456
  async function encodeAsMultipart(e) {
524
457
  const t = `----${Math.random().toString(36).slice(2)}`, r = `multipart/form-data; boundary=${t}`, s = new TextEncoder(), n = [];
525
- for (const [l, u] of Object.entries(e))
458
+ for (const [c, l] of Object.entries(e))
526
459
  n.push(`--${t}\r
527
- `), n.push(`Content-Disposition: form-data; name="${l}"`), u instanceof File && n.push(`; filename="${u.name}"`), n.push(`\r
528
- `), u instanceof File && (n.push("Content-Type: application/octet-stream"), n.push(`\r
460
+ `), n.push(`Content-Disposition: form-data; name="${c}"`), l instanceof File && n.push(`; filename="${l.name}"`), n.push(`\r
461
+ `), l instanceof File && (n.push("Content-Type: application/octet-stream"), n.push(`\r
529
462
  `)), n.push(`\r
530
- `), u instanceof File ? n.push(await fileToUint8Array(u)) : n.push(u), n.push(`\r
463
+ `), l instanceof File ? n.push(await fileToUint8Array(l)) : n.push(l), n.push(`\r
531
464
  `);
532
465
  n.push(`--${t}--\r
533
466
  `);
534
- const o = n.reduce((l, u) => l + u.length, 0), i = new Uint8Array(o);
467
+ const o = n.reduce((c, l) => c + l.length, 0), i = new Uint8Array(o);
535
468
  let a = 0;
536
- for (const l of n)
469
+ for (const c of n)
537
470
  i.set(
538
- typeof l == "string" ? s.encode(l) : l,
471
+ typeof c == "string" ? s.encode(c) : c,
539
472
  a
540
- ), a += l.length;
473
+ ), a += c.length;
541
474
  return { bytes: i, contentType: r };
542
475
  }
543
476
  function fileToUint8Array(e) {
@@ -548,7 +481,30 @@ function fileToUint8Array(e) {
548
481
  }, r.readAsArrayBuffer(e);
549
482
  });
550
483
  }
551
- var g, H, N, b, k, y, C, S, L, Z, O, X, U, ee;
484
+ class HttpCookieStore {
485
+ constructor() {
486
+ this.cookies = {};
487
+ }
488
+ rememberCookiesFromResponseHeaders(t) {
489
+ if (t != null && t["set-cookie"])
490
+ for (const r of t["set-cookie"])
491
+ try {
492
+ if (!r.includes("="))
493
+ continue;
494
+ const s = r.indexOf("="), n = r.substring(0, s), o = r.substring(s + 1).split(";")[0];
495
+ this.cookies[n] = o;
496
+ } catch (s) {
497
+ console.error(s);
498
+ }
499
+ }
500
+ getCookieRequestHeader() {
501
+ const t = [];
502
+ for (const r in this.cookies)
503
+ t.push(`${r}=${this.cookies[r]}`);
504
+ return t.join("; ");
505
+ }
506
+ }
507
+ var g, x, M, v, S, y, T, R, k, I, X, N, ee, L, te;
552
508
  class PHPRequestHandler {
553
509
  /**
554
510
  * @param php - The PHP instance.
@@ -561,7 +517,7 @@ class PHPRequestHandler {
561
517
  * @param fsPath - Absolute path of the static file to serve.
562
518
  * @returns The response.
563
519
  */
564
- d(this, L);
520
+ d(this, I);
565
521
  /**
566
522
  * Runs the requested PHP file with all the request and $_SERVER
567
523
  * superglobals populated.
@@ -569,7 +525,7 @@ class PHPRequestHandler {
569
525
  * @param request - The request.
570
526
  * @returns The response.
571
527
  */
572
- d(this, O);
528
+ d(this, N);
573
529
  /**
574
530
  * Resolve the requested path to the filesystem path of the requested PHP file.
575
531
  *
@@ -579,55 +535,120 @@ class PHPRequestHandler {
579
535
  * @throws {Error} If the requested path doesn't exist.
580
536
  * @returns The resolved filesystem path.
581
537
  */
582
- d(this, U);
538
+ d(this, L);
583
539
  d(this, g, void 0);
584
- d(this, H, void 0);
585
- d(this, N, void 0);
586
- d(this, b, void 0);
587
- d(this, k, void 0);
588
- d(this, y, void 0);
589
- d(this, C, void 0);
540
+ d(this, x, void 0);
541
+ d(this, M, void 0);
542
+ d(this, v, void 0);
590
543
  d(this, S, void 0);
591
- p(this, S, new Semaphore({ concurrency: 1 }));
544
+ d(this, y, void 0);
545
+ d(this, T, void 0);
546
+ d(this, R, void 0);
547
+ d(this, k, void 0);
548
+ p(this, R, new Semaphore({ concurrency: 1 }));
592
549
  const {
593
550
  documentRoot: s = "/www/",
594
551
  absoluteUrl: n = typeof location == "object" ? location == null ? void 0 : location.href : "",
595
552
  rewriteRules: o = []
596
553
  } = r;
597
- this.php = t, p(this, g, s);
554
+ this.php = t, p(this, k, new HttpCookieStore()), p(this, g, s);
598
555
  const i = new URL(n);
599
- p(this, N, i.hostname), p(this, b, i.port ? Number(i.port) : i.protocol === "https:" ? 443 : 80), p(this, H, (i.protocol || "").replace(":", ""));
600
- const a = c(this, b) !== 443 && c(this, b) !== 80;
601
- p(this, k, [
602
- c(this, N),
603
- a ? `:${c(this, b)}` : ""
604
- ].join("")), p(this, y, i.pathname.replace(/\/+$/, "")), p(this, C, [
605
- `${c(this, H)}://`,
606
- c(this, k),
607
- c(this, y)
556
+ p(this, M, i.hostname), p(this, v, i.port ? Number(i.port) : i.protocol === "https:" ? 443 : 80), p(this, x, (i.protocol || "").replace(":", ""));
557
+ const a = u(this, v) !== 443 && u(this, v) !== 80;
558
+ p(this, S, [
559
+ u(this, M),
560
+ a ? `:${u(this, v)}` : ""
561
+ ].join("")), p(this, y, i.pathname.replace(/\/+$/, "")), p(this, T, [
562
+ `${u(this, x)}://`,
563
+ u(this, S),
564
+ u(this, y)
608
565
  ].join("")), this.rewriteRules = o;
609
566
  }
610
- /** @inheritDoc */
567
+ /**
568
+ * Converts a path to an absolute URL based at the PHPRequestHandler
569
+ * root.
570
+ *
571
+ * @param path The server path to convert to an absolute URL.
572
+ * @returns The absolute URL.
573
+ */
611
574
  pathToInternalUrl(t) {
612
575
  return `${this.absoluteUrl}${t}`;
613
576
  }
614
- /** @inheritDoc */
577
+ /**
578
+ * Converts an absolute URL based at the PHPRequestHandler to a relative path
579
+ * without the server pathname and scope.
580
+ *
581
+ * @param internalUrl An absolute URL based at the PHPRequestHandler root.
582
+ * @returns The relative path.
583
+ */
615
584
  internalUrlToPath(t) {
616
585
  const r = new URL(t);
617
- return r.pathname.startsWith(c(this, y)) && (r.pathname = r.pathname.slice(c(this, y).length)), toRelativeUrl(r);
586
+ return r.pathname.startsWith(u(this, y)) && (r.pathname = r.pathname.slice(u(this, y).length)), toRelativeUrl(r);
618
587
  }
619
588
  get isRequestRunning() {
620
- return c(this, S).running > 0;
589
+ return u(this, R).running > 0;
621
590
  }
622
- /** @inheritDoc */
591
+ /**
592
+ * The absolute URL of this PHPRequestHandler instance.
593
+ */
623
594
  get absoluteUrl() {
624
- return c(this, C);
595
+ return u(this, T);
625
596
  }
626
- /** @inheritDoc */
597
+ /**
598
+ * The directory in the PHP filesystem where the server will look
599
+ * for the files to serve. Default: `/var/www`.
600
+ */
627
601
  get documentRoot() {
628
- return c(this, g);
602
+ return u(this, g);
629
603
  }
630
- /** @inheritDoc */
604
+ /**
605
+ * Serves the request – either by serving a static file, or by
606
+ * dispatching it to the PHP runtime.
607
+ *
608
+ * The request() method mode behaves like a web server and only works if
609
+ * the PHP was initialized with a `requestHandler` option (which the online version
610
+ * of WordPress Playground does by default).
611
+ *
612
+ * In the request mode, you pass an object containing the request information
613
+ * (method, headers, body, etc.) and the path to the PHP file to run:
614
+ *
615
+ * ```ts
616
+ * const php = PHP.load('7.4', {
617
+ * requestHandler: {
618
+ * documentRoot: "/www"
619
+ * }
620
+ * })
621
+ * php.writeFile("/www/index.php", `<?php echo file_get_contents("php://input");`);
622
+ * const result = await php.request({
623
+ * method: "GET",
624
+ * headers: {
625
+ * "Content-Type": "text/plain"
626
+ * },
627
+ * body: "Hello world!",
628
+ * path: "/www/index.php"
629
+ * });
630
+ * // result.text === "Hello world!"
631
+ * ```
632
+ *
633
+ * The `request()` method cannot be used in conjunction with `cli()`.
634
+ *
635
+ * @example
636
+ * ```js
637
+ * const output = await php.request({
638
+ * method: 'GET',
639
+ * url: '/index.php',
640
+ * headers: {
641
+ * 'X-foo': 'bar',
642
+ * },
643
+ * body: {
644
+ * foo: 'bar',
645
+ * },
646
+ * });
647
+ * console.log(output.stdout); // "Hello world!"
648
+ * ```
649
+ *
650
+ * @param request - PHP Request data.
651
+ */
631
652
  async request(t) {
632
653
  const r = t.url.startsWith("http://") || t.url.startsWith("https://"), s = new URL(
633
654
  // Remove the hash part of the URL as it's not meant for the server.
@@ -636,14 +657,14 @@ class PHPRequestHandler {
636
657
  ), n = applyRewriteRules(
637
658
  removePathPrefix(
638
659
  decodeURIComponent(s.pathname),
639
- c(this, y)
660
+ u(this, y)
640
661
  ),
641
662
  this.rewriteRules
642
- ), o = joinPaths(c(this, g), n);
643
- return seemsLikeAPHPRequestHandlerPath(o) ? await f(this, O, X).call(this, t, s) : f(this, L, Z).call(this, o);
663
+ ), o = joinPaths(u(this, g), n);
664
+ return seemsLikeAPHPRequestHandlerPath(o) ? await f(this, N, ee).call(this, t, s) : f(this, I, X).call(this, o);
644
665
  }
645
666
  }
646
- g = new WeakMap(), H = new WeakMap(), N = new WeakMap(), b = new WeakMap(), k = new WeakMap(), y = new WeakMap(), C = new WeakMap(), S = new WeakMap(), L = new WeakSet(), Z = function(t) {
667
+ g = new WeakMap(), x = new WeakMap(), M = new WeakMap(), v = new WeakMap(), S = new WeakMap(), y = new WeakMap(), T = new WeakMap(), R = new WeakMap(), k = new WeakMap(), I = new WeakSet(), X = function(t) {
647
668
  if (!this.php.fileExists(t))
648
669
  return new PHPResponse(
649
670
  404,
@@ -668,9 +689,9 @@ g = new WeakMap(), H = new WeakMap(), N = new WeakMap(), b = new WeakMap(), k =
668
689
  },
669
690
  r
670
691
  );
671
- }, O = new WeakSet(), X = async function(t, r) {
692
+ }, N = new WeakSet(), ee = async function(t, r) {
672
693
  var n;
673
- if (c(this, S).running > 0 && ((n = t.headers) == null ? void 0 : n["x-request-issuer"]) === "php")
694
+ if (u(this, R).running > 0 && ((n = t.headers) == null ? void 0 : n["x-request-issuer"]) === "php")
674
695
  return console.warn(
675
696
  "Possible deadlock: Called request() before the previous request() have finished. PHP likely issued an HTTP call to itself. Normally this would lead to infinite waiting as Request 1 holds the lock that the Request 2 is waiting to acquire. That's not useful, so PHPRequestHandler will return error 502 instead."
676
697
  ), new PHPResponse(
@@ -678,26 +699,23 @@ g = new WeakMap(), H = new WeakMap(), N = new WeakMap(), b = new WeakMap(), k =
678
699
  {},
679
700
  new TextEncoder().encode("502 Bad Gateway")
680
701
  );
681
- const s = await c(this, S).acquire();
702
+ const s = await u(this, R).acquire();
682
703
  try {
683
- this.php.addServerGlobalEntry("REMOTE_ADDR", "127.0.0.1"), this.php.addServerGlobalEntry("DOCUMENT_ROOT", c(this, g)), this.php.addServerGlobalEntry(
684
- "HTTPS",
685
- c(this, C).startsWith("https://") ? "on" : ""
686
- );
687
704
  let o = "GET";
688
705
  const i = {
689
- host: c(this, k),
690
- ...normalizeHeaders(t.headers || {})
706
+ host: u(this, S),
707
+ ...normalizeHeaders(t.headers || {}),
708
+ cookie: u(this, k).getCookieRequestHeader()
691
709
  };
692
710
  let a = t.body;
693
711
  if (typeof a == "object" && !(a instanceof Uint8Array)) {
694
712
  o = "POST";
695
- const { bytes: u, contentType: h } = await encodeAsMultipart(a);
696
- a = u, i["content-type"] = h;
713
+ const { bytes: l, contentType: h } = await encodeAsMultipart(a);
714
+ a = l, i["content-type"] = h;
697
715
  }
698
- let l;
716
+ let c;
699
717
  try {
700
- l = f(this, U, ee).call(this, decodeURIComponent(r.pathname));
718
+ c = f(this, L, te).call(this, decodeURIComponent(r.pathname));
701
719
  } catch {
702
720
  return new PHPResponse(
703
721
  404,
@@ -705,24 +723,39 @@ g = new WeakMap(), H = new WeakMap(), N = new WeakMap(), b = new WeakMap(), k =
705
723
  new TextEncoder().encode("404 File not found")
706
724
  );
707
725
  }
708
- return await this.php.run({
709
- relativeUri: ensurePathPrefix(
710
- toRelativeUrl(r),
711
- c(this, y)
712
- ),
713
- protocol: c(this, H),
714
- method: t.method || o,
715
- body: a,
716
- scriptPath: l,
717
- headers: i
718
- });
726
+ try {
727
+ const l = await this.php.run({
728
+ relativeUri: ensurePathPrefix(
729
+ toRelativeUrl(r),
730
+ u(this, y)
731
+ ),
732
+ protocol: u(this, x),
733
+ method: t.method || o,
734
+ $_SERVER: {
735
+ REMOTE_ADDR: "127.0.0.1",
736
+ DOCUMENT_ROOT: u(this, g),
737
+ HTTPS: u(this, T).startsWith("https://") ? "on" : ""
738
+ },
739
+ body: a,
740
+ scriptPath: c,
741
+ headers: i
742
+ });
743
+ return u(this, k).rememberCookiesFromResponseHeaders(
744
+ l.headers
745
+ ), l;
746
+ } catch (l) {
747
+ const h = l;
748
+ if (h != null && h.response)
749
+ return h.response;
750
+ throw l;
751
+ }
719
752
  } finally {
720
753
  s();
721
754
  }
722
- }, U = new WeakSet(), ee = function(t) {
723
- let r = removePathPrefix(t, c(this, y));
724
- r = applyRewriteRules(r, this.rewriteRules), r.includes(".php") ? r = r.split(".php")[0] + ".php" : this.php.isDir(`${c(this, g)}${r}`) ? (r.endsWith("/") || (r = `${r}/`), r = `${r}index.php`) : r = "/index.php";
725
- const s = `${c(this, g)}${r}`;
755
+ }, L = new WeakSet(), te = function(t) {
756
+ let r = removePathPrefix(t, u(this, y));
757
+ r = applyRewriteRules(r, this.rewriteRules), r.includes(".php") ? r = r.split(".php")[0] + ".php" : this.php.isDir(`${u(this, g)}${r}`) ? (r.endsWith("/") || (r = `${r}/`), r = `${r}index.php`) : r = "/index.php";
758
+ const s = `${u(this, g)}${r}`;
726
759
  if (this.php.fileExists(s))
727
760
  return s;
728
761
  throw new Error(`File not found: ${s}`);
@@ -763,6 +796,38 @@ function inferMimeType(e) {
763
796
  case "txt":
764
797
  case "md":
765
798
  return "text/plain";
799
+ case "pdf":
800
+ return "application/pdf";
801
+ case "webp":
802
+ return "image/webp";
803
+ case "mp3":
804
+ return "audio/mpeg";
805
+ case "mp4":
806
+ return "video/mp4";
807
+ case "csv":
808
+ return "text/csv";
809
+ case "xls":
810
+ return "application/vnd.ms-excel";
811
+ case "xlsx":
812
+ return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
813
+ case "doc":
814
+ return "application/msword";
815
+ case "docx":
816
+ return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
817
+ case "ppt":
818
+ return "application/vnd.ms-powerpoint";
819
+ case "pptx":
820
+ return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
821
+ case "zip":
822
+ return "application/zip";
823
+ case "rar":
824
+ return "application/x-rar-compressed";
825
+ case "tar":
826
+ return "application/x-tar";
827
+ case "gz":
828
+ return "application/gzip";
829
+ case "7z":
830
+ return "application/x-7z-compressed";
766
831
  default:
767
832
  return "application-octet-stream";
768
833
  }
@@ -873,10 +938,10 @@ function rethrowFileSystemError(e = "") {
873
938
  try {
874
939
  return o.apply(this, i);
875
940
  } catch (a) {
876
- const l = typeof a == "object" ? a == null ? void 0 : a.errno : null;
877
- if (l in FileErrorCodes) {
878
- const u = FileErrorCodes[l], h = typeof i[0] == "string" ? i[0] : null, _ = h !== null ? e.replaceAll("{path}", h) : e;
879
- throw new Error(`${_}: ${u}`, {
941
+ const c = typeof a == "object" ? a == null ? void 0 : a.errno : null;
942
+ if (c in FileErrorCodes) {
943
+ const l = FileErrorCodes[c], h = typeof i[0] == "string" ? i[0] : null, _ = h !== null ? e.replaceAll("{path}", h) : e;
944
+ throw new Error(`${_}: ${l}`, {
880
945
  cause: a
881
946
  });
882
947
  }
@@ -927,7 +992,12 @@ var __defProp = Object.defineProperty, __getOwnPropDesc = Object.getOwnPropertyD
927
992
  return s && n && __defProp(t, r, n), n;
928
993
  };
929
994
  const STRING = "string", NUMBER = "number", __private__dont__use = Symbol("__private__dont__use");
930
- var x, F, A, w, E, v, P, M, W, te, q, re, B, se, D, ne, $, ie, z, oe, j, ae, G, le, V, ce, J, ue, Q, de, Y, he;
995
+ class PHPExecutionFailureError extends Error {
996
+ constructor(t, r, s) {
997
+ super(t), this.response = r, this.source = s;
998
+ }
999
+ }
1000
+ var b, C, H, w, E, P, F, O, re, U, se, W, ne, B, ie, D, oe, $, ae, q, le, z, ce, j, ue, V, de, G, he, J, pe, Q, fe, Y, me, K, _e;
931
1001
  class BasePHP {
932
1002
  /**
933
1003
  * Initializes a PHP runtime.
@@ -937,46 +1007,55 @@ class BasePHP {
937
1007
  * @param serverOptions - Optional. Options for the PHPRequestHandler. If undefined, no request handler will be initialized.
938
1008
  */
939
1009
  constructor(e, t) {
1010
+ /**
1011
+ * Prepares the $_SERVER entries for the PHP runtime.
1012
+ *
1013
+ * @param defaults Default entries to include in $_SERVER.
1014
+ * @param headers HTTP headers to include in $_SERVER (as HTTP_ prefixed entries).
1015
+ * @param port HTTP port, used to determine infer $_SERVER['HTTPS'] value if none
1016
+ * was provided.
1017
+ * @returns Computed $_SERVER entries.
1018
+ */
1019
+ d(this, O);
1020
+ d(this, U);
940
1021
  d(this, W);
941
- d(this, q);
942
1022
  d(this, B);
943
1023
  d(this, D);
944
1024
  d(this, $);
1025
+ d(this, q);
945
1026
  d(this, z);
946
1027
  d(this, j);
947
- d(this, G);
948
1028
  d(this, V);
1029
+ d(this, G);
949
1030
  d(this, J);
950
1031
  d(this, Q);
951
1032
  d(this, Y);
952
- d(this, x, void 0);
953
- d(this, F, void 0);
954
- d(this, A, void 0);
1033
+ d(this, K);
1034
+ d(this, b, void 0);
1035
+ d(this, C, void 0);
1036
+ d(this, H, void 0);
955
1037
  d(this, w, void 0);
956
1038
  d(this, E, void 0);
957
- d(this, v, void 0);
958
1039
  d(this, P, void 0);
959
- d(this, M, void 0);
960
- p(this, x, []), p(this, w, !1), p(this, E, null), p(this, v, {}), p(this, P, /* @__PURE__ */ new Map()), p(this, M, []), this.semaphore = new Semaphore({ concurrency: 1 }), e !== void 0 && this.initializeRuntime(e), t && (this.requestHandler = new PHPBrowser(
961
- new PHPRequestHandler(this, t)
962
- ));
1040
+ d(this, F, void 0);
1041
+ p(this, b, []), p(this, w, !1), p(this, E, null), p(this, P, /* @__PURE__ */ new Map()), p(this, F, []), this.semaphore = new Semaphore({ concurrency: 1 }), e !== void 0 && this.initializeRuntime(e), t && (this.requestHandler = new PHPRequestHandler(this, t));
963
1042
  }
964
1043
  addEventListener(e, t) {
965
- c(this, P).has(e) || c(this, P).set(e, /* @__PURE__ */ new Set()), c(this, P).get(e).add(t);
1044
+ u(this, P).has(e) || u(this, P).set(e, /* @__PURE__ */ new Set()), u(this, P).get(e).add(t);
966
1045
  }
967
1046
  removeEventListener(e, t) {
968
1047
  var r;
969
- (r = c(this, P).get(e)) == null || r.delete(t);
1048
+ (r = u(this, P).get(e)) == null || r.delete(t);
970
1049
  }
971
1050
  dispatchEvent(e) {
972
- const t = c(this, P).get(e.type);
1051
+ const t = u(this, P).get(e.type);
973
1052
  if (t)
974
1053
  for (const r of t)
975
1054
  r(e);
976
1055
  }
977
1056
  /** @inheritDoc */
978
1057
  async onMessage(e) {
979
- c(this, M).push(e);
1058
+ u(this, F).push(e);
980
1059
  }
981
1060
  /** @inheritDoc */
982
1061
  async setSpawnHandler(handler) {
@@ -984,21 +1063,19 @@ class BasePHP {
984
1063
  }
985
1064
  /** @inheritDoc */
986
1065
  get absoluteUrl() {
987
- return this.requestHandler.requestHandler.absoluteUrl;
1066
+ return this.requestHandler.absoluteUrl;
988
1067
  }
989
1068
  /** @inheritDoc */
990
1069
  get documentRoot() {
991
- return this.requestHandler.requestHandler.documentRoot;
1070
+ return this.requestHandler.documentRoot;
992
1071
  }
993
1072
  /** @inheritDoc */
994
1073
  pathToInternalUrl(e) {
995
- return this.requestHandler.requestHandler.pathToInternalUrl(e);
1074
+ return this.requestHandler.pathToInternalUrl(e);
996
1075
  }
997
1076
  /** @inheritDoc */
998
1077
  internalUrlToPath(e) {
999
- return this.requestHandler.requestHandler.internalUrlToPath(
1000
- e
1001
- );
1078
+ return this.requestHandler.internalUrlToPath(e);
1002
1079
  }
1003
1080
  initializeRuntime(e) {
1004
1081
  if (this[__private__dont__use])
@@ -1007,7 +1084,7 @@ class BasePHP {
1007
1084
  if (!t)
1008
1085
  throw new Error("Invalid PHP runtime id.");
1009
1086
  this[__private__dont__use] = t, t.onMessage = async (r) => {
1010
- for (const s of c(this, M)) {
1087
+ for (const s of u(this, F)) {
1011
1088
  const n = await s(r);
1012
1089
  if (n)
1013
1090
  return n;
@@ -1028,13 +1105,13 @@ class BasePHP {
1028
1105
  throw new Error(
1029
1106
  "Could not set SAPI name. This can only be done before the PHP WASM module is initialized.Did you already dispatch any requests?"
1030
1107
  );
1031
- p(this, A, e);
1108
+ p(this, H, e);
1032
1109
  }
1033
1110
  /** @inheritDoc */
1034
1111
  setPhpIniPath(e) {
1035
- if (c(this, w))
1112
+ if (u(this, w))
1036
1113
  throw new Error("Cannot set PHP ini path after calling run().");
1037
- p(this, F, e), this[__private__dont__use].ccall(
1114
+ p(this, C, e), this[__private__dont__use].ccall(
1038
1115
  "wasm_set_phpini_path",
1039
1116
  null,
1040
1117
  ["string"],
@@ -1043,44 +1120,49 @@ class BasePHP {
1043
1120
  }
1044
1121
  /** @inheritDoc */
1045
1122
  setPhpIniEntry(e, t) {
1046
- if (c(this, w))
1123
+ if (u(this, w))
1047
1124
  throw new Error("Cannot set PHP ini entries after calling run().");
1048
- c(this, x).push([e, t]);
1125
+ u(this, b).push([e, t]);
1049
1126
  }
1050
1127
  /** @inheritDoc */
1051
1128
  chdir(e) {
1052
1129
  this[__private__dont__use].FS.chdir(e);
1053
1130
  }
1054
1131
  /** @inheritDoc */
1055
- async request(e, t) {
1132
+ async request(e) {
1056
1133
  if (!this.requestHandler)
1057
1134
  throw new Error("No request handler available.");
1058
- return this.requestHandler.request(e, t);
1135
+ return this.requestHandler.request(e);
1059
1136
  }
1060
1137
  /** @inheritDoc */
1061
1138
  async run(e) {
1062
1139
  const t = await this.semaphore.acquire();
1063
1140
  let r;
1064
1141
  try {
1065
- if (c(this, w) || (f(this, W, te).call(this), p(this, w, !0)), e.scriptPath && !this.fileExists(e.scriptPath))
1142
+ if (u(this, w) || (f(this, U, se).call(this), p(this, w, !0)), e.scriptPath && !this.fileExists(e.scriptPath))
1066
1143
  throw new Error(
1067
1144
  `The script path "${e.scriptPath}" does not exist.`
1068
1145
  );
1069
- f(this, G, le).call(this, e.scriptPath || ""), f(this, B, se).call(this, e.relativeUri || ""), f(this, $, ie).call(this, e.method || "GET");
1070
- const s = normalizeHeaders(e.headers || {}), n = s.host || "example.com:443";
1071
- f(this, D, ne).call(this, n, e.protocol || "http"), f(this, z, oe).call(this, s), e.body && (r = f(this, j, ae).call(this, e.body)), typeof e.code == "string" && f(this, Q, de).call(this, " ?>" + e.code), f(this, V, ce).call(this);
1072
- const o = e.env || {};
1073
- for (const a in o)
1074
- f(this, J, ue).call(this, a, o[a]);
1075
- const i = await f(this, Y, he).call(this);
1076
- if (i.exitCode !== 0) {
1077
- console.warn("PHP.run() output was:", i.text);
1078
- const a = new Error(
1079
- `PHP.run() failed with exit code ${i.exitCode} and the following output: ` + i.errors
1146
+ f(this, G, he).call(this, e.scriptPath || ""), f(this, B, ie).call(this, e.relativeUri || ""), f(this, z, ce).call(this, e.method || "GET");
1147
+ const s = normalizeHeaders(e.headers || {}), n = s.host || "example.com:443", o = f(this, q, le).call(this, n, e.protocol || "http");
1148
+ f(this, D, oe).call(this, n), f(this, $, ae).call(this, o), f(this, j, ue).call(this, s), e.body && (r = f(this, V, de).call(this, e.body)), typeof e.code == "string" && f(this, Y, me).call(this, " ?>" + e.code);
1149
+ const i = f(this, O, re).call(this, e.$_SERVER, s, o);
1150
+ for (const l in i)
1151
+ f(this, J, pe).call(this, l, i[l]);
1152
+ const a = e.env || {};
1153
+ for (const l in a)
1154
+ f(this, Q, fe).call(this, l, a[l]);
1155
+ const c = await f(this, K, _e).call(this);
1156
+ if (c.exitCode !== 0) {
1157
+ console.warn("PHP.run() output was:", c.text);
1158
+ const l = new PHPExecutionFailureError(
1159
+ `PHP.run() failed with exit code ${c.exitCode} and the following output: ` + c.errors,
1160
+ c,
1161
+ "request"
1080
1162
  );
1081
- throw a.response = i, a.source = "request", console.error(a), a;
1163
+ throw console.error(l), l;
1082
1164
  }
1083
- return i;
1165
+ return c;
1084
1166
  } catch (s) {
1085
1167
  throw this.dispatchEvent({
1086
1168
  type: "request.error",
@@ -1098,9 +1180,6 @@ class BasePHP {
1098
1180
  }
1099
1181
  }
1100
1182
  }
1101
- addServerGlobalEntry(e, t) {
1102
- c(this, v)[e] = t;
1103
- }
1104
1183
  defineConstant(e, t) {
1105
1184
  let r = {};
1106
1185
  try {
@@ -1188,17 +1267,18 @@ class BasePHP {
1188
1267
  * interrupting the operations of this PHP instance.
1189
1268
  *
1190
1269
  * @param runtime
1270
+ * @param cwd. Internal, the VFS path to recreate in the new runtime.
1271
+ * This arg is temporary and will be removed once BasePHP
1272
+ * is fully decoupled from the request handler and
1273
+ * accepts a constructor-level cwd argument.
1191
1274
  */
1192
- hotSwapPHPRuntime(e) {
1193
- const t = this[__private__dont__use].FS;
1275
+ hotSwapPHPRuntime(e, t) {
1276
+ const r = this[__private__dont__use].FS;
1194
1277
  try {
1195
1278
  this.exit();
1196
1279
  } catch {
1197
1280
  }
1198
- if (this.initializeRuntime(e), c(this, F) && this.setPhpIniPath(c(this, F)), c(this, A) && this.setSapiName(c(this, A)), this.requestHandler) {
1199
- const r = this.documentRoot;
1200
- copyFS(t, this[__private__dont__use].FS, r);
1201
- }
1281
+ this.initializeRuntime(e), u(this, C) && this.setPhpIniPath(u(this, C)), u(this, H) && this.setSapiName(u(this, H)), t && copyFS(r, this[__private__dont__use].FS, t);
1202
1282
  }
1203
1283
  exit(e = 0) {
1204
1284
  this.dispatchEvent({
@@ -1211,7 +1291,17 @@ class BasePHP {
1211
1291
  p(this, w, !1), p(this, E, null), delete this[__private__dont__use].onMessage, delete this[__private__dont__use];
1212
1292
  }
1213
1293
  }
1214
- x = new WeakMap(), F = new WeakMap(), A = new WeakMap(), w = new WeakMap(), E = new WeakMap(), v = new WeakMap(), P = new WeakMap(), M = new WeakMap(), W = new WeakSet(), te = function() {
1294
+ b = new WeakMap(), C = new WeakMap(), H = new WeakMap(), w = new WeakMap(), E = new WeakMap(), P = new WeakMap(), F = new WeakMap(), O = new WeakSet(), re = function(e, t, r) {
1295
+ const s = {
1296
+ ...e || {}
1297
+ };
1298
+ s.HTTPS = s.HTTPS || r === 443 ? "on" : "off";
1299
+ for (const n in t) {
1300
+ let o = "HTTP_";
1301
+ ["content-type", "content-length"].includes(n.toLowerCase()) && (o = ""), s[`${o}${n.toUpperCase().replace(/-/g, "_")}`] = t[n];
1302
+ }
1303
+ return s;
1304
+ }, U = new WeakSet(), se = function() {
1215
1305
  if (this.setPhpIniEntry("auto_prepend_file", "/internal/consts.php"), this.fileExists("/internal/consts.php") || this.writeFile(
1216
1306
  "/internal/consts.php",
1217
1307
  `<?php
@@ -1223,8 +1313,8 @@ x = new WeakMap(), F = new WeakMap(), A = new WeakMap(), w = new WeakMap(), E =
1223
1313
  }
1224
1314
  }
1225
1315
  }`
1226
- ), c(this, x).length > 0) {
1227
- const e = c(this, x).map(([t, r]) => `${t}=${r}`).join(`
1316
+ ), u(this, b).length > 0) {
1317
+ const e = u(this, b).map(([t, r]) => `${t}=${r}`).join(`
1228
1318
  `) + `
1229
1319
 
1230
1320
  `;
@@ -1236,7 +1326,7 @@ x = new WeakMap(), F = new WeakMap(), A = new WeakMap(), w = new WeakMap(), E =
1236
1326
  );
1237
1327
  }
1238
1328
  this[__private__dont__use].ccall("php_wasm_init", null, [], []);
1239
- }, q = new WeakSet(), re = function() {
1329
+ }, W = new WeakSet(), ne = function() {
1240
1330
  const e = "/internal/headers.json";
1241
1331
  if (!this.fileExists(e))
1242
1332
  throw new Error(
@@ -1253,7 +1343,7 @@ x = new WeakMap(), F = new WeakMap(), A = new WeakMap(), w = new WeakMap(), E =
1253
1343
  headers: r,
1254
1344
  httpStatusCode: t.status
1255
1345
  };
1256
- }, B = new WeakSet(), se = function(e) {
1346
+ }, B = new WeakSet(), ie = function(e) {
1257
1347
  if (this[__private__dont__use].ccall(
1258
1348
  "wasm_set_request_uri",
1259
1349
  null,
@@ -1268,32 +1358,35 @@ x = new WeakMap(), F = new WeakMap(), A = new WeakMap(), w = new WeakMap(), E =
1268
1358
  [t]
1269
1359
  );
1270
1360
  }
1271
- }, D = new WeakSet(), ne = function(e, t) {
1361
+ }, D = new WeakSet(), oe = function(e) {
1272
1362
  this[__private__dont__use].ccall(
1273
1363
  "wasm_set_request_host",
1274
1364
  null,
1275
1365
  [STRING],
1276
1366
  [e]
1277
1367
  );
1368
+ }, $ = new WeakSet(), ae = function(e) {
1369
+ this[__private__dont__use].ccall(
1370
+ "wasm_set_request_port",
1371
+ null,
1372
+ [NUMBER],
1373
+ [e]
1374
+ );
1375
+ }, q = new WeakSet(), le = function(e, t) {
1278
1376
  let r;
1279
1377
  try {
1280
1378
  r = parseInt(new URL(e).port, 10);
1281
1379
  } catch {
1282
1380
  }
1283
- (!r || isNaN(r) || r === 80) && (r = t === "https" ? 443 : 80), this[__private__dont__use].ccall(
1284
- "wasm_set_request_port",
1285
- null,
1286
- [NUMBER],
1287
- [r]
1288
- ), (t === "https" || !t && r === 443) && this.addServerGlobalEntry("HTTPS", "on");
1289
- }, $ = new WeakSet(), ie = function(e) {
1381
+ return (!r || isNaN(r) || r === 80) && (r = t === "https" ? 443 : 80), r;
1382
+ }, z = new WeakSet(), ce = function(e) {
1290
1383
  this[__private__dont__use].ccall(
1291
1384
  "wasm_set_request_method",
1292
1385
  null,
1293
1386
  [STRING],
1294
1387
  [e]
1295
1388
  );
1296
- }, z = new WeakSet(), oe = function(e) {
1389
+ }, j = new WeakSet(), ue = function(e) {
1297
1390
  e.cookie && this[__private__dont__use].ccall(
1298
1391
  "wasm_set_cookies",
1299
1392
  null,
@@ -1310,14 +1403,7 @@ x = new WeakMap(), F = new WeakMap(), A = new WeakMap(), w = new WeakMap(), E =
1310
1403
  [NUMBER],
1311
1404
  [parseInt(e["content-length"], 10)]
1312
1405
  );
1313
- for (const t in e) {
1314
- let r = "HTTP_";
1315
- ["content-type", "content-length"].includes(t.toLowerCase()) && (r = ""), this.addServerGlobalEntry(
1316
- `${r}${t.toUpperCase().replace(/-/g, "_")}`,
1317
- e[t]
1318
- );
1319
- }
1320
- }, j = new WeakSet(), ae = function(e) {
1406
+ }, V = new WeakSet(), de = function(e) {
1321
1407
  let t, r;
1322
1408
  typeof e == "string" ? (console.warn(
1323
1409
  "Passing a string as the request body is deprecated. Please use a Uint8Array instead. See https://github.com/WordPress/wordpress-playground/issues/997 for more details"
@@ -1340,46 +1426,45 @@ x = new WeakMap(), F = new WeakMap(), A = new WeakMap(), w = new WeakMap(), E =
1340
1426
  [NUMBER],
1341
1427
  [r]
1342
1428
  ), s;
1343
- }, G = new WeakSet(), le = function(e) {
1429
+ }, G = new WeakSet(), he = function(e) {
1344
1430
  this[__private__dont__use].ccall(
1345
1431
  "wasm_set_path_translated",
1346
1432
  null,
1347
1433
  [STRING],
1348
1434
  [e]
1349
1435
  );
1350
- }, V = new WeakSet(), ce = function() {
1351
- for (const e in c(this, v))
1352
- this[__private__dont__use].ccall(
1353
- "wasm_add_SERVER_entry",
1354
- null,
1355
- [STRING, STRING],
1356
- [e, c(this, v)[e]]
1357
- );
1358
- }, J = new WeakSet(), ue = function(e, t) {
1436
+ }, J = new WeakSet(), pe = function(e, t) {
1437
+ this[__private__dont__use].ccall(
1438
+ "wasm_add_SERVER_entry",
1439
+ null,
1440
+ [STRING, STRING],
1441
+ [e, t]
1442
+ );
1443
+ }, Q = new WeakSet(), fe = function(e, t) {
1359
1444
  this[__private__dont__use].ccall(
1360
1445
  "wasm_add_ENV_entry",
1361
1446
  null,
1362
1447
  [STRING, STRING],
1363
1448
  [e, t]
1364
1449
  );
1365
- }, Q = new WeakSet(), de = function(e) {
1450
+ }, Y = new WeakSet(), me = function(e) {
1366
1451
  this[__private__dont__use].ccall(
1367
1452
  "wasm_set_php_code",
1368
1453
  null,
1369
1454
  [STRING],
1370
1455
  [e]
1371
1456
  );
1372
- }, Y = new WeakSet(), he = async function() {
1457
+ }, K = new WeakSet(), _e = async function() {
1373
1458
  var n;
1374
1459
  let e, t;
1375
1460
  try {
1376
1461
  e = await new Promise((o, i) => {
1377
- var l;
1378
- t = (u) => {
1379
- console.error(u), console.error(u.error);
1462
+ var c;
1463
+ t = (l) => {
1464
+ console.error(l), console.error(l.error);
1380
1465
  const h = new Error("Rethrown");
1381
- h.cause = u.error, h.betterMessage = u.message, i(h);
1382
- }, (l = c(this, E)) == null || l.addEventListener(
1466
+ h.cause = l.error, h.betterMessage = l.message, i(h);
1467
+ }, (c = u(this, E)) == null || c.addEventListener(
1383
1468
  "error",
1384
1469
  t
1385
1470
  );
@@ -1393,21 +1478,21 @@ x = new WeakMap(), F = new WeakMap(), A = new WeakMap(), w = new WeakMap(), E =
1393
1478
  return a instanceof Promise ? a.then(o, i) : o(a);
1394
1479
  });
1395
1480
  } catch (o) {
1396
- for (const u in this)
1397
- typeof this[u] == "function" && (this[u] = () => {
1481
+ for (const l in this)
1482
+ typeof this[l] == "function" && (this[l] = () => {
1398
1483
  throw new Error(
1399
1484
  "PHP runtime has crashed – see the earlier error for details."
1400
1485
  );
1401
1486
  });
1402
1487
  this.functionsMaybeMissingFromAsyncify = getFunctionsMaybeMissingFromAsyncify();
1403
- const i = o, a = "betterMessage" in i ? i.betterMessage : i.message, l = new Error(a);
1404
- throw l.cause = i, console.error(l), l;
1488
+ const i = o, a = "betterMessage" in i ? i.betterMessage : i.message, c = new Error(a);
1489
+ throw c.cause = i, console.error(c), c;
1405
1490
  } finally {
1406
- (n = c(this, E)) == null || n.removeEventListener("error", t), p(this, v, {});
1491
+ (n = u(this, E)) == null || n.removeEventListener("error", t);
1407
1492
  }
1408
- const { headers: r, httpStatusCode: s } = f(this, q, re).call(this);
1493
+ const { headers: r, httpStatusCode: s } = f(this, W, ne).call(this);
1409
1494
  return new PHPResponse(
1410
- s,
1495
+ e === 0 ? s : 500,
1411
1496
  r,
1412
1497
  this.readFileAsBuffer("/internal/stdout"),
1413
1498
  this.readFileAsText("/internal/stderr"),
@@ -1516,42 +1601,42 @@ function expose(e, t = globalThis, r = ["*"]) {
1516
1601
  console.warn(`Invalid origin '${n.origin}' for comlink proxy`);
1517
1602
  return;
1518
1603
  }
1519
- const { id: o, type: i, path: a } = Object.assign({ path: [] }, n.data), l = (n.data.argumentList || []).map(fromWireValue);
1520
- let u;
1604
+ const { id: o, type: i, path: a } = Object.assign({ path: [] }, n.data), c = (n.data.argumentList || []).map(fromWireValue);
1605
+ let l;
1521
1606
  try {
1522
- const h = a.slice(0, -1).reduce((m, I) => m[I], e), _ = a.reduce((m, I) => m[I], e);
1607
+ const h = a.slice(0, -1).reduce((m, A) => m[A], e), _ = a.reduce((m, A) => m[A], e);
1523
1608
  switch (i) {
1524
1609
  case "GET":
1525
- u = _;
1610
+ l = _;
1526
1611
  break;
1527
1612
  case "SET":
1528
- h[a.slice(-1)[0]] = fromWireValue(n.data.value), u = !0;
1613
+ h[a.slice(-1)[0]] = fromWireValue(n.data.value), l = !0;
1529
1614
  break;
1530
1615
  case "APPLY":
1531
- u = _.apply(h, l);
1616
+ l = _.apply(h, c);
1532
1617
  break;
1533
1618
  case "CONSTRUCT":
1534
1619
  {
1535
- const m = new _(...l);
1536
- u = proxy(m);
1620
+ const m = new _(...c);
1621
+ l = proxy(m);
1537
1622
  }
1538
1623
  break;
1539
1624
  case "ENDPOINT":
1540
1625
  {
1541
- const { port1: m, port2: I } = new MessageChannel();
1542
- expose(e, I), u = transfer(m, [m]);
1626
+ const { port1: m, port2: A } = new MessageChannel();
1627
+ expose(e, A), l = transfer(m, [m]);
1543
1628
  }
1544
1629
  break;
1545
1630
  case "RELEASE":
1546
- u = void 0;
1631
+ l = void 0;
1547
1632
  break;
1548
1633
  default:
1549
1634
  return;
1550
1635
  }
1551
1636
  } catch (h) {
1552
- u = { value: h, [throwMarker]: 0 };
1637
+ l = { value: h, [throwMarker]: 0 };
1553
1638
  }
1554
- Promise.resolve(u).catch((h) => ({ value: h, [throwMarker]: 0 })).then((h) => {
1639
+ Promise.resolve(l).catch((h) => ({ value: h, [throwMarker]: 0 })).then((h) => {
1555
1640
  const [_, m] = toWireValue(h);
1556
1641
  t.postMessage(Object.assign(Object.assign({}, _), { id: o }), m), i === "RELEASE" && (t.removeEventListener("message", s), closeEndPoint(t), finalizer in e && typeof e[finalizer] == "function" && e[finalizer]());
1557
1642
  }).catch((h) => {
@@ -1608,7 +1693,7 @@ function createProxy(e, t = [], r = function() {
1608
1693
  return { then: () => n };
1609
1694
  const a = requestResponseMessage(e, {
1610
1695
  type: "GET",
1611
- path: t.map((l) => l.toString())
1696
+ path: t.map((c) => c.toString())
1612
1697
  }).then(fromWireValue);
1613
1698
  return a.then.bind(a);
1614
1699
  }
@@ -1616,37 +1701,37 @@ function createProxy(e, t = [], r = function() {
1616
1701
  },
1617
1702
  set(o, i, a) {
1618
1703
  throwIfProxyReleased(s);
1619
- const [l, u] = toWireValue(a);
1704
+ const [c, l] = toWireValue(a);
1620
1705
  return requestResponseMessage(e, {
1621
1706
  type: "SET",
1622
1707
  path: [...t, i].map((h) => h.toString()),
1623
- value: l
1624
- }, u).then(fromWireValue);
1708
+ value: c
1709
+ }, l).then(fromWireValue);
1625
1710
  },
1626
1711
  apply(o, i, a) {
1627
1712
  throwIfProxyReleased(s);
1628
- const l = t[t.length - 1];
1629
- if (l === createEndpoint)
1713
+ const c = t[t.length - 1];
1714
+ if (c === createEndpoint)
1630
1715
  return requestResponseMessage(e, {
1631
1716
  type: "ENDPOINT"
1632
1717
  }).then(fromWireValue);
1633
- if (l === "bind")
1718
+ if (c === "bind")
1634
1719
  return createProxy(e, t.slice(0, -1));
1635
- const [u, h] = processArguments(a);
1720
+ const [l, h] = processArguments(a);
1636
1721
  return requestResponseMessage(e, {
1637
1722
  type: "APPLY",
1638
1723
  path: t.map((_) => _.toString()),
1639
- argumentList: u
1724
+ argumentList: l
1640
1725
  }, h).then(fromWireValue);
1641
1726
  },
1642
1727
  construct(o, i) {
1643
1728
  throwIfProxyReleased(s);
1644
- const [a, l] = processArguments(i);
1729
+ const [a, c] = processArguments(i);
1645
1730
  return requestResponseMessage(e, {
1646
1731
  type: "CONSTRUCT",
1647
- path: t.map((u) => u.toString()),
1732
+ path: t.map((l) => l.toString()),
1648
1733
  argumentList: a
1649
- }, l).then(fromWireValue);
1734
+ }, c).then(fromWireValue);
1650
1735
  }
1651
1736
  });
1652
1737
  return registerProxy(n, e), n;
@@ -1735,10 +1820,10 @@ function exposeAPI(e, t) {
1735
1820
  setupTransferHandlers();
1736
1821
  const r = Promise.resolve();
1737
1822
  let s, n;
1738
- const o = new Promise((l, u) => {
1739
- s = l, n = u;
1823
+ const o = new Promise((c, l) => {
1824
+ s = c, n = l;
1740
1825
  }), i = proxyClone(e), a = new Proxy(i, {
1741
- get: (l, u) => u === "isConnected" ? () => r : u === "isReady" ? () => o : u in l ? l[u] : t == null ? void 0 : t[u]
1826
+ get: (c, l) => l === "isConnected" ? () => r : l === "isReady" ? () => o : l in c ? c[l] : t == null ? void 0 : t[l]
1742
1827
  });
1743
1828
  return expose(
1744
1829
  a,
@@ -1883,9 +1968,7 @@ class WebPHP extends BasePHP {
1883
1968
  static async loadRuntime(t, r = {}) {
1884
1969
  var o;
1885
1970
  const s = r.loadAllExtensions ? "kitchen-sink" : "light", n = await getPHPLoaderModule(t, s);
1886
- return (o = r.downloadMonitor) == null || o.expectAssets({
1887
- [n.dependencyFilename]: n.dependenciesTotalSize
1888
- }), await loadPHPRuntime(n, {
1971
+ return (o = r.onPhpLoaderModuleLoaded) == null || o.call(r, n), await loadPHPRuntime(n, {
1889
1972
  ...r.emscriptenOptions || {},
1890
1973
  ...fakeWebsocket()
1891
1974
  });
@@ -1924,17 +2007,13 @@ class WebPHPEndpoint {
1924
2007
  return _private.get(this).php.rmdir(t, r);
1925
2008
  }
1926
2009
  /** @inheritDoc @php-wasm/universal!RequestHandler.request */
1927
- request(t, r) {
1928
- return _private.get(this).php.request(t, r);
2010
+ request(t) {
2011
+ return _private.get(this).php.request(t);
1929
2012
  }
1930
2013
  /** @inheritDoc @php-wasm/web!WebPHP.run */
1931
2014
  async run(t) {
1932
2015
  return _private.get(this).php.run(t);
1933
2016
  }
1934
- /** @inheritDoc @php-wasm/web!WebPHP.setSpawnHandler */
1935
- setSpawnHandler(t) {
1936
- _private.get(this).php.setSpawnHandler(t);
1937
- }
1938
2017
  /** @inheritDoc @php-wasm/web!WebPHP.chdir */
1939
2018
  chdir(t) {
1940
2019
  return _private.get(this).php.chdir(t);
@@ -2028,17 +2107,8 @@ async function registerServiceWorker(e, t, r) {
2028
2107
  async function(i) {
2029
2108
  if (t && i.data.scope !== t)
2030
2109
  return;
2031
- const a = i.data.args || [], l = i.data.method;
2032
- let u;
2033
- try {
2034
- u = await e[l](...a);
2035
- } catch (h) {
2036
- if (l === "request" && h && typeof h == "object" && "response" in h)
2037
- u = h.response;
2038
- else
2039
- throw h;
2040
- }
2041
- i.source.postMessage(responseTo(i.data.requestId, u));
2110
+ const a = i.data.args || [], c = i.data.method, l = await e[c](...a);
2111
+ i.source.postMessage(responseTo(i.data.requestId, l));
2042
2112
  }
2043
2113
  ), s.startMessages();
2044
2114
  }