Skip to content

Commit b03b71e

Browse files
authored
[Website] Add Blueprint v2 handlers (noop) (#2657)
## Motivation for the change, related issues Adds Blueprint v2 handler for the Playground web client and a Blueprint v2 worker into the Playground remote. Those classes are no-op for now and will be plugged in in an upcoming PR. A part of #2586. ## Testing Instructions (or ideally a Blueprint) CI, but nothing should break since this only adds dead code
1 parent 56c7b47 commit b03b71e

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import type { ProgressTracker } from '@php-wasm/progress';
2+
import type { PlaygroundClient, StartPlaygroundOptions } from '.';
3+
import { collectPhpLogs, logger } from '@php-wasm/logger';
4+
import { consumeAPI } from '@php-wasm/universal';
5+
6+
export class BlueprintsV2Handler {
7+
constructor(private readonly options: StartPlaygroundOptions) {}
8+
9+
async bootPlayground(
10+
iframe: HTMLIFrameElement,
11+
progressTracker: ProgressTracker
12+
) {
13+
const {
14+
blueprint,
15+
onClientConnected,
16+
corsProxy,
17+
mounts,
18+
sapiName,
19+
scope,
20+
} = this.options;
21+
const downloadProgress = progressTracker!.stage();
22+
23+
// Connect the Comlink API client to the remote worker,
24+
// boot the playground, and run the blueprint steps.
25+
const playground = consumeAPI<PlaygroundClient>(
26+
iframe.contentWindow!,
27+
iframe.ownerDocument!.defaultView!
28+
) as PlaygroundClient;
29+
await playground.isConnected();
30+
progressTracker.pipe(playground);
31+
32+
// Connect the Comlink API client to the remote worker download monitor
33+
await playground.onDownloadProgress(downloadProgress.loadingListener);
34+
35+
await playground.boot({
36+
mounts,
37+
sapiName,
38+
scope: scope ?? Math.random().toFixed(16),
39+
corsProxyUrl: corsProxy,
40+
experimentalBlueprintsV2Runner: true,
41+
// Pass the declaration directly – the worker runs the V2 runner.
42+
blueprint: blueprint as any,
43+
} as any);
44+
45+
await playground.isReady();
46+
downloadProgress.finish();
47+
48+
collectPhpLogs(logger, playground);
49+
onClientConnected?.(playground);
50+
51+
/**
52+
* Pre-fetch WordPress update checks to speed up the initial wp-admin load.
53+
*
54+
* @see https://github.com/WordPress/wordpress-playground/pull/2295
55+
*/
56+
// @TODO
57+
// if (compiled.features.networking) {
58+
// await playground.prefetchUpdateChecks();
59+
// }
60+
61+
return playground;
62+
}
63+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { EmscriptenDownloadMonitor } from '@php-wasm/progress';
2+
import { exposeAPI } from '@php-wasm/web';
3+
import { PlaygroundWorkerEndpoint } from './playground-worker-endpoint';
4+
import type { WorkerBootOptions } from './playground-worker-endpoint';
5+
import { runBlueprintV2 } from '@wp-playground/blueprints';
6+
import type { BlueprintV2Declaration } from '@wp-playground/blueprints';
7+
/* @ts-ignore */
8+
import { corsProxyUrl as defaultCorsProxyUrl } from 'virtual:cors-proxy-url';
9+
10+
// post message to parent
11+
self.postMessage('worker-script-started');
12+
13+
const downloadMonitor = new EmscriptenDownloadMonitor();
14+
15+
class PlaygroundWorkerEndpointV2 extends PlaygroundWorkerEndpoint {
16+
override async boot({
17+
scope,
18+
// mounts = [],
19+
wpVersion,
20+
phpVersion,
21+
sapiName = 'cli',
22+
withICU = false,
23+
withNetworking = true,
24+
corsProxyUrl,
25+
blueprint,
26+
}: WorkerBootOptions) {
27+
if (this.booted) {
28+
throw new Error('Playground already booted');
29+
}
30+
if (corsProxyUrl === undefined) {
31+
corsProxyUrl = defaultCorsProxyUrl as any;
32+
}
33+
this.booted = true;
34+
this.scope = scope;
35+
this.requestedWordPressVersion = wpVersion;
36+
37+
try {
38+
const knownRemoteAssetPaths = new Set<string>();
39+
const siteUrl = this.computeSiteUrl(scope);
40+
const requestHandler = await this.createRequestHandler({
41+
siteUrl,
42+
sapiName,
43+
withICU,
44+
corsProxyUrl,
45+
knownRemoteAssetPaths,
46+
withNetworking,
47+
phpVersion: phpVersion!,
48+
});
49+
const primaryPhp = await requestHandler.getPrimaryPhp();
50+
51+
if (!blueprint) {
52+
throw new Error(
53+
'Blueprints v2 runner requires a blueprint declaration.'
54+
);
55+
}
56+
57+
const streamed = await runBlueprintV2({
58+
php: primaryPhp,
59+
cliArgs: ['--site-url=' + siteUrl],
60+
blueprint: blueprint as BlueprintV2Declaration,
61+
onMessage: async (message: any) => {
62+
for (const listener of this.blueprintMessageListeners) {
63+
await listener(message);
64+
}
65+
},
66+
});
67+
await streamed.finished;
68+
69+
await this.finalizeAfterBoot(
70+
requestHandler,
71+
withNetworking,
72+
knownRemoteAssetPaths
73+
);
74+
setApiReady();
75+
} catch (e) {
76+
setAPIError(e as Error);
77+
throw e as Error;
78+
}
79+
}
80+
}
81+
82+
const [setApiReady, setAPIError] = exposeAPI(
83+
new PlaygroundWorkerEndpointV2(downloadMonitor)
84+
);

0 commit comments

Comments
 (0)