Skip to content

Commit f6c8c67

Browse files
committed
Boot Cloudflare runtime from streamed WordPress seed
1 parent b89c7a4 commit f6c8c67

1 file changed

Lines changed: 49 additions & 6 deletions

File tree

packages/runtime-cloudflare/src/worker.ts

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ export default {
2020
const phase = new URL(request.url).searchParams.get("phase")
2121
if (phase) return runBootProbe(phase)
2222

23-
const runtime = await (bootPromise ??= bootWordPressRuntime())
23+
const runtime = await (bootPromise ??= bootWordPressRuntime(
24+
"do-not-attempt-installing",
25+
true,
26+
true,
27+
new Uint8Array(wordpressInstallSeed),
28+
))
2429
const phpVersion = (await runtime.php.run({ code: "<?php echo PHP_VERSION;" })).text.trim()
2530
return cloudflareRuntimeHealthResponse({
2631
schema: CLOUDFLARE_RUNTIME_HEALTH_SCHEMA,
@@ -82,7 +87,7 @@ async function runBootProbe(phase: string): Promise<Response> {
8287
}
8388
}
8489

85-
if (phase === "seeded-shortinit" || phase === "seeded-wordpress") {
90+
if (phase?.startsWith("seeded-")) {
8691
const runtime = await bootWordPressRuntime(
8792
"do-not-attempt-installing",
8893
true,
@@ -91,14 +96,17 @@ async function runBootProbe(phase: string): Promise<Response> {
9196
)
9297
try {
9398
const wordpress = (await runtime.php.run({
94-
code: phase === "seeded-shortinit"
95-
? "<?php define('SHORTINIT', true); require '/wordpress/wp-load.php'; echo json_encode(['wordpressVersion' => $wp_version, 'shortInit' => true]);"
96-
: "<?php require '/wordpress/wp-load.php'; echo json_encode(['siteUrl' => get_option('siteurl'), 'wordpressVersion' => get_bloginfo('version')]);",
99+
code: wordpressProbeCode(phase),
97100
})).text.trim()
98101
try {
99102
return probeResponse(phase, JSON.parse(wordpress) as Record<string, string>)
100103
} catch {
101-
throw new Error(`WordPress boot returned invalid JSON: ${wordpress}`)
104+
return Response.json({
105+
schema: "wp-codebox/cloudflare-boot-probe/v1",
106+
phase,
107+
completed: false,
108+
evidence: { rawPhpOutput: wordpress },
109+
}, { status: 500 })
102110
}
103111
} finally {
104112
runtime.php.exit()
@@ -122,6 +130,41 @@ async function runBootProbe(phase: string): Promise<Response> {
122130
return new Response(`Unknown boot probe phase: ${phase}`, { status: 400 })
123131
}
124132

133+
function wordpressProbeCode(phase: string): string {
134+
if (phase === "seeded-shortinit") {
135+
return "<?php define('SHORTINIT', true); require '/wordpress/wp-load.php'; echo json_encode(['wordpressVersion' => $wp_version, 'shortInit' => true]);"
136+
}
137+
if (phase === "seeded-wordpress") {
138+
return "<?php require '/wordpress/wp-load.php'; echo json_encode(['siteUrl' => get_option('siteurl'), 'wordpressVersion' => get_bloginfo('version')]);"
139+
}
140+
141+
const stops: Record<string, { needle: string; after?: boolean }> = {
142+
"seeded-includes": { needle: "/**\n * @since 3.3.0" },
143+
"seeded-muplugins": { needle: "if ( is_multisite() ) {\n\tms_cookie_constants();" },
144+
"seeded-plugins": { needle: "// Define constants which affect functionality if not already defined." },
145+
"seeded-theme": { needle: "// Create an instance of WP_Site_Health so that Cron events may fire." },
146+
"seeded-site-health-class": { needle: "WP_Site_Health::get_instance();" },
147+
"seeded-site-health": { needle: "// Set up current user." },
148+
"seeded-current-user": { needle: "/**\n * Fires after WordPress has finished loading but before any headers are sent." },
149+
"seeded-init": { needle: "// Check site status." },
150+
"seeded-wp-loaded": { needle: "do_action( 'wp_loaded' );", after: true },
151+
}
152+
const stop = stops[phase]
153+
if (!stop) throw new Error(`Unknown seeded WordPress probe phase: ${phase}`)
154+
155+
return `<?php
156+
$settings_path = '/wordpress/wp-settings.php';
157+
$settings = file_get_contents($settings_path);
158+
$needle = ${JSON.stringify(stop.needle)};
159+
if (strpos($settings, $needle) === false) {
160+
throw new Exception('WordPress bootstrap probe needle not found.');
161+
}
162+
$stop = "echo json_encode(['wordpressVersion' => \\$wp_version, 'bootstrapPhase' => '${phase}']); return;\n";
163+
$replacement = ${stop.after ? "$needle . \"\\n\" . $stop" : "$stop . $needle"};
164+
file_put_contents($settings_path, str_replace($needle, $replacement, $settings));
165+
require '/wordpress/wp-load.php';`
166+
}
167+
125168
async function bootWordPressRuntime(
126169
wordpressInstallMode: WordPressInstallMode = "install-from-existing-files",
127170
includeSqlite = true,

0 commit comments

Comments
 (0)