@php-wasm/node 0.1.45 → 0.1.49

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 (3) hide show
  1. package/index.cjs +31 -21
  2. package/index.d.ts +101 -12
  3. package/package.json +5 -5
package/index.cjs CHANGED
@@ -67117,6 +67117,16 @@ var PHPRequestHandler = class {
67117
67117
  } else {
67118
67118
  body = request.body;
67119
67119
  }
67120
+ let scriptPath;
67121
+ try {
67122
+ scriptPath = this.#resolvePHPFilePath(requestedUrl.pathname);
67123
+ } catch (error) {
67124
+ return new PHPResponse(
67125
+ 404,
67126
+ {},
67127
+ new TextEncoder().encode("404 File not found")
67128
+ );
67129
+ }
67120
67130
  return await this.php.run({
67121
67131
  relativeUri: ensurePathPrefix(
67122
67132
  toRelativeUrl(requestedUrl),
@@ -67126,7 +67136,7 @@ var PHPRequestHandler = class {
67126
67136
  method: request.method || preferredMethod,
67127
67137
  body,
67128
67138
  fileInfos,
67129
- scriptPath: this.#resolvePHPFilePath(requestedUrl.pathname),
67139
+ scriptPath,
67130
67140
  headers
67131
67141
  });
67132
67142
  } finally {
@@ -67139,6 +67149,7 @@ var PHPRequestHandler = class {
67139
67149
  * Fall back to index.php as if there was a url rewriting rule in place.
67140
67150
  *
67141
67151
  * @param requestedPath - The requested pathname.
67152
+ * @throws {Error} If the requested path doesn't exist.
67142
67153
  * @returns The resolved filesystem path.
67143
67154
  */
67144
67155
  #resolvePHPFilePath(requestedPath) {
@@ -67157,6 +67168,9 @@ var PHPRequestHandler = class {
67157
67168
  if (this.php.fileExists(resolvedFsPath)) {
67158
67169
  return resolvedFsPath;
67159
67170
  }
67171
+ if (!this.php.fileExists(`${this.#DOCROOT}/index.php`)) {
67172
+ throw new Error(`File not found: ${resolvedFsPath}`);
67173
+ }
67160
67174
  return `${this.#DOCROOT}/index.php`;
67161
67175
  }
67162
67176
  };
@@ -67691,7 +67705,7 @@ var BasePHP2 = class {
67691
67705
  let exitCode;
67692
67706
  let errorListener;
67693
67707
  try {
67694
- exitCode = await new Promise(async (resolve, reject) => {
67708
+ exitCode = await new Promise((resolve, reject) => {
67695
67709
  errorListener = (e) => {
67696
67710
  const rethrown = new Error("Rethrown");
67697
67711
  rethrown.cause = e.error;
@@ -67702,24 +67716,16 @@ var BasePHP2 = class {
67702
67716
  "error",
67703
67717
  errorListener
67704
67718
  );
67705
- try {
67706
- resolve(
67707
- /**
67708
- * This is awkward, but Asyncify makes wasm_sapi_handle_request return
67709
- * Promise<Promise<number>>.
67710
- *
67711
- * @TODO: Determine whether this is a bug in emscripten or in our code.
67712
- */
67713
- await await this[__private__dont__use].ccall(
67714
- "wasm_sapi_handle_request",
67715
- NUMBER,
67716
- [],
67717
- []
67718
- )
67719
- );
67720
- } catch (e) {
67721
- reject(e);
67719
+ const response = this[__private__dont__use].ccall(
67720
+ "wasm_sapi_handle_request",
67721
+ NUMBER,
67722
+ [],
67723
+ []
67724
+ );
67725
+ if (response instanceof Promise) {
67726
+ return response.then(resolve, reject);
67722
67727
  }
67728
+ return resolve(response);
67723
67729
  });
67724
67730
  } catch (e) {
67725
67731
  for (const name in this) {
@@ -68267,8 +68273,12 @@ var _NodePHP = class extends BasePHP2 {
68267
68273
  /**
68268
68274
  * Starts a PHP CLI session with given arguments.
68269
68275
  *
68270
- * Can only be used when PHP was compiled with the CLI SAPI.
68271
- * Cannot be used in conjunction with `run()`.
68276
+ * This method can only be used when PHP was compiled with the CLI SAPI
68277
+ * and it cannot be used in conjunction with `run()`.
68278
+ *
68279
+ * Once this method finishes running, the PHP instance is no
68280
+ * longer usable and should be discarded. This is because PHP
68281
+ * internally cleans up all the resources and calls exit().
68272
68282
  *
68273
68283
  * @param argv - The arguments to pass to the CLI.
68274
68284
  * @returns The exit code of the CLI session.
package/index.d.ts CHANGED
@@ -105,7 +105,33 @@ export interface RequestHandler {
105
105
  /**
106
106
  * Serves the request – either by serving a static file, or by
107
107
  * dispatching it to the PHP runtime.
108
- * Cannot be used in conjunction with `cli()`.
108
+ *
109
+ * The request() method mode behaves like a web server and only works if
110
+ * the PHP was initialized with a `requestHandler` option (which the online version
111
+ * of WordPress Playground does by default).
112
+ *
113
+ * In the request mode, you pass an object containing the request information
114
+ * (method, headers, body, etc.) and the path to the PHP file to run:
115
+ *
116
+ * ```ts
117
+ * const php = PHP.load('7.4', {
118
+ * requestHandler: {
119
+ * documentRoot: "/www"
120
+ * }
121
+ * })
122
+ * php.writeFile("/www/index.php", `<?php echo file_get_contents("php://input");`);
123
+ * const result = await php.run({
124
+ * method: "GET",
125
+ * headers: {
126
+ * "Content-Type": "text/plain"
127
+ * },
128
+ * body: "Hello world!",
129
+ * path: "/www/index.php"
130
+ * });
131
+ * // result.text === "Hello world!"
132
+ * ```
133
+ *
134
+ * The `request()` method cannot be used in conjunction with `cli()`.
109
135
  *
110
136
  * @example
111
137
  * ```js
@@ -152,7 +178,18 @@ export interface RequestHandler {
152
178
  documentRoot: string;
153
179
  }
154
180
  export interface IsomorphicLocalPHP extends RequestHandler {
181
+ /**
182
+ * Sets the path to the php.ini file to use for the PHP instance.
183
+ *
184
+ * @param path - The path to the php.ini file.
185
+ */
155
186
  setPhpIniPath(path: string): void;
187
+ /**
188
+ * Sets a value for a specific key in the php.ini file for the PHP instance.
189
+ *
190
+ * @param key - The key to set the value for.
191
+ * @param value - The value to set for the key.
192
+ */
156
193
  setPhpIniEntry(key: string, value: string): void;
157
194
  /**
158
195
  * Recursively creates a directory with the given path in the PHP filesystem.
@@ -244,24 +281,72 @@ export interface IsomorphicLocalPHP extends RequestHandler {
244
281
  chdir(path: string): void;
245
282
  /**
246
283
  * Runs PHP code.
247
- * Cannot be used in conjunction with `cli()`.
248
284
  *
249
- * @example
250
- * ```js
251
- * const output = await php.run('<?php echo "Hello world!";');
252
- * console.log(output.stdout); // "Hello world!"
285
+ * This low-level method directly interacts with the WebAssembly
286
+ * PHP interpreter.
287
+ *
288
+ * Every time you call run(), it prepares the PHP
289
+ * environment and:
290
+ *
291
+ * * Resets the internal PHP state
292
+ * * Populates superglobals ($_SERVER, $_GET, etc.)
293
+ * * Handles file uploads
294
+ * * Populates input streams (stdin, argv, etc.)
295
+ * * Sets the current working directory
296
+ *
297
+ * You can use run() in two primary modes:
298
+ *
299
+ * ### Code snippet mode
300
+ *
301
+ * In this mode, you pass a string containing PHP code to run.
302
+ *
303
+ * ```ts
304
+ * const result = await php.run({
305
+ * code: `<?php echo "Hello world!";`
306
+ * });
307
+ * // result.text === "Hello world!"
253
308
  * ```
254
309
  *
310
+ * In this mode, information like __DIR__ or __FILE__ isn't very
311
+ * useful because the code is not associated with any file.
312
+ *
313
+ * Under the hood, the PHP snippet is passed to the `zend_eval_string`
314
+ * C function.
315
+ *
316
+ * ### File mode
317
+ *
318
+ * In the file mode, you pass a scriptPath and PHP executes a file
319
+ * found at a that path:
320
+ *
321
+ * ```ts
322
+ * php.writeFile(
323
+ * "/www/index.php",
324
+ * `<?php echo "Hello world!";"`
325
+ * );
326
+ * const result = await php.run({
327
+ * scriptPath: "/www/index.php"
328
+ * });
329
+ * // result.text === "Hello world!"
330
+ * ```
331
+ *
332
+ * In this mode, you can rely on path-related information like __DIR__
333
+ * or __FILE__.
334
+ *
335
+ * Under the hood, the PHP file is executed with the `php_execute_script`
336
+ * C function.
337
+ *
338
+ * The `run()` method cannot be used in conjunction with `cli()`.
339
+ *
255
340
  * @example
256
341
  * ```js
257
- * console.log(await php.run(`<?php
342
+ * const result = await php.run(`<?php
258
343
  * $fp = fopen('php://stderr', 'w');
259
344
  * fwrite($fp, "Hello, world!");
260
- * `));
261
- * // {"exitCode":0,"stdout":"","stderr":["Hello, world!"]}
345
+ * `);
346
+ * // result.errors === "Hello, world!"
262
347
  * ```
263
348
  *
264
- * @param options - PHP run options.
349
+ * @param options - PHP runtime options.
265
350
  */
266
351
  run(options: PHPRunOptions): Promise<PHPResponse>;
267
352
  }
@@ -567,8 +652,12 @@ export declare class NodePHP extends BasePHP {
567
652
  /**
568
653
  * Starts a PHP CLI session with given arguments.
569
654
  *
570
- * Can only be used when PHP was compiled with the CLI SAPI.
571
- * Cannot be used in conjunction with `run()`.
655
+ * This method can only be used when PHP was compiled with the CLI SAPI
656
+ * and it cannot be used in conjunction with `run()`.
657
+ *
658
+ * Once this method finishes running, the PHP instance is no
659
+ * longer usable and should be discarded. This is because PHP
660
+ * internally cleans up all the resources and calls exit().
572
661
  *
573
662
  * @param argv - The arguments to pass to the CLI.
574
663
  * @returns The exit code of the CLI session.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@php-wasm/node",
3
- "version": "0.1.45",
3
+ "version": "0.1.49",
4
4
  "description": "PHP.wasm for Node.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,13 +28,13 @@
28
28
  "license": "GPL-2.0-or-later",
29
29
  "main": "index.cjs",
30
30
  "types": "index.d.ts",
31
- "gitHead": "ca75514c17b912dc8b5dc529f6d123295671320a",
31
+ "gitHead": "93834fa77a89c21be2ade4c1d67430981ed92c83",
32
32
  "dependencies": {
33
- "comlink": "4.4.1",
33
+ "comlink": "^4.4.1",
34
34
  "express": "4.18.2",
35
35
  "ws": "8.13.0",
36
36
  "yargs": "17.7.2",
37
- "@php-wasm/universal": "0.1.45",
38
- "@php-wasm/util": "0.1.45"
37
+ "@php-wasm/universal": "0.1.49",
38
+ "@php-wasm/util": "0.1.49"
39
39
  }
40
40
  }