From c5d91d50d04cfed9bbe30c5ed581d6f385b3d653 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Wed, 21 Oct 2026 15:39:26 +0800 Subject: [PATCH] perf(cdp): compact protocol metadata at build time Parse the vendored schema with serde_json::Value and serialize compact JSON in build.rs. Embed the generated bytes without request-time processing. Verify the embedded protocol matches the complete vendored schema. Validation on the unchanged final source tree: cargo fmt --all; cargo clippy --workspace --all-targets --all-features -- -D warnings; cargo nextest run --no-fail-fast (20,505 passed, 18 existing skips). --- moli-protocol-cdp/Cargo.toml | 3 +++ moli-protocol-cdp/build.rs | 14 ++++++++++++++ moli-protocol-cdp/src/metadata.rs | 3 +-- moli-protocol-cdp/tests/protocol_metadata.rs | 11 +++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 moli-protocol-cdp/build.rs create mode 100644 moli-protocol-cdp/tests/protocol_metadata.rs diff --git a/moli-protocol-cdp/Cargo.toml b/moli-protocol-cdp/Cargo.toml index 9ee5b4511f..3f96c942db 100644 --- a/moli-protocol-cdp/Cargo.toml +++ b/moli-protocol-cdp/Cargo.toml @@ -10,5 +10,8 @@ moli-time = { path = "../moli-time" } serde = { version = "1.0.228", features = ["derive"] } serde_json = "1.0.145" +[build-dependencies] +serde_json = "1.0.145" + [lints] workspace = true diff --git a/moli-protocol-cdp/build.rs b/moli-protocol-cdp/build.rs new file mode 100644 index 0000000000..dce3fb9602 --- /dev/null +++ b/moli-protocol-cdp/build.rs @@ -0,0 +1,14 @@ +use std::{env, fs, path::PathBuf}; + +const PROTOCOL_SOURCE: &str = "../vendor/v8-152.2.0/v8/include/js_protocol-1.3.json"; + +fn main() { + println!("cargo:rerun-if-changed={PROTOCOL_SOURCE}"); + let source = fs::read(PROTOCOL_SOURCE).expect("read vendored CDP protocol metadata"); + let protocol: serde_json::Value = + serde_json::from_slice(&source).expect("parse vendored CDP protocol metadata"); + let compact = serde_json::to_vec(&protocol).expect("serialize compact CDP protocol metadata"); + let destination = PathBuf::from(env::var_os("OUT_DIR").expect("Cargo build output directory")) + .join("cdp-protocol.json"); + fs::write(destination, compact).expect("write compact CDP protocol metadata"); +} diff --git a/moli-protocol-cdp/src/metadata.rs b/moli-protocol-cdp/src/metadata.rs index fa91c5684b..eb1d37aadc 100644 --- a/moli-protocol-cdp/src/metadata.rs +++ b/moli-protocol-cdp/src/metadata.rs @@ -1,2 +1 @@ -pub const CDP_PROTOCOL_JSON: &str = - include_str!("../../vendor/v8-152.2.0/v8/include/js_protocol-1.3.json"); +pub const CDP_PROTOCOL_JSON: &str = include_str!(concat!(env!("OUT_DIR"), "/cdp-protocol.json")); diff --git a/moli-protocol-cdp/tests/protocol_metadata.rs b/moli-protocol-cdp/tests/protocol_metadata.rs new file mode 100644 index 0000000000..16d96e08c2 --- /dev/null +++ b/moli-protocol-cdp/tests/protocol_metadata.rs @@ -0,0 +1,11 @@ +use moli_protocol_cdp::CDP_PROTOCOL_JSON; +use serde_json::Value; + +#[test] +fn embedded_protocol_preserves_the_complete_vendored_schema() { + let source = include_str!("../../vendor/v8-152.2.0/v8/include/js_protocol-1.3.json"); + let original: Value = serde_json::from_str(source).expect("valid vendored CDP protocol"); + let embedded: Value = + serde_json::from_str(CDP_PROTOCOL_JSON).expect("valid embedded CDP protocol"); + assert_eq!(embedded, original); +}