@php-wasm/web 0.6.16 → 0.7.0

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.d.ts +100 -201
  2. package/index.js +363 -298
  3. package/package.json +6 -6
package/index.d.ts CHANGED
@@ -106,137 +106,17 @@ export type PHPEvent = PHPRequestEndEvent | PHPRequestErrorEvent | PHPRuntimeIni
106
106
  * A callback function that handles PHP events.
107
107
  */
108
108
  export type PHPEventListener = (event: PHPEvent) => void;
109
- /**
110
- * Handles HTTP requests using PHP runtime as a backend.
111
- *
112
- * @public
113
- * @example Use PHPRequestHandler implicitly with a new PHP instance:
114
- * ```js
115
- * import { PHP } from '@php-wasm/web';
116
- *
117
- * const php = await PHP.load( '7.4', {
118
- * requestHandler: {
119
- * // PHP FS path to serve the files from:
120
- * documentRoot: '/www',
121
- *
122
- * // Used to populate $_SERVER['SERVER_NAME'] etc.:
123
- * absoluteUrl: 'http://127.0.0.1'
124
- * }
125
- * } );
126
- *
127
- * php.mkdirTree('/www');
128
- * php.writeFile('/www/index.php', '<?php echo "Hi from PHP!"; ');
129
- *
130
- * const response = await php.request({ path: '/index.php' });
131
- * console.log(response.text);
132
- * // "Hi from PHP!"
133
- * ```
134
- *
135
- * @example Explicitly create a PHPRequestHandler instance and run a PHP script:
136
- * ```js
137
- * import {
138
- * loadPHPRuntime,
139
- * PHP,
140
- * PHPRequestHandler,
141
- * getPHPLoaderModule,
142
- * } from '@php-wasm/web';
143
- *
144
- * const runtime = await loadPHPRuntime( await getPHPLoaderModule('7.4') );
145
- * const php = new PHP( runtime );
146
- *
147
- * php.mkdirTree('/www');
148
- * php.writeFile('/www/index.php', '<?php echo "Hi from PHP!"; ');
149
- *
150
- * const server = new PHPRequestHandler(php, {
151
- * // PHP FS path to serve the files from:
152
- * documentRoot: '/www',
153
- *
154
- * // Used to populate $_SERVER['SERVER_NAME'] etc.:
155
- * absoluteUrl: 'http://127.0.0.1'
156
- * });
157
- *
158
- * const response = server.request({ path: '/index.php' });
159
- * console.log(response.text);
160
- * // "Hi from PHP!"
161
- * ```
162
- */
163
- export interface RequestHandler {
164
- /**
165
- * Serves the request – either by serving a static file, or by
166
- * dispatching it to the PHP runtime.
167
- *
168
- * The request() method mode behaves like a web server and only works if
169
- * the PHP was initialized with a `requestHandler` option (which the online version
170
- * of WordPress Playground does by default).
171
- *
172
- * In the request mode, you pass an object containing the request information
173
- * (method, headers, body, etc.) and the path to the PHP file to run:
174
- *
175
- * ```ts
176
- * const php = PHP.load('7.4', {
177
- * requestHandler: {
178
- * documentRoot: "/www"
179
- * }
180
- * })
181
- * php.writeFile("/www/index.php", `<?php echo file_get_contents("php://input");`);
182
- * const result = await php.request({
183
- * method: "GET",
184
- * headers: {
185
- * "Content-Type": "text/plain"
186
- * },
187
- * body: "Hello world!",
188
- * path: "/www/index.php"
189
- * });
190
- * // result.text === "Hello world!"
191
- * ```
192
- *
193
- * The `request()` method cannot be used in conjunction with `cli()`.
194
- *
195
- * @example
196
- * ```js
197
- * const output = await php.request({
198
- * method: 'GET',
199
- * url: '/index.php',
200
- * headers: {
201
- * 'X-foo': 'bar',
202
- * },
203
- * body: {
204
- * foo: 'bar',
205
- * },
206
- * });
207
- * console.log(output.stdout); // "Hello world!"
208
- * ```
209
- *
210
- * @param request - PHP Request data.
211
- */
212
- request(request: PHPRequest, maxRedirects?: number): Promise<PHPResponse>;
213
- /**
214
- * Converts a path to an absolute URL based at the PHPRequestHandler
215
- * root.
216
- *
217
- * @param path The server path to convert to an absolute URL.
218
- * @returns The absolute URL.
219
- */
109
+ export interface IsomorphicLocalPHP {
110
+ /** @deprecated Use PHPRequestHandler instead. */
111
+ request(request: PHPRequest): Promise<PHPResponse>;
112
+ /** @deprecated Use PHPRequestHandler instead. */
220
113
  pathToInternalUrl(path: string): string;
221
- /**
222
- * Converts an absolute URL based at the PHPRequestHandler to a relative path
223
- * without the server pathname and scope.
224
- *
225
- * @param internalUrl An absolute URL based at the PHPRequestHandler root.
226
- * @returns The relative path.
227
- */
114
+ /** @deprecated Use PHPRequestHandler instead. */
228
115
  internalUrlToPath(internalUrl: string): string;
229
- /**
230
- * The absolute URL of this PHPRequestHandler instance.
231
- */
116
+ /** @deprecated Use PHPRequestHandler instead. */
232
117
  absoluteUrl: string;
233
- /**
234
- * The directory in the PHP filesystem where the server will look
235
- * for the files to serve. Default: `/var/www`.
236
- */
118
+ /** @deprecated Use PHPRequestHandler instead. */
237
119
  documentRoot: string;
238
- }
239
- export interface IsomorphicLocalPHP extends RequestHandler {
240
120
  /**
241
121
  * Sets the SAPI name exposed by the PHP module.
242
122
  * @param newName - The new SAPI name.
@@ -472,14 +352,6 @@ export interface IsomorphicLocalPHP extends RequestHandler {
472
352
  * @param listener Callback function to handle the message.
473
353
  */
474
354
  onMessage(listener: MessageListener): void;
475
- /**
476
- * Registers a handler to spawns a child process when
477
- * `proc_open()`, `popen()`, `exec()`, `system()`, or `passthru()`
478
- * is called.
479
- *
480
- * @param handler Callback function to spawn a process.
481
- */
482
- setSpawnHandler(handler: SpawnHandler | string): void;
483
355
  }
484
356
  export type MessageListener = (data: string) => Promise<string | Uint8Array | void> | string | void;
485
357
  export interface EventEmitter {
@@ -542,6 +414,10 @@ export interface PHPRunOptions {
542
414
  * Environment variables to set for this run.
543
415
  */
544
416
  env?: Record<string, string>;
417
+ /**
418
+ * $_SERVER entries to set for this run.
419
+ */
420
+ $_SERVER?: Record<string, string>;
545
421
  /**
546
422
  * The code snippet to eval instead of a php file.
547
423
  */
@@ -592,7 +468,7 @@ export interface PHPRequestHandlerConfiguration {
592
468
  */
593
469
  rewriteRules?: RewriteRule[];
594
470
  }
595
- declare class PHPRequestHandler implements RequestHandler {
471
+ declare class PHPRequestHandler {
596
472
  #private;
597
473
  rewriteRules: RewriteRule[];
598
474
  /**
@@ -604,62 +480,81 @@ declare class PHPRequestHandler implements RequestHandler {
604
480
  * @param config - Request Handler configuration.
605
481
  */
606
482
  constructor(php: BasePHP, config?: PHPRequestHandlerConfiguration);
607
- /** @inheritDoc */
483
+ /**
484
+ * Converts a path to an absolute URL based at the PHPRequestHandler
485
+ * root.
486
+ *
487
+ * @param path The server path to convert to an absolute URL.
488
+ * @returns The absolute URL.
489
+ */
608
490
  pathToInternalUrl(path: string): string;
609
- /** @inheritDoc */
610
- internalUrlToPath(internalUrl: string): string;
611
- get isRequestRunning(): boolean;
612
- /** @inheritDoc */
613
- get absoluteUrl(): string;
614
- /** @inheritDoc */
615
- get documentRoot(): string;
616
- /** @inheritDoc */
617
- request(request: PHPRequest): Promise<PHPResponse>;
618
- }
619
- export interface PHPBrowserConfiguration {
620
491
  /**
621
- * Should handle redirects internally?
492
+ * Converts an absolute URL based at the PHPRequestHandler to a relative path
493
+ * without the server pathname and scope.
494
+ *
495
+ * @param internalUrl An absolute URL based at the PHPRequestHandler root.
496
+ * @returns The relative path.
622
497
  */
623
- handleRedirects?: boolean;
498
+ internalUrlToPath(internalUrl: string): string;
499
+ get isRequestRunning(): boolean;
624
500
  /**
625
- * The maximum number of redirects to follow internally. Once
626
- * exceeded, request() will return the redirecting response.
501
+ * The absolute URL of this PHPRequestHandler instance.
627
502
  */
628
- maxRedirects?: number;
629
- }
630
- declare class PHPBrowser implements RequestHandler {
631
- #private;
632
- requestHandler: PHPRequestHandler;
503
+ get absoluteUrl(): string;
633
504
  /**
634
- * @param server - The PHP server to browse.
635
- * @param config - The browser configuration.
505
+ * The directory in the PHP filesystem where the server will look
506
+ * for the files to serve. Default: `/var/www`.
636
507
  */
637
- constructor(requestHandler: PHPRequestHandler, config?: PHPBrowserConfiguration);
508
+ get documentRoot(): string;
638
509
  /**
639
- * Sends the request to the server.
510
+ * Serves the request either by serving a static file, or by
511
+ * dispatching it to the PHP runtime.
512
+ *
513
+ * The request() method mode behaves like a web server and only works if
514
+ * the PHP was initialized with a `requestHandler` option (which the online version
515
+ * of WordPress Playground does by default).
516
+ *
517
+ * In the request mode, you pass an object containing the request information
518
+ * (method, headers, body, etc.) and the path to the PHP file to run:
519
+ *
520
+ * ```ts
521
+ * const php = PHP.load('7.4', {
522
+ * requestHandler: {
523
+ * documentRoot: "/www"
524
+ * }
525
+ * })
526
+ * php.writeFile("/www/index.php", `<?php echo file_get_contents("php://input");`);
527
+ * const result = await php.request({
528
+ * method: "GET",
529
+ * headers: {
530
+ * "Content-Type": "text/plain"
531
+ * },
532
+ * body: "Hello world!",
533
+ * path: "/www/index.php"
534
+ * });
535
+ * // result.text === "Hello world!"
536
+ * ```
640
537
  *
641
- * When cookies are present in the response, this method stores
642
- * them and sends them with any subsequent requests.
538
+ * The `request()` method cannot be used in conjunction with `cli()`.
643
539
  *
644
- * When a redirection is present in the response, this method
645
- * follows it by discarding a response and sending a subsequent
646
- * request.
540
+ * @example
541
+ * ```js
542
+ * const output = await php.request({
543
+ * method: 'GET',
544
+ * url: '/index.php',
545
+ * headers: {
546
+ * 'X-foo': 'bar',
547
+ * },
548
+ * body: {
549
+ * foo: 'bar',
550
+ * },
551
+ * });
552
+ * console.log(output.stdout); // "Hello world!"
553
+ * ```
647
554
  *
648
- * @param request - The request.
649
- * @param redirects - Internal. The number of redirects handled so far.
650
- * @returns PHPRequestHandler response.
555
+ * @param request - PHP Request data.
651
556
  */
652
- request(request: PHPRequest, redirects?: number): Promise<PHPResponse>;
653
- /** @inheritDoc */
654
- pathToInternalUrl(path: string): string;
655
- /** @inheritDoc */
656
- internalUrlToPath(internalUrl: string): string;
657
- /** @inheritDoc */
658
- get absoluteUrl(): string;
659
- /** @inheritDoc */
660
- get documentRoot(): string;
661
- setCookies(cookies: string[]): void;
662
- serializeCookies(): string;
557
+ request(request: PHPRequest): Promise<PHPResponse>;
663
558
  }
664
559
  export type PHPRuntimeId = number;
665
560
  export type PHPRuntime = any;
@@ -668,11 +563,6 @@ export type PHPLoaderModule = {
668
563
  dependenciesTotalSize: number;
669
564
  init: (jsRuntime: string, options: EmscriptenOptions) => PHPRuntime;
670
565
  };
671
- export type DataModule = {
672
- dependencyFilename: string;
673
- dependenciesTotalSize: number;
674
- default: (phpRuntime: PHPRuntime) => void;
675
- };
676
566
  export type EmscriptenOptions = {
677
567
  onAbort?: (message: string) => void;
678
568
  /**
@@ -688,16 +578,26 @@ export type EmscriptenOptions = {
688
578
  onRuntimeInitialized?: () => void;
689
579
  monitorRunDependencies?: (left: number) => void;
690
580
  onMessage?: (listener: EmscriptenMessageListener) => void;
581
+ instantiateWasm?: (info: WebAssembly.Imports, receiveInstance: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void) => void;
691
582
  } & Record<string, any>;
692
583
  export type EmscriptenMessageListener = (type: string, data: string) => void;
693
584
  export interface SemaphoreOptions {
585
+ /**
586
+ * The maximum number of concurrent locks.
587
+ */
694
588
  concurrency: number;
589
+ /**
590
+ * The maximum time to wait for a lock to become available.
591
+ */
592
+ timeout?: number;
695
593
  }
696
594
  declare class Semaphore {
697
595
  private _running;
698
596
  private concurrency;
597
+ private timeout?;
699
598
  private queue;
700
- constructor({ concurrency }: SemaphoreOptions);
599
+ constructor({ concurrency, timeout }: SemaphoreOptions);
600
+ get remaining(): number;
701
601
  get running(): number;
702
602
  acquire(): Promise<() => void>;
703
603
  run<T>(fn: () => T | Promise<T>): Promise<T>;
@@ -706,7 +606,7 @@ declare const __private__dont__use: unique symbol;
706
606
  declare abstract class BasePHP implements IsomorphicLocalPHP {
707
607
  #private;
708
608
  protected [__private__dont__use]: any;
709
- requestHandler?: PHPBrowser;
609
+ requestHandler?: PHPRequestHandler;
710
610
  /**
711
611
  * An exclusive lock that prevent multiple requests from running at
712
612
  * the same time.
@@ -745,10 +645,9 @@ declare abstract class BasePHP implements IsomorphicLocalPHP {
745
645
  /** @inheritDoc */
746
646
  chdir(path: string): void;
747
647
  /** @inheritDoc */
748
- request(request: PHPRequest, maxRedirects?: number): Promise<PHPResponse>;
648
+ request(request: PHPRequest): Promise<PHPResponse>;
749
649
  /** @inheritDoc */
750
650
  run(request: PHPRunOptions): Promise<PHPResponse>;
751
- addServerGlobalEntry(key: string, value: string): void;
752
651
  defineConstant(key: string, value: string | boolean | number | null): void;
753
652
  /** @inheritDoc */
754
653
  mkdir(path: string): void;
@@ -777,21 +676,18 @@ declare abstract class BasePHP implements IsomorphicLocalPHP {
777
676
  * interrupting the operations of this PHP instance.
778
677
  *
779
678
  * @param runtime
679
+ * @param cwd. Internal, the VFS path to recreate in the new runtime.
680
+ * This arg is temporary and will be removed once BasePHP
681
+ * is fully decoupled from the request handler and
682
+ * accepts a constructor-level cwd argument.
780
683
  */
781
- hotSwapPHPRuntime(runtime: number): void;
684
+ hotSwapPHPRuntime(runtime: number, cwd?: string): void;
782
685
  exit(code?: number): void;
783
686
  }
784
- declare class EmscriptenDownloadMonitor extends EventTarget {
785
- #private;
786
- constructor();
787
- expectAssets(assets: Record<string, number>): void;
788
- monitorFetch(fetchPromise: Promise<Response>): Promise<Response>;
789
- }
790
687
  export interface PHPWebLoaderOptions {
791
688
  emscriptenOptions?: EmscriptenOptions;
792
- downloadMonitor?: EmscriptenDownloadMonitor;
793
689
  requestHandler?: PHPRequestHandlerConfiguration;
794
- dataModules?: Array<DataModule | Promise<DataModule>>;
690
+ onPhpLoaderModuleLoaded?: (module: PHPLoaderModule) => void;
795
691
  /** @deprecated To be replaced with `extensions` in the future */
796
692
  loadAllExtensions?: boolean;
797
693
  }
@@ -811,6 +707,11 @@ export declare class WebPHP extends BasePHP {
811
707
  static load(phpVersion: SupportedPHPVersion, options?: PHPWebLoaderOptions): Promise<WebPHP>;
812
708
  static loadRuntime(phpVersion: SupportedPHPVersion, options?: PHPWebLoaderOptions): Promise<number>;
813
709
  }
710
+ declare class EmscriptenDownloadMonitor extends EventTarget {
711
+ #private;
712
+ expectAssets(assets: Record<string, number>): void;
713
+ monitorFetch(fetchPromise: Promise<Response>): Promise<Response>;
714
+ }
814
715
  /**
815
716
  * A PHP client that can be used to run PHP code in the browser.
816
717
  */
@@ -834,11 +735,9 @@ export declare class WebPHPEndpoint implements IsomorphicLocalPHP {
834
735
  /** @inheritDoc @php-wasm/universal!IsomorphicLocalPHP.rmdir */
835
736
  rmdir(path: string, options?: RmDirOptions): void;
836
737
  /** @inheritDoc @php-wasm/universal!RequestHandler.request */
837
- request(request: PHPRequest, redirects?: number): Promise<PHPResponse>;
738
+ request(request: PHPRequest): Promise<PHPResponse>;
838
739
  /** @inheritDoc @php-wasm/web!WebPHP.run */
839
740
  run(request: PHPRunOptions): Promise<PHPResponse>;
840
- /** @inheritDoc @php-wasm/web!WebPHP.setSpawnHandler */
841
- setSpawnHandler(listener: string | SpawnHandler): void;
842
741
  /** @inheritDoc @php-wasm/web!WebPHP.chdir */
843
742
  chdir(path: string): void;
844
743
  /** @inheritDoc @php-wasm/web!WebPHP.setSapiName */