@@ -3,7 +3,7 @@ import { PlaygroundCliExitError, type PlaygroundCliBufferedOutput } from "./play
33import { PlaygroundPreviewPortUnavailableError , assertPreviewPortAvailable , errorHasCode , withPreviewProxy , type PlaygroundCliServer } from "./preview-server.js"
44import { startProgrammaticPlaygroundServer } from "./programmatic-playground-runner.js"
55import { normalizeLiveProgressEvent , previewLease , type BrowserStartupProgressEvent , type BrowserStartupProgressPhase , type BrowserStartupProgressStatus , type MountSpec , type PreviewLease , type RuntimeCreateSpec , type RuntimePreviewLeaseProvider } from "@automattic/wp-codebox-core"
6- import { randomBytes , randomInt } from "node:crypto"
6+ import { randomInt } from "node:crypto"
77import { existsSync } from "node:fs"
88import { createServer as createHttpServer , type Server as HttpServer } from "node:http"
99import { mkdir , readFile , rename , rm , stat , unlink , utimes , writeFile } from "node:fs/promises"
@@ -80,11 +80,6 @@ export async function startPlaygroundCliServer(spec: RuntimeCreateSpec, mounts:
8080 const wordpressInstallMode = spec . environment . wordpressInstallMode ?? "install-from-existing-files"
8181 const bootstrapIniEntries = runtimeBootstrapPhpIniEntries ( spec )
8282 const useProgrammaticRunner = shouldUseProgrammaticPlaygroundRunner ( spec , options )
83- const requestWorkerEndpoint = useProgrammaticRunner ? undefined : {
84- route : `/wp-codebox-execute-${ randomBytes ( 12 ) . toString ( "hex" ) } .php` ,
85- token : randomBytes ( 32 ) . toString ( "base64url" ) ,
86- payloadDirectory : join ( spec . artifactsDirectory ?? "artifacts" , "playground-internal-shared" ) ,
87- }
8883 usesArchiveCache = ! wordpressDirectory && ! spec . environment . assets ?. wordpressZip
8984 readonlyMountStaging = await stageReadonlyPlaygroundMounts ( mounts )
9085 const stagedMounts = readonlyMountStaging . mounts
@@ -137,7 +132,7 @@ export async function startPlaygroundCliServer(spec: RuntimeCreateSpec, mounts:
137132 } , Boolean ( spec . preview ?. port ) ) : await startPlaygroundCliWithDynamicPortRetry ( async ( port ) => {
138133 const { runCLI } = options . cliModule ?? ( await import ( "@wp-playground/cli" ) ) as unknown as PlaygroundCliModule
139134 const localAssetServer = wordpressStartupAsset ?. localPath ? await serveLocalStartupAsset ( wordpressStartupAsset . localPath ) : undefined
140- const bootstrapSharedMounts = await pluginRuntimeBootstrapSharedMounts ( spec , requestWorkerEndpoint )
135+ const bootstrapSharedMounts = await pluginRuntimeBootstrapSharedMounts ( spec )
141136 try {
142137 return await runCLI ( {
143138 command : "server" ,
@@ -179,7 +174,7 @@ export async function startPlaygroundCliServer(spec: RuntimeCreateSpec, mounts:
179174 fixedPreviewPort : spec . preview ?. port ?? null ,
180175 } )
181176
182- const proxiedServer = await withPreviewLeaseProvider ( await withPreviewProxy ( { ... server , ... ( requestWorkerEndpoint ? { requestWorkerEndpoint } : { } ) } , spec . preview ?. port ?? 0 , spec . preview ?. bind ) , spec )
177+ const proxiedServer = await withPreviewLeaseProvider ( await withPreviewProxy ( server , spec . preview ?. port ?? 0 , spec . preview ?. bind ) , spec )
183178 emitProgress ( "preview:ready" , "complete" , "Preview ready" , {
184179 localUrl : proxiedServer . serverUrl ,
185180 upstreamUrl : server . serverUrl ,
@@ -303,9 +298,10 @@ export function shouldUseProgrammaticPlaygroundRunner(spec: RuntimeCreateSpec, o
303298 && ( Boolean ( runtimeBootstrapPhpIniEntries ( spec ) ) || Boolean ( spec . environment . extensions ?. length ) )
304299}
305300
306- async function pluginRuntimeBootstrapSharedMounts ( spec : RuntimeCreateSpec , requestWorkerEndpoint ?: { route : string ; token : string ; payloadDirectory : string } ) : Promise < Array < { hostPath : string ; vfsPath : string } > > {
301+ async function pluginRuntimeBootstrapSharedMounts ( spec : RuntimeCreateSpec ) : Promise < Array < { hostPath : string ; vfsPath : string } > > {
307302 const iniEntries = runtimeBootstrapPhpIniEntries ( spec )
308- if ( ! iniEntries && ! requestWorkerEndpoint ) {
303+ const externalWpConfig = externalDatabaseWpConfig ( spec )
304+ if ( ! iniEntries && ! externalWpConfig ) {
309305 return [ ]
310306 }
311307
@@ -315,55 +311,17 @@ async function pluginRuntimeBootstrapSharedMounts(spec: RuntimeCreateSpec, reque
315311 await writeFile ( join ( directory , "php.ini" ) , phpIniContent ( iniEntries ) , "utf8" )
316312 await writeFile ( join ( directory , "wp-codebox-auto-prepend.php" ) , runtimeAutoPrependPhp ( spec ) , "utf8" )
317313 }
318- if ( requestWorkerEndpoint ) await writeFile ( join ( directory , "request-worker.php" ) , requestWorkerPhp ( requestWorkerEndpoint . token ) , "utf8" )
319- const externalWpConfig = externalDatabaseWpConfig ( spec )
320314 if ( externalWpConfig ) await writeFile ( join ( directory , "wp-config.php" ) , externalWpConfig , "utf8" )
321315
322316 return [
323317 ...( iniEntries ? [
324318 { hostPath : join ( directory , "php.ini" ) , vfsPath : "/internal/shared/php.ini" } ,
325319 { hostPath : join ( directory , "wp-codebox-auto-prepend.php" ) , vfsPath : "/internal/shared/wp-codebox-auto-prepend.php" } ,
326320 ] : [ ] ) ,
327- ...( requestWorkerEndpoint ? [
328- { hostPath : directory , vfsPath : "/internal/wp-codebox" } ,
329- { hostPath : join ( directory , "request-worker.php" ) , vfsPath : `/wordpress${ requestWorkerEndpoint . route } ` } ,
330- ] : [ ] ) ,
331321 ...( externalWpConfig ? [ { hostPath : join ( directory , "wp-config.php" ) , vfsPath : "/wordpress/wp-config.php" } ] : [ ] ) ,
332322 ]
333323}
334324
335- function requestWorkerPhp ( token : string ) : string {
336- return `<?php
337- if (!hash_equals(${ phpLiteral ( token ) } , (string) ($_SERVER['HTTP_X_WP_CODEBOX_EXECUTION_TOKEN'] ?? ''))) {
338- http_response_code(404);
339- exit;
340- }
341- $wp_codebox_payload_id = (string) ($_SERVER['HTTP_X_WP_CODEBOX_EXECUTION_PAYLOAD'] ?? '');
342- if (!preg_match('/^[a-f0-9]{32}$/', $wp_codebox_payload_id)) {
343- http_response_code(400);
344- exit;
345- }
346- $wp_codebox_payload = json_decode((string) file_get_contents('/internal/wp-codebox/execution-' . $wp_codebox_payload_id . '.json'), true);
347- $wp_codebox_code = $wp_codebox_payload['code'] ?? null;
348- $wp_codebox_environment = $wp_codebox_payload['environment'] ?? null;
349- if (!is_string($wp_codebox_code) || !is_array($wp_codebox_environment)) {
350- http_response_code(400);
351- echo 'invalid execution payload';
352- exit;
353- }
354- foreach ($wp_codebox_environment as $wp_codebox_name => $wp_codebox_value) {
355- if (!is_string($wp_codebox_name) || !is_string($wp_codebox_value)) {
356- http_response_code(400);
357- exit;
358- }
359- putenv($wp_codebox_name . '=' . $wp_codebox_value);
360- $_ENV[$wp_codebox_name] = $wp_codebox_value;
361- $_SERVER[$wp_codebox_name] = $wp_codebox_value;
362- }
363- eval('?>' . $wp_codebox_code);
364- `
365- }
366-
367325function runtimeBootstrapPhpIniEntries ( spec : RuntimeCreateSpec ) : Record < string , string > | undefined {
368326 const entries = pluginRuntimeBootstrapPhpIniEntries ( spec ) ?? { }
369327 if ( Object . keys ( entries ) . length === 0 && ! runtimeAutoPrependPhpBody ( spec ) ) {
0 commit comments