@php-wasm/universal 0.7.20 → 0.9.4
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.cjs +14 -14
- package/index.js +1458 -1038
- package/lib/emscripten-types.d.ts +265 -0
- package/lib/fs-helpers.d.ts +94 -0
- package/lib/index.d.ts +8 -5
- package/lib/ini.d.ts +1 -1
- package/lib/php-process-manager.d.ts +8 -8
- package/lib/php-request-handler.d.ts +9 -9
- package/lib/php-worker.d.ts +77 -0
- package/lib/php.d.ts +314 -0
- package/lib/proxy-file-system.d.ts +2 -2
- package/lib/rotate-php-runtime.d.ts +4 -4
- package/lib/universal-php.d.ts +2 -257
- package/lib/wasm-error-reporting.d.ts +1 -1
- package/lib/write-files.d.ts +3 -1
- package/package.json +2 -2
- package/lib/base-php.d.ts +0 -120
- package/lib/is-local-php.d.ts +0 -2
- package/lib/is-remote-php.d.ts +0 -2
package/lib/php.d.ts
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { PHPResponse } from './php-response';
|
|
2
|
+
import type { PHPRuntimeId } from './load-php-runtime';
|
|
3
|
+
import { MessageListener, PHPRequest, PHPRequestHeaders, PHPRunOptions, SpawnHandler, PHPEventListener, PHPEvent } from './universal-php';
|
|
4
|
+
import { RmDirOptions, ListFilesOptions } from './fs-helpers';
|
|
5
|
+
import { Semaphore } from '../../../util/src/index.ts';
|
|
6
|
+
import { PHPRequestHandler } from './php-request-handler';
|
|
7
|
+
import { Emscripten } from './emscripten-types';
|
|
8
|
+
export declare const __private__dont__use: unique symbol;
|
|
9
|
+
export declare class PHPExecutionFailureError extends Error {
|
|
10
|
+
response: PHPResponse;
|
|
11
|
+
source: 'request' | 'php-wasm';
|
|
12
|
+
constructor(message: string, response: PHPResponse, source: 'request' | 'php-wasm');
|
|
13
|
+
}
|
|
14
|
+
export type UnmountFunction = (() => Promise<any>) | (() => any);
|
|
15
|
+
export type MountHandler = (php: PHP, FS: Emscripten.RootFS, vfsMountPoint: string) => UnmountFunction | Promise<UnmountFunction>;
|
|
16
|
+
export declare const PHP_INI_PATH = "/internal/shared/php.ini";
|
|
17
|
+
/**
|
|
18
|
+
* An environment-agnostic wrapper around the Emscripten PHP runtime
|
|
19
|
+
* that universals the super low-level API and provides a more convenient
|
|
20
|
+
* higher-level API.
|
|
21
|
+
*
|
|
22
|
+
* It exposes a minimal set of methods to run PHP scripts and to
|
|
23
|
+
* interact with the PHP filesystem.
|
|
24
|
+
*/
|
|
25
|
+
export declare class PHP implements Disposable {
|
|
26
|
+
#private;
|
|
27
|
+
protected [__private__dont__use]: any;
|
|
28
|
+
requestHandler?: PHPRequestHandler;
|
|
29
|
+
/**
|
|
30
|
+
* An exclusive lock that prevent multiple requests from running at
|
|
31
|
+
* the same time.
|
|
32
|
+
*/
|
|
33
|
+
semaphore: Semaphore;
|
|
34
|
+
/**
|
|
35
|
+
* Initializes a PHP runtime.
|
|
36
|
+
*
|
|
37
|
+
* @internal
|
|
38
|
+
* @param PHPRuntime - Optional. PHP Runtime ID as initialized by loadPHPRuntime.
|
|
39
|
+
* @param requestHandlerOptions - Optional. Options for the PHPRequestHandler. If undefined, no request handler will be initialized.
|
|
40
|
+
*/
|
|
41
|
+
constructor(PHPRuntimeId?: PHPRuntimeId);
|
|
42
|
+
/**
|
|
43
|
+
* Adds an event listener for a PHP event.
|
|
44
|
+
* @param eventType - The type of event to listen for.
|
|
45
|
+
* @param listener - The listener function to be called when the event is triggered.
|
|
46
|
+
*/
|
|
47
|
+
addEventListener(eventType: PHPEvent['type'], listener: PHPEventListener): void;
|
|
48
|
+
/**
|
|
49
|
+
* Removes an event listener for a PHP event.
|
|
50
|
+
* @param eventType - The type of event to remove the listener from.
|
|
51
|
+
* @param listener - The listener function to be removed.
|
|
52
|
+
*/
|
|
53
|
+
removeEventListener(eventType: PHPEvent['type'], listener: PHPEventListener): void;
|
|
54
|
+
dispatchEvent<Event extends PHPEvent>(event: Event): void;
|
|
55
|
+
/**
|
|
56
|
+
* Listens to message sent by the PHP code.
|
|
57
|
+
*
|
|
58
|
+
* To dispatch messages, call:
|
|
59
|
+
*
|
|
60
|
+
* post_message_to_js(string $data)
|
|
61
|
+
*
|
|
62
|
+
* Arguments:
|
|
63
|
+
* $data (string) – Data to pass to JavaScript.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* const php = await PHP.load('8.0');
|
|
69
|
+
*
|
|
70
|
+
* php.onMessage(
|
|
71
|
+
* // The data is always passed as a string
|
|
72
|
+
* function (data: string) {
|
|
73
|
+
* // Let's decode and log the data:
|
|
74
|
+
* console.log(JSON.parse(data));
|
|
75
|
+
* }
|
|
76
|
+
* );
|
|
77
|
+
*
|
|
78
|
+
* // Now that we have a listener in place, let's
|
|
79
|
+
* // dispatch a message:
|
|
80
|
+
* await php.run({
|
|
81
|
+
* code: `<?php
|
|
82
|
+
* post_message_to_js(
|
|
83
|
+
* json_encode([
|
|
84
|
+
* 'post_id' => '15',
|
|
85
|
+
* 'post_title' => 'This is a blog post!'
|
|
86
|
+
* ])
|
|
87
|
+
* ));
|
|
88
|
+
* `,
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @param listener Callback function to handle the message.
|
|
93
|
+
*/
|
|
94
|
+
onMessage(listener: MessageListener): void;
|
|
95
|
+
setSpawnHandler(handler: SpawnHandler | string): Promise<void>;
|
|
96
|
+
/** @deprecated Use PHPRequestHandler instead. */
|
|
97
|
+
get absoluteUrl(): string;
|
|
98
|
+
/** @deprecated Use PHPRequestHandler instead. */
|
|
99
|
+
get documentRoot(): string;
|
|
100
|
+
/** @deprecated Use PHPRequestHandler instead. */
|
|
101
|
+
pathToInternalUrl(path: string): string;
|
|
102
|
+
/** @deprecated Use PHPRequestHandler instead. */
|
|
103
|
+
internalUrlToPath(internalUrl: string): string;
|
|
104
|
+
initializeRuntime(runtimeId: PHPRuntimeId): void;
|
|
105
|
+
/** @inheritDoc */
|
|
106
|
+
setSapiName(newName: string): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Changes the current working directory in the PHP filesystem.
|
|
109
|
+
* This is the directory that will be used as the base for relative paths.
|
|
110
|
+
* For example, if the current working directory is `/root/php`, and the
|
|
111
|
+
* path is `data`, the absolute path will be `/root/php/data`.
|
|
112
|
+
*
|
|
113
|
+
* @param path - The new working directory.
|
|
114
|
+
*/
|
|
115
|
+
chdir(path: string): void;
|
|
116
|
+
/**
|
|
117
|
+
* Do not use. Use new PHPRequestHandler() instead.
|
|
118
|
+
* @deprecated
|
|
119
|
+
*/
|
|
120
|
+
request(request: PHPRequest): Promise<PHPResponse>;
|
|
121
|
+
/**
|
|
122
|
+
* Runs PHP code.
|
|
123
|
+
*
|
|
124
|
+
* This low-level method directly interacts with the WebAssembly
|
|
125
|
+
* PHP interpreter.
|
|
126
|
+
*
|
|
127
|
+
* Every time you call run(), it prepares the PHP
|
|
128
|
+
* environment and:
|
|
129
|
+
*
|
|
130
|
+
* * Resets the internal PHP state
|
|
131
|
+
* * Populates superglobals ($_SERVER, $_GET, etc.)
|
|
132
|
+
* * Handles file uploads
|
|
133
|
+
* * Populates input streams (stdin, argv, etc.)
|
|
134
|
+
* * Sets the current working directory
|
|
135
|
+
*
|
|
136
|
+
* You can use run() in two primary modes:
|
|
137
|
+
*
|
|
138
|
+
* ### Code snippet mode
|
|
139
|
+
*
|
|
140
|
+
* In this mode, you pass a string containing PHP code to run.
|
|
141
|
+
*
|
|
142
|
+
* ```ts
|
|
143
|
+
* const result = await php.run({
|
|
144
|
+
* code: `<?php echo "Hello world!";`
|
|
145
|
+
* });
|
|
146
|
+
* // result.text === "Hello world!"
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* In this mode, information like __DIR__ or __FILE__ isn't very
|
|
150
|
+
* useful because the code is not associated with any file.
|
|
151
|
+
*
|
|
152
|
+
* Under the hood, the PHP snippet is passed to the `zend_eval_string`
|
|
153
|
+
* C function.
|
|
154
|
+
*
|
|
155
|
+
* ### File mode
|
|
156
|
+
*
|
|
157
|
+
* In the file mode, you pass a scriptPath and PHP executes a file
|
|
158
|
+
* found at a that path:
|
|
159
|
+
*
|
|
160
|
+
* ```ts
|
|
161
|
+
* php.writeFile(
|
|
162
|
+
* "/www/index.php",
|
|
163
|
+
* `<?php echo "Hello world!";"`
|
|
164
|
+
* );
|
|
165
|
+
* const result = await php.run({
|
|
166
|
+
* scriptPath: "/www/index.php"
|
|
167
|
+
* });
|
|
168
|
+
* // result.text === "Hello world!"
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* In this mode, you can rely on path-related information like __DIR__
|
|
172
|
+
* or __FILE__.
|
|
173
|
+
*
|
|
174
|
+
* Under the hood, the PHP file is executed with the `php_execute_script`
|
|
175
|
+
* C function.
|
|
176
|
+
*
|
|
177
|
+
* The `run()` method cannot be used in conjunction with `cli()`.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```js
|
|
181
|
+
* const result = await php.run(`<?php
|
|
182
|
+
* $fp = fopen('php://stderr', 'w');
|
|
183
|
+
* fwrite($fp, "Hello, world!");
|
|
184
|
+
* `);
|
|
185
|
+
* // result.errors === "Hello, world!"
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* @param options - PHP runtime options.
|
|
189
|
+
*/
|
|
190
|
+
run(request: PHPRunOptions): Promise<PHPResponse>;
|
|
191
|
+
/**
|
|
192
|
+
* Defines a constant in the PHP runtime.
|
|
193
|
+
* @param key - The name of the constant.
|
|
194
|
+
* @param value - The value of the constant.
|
|
195
|
+
*/
|
|
196
|
+
defineConstant(key: string, value: string | boolean | number | null): void;
|
|
197
|
+
/**
|
|
198
|
+
* Recursively creates a directory with the given path in the PHP filesystem.
|
|
199
|
+
* For example, if the path is `/root/php/data`, and `/root` already exists,
|
|
200
|
+
* it will create the directories `/root/php` and `/root/php/data`.
|
|
201
|
+
*
|
|
202
|
+
* @param path - The directory path to create.
|
|
203
|
+
*/
|
|
204
|
+
mkdir(path: string): void;
|
|
205
|
+
/**
|
|
206
|
+
* @deprecated Use mkdir instead.
|
|
207
|
+
*/
|
|
208
|
+
mkdirTree(path: string): void;
|
|
209
|
+
/**
|
|
210
|
+
* Reads a file from the PHP filesystem and returns it as a string.
|
|
211
|
+
*
|
|
212
|
+
* @throws {@link @php-wasm/universal:ErrnoError} – If the file doesn't exist.
|
|
213
|
+
* @param path - The file path to read.
|
|
214
|
+
* @returns The file contents.
|
|
215
|
+
*/
|
|
216
|
+
readFileAsText(path: string): string;
|
|
217
|
+
/**
|
|
218
|
+
* Reads a file from the PHP filesystem and returns it as an array buffer.
|
|
219
|
+
*
|
|
220
|
+
* @throws {@link @php-wasm/universal:ErrnoError} – If the file doesn't exist.
|
|
221
|
+
* @param path - The file path to read.
|
|
222
|
+
* @returns The file contents.
|
|
223
|
+
*/
|
|
224
|
+
readFileAsBuffer(path: string): Uint8Array;
|
|
225
|
+
/**
|
|
226
|
+
* Overwrites data in a file in the PHP filesystem.
|
|
227
|
+
* Creates a new file if one doesn't exist yet.
|
|
228
|
+
*
|
|
229
|
+
* @param path - The file path to write to.
|
|
230
|
+
* @param data - The data to write to the file.
|
|
231
|
+
*/
|
|
232
|
+
writeFile(path: string, data: string | Uint8Array): void;
|
|
233
|
+
/**
|
|
234
|
+
* Removes a file from the PHP filesystem.
|
|
235
|
+
*
|
|
236
|
+
* @throws {@link @php-wasm/universal:ErrnoError} – If the file doesn't exist.
|
|
237
|
+
* @param path - The file path to remove.
|
|
238
|
+
*/
|
|
239
|
+
unlink(path: string): void;
|
|
240
|
+
/**
|
|
241
|
+
* Moves a file or directory in the PHP filesystem to a
|
|
242
|
+
* new location.
|
|
243
|
+
*
|
|
244
|
+
* @param oldPath The path to rename.
|
|
245
|
+
* @param newPath The new path.
|
|
246
|
+
*/
|
|
247
|
+
mv(fromPath: string, toPath: string): void;
|
|
248
|
+
/**
|
|
249
|
+
* Removes a directory from the PHP filesystem.
|
|
250
|
+
*
|
|
251
|
+
* @param path The directory path to remove.
|
|
252
|
+
* @param options Options for the removal.
|
|
253
|
+
*/
|
|
254
|
+
rmdir(path: string, options?: RmDirOptions): void;
|
|
255
|
+
/**
|
|
256
|
+
* Lists the files and directories in the given directory.
|
|
257
|
+
*
|
|
258
|
+
* @param path - The directory path to list.
|
|
259
|
+
* @param options - Options for the listing.
|
|
260
|
+
* @returns The list of files and directories in the given directory.
|
|
261
|
+
*/
|
|
262
|
+
listFiles(path: string, options?: ListFilesOptions): 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
|
+
* Hot-swaps the PHP runtime for a new one without
|
|
279
|
+
* interrupting the operations of this PHP instance.
|
|
280
|
+
*
|
|
281
|
+
* @param runtime
|
|
282
|
+
* @param cwd. Internal, the VFS path to recreate in the new runtime.
|
|
283
|
+
* This arg is temporary and will be removed once BasePHP
|
|
284
|
+
* is fully decoupled from the request handler and
|
|
285
|
+
* accepts a constructor-level cwd argument.
|
|
286
|
+
*/
|
|
287
|
+
hotSwapPHPRuntime(runtime: number, cwd?: string): void;
|
|
288
|
+
/**
|
|
289
|
+
* Mounts a filesystem to a given path in the PHP filesystem.
|
|
290
|
+
*
|
|
291
|
+
* @param virtualFSPath - Where to mount it in the PHP virtual filesystem.
|
|
292
|
+
* @param mountHandler - The mount handler to use.
|
|
293
|
+
* @return Unmount function to unmount the filesystem.
|
|
294
|
+
*/
|
|
295
|
+
mount(virtualFSPath: string, mountHandler: MountHandler): Promise<UnmountFunction>;
|
|
296
|
+
/**
|
|
297
|
+
* Starts a PHP CLI session with given arguments.
|
|
298
|
+
*
|
|
299
|
+
* This method can only be used when PHP was compiled with the CLI SAPI
|
|
300
|
+
* and it cannot be used in conjunction with `run()`.
|
|
301
|
+
*
|
|
302
|
+
* Once this method finishes running, the PHP instance is no
|
|
303
|
+
* longer usable and should be discarded. This is because PHP
|
|
304
|
+
* internally cleans up all the resources and calls exit().
|
|
305
|
+
*
|
|
306
|
+
* @param argv - The arguments to pass to the CLI.
|
|
307
|
+
* @returns The exit code of the CLI session.
|
|
308
|
+
*/
|
|
309
|
+
cli(argv: string[]): Promise<number>;
|
|
310
|
+
setSkipShebang(shouldSkip: boolean): void;
|
|
311
|
+
exit(code?: number): void;
|
|
312
|
+
[Symbol.dispose](): void;
|
|
313
|
+
}
|
|
314
|
+
export declare function normalizeHeaders(headers: PHPRequestHeaders): PHPRequestHeaders;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PHP } from './php';
|
|
2
2
|
/**
|
|
3
3
|
* Proxy specific paths to the parent's MEMFS instance.
|
|
4
4
|
* This is useful for sharing the WordPress installation
|
|
5
5
|
* between the parent and child processes.
|
|
6
6
|
*/
|
|
7
|
-
export declare function proxyFileSystem(sourceOfTruth:
|
|
7
|
+
export declare function proxyFileSystem(sourceOfTruth: PHP, replica: PHP, paths: string[]): void;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export interface RotateOptions
|
|
3
|
-
php:
|
|
1
|
+
import { PHP } from './php';
|
|
2
|
+
export interface RotateOptions {
|
|
3
|
+
php: PHP;
|
|
4
4
|
cwd: string;
|
|
5
5
|
recreateRuntime: () => Promise<number> | number;
|
|
6
6
|
maxRequests: number;
|
|
@@ -21,4 +21,4 @@ export interface RotateOptions<T extends BasePHP> {
|
|
|
21
21
|
*
|
|
22
22
|
* @return cleanup function to restore
|
|
23
23
|
*/
|
|
24
|
-
export declare function rotatePHPRuntime
|
|
24
|
+
export declare function rotatePHPRuntime({ php, cwd, recreateRuntime, maxRequests, }: RotateOptions): () => void;
|
package/lib/universal-php.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Remote } from 'comlink';
|
|
2
|
-
import {
|
|
2
|
+
import { LimitedPHPApi } from './php-worker';
|
|
3
3
|
/**
|
|
4
4
|
* Represents an event related to the PHP request.
|
|
5
5
|
*/
|
|
@@ -36,240 +36,7 @@ export type PHPEvent = PHPRequestEndEvent | PHPRequestErrorEvent | PHPRuntimeIni
|
|
|
36
36
|
* A callback function that handles PHP events.
|
|
37
37
|
*/
|
|
38
38
|
export type PHPEventListener = (event: PHPEvent) => void;
|
|
39
|
-
export
|
|
40
|
-
/** @deprecated Use PHPRequestHandler instead. */
|
|
41
|
-
request(request: PHPRequest): Promise<PHPResponse>;
|
|
42
|
-
/** @deprecated Use PHPRequestHandler instead. */
|
|
43
|
-
pathToInternalUrl(path: string): string;
|
|
44
|
-
/** @deprecated Use PHPRequestHandler instead. */
|
|
45
|
-
internalUrlToPath(internalUrl: string): string;
|
|
46
|
-
/** @deprecated Use PHPRequestHandler instead. */
|
|
47
|
-
absoluteUrl: string;
|
|
48
|
-
/** @deprecated Use PHPRequestHandler instead. */
|
|
49
|
-
documentRoot: string;
|
|
50
|
-
/**
|
|
51
|
-
* Sets the SAPI name exposed by the PHP module.
|
|
52
|
-
* @param newName - The new SAPI name.
|
|
53
|
-
*/
|
|
54
|
-
setSapiName(newName: string): void;
|
|
55
|
-
/**
|
|
56
|
-
* Defines a constant in the PHP runtime.
|
|
57
|
-
* @param key - The name of the constant.
|
|
58
|
-
* @param value - The value of the constant.
|
|
59
|
-
*/
|
|
60
|
-
defineConstant(key: string, value: boolean | string | number | null): void;
|
|
61
|
-
/**
|
|
62
|
-
* Adds an event listener for a PHP event.
|
|
63
|
-
* @param eventType - The type of event to listen for.
|
|
64
|
-
* @param listener - The listener function to be called when the event is triggered.
|
|
65
|
-
*/
|
|
66
|
-
addEventListener(eventType: PHPEvent['type'], listener: PHPEventListener): void;
|
|
67
|
-
/**
|
|
68
|
-
* Removes an event listener for a PHP event.
|
|
69
|
-
* @param eventType - The type of event to remove the listener from.
|
|
70
|
-
* @param listener - The listener function to be removed.
|
|
71
|
-
*/
|
|
72
|
-
removeEventListener(eventType: PHPEvent['type'], listener: PHPEventListener): void;
|
|
73
|
-
/**
|
|
74
|
-
* Recursively creates a directory with the given path in the PHP filesystem.
|
|
75
|
-
* For example, if the path is `/root/php/data`, and `/root` already exists,
|
|
76
|
-
* it will create the directories `/root/php` and `/root/php/data`.
|
|
77
|
-
*
|
|
78
|
-
* @param path - The directory path to create.
|
|
79
|
-
*/
|
|
80
|
-
mkdir(path: string): void;
|
|
81
|
-
/**
|
|
82
|
-
* @deprecated Use mkdir instead.
|
|
83
|
-
*/
|
|
84
|
-
mkdirTree(path: string): void;
|
|
85
|
-
/**
|
|
86
|
-
* Reads a file from the PHP filesystem and returns it as a string.
|
|
87
|
-
*
|
|
88
|
-
* @throws {@link @php-wasm/universal:ErrnoError} – If the file doesn't exist.
|
|
89
|
-
* @param path - The file path to read.
|
|
90
|
-
* @returns The file contents.
|
|
91
|
-
*/
|
|
92
|
-
readFileAsText(path: string): string;
|
|
93
|
-
/**
|
|
94
|
-
* Reads a file from the PHP filesystem and returns it as an array buffer.
|
|
95
|
-
*
|
|
96
|
-
* @throws {@link @php-wasm/universal:ErrnoError} – If the file doesn't exist.
|
|
97
|
-
* @param path - The file path to read.
|
|
98
|
-
* @returns The file contents.
|
|
99
|
-
*/
|
|
100
|
-
readFileAsBuffer(path: string): Uint8Array;
|
|
101
|
-
/**
|
|
102
|
-
* Overwrites data in a file in the PHP filesystem.
|
|
103
|
-
* Creates a new file if one doesn't exist yet.
|
|
104
|
-
*
|
|
105
|
-
* @param path - The file path to write to.
|
|
106
|
-
* @param data - The data to write to the file.
|
|
107
|
-
*/
|
|
108
|
-
writeFile(path: string, data: string | Uint8Array): void;
|
|
109
|
-
/**
|
|
110
|
-
* Removes a file from the PHP filesystem.
|
|
111
|
-
*
|
|
112
|
-
* @throws {@link @php-wasm/universal:ErrnoError} – If the file doesn't exist.
|
|
113
|
-
* @param path - The file path to remove.
|
|
114
|
-
*/
|
|
115
|
-
unlink(path: string): void;
|
|
116
|
-
/**
|
|
117
|
-
* Moves a file or directory in the PHP filesystem to a
|
|
118
|
-
* new location.
|
|
119
|
-
*
|
|
120
|
-
* @param oldPath The path to rename.
|
|
121
|
-
* @param newPath The new path.
|
|
122
|
-
*/
|
|
123
|
-
mv(oldPath: string, newPath: string): void;
|
|
124
|
-
/**
|
|
125
|
-
* Removes a directory from the PHP filesystem.
|
|
126
|
-
*
|
|
127
|
-
* @param path The directory path to remove.
|
|
128
|
-
* @param options Options for the removal.
|
|
129
|
-
*/
|
|
130
|
-
rmdir(path: string, options?: RmDirOptions): void;
|
|
131
|
-
/**
|
|
132
|
-
* Lists the files and directories in the given directory.
|
|
133
|
-
*
|
|
134
|
-
* @param path - The directory path to list.
|
|
135
|
-
* @param options - Options for the listing.
|
|
136
|
-
* @returns The list of files and directories in the given directory.
|
|
137
|
-
*/
|
|
138
|
-
listFiles(path: string, options?: ListFilesOptions): string[];
|
|
139
|
-
/**
|
|
140
|
-
* Checks if a directory exists in the PHP filesystem.
|
|
141
|
-
*
|
|
142
|
-
* @param path – The path to check.
|
|
143
|
-
* @returns True if the path is a directory, false otherwise.
|
|
144
|
-
*/
|
|
145
|
-
isDir(path: string): boolean;
|
|
146
|
-
/**
|
|
147
|
-
* Checks if a file (or a directory) exists in the PHP filesystem.
|
|
148
|
-
*
|
|
149
|
-
* @param path - The file path to check.
|
|
150
|
-
* @returns True if the file exists, false otherwise.
|
|
151
|
-
*/
|
|
152
|
-
fileExists(path: string): boolean;
|
|
153
|
-
/**
|
|
154
|
-
* Changes the current working directory in the PHP filesystem.
|
|
155
|
-
* This is the directory that will be used as the base for relative paths.
|
|
156
|
-
* For example, if the current working directory is `/root/php`, and the
|
|
157
|
-
* path is `data`, the absolute path will be `/root/php/data`.
|
|
158
|
-
*
|
|
159
|
-
* @param path - The new working directory.
|
|
160
|
-
*/
|
|
161
|
-
chdir(path: string): void;
|
|
162
|
-
/**
|
|
163
|
-
* Runs PHP code.
|
|
164
|
-
*
|
|
165
|
-
* This low-level method directly interacts with the WebAssembly
|
|
166
|
-
* PHP interpreter.
|
|
167
|
-
*
|
|
168
|
-
* Every time you call run(), it prepares the PHP
|
|
169
|
-
* environment and:
|
|
170
|
-
*
|
|
171
|
-
* * Resets the internal PHP state
|
|
172
|
-
* * Populates superglobals ($_SERVER, $_GET, etc.)
|
|
173
|
-
* * Handles file uploads
|
|
174
|
-
* * Populates input streams (stdin, argv, etc.)
|
|
175
|
-
* * Sets the current working directory
|
|
176
|
-
*
|
|
177
|
-
* You can use run() in two primary modes:
|
|
178
|
-
*
|
|
179
|
-
* ### Code snippet mode
|
|
180
|
-
*
|
|
181
|
-
* In this mode, you pass a string containing PHP code to run.
|
|
182
|
-
*
|
|
183
|
-
* ```ts
|
|
184
|
-
* const result = await php.run({
|
|
185
|
-
* code: `<?php echo "Hello world!";`
|
|
186
|
-
* });
|
|
187
|
-
* // result.text === "Hello world!"
|
|
188
|
-
* ```
|
|
189
|
-
*
|
|
190
|
-
* In this mode, information like __DIR__ or __FILE__ isn't very
|
|
191
|
-
* useful because the code is not associated with any file.
|
|
192
|
-
*
|
|
193
|
-
* Under the hood, the PHP snippet is passed to the `zend_eval_string`
|
|
194
|
-
* C function.
|
|
195
|
-
*
|
|
196
|
-
* ### File mode
|
|
197
|
-
*
|
|
198
|
-
* In the file mode, you pass a scriptPath and PHP executes a file
|
|
199
|
-
* found at a that path:
|
|
200
|
-
*
|
|
201
|
-
* ```ts
|
|
202
|
-
* php.writeFile(
|
|
203
|
-
* "/www/index.php",
|
|
204
|
-
* `<?php echo "Hello world!";"`
|
|
205
|
-
* );
|
|
206
|
-
* const result = await php.run({
|
|
207
|
-
* scriptPath: "/www/index.php"
|
|
208
|
-
* });
|
|
209
|
-
* // result.text === "Hello world!"
|
|
210
|
-
* ```
|
|
211
|
-
*
|
|
212
|
-
* In this mode, you can rely on path-related information like __DIR__
|
|
213
|
-
* or __FILE__.
|
|
214
|
-
*
|
|
215
|
-
* Under the hood, the PHP file is executed with the `php_execute_script`
|
|
216
|
-
* C function.
|
|
217
|
-
*
|
|
218
|
-
* The `run()` method cannot be used in conjunction with `cli()`.
|
|
219
|
-
*
|
|
220
|
-
* @example
|
|
221
|
-
* ```js
|
|
222
|
-
* const result = await php.run(`<?php
|
|
223
|
-
* $fp = fopen('php://stderr', 'w');
|
|
224
|
-
* fwrite($fp, "Hello, world!");
|
|
225
|
-
* `);
|
|
226
|
-
* // result.errors === "Hello, world!"
|
|
227
|
-
* ```
|
|
228
|
-
*
|
|
229
|
-
* @param options - PHP runtime options.
|
|
230
|
-
*/
|
|
231
|
-
run(options: PHPRunOptions): Promise<PHPResponse>;
|
|
232
|
-
/**
|
|
233
|
-
* Listens to message sent by the PHP code.
|
|
234
|
-
*
|
|
235
|
-
* To dispatch messages, call:
|
|
236
|
-
*
|
|
237
|
-
* post_message_to_js(string $data)
|
|
238
|
-
*
|
|
239
|
-
* Arguments:
|
|
240
|
-
* $data (string) – Data to pass to JavaScript.
|
|
241
|
-
*
|
|
242
|
-
* @example
|
|
243
|
-
*
|
|
244
|
-
* ```ts
|
|
245
|
-
* const php = await PHP.load('8.0');
|
|
246
|
-
*
|
|
247
|
-
* php.onMessage(
|
|
248
|
-
* // The data is always passed as a string
|
|
249
|
-
* function (data: string) {
|
|
250
|
-
* // Let's decode and log the data:
|
|
251
|
-
* console.log(JSON.parse(data));
|
|
252
|
-
* }
|
|
253
|
-
* );
|
|
254
|
-
*
|
|
255
|
-
* // Now that we have a listener in place, let's
|
|
256
|
-
* // dispatch a message:
|
|
257
|
-
* await php.run({
|
|
258
|
-
* code: `<?php
|
|
259
|
-
* post_message_to_js(
|
|
260
|
-
* json_encode([
|
|
261
|
-
* 'post_id' => '15',
|
|
262
|
-
* 'post_title' => 'This is a blog post!'
|
|
263
|
-
* ])
|
|
264
|
-
* ));
|
|
265
|
-
* `,
|
|
266
|
-
* });
|
|
267
|
-
* ```
|
|
268
|
-
*
|
|
269
|
-
* @param listener Callback function to handle the message.
|
|
270
|
-
*/
|
|
271
|
-
onMessage(listener: MessageListener): void;
|
|
272
|
-
}
|
|
39
|
+
export type UniversalPHP = LimitedPHPApi | Remote<LimitedPHPApi>;
|
|
273
40
|
export type MessageListener = (data: string) => Promise<string | Uint8Array | void> | string | void;
|
|
274
41
|
interface EventEmitter {
|
|
275
42
|
on(event: string, listener: (...args: any[]) => void): this;
|
|
@@ -280,14 +47,6 @@ type ChildProcess = EventEmitter & {
|
|
|
280
47
|
stderr: EventEmitter;
|
|
281
48
|
};
|
|
282
49
|
export type SpawnHandler = (command: string, args: string[]) => ChildProcess;
|
|
283
|
-
/**
|
|
284
|
-
* The omited methods must either be called synchronously before
|
|
285
|
-
* the PHP internal state is initialized, or with a complex argument
|
|
286
|
-
* that can't be serialized over a remote connection. Therefeore,
|
|
287
|
-
* they don't make sense in a remote PHP instance.
|
|
288
|
-
*/
|
|
289
|
-
export type IsomorphicRemotePHP = Remote<Omit<IsomorphicLocalPHP, 'setSapiName'>>;
|
|
290
|
-
export type UniversalPHP = IsomorphicLocalPHP | IsomorphicRemotePHP;
|
|
291
50
|
export type HTTPMethod = 'GET' | 'POST' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'PUT' | 'DELETE';
|
|
292
51
|
export type PHPRequestHeaders = Record<string, string>;
|
|
293
52
|
export interface PHPRequest {
|
|
@@ -359,18 +118,4 @@ export interface PHPOutput {
|
|
|
359
118
|
/** Stderr lines */
|
|
360
119
|
stderr: string[];
|
|
361
120
|
}
|
|
362
|
-
export interface RmDirOptions {
|
|
363
|
-
/**
|
|
364
|
-
* If true, recursively removes the directory and all its contents.
|
|
365
|
-
* Default: true.
|
|
366
|
-
*/
|
|
367
|
-
recursive?: boolean;
|
|
368
|
-
}
|
|
369
|
-
export interface ListFilesOptions {
|
|
370
|
-
/**
|
|
371
|
-
* If true, prepend given folder path to all file names.
|
|
372
|
-
* Default: false.
|
|
373
|
-
*/
|
|
374
|
-
prependPath: boolean;
|
|
375
|
-
}
|
|
376
121
|
export {};
|
package/lib/write-files.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export interface WriteFilesOptions {
|
|
|
6
6
|
*/
|
|
7
7
|
rmRoot?: boolean;
|
|
8
8
|
}
|
|
9
|
+
export interface FileTree extends Record<string, Uint8Array | string | FileTree> {
|
|
10
|
+
}
|
|
9
11
|
/**
|
|
10
12
|
* Writes multiple files to a specified directory in the Playground
|
|
11
13
|
* filesystem.
|
|
@@ -23,4 +25,4 @@ export interface WriteFilesOptions {
|
|
|
23
25
|
* @param newFiles
|
|
24
26
|
* @param options
|
|
25
27
|
*/
|
|
26
|
-
export declare function writeFiles(php: UniversalPHP, root: string, newFiles:
|
|
28
|
+
export declare function writeFiles(php: UniversalPHP, root: string, newFiles: FileTree, { rmRoot }?: WriteFilesOptions): Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@php-wasm/universal",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.4",
|
|
4
4
|
"description": "PHP.wasm – emscripten bindings for PHP",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"main": "./index.cjs",
|
|
37
37
|
"module": "./index.js",
|
|
38
38
|
"license": "GPL-2.0-or-later",
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "702bb9bed9c153ea23651059592aea19c1ba9be4",
|
|
40
40
|
"engines": {
|
|
41
41
|
"node": ">=18.18.0",
|
|
42
42
|
"npm": ">=8.11.0"
|