@php-wasm/web 0.0.1
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/.htaccess +5 -0
- package/iframe-worker.html +18 -0
- package/index-9258d089.js +1054 -0
- package/index.d.ts +644 -0
- package/lib/worker-thread/parse-startup-options.d.ts +2 -0
- package/lib/worker-thread/spawn-php-worker-thread.d.ts +16 -0
- package/package.json +28 -0
- package/php.js.bak +3 -0
- package/php_5_6.js +5 -0
- package/php_5_6.wasm +0 -0
- package/php_7_0.js +5 -0
- package/php_7_0.wasm +0 -0
- package/php_7_1.js +5 -0
- package/php_7_1.wasm +0 -0
- package/php_7_2.js +5 -0
- package/php_7_2.wasm +0 -0
- package/php_7_3.js +5 -0
- package/php_7_3.wasm +0 -0
- package/php_7_4.js +5 -0
- package/php_7_4.wasm +0 -0
- package/php_8_0.js +5 -0
- package/php_8_0.wasm +0 -0
- package/php_8_1.js +5 -0
- package/php_8_1.wasm +0 -0
- package/php_8_2.js +5 -0
- package/php_8_2.wasm +0 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
// Generated by dts-bundle-generator v7.2.0
|
|
2
|
+
|
|
3
|
+
import * as Comlink from 'comlink';
|
|
4
|
+
|
|
5
|
+
export type JavascriptRuntime = "NODE" | "WEB" | "WORKER";
|
|
6
|
+
export type PHPRequestHeaders = Record<string, string>;
|
|
7
|
+
export interface FileInfo {
|
|
8
|
+
key: string;
|
|
9
|
+
name: string;
|
|
10
|
+
type: string;
|
|
11
|
+
data: Uint8Array;
|
|
12
|
+
}
|
|
13
|
+
export interface PHPRequest {
|
|
14
|
+
/**
|
|
15
|
+
* Request path following the domain:port part.
|
|
16
|
+
*/
|
|
17
|
+
relativeUri?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Path of the .php file to execute.
|
|
20
|
+
*/
|
|
21
|
+
scriptPath?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Request protocol.
|
|
24
|
+
*/
|
|
25
|
+
protocol?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Request method. Default: `GET`.
|
|
28
|
+
*/
|
|
29
|
+
method?: "GET" | "POST" | "HEAD" | "OPTIONS" | "PATCH" | "PUT" | "DELETE";
|
|
30
|
+
/**
|
|
31
|
+
* Request headers.
|
|
32
|
+
*/
|
|
33
|
+
headers?: PHPRequestHeaders;
|
|
34
|
+
/**
|
|
35
|
+
* Request body without the files.
|
|
36
|
+
*/
|
|
37
|
+
body?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Uploaded files.
|
|
40
|
+
*/
|
|
41
|
+
fileInfos?: FileInfo[];
|
|
42
|
+
/**
|
|
43
|
+
* The code snippet to eval instead of a php file.
|
|
44
|
+
*/
|
|
45
|
+
code?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface PHPResponse {
|
|
48
|
+
/**
|
|
49
|
+
* The exit code of the script. `0` is a success, while
|
|
50
|
+
* `1` and `2` indicate an error.
|
|
51
|
+
*/
|
|
52
|
+
exitCode: number;
|
|
53
|
+
/**
|
|
54
|
+
* Response body. Contains the output from `echo`,
|
|
55
|
+
* `print`, inline HTML etc.
|
|
56
|
+
*/
|
|
57
|
+
body: ArrayBuffer;
|
|
58
|
+
/**
|
|
59
|
+
* PHP errors.
|
|
60
|
+
*/
|
|
61
|
+
errors: string;
|
|
62
|
+
/**
|
|
63
|
+
* Response headers.
|
|
64
|
+
*/
|
|
65
|
+
headers: Record<string, string[]>;
|
|
66
|
+
/**
|
|
67
|
+
* Response HTTP status code, e.g. 200.
|
|
68
|
+
*/
|
|
69
|
+
httpStatusCode: number;
|
|
70
|
+
}
|
|
71
|
+
export type PHPRuntimeId = number;
|
|
72
|
+
/**
|
|
73
|
+
* Loads the PHP runtime with the given arguments and data dependencies.
|
|
74
|
+
*
|
|
75
|
+
* This function handles the entire PHP initialization pipeline. In particular, it:
|
|
76
|
+
*
|
|
77
|
+
* * Instantiates the Emscripten PHP module
|
|
78
|
+
* * Wires it together with the data dependencies and loads them
|
|
79
|
+
* * Ensures is all happens in a correct order
|
|
80
|
+
* * Waits until the entire loading sequence is finished
|
|
81
|
+
*
|
|
82
|
+
* Basic usage:
|
|
83
|
+
*
|
|
84
|
+
* ```js
|
|
85
|
+
* const phpLoaderModule = await getPHPLoaderModule("7.4");
|
|
86
|
+
* const php = await loadPHPRuntime( phpLoaderModule );
|
|
87
|
+
* console.log(php.run(`<?php echo "Hello, world!"; `));
|
|
88
|
+
* // { stdout: ArrayBuffer containing the string "Hello, world!", stderr: [''], exitCode: 0 }
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* **The PHP loader module:**
|
|
92
|
+
*
|
|
93
|
+
* In the basic usage example, `phpLoaderModule` is **not** a vanilla Emscripten module. Instead,
|
|
94
|
+
* it's an ESM module that wraps the regular Emscripten output and adds some
|
|
95
|
+
* extra functionality. It's generated by the Dockerfile shipped with this repo.
|
|
96
|
+
* Here's the API it provides:
|
|
97
|
+
*
|
|
98
|
+
* ```js
|
|
99
|
+
* // php.wasm size in bytes:
|
|
100
|
+
* export const dependenciesTotalSize = 5644199;
|
|
101
|
+
*
|
|
102
|
+
* // php.wasm filename:
|
|
103
|
+
* export const dependencyFilename = 'php.wasm';
|
|
104
|
+
*
|
|
105
|
+
* // Run Emscripten's generated module:
|
|
106
|
+
* export default function(jsEnv, emscriptenModuleArgs) {}
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* **PHP Filesystem:**
|
|
110
|
+
*
|
|
111
|
+
* Once initialized, the PHP has its own filesystem separate from the project
|
|
112
|
+
* files. It's provided by [Emscripten and uses its FS library](https://emscripten.org/docs/api_reference/Filesystem-API.html).
|
|
113
|
+
*
|
|
114
|
+
* The API exposed to you via the PHP class is succinct and abstracts
|
|
115
|
+
* await certain unintuitive parts of low-level FS interactions.
|
|
116
|
+
*
|
|
117
|
+
* Here's how to use it:
|
|
118
|
+
*
|
|
119
|
+
* ```js
|
|
120
|
+
* // Recursively create a /var/www directory
|
|
121
|
+
* php.mkdirTree('/var/www');
|
|
122
|
+
*
|
|
123
|
+
* console.log(php.fileExists('/var/www/file.txt'));
|
|
124
|
+
* // false
|
|
125
|
+
*
|
|
126
|
+
* php.writeFile('/var/www/file.txt', 'Hello from the filesystem!');
|
|
127
|
+
*
|
|
128
|
+
* console.log(php.fileExists('/var/www/file.txt'));
|
|
129
|
+
* // true
|
|
130
|
+
*
|
|
131
|
+
* console.log(php.readFile('/var/www/file.txt'));
|
|
132
|
+
* // "Hello from the filesystem!
|
|
133
|
+
*
|
|
134
|
+
* // Delete the file:
|
|
135
|
+
* php.unlink('/var/www/file.txt');
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* For more details consult the PHP class directly.
|
|
139
|
+
*
|
|
140
|
+
* **Data dependencies:**
|
|
141
|
+
*
|
|
142
|
+
* Using existing PHP packages by manually recreating them file-by-file would
|
|
143
|
+
* be quite inconvenient. Fortunately, Emscripten provides a "data dependencies"
|
|
144
|
+
* feature.
|
|
145
|
+
*
|
|
146
|
+
* Data dependencies consist of a `dependency.data` file and a `dependency.js` loader and
|
|
147
|
+
* can be packaged with the [file_packager.py tool]( https://emscripten.org/docs/porting/files/packaging_files.html#packaging-using-the-file-packager-tool).
|
|
148
|
+
* This project requires wrapping the Emscripten-generated `dependency.js` file in an ES
|
|
149
|
+
* module as follows:
|
|
150
|
+
*
|
|
151
|
+
* 1. Prepend `export default function(emscriptenPHPModule) {'; `
|
|
152
|
+
* 2. Prepend `export const dependencyFilename = '<DATA FILE NAME>'; `
|
|
153
|
+
* 3. Prepend `export const dependenciesTotalSize = <DATA FILE SIZE>;`
|
|
154
|
+
* 4. Append `}`
|
|
155
|
+
*
|
|
156
|
+
* Be sure to use the `--export-name="emscriptenPHPModule"` file_packager.py option.
|
|
157
|
+
*
|
|
158
|
+
* You want the final output to look as follows:
|
|
159
|
+
*
|
|
160
|
+
* ```js
|
|
161
|
+
* export const dependenciesTotalSize = 5644199;
|
|
162
|
+
* export const dependencyFilename = 'dependency.data';
|
|
163
|
+
* export default function(emscriptenPHPModule) {
|
|
164
|
+
* // Emscripten-generated code:
|
|
165
|
+
* var Module = typeof emscriptenPHPModule !== 'undefined' ? emscriptenPHPModule : {};
|
|
166
|
+
* // ... the rest of it ...
|
|
167
|
+
* }
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* Such a constructions enables loading the `dependency.js` as an ES Module using
|
|
171
|
+
* `import("/dependency.js")`.
|
|
172
|
+
*
|
|
173
|
+
* Once it's ready, you can load PHP and your data dependencies as follows:
|
|
174
|
+
*
|
|
175
|
+
* ```js
|
|
176
|
+
* const [phpLoaderModule, wordPressLoaderModule] = await Promise.all([
|
|
177
|
+
* getPHPLoaderModule("7.4"),
|
|
178
|
+
* import("/wp.js")
|
|
179
|
+
* ]);
|
|
180
|
+
* const php = await loadPHPRuntime(phpLoaderModule, {}, [wordPressLoaderModule]);
|
|
181
|
+
* ```
|
|
182
|
+
*
|
|
183
|
+
* @public
|
|
184
|
+
* @param phpLoaderModule - The ESM-wrapped Emscripten module. Consult the Dockerfile for the build process.
|
|
185
|
+
* @param phpModuleArgs - The Emscripten module arguments, see https://emscripten.org/docs/api_reference/module.html#affecting-execution.
|
|
186
|
+
* @param dataDependenciesModules - A list of the ESM-wrapped Emscripten data dependency modules.
|
|
187
|
+
* @returns Loaded runtime id.
|
|
188
|
+
*/
|
|
189
|
+
export declare function loadPHPRuntime(phpLoaderModule: PHPLoaderModule, phpModuleArgs?: EmscriptenOptions, dataDependenciesModules?: DataModule[]): Promise<number>;
|
|
190
|
+
export interface WithPHPIniBindings {
|
|
191
|
+
setPhpIniPath(path: string): void;
|
|
192
|
+
setPhpIniEntry(key: string, value: string): void;
|
|
193
|
+
}
|
|
194
|
+
export interface WithCLI {
|
|
195
|
+
/**
|
|
196
|
+
* Starts a PHP CLI session with given arguments.
|
|
197
|
+
*
|
|
198
|
+
* Can only be used when PHP was compiled with the CLI SAPI.
|
|
199
|
+
* Cannot be used in conjunction with `run()`.
|
|
200
|
+
*
|
|
201
|
+
* @param argv - The arguments to pass to the CLI.
|
|
202
|
+
* @returns The exit code of the CLI session.
|
|
203
|
+
*/
|
|
204
|
+
cli(argv: string[]): Promise<number>;
|
|
205
|
+
}
|
|
206
|
+
export interface WithNodeFilesystem {
|
|
207
|
+
/**
|
|
208
|
+
* Mounts a Node.js filesystem to a given path in the PHP filesystem.
|
|
209
|
+
*
|
|
210
|
+
* @param settings - The Node.js filesystem settings.
|
|
211
|
+
* @param path - The path to mount the filesystem to.
|
|
212
|
+
* @see {@link https://emscripten.org/docs/api_reference/Filesystem-API.html#FS.mount}
|
|
213
|
+
*/
|
|
214
|
+
mount(settings: any, path: string): void;
|
|
215
|
+
}
|
|
216
|
+
export interface WithFilesystem {
|
|
217
|
+
/**
|
|
218
|
+
* Recursively creates a directory with the given path in the PHP filesystem.
|
|
219
|
+
* For example, if the path is `/root/php/data`, and `/root` already exists,
|
|
220
|
+
* it will create the directories `/root/php` and `/root/php/data`.
|
|
221
|
+
*
|
|
222
|
+
* @param path - The directory path to create.
|
|
223
|
+
*/
|
|
224
|
+
mkdirTree(path: string): void;
|
|
225
|
+
/**
|
|
226
|
+
* Reads a file from the PHP filesystem and returns it as a string.
|
|
227
|
+
*
|
|
228
|
+
* @throws {@link ErrnoError} – If the file doesn't exist.
|
|
229
|
+
* @param path - The file path to read.
|
|
230
|
+
* @returns The file contents.
|
|
231
|
+
*/
|
|
232
|
+
readFileAsText(path: string): string;
|
|
233
|
+
/**
|
|
234
|
+
* Reads a file from the PHP filesystem and returns it as an array buffer.
|
|
235
|
+
*
|
|
236
|
+
* @throws {@link ErrnoError} – If the file doesn't exist.
|
|
237
|
+
* @param path - The file path to read.
|
|
238
|
+
* @returns The file contents.
|
|
239
|
+
*/
|
|
240
|
+
readFileAsBuffer(path: string): Uint8Array;
|
|
241
|
+
/**
|
|
242
|
+
* Overwrites data in a file in the PHP filesystem.
|
|
243
|
+
* Creates a new file if one doesn't exist yet.
|
|
244
|
+
*
|
|
245
|
+
* @param path - The file path to write to.
|
|
246
|
+
* @param data - The data to write to the file.
|
|
247
|
+
*/
|
|
248
|
+
writeFile(path: string, data: string | Uint8Array): void;
|
|
249
|
+
/**
|
|
250
|
+
* Removes a file from the PHP filesystem.
|
|
251
|
+
*
|
|
252
|
+
* @throws {@link ErrnoError} – If the file doesn't exist.
|
|
253
|
+
* @param path - The file path to remove.
|
|
254
|
+
*/
|
|
255
|
+
unlink(path: string): void;
|
|
256
|
+
/**
|
|
257
|
+
* Lists the files and directories in the given directory.
|
|
258
|
+
*
|
|
259
|
+
* @param path - The directory path to list.
|
|
260
|
+
* @returns The list of files and directories in the given directory.
|
|
261
|
+
*/
|
|
262
|
+
listFiles(path: string): string[];
|
|
263
|
+
/**
|
|
264
|
+
* Checks if a directory exists in the PHP filesystem.
|
|
265
|
+
*
|
|
266
|
+
* @param path – The path to check.
|
|
267
|
+
* @returns True if the path is a directory, false otherwise.
|
|
268
|
+
*/
|
|
269
|
+
isDir(path: string): boolean;
|
|
270
|
+
/**
|
|
271
|
+
* Checks if a file (or a directory) exists in the PHP filesystem.
|
|
272
|
+
*
|
|
273
|
+
* @param path - The file path to check.
|
|
274
|
+
* @returns True if the file exists, false otherwise.
|
|
275
|
+
*/
|
|
276
|
+
fileExists(path: string): boolean;
|
|
277
|
+
}
|
|
278
|
+
export interface WithRun {
|
|
279
|
+
/**
|
|
280
|
+
* Dispatches a PHP request.
|
|
281
|
+
* Cannot be used in conjunction with `cli()`.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```js
|
|
285
|
+
* const output = php.run('<?php echo "Hello world!";');
|
|
286
|
+
* console.log(output.stdout); // "Hello world!"
|
|
287
|
+
* ```
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```js
|
|
291
|
+
* console.log(php.run(`<?php
|
|
292
|
+
* $fp = fopen('php://stderr', 'w');
|
|
293
|
+
* fwrite($fp, "Hello, world!");
|
|
294
|
+
* `));
|
|
295
|
+
* // {"exitCode":0,"stdout":"","stderr":["Hello, world!"]}
|
|
296
|
+
* ```
|
|
297
|
+
*
|
|
298
|
+
* @param request - PHP Request data.
|
|
299
|
+
*/
|
|
300
|
+
run(request?: PHPRequest): PHPResponse;
|
|
301
|
+
}
|
|
302
|
+
export type PHPRuntime = any;
|
|
303
|
+
export type PHPLoaderModule = {
|
|
304
|
+
dependencyFilename: string;
|
|
305
|
+
dependenciesTotalSize: number;
|
|
306
|
+
default: (jsRuntime: string, options: EmscriptenOptions) => PHPRuntime;
|
|
307
|
+
};
|
|
308
|
+
export type DataModule = {
|
|
309
|
+
dependencyFilename: string;
|
|
310
|
+
dependenciesTotalSize: number;
|
|
311
|
+
default: (phpRuntime: PHPRuntime) => void;
|
|
312
|
+
};
|
|
313
|
+
export type EmscriptenOptions = {
|
|
314
|
+
onAbort?: (message: string) => void;
|
|
315
|
+
ENV?: Record<string, string>;
|
|
316
|
+
locateFile?: (path: string) => string;
|
|
317
|
+
noInitialRun?: boolean;
|
|
318
|
+
dataFileDownloads?: Record<string, number>;
|
|
319
|
+
print?: (message: string) => void;
|
|
320
|
+
printErr?: (message: string) => void;
|
|
321
|
+
onRuntimeInitialized?: () => void;
|
|
322
|
+
monitorRunDependencies?: (left: number) => void;
|
|
323
|
+
} & Record<string, any>;
|
|
324
|
+
export type MountSettings = {
|
|
325
|
+
root: string;
|
|
326
|
+
mountpoint: string;
|
|
327
|
+
};
|
|
328
|
+
/**
|
|
329
|
+
* An environment-agnostic wrapper around the Emscripten PHP runtime
|
|
330
|
+
* that abstracts the super low-level API and provides a more convenient
|
|
331
|
+
* higher-level API.
|
|
332
|
+
*
|
|
333
|
+
* It exposes a minimal set of methods to run PHP scripts and to
|
|
334
|
+
* interact with the PHP filesystem.
|
|
335
|
+
*
|
|
336
|
+
* @see {startPHP} This class is not meant to be used directly. Use `startPHP` instead.
|
|
337
|
+
*/
|
|
338
|
+
export declare class PHP implements WithPHPIniBindings, WithFilesystem, WithNodeFilesystem, WithCLI, WithRun {
|
|
339
|
+
#private;
|
|
340
|
+
/**
|
|
341
|
+
* Initializes a PHP runtime.
|
|
342
|
+
*
|
|
343
|
+
* @internal
|
|
344
|
+
* @param PHPRuntime - Optional. PHP Runtime ID as initialized by loadPHPRuntime.
|
|
345
|
+
*/
|
|
346
|
+
constructor(PHPRuntimeId?: PHPRuntimeId);
|
|
347
|
+
initializeRuntime(runtimeId: PHPRuntimeId): void;
|
|
348
|
+
/** @inheritDoc */
|
|
349
|
+
setPhpIniPath(path: string): void;
|
|
350
|
+
/** @inheritDoc */
|
|
351
|
+
setPhpIniEntry(key: string, value: string): void;
|
|
352
|
+
/** @inheritDoc */
|
|
353
|
+
run(request?: PHPRequest): PHPResponse;
|
|
354
|
+
cli(argv: string[]): Promise<number>;
|
|
355
|
+
setSkipShebang(shouldSkip: boolean): void;
|
|
356
|
+
addServerGlobalEntry(key: string, value: string): void;
|
|
357
|
+
mkdirTree(path: string): void;
|
|
358
|
+
readFileAsText(path: string): string;
|
|
359
|
+
/** @inheritDoc */
|
|
360
|
+
readFileAsBuffer(path: string): Uint8Array;
|
|
361
|
+
/** @inheritDoc */
|
|
362
|
+
writeFile(path: string, data: string | Uint8Array): void;
|
|
363
|
+
/** @inheritDoc */
|
|
364
|
+
unlink(path: string): void;
|
|
365
|
+
/** @inheritDoc */
|
|
366
|
+
listFiles(path: string): string[];
|
|
367
|
+
isDir(path: string): boolean;
|
|
368
|
+
fileExists(path: string): boolean;
|
|
369
|
+
mount(settings: MountSettings, path: string): void;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Output of the PHP.wasm runtime.
|
|
373
|
+
*/
|
|
374
|
+
export interface PHPOutput {
|
|
375
|
+
/** Exit code of the PHP process. 0 means success, 1 and 2 mean error. */
|
|
376
|
+
exitCode: number;
|
|
377
|
+
/** Stdout data */
|
|
378
|
+
stdout: ArrayBuffer;
|
|
379
|
+
/** Stderr lines */
|
|
380
|
+
stderr: string[];
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Emscripten's filesystem-related Exception.
|
|
384
|
+
*
|
|
385
|
+
* @see https://emscripten.org/docs/api_reference/Filesystem-API.html
|
|
386
|
+
* @see https://github.com/emscripten-core/emscripten/blob/main/system/lib/libc/musl/arch/emscripten/bits/errno.h
|
|
387
|
+
*/
|
|
388
|
+
export type ErrnoError = Error;
|
|
389
|
+
export type PHPServerRequest = Pick<PHPRequest, "method" | "headers"> & {
|
|
390
|
+
files?: Record<string, File>;
|
|
391
|
+
} & ({
|
|
392
|
+
absoluteUrl: string;
|
|
393
|
+
relativeUrl?: never;
|
|
394
|
+
} | {
|
|
395
|
+
absoluteUrl?: never;
|
|
396
|
+
relativeUrl: string;
|
|
397
|
+
}) & ((Pick<PHPRequest, "body"> & {
|
|
398
|
+
formData?: never;
|
|
399
|
+
}) | {
|
|
400
|
+
body?: never;
|
|
401
|
+
formData: Record<string, unknown>;
|
|
402
|
+
});
|
|
403
|
+
/**
|
|
404
|
+
* A fake PHP server that handles HTTP requests but does not
|
|
405
|
+
* bind to any port.
|
|
406
|
+
*
|
|
407
|
+
* @public
|
|
408
|
+
* @example
|
|
409
|
+
* ```js
|
|
410
|
+
* import {
|
|
411
|
+
* loadPHPRuntime,
|
|
412
|
+
* PHP,
|
|
413
|
+
* PHPServer,
|
|
414
|
+
* PHPBrowser,
|
|
415
|
+
* getPHPLoaderModule,
|
|
416
|
+
* } from '@php-wasm/web';
|
|
417
|
+
*
|
|
418
|
+
* const runtime = await loadPHPRuntime( await getPHPLoaderModule('7.4') );
|
|
419
|
+
* const php = new PHP( runtime );
|
|
420
|
+
*
|
|
421
|
+
* php.mkdirTree('/www');
|
|
422
|
+
* php.writeFile('/www/index.php', '<?php echo "Hi from PHP!"; ');
|
|
423
|
+
*
|
|
424
|
+
* const server = new PHPServer(php, {
|
|
425
|
+
* // PHP FS path to serve the files from:
|
|
426
|
+
* documentRoot: '/www',
|
|
427
|
+
*
|
|
428
|
+
* // Used to populate $_SERVER['SERVER_NAME'] etc.:
|
|
429
|
+
* absoluteUrl: 'http://127.0.0.1'
|
|
430
|
+
* });
|
|
431
|
+
*
|
|
432
|
+
* const output = server.request({ path: '/index.php' }).body;
|
|
433
|
+
* console.log(new TextDecoder().decode(output));
|
|
434
|
+
* // "Hi from PHP!"
|
|
435
|
+
* ```
|
|
436
|
+
*/
|
|
437
|
+
export declare class PHPServer {
|
|
438
|
+
#private;
|
|
439
|
+
/**
|
|
440
|
+
* The PHP instance
|
|
441
|
+
*/
|
|
442
|
+
php: PHP;
|
|
443
|
+
/**
|
|
444
|
+
* @param php - The PHP instance.
|
|
445
|
+
* @param config - Server configuration.
|
|
446
|
+
*/
|
|
447
|
+
constructor(php: PHP, config?: PHPServerConfigation);
|
|
448
|
+
/**
|
|
449
|
+
* Converts a path to an absolute URL based at the PHPServer
|
|
450
|
+
* root.
|
|
451
|
+
*
|
|
452
|
+
* @param path The server path to convert to an absolute URL.
|
|
453
|
+
* @returns The absolute URL.
|
|
454
|
+
*/
|
|
455
|
+
pathToInternalUrl(path: string): string;
|
|
456
|
+
/**
|
|
457
|
+
* Converts an absolute URL based at the PHPServer to a relative path
|
|
458
|
+
* without the server pathname and scope.
|
|
459
|
+
*
|
|
460
|
+
* @param internalUrl An absolute URL based at the PHPServer root.
|
|
461
|
+
* @returns The relative path.
|
|
462
|
+
*/
|
|
463
|
+
internalUrlToPath(internalUrl: string): string;
|
|
464
|
+
/**
|
|
465
|
+
* The absolute URL of this PHPServer instance.
|
|
466
|
+
*/
|
|
467
|
+
get absoluteUrl(): string;
|
|
468
|
+
/**
|
|
469
|
+
* The absolute URL of this PHPServer instance.
|
|
470
|
+
*/
|
|
471
|
+
get documentRoot(): string;
|
|
472
|
+
/**
|
|
473
|
+
* Serves the request – either by serving a static file, or by
|
|
474
|
+
* dispatching it to the PHP runtime.
|
|
475
|
+
*
|
|
476
|
+
* @param request - The request.
|
|
477
|
+
* @returns The response.
|
|
478
|
+
*/
|
|
479
|
+
request(request: PHPServerRequest): Promise<PHPResponse>;
|
|
480
|
+
}
|
|
481
|
+
export interface PHPServerConfigation {
|
|
482
|
+
/**
|
|
483
|
+
* The directory in the PHP filesystem where the server will look
|
|
484
|
+
* for the files to serve. Default: `/var/www`.
|
|
485
|
+
*/
|
|
486
|
+
documentRoot?: string;
|
|
487
|
+
/**
|
|
488
|
+
* Server URL. Used to populate $_SERVER details like HTTP_HOST.
|
|
489
|
+
*/
|
|
490
|
+
absoluteUrl?: string;
|
|
491
|
+
/**
|
|
492
|
+
* Callback used by the PHPServer to decide whether
|
|
493
|
+
* the requested path refers to a PHP file or a static file.
|
|
494
|
+
*/
|
|
495
|
+
isStaticFilePath?: (path: string) => boolean;
|
|
496
|
+
}
|
|
497
|
+
export interface WithRequest {
|
|
498
|
+
/**
|
|
499
|
+
* Sends the request to the server.
|
|
500
|
+
*
|
|
501
|
+
* When cookies are present in the response, this method stores
|
|
502
|
+
* them and sends them with any subsequent requests.
|
|
503
|
+
*
|
|
504
|
+
* When a redirection is present in the response, this method
|
|
505
|
+
* follows it by discarding a response and sending a subsequent
|
|
506
|
+
* request.
|
|
507
|
+
*
|
|
508
|
+
* @param request - The request.
|
|
509
|
+
* @param redirects - Internal. The number of redirects handled so far.
|
|
510
|
+
* @returns PHPServer response.
|
|
511
|
+
*/
|
|
512
|
+
request(request: PHPServerRequest, redirects?: number): Promise<PHPResponse>;
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* A fake web browser that handles PHPServer's cookies and redirects
|
|
516
|
+
* internally without exposing them to the consumer.
|
|
517
|
+
*
|
|
518
|
+
* @public
|
|
519
|
+
*/
|
|
520
|
+
export declare class PHPBrowser implements WithRequest {
|
|
521
|
+
#private;
|
|
522
|
+
server: PHPServer;
|
|
523
|
+
/**
|
|
524
|
+
* @param server - The PHP server to browse.
|
|
525
|
+
* @param config - The browser configuration.
|
|
526
|
+
*/
|
|
527
|
+
constructor(server: PHPServer, config?: PHPBrowserConfiguration);
|
|
528
|
+
request(request: PHPServerRequest, redirects?: number): Promise<PHPResponse>;
|
|
529
|
+
}
|
|
530
|
+
export interface PHPBrowserConfiguration {
|
|
531
|
+
/**
|
|
532
|
+
* Should handle redirects internally?
|
|
533
|
+
*/
|
|
534
|
+
handleRedirects?: boolean;
|
|
535
|
+
/**
|
|
536
|
+
* The maximum number of redirects to follow internally. Once
|
|
537
|
+
* exceeded, request() will return the redirecting response.
|
|
538
|
+
*/
|
|
539
|
+
maxRedirects?: number;
|
|
540
|
+
}
|
|
541
|
+
export type WorkerStartupOptions<T extends Record<string, string> = Record<string, string>> = T;
|
|
542
|
+
export declare function consumeAPI<APIType>(remote: Worker | Window): Comlink.Remote<APIType>;
|
|
543
|
+
export type PublicAPI<Methods, PipedAPI = unknown> = Methods & PipedAPI & {
|
|
544
|
+
isReady: () => Promise<void>;
|
|
545
|
+
};
|
|
546
|
+
export declare function exposeAPI<Methods, PipedAPI>(apiMethods?: Methods, pipedApi?: PipedAPI): [
|
|
547
|
+
() => void,
|
|
548
|
+
PublicAPI<Methods, PipedAPI>
|
|
549
|
+
];
|
|
550
|
+
export interface MonitoredModule {
|
|
551
|
+
dependencyFilename: string;
|
|
552
|
+
dependenciesTotalSize: number;
|
|
553
|
+
}
|
|
554
|
+
declare class EmscriptenDownloadMonitor extends EventTarget {
|
|
555
|
+
#private;
|
|
556
|
+
constructor(modules?: MonitoredModule[]);
|
|
557
|
+
getEmscriptenArgs(): {
|
|
558
|
+
dataFileDownloads: Record<string, any>;
|
|
559
|
+
};
|
|
560
|
+
setModules(modules: MonitoredModule[]): void;
|
|
561
|
+
}
|
|
562
|
+
/** @inheritdoc T */
|
|
563
|
+
export type Promisify<T> = {
|
|
564
|
+
[P in keyof T]: T[P] extends (...args: infer A) => infer R ? R extends void | Promise<any> ? T[P] : (...args: A) => Promise<ReturnType<T[P]>> : Promise<T[P]>;
|
|
565
|
+
};
|
|
566
|
+
export interface WithPathConversion {
|
|
567
|
+
pathToInternalUrl(path: string): Promise<string>;
|
|
568
|
+
internalUrlToPath(internalUrl: string): Promise<string>;
|
|
569
|
+
}
|
|
570
|
+
export interface WithProgress {
|
|
571
|
+
onDownloadProgress(callback: (progress: CustomEvent<ProgressEvent>) => void): Promise<void>;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* A PHP client that can be used to run PHP code in the browser.
|
|
575
|
+
*/
|
|
576
|
+
export declare class PHPClient implements Promisify<WithRequest & WithPHPIniBindings & WithFilesystem & WithRun & WithProgress & WithPathConversion> {
|
|
577
|
+
/** @inheritDoc @php-wasm/web!PHPServer.absoluteUrl */
|
|
578
|
+
absoluteUrl: Promise<string>;
|
|
579
|
+
/** @inheritDoc @php-wasm/web!PHPServer.documentRoot */
|
|
580
|
+
documentRoot: Promise<string>;
|
|
581
|
+
/** @inheritDoc */
|
|
582
|
+
constructor(browser: PHPBrowser, monitor?: EmscriptenDownloadMonitor);
|
|
583
|
+
/** @inheritDoc @php-wasm/web!PHPServer.pathToInternalUrl */
|
|
584
|
+
pathToInternalUrl(path: string): Promise<string>;
|
|
585
|
+
/** @inheritDoc @php-wasm/web!PHPServer.internalUrlToPath */
|
|
586
|
+
internalUrlToPath(internalUrl: string): Promise<string>;
|
|
587
|
+
onDownloadProgress(callback: (progress: CustomEvent<ProgressEvent>) => void): Promise<void>;
|
|
588
|
+
/** @inheritDoc @php-wasm/web!PHPServer.request */
|
|
589
|
+
request(request: PHPServerRequest, redirects?: number): Promise<PHPResponse>;
|
|
590
|
+
/** @inheritDoc @php-wasm/web!PHP.run */
|
|
591
|
+
run(request?: PHPRequest | undefined): Promise<PHPResponse>;
|
|
592
|
+
/** @inheritDoc @php-wasm/web!PHP.setPhpIniPath */
|
|
593
|
+
setPhpIniPath(path: string): void;
|
|
594
|
+
/** @inheritDoc @php-wasm/web!PHP.setPhpIniEntry */
|
|
595
|
+
setPhpIniEntry(key: string, value: string): void;
|
|
596
|
+
/** @inheritDoc @php-wasm/web!PHP.mkdirTree */
|
|
597
|
+
mkdirTree(path: string): void;
|
|
598
|
+
/** @inheritDoc @php-wasm/web!PHP.readFileAsText */
|
|
599
|
+
readFileAsText(path: string): Promise<string>;
|
|
600
|
+
/** @inheritDoc @php-wasm/web!PHP.readFileAsBuffer */
|
|
601
|
+
readFileAsBuffer(path: string): Promise<Uint8Array>;
|
|
602
|
+
/** @inheritDoc @php-wasm/web!PHP.writeFile */
|
|
603
|
+
writeFile(path: string, data: string | Uint8Array): void;
|
|
604
|
+
/** @inheritDoc @php-wasm/web!PHP.unlink */
|
|
605
|
+
unlink(path: string): void;
|
|
606
|
+
/** @inheritDoc @php-wasm/web!PHP.listFiles */
|
|
607
|
+
listFiles(path: string): Promise<string[]>;
|
|
608
|
+
/** @inheritDoc @php-wasm/web!PHP.isDir */
|
|
609
|
+
isDir(path: string): Promise<boolean>;
|
|
610
|
+
/** @inheritDoc @php-wasm/web!PHP.fileExists */
|
|
611
|
+
fileExists(path: string): Promise<boolean>;
|
|
612
|
+
}
|
|
613
|
+
export declare const isAssignableToRemoteClient: PHPClient;
|
|
614
|
+
export declare function getPHPLoaderModule(version?: string): Promise<PHPLoaderModule>;
|
|
615
|
+
export declare function parseWorkerStartupOptions<T extends Record<string, string>>(): WorkerStartupOptions<T>;
|
|
616
|
+
/**
|
|
617
|
+
* Recommended Worker Thread backend.
|
|
618
|
+
* It's typically "webworker", but in Firefox it's "iframe"
|
|
619
|
+
* because Firefox doesn't support module workers with dynamic imports.
|
|
620
|
+
* See https://github.com/mdn/content/issues/24402
|
|
621
|
+
*/
|
|
622
|
+
export declare const recommendedWorkerBackend: string;
|
|
623
|
+
/**
|
|
624
|
+
* Spawns a new Worker Thread.
|
|
625
|
+
*
|
|
626
|
+
* @param workerUrl The absolute URL of the worker script.
|
|
627
|
+
* @param workerBackend The Worker Thread backend to use. Either 'webworker' or 'iframe'.
|
|
628
|
+
* @param config
|
|
629
|
+
* @returns The spawned Worker Thread.
|
|
630
|
+
*/
|
|
631
|
+
export declare function spawnPHPWorkerThread(workerUrl: string, workerBackend?: "webworker" | "iframe", startupOptions?: Record<string, string>): Promise<Window | Worker>;
|
|
632
|
+
/**
|
|
633
|
+
* Run this in the main application to register the service worker or
|
|
634
|
+
* reload the registered worker if the app expects a different version
|
|
635
|
+
* than the currently registered one.
|
|
636
|
+
*
|
|
637
|
+
* @param {string} scriptUrl The URL of the service worker script.
|
|
638
|
+
* @param {string} expectedVersion The expected version of the service worker script. If
|
|
639
|
+
* mismatched with the actual version, the service worker
|
|
640
|
+
* will be re-registered.
|
|
641
|
+
*/
|
|
642
|
+
export declare function registerServiceWorker<Client extends PHPClient>(phpApi: Client, scope: string, scriptUrl: string, expectedVersion: string): Promise<void>;
|
|
643
|
+
|
|
644
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recommended Worker Thread backend.
|
|
3
|
+
* It's typically "webworker", but in Firefox it's "iframe"
|
|
4
|
+
* because Firefox doesn't support module workers with dynamic imports.
|
|
5
|
+
* See https://github.com/mdn/content/issues/24402
|
|
6
|
+
*/
|
|
7
|
+
export declare const recommendedWorkerBackend: string;
|
|
8
|
+
/**
|
|
9
|
+
* Spawns a new Worker Thread.
|
|
10
|
+
*
|
|
11
|
+
* @param workerUrl The absolute URL of the worker script.
|
|
12
|
+
* @param workerBackend The Worker Thread backend to use. Either 'webworker' or 'iframe'.
|
|
13
|
+
* @param config
|
|
14
|
+
* @returns The spawned Worker Thread.
|
|
15
|
+
*/
|
|
16
|
+
export declare function spawnPHPWorkerThread(workerUrl: string, workerBackend?: 'webworker' | 'iframe', startupOptions?: Record<string, string>): Promise<Window | Worker>;
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@php-wasm/web",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "PHP.wasm for the web",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/WordPress/wordpress-playground"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://developer.wordpress.org/playground",
|
|
10
|
+
"author": "The WordPress contributors",
|
|
11
|
+
"contributors": [
|
|
12
|
+
{
|
|
13
|
+
"name": "Adam Zielinski",
|
|
14
|
+
"email": "adam@adamziel.com",
|
|
15
|
+
"url": "https://github.com/adamziel"
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
"typedoc": {
|
|
19
|
+
"entryPoint": "./src/index.ts",
|
|
20
|
+
"readmeFile": "./README.md",
|
|
21
|
+
"displayName": "@php-wasm/web",
|
|
22
|
+
"tsconfig": "./tsconfig.lib.json"
|
|
23
|
+
},
|
|
24
|
+
"license": "Apache-2.0",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "index.js",
|
|
27
|
+
"types": "index.d.ts"
|
|
28
|
+
}
|