@php-wasm/universal 0.1.40 → 0.1.42
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 +40 -0
- package/index.js +1070 -0
- package/lib/base-php.d.ts +70 -0
- package/lib/error-event-polyfill.d.ts +30 -0
- package/{src/lib/index.ts → lib/index.d.ts} +4 -36
- package/lib/is-local-php.d.ts +2 -0
- package/lib/is-remote-php.d.ts +2 -0
- package/{src/lib/load-php-runtime.ts → lib/load-php-runtime.d.ts} +23 -101
- package/lib/php-browser.d.ts +52 -0
- package/lib/php-request-handler.d.ts +43 -0
- package/lib/php-response.d.ts +54 -0
- package/lib/rethrow-file-system-error.d.ts +13 -0
- package/lib/supported-php-versions.d.ts +4 -0
- package/lib/universal-php.d.ts +309 -0
- package/{src/lib/urls.ts → lib/urls.d.ts} +4 -19
- package/lib/wasm-error-reporting.d.ts +26 -0
- package/package.json +4 -3
- package/.eslintrc.json +0 -18
- package/LICENSE +0 -339
- package/README.md +0 -0
- package/project.json +0 -50
- package/src/lib/base-php.ts +0 -555
- package/src/lib/error-event-polyfill.ts +0 -50
- package/src/lib/is-local-php.ts +0 -8
- package/src/lib/is-remote-php.ts +0 -8
- package/src/lib/php-browser.ts +0 -137
- package/src/lib/php-request-handler.ts +0 -381
- package/src/lib/php-response.ts +0 -104
- package/src/lib/rethrow-file-system-error.ts +0 -125
- package/src/lib/supported-php-versions.ts +0 -14
- package/src/lib/universal-php.ts +0 -354
- package/src/lib/wasm-error-reporting.ts +0 -172
- package/tsconfig.json +0 -22
- package/tsconfig.lib.json +0 -14
- package/tsconfig.spec.json +0 -20
- package/vite.config.ts +0 -47
- /package/{src/index.ts → index.d.ts} +0 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { Remote } from 'comlink';
|
|
2
|
+
import { PHPResponse } from './php-response';
|
|
3
|
+
/**
|
|
4
|
+
* Handles HTTP requests using PHP runtime as a backend.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
* @example Use PHPRequestHandler implicitly with a new PHP instance:
|
|
8
|
+
* ```js
|
|
9
|
+
* import { PHP } from '@php-wasm/web';
|
|
10
|
+
*
|
|
11
|
+
* const php = await PHP.load( '7.4', {
|
|
12
|
+
* requestHandler: {
|
|
13
|
+
* // PHP FS path to serve the files from:
|
|
14
|
+
* documentRoot: '/www',
|
|
15
|
+
*
|
|
16
|
+
* // Used to populate $_SERVER['SERVER_NAME'] etc.:
|
|
17
|
+
* absoluteUrl: 'http://127.0.0.1'
|
|
18
|
+
* }
|
|
19
|
+
* } );
|
|
20
|
+
*
|
|
21
|
+
* php.mkdirTree('/www');
|
|
22
|
+
* php.writeFile('/www/index.php', '<?php echo "Hi from PHP!"; ');
|
|
23
|
+
*
|
|
24
|
+
* const response = await php.request({ path: '/index.php' });
|
|
25
|
+
* console.log(response.text);
|
|
26
|
+
* // "Hi from PHP!"
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Explicitly create a PHPRequestHandler instance and run a PHP script:
|
|
30
|
+
* ```js
|
|
31
|
+
* import {
|
|
32
|
+
* loadPHPRuntime,
|
|
33
|
+
* PHP,
|
|
34
|
+
* PHPRequestHandler,
|
|
35
|
+
* getPHPLoaderModule,
|
|
36
|
+
* } from '@php-wasm/web';
|
|
37
|
+
*
|
|
38
|
+
* const runtime = await loadPHPRuntime( await getPHPLoaderModule('7.4') );
|
|
39
|
+
* const php = new PHP( runtime );
|
|
40
|
+
*
|
|
41
|
+
* php.mkdirTree('/www');
|
|
42
|
+
* php.writeFile('/www/index.php', '<?php echo "Hi from PHP!"; ');
|
|
43
|
+
*
|
|
44
|
+
* const server = new PHPRequestHandler(php, {
|
|
45
|
+
* // PHP FS path to serve the files from:
|
|
46
|
+
* documentRoot: '/www',
|
|
47
|
+
*
|
|
48
|
+
* // Used to populate $_SERVER['SERVER_NAME'] etc.:
|
|
49
|
+
* absoluteUrl: 'http://127.0.0.1'
|
|
50
|
+
* });
|
|
51
|
+
*
|
|
52
|
+
* const response = server.request({ path: '/index.php' });
|
|
53
|
+
* console.log(response.text);
|
|
54
|
+
* // "Hi from PHP!"
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export interface RequestHandler {
|
|
58
|
+
/**
|
|
59
|
+
* Serves the request – either by serving a static file, or by
|
|
60
|
+
* dispatching it to the PHP runtime.
|
|
61
|
+
* Cannot be used in conjunction with `cli()`.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```js
|
|
65
|
+
* const output = await php.request({
|
|
66
|
+
* method: 'GET',
|
|
67
|
+
* url: '/index.php',
|
|
68
|
+
* headers: {
|
|
69
|
+
* 'X-foo': 'bar',
|
|
70
|
+
* },
|
|
71
|
+
* formData: {
|
|
72
|
+
* foo: 'bar',
|
|
73
|
+
* },
|
|
74
|
+
* });
|
|
75
|
+
* console.log(output.stdout); // "Hello world!"
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @param request - PHP Request data.
|
|
79
|
+
*/
|
|
80
|
+
request(request: PHPRequest, maxRedirects?: number): Promise<PHPResponse>;
|
|
81
|
+
/**
|
|
82
|
+
* Converts a path to an absolute URL based at the PHPRequestHandler
|
|
83
|
+
* root.
|
|
84
|
+
*
|
|
85
|
+
* @param path The server path to convert to an absolute URL.
|
|
86
|
+
* @returns The absolute URL.
|
|
87
|
+
*/
|
|
88
|
+
pathToInternalUrl(path: string): string;
|
|
89
|
+
/**
|
|
90
|
+
* Converts an absolute URL based at the PHPRequestHandler to a relative path
|
|
91
|
+
* without the server pathname and scope.
|
|
92
|
+
*
|
|
93
|
+
* @param internalUrl An absolute URL based at the PHPRequestHandler root.
|
|
94
|
+
* @returns The relative path.
|
|
95
|
+
*/
|
|
96
|
+
internalUrlToPath(internalUrl: string): string;
|
|
97
|
+
/**
|
|
98
|
+
* The absolute URL of this PHPRequestHandler instance.
|
|
99
|
+
*/
|
|
100
|
+
absoluteUrl: string;
|
|
101
|
+
/**
|
|
102
|
+
* The directory in the PHP filesystem where the server will look
|
|
103
|
+
* for the files to serve. Default: `/var/www`.
|
|
104
|
+
*/
|
|
105
|
+
documentRoot: string;
|
|
106
|
+
}
|
|
107
|
+
export interface IsomorphicLocalPHP extends RequestHandler {
|
|
108
|
+
setPhpIniPath(path: string): void;
|
|
109
|
+
setPhpIniEntry(key: string, value: string): void;
|
|
110
|
+
/**
|
|
111
|
+
* Recursively creates a directory with the given path in the PHP filesystem.
|
|
112
|
+
* For example, if the path is `/root/php/data`, and `/root` already exists,
|
|
113
|
+
* it will create the directories `/root/php` and `/root/php/data`.
|
|
114
|
+
*
|
|
115
|
+
* @param path - The directory path to create.
|
|
116
|
+
*/
|
|
117
|
+
mkdir(path: string): void;
|
|
118
|
+
/**
|
|
119
|
+
* @deprecated Use mkdir instead.
|
|
120
|
+
*/
|
|
121
|
+
mkdirTree(path: string): void;
|
|
122
|
+
/**
|
|
123
|
+
* Reads a file from the PHP filesystem and returns it as a string.
|
|
124
|
+
*
|
|
125
|
+
* @throws {@link @php-wasm/universal:ErrnoError} – If the file doesn't exist.
|
|
126
|
+
* @param path - The file path to read.
|
|
127
|
+
* @returns The file contents.
|
|
128
|
+
*/
|
|
129
|
+
readFileAsText(path: string): string;
|
|
130
|
+
/**
|
|
131
|
+
* Reads a file from the PHP filesystem and returns it as an array buffer.
|
|
132
|
+
*
|
|
133
|
+
* @throws {@link @php-wasm/universal:ErrnoError} – If the file doesn't exist.
|
|
134
|
+
* @param path - The file path to read.
|
|
135
|
+
* @returns The file contents.
|
|
136
|
+
*/
|
|
137
|
+
readFileAsBuffer(path: string): Uint8Array;
|
|
138
|
+
/**
|
|
139
|
+
* Overwrites data in a file in the PHP filesystem.
|
|
140
|
+
* Creates a new file if one doesn't exist yet.
|
|
141
|
+
*
|
|
142
|
+
* @param path - The file path to write to.
|
|
143
|
+
* @param data - The data to write to the file.
|
|
144
|
+
*/
|
|
145
|
+
writeFile(path: string, data: string | Uint8Array): void;
|
|
146
|
+
/**
|
|
147
|
+
* Removes a file from the PHP filesystem.
|
|
148
|
+
*
|
|
149
|
+
* @throws {@link @php-wasm/universal:ErrnoError} – If the file doesn't exist.
|
|
150
|
+
* @param path - The file path to remove.
|
|
151
|
+
*/
|
|
152
|
+
unlink(path: string): void;
|
|
153
|
+
/**
|
|
154
|
+
* Moves a file or directory in the PHP filesystem to a
|
|
155
|
+
* new location.
|
|
156
|
+
*
|
|
157
|
+
* @param oldPath The path to rename.
|
|
158
|
+
* @param newPath The new path.
|
|
159
|
+
*/
|
|
160
|
+
mv(oldPath: string, newPath: string): void;
|
|
161
|
+
/**
|
|
162
|
+
* Removes a directory from the PHP filesystem.
|
|
163
|
+
*
|
|
164
|
+
* @param path The directory path to remove.
|
|
165
|
+
* @param options Options for the removal.
|
|
166
|
+
*/
|
|
167
|
+
rmdir(path: string, options?: RmDirOptions): void;
|
|
168
|
+
/**
|
|
169
|
+
* Lists the files and directories in the given directory.
|
|
170
|
+
*
|
|
171
|
+
* @param path - The directory path to list.
|
|
172
|
+
* @returns The list of files and directories in the given directory.
|
|
173
|
+
*/
|
|
174
|
+
listFiles(path: string): string[];
|
|
175
|
+
/**
|
|
176
|
+
* Checks if a directory exists in the PHP filesystem.
|
|
177
|
+
*
|
|
178
|
+
* @param path – The path to check.
|
|
179
|
+
* @returns True if the path is a directory, false otherwise.
|
|
180
|
+
*/
|
|
181
|
+
isDir(path: string): boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Checks if a file (or a directory) exists in the PHP filesystem.
|
|
184
|
+
*
|
|
185
|
+
* @param path - The file path to check.
|
|
186
|
+
* @returns True if the file exists, false otherwise.
|
|
187
|
+
*/
|
|
188
|
+
fileExists(path: string): boolean;
|
|
189
|
+
/**
|
|
190
|
+
* Changes the current working directory in the PHP filesystem.
|
|
191
|
+
* This is the directory that will be used as the base for relative paths.
|
|
192
|
+
* For example, if the current working directory is `/root/php`, and the
|
|
193
|
+
* path is `data`, the absolute path will be `/root/php/data`.
|
|
194
|
+
*
|
|
195
|
+
* @param path - The new working directory.
|
|
196
|
+
*/
|
|
197
|
+
chdir(path: string): void;
|
|
198
|
+
/**
|
|
199
|
+
* Runs PHP code.
|
|
200
|
+
* Cannot be used in conjunction with `cli()`.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```js
|
|
204
|
+
* const output = await php.run('<?php echo "Hello world!";');
|
|
205
|
+
* console.log(output.stdout); // "Hello world!"
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```js
|
|
210
|
+
* console.log(await php.run(`<?php
|
|
211
|
+
* $fp = fopen('php://stderr', 'w');
|
|
212
|
+
* fwrite($fp, "Hello, world!");
|
|
213
|
+
* `));
|
|
214
|
+
* // {"exitCode":0,"stdout":"","stderr":["Hello, world!"]}
|
|
215
|
+
* ```
|
|
216
|
+
*
|
|
217
|
+
* @param options - PHP run options.
|
|
218
|
+
*/
|
|
219
|
+
run(options: PHPRunOptions): Promise<PHPResponse>;
|
|
220
|
+
}
|
|
221
|
+
export type IsomorphicRemotePHP = Remote<IsomorphicLocalPHP>;
|
|
222
|
+
export type UniversalPHP = IsomorphicLocalPHP | IsomorphicRemotePHP;
|
|
223
|
+
export type HTTPMethod = 'GET' | 'POST' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'PUT' | 'DELETE';
|
|
224
|
+
export type PHPRequestHeaders = Record<string, string>;
|
|
225
|
+
export interface PHPRequest {
|
|
226
|
+
/**
|
|
227
|
+
* Request method. Default: `GET`.
|
|
228
|
+
*/
|
|
229
|
+
method?: HTTPMethod;
|
|
230
|
+
/**
|
|
231
|
+
* Request path or absolute URL.
|
|
232
|
+
*/
|
|
233
|
+
url: string;
|
|
234
|
+
/**
|
|
235
|
+
* Request headers.
|
|
236
|
+
*/
|
|
237
|
+
headers?: PHPRequestHeaders;
|
|
238
|
+
/**
|
|
239
|
+
* Uploaded files
|
|
240
|
+
*/
|
|
241
|
+
files?: Record<string, File>;
|
|
242
|
+
/**
|
|
243
|
+
* Request body without the files.
|
|
244
|
+
*/
|
|
245
|
+
body?: string;
|
|
246
|
+
/**
|
|
247
|
+
* Form data. If set, the request body will be ignored and
|
|
248
|
+
* the content-type header will be set to `application/x-www-form-urlencoded`.
|
|
249
|
+
*/
|
|
250
|
+
formData?: Record<string, unknown>;
|
|
251
|
+
}
|
|
252
|
+
export interface PHPRunOptions {
|
|
253
|
+
/**
|
|
254
|
+
* Request path following the domain:port part.
|
|
255
|
+
*/
|
|
256
|
+
relativeUri?: string;
|
|
257
|
+
/**
|
|
258
|
+
* Path of the .php file to execute.
|
|
259
|
+
*/
|
|
260
|
+
scriptPath?: string;
|
|
261
|
+
/**
|
|
262
|
+
* Request protocol.
|
|
263
|
+
*/
|
|
264
|
+
protocol?: string;
|
|
265
|
+
/**
|
|
266
|
+
* Request method. Default: `GET`.
|
|
267
|
+
*/
|
|
268
|
+
method?: HTTPMethod;
|
|
269
|
+
/**
|
|
270
|
+
* Request headers.
|
|
271
|
+
*/
|
|
272
|
+
headers?: PHPRequestHeaders;
|
|
273
|
+
/**
|
|
274
|
+
* Request body without the files.
|
|
275
|
+
*/
|
|
276
|
+
body?: string;
|
|
277
|
+
/**
|
|
278
|
+
* Uploaded files.
|
|
279
|
+
*/
|
|
280
|
+
fileInfos?: FileInfo[];
|
|
281
|
+
/**
|
|
282
|
+
* The code snippet to eval instead of a php file.
|
|
283
|
+
*/
|
|
284
|
+
code?: string;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Output of the PHP.wasm runtime.
|
|
288
|
+
*/
|
|
289
|
+
export interface PHPOutput {
|
|
290
|
+
/** Exit code of the PHP process. 0 means success, 1 and 2 mean error. */
|
|
291
|
+
exitCode: number;
|
|
292
|
+
/** Stdout data */
|
|
293
|
+
stdout: ArrayBuffer;
|
|
294
|
+
/** Stderr lines */
|
|
295
|
+
stderr: string[];
|
|
296
|
+
}
|
|
297
|
+
export interface FileInfo {
|
|
298
|
+
key: string;
|
|
299
|
+
name: string;
|
|
300
|
+
type: string;
|
|
301
|
+
data: Uint8Array;
|
|
302
|
+
}
|
|
303
|
+
export interface RmDirOptions {
|
|
304
|
+
/**
|
|
305
|
+
* If true, recursively removes the directory and all its contents.
|
|
306
|
+
* Default: true.
|
|
307
|
+
*/
|
|
308
|
+
recursive?: boolean;
|
|
309
|
+
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The default base used to convert a path into the URL object.
|
|
3
3
|
*/
|
|
4
|
-
export const DEFAULT_BASE_URL =
|
|
5
|
-
|
|
4
|
+
export declare const DEFAULT_BASE_URL = "http://example.com";
|
|
6
5
|
/**
|
|
7
6
|
* Returns a string representing the path, query, and
|
|
8
7
|
* fragment of the given URL.
|
|
@@ -16,10 +15,7 @@ export const DEFAULT_BASE_URL = 'http://example.com';
|
|
|
16
15
|
* @param url The URL.
|
|
17
16
|
* @returns The path, query, and fragment.
|
|
18
17
|
*/
|
|
19
|
-
export function toRelativeUrl(url: URL): string
|
|
20
|
-
return url.toString().substring(url.origin.length);
|
|
21
|
-
}
|
|
22
|
-
|
|
18
|
+
export declare function toRelativeUrl(url: URL): string;
|
|
23
19
|
/**
|
|
24
20
|
* Removes the given prefix from the given path.
|
|
25
21
|
*
|
|
@@ -33,13 +29,7 @@ export function toRelativeUrl(url: URL): string {
|
|
|
33
29
|
* @param prefix The prefix to remove.
|
|
34
30
|
* @returns Path with the prefix removed.
|
|
35
31
|
*/
|
|
36
|
-
export function removePathPrefix(path: string, prefix: string): string
|
|
37
|
-
if (!prefix || !path.startsWith(prefix)) {
|
|
38
|
-
return path;
|
|
39
|
-
}
|
|
40
|
-
return path.substring(prefix.length);
|
|
41
|
-
}
|
|
42
|
-
|
|
32
|
+
export declare function removePathPrefix(path: string, prefix: string): string;
|
|
43
33
|
/**
|
|
44
34
|
* Ensures the given path has the given prefix.
|
|
45
35
|
*
|
|
@@ -53,9 +43,4 @@ export function removePathPrefix(path: string, prefix: string): string {
|
|
|
53
43
|
* @param prefix
|
|
54
44
|
* @returns Path with the prefix added.
|
|
55
45
|
*/
|
|
56
|
-
export function ensurePathPrefix(path: string, prefix: string): string
|
|
57
|
-
if (!prefix || path.startsWith(prefix)) {
|
|
58
|
-
return path;
|
|
59
|
-
}
|
|
60
|
-
return prefix + path;
|
|
61
|
-
}
|
|
46
|
+
export declare function ensurePathPrefix(path: string, prefix: string): string;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
type Runtime = {
|
|
2
|
+
asm: Record<string, unknown>;
|
|
3
|
+
lastAsyncifyStackSource?: Error;
|
|
4
|
+
};
|
|
5
|
+
export declare class UnhandledRejectionsTarget extends EventTarget {
|
|
6
|
+
listenersCount: number;
|
|
7
|
+
addEventListener(type: unknown, callback: unknown): void;
|
|
8
|
+
removeEventListener(type: unknown, callback: unknown): void;
|
|
9
|
+
hasListeners(): boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Creates Asyncify errors listener.
|
|
13
|
+
*
|
|
14
|
+
* Emscripten turns Asyncify errors into unhandled rejections by
|
|
15
|
+
* throwing them outside of the context of the original function call.
|
|
16
|
+
*
|
|
17
|
+
* With this listener, we can catch and rethrow them in a proper context,
|
|
18
|
+
* or at least log them in a more readable way.
|
|
19
|
+
*
|
|
20
|
+
* @param runtime
|
|
21
|
+
*/
|
|
22
|
+
export declare function improveWASMErrorReporting(runtime: Runtime): UnhandledRejectionsTarget;
|
|
23
|
+
export declare function getFunctionsMaybeMissingFromAsyncify(): string[];
|
|
24
|
+
export declare function clarifyErrorMessage(crypticError: Error, asyncifyStack?: string): string;
|
|
25
|
+
export declare function showCriticalErrorBox(message: string): void;
|
|
26
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@php-wasm/universal",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.42",
|
|
4
4
|
"description": "PHP.wasm – emscripten bindings for PHP",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -29,11 +29,12 @@
|
|
|
29
29
|
"./package.json": "./package.json"
|
|
30
30
|
},
|
|
31
31
|
"publishConfig": {
|
|
32
|
-
"access": "public"
|
|
32
|
+
"access": "public",
|
|
33
|
+
"directory": "../../../dist/packages/php-wasm/universal"
|
|
33
34
|
},
|
|
34
35
|
"type": "module",
|
|
35
36
|
"main": "./index.cjs",
|
|
36
37
|
"module": "./index.js",
|
|
37
38
|
"license": "GPL-2.0-or-later",
|
|
38
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "14707001dbf7738ca8f173f3040559c6c165110e"
|
|
39
40
|
}
|
package/.eslintrc.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": ["../../../.eslintrc.json"],
|
|
3
|
-
"ignorePatterns": ["!**/*"],
|
|
4
|
-
"overrides": [
|
|
5
|
-
{
|
|
6
|
-
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
|
|
7
|
-
"rules": {}
|
|
8
|
-
},
|
|
9
|
-
{
|
|
10
|
-
"files": ["*.ts", "*.tsx"],
|
|
11
|
-
"rules": {}
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
"files": ["*.js", "*.jsx"],
|
|
15
|
-
"rules": {}
|
|
16
|
-
}
|
|
17
|
-
]
|
|
18
|
-
}
|