@php-wasm/node 0.1.17 → 0.1.19

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.d.ts CHANGED
@@ -1,190 +1,287 @@
1
1
  // Generated by dts-bundle-generator v7.2.0
2
2
 
3
- export type JavascriptRuntime = "NODE" | "WEB" | "WORKER";
4
- export type PHPRequestHeaders = Record<string, string>;
5
- export interface FileInfo {
6
- key: string;
7
- name: string;
8
- type: string;
9
- data: Uint8Array;
10
- }
11
- export interface PHPRequest {
3
+ export interface PHPResponseData {
12
4
  /**
13
- * Request path following the domain:port part.
5
+ * Response headers.
14
6
  */
15
- relativeUri?: string;
7
+ readonly headers: Record<string, string[]>;
16
8
  /**
17
- * Path of the .php file to execute.
9
+ * Response body. Contains the output from `echo`,
10
+ * `print`, inline HTML etc.
18
11
  */
19
- scriptPath?: string;
12
+ readonly bytes: ArrayBuffer;
20
13
  /**
21
- * Request protocol.
14
+ * Stderr contents, if any.
22
15
  */
23
- protocol?: string;
16
+ readonly errors: string;
24
17
  /**
25
- * Request method. Default: `GET`.
18
+ * The exit code of the script. `0` is a success, while
19
+ * `1` and `2` indicate an error.
26
20
  */
27
- method?: "GET" | "POST" | "HEAD" | "OPTIONS" | "PATCH" | "PUT" | "DELETE";
21
+ readonly exitCode: number;
28
22
  /**
29
- * Request headers.
23
+ * Response HTTP status code, e.g. 200.
30
24
  */
31
- headers?: PHPRequestHeaders;
25
+ readonly httpStatusCode: number;
26
+ }
27
+ /**
28
+ * PHP response. Body is an `ArrayBuffer` because it can
29
+ * contain binary data.
30
+ *
31
+ * This type is used in Comlink.transferHandlers.set('PHPResponse', { ... })
32
+ * so be sure to update that if you change this type.
33
+ */
34
+ export declare class PHPResponse implements PHPResponseData {
35
+ /** @inheritDoc */
36
+ readonly headers: Record<string, string[]>;
37
+ /** @inheritDoc */
38
+ readonly bytes: ArrayBuffer;
39
+ /** @inheritDoc */
40
+ readonly errors: string;
41
+ /** @inheritDoc */
42
+ readonly exitCode: number;
43
+ /** @inheritDoc */
44
+ readonly httpStatusCode: number;
45
+ constructor(httpStatusCode: number, headers: Record<string, string[]>, body: ArrayBuffer, errors?: string, exitCode?: number);
46
+ static fromRawData(data: PHPResponseData): PHPResponse;
47
+ toRawData(): PHPResponseData;
32
48
  /**
33
- * Request body without the files.
49
+ * Response body as JSON.
34
50
  */
35
- body?: string;
51
+ get json(): any;
36
52
  /**
37
- * Uploaded files.
53
+ * Response body as text.
38
54
  */
39
- fileInfos?: FileInfo[];
55
+ get text(): string;
56
+ }
57
+ export type HTTPMethod = "GET" | "POST" | "HEAD" | "OPTIONS" | "PATCH" | "PUT" | "DELETE";
58
+ export type PHPRequestHeaders = Record<string, string>;
59
+ export interface PHPRequest {
40
60
  /**
41
- * The code snippet to eval instead of a php file.
61
+ * Request method. Default: `GET`.
42
62
  */
43
- code?: string;
44
- }
45
- export interface PHPResponse {
63
+ method?: HTTPMethod;
46
64
  /**
47
- * The exit code of the script. `0` is a success, while
48
- * `1` and `2` indicate an error.
65
+ * Request path or absolute URL.
49
66
  */
50
- exitCode: number;
67
+ url: string;
51
68
  /**
52
- * Response body. Contains the output from `echo`,
53
- * `print`, inline HTML etc.
69
+ * Request headers.
54
70
  */
55
- body: ArrayBuffer;
71
+ headers?: PHPRequestHeaders;
56
72
  /**
57
- * PHP errors.
73
+ * Uploaded files
58
74
  */
59
- errors: string;
75
+ files?: Record<string, File>;
60
76
  /**
61
- * Response headers.
77
+ * Request body without the files.
62
78
  */
63
- headers: Record<string, string[]>;
79
+ body?: string;
64
80
  /**
65
- * Response HTTP status code, e.g. 200.
81
+ * Form data. If set, the request body will be ignored and
82
+ * the content-type header will be set to `application/x-www-form-urlencoded`.
66
83
  */
67
- httpStatusCode: number;
84
+ formData?: Record<string, unknown>;
85
+ }
86
+ export interface PHPRequestHandlerConfiguration {
87
+ /**
88
+ * The directory in the PHP filesystem where the server will look
89
+ * for the files to serve. Default: `/var/www`.
90
+ */
91
+ documentRoot?: string;
92
+ /**
93
+ * Request Handler URL. Used to populate $_SERVER details like HTTP_HOST.
94
+ */
95
+ absoluteUrl?: string;
96
+ /**
97
+ * Callback used by the PHPRequestHandler to decide whether
98
+ * the requested path refers to a PHP file or a static file.
99
+ */
100
+ isStaticFilePath?: (path: string) => boolean;
68
101
  }
69
- export type PHPRuntimeId = number;
70
102
  /**
71
- * Loads the PHP runtime with the given arguments and data dependencies.
72
- *
73
- * This function handles the entire PHP initialization pipeline. In particular, it:
74
- *
75
- * * Instantiates the Emscripten PHP module
76
- * * Wires it together with the data dependencies and loads them
77
- * * Ensures is all happens in a correct order
78
- * * Waits until the entire loading sequence is finished
79
- *
80
- * Basic usage:
103
+ * A fake PHP server that handles HTTP requests but does not
104
+ * bind to any port.
81
105
  *
106
+ * @public
107
+ * @example Use PHPRequestHandler implicitly with a new PHP instance:
82
108
  * ```js
83
- * const phpLoaderModule = await getPHPLoaderModule("7.4");
84
- * const php = await loadPHPRuntime( phpLoaderModule );
85
- * console.log(php.run(`<?php echo "Hello, world!"; `));
86
- * // { stdout: ArrayBuffer containing the string "Hello, world!", stderr: [''], exitCode: 0 }
87
- * ```
109
+ * import { PHP } from '@php-wasm/web';
88
110
  *
89
- * **The PHP loader module:**
111
+ * const php = await PHP.load( '7.4', {
112
+ * requestHandler: {
113
+ * // PHP FS path to serve the files from:
114
+ * documentRoot: '/www',
90
115
  *
91
- * In the basic usage example, `phpLoaderModule` is **not** a vanilla Emscripten module. Instead,
92
- * it's an ESM module that wraps the regular Emscripten output and adds some
93
- * extra functionality. It's generated by the Dockerfile shipped with this repo.
94
- * Here's the API it provides:
95
- *
96
- * ```js
97
- * // php.wasm size in bytes:
98
- * export const dependenciesTotalSize = 5644199;
116
+ * // Used to populate $_SERVER['SERVER_NAME'] etc.:
117
+ * absoluteUrl: 'http://127.0.0.1'
118
+ * }
119
+ * } );
99
120
  *
100
- * // php.wasm filename:
101
- * export const dependencyFilename = 'php.wasm';
121
+ * php.mkdirTree('/www');
122
+ * php.writeFile('/www/index.php', '<?php echo "Hi from PHP!"; ');
102
123
  *
103
- * // Run Emscripten's generated module:
104
- * export default function(jsEnv, emscriptenModuleArgs) {}
124
+ * const response = await php.request({ path: '/index.php' });
125
+ * console.log(response.text);
126
+ * // "Hi from PHP!"
105
127
  * ```
106
128
  *
107
- * **PHP Filesystem:**
108
- *
109
- * Once initialized, the PHP has its own filesystem separate from the project
110
- * files. It's provided by [Emscripten and uses its FS library](https://emscripten.org/docs/api_reference/Filesystem-API.html).
111
- *
112
- * The API exposed to you via the PHP class is succinct and abstracts
113
- * await certain unintuitive parts of low-level FS interactions.
114
- *
115
- * Here's how to use it:
116
- *
129
+ * @example Explicitly create a PHPRequestHandler instance and run a PHP script:
117
130
  * ```js
118
- * // Recursively create a /var/www directory
119
- * php.mkdirTree('/var/www');
120
- *
121
- * console.log(php.fileExists('/var/www/file.txt'));
122
- * // false
123
- *
124
- * php.writeFile('/var/www/file.txt', 'Hello from the filesystem!');
125
- *
126
- * console.log(php.fileExists('/var/www/file.txt'));
127
- * // true
128
- *
129
- * console.log(php.readFile('/var/www/file.txt'));
130
- * // "Hello from the filesystem!
131
- *
132
- * // Delete the file:
133
- * php.unlink('/var/www/file.txt');
134
- * ```
135
- *
136
- * For more details consult the PHP class directly.
137
- *
138
- * **Data dependencies:**
139
- *
140
- * Using existing PHP packages by manually recreating them file-by-file would
141
- * be quite inconvenient. Fortunately, Emscripten provides a "data dependencies"
142
- * feature.
143
- *
144
- * Data dependencies consist of a `dependency.data` file and a `dependency.js` loader and
145
- * can be packaged with the [file_packager.py tool]( https://emscripten.org/docs/porting/files/packaging_files.html#packaging-using-the-file-packager-tool).
146
- * This project requires wrapping the Emscripten-generated `dependency.js` file in an ES
147
- * module as follows:
148
- *
149
- * 1. Prepend `export default function(emscriptenPHPModule) {'; `
150
- * 2. Prepend `export const dependencyFilename = '<DATA FILE NAME>'; `
151
- * 3. Prepend `export const dependenciesTotalSize = <DATA FILE SIZE>;`
152
- * 4. Append `}`
153
- *
154
- * Be sure to use the `--export-name="emscriptenPHPModule"` file_packager.py option.
131
+ * import {
132
+ * loadPHPRuntime,
133
+ * PHP,
134
+ * PHPRequestHandler,
135
+ * getPHPLoaderModule,
136
+ * } from '@php-wasm/web';
155
137
  *
156
- * You want the final output to look as follows:
138
+ * const runtime = await loadPHPRuntime( await getPHPLoaderModule('7.4') );
139
+ * const php = new PHP( runtime );
157
140
  *
158
- * ```js
159
- * export const dependenciesTotalSize = 5644199;
160
- * export const dependencyFilename = 'dependency.data';
161
- * export default function(emscriptenPHPModule) {
162
- * // Emscripten-generated code:
163
- * var Module = typeof emscriptenPHPModule !== 'undefined' ? emscriptenPHPModule : {};
164
- * // ... the rest of it ...
165
- * }
166
- * ```
141
+ * php.mkdirTree('/www');
142
+ * php.writeFile('/www/index.php', '<?php echo "Hi from PHP!"; ');
167
143
  *
168
- * Such a constructions enables loading the `dependency.js` as an ES Module using
169
- * `import("/dependency.js")`.
144
+ * const server = new PHPRequestHandler(php, {
145
+ * // PHP FS path to serve the files from:
146
+ * documentRoot: '/www',
170
147
  *
171
- * Once it's ready, you can load PHP and your data dependencies as follows:
148
+ * // Used to populate $_SERVER['SERVER_NAME'] etc.:
149
+ * absoluteUrl: 'http://127.0.0.1'
150
+ * });
172
151
  *
173
- * ```js
174
- * const [phpLoaderModule, wordPressLoaderModule] = await Promise.all([
175
- * getPHPLoaderModule("7.4"),
176
- * import("/wp.js")
177
- * ]);
178
- * const php = await loadPHPRuntime(phpLoaderModule, {}, [wordPressLoaderModule]);
152
+ * const response = server.request({ path: '/index.php' });
153
+ * console.log(response.text);
154
+ * // "Hi from PHP!"
179
155
  * ```
156
+ */
157
+ export declare class PHPRequestHandler {
158
+ #private;
159
+ /**
160
+ * The PHP instance
161
+ */
162
+ php: BasePHP;
163
+ /**
164
+ * @param php - The PHP instance.
165
+ * @param config - Request Handler configuration.
166
+ */
167
+ constructor(php: BasePHP, config?: PHPRequestHandlerConfiguration);
168
+ /**
169
+ * Converts a path to an absolute URL based at the PHPRequestHandler
170
+ * root.
171
+ *
172
+ * @param path The server path to convert to an absolute URL.
173
+ * @returns The absolute URL.
174
+ */
175
+ pathToInternalUrl(path: string): string;
176
+ /**
177
+ * Converts an absolute URL based at the PHPRequestHandler to a relative path
178
+ * without the server pathname and scope.
179
+ *
180
+ * @param internalUrl An absolute URL based at the PHPRequestHandler root.
181
+ * @returns The relative path.
182
+ */
183
+ internalUrlToPath(internalUrl: string): string;
184
+ get isRequestRunning(): boolean;
185
+ /**
186
+ * The absolute URL of this PHPRequestHandler instance.
187
+ */
188
+ get absoluteUrl(): string;
189
+ /**
190
+ * The absolute URL of this PHPRequestHandler instance.
191
+ */
192
+ get documentRoot(): string;
193
+ /**
194
+ * Serves the request – either by serving a static file, or by
195
+ * dispatching it to the PHP runtime.
196
+ *
197
+ * @param request - The request.
198
+ * @returns The response.
199
+ */
200
+ request(request: PHPRequest): Promise<PHPResponse>;
201
+ }
202
+ export interface PHPBrowserConfiguration {
203
+ /**
204
+ * Should handle redirects internally?
205
+ */
206
+ handleRedirects?: boolean;
207
+ /**
208
+ * The maximum number of redirects to follow internally. Once
209
+ * exceeded, request() will return the redirecting response.
210
+ */
211
+ maxRedirects?: number;
212
+ }
213
+ /**
214
+ * A fake web browser that handles PHPRequestHandler's cookies and redirects
215
+ * internally without exposing them to the consumer.
180
216
  *
181
217
  * @public
182
- * @param phpLoaderModule - The ESM-wrapped Emscripten module. Consult the Dockerfile for the build process.
183
- * @param phpModuleArgs - The Emscripten module arguments, see https://emscripten.org/docs/api_reference/module.html#affecting-execution.
184
- * @param dataDependenciesModules - A list of the ESM-wrapped Emscripten data dependency modules.
185
- * @returns Loaded runtime id.
186
218
  */
187
- export declare function loadPHPRuntime(phpLoaderModule: PHPLoaderModule, phpModuleArgs?: EmscriptenOptions, dataDependenciesModules?: DataModule[]): Promise<number>;
219
+ export declare class PHPBrowser implements WithRequestHandler {
220
+ #private;
221
+ server: PHPRequestHandler;
222
+ /**
223
+ * @param server - The PHP server to browse.
224
+ * @param config - The browser configuration.
225
+ */
226
+ constructor(server: PHPRequestHandler, config?: PHPBrowserConfiguration);
227
+ /**
228
+ * Sends the request to the server.
229
+ *
230
+ * When cookies are present in the response, this method stores
231
+ * them and sends them with any subsequent requests.
232
+ *
233
+ * When a redirection is present in the response, this method
234
+ * follows it by discarding a response and sending a subsequent
235
+ * request.
236
+ *
237
+ * @param request - The request.
238
+ * @param redirects - Internal. The number of redirects handled so far.
239
+ * @returns PHPRequestHandler response.
240
+ */
241
+ request(request: PHPRequest, redirects?: number): Promise<PHPResponse>;
242
+ }
243
+ export type RuntimeType = "NODE" | "WEB" | "WORKER";
244
+ export interface FileInfo {
245
+ key: string;
246
+ name: string;
247
+ type: string;
248
+ data: Uint8Array;
249
+ }
250
+ export interface PHPRunOptions {
251
+ /**
252
+ * Request path following the domain:port part.
253
+ */
254
+ relativeUri?: string;
255
+ /**
256
+ * Path of the .php file to execute.
257
+ */
258
+ scriptPath?: string;
259
+ /**
260
+ * Request protocol.
261
+ */
262
+ protocol?: string;
263
+ /**
264
+ * Request method. Default: `GET`.
265
+ */
266
+ method?: HTTPMethod;
267
+ /**
268
+ * Request headers.
269
+ */
270
+ headers?: PHPRequestHeaders;
271
+ /**
272
+ * Request body without the files.
273
+ */
274
+ body?: string;
275
+ /**
276
+ * Uploaded files.
277
+ */
278
+ fileInfos?: FileInfo[];
279
+ /**
280
+ * The code snippet to eval instead of a php file.
281
+ */
282
+ code?: string;
283
+ }
284
+ export type PHPRuntimeId = number;
188
285
  export interface WithPHPIniBindings {
189
286
  setPhpIniPath(path: string): void;
190
287
  setPhpIniEntry(key: string, value: string): void;
@@ -272,21 +369,30 @@ export interface WithFilesystem {
272
369
  * @returns True if the file exists, false otherwise.
273
370
  */
274
371
  fileExists(path: string): boolean;
372
+ /**
373
+ * Changes the current working directory in the PHP filesystem.
374
+ * This is the directory that will be used as the base for relative paths.
375
+ * For example, if the current working directory is `/root/php`, and the
376
+ * path is `data`, the absolute path will be `/root/php/data`.
377
+ *
378
+ * @param path - The new working directory.
379
+ */
380
+ chdir(path: string): void;
275
381
  }
276
382
  export interface WithRun {
277
383
  /**
278
- * Dispatches a PHP request.
384
+ * Runs PHP code.
279
385
  * Cannot be used in conjunction with `cli()`.
280
386
  *
281
387
  * @example
282
388
  * ```js
283
- * const output = php.run('<?php echo "Hello world!";');
389
+ * const output = await php.run('<?php echo "Hello world!";');
284
390
  * console.log(output.stdout); // "Hello world!"
285
391
  * ```
286
392
  *
287
393
  * @example
288
394
  * ```js
289
- * console.log(php.run(`<?php
395
+ * console.log(await php.run(`<?php
290
396
  * $fp = fopen('php://stderr', 'w');
291
397
  * fwrite($fp, "Hello, world!");
292
398
  * `));
@@ -295,13 +401,37 @@ export interface WithRun {
295
401
  *
296
402
  * @param request - PHP Request data.
297
403
  */
298
- run(request?: PHPRequest): PHPResponse;
404
+ run(request?: PHPRunOptions): Promise<PHPResponse>;
405
+ }
406
+ export interface WithRequestHandler {
407
+ /**
408
+ * Dispatches a HTTP request using PHP as a backend.
409
+ * Cannot be used in conjunction with `cli()`.
410
+ *
411
+ * @example
412
+ * ```js
413
+ * const output = await php.request({
414
+ * method: 'GET',
415
+ * url: '/index.php',
416
+ * headers: {
417
+ * 'X-foo': 'bar',
418
+ * },
419
+ * formData: {
420
+ * foo: 'bar',
421
+ * },
422
+ * });
423
+ * console.log(output.stdout); // "Hello world!"
424
+ * ```
425
+ *
426
+ * @param request - PHP Request data.
427
+ */
428
+ request(request?: PHPRequest): Promise<PHPResponse>;
299
429
  }
300
430
  export type PHPRuntime = any;
301
431
  export type PHPLoaderModule = {
302
432
  dependencyFilename: string;
303
433
  dependenciesTotalSize: number;
304
- default: (jsRuntime: string, options: EmscriptenOptions) => PHPRuntime;
434
+ init: (jsRuntime: string, options: EmscriptenOptions) => PHPRuntime;
305
435
  };
306
436
  export type DataModule = {
307
437
  dependencyFilename: string;
@@ -321,39 +451,35 @@ export type EmscriptenOptions = {
321
451
  } & Record<string, any>;
322
452
  export type MountSettings = {
323
453
  root: string;
324
- mountpoint?: string;
325
454
  };
326
- /**
327
- * An environment-agnostic wrapper around the Emscripten PHP runtime
328
- * that abstracts the super low-level API and provides a more convenient
329
- * higher-level API.
330
- *
331
- * It exposes a minimal set of methods to run PHP scripts and to
332
- * interact with the PHP filesystem.
333
- *
334
- * @see {startPHP} This class is not meant to be used directly. Use `startPHP` instead.
335
- */
336
- export declare class PHP implements WithPHPIniBindings, WithFilesystem, WithNodeFilesystem, WithCLI, WithRun {
455
+ declare abstract class BasePHP implements WithPHPIniBindings, WithFilesystem, WithNodeFilesystem, WithCLI, WithRequestHandler, WithRun {
337
456
  #private;
457
+ requestHandler?: PHPBrowser;
338
458
  /**
339
459
  * Initializes a PHP runtime.
340
460
  *
341
461
  * @internal
342
462
  * @param PHPRuntime - Optional. PHP Runtime ID as initialized by loadPHPRuntime.
463
+ * @param serverOptions - Optional. Options for the PHPRequestHandler. If undefined, no request handler will be initialized.
343
464
  */
344
- constructor(PHPRuntimeId?: PHPRuntimeId);
465
+ constructor(PHPRuntimeId?: PHPRuntimeId, serverOptions?: PHPRequestHandlerConfiguration);
345
466
  initializeRuntime(runtimeId: PHPRuntimeId): void;
346
467
  /** @inheritDoc */
347
468
  setPhpIniPath(path: string): void;
348
469
  /** @inheritDoc */
349
470
  setPhpIniEntry(key: string, value: string): void;
471
+ /** @inheritDoc */
350
472
  chdir(path: string): void;
351
473
  /** @inheritDoc */
352
- run(request?: PHPRequest): PHPResponse;
474
+ request(request: PHPRequest, maxRedirects?: number): Promise<PHPResponse>;
475
+ /** @inheritDoc */
476
+ run(request?: PHPRunOptions): Promise<PHPResponse>;
353
477
  cli(argv: string[]): Promise<number>;
354
478
  setSkipShebang(shouldSkip: boolean): void;
355
479
  addServerGlobalEntry(key: string, value: string): void;
480
+ /** @inheritDoc */
356
481
  mkdirTree(path: string): void;
482
+ /** @inheritDoc */
357
483
  readFileAsText(path: string): string;
358
484
  /** @inheritDoc */
359
485
  readFileAsBuffer(path: string): Uint8Array;
@@ -363,8 +489,11 @@ export declare class PHP implements WithPHPIniBindings, WithFilesystem, WithNode
363
489
  unlink(path: string): void;
364
490
  /** @inheritDoc */
365
491
  listFiles(path: string): string[];
492
+ /** @inheritDoc */
366
493
  isDir(path: string): boolean;
494
+ /** @inheritDoc */
367
495
  fileExists(path: string): boolean;
496
+ /** @inheritDoc */
368
497
  mount(settings: MountSettings, path: string): void;
369
498
  }
370
499
  /**
@@ -383,162 +512,68 @@ export interface PHPOutput {
383
512
  *
384
513
  * @see https://emscripten.org/docs/api_reference/Filesystem-API.html
385
514
  * @see https://github.com/emscripten-core/emscripten/blob/main/system/lib/libc/musl/arch/emscripten/bits/errno.h
515
+ * @see https://github.com/emscripten-core/emscripten/blob/38eedc630f17094b3202fd48ac0c2c585dbea31e/system/include/wasi/api.h#L336
386
516
  */
387
- export type ErrnoError = Error;
388
- export type PHPServerRequest = Pick<PHPRequest, "method" | "headers"> & {
389
- files?: Record<string, File>;
390
- } & ({
391
- absoluteUrl: string;
392
- relativeUrl?: never;
393
- } | {
394
- absoluteUrl?: never;
395
- relativeUrl: string;
396
- }) & ((Pick<PHPRequest, "body"> & {
397
- formData?: never;
398
- }) | {
399
- body?: never;
400
- formData: Record<string, unknown>;
401
- });
402
- /**
403
- * A fake PHP server that handles HTTP requests but does not
404
- * bind to any port.
405
- *
406
- * @public
407
- * @example
408
- * ```js
409
- * import {
410
- * loadPHPRuntime,
411
- * PHP,
412
- * PHPServer,
413
- * PHPBrowser,
414
- * getPHPLoaderModule,
415
- * } from '@php-wasm/web';
416
- *
417
- * const runtime = await loadPHPRuntime( await getPHPLoaderModule('7.4') );
418
- * const php = new PHP( runtime );
419
- *
420
- * php.mkdirTree('/www');
421
- * php.writeFile('/www/index.php', '<?php echo "Hi from PHP!"; ');
422
- *
423
- * const server = new PHPServer(php, {
424
- * // PHP FS path to serve the files from:
425
- * documentRoot: '/www',
426
- *
427
- * // Used to populate $_SERVER['SERVER_NAME'] etc.:
428
- * absoluteUrl: 'http://127.0.0.1'
429
- * });
430
- *
431
- * const output = server.request({ path: '/index.php' }).body;
432
- * console.log(new TextDecoder().decode(output));
433
- * // "Hi from PHP!"
434
- * ```
435
- */
436
- export declare class PHPServer {
437
- #private;
438
- /**
439
- * The PHP instance
440
- */
441
- php: PHP;
442
- /**
443
- * @param php - The PHP instance.
444
- * @param config - Server configuration.
445
- */
446
- constructor(php: PHP, config?: PHPServerConfigation);
447
- /**
448
- * Converts a path to an absolute URL based at the PHPServer
449
- * root.
450
- *
451
- * @param path The server path to convert to an absolute URL.
452
- * @returns The absolute URL.
453
- */
454
- pathToInternalUrl(path: string): string;
455
- /**
456
- * Converts an absolute URL based at the PHPServer to a relative path
457
- * without the server pathname and scope.
458
- *
459
- * @param internalUrl An absolute URL based at the PHPServer root.
460
- * @returns The relative path.
461
- */
462
- internalUrlToPath(internalUrl: string): string;
463
- /**
464
- * The absolute URL of this PHPServer instance.
465
- */
466
- get absoluteUrl(): string;
467
- /**
468
- * The absolute URL of this PHPServer instance.
469
- */
470
- get documentRoot(): string;
471
- /**
472
- * Serves the request – either by serving a static file, or by
473
- * dispatching it to the PHP runtime.
474
- *
475
- * @param request - The request.
476
- * @returns The response.
477
- */
478
- request(request: PHPServerRequest): Promise<PHPResponse>;
517
+ export interface ErrnoError extends Error {
518
+ node?: any;
519
+ errno: number;
520
+ message: string;
479
521
  }
480
- export interface PHPServerConfigation {
481
- /**
482
- * The directory in the PHP filesystem where the server will look
483
- * for the files to serve. Default: `/var/www`.
484
- */
485
- documentRoot?: string;
486
- /**
487
- * Server URL. Used to populate $_SERVER details like HTTP_HOST.
488
- */
489
- absoluteUrl?: string;
490
- /**
491
- * Callback used by the PHPServer to decide whether
492
- * the requested path refers to a PHP file or a static file.
493
- */
494
- isStaticFilePath?: (path: string) => boolean;
522
+ export declare const SupportedPHPVersions: readonly [
523
+ "8.2",
524
+ "8.1",
525
+ "8.0",
526
+ "7.4",
527
+ "7.3",
528
+ "7.2",
529
+ "7.1",
530
+ "7.0",
531
+ "5.6"
532
+ ];
533
+ export declare const LatestSupportedPHPVersion: "8.2";
534
+ export declare const SupportedPHPVersionsList: string[];
535
+ export type SupportedPHPVersion = (typeof SupportedPHPVersions)[number];
536
+ export type WorkerStartupOptions<T extends Record<string, string> = Record<string, string>> = T;
537
+ export declare function getPHPLoaderModule(version?: SupportedPHPVersion): Promise<PHPLoaderModule>;
538
+ export declare function parseWorkerStartupOptions(): WorkerStartupOptions;
539
+ export declare function withNetworking(phpModuleArgs?: EmscriptenOptions): Promise<EmscriptenOptions>;
540
+ export interface PHPLoaderOptions {
541
+ emscriptenOptions?: EmscriptenOptions;
542
+ requestHandler?: PHPRequestHandlerConfiguration;
495
543
  }
496
- export interface WithRequest {
544
+ export declare class PHP extends BasePHP {
497
545
  /**
498
- * Sends the request to the server.
546
+ * Creates a new PHP instance.
499
547
  *
500
- * When cookies are present in the response, this method stores
501
- * them and sends them with any subsequent requests.
548
+ * Dynamically imports the PHP module, initializes the runtime,
549
+ * and sets up networking. It's a shorthand for the lower-level
550
+ * functions like `getPHPLoaderModule`, `loadPHPRuntime`, and
551
+ * `PHP.initializeRuntime`
502
552
  *
503
- * When a redirection is present in the response, this method
504
- * follows it by discarding a response and sending a subsequent
505
- * request.
506
- *
507
- * @param request - The request.
508
- * @param redirects - Internal. The number of redirects handled so far.
509
- * @returns PHPServer response.
510
- */
511
- request(request: PHPServerRequest, redirects?: number): Promise<PHPResponse>;
512
- }
513
- /**
514
- * A fake web browser that handles PHPServer's cookies and redirects
515
- * internally without exposing them to the consumer.
516
- *
517
- * @public
518
- */
519
- export declare class PHPBrowser implements WithRequest {
520
- #private;
521
- server: PHPServer;
522
- /**
523
- * @param server - The PHP server to browse.
524
- * @param config - The browser configuration.
553
+ * @param phpVersion The PHP Version to load
554
+ * @param options The options to use when loading PHP
555
+ * @returns A new PHP instance
525
556
  */
526
- constructor(server: PHPServer, config?: PHPBrowserConfiguration);
527
- request(request: PHPServerRequest, redirects?: number): Promise<PHPResponse>;
528
- }
529
- export interface PHPBrowserConfiguration {
557
+ static load(phpVersion: SupportedPHPVersion, options?: PHPLoaderOptions): Promise<PHP>;
530
558
  /**
531
- * Should handle redirects internally?
559
+ * Does what load() does, but synchronously returns
560
+ * an object with the PHP instance and a promise that
561
+ * resolves when the PHP instance is ready.
562
+ *
563
+ * @see load
532
564
  */
533
- handleRedirects?: boolean;
565
+ static loadSync(phpVersion: SupportedPHPVersion, options?: PHPLoaderOptions): {
566
+ php: PHP;
567
+ phpReady: Promise<PHP>;
568
+ };
534
569
  /**
535
- * The maximum number of redirects to follow internally. Once
536
- * exceeded, request() will return the redirecting response.
570
+ * Enables host filesystem usage by mounting root
571
+ * directories (e.g. /, /home, /var) into the in-memory
572
+ * virtual filesystem used by this PHP instance, and
573
+ * setting the current working directory to one used by
574
+ * the current node.js process.
537
575
  */
538
- maxRedirects?: number;
576
+ useHostFilesystem(): void;
539
577
  }
540
- export type WorkerStartupOptions<T extends Record<string, string> = Record<string, string>> = T;
541
- export declare function getPHPLoaderModule(version?: string): Promise<PHPLoaderModule>;
542
- export declare function parseWorkerStartupOptions(): WorkerStartupOptions;
543
578
 
544
579
  export {};