@php-wasm/node 0.1.61 → 0.3.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.
package/README.md CHANGED
@@ -34,3 +34,7 @@ const response = await php.request({
34
34
  });
35
35
  console.log(response.text);
36
36
  ```
37
+
38
+ ## Attribution
39
+
40
+ `@php-wasm/node` started as a fork of the original PHP to WebAssembly build published by Oraoto in https://github.com/oraoto/pib and modified by Sean Morris in https://github.com/seanmorris/php-wasm.
package/index.cjs CHANGED
@@ -66724,6 +66724,14 @@ Object.defineProperty(ErrorEvent2.prototype, "error", { enumerable: true });
66724
66724
  Object.defineProperty(ErrorEvent2.prototype, "message", { enumerable: true });
66725
66725
  var ErrorEvent = typeof globalThis.ErrorEvent === "function" ? globalThis.ErrorEvent : ErrorEvent2;
66726
66726
 
66727
+ // packages/php-wasm/universal/src/lib/is-exit-code-zero.ts
66728
+ function isExitCodeZero(e) {
66729
+ if (!(e instanceof Error)) {
66730
+ return false;
66731
+ }
66732
+ return "exitCode" in e && e?.exitCode === 0 || e?.name === "ExitStatus" && "status" in e && e.status === 0;
66733
+ }
66734
+
66727
66735
  // packages/php-wasm/universal/src/lib/wasm-error-reporting.ts
66728
66736
  var UnhandledRejectionsTarget = class extends EventTarget {
66729
66737
  listenersCount = 0;
@@ -66754,9 +66762,6 @@ function improveWASMErrorReporting(runtime) {
66754
66762
  if (!(e instanceof Error)) {
66755
66763
  throw e;
66756
66764
  }
66757
- if ("exitCode" in e && e?.exitCode === 0) {
66758
- return;
66759
- }
66760
66765
  const clearMessage = clarifyErrorMessage(
66761
66766
  e,
66762
66767
  runtime.lastAsyncifyStackSource?.stack
@@ -66764,16 +66769,19 @@ function improveWASMErrorReporting(runtime) {
66764
66769
  if (runtime.lastAsyncifyStackSource) {
66765
66770
  e.cause = runtime.lastAsyncifyStackSource;
66766
66771
  }
66767
- if (!target.hasListeners()) {
66772
+ if (target.hasListeners()) {
66773
+ target.dispatchEvent(
66774
+ new ErrorEvent("error", {
66775
+ error: e,
66776
+ message: clearMessage
66777
+ })
66778
+ );
66779
+ return;
66780
+ }
66781
+ if (!isExitCodeZero(e)) {
66768
66782
  showCriticalErrorBox(clearMessage);
66769
- throw e;
66770
66783
  }
66771
- target.dispatchEvent(
66772
- new ErrorEvent("error", {
66773
- error: e,
66774
- message: clearMessage
66775
- })
66776
- );
66784
+ throw e;
66777
66785
  }
66778
66786
  };
66779
66787
  }
@@ -67107,7 +67115,6 @@ var PHPRequestHandler = class {
67107
67115
  * The PHP instance
67108
67116
  */
67109
67117
  php;
67110
- #isStaticFilePath;
67111
67118
  /**
67112
67119
  * @param php - The PHP instance.
67113
67120
  * @param config - Request Handler configuration.
@@ -67116,12 +67123,10 @@ var PHPRequestHandler = class {
67116
67123
  this.#semaphore = new Semaphore({ concurrency: 1 });
67117
67124
  const {
67118
67125
  documentRoot = "/www/",
67119
- absoluteUrl = typeof location === "object" ? location?.href : "",
67120
- isStaticFilePath = () => false
67126
+ absoluteUrl = typeof location === "object" ? location?.href : ""
67121
67127
  } = config;
67122
67128
  this.php = php;
67123
67129
  this.#DOCROOT = documentRoot;
67124
- this.#isStaticFilePath = isStaticFilePath;
67125
67130
  const url = new URL(absoluteUrl);
67126
67131
  this.#HOSTNAME = url.hostname;
67127
67132
  this.#PORT = url.port ? Number(url.port) : url.protocol === "https:" ? 443 : 80;
@@ -67168,27 +67173,31 @@ var PHPRequestHandler = class {
67168
67173
  request.url,
67169
67174
  isAbsolute ? void 0 : DEFAULT_BASE_URL
67170
67175
  );
67171
- const normalizedRelativeUrl = removePathPrefix(
67176
+ const normalizedRequestedPath = removePathPrefix(
67172
67177
  requestedUrl.pathname,
67173
67178
  this.#PATHNAME
67174
67179
  );
67175
- if (this.#isStaticFilePath(normalizedRelativeUrl)) {
67176
- return this.#serveStaticFile(normalizedRelativeUrl);
67180
+ const fsPath = `${this.#DOCROOT}${normalizedRequestedPath}`;
67181
+ if (seemsLikeAPHPRequestHandlerPath(fsPath)) {
67182
+ return await this.#dispatchToPHP(request, requestedUrl);
67177
67183
  }
67178
- return await this.#dispatchToPHP(request, requestedUrl);
67184
+ return this.#serveStaticFile(fsPath);
67179
67185
  }
67180
67186
  /**
67181
67187
  * Serves a static file from the PHP filesystem.
67182
67188
  *
67183
- * @param path - The requested static file path.
67189
+ * @param fsPath - Absolute path of the static file to serve.
67184
67190
  * @returns The response.
67185
67191
  */
67186
- #serveStaticFile(path) {
67187
- const fsPath = `${this.#DOCROOT}${path}`;
67192
+ #serveStaticFile(fsPath) {
67188
67193
  if (!this.php.fileExists(fsPath)) {
67189
67194
  return new PHPResponse(
67190
67195
  404,
67191
- {},
67196
+ // Let the service worker know that no static file was found
67197
+ // and that it's okay to issue a real fetch() to the server.
67198
+ {
67199
+ "x-file-type": ["static"]
67200
+ },
67192
67201
  new TextEncoder().encode("404 File not found")
67193
67202
  );
67194
67203
  }
@@ -67377,6 +67386,16 @@ function inferMimeType(path) {
67377
67386
  return "application-octet-stream";
67378
67387
  }
67379
67388
  }
67389
+ function seemsLikeAPHPRequestHandlerPath(path) {
67390
+ return seemsLikeAPHPFile(path) || seemsLikeADirectoryRoot(path);
67391
+ }
67392
+ function seemsLikeAPHPFile(path) {
67393
+ return path.endsWith(".php") || path.includes(".php/");
67394
+ }
67395
+ function seemsLikeADirectoryRoot(path) {
67396
+ const lastSegment = path.split("/").pop();
67397
+ return !lastSegment.includes(".");
67398
+ }
67380
67399
 
67381
67400
  // packages/php-wasm/universal/src/lib/rethrow-file-system-error.ts
67382
67401
  var FileErrorCodes = {
@@ -67482,16 +67501,12 @@ function rethrowFileSystemError(messagePrefix = "") {
67482
67501
 
67483
67502
  // packages/php-wasm/universal/src/lib/load-php-runtime.ts
67484
67503
  async function loadPHPRuntime(phpLoaderModule, phpModuleArgs = {}, dataDependenciesModules = []) {
67485
- let resolvePhpReady, resolveDepsReady;
67486
- const depsReady = new Promise((resolve) => {
67487
- resolveDepsReady = resolve;
67488
- });
67489
- const phpReady = new Promise((resolve) => {
67490
- resolvePhpReady = resolve;
67491
- });
67504
+ const [phpReady, resolvePHP, rejectPHP] = makePromise();
67505
+ const [depsReady, resolveDeps] = makePromise();
67492
67506
  const PHPRuntime = phpLoaderModule.init(currentJsRuntime, {
67493
67507
  onAbort(reason) {
67494
- console.error("WASM aborted: ");
67508
+ rejectPHP(reason);
67509
+ resolveDeps();
67495
67510
  console.error(reason);
67496
67511
  },
67497
67512
  ENV: {},
@@ -67505,20 +67520,22 @@ async function loadPHPRuntime(phpLoaderModule, phpModuleArgs = {}, dataDependenc
67505
67520
  if (phpModuleArgs.onRuntimeInitialized) {
67506
67521
  phpModuleArgs.onRuntimeInitialized();
67507
67522
  }
67508
- resolvePhpReady();
67523
+ resolvePHP();
67509
67524
  },
67510
67525
  monitorRunDependencies(nbLeft) {
67511
67526
  if (nbLeft === 0) {
67512
67527
  delete PHPRuntime.monitorRunDependencies;
67513
- resolveDepsReady();
67528
+ resolveDeps();
67514
67529
  }
67515
67530
  }
67516
67531
  });
67517
- for (const { default: loadDataModule } of dataDependenciesModules) {
67518
- loadDataModule(PHPRuntime);
67519
- }
67532
+ await Promise.all(
67533
+ dataDependenciesModules.map(
67534
+ ({ default: dataModule }) => dataModule(PHPRuntime)
67535
+ )
67536
+ );
67520
67537
  if (!dataDependenciesModules.length) {
67521
- resolveDepsReady();
67538
+ resolveDeps();
67522
67539
  }
67523
67540
  await depsReady;
67524
67541
  await phpReady;
@@ -67540,6 +67557,14 @@ var currentJsRuntime = function() {
67540
67557
  return "NODE";
67541
67558
  }
67542
67559
  }();
67560
+ var makePromise = () => {
67561
+ const methods = [];
67562
+ const promise = new Promise((resolve, reject) => {
67563
+ methods.push(resolve, reject);
67564
+ });
67565
+ methods.unshift(promise);
67566
+ return methods;
67567
+ };
67543
67568
 
67544
67569
  // packages/php-wasm/universal/src/lib/base-php.ts
67545
67570
  var STRING = "string";
@@ -68442,7 +68467,7 @@ var _NodePHP = class extends BasePHP2 {
68442
68467
  * @param argv - The arguments to pass to the CLI.
68443
68468
  * @returns The exit code of the CLI session.
68444
68469
  */
68445
- cli(argv) {
68470
+ async cli(argv) {
68446
68471
  for (const arg of argv) {
68447
68472
  this[__private__dont__use].ccall(
68448
68473
  "wasm_add_cli_arg",
@@ -68451,9 +68476,22 @@ var _NodePHP = class extends BasePHP2 {
68451
68476
  [arg]
68452
68477
  );
68453
68478
  }
68454
- return this[__private__dont__use].ccall("run_cli", null, [], [], {
68455
- async: true
68456
- });
68479
+ try {
68480
+ return await this[__private__dont__use].ccall(
68481
+ "run_cli",
68482
+ null,
68483
+ [],
68484
+ [],
68485
+ {
68486
+ async: true
68487
+ }
68488
+ );
68489
+ } catch (error) {
68490
+ if (isExitCodeZero(error)) {
68491
+ return 0;
68492
+ }
68493
+ throw error;
68494
+ }
68457
68495
  }
68458
68496
  setSkipShebang(shouldSkip) {
68459
68497
  this[__private__dont__use].ccall(
package/index.d.ts CHANGED
@@ -120,7 +120,7 @@ export interface RequestHandler {
120
120
  * }
121
121
  * })
122
122
  * php.writeFile("/www/index.php", `<?php echo file_get_contents("php://input");`);
123
- * const result = await php.run({
123
+ * const result = await php.request({
124
124
  * method: "GET",
125
125
  * headers: {
126
126
  * "Content-Type": "text/plain"
@@ -358,7 +358,7 @@ export interface IsomorphicLocalPHP extends RequestHandler {
358
358
  * post_message_to_js(string $data)
359
359
  *
360
360
  * Arguments:
361
- * $data – any extra information as a string
361
+ * $data (string) Data to pass to JavaScript.
362
362
  *
363
363
  * @example
364
364
  *
@@ -497,11 +497,6 @@ export interface PHPRequestHandlerConfiguration {
497
497
  * Request Handler URL. Used to populate $_SERVER details like HTTP_HOST.
498
498
  */
499
499
  absoluteUrl?: string;
500
- /**
501
- * Callback used by the PHPRequestHandler to decide whether
502
- * the requested path refers to a PHP file or a static file.
503
- */
504
- isStaticFilePath?: (path: string) => boolean;
505
500
  }
506
501
  declare class PHPRequestHandler implements RequestHandler {
507
502
  #private;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@php-wasm/node",
3
- "version": "0.1.61",
3
+ "version": "0.3.0",
4
4
  "description": "PHP.wasm for Node.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,13 +28,17 @@
28
28
  "license": "GPL-2.0-or-later",
29
29
  "main": "index.cjs",
30
30
  "types": "index.d.ts",
31
- "gitHead": "f2cd7382004f7b003904b5db1d33d37fd2b1bfab",
31
+ "gitHead": "6890ff9243f9a10f0b86755fead101647a8c8535",
32
+ "engines": {
33
+ "node": ">=16.15.1",
34
+ "npm": ">=8.11.0"
35
+ },
32
36
  "dependencies": {
33
37
  "comlink": "^4.4.1",
34
38
  "express": "4.18.2",
35
39
  "ws": "8.13.0",
36
40
  "yargs": "17.7.2",
37
- "@php-wasm/universal": "0.1.61",
38
- "@php-wasm/util": "0.1.61"
41
+ "@php-wasm/universal": "0.3.0",
42
+ "@php-wasm/util": "0.3.0"
39
43
  }
40
44
  }
@@ -34,3 +34,7 @@ const response = await php.request({
34
34
  });
35
35
  console.log(response.text);
36
36
  ```
37
+
38
+ ## Attribution
39
+
40
+ `@php-wasm/node` started as a fork of the original PHP to WebAssembly build published by Oraoto in https://github.com/oraoto/pib and modified by Sean Morris in https://github.com/seanmorris/php-wasm.