@wp-playground/client 0.5.6 → 0.6.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 (4) hide show
  1. package/index.cjs +148 -79
  2. package/index.d.ts +114 -56
  3. package/index.js +3860 -3484
  4. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -3,17 +3,11 @@
3
3
  import * as Comlink from 'comlink';
4
4
  import { Remote } from 'comlink';
5
5
 
6
- export interface MonitoredModule {
7
- dependencyFilename: string;
8
- dependenciesTotalSize: number;
9
- }
10
6
  declare class EmscriptenDownloadMonitor extends EventTarget {
11
7
  #private;
12
- constructor(modules?: MonitoredModule[]);
13
- getEmscriptenOptions(): {
14
- dataFileDownloads: Record<string, any>;
15
- };
16
- setModules(modules: MonitoredModule[]): void;
8
+ constructor();
9
+ expectAssets(assets: Record<string, number>): void;
10
+ monitorFetch(fetchPromise: Promise<Response>): Promise<Response>;
17
11
  }
18
12
  /**
19
13
  * Options for customizing the progress tracker.
@@ -202,17 +196,29 @@ export declare class PHPResponse implements PHPResponseData {
202
196
  get text(): string;
203
197
  }
204
198
  /**
205
- * Represents an event related to the PHP filesystem.
199
+ * Represents an event related to the PHP request.
206
200
  */
207
201
  export interface PHPRequestEndEvent {
208
202
  type: "request.end";
209
203
  }
204
+ /**
205
+ * Represents a PHP runtime initialization event.
206
+ */
207
+ export interface PHPRuntimeInitializedEvent {
208
+ type: "runtime.initialized";
209
+ }
210
+ /**
211
+ * Represents a PHP runtime destruction event.
212
+ */
213
+ export interface PHPRuntimeBeforeDestroyEvent {
214
+ type: "runtime.beforedestroy";
215
+ }
210
216
  /**
211
217
  * Represents an event related to the PHP instance.
212
218
  * This is intentionally not an extension of CustomEvent
213
219
  * to make it isomorphic between different JavaScript runtimes.
214
220
  */
215
- export type PHPEvent = PHPRequestEndEvent;
221
+ export type PHPEvent = PHPRequestEndEvent | PHPRuntimeInitializedEvent | PHPRuntimeBeforeDestroyEvent;
216
222
  /**
217
223
  * A callback function that handles PHP events.
218
224
  */
@@ -348,6 +354,11 @@ export interface RequestHandler {
348
354
  documentRoot: string;
349
355
  }
350
356
  export interface IsomorphicLocalPHP extends RequestHandler {
357
+ /**
358
+ * Sets the SAPI name exposed by the PHP module.
359
+ * @param newName - The new SAPI name.
360
+ */
361
+ setSapiName(newName: string): void;
351
362
  /**
352
363
  * Defines a constant in the PHP runtime.
353
364
  * @param key - The name of the constant.
@@ -585,7 +596,7 @@ export interface IsomorphicLocalPHP extends RequestHandler {
585
596
  *
586
597
  * @param handler Callback function to spawn a process.
587
598
  */
588
- setSpawnHandler(handler: SpawnHandler): void;
599
+ setSpawnHandler(handler: SpawnHandler | string): void;
589
600
  }
590
601
  export type MessageListener = (data: string) => Promise<string | Uint8Array | void> | string | void;
591
602
  export interface EventEmitter {
@@ -596,7 +607,7 @@ export type ChildProcess = EventEmitter & {
596
607
  stdout: EventEmitter;
597
608
  stderr: EventEmitter;
598
609
  };
599
- export type SpawnHandler = (command: string) => ChildProcess;
610
+ export type SpawnHandler = (command: string, args: string[]) => ChildProcess;
600
611
  export type IsomorphicRemotePHP = Remote<IsomorphicLocalPHP>;
601
612
  export type UniversalPHP = IsomorphicLocalPHP | IsomorphicRemotePHP;
602
613
  export type HTTPMethod = "GET" | "POST" | "HEAD" | "OPTIONS" | "PATCH" | "PUT" | "DELETE";
@@ -661,6 +672,11 @@ export interface PHPRunOptions {
661
672
  * The code snippet to eval instead of a php file.
662
673
  */
663
674
  code?: string;
675
+ /**
676
+ * Whether to throw an error if the PHP process exits with a non-zero code
677
+ * or outputs to stderr.
678
+ */
679
+ throwOnError?: boolean;
664
680
  }
665
681
  /**
666
682
  * Output of the PHP.wasm runtime.
@@ -824,7 +840,6 @@ export type EmscriptenOptions = {
824
840
  ENV?: Record<string, string>;
825
841
  locateFile?: (path: string) => string;
826
842
  noInitialRun?: boolean;
827
- dataFileDownloads?: Record<string, number>;
828
843
  print?: (message: string) => void;
829
844
  printErr?: (message: string) => void;
830
845
  quit?: (status: number, toThrow: any) => void;
@@ -833,11 +848,30 @@ export type EmscriptenOptions = {
833
848
  onMessage?: (listener: EmscriptenMessageListener) => void;
834
849
  } & Record<string, any>;
835
850
  export type EmscriptenMessageListener = (type: string, data: string) => void;
851
+ export interface SemaphoreOptions {
852
+ concurrency: number;
853
+ }
854
+ declare class Semaphore {
855
+ private _running;
856
+ private concurrency;
857
+ private queue;
858
+ constructor({ concurrency }: SemaphoreOptions);
859
+ get running(): number;
860
+ acquire(): Promise<() => void>;
861
+ run<T>(fn: () => T | Promise<T>): Promise<T>;
862
+ }
863
+ export declare function phpVar(value: unknown): string;
864
+ export declare function phpVars<T extends Record<string, unknown>>(vars: T): Record<keyof T, string>;
836
865
  declare const __private__dont__use: unique symbol;
837
866
  declare abstract class BasePHP implements IsomorphicLocalPHP {
838
867
  #private;
839
868
  protected [__private__dont__use]: any;
840
869
  requestHandler?: PHPBrowser;
870
+ /**
871
+ * An exclusive lock that prevent multiple requests from running at
872
+ * the same time.
873
+ */
874
+ semaphore: Semaphore;
841
875
  /**
842
876
  * Initializes a PHP runtime.
843
877
  *
@@ -852,7 +886,7 @@ declare abstract class BasePHP implements IsomorphicLocalPHP {
852
886
  /** @inheritDoc */
853
887
  onMessage(listener: MessageListener): Promise<void>;
854
888
  /** @inheritDoc */
855
- setSpawnHandler(handler: SpawnHandler): Promise<void>;
889
+ setSpawnHandler(handler: SpawnHandler | string): Promise<void>;
856
890
  /** @inheritDoc */
857
891
  get absoluteUrl(): string;
858
892
  /** @inheritDoc */
@@ -863,6 +897,8 @@ declare abstract class BasePHP implements IsomorphicLocalPHP {
863
897
  internalUrlToPath(internalUrl: string): string;
864
898
  initializeRuntime(runtimeId: PHPRuntimeId): void;
865
899
  /** @inheritDoc */
900
+ setSapiName(newName: string): Promise<void>;
901
+ /** @inheritDoc */
866
902
  setPhpIniPath(path: string): void;
867
903
  /** @inheritDoc */
868
904
  setPhpIniEntry(key: string, value: string): void;
@@ -873,7 +909,7 @@ declare abstract class BasePHP implements IsomorphicLocalPHP {
873
909
  /** @inheritDoc */
874
910
  run(request: PHPRunOptions): Promise<PHPResponse>;
875
911
  addServerGlobalEntry(key: string, value: string): void;
876
- defineConstant(key: string, value: string | number | null): void;
912
+ defineConstant(key: string, value: string | boolean | number | null): void;
877
913
  /** @inheritDoc */
878
914
  mkdir(path: string): void;
879
915
  /** @inheritDoc */
@@ -896,22 +932,15 @@ declare abstract class BasePHP implements IsomorphicLocalPHP {
896
932
  isDir(path: string): boolean;
897
933
  /** @inheritDoc */
898
934
  fileExists(path: string): boolean;
899
- exit(code?: number): any;
900
- }
901
- export interface SemaphoreOptions {
902
- concurrency: number;
903
- }
904
- declare class Semaphore {
905
- private _running;
906
- private concurrency;
907
- private queue;
908
- constructor({ concurrency }: SemaphoreOptions);
909
- get running(): number;
910
- acquire(): Promise<() => void>;
911
- run<T>(fn: () => Promise<T>): Promise<T>;
935
+ /**
936
+ * Hot-swaps the PHP runtime for a new one without
937
+ * interrupting the operations of this PHP instance.
938
+ *
939
+ * @param runtime
940
+ */
941
+ hotSwapPHPRuntime(runtime: number): void;
942
+ exit(code?: number): void;
912
943
  }
913
- export declare function phpVar(value: unknown): string;
914
- export declare function phpVars<T extends Record<string, unknown>>(vars: T): Record<keyof T, string>;
915
944
  export declare const ResourceTypes: readonly [
916
945
  "vfs",
917
946
  "literal",
@@ -1580,7 +1609,7 @@ export interface RunPHPStep {
1580
1609
  /**
1581
1610
  * Runs PHP code.
1582
1611
  */
1583
- export declare const runPHP: StepHandler<RunPHPStep>;
1612
+ export declare const runPHP: StepHandler<RunPHPStep, Promise<PHPResponse>>;
1584
1613
  /**
1585
1614
  * @inheritDoc runPHP
1586
1615
  * @hasRunnableExample
@@ -1639,7 +1668,7 @@ export interface RequestStep {
1639
1668
  /**
1640
1669
  * Sends a HTTP request to the Playground.
1641
1670
  */
1642
- export declare const request: StepHandler<RequestStep>;
1671
+ export declare const request: StepHandler<RequestStep, Promise<PHPResponse>>;
1643
1672
  /**
1644
1673
  * @inheritDoc writeFile
1645
1674
  * @hasRunnableExample
@@ -1746,15 +1775,23 @@ export declare const activateTheme: StepHandler<ActivateThemeStep>;
1746
1775
  * <code>
1747
1776
  * {
1748
1777
  * "step": "unzip",
1749
- * "zipPath": "/wordpress/data.zip",
1778
+ * "zipFile": {
1779
+ * "resource": "vfs",
1780
+ * "path": "/wordpress/data.zip"
1781
+ * },
1750
1782
  * "extractToPath": "/wordpress"
1751
1783
  * }
1752
1784
  * </code>
1753
1785
  */
1754
- export interface UnzipStep {
1786
+ export interface UnzipStep<ResourceType> {
1755
1787
  step: "unzip";
1756
1788
  /** The zip file to extract */
1757
- zipPath: string;
1789
+ zipFile?: ResourceType;
1790
+ /**
1791
+ * The path of the zip file to extract
1792
+ * @deprecated Use zipFile instead.
1793
+ */
1794
+ zipPath?: string;
1758
1795
  /** The path to extract the zip file to */
1759
1796
  extractToPath: string;
1760
1797
  }
@@ -1765,7 +1802,7 @@ export interface UnzipStep {
1765
1802
  * @param zipPath The zip file to unzip.
1766
1803
  * @param extractTo The directory to extract the zip file to.
1767
1804
  */
1768
- export declare const unzip: StepHandler<UnzipStep>;
1805
+ export declare const unzip: StepHandler<UnzipStep<File>>;
1769
1806
  /**
1770
1807
  * @inheritDoc importWordPressFiles
1771
1808
  * @example
@@ -1834,6 +1871,29 @@ export interface ImportFileStep<ResourceType> {
1834
1871
  * @param file The file to import.
1835
1872
  */
1836
1873
  export declare const importFile: StepHandler<ImportFileStep<File>>;
1874
+ /**
1875
+ * @inheritDoc enableMultisite
1876
+ * @hasRunnableExample
1877
+ * @example
1878
+ *
1879
+ * <code>
1880
+ * {
1881
+ * "step": "enableMultisite",
1882
+ * }
1883
+ * </code>
1884
+ */
1885
+ export interface EnableMultisiteStep {
1886
+ step: "enableMultisite";
1887
+ }
1888
+ /**
1889
+ * Defines constants in a wp-config.php file.
1890
+ *
1891
+ * This step can be called multiple times, and the constants will be merged.
1892
+ *
1893
+ * @param playground The playground client.
1894
+ * @param enableMultisite
1895
+ */
1896
+ export declare const enableMultisite: StepHandler<EnableMultisiteStep>;
1837
1897
  /**
1838
1898
  * Used by the export step to exclude the Playground-specific files
1839
1899
  * from the zip file. Keep it in sync with the list of files created
@@ -1851,7 +1911,7 @@ export type StepDefinition = Step & {
1851
1911
  * If you add a step here, make sure to also
1852
1912
  * add it to the exports below.
1853
1913
  */
1854
- export type GenericStep<Resource> = ActivatePluginStep | ActivateThemeStep | ApplyWordPressPatchesStep | CpStep | DefineWpConfigConstsStep | DefineSiteUrlStep | ImportFileStep<Resource> | ImportWordPressFilesStep<Resource> | InstallPluginStep<Resource> | InstallThemeStep<Resource> | LoginStep | MkdirStep | MvStep | RequestStep | RmStep | RmdirStep | RunPHPStep | RunPHPWithOptionsStep | RunWpInstallationWizardStep | RunSqlStep<Resource> | SetPhpIniEntryStep | SetSiteOptionsStep | UnzipStep | UpdateUserMetaStep | WriteFileStep<Resource>;
1914
+ export type GenericStep<Resource> = ActivatePluginStep | ActivateThemeStep | ApplyWordPressPatchesStep | CpStep | DefineWpConfigConstsStep | DefineSiteUrlStep | EnableMultisiteStep | ImportFileStep<Resource> | ImportWordPressFilesStep<Resource> | InstallPluginStep<Resource> | InstallThemeStep<Resource> | LoginStep | MkdirStep | MvStep | RequestStep | RmStep | RmdirStep | RunPHPStep | RunPHPWithOptionsStep | RunWpInstallationWizardStep | RunSqlStep<Resource> | SetPhpIniEntryStep | SetSiteOptionsStep | UnzipStep<Resource> | UpdateUserMetaStep | WriteFileStep<Resource>;
1855
1915
  /**
1856
1916
  * Progress reporting details.
1857
1917
  */
@@ -1859,11 +1919,11 @@ export type StepProgress = {
1859
1919
  tracker: ProgressTracker;
1860
1920
  initialCaption?: string;
1861
1921
  };
1862
- export type StepHandler<S extends GenericStep<File>> = (
1922
+ export type StepHandler<S extends GenericStep<File>, Return = any> = (
1863
1923
  /**
1864
1924
  * A PHP instance or Playground client.
1865
1925
  */
1866
- php: UniversalPHP, args: Omit<S, "step">, progressArgs?: StepProgress) => any;
1926
+ php: UniversalPHP, args: Omit<S, "step">, progressArgs?: StepProgress) => Return;
1867
1927
  /**
1868
1928
  * Exports the WordPress database as a WXR file using
1869
1929
  * the core WordPress export tool.
@@ -2039,17 +2099,7 @@ declare class WebPHP extends BasePHP {
2039
2099
  * @returns A new PHP instance
2040
2100
  */
2041
2101
  static load(phpVersion: SupportedPHPVersion, options?: PHPWebLoaderOptions): Promise<WebPHP>;
2042
- /**
2043
- * Does what load() does, but synchronously returns
2044
- * an object with the PHP instance and a promise that
2045
- * resolves when the PHP instance is ready.
2046
- *
2047
- * @see load
2048
- */
2049
- static loadSync(phpVersion: SupportedPHPVersion, options?: PHPWebLoaderOptions): {
2050
- php: WebPHP;
2051
- phpReady: Promise<WebPHP>;
2052
- };
2102
+ static loadRuntime(phpVersion: SupportedPHPVersion, options?: PHPWebLoaderOptions): Promise<number>;
2053
2103
  }
2054
2104
  declare class WebPHPEndpoint implements IsomorphicLocalPHP {
2055
2105
  /** @inheritDoc @php-wasm/universal!RequestHandler.absoluteUrl */
@@ -2075,9 +2125,11 @@ declare class WebPHPEndpoint implements IsomorphicLocalPHP {
2075
2125
  /** @inheritDoc @php-wasm/web!WebPHP.run */
2076
2126
  run(request: PHPRunOptions): Promise<PHPResponse>;
2077
2127
  /** @inheritDoc @php-wasm/web!WebPHP.setSpawnHandler */
2078
- setSpawnHandler(listener: SpawnHandler): void;
2128
+ setSpawnHandler(listener: string | SpawnHandler): void;
2079
2129
  /** @inheritDoc @php-wasm/web!WebPHP.chdir */
2080
2130
  chdir(path: string): void;
2131
+ /** @inheritDoc @php-wasm/web!WebPHP.setSapiName */
2132
+ setSapiName(newName: string): void;
2081
2133
  /** @inheritDoc @php-wasm/web!WebPHP.setPhpIniPath */
2082
2134
  setPhpIniPath(path: string): void;
2083
2135
  /** @inheritDoc @php-wasm/web!WebPHP.setPhpIniEntry */
@@ -2103,7 +2155,7 @@ declare class WebPHPEndpoint implements IsomorphicLocalPHP {
2103
2155
  /** @inheritDoc @php-wasm/web!WebPHP.onMessage */
2104
2156
  onMessage(listener: MessageListener): void;
2105
2157
  /** @inheritDoc @php-wasm/web!WebPHP.defineConstant */
2106
- defineConstant(key: string, value: string | number | null): void;
2158
+ defineConstant(key: string, value: string | boolean | number | null): void;
2107
2159
  /** @inheritDoc @php-wasm/web!WebPHP.addEventListener */
2108
2160
  addEventListener(eventType: PHPEvent["type"], listener: PHPEventListener): void;
2109
2161
  /** @inheritDoc @php-wasm/web!WebPHP.removeEventListener */
@@ -2194,14 +2246,13 @@ declare class PlaygroundWorkerEndpoint extends WebPHPEndpoint {
2194
2246
  "6.3": string;
2195
2247
  "6.2": string;
2196
2248
  "6.1": string;
2197
- "6.0": string;
2198
2249
  };
2199
2250
  latest: string;
2200
2251
  }>;
2201
2252
  resetVirtualOpfs(): Promise<void>;
2202
2253
  reloadFilesFromOpfs(): Promise<void>;
2203
2254
  bindOpfs(opfs: FileSystemDirectoryHandle, onProgress?: SyncProgressCallback): Promise<void>;
2204
- journalFSEvents(root: string, callback: (op: FilesystemOperation) => void): Promise<() => void>;
2255
+ journalFSEvents(root: string, callback: (op: FilesystemOperation) => void): Promise<() => any>;
2205
2256
  replayFSJournal(events: FilesystemOperation[]): Promise<void>;
2206
2257
  }
2207
2258
  export interface ProgressBarOptions {
@@ -2243,6 +2294,7 @@ export interface WebClientMixin extends ProgressReceiver {
2243
2294
  * The onDownloadProgress event listener.
2244
2295
  */
2245
2296
  onDownloadProgress: PlaygroundWorkerEndpoint["onDownloadProgress"];
2297
+ setSpawnHandler: PlaygroundWorkerEndpoint["setSpawnHandler"];
2246
2298
  journalFSEvents: PlaygroundWorkerEndpoint["journalFSEvents"];
2247
2299
  replayFSJournal: PlaygroundWorkerEndpoint["replayFSJournal"];
2248
2300
  addEventListener: PlaygroundWorkerEndpoint["addEventListener"];
@@ -2263,6 +2315,12 @@ export interface StartPlaygroundOptions {
2263
2315
  disableProgressBar?: boolean;
2264
2316
  blueprint?: Blueprint;
2265
2317
  onBlueprintStepCompleted?: OnStepCompleted;
2318
+ /**
2319
+ * The SAPI name PHP will use.
2320
+ * @internal
2321
+ * @private
2322
+ */
2323
+ sapiName?: string;
2266
2324
  }
2267
2325
  /**
2268
2326
  * Loads playground in iframe and returns a PlaygroundClient instance.
@@ -2271,7 +2329,7 @@ export interface StartPlaygroundOptions {
2271
2329
  * @param options Options for loading the playground.
2272
2330
  * @returns A PlaygroundClient instance.
2273
2331
  */
2274
- export declare function startPlaygroundWeb({ iframe, blueprint, remoteUrl, progressTracker, disableProgressBar, onBlueprintStepCompleted, }: StartPlaygroundOptions): Promise<PlaygroundClient>;
2332
+ export declare function startPlaygroundWeb({ iframe, blueprint, remoteUrl, progressTracker, disableProgressBar, onBlueprintStepCompleted, sapiName, }: StartPlaygroundOptions): Promise<PlaygroundClient>;
2275
2333
  /**
2276
2334
  * @deprecated Use `startPlayground` instead.
2277
2335
  *