diff --git a/src/workerd/tools/BUILD.bazel b/src/workerd/tools/BUILD.bazel index 8b904599bf1..c8c4d3181f0 100644 --- a/src/workerd/tools/BUILD.bazel +++ b/src/workerd/tools/BUILD.bazel @@ -13,13 +13,42 @@ wd_cc_binary( ], ) -run_binary( +compat_dates = [ + # Oldest compatibility date, with no flags enabled + ("2021-01-01", "oldest"), + # https://developers.cloudflare.com/workers/platform/compatibility-dates/#formdata-parsing-supports-file + ("2021-11-03", "2021-11-03"), + # https://developers.cloudflare.com/workers/platform/compatibility-dates/#settersgetters-on-api-object-prototypes + ("2022-01-31", "2022-01-31"), + # https://developers.cloudflare.com/workers/platform/compatibility-dates/#global-navigator + ("2022-03-21", "2022-03-21"), + # https://developers.cloudflare.com/workers/platform/compatibility-dates/#global-navigator + ("2022-08-04", "2022-08-04"), + # Latest compatibility date + ("2030-01-01", "experimental"), +] + +filegroup( name = "api_encoder", - outs = ["api.capnp.bin"], - args = [ - "--output", - "$(location api.capnp.bin)", + srcs = [ + "//src/workerd/tools:api_encoder_" + label + for (date, label) in compat_dates ], - tool = "api_encoder_bin", visibility = ["//visibility:public"], ) + +[ + run_binary( + name = "api_encoder_" + label, + outs = [label + ".api.capnp.bin"], + args = [ + "--output", + "$(location " + label + ".api.capnp.bin)", + "--compatibility-date", + date, + ], + tool = "api_encoder_bin", + visibility = ["//visibility:public"], + ) + for (date, label) in compat_dates +] diff --git a/src/workerd/tools/api-encoder.c++ b/src/workerd/tools/api-encoder.c++ index 5cc9a324ad2..9d89ed41f3a 100644 --- a/src/workerd/tools/api-encoder.c++ +++ b/src/workerd/tools/api-encoder.c++ @@ -64,6 +64,10 @@ struct ApiEncoderMain { return kj::MainBuilder(context, "", "API Encoder") .addOptionWithArg({"o", "output"}, KJ_BIND_METHOD(*this, setOutput), "", "Output to ") + .addOptionWithArg( + {"c", "compatibility-date"}, + KJ_BIND_METHOD(*this, setCompatibilityDate), "", + "Set the compatibility date of the generated types to ") .callAfterParsing(KJ_BIND_METHOD(*this, run)) .build(); } @@ -73,6 +77,11 @@ struct ApiEncoderMain { return true; } + kj::MainBuilder::Validity setCompatibilityDate(kj::StringPtr value) { + compatibilityDate = value; + return true; + } + CompatibilityFlags::Reader compileFlags(capnp::MessageBuilder &message, kj::StringPtr compatDate, kj::ArrayPtr compatFlags) { @@ -102,11 +111,13 @@ struct ApiEncoderMain { bool run() { // Create RTTI builder with all non-experimental compatibility flags enabled - // TODO(soon): generate different types for different flags, for now, we - // set the compatibility date in the future such that all flags with a - // $compatEnableDate are enabled. capnp::MallocMessageBuilder flagsMessage; - auto flags = compileFlags(flagsMessage, "2023-01-01", {}); + CompatibilityFlags::Reader flags; + KJ_IF_MAYBE (date, compatibilityDate) { + flags = compileFlags(flagsMessage, *date, {}); + } else { + flags = compileFlags(flagsMessage, "2021-01-01", {}); + } auto builder = rtti::Builder(flags); // Build structure groups @@ -153,8 +164,7 @@ struct ApiEncoderMain { template void writeGroup( - capnp::List::Builder - &groups, + capnp::List::Builder &groups, rtti::Builder &builder, kj::StringPtr name) { auto group = groups[groupsIndex++]; group.setName(name); @@ -169,6 +179,7 @@ struct ApiEncoderMain { private: kj::ProcessContext &context; kj::Maybe output; + kj::Maybe compatibilityDate; unsigned int groupsIndex = 0; unsigned int structureIndex = 0; diff --git a/types/BUILD.bazel b/types/BUILD.bazel index 9d67f86d69e..bdf9ff6f7c0 100644 --- a/types/BUILD.bazel +++ b/types/BUILD.bazel @@ -28,13 +28,13 @@ js_run_binary( srcs = [ "//src/workerd/tools:api_encoder", ], - outs = ["api.d.ts", "api.ts"], # TODO(soon) switch to out_dirs when generating multiple files args = [ - "src/workerd/tools/api.capnp.bin", + "src/workerd/tools", "--output", - "types/api.d.ts", + "types/definitions", "--format", ], + out_dirs = ["definitions"], silent_on_success = False, # Always enable logging for debugging tool = ":types_bin", ) diff --git a/types/src/index.ts b/types/src/index.ts index e39a0677b1a..f405cec70d1 100644 --- a/types/src/index.ts +++ b/types/src/index.ts @@ -1,8 +1,7 @@ #!/usr/bin/env node import assert from "assert"; -import { mkdir, readFile, writeFile } from "fs/promises"; +import { appendFile, mkdir, readFile, readdir, writeFile } from "fs/promises"; import path from "path"; -import { arrayBuffer } from "stream/consumers"; import util from "util"; import { StructureGroups } from "@workerd/jsg/rtti.capnp.js"; import { Message } from "capnp-ts"; @@ -141,13 +140,14 @@ function printDefinitions( // Usage: types [options] [input] // // Options: -// -o, --output -// File path to write TypeScript to, defaults to stdout if omitted +// -o, --output +// Directory to write types to, in folders based on compat date // -f, --format // Formats generated types with Prettier // // Input: -// Binary Cap’n Proto file path, defaults to reading from stdin if omitted +// Directory containing binary Cap’n Proto file paths, in the format