@php-wasm/universal 0.1.40 → 0.1.41

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.
@@ -0,0 +1,70 @@
1
+ import { PHPBrowser } from './php-browser';
2
+ import { PHPRequestHandlerConfiguration } from './php-request-handler';
3
+ import { PHPResponse } from './php-response';
4
+ import type { PHPRuntimeId } from './load-php-runtime';
5
+ import { IsomorphicLocalPHP, PHPRequest, PHPRunOptions, RmDirOptions } from './universal-php';
6
+ export declare const __private__dont__use: unique symbol;
7
+ /**
8
+ * An environment-agnostic wrapper around the Emscripten PHP runtime
9
+ * that universals the super low-level API and provides a more convenient
10
+ * higher-level API.
11
+ *
12
+ * It exposes a minimal set of methods to run PHP scripts and to
13
+ * interact with the PHP filesystem.
14
+ */
15
+ export declare abstract class BasePHP implements IsomorphicLocalPHP {
16
+ #private;
17
+ protected [__private__dont__use]: any;
18
+ requestHandler?: PHPBrowser;
19
+ /**
20
+ * Initializes a PHP runtime.
21
+ *
22
+ * @internal
23
+ * @param PHPRuntime - Optional. PHP Runtime ID as initialized by loadPHPRuntime.
24
+ * @param serverOptions - Optional. Options for the PHPRequestHandler. If undefined, no request handler will be initialized.
25
+ */
26
+ constructor(PHPRuntimeId?: PHPRuntimeId, serverOptions?: PHPRequestHandlerConfiguration);
27
+ /** @inheritDoc */
28
+ get absoluteUrl(): string;
29
+ /** @inheritDoc */
30
+ get documentRoot(): string;
31
+ /** @inheritDoc */
32
+ pathToInternalUrl(path: string): string;
33
+ /** @inheritDoc */
34
+ internalUrlToPath(internalUrl: string): string;
35
+ initializeRuntime(runtimeId: PHPRuntimeId): void;
36
+ /** @inheritDoc */
37
+ setPhpIniPath(path: string): void;
38
+ /** @inheritDoc */
39
+ setPhpIniEntry(key: string, value: string): void;
40
+ /** @inheritDoc */
41
+ chdir(path: string): void;
42
+ /** @inheritDoc */
43
+ request(request: PHPRequest, maxRedirects?: number): Promise<PHPResponse>;
44
+ /** @inheritDoc */
45
+ run(request: PHPRunOptions): Promise<PHPResponse>;
46
+ addServerGlobalEntry(key: string, value: string): void;
47
+ /** @inheritDoc */
48
+ mkdir(path: string): void;
49
+ /** @inheritDoc */
50
+ mkdirTree(path: string): void;
51
+ /** @inheritDoc */
52
+ readFileAsText(path: string): string;
53
+ /** @inheritDoc */
54
+ readFileAsBuffer(path: string): Uint8Array;
55
+ /** @inheritDoc */
56
+ writeFile(path: string, data: string | Uint8Array): void;
57
+ /** @inheritDoc */
58
+ unlink(path: string): void;
59
+ /** @inheritDoc */
60
+ mv(fromPath: string, toPath: string): void;
61
+ /** @inheritDoc */
62
+ rmdir(path: string, options?: RmDirOptions): void;
63
+ /** @inheritDoc */
64
+ listFiles(path: string): string[];
65
+ /** @inheritDoc */
66
+ isDir(path: string): boolean;
67
+ /** @inheritDoc */
68
+ fileExists(path: string): boolean;
69
+ }
70
+ export declare function normalizeHeaders(headers: PHPRunOptions['headers']): PHPRunOptions['headers'];
@@ -0,0 +1,30 @@
1
+ declare const kError: unique symbol;
2
+ declare const kMessage: unique symbol;
3
+ interface ErrorEventOptions {
4
+ error?: Error;
5
+ message?: string;
6
+ }
7
+ /**
8
+ * Class representing an error event.
9
+ *
10
+ * @extends Event
11
+ */
12
+ declare class ErrorEvent2 extends Event {
13
+ [kError]: any;
14
+ [kMessage]: any;
15
+ /**
16
+ * Create a new `ErrorEvent`.
17
+ *
18
+ * @param type The name of the event
19
+ * @param options A dictionary object that allows for setting
20
+ * attributes via object members of the same name.
21
+ */
22
+ constructor(type: 'error', options?: ErrorEventOptions);
23
+ get error(): any;
24
+ get message(): any;
25
+ }
26
+ export declare const ErrorEvent: typeof ErrorEvent2 | {
27
+ new (type: string, eventInitDict?: ErrorEventInit | undefined): ErrorEvent;
28
+ prototype: ErrorEvent;
29
+ };
30
+ export {};
@@ -1,49 +1,17 @@
1
- export type {
2
- FileInfo,
3
- IsomorphicLocalPHP,
4
- IsomorphicRemotePHP,
5
- PHPOutput,
6
- PHPRunOptions,
7
- UniversalPHP,
8
- RmDirOptions,
9
- HTTPMethod,
10
- PHPRequest,
11
- PHPRequestHeaders,
12
- RequestHandler,
13
- } from './universal-php';
14
-
1
+ export type { FileInfo, IsomorphicLocalPHP, IsomorphicRemotePHP, PHPOutput, PHPRunOptions, UniversalPHP, RmDirOptions, HTTPMethod, PHPRequest, PHPRequestHeaders, RequestHandler, } from './universal-php';
15
2
  export { PHPResponse } from './php-response';
16
3
  export type { PHPResponseData } from './php-response';
17
4
  export type { ErrnoError } from './rethrow-file-system-error';
18
- export {
19
- LatestSupportedPHPVersion,
20
- SupportedPHPVersions,
21
- SupportedPHPVersionsList,
22
- } from './supported-php-versions';
5
+ export { LatestSupportedPHPVersion, SupportedPHPVersions, SupportedPHPVersionsList, } from './supported-php-versions';
23
6
  export type { SupportedPHPVersion } from './supported-php-versions';
24
7
  export { BasePHP, __private__dont__use } from './base-php';
25
8
  export { loadPHPRuntime } from './load-php-runtime';
26
- export type {
27
- DataModule,
28
- EmscriptenOptions,
29
- PHPLoaderModule,
30
- PHPRuntime,
31
- PHPRuntimeId,
32
- RuntimeType,
33
- } from './load-php-runtime';
9
+ export type { DataModule, EmscriptenOptions, PHPLoaderModule, PHPRuntime, PHPRuntimeId, RuntimeType, } from './load-php-runtime';
34
10
  export { rethrowFileSystemError } from './rethrow-file-system-error';
35
-
36
11
  export { isLocalPHP } from './is-local-php';
37
12
  export { isRemotePHP } from './is-remote-php';
38
-
39
13
  export type { PHPRequestHandlerConfiguration } from './php-request-handler';
40
14
  export { PHPRequestHandler } from './php-request-handler';
41
15
  export type { PHPBrowserConfiguration } from './php-browser';
42
16
  export { PHPBrowser } from './php-browser';
43
-
44
- export {
45
- DEFAULT_BASE_URL,
46
- ensurePathPrefix,
47
- removePathPrefix,
48
- toRelativeUrl,
49
- } from './urls';
17
+ export { DEFAULT_BASE_URL, ensurePathPrefix, removePathPrefix, toRelativeUrl, } from './urls';
@@ -0,0 +1,2 @@
1
+ import { IsomorphicLocalPHP, UniversalPHP } from './universal-php';
2
+ export declare function isLocalPHP(playground: UniversalPHP): playground is IsomorphicLocalPHP;
@@ -0,0 +1,2 @@
1
+ import { IsomorphicRemotePHP, UniversalPHP } from './universal-php';
2
+ export declare function isRemotePHP(playground: UniversalPHP): playground is IsomorphicRemotePHP;
@@ -115,113 +115,35 @@
115
115
  * @param dataDependenciesModules - A list of the ESM-wrapped Emscripten data dependency modules.
116
116
  * @returns Loaded runtime id.
117
117
  */
118
-
119
- export async function loadPHPRuntime(
120
- phpLoaderModule: PHPLoaderModule,
121
- phpModuleArgs: EmscriptenOptions = {},
122
- dataDependenciesModules: DataModule[] = []
123
- ): Promise<number> {
124
- let resolvePhpReady: any, resolveDepsReady: any;
125
- const depsReady = new Promise((resolve) => {
126
- resolveDepsReady = resolve;
127
- });
128
- const phpReady = new Promise((resolve) => {
129
- resolvePhpReady = resolve;
130
- });
131
-
132
- const PHPRuntime = phpLoaderModule.init(currentJsRuntime, {
133
- onAbort(reason) {
134
- console.error('WASM aborted: ');
135
- console.error(reason);
136
- },
137
- ENV: {},
138
- // Emscripten sometimes prepends a '/' to the path, which
139
- // breaks vite dev mode. An identity `locateFile` function
140
- // fixes it.
141
- locateFile: (path) => path,
142
- ...phpModuleArgs,
143
- noInitialRun: true,
144
- onRuntimeInitialized() {
145
- if (phpModuleArgs.onRuntimeInitialized) {
146
- phpModuleArgs.onRuntimeInitialized();
147
- }
148
- resolvePhpReady();
149
- },
150
- monitorRunDependencies(nbLeft) {
151
- if (nbLeft === 0) {
152
- delete PHPRuntime.monitorRunDependencies;
153
- resolveDepsReady();
154
- }
155
- },
156
- });
157
- for (const { default: loadDataModule } of dataDependenciesModules) {
158
- loadDataModule(PHPRuntime);
159
- }
160
- if (!dataDependenciesModules.length) {
161
- resolveDepsReady();
162
- }
163
-
164
- await depsReady;
165
- await phpReady;
166
-
167
- loadedRuntimes.push(PHPRuntime);
168
- return loadedRuntimes.length - 1;
169
- }
170
-
118
+ export declare function loadPHPRuntime(phpLoaderModule: PHPLoaderModule, phpModuleArgs?: EmscriptenOptions, dataDependenciesModules?: DataModule[]): Promise<number>;
171
119
  export type RuntimeType = 'NODE' | 'WEB' | 'WORKER';
172
-
173
- declare const self: WindowOrWorkerGlobalScope;
174
- declare const WorkerGlobalScope: object | undefined;
175
-
176
120
  export type PHPRuntimeId = number;
177
- const loadedRuntimes: PHPRuntime[] = [];
178
-
179
- export function getLoadedRuntime(id: PHPRuntimeId): PHPRuntime {
180
- return loadedRuntimes[id];
181
- }
182
-
183
- export const currentJsRuntime = (function () {
184
- if (typeof process !== 'undefined' && process.release?.name === 'node') {
185
- return 'NODE';
186
- } else if (typeof window !== 'undefined') {
187
- return 'WEB';
188
- } else if (
189
- typeof WorkerGlobalScope !== 'undefined' &&
190
- self instanceof (WorkerGlobalScope as any)
191
- ) {
192
- return 'WORKER';
193
- } else {
194
- return 'NODE';
195
- }
196
- })();
197
-
121
+ export declare function getLoadedRuntime(id: PHPRuntimeId): PHPRuntime;
122
+ export declare const currentJsRuntime: string;
198
123
  export type PHPRuntime = any;
199
-
200
124
  export type PHPLoaderModule = {
201
- dependencyFilename: string;
202
- dependenciesTotalSize: number;
203
- init: (jsRuntime: string, options: EmscriptenOptions) => PHPRuntime;
125
+ dependencyFilename: string;
126
+ dependenciesTotalSize: number;
127
+ init: (jsRuntime: string, options: EmscriptenOptions) => PHPRuntime;
204
128
  };
205
-
206
129
  export type DataModule = {
207
- dependencyFilename: string;
208
- dependenciesTotalSize: number;
209
- default: (phpRuntime: PHPRuntime) => void;
130
+ dependencyFilename: string;
131
+ dependenciesTotalSize: number;
132
+ default: (phpRuntime: PHPRuntime) => void;
210
133
  };
211
-
212
134
  export type EmscriptenOptions = {
213
- onAbort?: (message: string) => void;
214
- /**
215
- * Set to true for debugging tricky WebAssembly errors.
216
- */
217
- debug?: boolean;
218
- ENV?: Record<string, string>;
219
- locateFile?: (path: string) => string;
220
- noInitialRun?: boolean;
221
- dataFileDownloads?: Record<string, number>;
222
- print?: (message: string) => void;
223
- printErr?: (message: string) => void;
224
- quit?: (status: number, toThrow: any) => void;
225
- onRuntimeInitialized?: () => void;
226
- monitorRunDependencies?: (left: number) => void;
135
+ onAbort?: (message: string) => void;
136
+ /**
137
+ * Set to true for debugging tricky WebAssembly errors.
138
+ */
139
+ debug?: boolean;
140
+ ENV?: Record<string, string>;
141
+ locateFile?: (path: string) => string;
142
+ noInitialRun?: boolean;
143
+ dataFileDownloads?: Record<string, number>;
144
+ print?: (message: string) => void;
145
+ printErr?: (message: string) => void;
146
+ quit?: (status: number, toThrow: any) => void;
147
+ onRuntimeInitialized?: () => void;
148
+ monitorRunDependencies?: (left: number) => void;
227
149
  } & Record<string, any>;
@@ -0,0 +1,52 @@
1
+ import type { PHPRequestHandler } from './php-request-handler';
2
+ import type { PHPResponse } from './php-response';
3
+ import { PHPRequest, RequestHandler } from './universal-php';
4
+ export interface PHPBrowserConfiguration {
5
+ /**
6
+ * Should handle redirects internally?
7
+ */
8
+ handleRedirects?: boolean;
9
+ /**
10
+ * The maximum number of redirects to follow internally. Once
11
+ * exceeded, request() will return the redirecting response.
12
+ */
13
+ maxRedirects?: number;
14
+ }
15
+ /**
16
+ * A fake web browser that handles PHPRequestHandler's cookies and redirects
17
+ * internally without exposing them to the consumer.
18
+ *
19
+ * @public
20
+ */
21
+ export declare class PHPBrowser implements RequestHandler {
22
+ #private;
23
+ requestHandler: PHPRequestHandler;
24
+ /**
25
+ * @param server - The PHP server to browse.
26
+ * @param config - The browser configuration.
27
+ */
28
+ constructor(requestHandler: PHPRequestHandler, config?: PHPBrowserConfiguration);
29
+ /**
30
+ * Sends the request to the server.
31
+ *
32
+ * When cookies are present in the response, this method stores
33
+ * them and sends them with any subsequent requests.
34
+ *
35
+ * When a redirection is present in the response, this method
36
+ * follows it by discarding a response and sending a subsequent
37
+ * request.
38
+ *
39
+ * @param request - The request.
40
+ * @param redirects - Internal. The number of redirects handled so far.
41
+ * @returns PHPRequestHandler response.
42
+ */
43
+ request(request: PHPRequest, redirects?: number): Promise<PHPResponse>;
44
+ /** @inheritDoc */
45
+ pathToInternalUrl(path: string): string;
46
+ /** @inheritDoc */
47
+ internalUrlToPath(internalUrl: string): string;
48
+ /** @inheritDoc */
49
+ get absoluteUrl(): string;
50
+ /** @inheritDoc */
51
+ get documentRoot(): string;
52
+ }
@@ -0,0 +1,43 @@
1
+ import { BasePHP } from './base-php';
2
+ import { PHPResponse } from './php-response';
3
+ import { PHPRequest, RequestHandler } from './universal-php';
4
+ export interface PHPRequestHandlerConfiguration {
5
+ /**
6
+ * The directory in the PHP filesystem where the server will look
7
+ * for the files to serve. Default: `/var/www`.
8
+ */
9
+ documentRoot?: string;
10
+ /**
11
+ * Request Handler URL. Used to populate $_SERVER details like HTTP_HOST.
12
+ */
13
+ absoluteUrl?: string;
14
+ /**
15
+ * Callback used by the PHPRequestHandler to decide whether
16
+ * the requested path refers to a PHP file or a static file.
17
+ */
18
+ isStaticFilePath?: (path: string) => boolean;
19
+ }
20
+ /** @inheritDoc */
21
+ export declare class PHPRequestHandler implements RequestHandler {
22
+ #private;
23
+ /**
24
+ * The PHP instance
25
+ */
26
+ php: BasePHP;
27
+ /**
28
+ * @param php - The PHP instance.
29
+ * @param config - Request Handler configuration.
30
+ */
31
+ constructor(php: BasePHP, config?: PHPRequestHandlerConfiguration);
32
+ /** @inheritDoc */
33
+ pathToInternalUrl(path: string): string;
34
+ /** @inheritDoc */
35
+ internalUrlToPath(internalUrl: string): string;
36
+ get isRequestRunning(): boolean;
37
+ /** @inheritDoc */
38
+ get absoluteUrl(): string;
39
+ /** @inheritDoc */
40
+ get documentRoot(): string;
41
+ /** @inheritDoc */
42
+ request(request: PHPRequest): Promise<PHPResponse>;
43
+ }
@@ -0,0 +1,54 @@
1
+ export interface PHPResponseData {
2
+ /**
3
+ * Response headers.
4
+ */
5
+ readonly headers: Record<string, string[]>;
6
+ /**
7
+ * Response body. Contains the output from `echo`,
8
+ * `print`, inline HTML etc.
9
+ */
10
+ readonly bytes: ArrayBuffer;
11
+ /**
12
+ * Stderr contents, if any.
13
+ */
14
+ readonly errors: string;
15
+ /**
16
+ * The exit code of the script. `0` is a success, while
17
+ * `1` and `2` indicate an error.
18
+ */
19
+ readonly exitCode: number;
20
+ /**
21
+ * Response HTTP status code, e.g. 200.
22
+ */
23
+ readonly httpStatusCode: number;
24
+ }
25
+ /**
26
+ * PHP response. Body is an `ArrayBuffer` because it can
27
+ * contain binary data.
28
+ *
29
+ * This type is used in Comlink.transferHandlers.set('PHPResponse', \{ ... \})
30
+ * so be sure to update that if you change this type.
31
+ */
32
+ export declare class PHPResponse implements PHPResponseData {
33
+ /** @inheritDoc */
34
+ readonly headers: Record<string, string[]>;
35
+ /** @inheritDoc */
36
+ readonly bytes: ArrayBuffer;
37
+ /** @inheritDoc */
38
+ readonly errors: string;
39
+ /** @inheritDoc */
40
+ readonly exitCode: number;
41
+ /** @inheritDoc */
42
+ readonly httpStatusCode: number;
43
+ constructor(httpStatusCode: number, headers: Record<string, string[]>, body: ArrayBuffer, errors?: string, exitCode?: number);
44
+ static fromRawData(data: PHPResponseData): PHPResponse;
45
+ toRawData(): PHPResponseData;
46
+ /**
47
+ * Response body as JSON.
48
+ */
49
+ get json(): any;
50
+ /**
51
+ * Response body as text.
52
+ */
53
+ get text(): string;
54
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Emscripten's filesystem-related Exception.
3
+ *
4
+ * @see https://emscripten.org/docs/api_reference/Filesystem-API.html
5
+ * @see https://github.com/emscripten-core/emscripten/blob/main/system/lib/libc/musl/arch/emscripten/bits/errno.h
6
+ * @see https://github.com/emscripten-core/emscripten/blob/38eedc630f17094b3202fd48ac0c2c585dbea31e/system/include/wasi/api.h#L336
7
+ */
8
+ export interface ErrnoError extends Error {
9
+ node?: any;
10
+ errno: number;
11
+ message: string;
12
+ }
13
+ export declare function rethrowFileSystemError(messagePrefix?: string): (target: any, methodName: string, descriptor: PropertyDescriptor) => void;
@@ -0,0 +1,4 @@
1
+ export declare const SupportedPHPVersions: readonly ["8.2", "8.1", "8.0", "7.4", "7.3", "7.2", "7.1", "7.0", "5.6"];
2
+ export declare const LatestSupportedPHPVersion: "8.2";
3
+ export declare const SupportedPHPVersionsList: string[];
4
+ export type SupportedPHPVersion = (typeof SupportedPHPVersions)[number];