@php-wasm/web 0.1.59 → 0.1.61

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.d.ts CHANGED
@@ -372,7 +372,48 @@ export interface IsomorphicLocalPHP extends RequestHandler {
372
372
  * @param options - PHP runtime options.
373
373
  */
374
374
  run(options: PHPRunOptions): Promise<PHPResponse>;
375
+ /**
376
+ * Listens to message sent by the PHP code.
377
+ *
378
+ * To dispatch messages, call:
379
+ *
380
+ * post_message_to_js(string $data)
381
+ *
382
+ * Arguments:
383
+ * $data – any extra information as a string
384
+ *
385
+ * @example
386
+ *
387
+ * ```ts
388
+ * const php = await PHP.load('8.0');
389
+ *
390
+ * php.onMessage(
391
+ * // The data is always passed as a string
392
+ * function (data: string) {
393
+ * // Let's decode and log the data:
394
+ * console.log(JSON.parse(data));
395
+ * }
396
+ * );
397
+ *
398
+ * // Now that we have a listener in place, let's
399
+ * // dispatch a message:
400
+ * await php.run({
401
+ * code: `<?php
402
+ * post_message_to_js(
403
+ * json_encode([
404
+ * 'post_id' => '15',
405
+ * 'post_title' => 'This is a blog post!'
406
+ * ])
407
+ * ));
408
+ * `,
409
+ * });
410
+ * ```
411
+ *
412
+ * @param listener Callback function to handle the message.
413
+ */
414
+ onMessage(listener: MessageListener): void;
375
415
  }
416
+ export type MessageListener = (data: string) => void;
376
417
  export type HTTPMethod = "GET" | "POST" | "HEAD" | "OPTIONS" | "PATCH" | "PUT" | "DELETE";
377
418
  export type PHPRequestHeaders = Record<string, string>;
378
419
  export interface PHPRequest {
@@ -577,7 +618,9 @@ export type EmscriptenOptions = {
577
618
  quit?: (status: number, toThrow: any) => void;
578
619
  onRuntimeInitialized?: () => void;
579
620
  monitorRunDependencies?: (left: number) => void;
621
+ onMessage?: (listener: EmscriptenMessageListener) => void;
580
622
  } & Record<string, any>;
623
+ export type EmscriptenMessageListener = (type: string, data: string) => void;
581
624
  declare const __private__dont__use: unique symbol;
582
625
  declare abstract class BasePHP implements IsomorphicLocalPHP {
583
626
  #private;
@@ -592,6 +635,8 @@ declare abstract class BasePHP implements IsomorphicLocalPHP {
592
635
  */
593
636
  constructor(PHPRuntimeId?: PHPRuntimeId, serverOptions?: PHPRequestHandlerConfiguration);
594
637
  /** @inheritDoc */
638
+ onMessage(listener: MessageListener): Promise<void>;
639
+ /** @inheritDoc */
595
640
  get absoluteUrl(): string;
596
641
  /** @inheritDoc */
597
642
  get documentRoot(): string;
@@ -676,7 +721,6 @@ export declare class WebPHP extends BasePHP {
676
721
  static loadSync(phpVersion: SupportedPHPVersion, options?: PHPWebLoaderOptions): {
677
722
  php: WebPHP;
678
723
  phpReady: Promise<WebPHP>;
679
- dataModules: Promise<DataModule[]>;
680
724
  };
681
725
  }
682
726
  /**
@@ -729,6 +773,8 @@ export declare class WebPHPEndpoint implements IsomorphicLocalPHP {
729
773
  isDir(path: string): boolean;
730
774
  /** @inheritDoc @php-wasm/web!WebPHP.fileExists */
731
775
  fileExists(path: string): boolean;
776
+ /** @inheritDoc @php-wasm/web!WebPHP.onMessage */
777
+ onMessage(listener: MessageListener): void;
732
778
  }
733
779
  export declare function getPHPLoaderModule(version?: SupportedPHPVersion): Promise<PHPLoaderModule>;
734
780
  /**