Skip to content

Commit 97ba1f7

Browse files
committed
Persist canonical MDI revisions in R2
1 parent b8ea986 commit 97ba1f7

8 files changed

Lines changed: 202 additions & 99 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"wp-codebox:source": "node bin/wp-codebox-source.mjs",
9191
"generate:browser-fanout-aggregation-runtime": "tsx scripts/generate-browser-fanout-aggregation-runtime.ts",
9292
"generate:fanout-aggregation-contract": "tsx scripts/generate-fanout-aggregation-contract-fixture.ts",
93+
"generate:cloudflare-mdi-runtime-bundle": "node scripts/build-cloudflare-mdi-runtime-bundle.mjs",
9394
"smoke": "tsx scripts/run-smoke.ts",
9495
"test:redaction": "tsx tests/redaction.test.ts",
9596
"test:cloudflare-runtime": "tsx tests/cloudflare-runtime.test.ts && node ./node_modules/typescript/bin/tsc -p packages/runtime-cloudflare --noEmit",
Binary file not shown.
Binary file not shown.

packages/runtime-cloudflare/src/worker.ts

Lines changed: 160 additions & 98 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "*.zip" {
2+
const archive: ArrayBuffer
3+
export default archive
4+
}

packages/runtime-cloudflare/wrangler.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
],
1818
"rules": [
1919
{ "type": "CompiledWasm", "globs": ["**/*.wasm"], "fallthrough": false },
20-
{ "type": "Data", "globs": ["**/*.sqlite"], "fallthrough": false }
20+
{ "type": "Data", "globs": ["**/*.sqlite", "**/*-runtime.zip"], "fallthrough": false }
2121
]
2222
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { writeFile } from "node:fs/promises"
2+
import { decodeZip, encodeZip } from "@php-wasm/stream-compression"
3+
4+
const revision = "af451895a9429895e3ad4d9b4e073bfd88873745"
5+
const archiveUrl = `https://codeload.github.com/Automattic/markdown-database-integration/zip/${revision}`
6+
const output = new URL("../packages/runtime-cloudflare/assets/markdown-database-integration-runtime.zip", import.meta.url)
7+
const response = await fetch(archiveUrl)
8+
if (!response.ok || !response.body) throw new Error(`Unable to fetch Markdown Database Integration: ${response.status}.`)
9+
10+
const runtimePaths = new Set([
11+
"db.php",
12+
"inc/class-wp-markdown-db.php",
13+
"inc/class-wp-markdown-driver.php",
14+
"inc/class-wp-markdown-frontmatter-profiles.php",
15+
"inc/class-wp-markdown-loader.php",
16+
"inc/class-wp-markdown-search.php",
17+
"inc/class-wp-markdown-storage.php",
18+
"inc/class-wp-markdown-write-engine.php",
19+
])
20+
const runtimeFiles = []
21+
for await (const entry of decodeZip(response.body)) {
22+
const separator = entry.name.indexOf("/")
23+
const relative = separator === -1 ? "" : entry.name.slice(separator + 1)
24+
if (!runtimePaths.has(relative)) continue
25+
runtimeFiles.push(new File([await entry.arrayBuffer()], relative, { lastModified: 0 }))
26+
}
27+
if (runtimeFiles.length !== runtimePaths.size) throw new Error(`Expected ${runtimePaths.size} MDI runtime files, received ${runtimeFiles.length}.`)
28+
29+
const archive = await new Response(encodeZip(runtimeFiles)).arrayBuffer()
30+
await writeFile(output, new Uint8Array(archive))
31+
console.log(`Bundled ${runtimeFiles.length} MDI runtime files from ${revision} (${archive.byteLength} bytes).`)

tests/cloudflare-runtime.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ test("Cloudflare runtime packages the disposable WordPress install seed", async
3737
migrations?: Array<{ new_sqlite_classes?: string[] }>
3838
}
3939
const seed = await readFile(new URL("../packages/runtime-cloudflare/assets/wordpress-install-seed.sqlite", import.meta.url))
40+
const markdownIndex = await readFile(new URL("../packages/runtime-cloudflare/assets/markdown-primary-bootstrap-index.sqlite", import.meta.url))
41+
const markdownRuntime = await readFile(new URL("../packages/runtime-cloudflare/assets/markdown-database-integration-runtime.zip", import.meta.url))
4042

4143
assert.equal(seed.subarray(0, 16).toString(), "SQLite format 3\0")
44+
assert.equal(markdownIndex.subarray(0, 16).toString(), "SQLite format 3\0")
45+
assert.equal(markdownRuntime.subarray(0, 4).toString("hex"), "504b0304")
4246
assert.ok(config.rules?.some((rule) => rule.type === "Data" && rule.globs?.includes("**/*.sqlite")))
47+
assert.ok(config.rules?.some((rule) => rule.type === "Data" && rule.globs?.includes("**/*-runtime.zip")))
4348
assert.deepEqual(config.r2_buckets, [{ binding: "WORDPRESS_STATE_BUCKET", bucket_name: "wp-codebox-runtime-chubes" }])
4449
assert.deepEqual(config.durable_objects?.bindings, [{ name: "WORDPRESS_STATE", class_name: "WordPressStateCoordinator" }])
4550
assert.ok(config.migrations?.some((migration) => migration.new_sqlite_classes?.includes("WordPressStateCoordinator")))

0 commit comments

Comments
 (0)