@wp-playground/client 0.5.6 → 0.6.1

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
@@ -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,12 +354,17 @@ 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.
354
365
  * @param value - The value of the constant.
355
366
  */
356
- defineConstant(key: string, value: string | number | null): void;
367
+ defineConstant(key: string, value: boolean | string | number | null): void;
357
368
  /**
358
369
  * Adds an event listener for a PHP event.
359
370
  * @param eventType - The type of event to listen for.
@@ -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",
@@ -1143,21 +1172,6 @@ export interface ActivatePluginStep {
1143
1172
  * @param playground The playground client.
1144
1173
  */
1145
1174
  export declare const activatePlugin: StepHandler<ActivatePluginStep>;
1146
- /**
1147
- * @private
1148
- */
1149
- export interface ApplyWordPressPatchesStep {
1150
- step: "applyWordPressPatches";
1151
- siteUrl?: string;
1152
- wordpressPath?: string;
1153
- addPhpInfo?: boolean;
1154
- patchSecrets?: boolean;
1155
- disableSiteHealth?: boolean;
1156
- disableWpNewBlogNotification?: boolean;
1157
- prepareForRunningInsideWebBrowser?: boolean;
1158
- addFetchNetworkTransport?: boolean;
1159
- }
1160
- export declare const applyWordPressPatches: StepHandler<ApplyWordPressPatchesStep>;
1161
1175
  /**
1162
1176
  * @inheritDoc defineSiteUrl
1163
1177
  * @hasRunnableExample
@@ -1580,7 +1594,7 @@ export interface RunPHPStep {
1580
1594
  /**
1581
1595
  * Runs PHP code.
1582
1596
  */
1583
- export declare const runPHP: StepHandler<RunPHPStep>;
1597
+ export declare const runPHP: StepHandler<RunPHPStep, Promise<PHPResponse>>;
1584
1598
  /**
1585
1599
  * @inheritDoc runPHP
1586
1600
  * @hasRunnableExample
@@ -1639,7 +1653,7 @@ export interface RequestStep {
1639
1653
  /**
1640
1654
  * Sends a HTTP request to the Playground.
1641
1655
  */
1642
- export declare const request: StepHandler<RequestStep>;
1656
+ export declare const request: StepHandler<RequestStep, Promise<PHPResponse>>;
1643
1657
  /**
1644
1658
  * @inheritDoc writeFile
1645
1659
  * @hasRunnableExample
@@ -1746,15 +1760,23 @@ export declare const activateTheme: StepHandler<ActivateThemeStep>;
1746
1760
  * <code>
1747
1761
  * {
1748
1762
  * "step": "unzip",
1749
- * "zipPath": "/wordpress/data.zip",
1763
+ * "zipFile": {
1764
+ * "resource": "vfs",
1765
+ * "path": "/wordpress/data.zip"
1766
+ * },
1750
1767
  * "extractToPath": "/wordpress"
1751
1768
  * }
1752
1769
  * </code>
1753
1770
  */
1754
- export interface UnzipStep {
1771
+ export interface UnzipStep<ResourceType> {
1755
1772
  step: "unzip";
1756
1773
  /** The zip file to extract */
1757
- zipPath: string;
1774
+ zipFile?: ResourceType;
1775
+ /**
1776
+ * The path of the zip file to extract
1777
+ * @deprecated Use zipFile instead.
1778
+ */
1779
+ zipPath?: string;
1758
1780
  /** The path to extract the zip file to */
1759
1781
  extractToPath: string;
1760
1782
  }
@@ -1765,7 +1787,7 @@ export interface UnzipStep {
1765
1787
  * @param zipPath The zip file to unzip.
1766
1788
  * @param extractTo The directory to extract the zip file to.
1767
1789
  */
1768
- export declare const unzip: StepHandler<UnzipStep>;
1790
+ export declare const unzip: StepHandler<UnzipStep<File>>;
1769
1791
  /**
1770
1792
  * @inheritDoc importWordPressFiles
1771
1793
  * @example
@@ -1834,6 +1856,29 @@ export interface ImportFileStep<ResourceType> {
1834
1856
  * @param file The file to import.
1835
1857
  */
1836
1858
  export declare const importFile: StepHandler<ImportFileStep<File>>;
1859
+ /**
1860
+ * @inheritDoc enableMultisite
1861
+ * @hasRunnableExample
1862
+ * @example
1863
+ *
1864
+ * <code>
1865
+ * {
1866
+ * "step": "enableMultisite"
1867
+ * }
1868
+ * </code>
1869
+ */
1870
+ export interface EnableMultisiteStep {
1871
+ step: "enableMultisite";
1872
+ }
1873
+ /**
1874
+ * Defines constants in a wp-config.php file.
1875
+ *
1876
+ * This step can be called multiple times, and the constants will be merged.
1877
+ *
1878
+ * @param playground The playground client.
1879
+ * @param enableMultisite
1880
+ */
1881
+ export declare const enableMultisite: StepHandler<EnableMultisiteStep>;
1837
1882
  /**
1838
1883
  * Used by the export step to exclude the Playground-specific files
1839
1884
  * from the zip file. Keep it in sync with the list of files created
@@ -1851,7 +1896,7 @@ export type StepDefinition = Step & {
1851
1896
  * If you add a step here, make sure to also
1852
1897
  * add it to the exports below.
1853
1898
  */
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>;
1899
+ export type GenericStep<Resource> = ActivatePluginStep | ActivateThemeStep | 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
1900
  /**
1856
1901
  * Progress reporting details.
1857
1902
  */
@@ -1859,11 +1904,11 @@ export type StepProgress = {
1859
1904
  tracker: ProgressTracker;
1860
1905
  initialCaption?: string;
1861
1906
  };
1862
- export type StepHandler<S extends GenericStep<File>> = (
1907
+ export type StepHandler<S extends GenericStep<File>, Return = any> = (
1863
1908
  /**
1864
1909
  * A PHP instance or Playground client.
1865
1910
  */
1866
- php: UniversalPHP, args: Omit<S, "step">, progressArgs?: StepProgress) => any;
1911
+ php: UniversalPHP, args: Omit<S, "step">, progressArgs?: StepProgress) => Return;
1867
1912
  /**
1868
1913
  * Exports the WordPress database as a WXR file using
1869
1914
  * the core WordPress export tool.
@@ -2039,17 +2084,7 @@ declare class WebPHP extends BasePHP {
2039
2084
  * @returns A new PHP instance
2040
2085
  */
2041
2086
  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
- };
2087
+ static loadRuntime(phpVersion: SupportedPHPVersion, options?: PHPWebLoaderOptions): Promise<number>;
2053
2088
  }
2054
2089
  declare class WebPHPEndpoint implements IsomorphicLocalPHP {
2055
2090
  /** @inheritDoc @php-wasm/universal!RequestHandler.absoluteUrl */
@@ -2075,9 +2110,11 @@ declare class WebPHPEndpoint implements IsomorphicLocalPHP {
2075
2110
  /** @inheritDoc @php-wasm/web!WebPHP.run */
2076
2111
  run(request: PHPRunOptions): Promise<PHPResponse>;
2077
2112
  /** @inheritDoc @php-wasm/web!WebPHP.setSpawnHandler */
2078
- setSpawnHandler(listener: SpawnHandler): void;
2113
+ setSpawnHandler(listener: string | SpawnHandler): void;
2079
2114
  /** @inheritDoc @php-wasm/web!WebPHP.chdir */
2080
2115
  chdir(path: string): void;
2116
+ /** @inheritDoc @php-wasm/web!WebPHP.setSapiName */
2117
+ setSapiName(newName: string): void;
2081
2118
  /** @inheritDoc @php-wasm/web!WebPHP.setPhpIniPath */
2082
2119
  setPhpIniPath(path: string): void;
2083
2120
  /** @inheritDoc @php-wasm/web!WebPHP.setPhpIniEntry */
@@ -2103,7 +2140,7 @@ declare class WebPHPEndpoint implements IsomorphicLocalPHP {
2103
2140
  /** @inheritDoc @php-wasm/web!WebPHP.onMessage */
2104
2141
  onMessage(listener: MessageListener): void;
2105
2142
  /** @inheritDoc @php-wasm/web!WebPHP.defineConstant */
2106
- defineConstant(key: string, value: string | number | null): void;
2143
+ defineConstant(key: string, value: string | boolean | number | null): void;
2107
2144
  /** @inheritDoc @php-wasm/web!WebPHP.addEventListener */
2108
2145
  addEventListener(eventType: PHPEvent["type"], listener: PHPEventListener): void;
2109
2146
  /** @inheritDoc @php-wasm/web!WebPHP.removeEventListener */
@@ -2194,14 +2231,13 @@ declare class PlaygroundWorkerEndpoint extends WebPHPEndpoint {
2194
2231
  "6.3": string;
2195
2232
  "6.2": string;
2196
2233
  "6.1": string;
2197
- "6.0": string;
2198
2234
  };
2199
2235
  latest: string;
2200
2236
  }>;
2201
2237
  resetVirtualOpfs(): Promise<void>;
2202
2238
  reloadFilesFromOpfs(): Promise<void>;
2203
2239
  bindOpfs(opfs: FileSystemDirectoryHandle, onProgress?: SyncProgressCallback): Promise<void>;
2204
- journalFSEvents(root: string, callback: (op: FilesystemOperation) => void): Promise<() => void>;
2240
+ journalFSEvents(root: string, callback: (op: FilesystemOperation) => void): Promise<() => any>;
2205
2241
  replayFSJournal(events: FilesystemOperation[]): Promise<void>;
2206
2242
  }
2207
2243
  export interface ProgressBarOptions {
@@ -2243,6 +2279,7 @@ export interface WebClientMixin extends ProgressReceiver {
2243
2279
  * The onDownloadProgress event listener.
2244
2280
  */
2245
2281
  onDownloadProgress: PlaygroundWorkerEndpoint["onDownloadProgress"];
2282
+ setSpawnHandler: PlaygroundWorkerEndpoint["setSpawnHandler"];
2246
2283
  journalFSEvents: PlaygroundWorkerEndpoint["journalFSEvents"];
2247
2284
  replayFSJournal: PlaygroundWorkerEndpoint["replayFSJournal"];
2248
2285
  addEventListener: PlaygroundWorkerEndpoint["addEventListener"];
@@ -2263,6 +2300,12 @@ export interface StartPlaygroundOptions {
2263
2300
  disableProgressBar?: boolean;
2264
2301
  blueprint?: Blueprint;
2265
2302
  onBlueprintStepCompleted?: OnStepCompleted;
2303
+ /**
2304
+ * The SAPI name PHP will use.
2305
+ * @internal
2306
+ * @private
2307
+ */
2308
+ sapiName?: string;
2266
2309
  }
2267
2310
  /**
2268
2311
  * Loads playground in iframe and returns a PlaygroundClient instance.
@@ -2271,7 +2314,7 @@ export interface StartPlaygroundOptions {
2271
2314
  * @param options Options for loading the playground.
2272
2315
  * @returns A PlaygroundClient instance.
2273
2316
  */
2274
- export declare function startPlaygroundWeb({ iframe, blueprint, remoteUrl, progressTracker, disableProgressBar, onBlueprintStepCompleted, }: StartPlaygroundOptions): Promise<PlaygroundClient>;
2317
+ export declare function startPlaygroundWeb({ iframe, blueprint, remoteUrl, progressTracker, disableProgressBar, onBlueprintStepCompleted, sapiName, }: StartPlaygroundOptions): Promise<PlaygroundClient>;
2275
2318
  /**
2276
2319
  * @deprecated Use `startPlayground` instead.
2277
2320
  *