From 8bd558991d884a8651def1766a80f54d7491542f Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Fri, 30 Oct 2026 23:02:35 -0500 Subject: [PATCH 1/2] feat(add,create): matcha add cookie and matcha create transformer add cookie reads a cookie with @cookie and sets one through a @Transformer and setCookie, since core sends Set-Cookie only as a header. create transformer writes a typed TransformerFn and prints how to attach it; there is nothing to wire into a module. starter-smoke now covers both. add cookie uses @cookie and setCookie, which are new in core RC.1, so this lands together with the CORE_VERSION bump. Signed-off-by: Diego Resendez --- CHANGELOG.md | 6 ++++++ scripts/starter-smoke.sh | 3 ++- src/cmd_add.rs | 33 +++++++++++++++++++++++++++++++++ src/cmd_create.rs | 33 ++++++++++++++++++++++++++++++++- tests/add.rs | 34 ++++++++++++++++++++++++++++++++++ tests/create.rs | 26 ++++++++++++++++++++++++++ 6 files changed, 133 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88cfcae..3f731e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,12 @@ is pinned to is `CORE_VERSION` in `src/template.rs`, and each entry below says w ### Added +- **`matcha add cookie`** adds `GET /prefs`, which reads a cookie with `@cookie('theme')`, and + `POST /prefs`, which sets it through a `@Transformer` and `setCookie()`. Core has no response-cookie + API, so the transformer is where a `Set-Cookie` header is sent. Needs `@green-tea/core` RC.1. +- **`matcha create transformer `** writes `src/transformers/.transformer.ts`, a typed + `TransformerFn`, and prints the `@Transformer(...)` line that attaches it. A transformer belongs to + a route, not a module, so there is nothing to wire. - **Every starter ships an `AGENTS.md` and a `CLAUDE.md` that imports it.** green-tea is new, so a coding agent falls back on Express habits. The file states the rules that differ: it is a graph, not a chain, a handler declares its needs in its signature, and the graph is checked at boot. It also points diff --git a/scripts/starter-smoke.sh b/scripts/starter-smoke.sh index f38ad5c..c3f12ac 100755 --- a/scripts/starter-smoke.sh +++ b/scripts/starter-smoke.sh @@ -96,9 +96,10 @@ echo "✓ graph, explain and openapi resolve" # core lazy-requires `ws` and `busboy`, so neither is needed to compile, and a # check that needed them would be hiding that fact. echo "▸ matcha add (every capability)" -for cap in sse ws upload stream buffer; do +for cap in sse ws upload stream buffer cookie; do "$MATCHA" add "$cap" >/dev/null || { echo "matcha add $cap failed"; exit 1; } done +"$MATCHA" create transformer Csv >/dev/null || { echo "matcha create transformer failed"; exit 1; } "${CHECK[@]}" || { echo "the generated handlers do not type-check"; exit 1; } echo "✓ every add capability type-checks" diff --git a/src/cmd_add.rs b/src/cmd_add.rs index 4aa09e3..fe76248 100644 --- a/src/cmd_add.rs +++ b/src/cmd_add.rs @@ -139,6 +139,39 @@ const CAPABILITIES: &[Capability] = &[ marker: "'/upload'", peer: Some(("busboy", "every runtime")), }, + Capability { + name: "cookie", + // Reading is a decorator. Writing is a `Set-Cookie` header, which core only builds + // (`setCookie`) and a transformer sends: core has no response-cookie API of its own. + imports: &["Get", "Post", "Transformer", "body", "cookie", "setCookie"], + type_imports: &[], + method: r#" + @Get('/prefs') + prefs(@cookie('theme') theme?: string) { + return { theme: theme ?? 'light' }; + } + + @Post('/prefs') + @Transformer((value) => { + const { theme } = value as { theme: string }; + return { + status: 200, + headers: { + 'content-type': 'application/json', + // Secure and HttpOnly are on by default. Safari, a LAN IP or a plain-HTTP host other + // than localhost drops a Secure cookie: pass `secure: false` there in development. + 'set-cookie': setCookie('theme', encodeURIComponent(theme), { maxAge: 60 * 60 * 24 * 365 }), + }, + body: JSON.stringify({ theme }), + }; + }) + savePrefs(@body('theme') theme: string) { + return { theme }; + } +"#, + marker: "'/prefs'", + peer: None, + }, ]; /// The capability names, for clap's `value_parser` and for error messages. diff --git a/src/cmd_create.rs b/src/cmd_create.rs index 65fabee..e8143a4 100644 --- a/src/cmd_create.rs +++ b/src/cmd_create.rs @@ -56,7 +56,7 @@ pub fn kinds() -> Vec<&'static str> { ["module"] .into_iter() .chain(PIECES.iter().map(|p| p.kind)) - .chain(["plugin"]) + .chain(["transformer", "plugin"]) .collect() } @@ -121,6 +121,9 @@ pub fn run(kind: &str, name: &str, check: bool) -> std::io::Result<()> { if kind == "module" { return create_module(name, check); } + if kind == "transformer" { + return create_transformer(name, check); + } match PIECES.iter().find(|p| p.kind == kind) { Some(piece) => create_piece(name, check, piece), None => Err(Error::new( @@ -200,6 +203,34 @@ fn create_module(name: &str, check: bool) -> std::io::Result<()> { maybe_check(check) } +/// Writes `src/transformers/.transformer.ts`. Nothing to wire: a transformer is attached +/// per route with `@Transformer`, so the hint says where it goes instead. +fn create_transformer(name: &str, check: bool) -> std::io::Result<()> { + let lower = name.to_lowercase(); + let file = Path::new("src/transformers").join(format!("{lower}.transformer.ts")); + let symbol = format!("{}Transformer", token(name)); + write_new(&file, transformer_stub(&symbol))?; + println!("✓ {}", file.display()); + println!( + "→ attach it to a route: @Transformer({symbol}), imported from './transformers/{lower}.transformer'" + ); + maybe_check(check) +} + +fn transformer_stub(symbol: &str) -> String { + format!( + "import type {{ TransformerFn }} from '@green-tea/core';\n\n\ +/**\n * Turns what a handler returns into the response. Attach it to a route with\n\ + * `@Transformer({symbol})`; routes without one use the default JSON transformer.\n\ + * Set the status and headers here (`set-cookie` takes an array, one header per cookie).\n */\n\ +export const {symbol}: TransformerFn = (value) => ({{\n\ + status: 200,\n\ + headers: {{ 'content-type': 'application/json' }},\n\ + body: JSON.stringify(value),\n\ +}});\n" + ) +} + pub(crate) fn maybe_check(check: bool) -> std::io::Result<()> { if check { match crate::check::run(Path::new(".")) { diff --git a/tests/add.rs b/tests/add.rs index 8bbbf1d..998a29e 100644 --- a/tests/add.rs +++ b/tests/add.rs @@ -199,3 +199,37 @@ fn add_rejects_a_controller_path_that_does_not_exist() { .failure() .stderr(predicates::str::contains("no such controller")); } + +#[test] +fn add_cookie_reads_with_the_decorator_and_writes_through_a_transformer() { + let d = tempdir().unwrap(); + std::fs::create_dir_all(d.path().join("src/controllers")).unwrap(); + std::fs::write( + d.path().join("src/controllers/home.controller.ts"), + "import { Route, Get } from '@green-tea/core';\n\n\ +@Route('/')\nexport class HomeController {\n @Get('/')\n home() {}\n}\n", + ) + .unwrap(); + for _ in 0..2 { + Command::cargo_bin("matcha") + .unwrap() + .current_dir(d.path()) + .args(["add", "cookie"]) + .assert() + .success(); + } + let out = std::fs::read_to_string(d.path().join("src/controllers/home.controller.ts")).unwrap(); + assert!(out.contains("@cookie('theme')"), "{out}"); + assert!(out.contains("'set-cookie': setCookie("), "{out}"); + for symbol in ["Post", "Transformer", "body", "cookie", "setCookie"] { + assert!( + out.contains(&format!("{symbol},")) || out.contains(&format!("{symbol} }}")), + "{symbol} not imported:\n{out}" + ); + } + assert_eq!( + out.matches("@Post('/prefs')").count(), + 1, + "not idempotent:\n{out}" + ); +} diff --git a/tests/create.rs b/tests/create.rs index 966f7d5..4f55a3c 100644 --- a/tests/create.rs +++ b/tests/create.rs @@ -170,3 +170,29 @@ fn create_does_not_overwrite_an_existing_file() { .stderr(predicates::str::contains("already exists")); assert_eq!(std::fs::read_to_string(&file).unwrap(), "// edited\n"); } + +#[test] +fn create_transformer_writes_a_typed_function_and_says_where_it_goes() { + let d = tempdir().unwrap(); + seed_module(d.path()); + create(d.path(), "transformer", "csv-export") + .success() + .stdout(predicates::str::contains( + "@Transformer(csvExportTransformer)", + )); + let file = std::fs::read_to_string(d.path().join("src/transformers/csvexport.transformer.ts")) + .unwrap(); + assert!( + file.contains("import type { TransformerFn } from '@green-tea/core'"), + "{file}" + ); + assert!( + file.contains("export const csvExportTransformer: TransformerFn"), + "{file}" + ); + let module = std::fs::read_to_string(d.path().join("src/app.module.ts")).unwrap(); + assert!( + !module.contains("Transformer"), + "a transformer is not a module member:\n{module}" + ); +} -- 2.54.1 From 8e45c1170feaca69c7c897898a0a8f81a91b0991 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Tue, 17 Nov 2026 17:31:03 -0600 Subject: [PATCH 2/2] feat(templates): scaffold against core 26.11.0-rc.1; edge installs core from npm core#93 is fixed in RC.1: the npm ESM build's createRequire banner no longer throws on workerd, so the edge starter drops its .npmrc and the npm:@jsr alias and installs @green-tea/core from npm like every other runtime. doctor still reads the alias, so edge projects scaffolded before this keep working. The weekly core-freshness job compares against core's latest dist-tag: release candidates and the stable release publish there, and beta stays on 26.9.0-beta.2. Starter smoke passes locally on node, deno, bun and edge. Deno needs --minimum-dependency-age=0 until RC.1 is a day old. Signed-off-by: Diego Resendez --- .github/workflows/ci.yml | 6 +++--- CONTRIBUTING.md | 2 +- README.md | 8 ++------ src/template.rs | 4 ++-- template/runtimes/edge/.npmrc | 9 --------- template/runtimes/edge/package.json | 2 +- tests/plugin.rs | 2 +- 7 files changed, 10 insertions(+), 23 deletions(-) delete mode 100644 template/runtimes/edge/.npmrc diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cf22e3b..9d55c25 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -65,13 +65,13 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v5 - - name: Compare the scaffold pin against the beta dist-tag + - name: Compare the scaffold pin against the latest dist-tag run: | pinned=$(grep -oP 'CORE_VERSION: &str = "\K[^"]+' src/template.rs) - latest=$(npm view @green-tea/core dist-tags.beta) + latest=$(npm view @green-tea/core dist-tags.latest) echo "pinned=$pinned latest=$latest" if [ "$pinned" != "$latest" ]; then - echo "::error::matcha scaffolds @green-tea/core@$pinned but the beta tag is $latest." + echo "::error::matcha scaffolds @green-tea/core@$pinned but the latest tag is $latest." echo "Bump CORE_VERSION in src/template.rs and let the starter job prove it still boots." exit 1 fi diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d3b6d37..d00177c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -126,7 +126,7 @@ scripts/starter-smoke.sh ``` CI runs the smoke for all four runtimes on a matrix, plus a weekly `core-freshness` job -that fails when `CORE_VERSION` in `src/template.rs` falls behind core's `beta` dist-tag. +that fails when `CORE_VERSION` in `src/template.rs` falls behind core's `latest` dist-tag. **Non-trivial logic leaves a check behind.** Not a suite — the smallest thing that fails if the logic breaks. A generator gets a test that the generated code carries what it must; a diff --git a/README.md b/README.md index e3bf392..c5112ce 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ prerelease-only, and npm's semver excludes a prerelease from any range whose comparators carry a different `major.minor.patch` — so `^26.7.0-beta.0` never resolves past `26.7.0-beta.0` however many betas ship after it. CI scaffolds, installs, boots and introspects a real starter on every runtime, and a weekly -job fails when the pin falls behind the `beta` dist-tag. +job fails when the pin falls behind the `latest` dist-tag. ### Introspection @@ -194,11 +194,7 @@ which boots real workerd locally. Two differences the edge cannot hide. `@Html('file')`, `static` and multipart uploads need a filesystem workerd does not have, and shutdown teardown never -runs — an isolate is discarded, not closed. And core is installed from **JSR** -here rather than npm: the npm ESM build carries a `createRequire(import.meta.url)` -banner that throws at load on workerd. The scaffolded `.npmrc` says so and links -[the issue](https://github.com/Expressive-Tea/green-tea/issues/93); both it and -the alias can go once that is fixed. +runs — an isolate is discarded, not closed. ## Development diff --git a/src/template.rs b/src/template.rs index 0c706bb..ee5945f 100644 --- a/src/template.rs +++ b/src/template.rs @@ -13,8 +13,8 @@ use crate::runtime::Runtime; /// pin to the oldest matching prerelease that nothing can move off. When core /// reaches a stable major this becomes a range (`^27`) and the exactness stops /// mattering; until then the `core-freshness` CI job fails when this falls -/// behind the `beta` dist-tag, which is what keeps it current. -pub const CORE_VERSION: &str = "26.9.0-beta.2"; +/// behind the `latest` dist-tag, which is what keeps it current. +pub const CORE_VERSION: &str = "26.11.0-rc.1"; pub static SHARED: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/shared"); pub static RUNTIME_DENO: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/runtimes/deno"); diff --git a/template/runtimes/edge/.npmrc b/template/runtimes/edge/.npmrc deleted file mode 100644 index 4d2ef63..0000000 --- a/template/runtimes/edge/.npmrc +++ /dev/null @@ -1,9 +0,0 @@ -# core is installed from JSR here, and from npm on every other runtime. -# -# Not a preference: the npm ESM build carries tsup's -# `createRequire(import.meta.url)` banner at module scope, and `import.meta.url` -# is undefined on workerd — so `@green-tea/core/edge` throws at load, before any -# of its own code runs. JSR publishes transpiled source with no bundler and no -# banner, and boots clean. See Green-Tea/core#93; once that is fixed this file -# and the `npm:@jsr/...` alias in package.json can both go. -@jsr:registry=https://npm.jsr.io diff --git a/template/runtimes/edge/package.json b/template/runtimes/edge/package.json index 6dc08ad..5a73cb7 100644 --- a/template/runtimes/edge/package.json +++ b/template/runtimes/edge/package.json @@ -4,6 +4,6 @@ "private": true, "engines": { "node": ">=22" }, "scripts": { "dev": "wrangler dev", "deploy": "wrangler deploy" }, - "dependencies": { "@green-tea/core": "npm:@jsr/green-tea__core@{{core_version}}" }, + "dependencies": { "@green-tea/core": "{{core_version}}" }, "devDependencies": { "tsx": "^4", "typescript": "^5", "wrangler": "^4" } } diff --git a/tests/plugin.rs b/tests/plugin.rs index 62d6b0f..3a38dda 100644 --- a/tests/plugin.rs +++ b/tests/plugin.rs @@ -191,7 +191,7 @@ fn package_jsr_writes_the_tree_into_dir() { ); let deno = std::fs::read_to_string(pkg.join("deno.json")).unwrap(); assert!(deno.contains("\"name\": \"@acme/plugin-algo\""), "{deno}"); - assert!(deno.contains("npm:@green-tea/core@26.9.0-beta.2"), "{deno}"); + assert!(deno.contains("npm:@green-tea/core@26.11.0-rc.1"), "{deno}"); // Without it `deno test` needs a `deno install` first, and then fails to // type-check `node:assert` for want of @types/node. assert!(deno.contains("\"nodeModulesDir\": \"auto\""), "{deno}"); -- 2.54.1