Skip to content

Commit 982d1e5

Browse files
authored
Merge pull request #1989 from Automattic/ops/1976-cloudflare-d1-provision
Preserve glob strings in generated D1 config
2 parents 1958200 + c7ae6d6 commit 982d1e5

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

scripts/provision-cloudflare-d1-coordinator.mjs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,34 @@ async function run(command) {
5555
}
5656

5757
function parseJsonc(value) {
58-
return JSON.parse(value.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/.*$/gm, "").replace(/,\s*([}\]])/g, "$1"))
58+
let output = ""
59+
let string = false
60+
let escaped = false
61+
let lineComment = false
62+
let blockComment = false
63+
for (let index = 0; index < value.length; index++) {
64+
const character = value[index]
65+
const next = value[index + 1]
66+
if (lineComment) {
67+
if (character === "\n") { lineComment = false; output += character }
68+
continue
69+
}
70+
if (blockComment) {
71+
if (character === "*" && next === "/") { blockComment = false; index++ }
72+
else if (character === "\n") output += character
73+
continue
74+
}
75+
if (string) {
76+
output += character
77+
if (escaped) escaped = false
78+
else if (character === "\\") escaped = true
79+
else if (character === '"') string = false
80+
continue
81+
}
82+
if (character === '"') { string = true; output += character }
83+
else if (character === "/" && next === "/") { lineComment = true; index++ }
84+
else if (character === "/" && next === "*") { blockComment = true; index++ }
85+
else output += character
86+
}
87+
return JSON.parse(output.replace(/,\s*([}\]])/g, "$1"))
5988
}

tests/cloudflare-d1-provisioner.test.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test("D1 provisioner creates once and emits a deployable config deterministicall
1717
const output = join(directory, "production.json")
1818
await writeFile(state, "missing")
1919
await writeFile(calls, "")
20-
await writeFile(template, `// template\n{"name":"worker","main":"src/worker-d1.ts","d1_databases":[{"binding":"WORDPRESS_STATE_DATABASE","database_name":"wp-codebox-runtime-state","database_id":"00000000-0000-0000-0000-000000000000"}],}`)
20+
await writeFile(template, `// template\n{"name":"worker","main":"src/worker-d1.ts","d1_databases":[{"binding":"WORDPRESS_STATE_DATABASE","database_name":"wp-codebox-runtime-state","database_id":"00000000-0000-0000-0000-000000000000"}],"rules":[{"globs":["**/*.wasm","https://example.test/*"]}],}`)
2121
await writeFile(wrangler, `#!${process.execPath}\nimport { appendFileSync, readFileSync, writeFileSync } from "node:fs"; const args=process.argv.slice(2); appendFileSync(${JSON.stringify(calls)}, JSON.stringify(args)+"\\n"); if(args[1]==="list") process.stdout.write(readFileSync(${JSON.stringify(state)},"utf8")==="created"?JSON.stringify([{name:"wp-codebox-runtime-state",uuid:"11111111-2222-3333-4444-555555555555"}]):"[]"); else if(args[1]==="create") writeFileSync(${JSON.stringify(state)},"created");`)
2222
await chmod(wrangler, 0o755)
2323

@@ -28,6 +28,7 @@ test("D1 provisioner creates once and emits a deployable config deterministicall
2828
const config = JSON.parse(await readFile(output, "utf8"))
2929
assert.equal(config.d1_databases[0].database_id, first.databaseId)
3030
assert.equal(config.main, join(directory, "src/worker-d1.ts"))
31+
assert.deepEqual(config.rules[0].globs, ["**/*.wasm", "https://example.test/*"])
3132
const invocations = (await readFile(calls, "utf8")).trim().split("\n").map(JSON.parse)
3233
assert.equal(invocations.filter((args) => args[1] === "create").length, 1)
3334
} finally {

0 commit comments

Comments
 (0)