@php-wasm/web 0.1.60 → 0.2.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.
package/.htaccess CHANGED
@@ -1,5 +1 @@
1
1
  AddType application/wasm .wasm
2
-
3
- <FilesMatch "iframe-worker.html$">
4
- Header set Origin-Agent-Cluster: ?1
5
- </FilesMatch>
package/index.d.ts CHANGED
@@ -20,6 +20,7 @@ export declare function consumeAPI<APIType>(remote: Worker | Window): RemoteAPI<
20
20
  export type PublicAPI<Methods, PipedAPI = unknown> = RemoteAPI<Methods & PipedAPI>;
21
21
  export declare function exposeAPI<Methods, PipedAPI>(apiMethods?: Methods, pipedApi?: PipedAPI): [
22
22
  () => void,
23
+ (e: Error) => void,
23
24
  PublicAPI<Methods, PipedAPI>
24
25
  ];
25
26
  export interface PHPResponseData {
@@ -142,7 +143,7 @@ export interface RequestHandler {
142
143
  * }
143
144
  * })
144
145
  * php.writeFile("/www/index.php", `<?php echo file_get_contents("php://input");`);
145
- * const result = await php.run({
146
+ * const result = await php.request({
146
147
  * method: "GET",
147
148
  * headers: {
148
149
  * "Content-Type": "text/plain"
@@ -372,7 +373,48 @@ export interface IsomorphicLocalPHP extends RequestHandler {
372
373
  * @param options - PHP runtime options.
373
374
  */
374
375
  run(options: PHPRunOptions): Promise<PHPResponse>;
376
+ /**
377
+ * Listens to message sent by the PHP code.
378
+ *
379
+ * To dispatch messages, call:
380
+ *
381
+ * post_message_to_js(string $data)
382
+ *
383
+ * Arguments:
384
+ * $data (string) – Data to pass to JavaScript.
385
+ *
386
+ * @example
387
+ *
388
+ * ```ts
389
+ * const php = await PHP.load('8.0');
390
+ *
391
+ * php.onMessage(
392
+ * // The data is always passed as a string
393
+ * function (data: string) {
394
+ * // Let's decode and log the data:
395
+ * console.log(JSON.parse(data));
396
+ * }
397
+ * );
398
+ *
399
+ * // Now that we have a listener in place, let's
400
+ * // dispatch a message:
401
+ * await php.run({
402
+ * code: `<?php
403
+ * post_message_to_js(
404
+ * json_encode([
405
+ * 'post_id' => '15',
406
+ * 'post_title' => 'This is a blog post!'
407
+ * ])
408
+ * ));
409
+ * `,
410
+ * });
411
+ * ```
412
+ *
413
+ * @param listener Callback function to handle the message.
414
+ */
415
+ onMessage(listener: MessageListener): void;
375
416
  }
417
+ export type MessageListener = (data: string) => void;
376
418
  export type HTTPMethod = "GET" | "POST" | "HEAD" | "OPTIONS" | "PATCH" | "PUT" | "DELETE";
377
419
  export type PHPRequestHeaders = Record<string, string>;
378
420
  export interface PHPRequest {
@@ -577,7 +619,9 @@ export type EmscriptenOptions = {
577
619
  quit?: (status: number, toThrow: any) => void;
578
620
  onRuntimeInitialized?: () => void;
579
621
  monitorRunDependencies?: (left: number) => void;
622
+ onMessage?: (listener: EmscriptenMessageListener) => void;
580
623
  } & Record<string, any>;
624
+ export type EmscriptenMessageListener = (type: string, data: string) => void;
581
625
  declare const __private__dont__use: unique symbol;
582
626
  declare abstract class BasePHP implements IsomorphicLocalPHP {
583
627
  #private;
@@ -592,6 +636,8 @@ declare abstract class BasePHP implements IsomorphicLocalPHP {
592
636
  */
593
637
  constructor(PHPRuntimeId?: PHPRuntimeId, serverOptions?: PHPRequestHandlerConfiguration);
594
638
  /** @inheritDoc */
639
+ onMessage(listener: MessageListener): Promise<void>;
640
+ /** @inheritDoc */
595
641
  get absoluteUrl(): string;
596
642
  /** @inheritDoc */
597
643
  get documentRoot(): string;
@@ -676,7 +722,6 @@ export declare class WebPHP extends BasePHP {
676
722
  static loadSync(phpVersion: SupportedPHPVersion, options?: PHPWebLoaderOptions): {
677
723
  php: WebPHP;
678
724
  phpReady: Promise<WebPHP>;
679
- dataModules: Promise<DataModule[]>;
680
725
  };
681
726
  }
682
727
  /**
@@ -729,6 +774,8 @@ export declare class WebPHPEndpoint implements IsomorphicLocalPHP {
729
774
  isDir(path: string): boolean;
730
775
  /** @inheritDoc @php-wasm/web!WebPHP.fileExists */
731
776
  fileExists(path: string): boolean;
777
+ /** @inheritDoc @php-wasm/web!WebPHP.onMessage */
778
+ onMessage(listener: MessageListener): void;
732
779
  }
733
780
  export declare function getPHPLoaderModule(version?: SupportedPHPVersion): Promise<PHPLoaderModule>;
734
781
  /**
@@ -741,23 +788,15 @@ export declare function getPHPLoaderModule(version?: SupportedPHPVersion): Promi
741
788
  * mismatched with the actual version, the service worker
742
789
  * will be re-registered.
743
790
  */
744
- export declare function registerServiceWorker<Client extends Remote<WebPHPEndpoint>>(phpApi: Client, scope: string, scriptUrl: string, expectedVersion: string): Promise<void>;
791
+ export declare function registerServiceWorker<Client extends Remote<WebPHPEndpoint>>(phpApi: Client, scope: string, scriptUrl: string): Promise<void>;
745
792
  export declare function parseWorkerStartupOptions<T extends Record<string, string>>(): T;
746
- /**
747
- * Recommended Worker Thread backend.
748
- * It's typically "webworker", but in Firefox it's "iframe"
749
- * because Firefox doesn't support module workers with dynamic imports.
750
- * See https://github.com/mdn/content/issues/24402
751
- */
752
- export declare const recommendedWorkerBackend: string;
753
793
  /**
754
794
  * Spawns a new Worker Thread.
755
795
  *
756
796
  * @param workerUrl The absolute URL of the worker script.
757
- * @param workerBackend The Worker Thread backend to use. Either 'webworker' or 'iframe'.
758
797
  * @param config
759
798
  * @returns The spawned Worker Thread.
760
799
  */
761
- export declare function spawnPHPWorkerThread(workerUrl: string, workerBackend?: "webworker" | "iframe", startupOptions?: Record<string, string>): Promise<Window | Worker>;
800
+ export declare function spawnPHPWorkerThread(workerUrl: string, startupOptions?: Record<string, string>): Promise<Worker>;
762
801
 
763
802
  export {};