Skip to content
File

Blob: types/scripts/build-types.ts

typescript201 lines
1// Copyright (c) 2026 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5import assert from "node:assert";
6import childProcess from "node:child_process";
7import events from "node:events";
8import { readFileSync, readdirSync } from "node:fs";
9import fs from "node:fs/promises";
10import path from "node:path";
11import ts from "typescript";
12import { SourcesMap, createMemoryProgram } from "../src/program";
13import { getFilePath } from "../src/utils";
14 
15const OUTPUT_PATH = getFilePath("types/definitions");
16const ENTRYPOINTS = [
17 { compatDate: "2021-01-01", name: "oldest" },
18 // https://developers.cloudflare.com/workers/platform/compatibility-dates/#formdata-parsing-supports-file
19 { compatDate: "2021-11-03" },
20 // https://developers.cloudflare.com/workers/platform/compatibility-dates/#settersgetters-on-api-object-prototypes
21 { compatDate: "2022-01-31" },
22 // https://developers.cloudflare.com/workers/platform/compatibility-dates/#global-navigator
23 { compatDate: "2022-03-21" },
24 // https://developers.cloudflare.com/workers/platform/compatibility-dates/#r2-bucket-list-respects-the-include-option
25 { compatDate: "2022-08-04" },
26 // https://developers.cloudflare.com/workers/platform/compatibility-dates/#new-url-parser-implementation
27 { compatDate: "2022-10-31" },
28 // https://developers.cloudflare.com/workers/platform/compatibility-dates/#streams-constructors
29 // https://developers.cloudflare.com/workers/platform/compatibility-dates/#compliant-transformstream-constructor
30 { compatDate: "2022-11-30" },
31 // https://github.com/cloudflare/workerd/blob/fcb6f33d10c71975cb2ce68dbf1924a1eeadbd8a/src/workerd/io/compatibility-date.capnp#L275-L280 (http_headers_getsetcookie)
32 { compatDate: "2023-03-01" },
33 // https://github.com/cloudflare/workerd/blob/fcb6f33d10c71975cb2ce68dbf1924a1eeadbd8a/src/workerd/io/compatibility-date.capnp#L307-L312 (urlsearchparams_delete_has_value_arg)
34 { compatDate: "2023-07-01" },
35 // Latest compatibility date with experimental features
36 { compatDate: "2999-12-31", name: "latest" },
37 // Latest compatibility date with experimental features
38 { compatDate: "experimental" },
39];
40 
41/**
42 * Copy all TS lib files into the memory filesystem. We only use lib.esnext
43 * but since TS lib files all reference each other to various extents it's
44 * easier to add them all and let TS figure out which ones it actually needs to load.
45 * This function uses the current local installation of TS as a source for lib files
46 */
47let cachedLibFiles: SourcesMap | null = null;
48function loadLibFiles(): SourcesMap {
49 if (cachedLibFiles != null) {
50 return cachedLibFiles;
51 }
52 
53 const libLocation = path.dirname(require.resolve("typescript"));
54 const libFiles = readdirSync(libLocation).filter(
55 (file) => file.startsWith("lib.") && file.endsWith(".d.ts")
56 );
57 const lib: SourcesMap = new Map();
58 for (const file of libFiles) {
59 lib.set(
60 `/node_modules/typescript/lib/${file}`,
61 readFileSync(path.join(libLocation, file), "utf-8")
62 );
63 }
64 
65 cachedLibFiles = lib;
66 return lib;
67}
68 
69function checkDiagnostics(sources: SourcesMap): void {
70 const program = createMemoryProgram(
71 sources,
72 undefined,
73 {
74 noEmit: true,
75 lib: ["lib.esnext.d.ts"],
76 types: [],
77 noUnusedParameters: true,
78 },
79 loadLibFiles()
80 );
81 
82 const emitResult = program.emit();
83 
84 const allDiagnostics = ts
85 .getPreEmitDiagnostics(program)
86 .concat(emitResult.diagnostics);
87 
88 allDiagnostics.forEach((diagnostic) => {
89 if (diagnostic.file) {
90 const { line, character } = ts.getLineAndCharacterOfPosition(
91 diagnostic.file,
92 diagnostic.start
93 );
94 const message = ts.flattenDiagnosticMessageText(
95 diagnostic.messageText,
96 "\n"
97 );
98 console.log(
99 `${diagnostic.file.fileName}:${line + 1}:${character + 1} : ${message}`
100 );
101 } else {
102 console.log(
103 ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")
104 );
105 }
106 });
107 
108 assert(allDiagnostics.length === 0, "TypeScript failed to compile!");
109}
110 
111function spawnWorkerd(
112 configPath: string
113): Promise<{ url: URL; kill: () => Promise<void> }> {
114 return new Promise((resolve) => {
115 const workerdProcess = childProcess.spawn(
116 getFilePath("src/workerd/server/workerd"),
117 ["serve", "--verbose", "--experimental", "--control-fd=3", configPath],
118 { stdio: ["inherit", "inherit", "inherit", "pipe"] }
119 );
120 const exitPromise = events.once(workerdProcess, "exit");
121 workerdProcess.stdio[3]?.on("data", (chunk: Buffer): void => {
122 const message = JSON.parse(chunk.toString().trim()) as {
123 event: string
124 port: number
125 };
126 assert.strictEqual(message.event, "listen");
127 resolve({
128 url: new URL(`http://127.0.0.1:${message.port}`),
129 async kill() {
130 workerdProcess.kill("SIGTERM");
131 await exitPromise;
132 },
133 });
134 });
135 });
136}
137 
138async function buildEntrypoint(
139 entrypoint: (typeof ENTRYPOINTS)[number],
140 workerUrl: URL
141): Promise<{ name: string; files: Array<{ fileName: string; content: string }> }> {
142 const url = new URL(`/${entrypoint.compatDate}.bundle`, workerUrl);
143 const response = await fetch(url);
144 if (!response.ok) throw new Error(await response.text());
145 const bundle = await response.formData();
146 
147 const name = entrypoint.name ?? entrypoint.compatDate;
148 const entrypointPath = path.join(OUTPUT_PATH, name);
149 await fs.mkdir(entrypointPath, { recursive: true });
150 
151 const files: Array<{ fileName: string; content: string }> = [];
152 
153 const filePromises: Promise<void>[] = [];
154 
155 for (const [fileName, definitions] of bundle) {
156 assert(typeof definitions === "string");
157 const prettierIgnoreRegexp = /^\s*\/\/\s*prettier-ignore\s*\n/gm;
158 const typings = definitions.replaceAll(prettierIgnoreRegexp, "");
159 
160 files.push({ fileName, content: typings });
161 filePromises.push(fs.writeFile(path.join(entrypointPath, fileName), typings));
162 }
163 
164 // Write all files in parallel (without prettier formatting)
165 await Promise.all(filePromises);
166 
167 return { name, files };
168}
169 
170async function buildAllEntrypoints(workerUrl: URL): Promise<void> {
171 const allEntrypoints = await Promise.all(
172 ENTRYPOINTS.map(entrypoint => buildEntrypoint(entrypoint, workerUrl))
173 );
174 
175 // Format all TypeScript files with a single Prettier CLI call using exact same defaults as API
176 const prettierPath = require.resolve("prettier/bin/prettier.cjs");
177 childProcess.execSync(`${prettierPath} "${OUTPUT_PATH}/**/*.ts" --write --parser=typescript`);
178 
179 for (const { files } of allEntrypoints) {
180 const entrypointFiles = new SourcesMap();
181 for (const { fileName, content } of files) {
182 entrypointFiles.set(`/$virtual/${fileName}`, content);
183 }
184 
185 if (entrypointFiles.size > 0) {
186 checkDiagnostics(entrypointFiles);
187 }
188 }
189}
190export async function main(): Promise<void> {
191 const worker = await spawnWorkerd(getFilePath("types/scripts/config.capnp"));
192 try {
193 await buildAllEntrypoints(worker.url);
194 } finally {
195 await worker.kill();
196 }
197}
198 
199// Outputting to a CommonJS module so can't use top-level await
200if (require.main === module) void main();