@php-wasm/node 0.1.18 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.cjs +23 -23
  2. package/index.d.ts +106 -20
  3. package/package.json +11 -4
package/index.d.ts CHANGED
@@ -1,10 +1,6 @@
1
1
  // Generated by dts-bundle-generator v7.2.0
2
2
 
3
- /**
4
- * PHP response. Body is an `ArrayBuffer` because it can
5
- * contain binary data.
6
- */
7
- export declare class PHPResponse {
3
+ export interface PHPResponseData {
8
4
  /**
9
5
  * Response headers.
10
6
  */
@@ -13,7 +9,7 @@ export declare class PHPResponse {
13
9
  * Response body. Contains the output from `echo`,
14
10
  * `print`, inline HTML etc.
15
11
  */
16
- private readonly body;
12
+ readonly bytes: ArrayBuffer;
17
13
  /**
18
14
  * Stderr contents, if any.
19
15
  */
@@ -27,7 +23,28 @@ export declare class PHPResponse {
27
23
  * Response HTTP status code, e.g. 200.
28
24
  */
29
25
  readonly httpStatusCode: number;
26
+ }
27
+ /**
28
+ * PHP response. Body is an `ArrayBuffer` because it can
29
+ * contain binary data.
30
+ *
31
+ * This type is used in Comlink.transferHandlers.set('PHPResponse', { ... })
32
+ * so be sure to update that if you change this type.
33
+ */
34
+ export declare class PHPResponse implements PHPResponseData {
35
+ /** @inheritDoc */
36
+ readonly headers: Record<string, string[]>;
37
+ /** @inheritDoc */
38
+ readonly bytes: ArrayBuffer;
39
+ /** @inheritDoc */
40
+ readonly errors: string;
41
+ /** @inheritDoc */
42
+ readonly exitCode: number;
43
+ /** @inheritDoc */
44
+ readonly httpStatusCode: number;
30
45
  constructor(httpStatusCode: number, headers: Record<string, string[]>, body: ArrayBuffer, errors?: string, exitCode?: number);
46
+ static fromRawData(data: PHPResponseData): PHPResponse;
47
+ toRawData(): PHPResponseData;
31
48
  /**
32
49
  * Response body as JSON.
33
50
  */
@@ -36,20 +53,36 @@ export declare class PHPResponse {
36
53
  * Response body as text.
37
54
  */
38
55
  get text(): string;
56
+ }
57
+ export type HTTPMethod = "GET" | "POST" | "HEAD" | "OPTIONS" | "PATCH" | "PUT" | "DELETE";
58
+ export type PHPRequestHeaders = Record<string, string>;
59
+ export interface PHPRequest {
39
60
  /**
40
- * Response body as bytes.
61
+ * Request method. Default: `GET`.
62
+ */
63
+ method?: HTTPMethod;
64
+ /**
65
+ * Request path or absolute URL.
41
66
  */
42
- get bytes(): ArrayBuffer;
43
- }
44
- export type PHPRequest = Pick<PHPRunOptions, "method" | "headers"> & {
45
67
  url: string;
68
+ /**
69
+ * Request headers.
70
+ */
71
+ headers?: PHPRequestHeaders;
72
+ /**
73
+ * Uploaded files
74
+ */
46
75
  files?: Record<string, File>;
47
- } & ((Pick<PHPRunOptions, "body"> & {
48
- formData?: never;
49
- }) | {
50
- body?: never;
51
- formData: Record<string, unknown>;
52
- });
76
+ /**
77
+ * Request body without the files.
78
+ */
79
+ body?: string;
80
+ /**
81
+ * Form data. If set, the request body will be ignored and
82
+ * the content-type header will be set to `application/x-www-form-urlencoded`.
83
+ */
84
+ formData?: Record<string, unknown>;
85
+ }
53
86
  export interface PHPRequestHandlerConfiguration {
54
87
  /**
55
88
  * The directory in the PHP filesystem where the server will look
@@ -66,7 +99,62 @@ export interface PHPRequestHandlerConfiguration {
66
99
  */
67
100
  isStaticFilePath?: (path: string) => boolean;
68
101
  }
69
- declare class PHPRequestHandler {
102
+ /**
103
+ * A fake PHP server that handles HTTP requests but does not
104
+ * bind to any port.
105
+ *
106
+ * @public
107
+ * @example Use PHPRequestHandler implicitly with a new PHP instance:
108
+ * ```js
109
+ * import { PHP } from '@php-wasm/web';
110
+ *
111
+ * const php = await PHP.load( '7.4', {
112
+ * requestHandler: {
113
+ * // PHP FS path to serve the files from:
114
+ * documentRoot: '/www',
115
+ *
116
+ * // Used to populate $_SERVER['SERVER_NAME'] etc.:
117
+ * absoluteUrl: 'http://127.0.0.1'
118
+ * }
119
+ * } );
120
+ *
121
+ * php.mkdirTree('/www');
122
+ * php.writeFile('/www/index.php', '<?php echo "Hi from PHP!"; ');
123
+ *
124
+ * const response = await php.request({ path: '/index.php' });
125
+ * console.log(response.text);
126
+ * // "Hi from PHP!"
127
+ * ```
128
+ *
129
+ * @example Explicitly create a PHPRequestHandler instance and run a PHP script:
130
+ * ```js
131
+ * import {
132
+ * loadPHPRuntime,
133
+ * PHP,
134
+ * PHPRequestHandler,
135
+ * getPHPLoaderModule,
136
+ * } from '@php-wasm/web';
137
+ *
138
+ * const runtime = await loadPHPRuntime( await getPHPLoaderModule('7.4') );
139
+ * const php = new PHP( runtime );
140
+ *
141
+ * php.mkdirTree('/www');
142
+ * php.writeFile('/www/index.php', '<?php echo "Hi from PHP!"; ');
143
+ *
144
+ * const server = new PHPRequestHandler(php, {
145
+ * // PHP FS path to serve the files from:
146
+ * documentRoot: '/www',
147
+ *
148
+ * // Used to populate $_SERVER['SERVER_NAME'] etc.:
149
+ * absoluteUrl: 'http://127.0.0.1'
150
+ * });
151
+ *
152
+ * const response = server.request({ path: '/index.php' });
153
+ * console.log(response.text);
154
+ * // "Hi from PHP!"
155
+ * ```
156
+ */
157
+ export declare class PHPRequestHandler {
70
158
  #private;
71
159
  /**
72
160
  * The PHP instance
@@ -153,7 +241,6 @@ export declare class PHPBrowser implements WithRequestHandler {
153
241
  request(request: PHPRequest, redirects?: number): Promise<PHPResponse>;
154
242
  }
155
243
  export type RuntimeType = "NODE" | "WEB" | "WORKER";
156
- export type PHPRequestHeaders = Record<string, string>;
157
244
  export interface FileInfo {
158
245
  key: string;
159
246
  name: string;
@@ -176,7 +263,7 @@ export interface PHPRunOptions {
176
263
  /**
177
264
  * Request method. Default: `GET`.
178
265
  */
179
- method?: "GET" | "POST" | "HEAD" | "OPTIONS" | "PATCH" | "PUT" | "DELETE";
266
+ method?: HTTPMethod;
180
267
  /**
181
268
  * Request headers.
182
269
  */
@@ -474,7 +561,6 @@ export declare class PHP extends BasePHP {
474
561
  * resolves when the PHP instance is ready.
475
562
  *
476
563
  * @see load
477
- * @inheritdoc load
478
564
  */
479
565
  static loadSync(phpVersion: SupportedPHPVersion, options?: PHPLoaderOptions): {
480
566
  php: PHP;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@php-wasm/node",
3
- "version": "0.1.18",
3
+ "version": "0.1.19",
4
4
  "description": "PHP.wasm for Node.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,16 +15,23 @@
15
15
  "url": "https://github.com/adamziel"
16
16
  }
17
17
  ],
18
+ "typedoc": {
19
+ "entryPoint": "./src/index.ts",
20
+ "readmeFile": "./README.md",
21
+ "displayName": "@php-wasm/node",
22
+ "tsconfig": "./tsconfig.lib.json"
23
+ },
18
24
  "publishConfig": {
19
25
  "access": "public",
20
26
  "directory": "../../../dist/packages/php-wasm/node"
21
27
  },
22
28
  "license": "(GPL-2.0-or-later OR MPL-2.0)",
23
- "gitHead": "2de8f9182b249b7e87694efaa06e7f25b64b7bb3",
29
+ "main": "index.js",
30
+ "types": "index.d.ts",
31
+ "gitHead": "ce0d93a79ad524677200b7e956ae1543d774696e",
24
32
  "dependencies": {
25
33
  "comlink": "4.4.1",
26
34
  "express": "4.18.2",
27
35
  "ws": "8.13.0"
28
- },
29
- "main": "index.cjs"
36
+ }
30
37
  }