feat(edge): a Cloudflare Workers target, and a doctor for the quiet failures
`matcha new --runtime edge` scaffolds a Worker: `nodejs_compat`, `edgeHandler` as the default export, and wrangler's `[assets]` serving `./public` so `/` is the same page as on the other three runtimes and `/zen` the same stream from the worker. `matcha run` is `wrangler dev`, which is real workerd, so edge stops being a deploy-only target. Runtime detection puts `wrangler.toml` ahead of `package.json`, since an edge project has both, and the graph resolves under node — `src/app.ts` imports core, never `edgeHandler`. Getting there turned up a core defect, filed as Green-Tea/core#93: the npm ESM build cannot load on workerd at all. tsup's `createRequire(import.meta.url)` banner runs at module scope and `import.meta.url` is undefined there, so `@green-tea/core/edge` throws before any of its own code runs — while `src/node-require.ts` is written precisely to make that lazy and survivable. core's own edge test cannot see it: it bundles `src/`, so `dist/` is the one artifact users install and the one nothing exercises. The scaffold installs from JSR, which publishes source with no banner and boots clean; the `.npmrc` says why and links the issue, and both it and the alias can go once it is fixed. `matcha doctor` reports the misconfigurations that fail confusingly rather than loudly: `experimentalDecorators` missing, which turns every `@Route` into a syntax error blaming the decorator; a core pin that cannot resolve or that does not match what is installed; an entry that does not export `app`; and `@Ws` or multipart routes whose lazy-loaded peer dependency is absent, which compile and then fail at the first request. Every check is a file read — no runtime spawned, nothing fetched — so it works before the project installs. It reads the installed version from the package's own manifest rather than core's exported `VERSION`, which is stale in the published artifact (Green-Tea/core#92) and would give the wrong answer to the one check that exists to catch a wrong version. The starter smoke and the CI matrix gained edge as a fourth runtime. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012x1UPw987zNSxjH2DxHy2C
This commit is contained in:
18 files changed
+606
-13
No files matched your search
@@ -0,0 +1,9 @@
|
||||
# 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
|
||||
@@ -0,0 +1 @@
|
||||
runtime = "edge"
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "{{project_name}}",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": { "dev": "wrangler dev", "deploy": "wrangler deploy" },
|
||||
"dependencies": { "@green-tea/core": "npm:@jsr/green-tea__core@{{core_version}}" },
|
||||
"devDependencies": { "tsx": "^4", "typescript": "^5", "wrangler": "^4" }
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Route, Sse } from '@green-tea/core';
|
||||
|
||||
const ZEN = [
|
||||
'Simplicity is the ultimate sophistication.',
|
||||
'The obstacle is the path.',
|
||||
'When you let go, you have two hands to work.',
|
||||
'Make it work, make it right, make it fast.',
|
||||
'Less, but better.',
|
||||
];
|
||||
|
||||
@Route('/')
|
||||
export class HomeController {
|
||||
// No @Html('public/index.html') here: workerd has no filesystem to read it
|
||||
// from. wrangler.toml's [assets] serves ./public instead, and the worker is
|
||||
// reached for everything the assets do not answer.
|
||||
@Sse('/zen')
|
||||
zen() {
|
||||
return (async function* () {
|
||||
for (let i = 0; ; i++) {
|
||||
yield { zen: ZEN[i % ZEN.length] };
|
||||
await new Promise((r) => setTimeout(r, 30_000));
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { edgeHandler } from '@green-tea/core/edge';
|
||||
|
||||
import { app } from './app';
|
||||
|
||||
export default { fetch: edgeHandler(app) };
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "esnext",
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
name = "{{project_name}}"
|
||||
main = "src/main.ts"
|
||||
compatibility_date = "2024-11-01"
|
||||
# green-tea reaches node builtins on every runtime that has them; workerd only
|
||||
# provides them behind this flag.
|
||||
compatibility_flags = ["nodejs_compat"]
|
||||
|
||||
# The edge has no filesystem, so @Html('public/index.html') cannot serve the
|
||||
# starter page the way it does on node, deno and bun. Wrangler serves ./public
|
||||
# itself and hands the worker everything it does not match — so '/' is the same
|
||||
# page and '/zen' is the same stream.
|
||||
[assets]
|
||||
directory = "./public"
|
||||
Reference in new issue
Block a user