@wp-playground/client 0.6.8 → 0.6.9

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 +25 -25
  2. package/index.d.ts +12 -7
  3. package/index.js +33 -30
  4. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -207,6 +207,7 @@ export interface PHPRequestEndEvent {
207
207
  export interface PHPRequestErrorEvent {
208
208
  type: "request.error";
209
209
  error: Error;
210
+ source?: "request" | "php-wasm";
210
211
  }
211
212
  /**
212
213
  * Represents a PHP runtime initialization event.
@@ -672,11 +673,6 @@ export interface PHPRunOptions {
672
673
  * The code snippet to eval instead of a php file.
673
674
  */
674
675
  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;
680
676
  }
681
677
  /**
682
678
  * Output of the PHP.wasm runtime.
@@ -1888,7 +1884,7 @@ export declare const enableMultisite: StepHandler<EnableMultisiteStep>;
1888
1884
  *
1889
1885
  * <code>
1890
1886
  * {
1891
- * "step": "wpCLI",
1887
+ * "step": "wp-cli",
1892
1888
  * "command": "wp post create --post_title='Test post' --post_excerpt='Some content'"
1893
1889
  * }
1894
1890
  * </code>
@@ -2276,6 +2272,7 @@ declare class PlaygroundWorkerEndpoint extends WebPHPEndpoint {
2276
2272
  all: {
2277
2273
  nightly: string;
2278
2274
  beta: string;
2275
+ "6.5": string;
2279
2276
  "6.4": string;
2280
2277
  "6.3": string;
2281
2278
  "6.2": string;
@@ -2349,6 +2346,14 @@ export interface StartPlaygroundOptions {
2349
2346
  disableProgressBar?: boolean;
2350
2347
  blueprint?: Blueprint;
2351
2348
  onBlueprintStepCompleted?: OnStepCompleted;
2349
+ /**
2350
+ * Called when the playground client is connected, but before the blueprint
2351
+ * steps are run.
2352
+ *
2353
+ * @param playground
2354
+ * @returns
2355
+ */
2356
+ onClientConnected?: (playground: PlaygroundClient) => void;
2352
2357
  /**
2353
2358
  * The SAPI name PHP will use.
2354
2359
  * @internal
@@ -2363,7 +2368,7 @@ export interface StartPlaygroundOptions {
2363
2368
  * @param options Options for loading the playground.
2364
2369
  * @returns A PlaygroundClient instance.
2365
2370
  */
2366
- export declare function startPlaygroundWeb({ iframe, blueprint, remoteUrl, progressTracker, disableProgressBar, onBlueprintStepCompleted, sapiName, }: StartPlaygroundOptions): Promise<PlaygroundClient>;
2371
+ export declare function startPlaygroundWeb({ iframe, blueprint, remoteUrl, progressTracker, disableProgressBar, onBlueprintStepCompleted, onClientConnected, sapiName, }: StartPlaygroundOptions): Promise<PlaygroundClient>;
2367
2372
  /**
2368
2373
  * @deprecated Use `startPlayground` instead.
2369
2374
  *
package/index.js CHANGED
@@ -269,7 +269,6 @@ const activatePlugin = async (e, { pluginPath: t, pluginName: r }, n) => {
269
269
  n == null || n.tracker.setCaption(`Activating ${r || t}`);
270
270
  const s = await e.documentRoot;
271
271
  await e.run({
272
- throwOnError: !0,
273
272
  code: `<?php
274
273
  define( 'WP_ADMIN', true );
275
274
  require_once( ${phpVar(s)}. "/wp-load.php" );
@@ -301,7 +300,6 @@ exit(1);
301
300
  r == null || r.tracker.setCaption(`Activating ${t}`);
302
301
  const n = await e.documentRoot;
303
302
  await e.run({
304
- throwOnError: !0,
305
303
  code: `<?php
306
304
  define( 'WP_ADMIN', true );
307
305
  require_once( ${phpVar(n)}. "/wp-load.php" );
@@ -312,7 +310,7 @@ set_current_user( get_users(array('role' => 'Administrator') )[0] );
312
310
  switch_theme( ${phpVar(t)} );
313
311
  `
314
312
  });
315
- }, runPHP = async (e, { code: t }) => await e.run({ code: t, throwOnError: !0 }), runPHPWithOptions = async (e, { options: t }) => await e.run(t), rm = async (e, { path: t }) => {
313
+ }, runPHP = async (e, { code: t }) => await e.run({ code: t }), runPHPWithOptions = async (e, { options: t }) => await e.run(t), rm = async (e, { path: t }) => {
316
314
  await e.unlink(t);
317
315
  }, runSql = async (e, { sql: t }, r) => {
318
316
  r == null || r.tracker.setCaption("Executing SQL Queries");
@@ -712,7 +710,6 @@ async function rewriteDefineCalls(e, t, r) {
712
710
  consts: r
713
711
  });
714
712
  return await e.run({
715
- throwOnError: !0,
716
713
  code: `${rewriteWpConfigToDefineConstants}
717
714
  $wp_config_path = '/tmp/code.php';
718
715
  $wp_config = file_get_contents($wp_config_path);
@@ -745,7 +742,6 @@ const login = async (e, { username: t = "admin", password: r = "password" } = {}
745
742
  }, setSiteOptions = async (e, { options: t }) => {
746
743
  const r = await e.documentRoot;
747
744
  await e.run({
748
- throwOnError: !0,
749
745
  code: `<?php
750
746
  include ${phpVar(r)} . '/wp-load.php';
751
747
  $site_options = ${phpVar(t)};
@@ -758,7 +754,6 @@ const login = async (e, { username: t = "admin", password: r = "password" } = {}
758
754
  }, updateUserMeta = async (e, { meta: t, userId: r }) => {
759
755
  const n = await e.documentRoot;
760
756
  await e.run({
761
- throwOnError: !0,
762
757
  code: `<?php
763
758
  include ${phpVar(n)} . '/wp-load.php';
764
759
  $meta = ${phpVar(t)};
@@ -794,7 +789,6 @@ const enableMultisite = async (e) => {
794
789
  }
795
790
  }), await login(e, {});
796
791
  const s = await e.documentRoot, o = (await e.run({
797
- throwOnError: !0,
798
792
  code: `<?php
799
793
  define( 'WP_ADMIN', true );
800
794
  require_once(${phpVar(s)} . "/wp-load.php");
@@ -1035,7 +1029,6 @@ function delTree($dir)
1035
1029
  `;
1036
1030
  async function runPhpWithZipFunctions(e, t) {
1037
1031
  return await e.run({
1038
- throwOnError: !0,
1039
1032
  code: zipFunctions + t
1040
1033
  });
1041
1034
  }
@@ -1101,7 +1094,6 @@ const tmpPath = "/tmp/file.zip", unzip = async (e, { zipFile: t, zipPath: r, ext
1101
1094
  joinPaths(n, "wp-admin", "upgrade.php")
1102
1095
  );
1103
1096
  await e.run({
1104
- throwOnError: !0,
1105
1097
  code: `<?php
1106
1098
  $_GET['step'] = 'upgrade_db';
1107
1099
  require ${u};
@@ -1942,7 +1934,8 @@ class PHPRequestHandler {
1942
1934
  /** @inheritDoc */
1943
1935
  async request(t) {
1944
1936
  const r = t.url.startsWith("http://") || t.url.startsWith("https://"), n = new URL(
1945
- t.url,
1937
+ // Remove the hash part of the URL as it's not meant for the server.
1938
+ t.url.split("#")[0],
1946
1939
  r ? void 0 : DEFAULT_BASE_URL
1947
1940
  ), s = applyRewriteRules(
1948
1941
  removePathPrefix(n.pathname, H(this, ve)),
@@ -2353,7 +2346,7 @@ class BasePHP {
2353
2346
  for (const c in i)
2354
2347
  ce(this, Tt, rr).call(this, c, i[c]);
2355
2348
  const o = await ce(this, Ct, sr).call(this);
2356
- if (e.throwOnError && o.exitCode !== 0) {
2349
+ if (o.exitCode !== 0) {
2357
2350
  const c = {
2358
2351
  stdout: o.text,
2359
2352
  stderr: o.errors
@@ -2362,13 +2355,15 @@ class BasePHP {
2362
2355
  const d = new Error(
2363
2356
  `PHP.run() failed with exit code ${o.exitCode} and the following output: ` + o.errors
2364
2357
  );
2365
- throw d.output = c, console.error(d), d;
2358
+ throw d.output = c, d.source = "request", console.error(d), d;
2366
2359
  }
2367
2360
  return o;
2368
2361
  } catch (n) {
2369
2362
  throw this.dispatchEvent({
2370
2363
  type: "request.error",
2371
- error: n
2364
+ error: n,
2365
+ // Distinguish between PHP request and PHP-wasm errors
2366
+ source: n.source ?? "php-wasm"
2372
2367
  }), n;
2373
2368
  } finally {
2374
2369
  try {
@@ -9182,10 +9177,6 @@ for existing apps using this option.`
9182
9177
  code: {
9183
9178
  type: "string",
9184
9179
  description: "The code snippet to eval instead of a php file."
9185
- },
9186
- throwOnError: {
9187
- type: "boolean",
9188
- description: "Whether to throw an error if the PHP process exits with a non-zero code or outputs to stderr."
9189
9180
  }
9190
9181
  },
9191
9182
  additionalProperties: !1
@@ -9764,7 +9755,10 @@ class Logger extends EventTarget {
9764
9755
  * @param PromiseRejectionEvent event
9765
9756
  */
9766
9757
  logUnhandledRejection(t) {
9767
- this.log(`${t.reason.stack}`, "Error");
9758
+ if (!(t != null && t.reason))
9759
+ return;
9760
+ const r = (t == null ? void 0 : t.reason.stack) ?? t.reason;
9761
+ this.log(r, "Error");
9768
9762
  }
9769
9763
  /**
9770
9764
  * Register a listener for the window error events and log the data.
@@ -9794,7 +9788,8 @@ class Logger extends EventTarget {
9794
9788
  ), this.dispatchEvent(
9795
9789
  new CustomEvent(this.fatalErrorEvent, {
9796
9790
  detail: {
9797
- logs: this.getLogs()
9791
+ logs: this.getLogs(),
9792
+ source: r.source
9798
9793
  }
9799
9794
  })
9800
9795
  ));
@@ -9872,27 +9867,35 @@ async function startPlaygroundWeb({
9872
9867
  progressTracker: n = new ProgressTracker(),
9873
9868
  disableProgressBar: s,
9874
9869
  onBlueprintStepCompleted: i,
9875
- sapiName: o
9870
+ onClientConnected: o = () => {
9871
+ },
9872
+ sapiName: c
9876
9873
  }) {
9877
9874
  if (assertValidRemote(r), allowStorageAccessByUserActivation(e), r = setQueryParams(r, {
9878
9875
  progressbar: !s
9879
- }), n.setCaption("Preparing WordPress"), !t)
9880
- return doStartPlaygroundWeb(e, r, n);
9881
- const c = compileBlueprint(t, {
9876
+ }), n.setCaption("Preparing WordPress"), !t) {
9877
+ const p = await doStartPlaygroundWeb(
9878
+ e,
9879
+ r,
9880
+ n
9881
+ );
9882
+ return o(p), p;
9883
+ }
9884
+ const d = compileBlueprint(t, {
9882
9885
  progress: n.stage(0.5),
9883
9886
  onStepCompleted: i
9884
- }), d = await doStartPlaygroundWeb(
9887
+ }), u = await doStartPlaygroundWeb(
9885
9888
  e,
9886
9889
  setQueryParams(r, {
9887
- php: c.versions.php,
9888
- wp: c.versions.wp,
9889
- "sapi-name": o,
9890
- "php-extension": c.phpExtensions,
9891
- networking: c.features.networking ? "yes" : "no"
9890
+ php: d.versions.php,
9891
+ wp: d.versions.wp,
9892
+ "sapi-name": c,
9893
+ "php-extension": d.phpExtensions,
9894
+ networking: d.features.networking ? "yes" : "no"
9892
9895
  }),
9893
9896
  n
9894
9897
  );
9895
- return collectPhpLogs(logger, d), await runBlueprintSteps(c, d), n.finish(), d;
9898
+ return collectPhpLogs(logger, u), o(u), await runBlueprintSteps(d, u), n.finish(), u;
9896
9899
  }
9897
9900
  function allowStorageAccessByUserActivation(e) {
9898
9901
  var t, r;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wp-playground/client",
3
- "version": "0.6.8",
3
+ "version": "0.6.9",
4
4
  "description": "WordPress Playground client",
5
5
  "repository": {
6
6
  "type": "git",
@@ -37,7 +37,7 @@
37
37
  "main": "./index.cjs",
38
38
  "module": "./index.js",
39
39
  "types": "index.d.ts",
40
- "gitHead": "cb7b1af260489022238495a2fc0d96f767a1294a",
40
+ "gitHead": "f6b98ad6d05617ee8cb1234c21318fdde17a6e56",
41
41
  "engines": {
42
42
  "node": ">=18.18.2",
43
43
  "npm": ">=8.11.0"