70bf21e064
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
127 lines
3.1 KiB
TypeScript
127 lines
3.1 KiB
TypeScript
import assert from "node:assert";
|
|
import { readFile, writeFile } from "node:fs/promises";
|
|
import { FormData, Response } from "undici";
|
|
|
|
async function serialiseHashes(
|
|
workers: Record<string, FormData>
|
|
): Promise<Record<string, string>> {
|
|
const workerHashes = {};
|
|
for (const [name, worker] of Object.entries(workers)) {
|
|
workerHashes[name] = await serialiseWorker(worker);
|
|
}
|
|
return workerHashes;
|
|
}
|
|
|
|
async function generateFileForWorker(workers: Record<string, FormData>) {
|
|
const workerHashes = await serialiseHashes(workers);
|
|
|
|
return `export default ${JSON.stringify(workerHashes, null, 2)};\n`;
|
|
}
|
|
|
|
const pythonWorker = async () => {
|
|
const worker = new FormData();
|
|
|
|
const metadata = {
|
|
main_module: "index.py",
|
|
compatibility_date: `$REPLACE_COMPAT_DATE`,
|
|
compatibility_flags: ["python_workers"],
|
|
};
|
|
|
|
worker.set("metadata", JSON.stringify(metadata));
|
|
|
|
worker.set(
|
|
"index.py",
|
|
new Blob([await readFile("./welcome/index.py", "utf8")], {
|
|
type: "text/x-python",
|
|
}),
|
|
"index.py"
|
|
);
|
|
return worker;
|
|
};
|
|
|
|
const defaultWorker = async () => {
|
|
const worker = new FormData();
|
|
|
|
const metadata = {
|
|
main_module: "index.js",
|
|
compatibility_date: `$REPLACE_COMPAT_DATE`,
|
|
compatibility_flags: ["nodejs_compat"],
|
|
};
|
|
|
|
worker.set("metadata", JSON.stringify(metadata));
|
|
|
|
worker.set(
|
|
"data.js",
|
|
new Blob([await readFile("./welcome/data.js", "utf8")], {
|
|
type: "application/javascript+module",
|
|
}),
|
|
"data.js"
|
|
);
|
|
|
|
worker.set(
|
|
"index.js",
|
|
new Blob([await readFile("./welcome/index.js", "utf8")], {
|
|
type: "application/javascript+module",
|
|
}),
|
|
"index.js"
|
|
);
|
|
|
|
worker.set(
|
|
"welcome.html",
|
|
new Blob([await readFile("./welcome/welcome.html", "utf8")], {
|
|
type: "text/plain",
|
|
}),
|
|
"welcome.html"
|
|
);
|
|
return worker;
|
|
};
|
|
|
|
async function serialiseWorker(worker: FormData) {
|
|
const serialisedWorker = new Response(worker);
|
|
|
|
const generatedBoundary =
|
|
// content-type is always set for a FormData response, and always has the boundary as a parameter
|
|
(serialisedWorker.headers.get("content-type") as string)
|
|
.split(";")[1]
|
|
.split("=")[1]
|
|
.trim();
|
|
|
|
// This boundary is arbitrary, it's just specified for stability
|
|
const fixedBoundary = "----formdata-88e2b909-318c-42df-af0d-9077f33c7988";
|
|
|
|
return {
|
|
contentType: `multipart/form-data; boundary=${fixedBoundary}`,
|
|
worker: await (
|
|
await serialisedWorker.text()
|
|
).replaceAll(generatedBoundary, fixedBoundary),
|
|
};
|
|
}
|
|
|
|
const pythonWorkerContent = await pythonWorker();
|
|
const defaultWorkerContent = await defaultWorker();
|
|
|
|
if (process.argv[2] === "check") {
|
|
const currentFile = await import("./src/QuickEditor/defaultHashes.js");
|
|
const generated = await serialiseHashes({
|
|
"/python": pythonWorkerContent,
|
|
"/": defaultWorkerContent,
|
|
});
|
|
|
|
try {
|
|
assert.deepEqual(currentFile.default, generated);
|
|
} catch (error) {
|
|
console.error("Hash not up to date", error);
|
|
console.log("current.txt", currentFile.default);
|
|
console.log("gen.txt", generated);
|
|
process.exit(1);
|
|
}
|
|
} else {
|
|
await writeFile(
|
|
"./src/QuickEditor/defaultHashes.js",
|
|
await generateFileForWorker({
|
|
"/python": pythonWorkerContent,
|
|
"/": defaultWorkerContent,
|
|
})
|
|
);
|
|
}
|