@php-wasm/universal 3.1.2 → 3.1.3

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
@@ -311,19 +311,31 @@ class FSHelpers {
311
311
  e.mkdirTree(t);
312
312
  }
313
313
  static copyRecursive(e, t, s) {
314
- const n = e.lookupPath(t).node;
315
- if (e.isDir(n.mode)) {
316
- e.mkdirTree(s);
317
- const i = e.readdir(t).filter(
318
- (o) => o !== "." && o !== ".."
319
- );
320
- for (const o of i)
321
- FSHelpers.copyRecursive(
322
- e,
323
- joinPaths(t, o),
324
- joinPaths(s, o)
314
+ try {
315
+ const n = e.lookupPath(t).node;
316
+ if (e.isDir(n.mode)) {
317
+ if (t === s || s.startsWith(`${t}/`))
318
+ throw new ErrnoError(28);
319
+ e.mkdirTree(s);
320
+ const i = e.readdir(t).filter(
321
+ (o) => o !== "." && o !== ".."
325
322
  );
326
- } else e.isLink(n.mode) ? e.symlink(e.readlink(t), s) : e.writeFile(s, e.readFile(t));
323
+ for (const o of i)
324
+ FSHelpers.copyRecursive(
325
+ e,
326
+ joinPaths(t, o),
327
+ joinPaths(s, o)
328
+ );
329
+ } else e.isLink(n.mode) ? e.symlink(e.readlink(t), s) : e.writeFile(s, e.readFile(t));
330
+ } catch (n) {
331
+ const i = getEmscriptenFsError(n);
332
+ throw i ? new Error(
333
+ `Could not copy ${t} to ${s}: ${i}`,
334
+ {
335
+ cause: n
336
+ }
337
+ ) : n;
338
+ }
327
339
  }
328
340
  }
329
341
  FSHelpers.readFileAsText = rethrowFileSystemError('Could not read "{path}"')(
@@ -359,9 +371,6 @@ FSHelpers.fileExists = rethrowFileSystemError('Could not stat "{path}"')(
359
371
  FSHelpers.mkdir = rethrowFileSystemError('Could not create directory "{path}"')(
360
372
  FSHelpers.mkdir
361
373
  );
362
- FSHelpers.copyRecursive = rethrowFileSystemError(
363
- 'Could not copy files from "{path}"'
364
- )(FSHelpers.copyRecursive);
365
374
  const _private = /* @__PURE__ */ new WeakMap();
366
375
  var x;
367
376
  class PHPWorker {
@@ -651,23 +660,36 @@ const D = class D {
651
660
  */
652
661
  static fromPHPResponse(e) {
653
662
  const t = new ReadableStream({
654
- start(i) {
655
- i.enqueue(e.bytes), i.close();
663
+ start(c) {
664
+ c.enqueue(e.bytes), c.close();
665
+ }
666
+ }), s = [];
667
+ for (const [c, l] of Object.entries(e.headers))
668
+ for (const d of l)
669
+ s.push(`${c}: ${d}`);
670
+ const n = JSON.stringify({
671
+ status: e.httpStatusCode,
672
+ headers: s
673
+ }), i = new ReadableStream({
674
+ start(c) {
675
+ c.enqueue(new TextEncoder().encode(n)), c.close();
656
676
  }
657
- }), s = () => new ReadableStream({
658
- start(i) {
659
- i.close();
677
+ }), o = new ReadableStream({
678
+ start(c) {
679
+ e.errors.length > 0 && c.enqueue(
680
+ new TextEncoder().encode(e.errors)
681
+ ), c.close();
660
682
  }
661
- }), n = new D(
662
- s(),
683
+ }), a = new D(
684
+ i,
663
685
  t,
664
- s(),
686
+ o,
665
687
  Promise.resolve(e.exitCode)
666
688
  );
667
- return n.parsedHeaders = Promise.resolve({
689
+ return a.parsedHeaders = Promise.resolve({
668
690
  headers: e.headers,
669
691
  httpStatusCode: e.httpStatusCode
670
- }), n;
692
+ }), a;
671
693
  }
672
694
  /**
673
695
  * Creates a StreamedPHPResponse for a given HTTP status code.
@@ -1021,7 +1043,7 @@ class PHPExecutionFailureError extends Error {
1021
1043
  }
1022
1044
  }
1023
1045
  const PHP_INI_PATH = "/internal/shared/php.ini", AUTO_PREPEND_SCRIPT = "/internal/shared/auto_prepend_file.php", OPCACHE_FILE_FOLDER = "/internal/shared/opcache";
1024
- var M, R, b, S, H, T, w, h, J, Y, X, Q, K, Z, ee, te, j, re, q, z;
1046
+ var M, R, b, S, H, T, w, p, J, Y, X, Q, K, Z, ee, te, j, re, q, $;
1025
1047
  class PHP {
1026
1048
  /**
1027
1049
  * Initializes a PHP runtime.
@@ -1031,7 +1053,7 @@ class PHP {
1031
1053
  * @param requestHandlerOptions - Optional. Options for the PHPRequestHandler. If undefined, no request handler will be initialized.
1032
1054
  */
1033
1055
  constructor(r) {
1034
- y(this, h);
1056
+ y(this, p);
1035
1057
  y(this, M);
1036
1058
  y(this, R, !1);
1037
1059
  y(this, b, null);
@@ -1456,7 +1478,7 @@ class PHP {
1456
1478
  async runStream(r) {
1457
1479
  const e = await this.semaphore.acquire();
1458
1480
  let t;
1459
- const s = m(this, h, z).call(this, async () => {
1481
+ const s = m(this, p, $).call(this, async () => {
1460
1482
  if (u(this, R) || (await this[__private__dont__use].ccall(
1461
1483
  "php_wasm_init",
1462
1484
  null,
@@ -1469,22 +1491,22 @@ class PHP {
1469
1491
  throw new Error(
1470
1492
  `The script path "${r.scriptPath}" does not exist.`
1471
1493
  );
1472
- m(this, h, Y).call(this, r.relativeUri || ""), m(this, h, Z).call(this, r.method || "GET");
1473
- const i = normalizeHeaders(r.headers || {}), o = i.host || "example.com:443", a = m(this, h, K).call(this, o, r.protocol || "http");
1474
- if (m(this, h, X).call(this, o), m(this, h, Q).call(this, a), m(this, h, ee).call(this, i), r.body && (t = m(this, h, te).call(this, r.body)), typeof r.code == "string")
1475
- this.writeFile("/internal/eval.php", r.code), m(this, h, j).call(this, "/internal/eval.php");
1494
+ m(this, p, Y).call(this, r.relativeUri || ""), m(this, p, Z).call(this, r.method || "GET");
1495
+ const i = normalizeHeaders(r.headers || {}), o = i.host || "example.com:443", a = m(this, p, K).call(this, o, r.protocol || "http");
1496
+ if (m(this, p, X).call(this, o), m(this, p, Q).call(this, a), m(this, p, ee).call(this, i), r.body && (t = m(this, p, te).call(this, r.body)), typeof r.code == "string")
1497
+ this.writeFile("/internal/eval.php", r.code), m(this, p, j).call(this, "/internal/eval.php");
1476
1498
  else if (typeof r.scriptPath == "string")
1477
- m(this, h, j).call(this, r.scriptPath || "");
1499
+ m(this, p, j).call(this, r.scriptPath || "");
1478
1500
  else
1479
1501
  throw new TypeError(
1480
1502
  "The request object must have either a `code` or a `scriptPath` property."
1481
1503
  );
1482
- const c = m(this, h, J).call(this, r.$_SERVER, i, a);
1504
+ const c = m(this, p, J).call(this, r.$_SERVER, i, a);
1483
1505
  for (const d in c)
1484
- m(this, h, re).call(this, d, c[d]);
1506
+ m(this, p, re).call(this, d, c[d]);
1485
1507
  const l = r.env || {};
1486
1508
  for (const d in l)
1487
- m(this, h, q).call(this, d, l[d]);
1509
+ m(this, p, q).call(this, d, l[d]);
1488
1510
  return await this[__private__dont__use].ccall(
1489
1511
  "wasm_sapi_handle_request",
1490
1512
  NUMBER,
@@ -1602,8 +1624,8 @@ class PHP {
1602
1624
  * Moves a file or directory in the PHP filesystem to a
1603
1625
  * new location.
1604
1626
  *
1605
- * @param oldPath The path to rename.
1606
- * @param newPath The new path.
1627
+ * @param fromPath The path to rename.
1628
+ * @param toPath The new path.
1607
1629
  */
1608
1630
  mv(r, e) {
1609
1631
  const t = FSHelpers.mv(
@@ -1613,6 +1635,21 @@ class PHP {
1613
1635
  );
1614
1636
  return this.dispatchEvent({ type: "filesystem.write" }), t;
1615
1637
  }
1638
+ /**
1639
+ * Copies a file or directory in the PHP filesystem to a
1640
+ * new location.
1641
+ *
1642
+ * @param fromPath The source path.
1643
+ * @param toPath The target path.
1644
+ */
1645
+ cp(r, e) {
1646
+ const t = FSHelpers.copyRecursive(
1647
+ this[__private__dont__use].FS,
1648
+ r,
1649
+ e
1650
+ );
1651
+ return this.dispatchEvent({ type: "filesystem.write" }), t;
1652
+ }
1616
1653
  /**
1617
1654
  * Removes a directory from the PHP filesystem.
1618
1655
  *
@@ -1803,10 +1840,10 @@ class PHP {
1803
1840
  return this.subProcess(r, e);
1804
1841
  u(this, R) && (u(this, w).needsRotating = !0);
1805
1842
  const t = await this.semaphore.acquire();
1806
- return await m(this, h, z).call(this, () => {
1843
+ return await m(this, p, $).call(this, () => {
1807
1844
  const s = e.env || {};
1808
1845
  for (const [n, i] of Object.entries(s))
1809
- m(this, h, q).call(this, n, i);
1846
+ m(this, p, q).call(this, n, i);
1810
1847
  r = [r[0], "-c", PHP_INI_PATH, ...r.slice(1)];
1811
1848
  for (const n of r)
1812
1849
  this[__private__dont__use].ccall(
@@ -1897,7 +1934,7 @@ class PHP {
1897
1934
  this.exit(0);
1898
1935
  }
1899
1936
  }
1900
- M = new WeakMap(), R = new WeakMap(), b = new WeakMap(), S = new WeakMap(), H = new WeakMap(), T = new WeakMap(), w = new WeakMap(), h = new WeakSet(), /**
1937
+ M = new WeakMap(), R = new WeakMap(), b = new WeakMap(), S = new WeakMap(), H = new WeakMap(), T = new WeakMap(), w = new WeakMap(), p = new WeakSet(), /**
1901
1938
  * Prepares the $_SERVER entries for the PHP runtime.
1902
1939
  *
1903
1940
  * @param defaults Default entries to include in $_SERVER.
@@ -2019,34 +2056,34 @@ J = function(r, e, t) {
2019
2056
  [STRING, STRING],
2020
2057
  [r, e]
2021
2058
  );
2022
- }, z = async function(r) {
2059
+ }, $ = async function(r) {
2023
2060
  u(this, w).enabled && u(this, w).needsRotating && await this.rotateRuntime(), ++u(this, w).requestsMade, u(this, w).requestsMade >= u(this, w).maxRequests && (u(this, w).needsRotating = !0);
2024
2061
  const e = this[__private__dont__use], t = await createInvertedReadableStream();
2025
- e.onHeaders = (p) => {
2026
- a || s || t.controller.enqueue(p.slice());
2062
+ e.onHeaders = (h) => {
2063
+ a || s || t.controller.enqueue(h.slice());
2027
2064
  };
2028
2065
  let s = !1;
2029
2066
  const n = () => {
2030
2067
  s || (s = !0, t.controller.close());
2031
2068
  }, i = await createInvertedReadableStream();
2032
- e.onStdout = (p) => {
2033
- n(), !a && i.controller.enqueue(p.slice());
2069
+ e.onStdout = (h) => {
2070
+ n(), !a && i.controller.enqueue(h.slice());
2034
2071
  };
2035
2072
  const o = await createInvertedReadableStream();
2036
- e.onStderr = (p) => {
2037
- a || o.controller.enqueue(p.slice());
2073
+ e.onStderr = (h) => {
2074
+ a || o.controller.enqueue(h.slice());
2038
2075
  };
2039
2076
  let a = !1, c;
2040
2077
  const d = (async () => {
2041
- var p;
2078
+ var h;
2042
2079
  try {
2043
2080
  return await Promise.race([
2044
2081
  r(),
2045
2082
  new Promise((_, F) => {
2046
- var $;
2083
+ var z;
2047
2084
  c = (V) => {
2048
2085
  isExitCode(V.error) || F(V.error);
2049
- }, ($ = u(this, b)) == null || $.addEventListener(
2086
+ }, (z = u(this, b)) == null || z.addEventListener(
2050
2087
  "error",
2051
2088
  c,
2052
2089
  { once: !0 }
@@ -2065,27 +2102,27 @@ J = function(r, e, t) {
2065
2102
  });
2066
2103
  throw this.functionsMaybeMissingFromAsyncify = getFunctionsMaybeMissingFromAsyncify(), f;
2067
2104
  } finally {
2068
- a || (i.controller.close(), o.controller.close(), n(), a = !0), (p = u(this, b)) == null || p.removeEventListener(
2105
+ a || (i.controller.close(), o.controller.close(), n(), a = !0), (h = u(this, b)) == null || h.removeEventListener(
2069
2106
  "error",
2070
2107
  c
2071
2108
  );
2072
2109
  }
2073
2110
  })().then(
2074
- (p) => (p !== 0 && this.dispatchEvent({
2111
+ (h) => (h !== 0 && this.dispatchEvent({
2075
2112
  type: "request.error",
2076
2113
  error: new Error(
2077
- `PHP.run() failed with exit code ${p}.`
2114
+ `PHP.run() failed with exit code ${h}.`
2078
2115
  ),
2079
2116
  // Distinguish between PHP request and PHP-wasm errors
2080
2117
  source: "php-wasm"
2081
- }), p),
2082
- (p) => {
2083
- const f = p.source ?? "php-wasm";
2118
+ }), h),
2119
+ (h) => {
2120
+ const f = h.source ?? "php-wasm";
2084
2121
  throw this.dispatchEvent({
2085
2122
  type: "request.error",
2086
- error: p,
2123
+ error: h,
2087
2124
  source: f
2088
- }), p;
2125
+ }), h;
2089
2126
  }
2090
2127
  );
2091
2128
  return new StreamedPHPResponse(
@@ -2568,11 +2605,11 @@ class PHPRequestHandler {
2568
2605
  else if (e.phpFactory)
2569
2606
  this.instanceManager = new PHPProcessManager({
2570
2607
  phpFactory: async (d) => {
2571
- const p = await e.phpFactory({
2608
+ const h = await e.phpFactory({
2572
2609
  ...d,
2573
2610
  requestHandler: this
2574
2611
  });
2575
- return a(p), p;
2612
+ return a(h), h;
2576
2613
  },
2577
2614
  maxPhpInstances: e.maxPhpInstances
2578
2615
  });
@@ -2959,8 +2996,8 @@ ne = function(e, t) {
2959
2996
  let c = t.body;
2960
2997
  if (typeof c == "object" && !(c instanceof Uint8Array)) {
2961
2998
  o = "POST";
2962
- const { bytes: d, contentType: p } = await encodeAsMultipart(c);
2963
- c = d, a["content-type"] = p;
2999
+ const { bytes: d, contentType: h } = await encodeAsMultipart(c);
3000
+ c = d, a["content-type"] = h;
2964
3001
  }
2965
3002
  const l = await e.runStream({
2966
3003
  relativeUri: ensurePathPrefix(
@@ -2980,8 +3017,8 @@ ne = function(e, t) {
2980
3017
  });
2981
3018
  if (u(this, k)) {
2982
3019
  const d = u(this, k);
2983
- l.headers.then((p) => {
2984
- d.rememberCookiesFromResponseHeaders(p);
3020
+ l.headers.then((h) => {
3021
+ d.rememberCookiesFromResponseHeaders(h);
2985
3022
  });
2986
3023
  }
2987
3024
  return l;
@@ -3032,12 +3069,12 @@ function ensureProxyFSHasMmapSupport(r) {
3032
3069
  const d = t.malloc(o);
3033
3070
  if (!d)
3034
3071
  throw new n.ErrnoError(48);
3035
- const p = t.HEAPU8.subarray(d, d + o);
3072
+ const h = t.HEAPU8.subarray(d, d + o);
3036
3073
  let f = 0;
3037
3074
  for (; f < o; ) {
3038
3075
  const _ = i.stream_ops.read(
3039
3076
  i,
3040
- p,
3077
+ h,
3041
3078
  f,
3042
3079
  o - f,
3043
3080
  f
@@ -3328,18 +3365,18 @@ function expose(r, e = globalThis, t = ["*"], s) {
3328
3365
  }, l = (i.data.argumentList || []).map(fromWireValue);
3329
3366
  let d;
3330
3367
  try {
3331
- const p = c.slice(0, -1).reduce((_, F) => _[F], r), f = c.reduce((_, F) => _[F], r);
3368
+ const h = c.slice(0, -1).reduce((_, F) => _[F], r), f = c.reduce((_, F) => _[F], r);
3332
3369
  switch (a) {
3333
3370
  case MessageType.GET:
3334
3371
  d = f;
3335
3372
  break;
3336
3373
  case MessageType.SET:
3337
- p[c.slice(-1)[0]] = fromWireValue(
3374
+ h[c.slice(-1)[0]] = fromWireValue(
3338
3375
  i.data.value
3339
3376
  ), d = !0;
3340
3377
  break;
3341
3378
  case MessageType.APPLY:
3342
- d = f.apply(p, l);
3379
+ d = f.apply(h, l);
3343
3380
  break;
3344
3381
  case MessageType.CONSTRUCT:
3345
3382
  {
@@ -3359,18 +3396,18 @@ function expose(r, e = globalThis, t = ["*"], s) {
3359
3396
  default:
3360
3397
  return;
3361
3398
  }
3362
- } catch (p) {
3363
- d = { value: p, [throwMarker]: 0 };
3399
+ } catch (h) {
3400
+ d = { value: h, [throwMarker]: 0 };
3364
3401
  }
3365
- Promise.resolve(d).catch((p) => ({ value: p, [throwMarker]: 0 })).then((p) => {
3366
- const [f, _] = toWireValue(p);
3402
+ Promise.resolve(d).catch((h) => ({ value: h, [throwMarker]: 0 })).then((h) => {
3403
+ const [f, _] = toWireValue(h);
3367
3404
  e.postMessage({ ...f, id: o }, _), a === MessageType.RELEASE && (e.removeEventListener("message", n), closeEndPoint(e), finalizer in r && typeof r[finalizer] == "function" && r[finalizer]());
3368
3405
  }).catch(() => {
3369
- const [p, f] = toWireValue({
3406
+ const [h, f] = toWireValue({
3370
3407
  value: new TypeError("Unserializable return value"),
3371
3408
  [throwMarker]: 0
3372
3409
  });
3373
- e.postMessage({ ...p, id: o }, f);
3410
+ e.postMessage({ ...h, id: o }, f);
3374
3411
  }).finally(() => {
3375
3412
  s == null || s(i);
3376
3413
  });
@@ -3447,7 +3484,7 @@ function createProxy(r, e, t = [], s = function() {
3447
3484
  e,
3448
3485
  {
3449
3486
  type: MessageType.SET,
3450
- path: [...t, a].map((p) => p.toString()),
3487
+ path: [...t, a].map((h) => h.toString()),
3451
3488
  value: l
3452
3489
  },
3453
3490
  d
@@ -3462,7 +3499,7 @@ function createProxy(r, e, t = [], s = function() {
3462
3499
  }).then(fromWireValue);
3463
3500
  if (l === "bind")
3464
3501
  return createProxy(r, e, t.slice(0, -1));
3465
- const [d, p] = processArguments(c);
3502
+ const [d, h] = processArguments(c);
3466
3503
  return requestResponseMessage(
3467
3504
  r,
3468
3505
  e,
@@ -3471,7 +3508,7 @@ function createProxy(r, e, t = [], s = function() {
3471
3508
  path: t.map((f) => f.toString()),
3472
3509
  argumentList: d
3473
3510
  },
3474
- p
3511
+ h
3475
3512
  ).then(fromWireValue);
3476
3513
  },
3477
3514
  construct(o, a) {
@@ -4656,19 +4693,39 @@ function createObjectPoolProxy(r) {
4656
4693
  return new Proxy(function() {
4657
4694
  }, {
4658
4695
  apply(c, l, d) {
4659
- return i((p) => p[a](...d));
4696
+ return i((h) => h[a](...d));
4660
4697
  },
4661
4698
  get(c, l) {
4662
4699
  if (l === "then")
4663
- return (d, p) => i((f) => f[a]).then(
4700
+ return (d, h) => i((f) => f[a]).then(
4664
4701
  d,
4665
- p
4702
+ h
4666
4703
  );
4667
4704
  }
4668
4705
  });
4669
4706
  }
4670
4707
  });
4671
4708
  }
4709
+ const maxValueForSigned32BitInteger = 2 ** 31 - 1;
4710
+ class ProcessIdAllocator {
4711
+ constructor(e = 1, t = maxValueForSigned32BitInteger) {
4712
+ this.claimed = /* @__PURE__ */ new Set(), this.initialId = e, this.maxId = t, this.nextId = e;
4713
+ }
4714
+ claim() {
4715
+ const e = this.maxId - this.initialId + 1;
4716
+ for (let t = 0; t < e; t++)
4717
+ if (this.claimed.has(this.nextId))
4718
+ this.nextId++, this.nextId > this.maxId && (this.nextId = this.initialId);
4719
+ else
4720
+ return this.claimed.add(this.nextId), this.nextId;
4721
+ throw new Error(
4722
+ `Unable to find free process ID after ${e} tries.`
4723
+ );
4724
+ }
4725
+ release(e) {
4726
+ return this.claimed.has(e) ? (this.claimed.delete(e), !0) : !1;
4727
+ }
4728
+ }
4672
4729
  export {
4673
4730
  DEFAULT_BASE_URL,
4674
4731
  FSHelpers,
@@ -4685,6 +4742,7 @@ export {
4685
4742
  PHPRequestHandler,
4686
4743
  PHPResponse,
4687
4744
  PHPWorker,
4745
+ ProcessIdAllocator,
4688
4746
  SinglePHPInstanceManager,
4689
4747
  StreamedPHPResponse,
4690
4748
  SupportedPHPVersions,