sordo 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +78 -0
- package/package.json +1 -1
- package/src/adapters/edge.ts +38 -1
- package/src/adapters/types.ts +4 -0
- package/src/build/index.ts +7 -3
- package/src/build/server-entry.ts +1 -1
- package/src/cli/build.ts +11 -0
- package/src/config/schema.ts +8 -1
- package/src/index.ts +1 -0
- package/src/middleware/index.ts +29 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# sordo
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 5ebcc79: Edge: expose Worker bindings and `waitUntil` on `SordoContext` (`ctx.bindings`, typed via the augmentable `SordoBindings`), merge Worker string vars into `ctx.env`, and add `adapter.edge.assets: "external"` to emit client assets to `worker-assets/` instead of inlining them. `sordo build` warns when `worker.js` nears Cloudflare's size limit.
|
|
8
|
+
|
|
9
|
+
Document the manual Cloudflare Workers deploy recipe (`wrangler deploy` against `worker.js`) in the README, and note in the `sordo deploy` hint that `@sordo/deploy-cloudflare` is not yet published.
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- b266211: New `@sordo/deploy-cloudflare` deploy plugin: `sordo deploy --target edge` runs `wrangler deploy` against the edge build, generating a minimal Wrangler config when the project has none. The README now documents the plugin and the manual Wrangler path (`.sordo/worker.js`, `bun run build`, external assets served under `/_sordo/assets/`), and the `sordo deploy` hint no longer calls the plugin unpublished.
|
|
14
|
+
|
|
3
15
|
## 0.3.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -72,6 +72,84 @@ sordo deploy # build then dispatch to an installed deploy plugin
|
|
|
72
72
|
sordo secret # manage dev-time secrets in the OS keychain
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
+
On `--target edge`, client chunks and CSS are inlined into `dist/worker.js` by
|
|
76
|
+
default. Set `adapter: { edge: { assets: "external" } }` to write them (and
|
|
77
|
+
`public/`) to `dist/worker-assets/` instead — URL paths mirror the directory,
|
|
78
|
+
with a `_headers` file and `manifest.json` — and upload that directory as
|
|
79
|
+
Workers static assets. `sordo build` warns when `worker.js` nears the 3 MB gz
|
|
80
|
+
free-plan limit.
|
|
81
|
+
|
|
82
|
+
### Deploying to Cloudflare Workers
|
|
83
|
+
|
|
84
|
+
Install the deploy plugin and let `sordo deploy` build and ship the Worker:
|
|
85
|
+
|
|
86
|
+
```sh
|
|
87
|
+
bun add -d @sordo/deploy-cloudflare
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
// sordo.config.ts
|
|
92
|
+
import { defineConfig } from "sordo/config";
|
|
93
|
+
import { cloudflare } from "@sordo/deploy-cloudflare";
|
|
94
|
+
|
|
95
|
+
export default defineConfig({
|
|
96
|
+
target: "edge",
|
|
97
|
+
plugins: [cloudflare({ name: "my-site" })],
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
```sh
|
|
102
|
+
bunx wrangler login
|
|
103
|
+
bun run build # scaffolded apps generate CSS first, then build
|
|
104
|
+
sordo deploy --target edge
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
With no `wrangler.jsonc`/`wrangler.toml` in the project the plugin generates
|
|
108
|
+
`.sordo/wrangler.generated.jsonc`; add your own to control bindings, routes or
|
|
109
|
+
vars. See the plugin README for options (`wranglerEnv`, `dryRun`, `args`).
|
|
110
|
+
|
|
111
|
+
#### Manual path (no plugin)
|
|
112
|
+
|
|
113
|
+
Sordo emits no `wrangler.jsonc` — you own it. `sordo build --target edge` writes
|
|
114
|
+
`.sordo/worker.js` (default `outDir` is `./.sordo`, not `dist/`). Scaffolded
|
|
115
|
+
apps generate Tailwind CSS in their `build` script, so use `bun run build`
|
|
116
|
+
rather than calling `sordo build` directly.
|
|
117
|
+
|
|
118
|
+
```sh
|
|
119
|
+
bun run build
|
|
120
|
+
bunx wrangler deploy # reads wrangler.jsonc
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
```jsonc
|
|
124
|
+
// wrangler.jsonc
|
|
125
|
+
{
|
|
126
|
+
"name": "my-site",
|
|
127
|
+
"main": ".sordo/worker.js",
|
|
128
|
+
"compatibility_date": "2026-01-01",
|
|
129
|
+
"services": [{ "binding": "CMS", "service": "my-cms-worker" }]
|
|
130
|
+
// only with adapter.edge.assets: "external":
|
|
131
|
+
// "assets": { "directory": ".sordo/worker-assets" }
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Secrets: `bunx wrangler secret put NAME`. Handlers read them as `ctx.env.NAME`
|
|
136
|
+
and read bindings (service, KV, R2, D1) from `ctx.bindings`; `ctx.waitUntil(p)`
|
|
137
|
+
extends the request. Type bindings by augmenting the interface:
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
declare module "sordo" {
|
|
141
|
+
interface SordoBindings {
|
|
142
|
+
CMS: { fetch: typeof fetch };
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
By default `worker.js` is self-contained (client chunks and CSS are inlined), so
|
|
148
|
+
`main` is the only file Wrangler needs. With `adapter.edge.assets: "external"`
|
|
149
|
+
also set `assets.directory`; files from `app/public/` are then served at
|
|
150
|
+
`/_sordo/assets/<name>` (not at `/<name>`). Only `config.env.public` values are
|
|
151
|
+
inlined at build; secrets never are.
|
|
152
|
+
|
|
75
153
|
UI components (upstream shadcn/ui, local paths, or registries):
|
|
76
154
|
|
|
77
155
|
```sh
|
package/package.json
CHANGED
package/src/adapters/edge.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { collectClientChunks } from "../build/embedded.ts";
|
|
2
|
+
import { xxh64 } from "../build/hash.ts";
|
|
1
3
|
import type {
|
|
2
4
|
Adapter,
|
|
3
5
|
AdapterBuildInput,
|
|
@@ -29,10 +31,45 @@ export const edgeAdapter: Adapter = {
|
|
|
29
31
|
}
|
|
30
32
|
const bytes = await Bun.file(input.serverEntryOut).bytes();
|
|
31
33
|
await Bun.write(workerPath, bytes);
|
|
34
|
+
const extraFiles = [workerPath];
|
|
35
|
+
if (ctx.edgeAssets === "external") {
|
|
36
|
+
extraFiles.push(
|
|
37
|
+
await writeExternalAssets(input, `${ctx.outDir}/worker-assets`),
|
|
38
|
+
);
|
|
39
|
+
}
|
|
32
40
|
return {
|
|
33
41
|
serverEntry: workerPath,
|
|
34
42
|
assets: input.assets,
|
|
35
|
-
extraFiles
|
|
43
|
+
extraFiles,
|
|
36
44
|
};
|
|
37
45
|
},
|
|
38
46
|
};
|
|
47
|
+
|
|
48
|
+
// Lays files out so the directory root maps 1:1 to URL paths — point Workers
|
|
49
|
+
// static assets (`assets.directory`) at it. Client chunks are content-hashed by
|
|
50
|
+
// the client build; public/ files keep their names, the manifest records hashes.
|
|
51
|
+
async function writeExternalAssets(
|
|
52
|
+
input: AdapterBuildInput,
|
|
53
|
+
dir: string,
|
|
54
|
+
): Promise<string> {
|
|
55
|
+
await Bun.$`rm -rf ${dir}`.quiet();
|
|
56
|
+
const manifest: Record<string, { hash: string; bytes: number }> = {};
|
|
57
|
+
const files: { url: string; src: string }[] = [
|
|
58
|
+
...(input.clientOutDir
|
|
59
|
+
? await collectClientChunks(input.clientOutDir)
|
|
60
|
+
: []),
|
|
61
|
+
...input.assets.map((a) => ({ url: a.publicPath, src: a.src })),
|
|
62
|
+
];
|
|
63
|
+
for (const { url, src } of files) {
|
|
64
|
+
const bytes = await Bun.file(src).bytes();
|
|
65
|
+
await Bun.write(`${dir}${url}`, bytes);
|
|
66
|
+
manifest[url] = { hash: xxh64(bytes), bytes: bytes.byteLength };
|
|
67
|
+
}
|
|
68
|
+
// Workers static assets reads `_headers`; client URLs carry a content hash.
|
|
69
|
+
await Bun.write(
|
|
70
|
+
`${dir}/_headers`,
|
|
71
|
+
"/_sordo/client/*\n Cache-Control: public, max-age=31536000, immutable\n",
|
|
72
|
+
);
|
|
73
|
+
await Bun.write(`${dir}/manifest.json`, JSON.stringify(manifest, null, 2));
|
|
74
|
+
return dir;
|
|
75
|
+
}
|
package/src/adapters/types.ts
CHANGED
|
@@ -7,6 +7,8 @@ export interface AdapterContext {
|
|
|
7
7
|
mode: "development" | "production";
|
|
8
8
|
config?: SordoConfig;
|
|
9
9
|
compile?: boolean;
|
|
10
|
+
/** edge only: where client chunks + public assets are served from. */
|
|
11
|
+
edgeAssets?: "inline" | "external";
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export interface AdapterBuildInput {
|
|
@@ -17,6 +19,8 @@ export interface AdapterBuildInput {
|
|
|
17
19
|
/** Absolute path to the generated server-entry.ts SOURCE (for --compile). */
|
|
18
20
|
serverEntrySrc: string;
|
|
19
21
|
assets: BuiltAsset[];
|
|
22
|
+
/** Absolute path to the built client output dir (chunks, CSS). */
|
|
23
|
+
clientOutDir?: string;
|
|
20
24
|
}
|
|
21
25
|
|
|
22
26
|
export interface EmittedArtifacts {
|
package/src/build/index.ts
CHANGED
|
@@ -70,6 +70,9 @@ export interface BuildOptions {
|
|
|
70
70
|
hydrate?: HydrateDefault;
|
|
71
71
|
// Stylesheet entry (`config.css`). Relative paths resolve against cwd.
|
|
72
72
|
css?: CssOption;
|
|
73
|
+
// `adapter.edge.assets`: with "external" the edge build writes client
|
|
74
|
+
// outputs + public/ to <outDir>/worker-assets instead of inlining them.
|
|
75
|
+
edgeAssets?: "inline" | "external";
|
|
73
76
|
}
|
|
74
77
|
|
|
75
78
|
const PUBLIC_PREFIX = "/_sordo/client/";
|
|
@@ -219,7 +222,8 @@ export async function build(options: BuildOptions): Promise<BuildResult> {
|
|
|
219
222
|
// page chunks, the CSS sheet and its assets) from bytes inlined at build
|
|
220
223
|
// time; compile mode embeds client/ into the binary instead.
|
|
221
224
|
let inlineAssetsPath: string | undefined;
|
|
222
|
-
|
|
225
|
+
const externalAssets = target === "edge" && options.edgeAssets === "external";
|
|
226
|
+
if (!compile && !externalAssets) {
|
|
223
227
|
const clientFiles = (await collectClientChunks(clientOutDir)).map((c) => ({
|
|
224
228
|
url: c.url,
|
|
225
229
|
path: c.src,
|
|
@@ -319,8 +323,8 @@ export async function build(options: BuildOptions): Promise<BuildResult> {
|
|
|
319
323
|
const assets = await copyAssets(appDir, outDir, { write: !compile });
|
|
320
324
|
|
|
321
325
|
const emitted = await adapter.emit(
|
|
322
|
-
{ serverEntryOut, serverEntrySrc: serverEntryPath, assets },
|
|
323
|
-
{ appDir, outDir, mode, compile },
|
|
326
|
+
{ serverEntryOut, serverEntrySrc: serverEntryPath, assets, clientOutDir },
|
|
327
|
+
{ appDir, outDir, mode, compile, edgeAssets: options.edgeAssets },
|
|
324
328
|
);
|
|
325
329
|
|
|
326
330
|
const result: BuildResult = {
|
|
@@ -206,7 +206,7 @@ export default {
|
|
|
206
206
|
async fetch(req, env, execCtx) {${inline ? "\n const asset = matchInlineAsset(INLINE_ASSET_DATA, req)\n if (asset) return asset" : ""}
|
|
207
207
|
const hit = await dispatch(router.routes, req, { env, execCtx }, __compiled)
|
|
208
208
|
if (hit) return hit
|
|
209
|
-
return router.fallback(req)
|
|
209
|
+
return router.fallback(Object.assign(req, { env, execCtx }))
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
212
|
`.trim();
|
package/src/cli/build.ts
CHANGED
|
@@ -8,6 +8,9 @@ import { assertPluginCompat, type Plugin } from "../plugin/index.ts";
|
|
|
8
8
|
import type { CliContext } from "./index.ts";
|
|
9
9
|
import { EXIT, formatError, log } from "./log.ts";
|
|
10
10
|
|
|
11
|
+
// 80% of the 3 MB gzipped free-plan Worker script limit.
|
|
12
|
+
const WORKER_WARN_BYTES = 0.8 * 3 * 1024 * 1024;
|
|
13
|
+
|
|
11
14
|
const FRAMEWORK_VERSION = (pkg as { version: string }).version;
|
|
12
15
|
|
|
13
16
|
export interface BuildArgs {
|
|
@@ -66,6 +69,7 @@ export async function runBuild(
|
|
|
66
69
|
plugins,
|
|
67
70
|
hydrate: config.render.hydrate,
|
|
68
71
|
css: config.css,
|
|
72
|
+
edgeAssets: config.adapter.edge.assets,
|
|
69
73
|
});
|
|
70
74
|
} catch (err) {
|
|
71
75
|
failure = err as Error;
|
|
@@ -127,6 +131,13 @@ async function printSummary(
|
|
|
127
131
|
log.dim(
|
|
128
132
|
` serverEntry: ${result.serverEntry} (${formatBytes(serverSize)} gz)`,
|
|
129
133
|
);
|
|
134
|
+
if (ctx.target === "edge") {
|
|
135
|
+
if (serverSize >= WORKER_WARN_BYTES) {
|
|
136
|
+
log.warn(
|
|
137
|
+
`worker.js is ${formatBytes(serverSize)} gz — Cloudflare caps a Worker at 3 MB gz (free) / 10 MB gz (paid). Set adapter.edge.assets: "external" to move client assets out of the script.`,
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
130
141
|
if (result.assets.length > 0) {
|
|
131
142
|
log.dim(` assets:`);
|
|
132
143
|
for (const a of result.assets) {
|
package/src/config/schema.ts
CHANGED
|
@@ -92,8 +92,15 @@ export const SordoConfigSchema = z.object({
|
|
|
92
92
|
compile: z.boolean().default(false),
|
|
93
93
|
})
|
|
94
94
|
.default({ compile: false }),
|
|
95
|
+
edge: z
|
|
96
|
+
.object({
|
|
97
|
+
// "inline" bakes client chunks + CSS into worker.js; "external" writes
|
|
98
|
+
// them to <outDir>/worker-assets for Workers static assets.
|
|
99
|
+
assets: z.enum(["inline", "external"]).default("inline"),
|
|
100
|
+
})
|
|
101
|
+
.default({ assets: "inline" }),
|
|
95
102
|
})
|
|
96
|
-
.default({ bun: { compile: false } }),
|
|
103
|
+
.default({ bun: { compile: false }, edge: { assets: "inline" } }),
|
|
97
104
|
// Generated agent-manifest target(s). Defaults to "CLAUDE.md" so the
|
|
98
105
|
// manifest lands in the same file Claude already reads. The CLI splices
|
|
99
106
|
// the manifest between fenced markers so any human-written content
|
package/src/index.ts
CHANGED
package/src/middleware/index.ts
CHANGED
|
@@ -9,13 +9,23 @@ import {
|
|
|
9
9
|
redirectResponse,
|
|
10
10
|
} from "./response-helpers.ts";
|
|
11
11
|
|
|
12
|
+
// Apps augment this to type their Worker bindings on the edge target:
|
|
13
|
+
// declare module "sordo" { interface SordoBindings { CMS: Fetcher } }
|
|
14
|
+
// biome-ignore lint/suspicious/noEmptyInterface: declaration-merging seam
|
|
15
|
+
export interface SordoBindings {}
|
|
16
|
+
|
|
12
17
|
export interface SordoContext {
|
|
13
18
|
params: Record<string, string>;
|
|
14
19
|
// Framework-generated correlation id, populated before user middleware runs.
|
|
15
20
|
// User middleware/plugins read it; they MUST NOT re-generate it.
|
|
16
21
|
requestId: string;
|
|
17
22
|
cookies: unknown;
|
|
23
|
+
// Process env merged with the string vars/secrets from the Worker env on edge.
|
|
18
24
|
env: Record<string, string | undefined>;
|
|
25
|
+
// Raw Worker env (KV/R2/D1/service bindings) on edge; empty object on bun.
|
|
26
|
+
bindings: SordoBindings;
|
|
27
|
+
// Extends the request lifetime on edge; awaits nothing and swallows nothing on bun.
|
|
28
|
+
waitUntil(p: Promise<unknown>): void;
|
|
19
29
|
aiContext?: AiContext;
|
|
20
30
|
csrf?: { token(): string; verify(t: string | null): boolean };
|
|
21
31
|
vars: Record<string, unknown>;
|
|
@@ -62,7 +72,17 @@ export function makeContext(
|
|
|
62
72
|
): SordoContext {
|
|
63
73
|
let urlCache: URL | null = null;
|
|
64
74
|
let cookiesCache: unknown;
|
|
65
|
-
const env =
|
|
75
|
+
const { env: workerEnv, execCtx } = req as unknown as {
|
|
76
|
+
env?: unknown;
|
|
77
|
+
execCtx?: { waitUntil?: (p: Promise<unknown>) => void };
|
|
78
|
+
};
|
|
79
|
+
const bindings = (
|
|
80
|
+
workerEnv && typeof workerEnv === "object" ? workerEnv : {}
|
|
81
|
+
) as SordoBindings;
|
|
82
|
+
const env = { ...runtimeEnv() };
|
|
83
|
+
for (const [k, v] of Object.entries(bindings)) {
|
|
84
|
+
if (typeof v === "string") env[k] = v;
|
|
85
|
+
}
|
|
66
86
|
|
|
67
87
|
const ctx: SordoContext = {
|
|
68
88
|
params,
|
|
@@ -84,6 +104,14 @@ export function makeContext(
|
|
|
84
104
|
return cookiesCache;
|
|
85
105
|
},
|
|
86
106
|
env,
|
|
107
|
+
bindings,
|
|
108
|
+
waitUntil(p) {
|
|
109
|
+
if (typeof execCtx?.waitUntil === "function") {
|
|
110
|
+
execCtx.waitUntil(p);
|
|
111
|
+
} else {
|
|
112
|
+
p.catch(() => {});
|
|
113
|
+
}
|
|
114
|
+
},
|
|
87
115
|
vars: {},
|
|
88
116
|
get url() {
|
|
89
117
|
if (!urlCache) urlCache = new URL(req.url);
|