|
1 | 1 | import { createHash } from "node:crypto" |
2 | 2 | import { mkdir, writeFile } from "node:fs/promises" |
3 | 3 | import { dirname, resolve } from "node:path" |
4 | | -import { decodeRemoteZip, encodeZip } from "@php-wasm/stream-compression" |
| 4 | +import { decodeZip, encodeZip } from "@php-wasm/stream-compression" |
5 | 5 | import { isWordPressRuntimeFile } from "../packages/runtime-cloudflare/src/wordpress-runtime-corpus.js" |
6 | 6 | import { WORDPRESS_RUNTIME_ARTIFACT_SCHEMA, wordpressRuntimeArtifactKey, type WordPressRuntimeArtifactManifest } from "../packages/runtime-cloudflare/src/wordpress-runtime-artifact.js" |
7 | 7 |
|
8 | 8 | const sourceUrl = process.env.WORDPRESS_RUNTIME_ARCHIVE_URL ?? "https://downloads.wordpress.org/release/wordpress-7.0.2.zip" |
9 | 9 | const sourceVersion = process.env.WORDPRESS_RUNTIME_VERSION ?? "7.0.2" |
10 | 10 | const output = resolve(process.env.WORDPRESS_RUNTIME_ARTIFACT_OUTPUT ?? "artifacts/cloudflare-wordpress-runtime-corpus.zip") |
11 | 11 | const manifestOutput = resolve("packages/runtime-cloudflare/assets/wordpress-runtime-artifact.json") |
12 | | -const decoder = new TextDecoder() |
13 | | -const archivePaths = new Set<string>() |
14 | | -const selected: File[] = [] |
15 | | - |
16 | | -const response = await fetch(sourceUrl, { method: "HEAD" }) |
17 | | -if (!response.ok) throw new Error(`Unable to inspect WordPress archive: ${response.status}.`) |
| 12 | +const response = await fetch(sourceUrl) |
| 13 | +if (!response.ok || !response.body) throw new Error(`Unable to download WordPress archive: ${response.status}.`) |
18 | 14 | const identity = response.headers.get("etag") ?? response.headers.get("last-modified") ?? undefined |
19 | | -const stream = await decodeRemoteZip(sourceUrl, (entry) => { |
20 | | - const path = decoder.decode(entry.path) |
21 | | - archivePaths.add(path) |
22 | | - return isWordPressRuntimeFile(path, archivePaths) |
23 | | -}) |
24 | | -for await (const entry of stream) { |
25 | | - const path = entry instanceof File ? entry.name : decoder.decode(entry.path) |
26 | | - if (!isWordPressRuntimeFile(path, archivePaths)) continue |
27 | | - const bytes = entry instanceof File ? new Uint8Array(await entry.arrayBuffer()) : entry.bytes |
28 | | - selected.push(new File([bytes], path, { lastModified: 0 })) |
29 | | -} |
| 15 | +const files: File[] = [] |
| 16 | +for await (const file of decodeZip(response.body)) files.push(file) |
| 17 | +const archivePaths = new Set(files.map((file) => file.name)) |
| 18 | +const selected = files |
| 19 | + .filter((file) => isWordPressRuntimeFile(file.name, archivePaths)) |
| 20 | + .map((file) => new File([file], file.name, { lastModified: 0 })) |
30 | 21 | selected.sort((left, right) => left.name.localeCompare(right.name)) |
31 | 22 | if (!selected.length) throw new Error("WordPress archive did not yield a runtime corpus.") |
32 | 23 | const archive = new Uint8Array(await new Response(encodeZip(selected)).arrayBuffer()) |
|
0 commit comments