@php-wasm/universal 2.0.5 → 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -558,12 +558,7 @@ class StreamedPHPResponse {
558
558
  * Resolves once HTTP status code is available.
559
559
  */
560
560
  get httpStatusCode() {
561
- return Promise.race([
562
- this.getParsedHeaders().then((e) => e.httpStatusCode),
563
- this.exitCode.then(
564
- (e) => e !== 0 ? 500 : void 0
565
- )
566
- ]).then((e) => e !== void 0 ? e : this.getParsedHeaders().then(
561
+ return this.getParsedHeaders().then((e) => e.httpStatusCode).then((e) => e !== void 0 ? e : this.getParsedHeaders().then(
567
562
  (r) => r.httpStatusCode,
568
563
  () => 200
569
564
  )).catch(() => 500);
@@ -644,6 +639,13 @@ class PHPResponse {
644
639
  await e.exitCode
645
640
  );
646
641
  }
642
+ /**
643
+ * True if the response is successful (HTTP status code 200-399),
644
+ * false otherwise.
645
+ */
646
+ ok() {
647
+ return this.httpStatusCode >= 200 && this.httpStatusCode < 400;
648
+ }
647
649
  toRawData() {
648
650
  return {
649
651
  headers: this.headers,
@@ -1297,8 +1299,8 @@ class PHP {
1297
1299
  `The script path "${t.scriptPath}" does not exist.`
1298
1300
  );
1299
1301
  m(this, h, $).call(this, t.relativeUri || ""), m(this, h, J).call(this, t.method || "GET");
1300
- const n = normalizeHeaders(t.headers || {}), i = n.host || "example.com:443", o = m(this, h, G).call(this, i, t.protocol || "http");
1301
- if (m(this, h, B).call(this, i), m(this, h, V).call(this, o), m(this, h, Y).call(this, n), t.body && (r = m(this, h, K).call(this, t.body)), typeof t.code == "string")
1302
+ const i = normalizeHeaders(t.headers || {}), o = i.host || "example.com:443", a = m(this, h, G).call(this, o, t.protocol || "http");
1303
+ if (m(this, h, B).call(this, o), m(this, h, V).call(this, a), m(this, h, Y).call(this, i), t.body && (r = m(this, h, K).call(this, t.body)), typeof t.code == "string")
1302
1304
  this.writeFile("/internal/eval.php", t.code), m(this, h, L).call(this, "/internal/eval.php");
1303
1305
  else if (typeof t.scriptPath == "string")
1304
1306
  m(this, h, L).call(this, t.scriptPath || "");
@@ -1306,12 +1308,12 @@ class PHP {
1306
1308
  throw new TypeError(
1307
1309
  "The request object must have either a `code` or a `scriptPath` property."
1308
1310
  );
1309
- const a = m(this, h, z).call(this, t.$_SERVER, n, o);
1310
- for (const l in a)
1311
- m(this, h, X).call(this, l, a[l]);
1312
- const c = t.env || {};
1313
- for (const l in c)
1314
- m(this, h, O).call(this, l, c[l]);
1311
+ const c = m(this, h, z).call(this, t.$_SERVER, i, a);
1312
+ for (const p in c)
1313
+ m(this, h, X).call(this, p, c[p]);
1314
+ const l = t.env || {};
1315
+ for (const p in l)
1316
+ m(this, h, O).call(this, p, l[p]);
1315
1317
  return await this[__private__dont__use].ccall(
1316
1318
  "wasm_sapi_handle_request",
1317
1319
  NUMBER,
@@ -1319,14 +1321,23 @@ class PHP {
1319
1321
  [],
1320
1322
  { async: !0 }
1321
1323
  );
1322
- });
1323
- return await s.finally(() => {
1324
- r && this[__private__dont__use].free(r);
1325
- }).finally(() => {
1324
+ }), n = () => {
1325
+ if (r)
1326
+ try {
1327
+ this[__private__dont__use].free(r);
1328
+ } catch (i) {
1329
+ logger.error(i);
1330
+ }
1326
1331
  e(), this.dispatchEvent({
1327
1332
  type: "request.end"
1328
1333
  });
1329
- }), s;
1334
+ };
1335
+ return s.then(
1336
+ (i) => (i.finished.finally(n), i),
1337
+ (i) => {
1338
+ throw n(), i;
1339
+ }
1340
+ );
1330
1341
  }
1331
1342
  /**
1332
1343
  * Defines a constant in the PHP runtime.
@@ -2438,8 +2449,14 @@ class PHPRequestHandler {
2438
2449
  ...e,
2439
2450
  // Pass along URL with the #fragment filtered out
2440
2451
  url: s.toString()
2441
- };
2442
- return m(this, H, Z).call(this, a, o);
2452
+ }, c = await m(this, H, Z).call(this, a, o);
2453
+ return c.ok() && c.exitCode !== 0 ? new PHPResponse(
2454
+ 500,
2455
+ c.headers,
2456
+ c.bytes,
2457
+ c.errors,
2458
+ c.exitCode
2459
+ ) : c;
2443
2460
  } else
2444
2461
  return m(this, H, Q).call(this, i, o);
2445
2462
  else