@elumixor/devkit 0.12.9 → 0.12.10

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/README.md CHANGED
@@ -258,6 +258,23 @@ immutable — its own S3 prefix and Lambda version — so a rollback is two writ
258
258
  }
259
259
  ```
260
260
 
261
+ `headers` sets response headers by path, as `vercel.json` does, and takes a `vercel.json` block
262
+ pasted as is (`[{ "key", "value" }]`) or a plain map. `source` is Vercel's syntax: `:name` is one
263
+ segment, `:name*` any depth, `( )` a raw regex. The router adds them on the way out, cache hits
264
+ included, so they change with the deploy and roll back with it. They apply to 2xx and 3xx
265
+ responses only (CloudFront runs no function on an origin error), and compile to 1 KB at most.
266
+
267
+ ```jsonc
268
+ "headers": [
269
+ { "source": "/bar.js", "headers": { "Access-Control-Allow-Origin": "*", "Cache-Control": "public, max-age=300" } },
270
+ { "source": "/icons/(.*)", "headers": { "Access-Control-Allow-Origin": "*" } }
271
+ ]
272
+ ```
273
+
274
+ `"apiPrefixes": []` makes a static site: no Nitro build, no function, role, schedules or health
275
+ check, only the files. A frontend with no `build` script is served as it is, so a folder of
276
+ hand-written files is `"frontendDir": ".", "webBuildDir": "public"`.
277
+
261
278
  A `domains` entry starting with `*.` (say `*.desk.atmagaming.com`) only adds a wildcard alias to the
262
279
  distribution: every hostname under it then rides one certificate name, which keeps the shared
263
280
  certificate under ACM's per-certificate name quota. List it next to the hosts it covers.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elumixor/devkit",
3
- "version": "0.12.9",
3
+ "version": "0.12.10",
4
4
  "description": "Dev orchestrator for bun monorepos — free ports, run apps side-by-side, open the browser when ready",
5
5
  "author": "elumixor",
6
6
  "license": "ISC",
@@ -18,6 +18,8 @@ export interface WebBuildOptions {
18
18
  include?: Record<string, string>;
19
19
  /** Who serves the result: Vercel's build output API, or Devkit Cloud's S3 + Lambda (default `vercel`). */
20
20
  host?: "vercel" | "aws";
21
+ /** Static files only, no Nitro API (`aws` host only; the target sets it when `apiPrefixes` is empty). */
22
+ static?: boolean;
21
23
  }
22
24
 
23
25
  /** Where the `aws` host's build lands, relative to the repo root. */
@@ -34,7 +36,12 @@ export async function buildWeb(options: WebBuildOptions) {
34
36
  const backend = resolve(root, options.backendDir ?? "apps/backend");
35
37
  const webBuild = join(frontend, options.webBuildDir ?? "build");
36
38
 
37
- await $`bun run build`.cwd(frontend);
39
+ // A site of hand-written files (a `public/` folder) has nothing to build.
40
+ const scripts = existsSync(join(frontend, "package.json"))
41
+ ? ((await Bun.file(join(frontend, "package.json")).json()) as { scripts?: Record<string, string> }).scripts
42
+ : undefined;
43
+ if (scripts?.build) await $`bun run build`.cwd(frontend);
44
+ else console.log(`No build script in ${frontend} — serving ${webBuild} as is`);
38
45
  if (!existsSync(webBuild)) throw new Error(`No web build at ${webBuild} — did the web build land?`);
39
46
 
40
47
  if (options.host === "aws") return buildForAws(root, backend, webBuild, options);
@@ -74,6 +81,10 @@ async function buildForAws(root: string, backend: string, webBuild: string, opti
74
81
  await mkdir(output, { recursive: true });
75
82
  await cp(webBuild, staticDir, { recursive: true });
76
83
  await copyIncluded(root, staticDir, options.include);
84
+ if (options.static) {
85
+ console.log(`Static files in ${AWS_OUTPUT}/static, no function`);
86
+ return;
87
+ }
77
88
 
78
89
  await rm(nitroOutput, { recursive: true, force: true });
79
90
  await $`bun ${join(import.meta.dir, "../cloud/nitro-build.ts")} ${backend}`;
@@ -15,6 +15,7 @@ import {
15
15
  ensureCachePolicy,
16
16
  ensureKeyValueStore,
17
17
  ensureOriginAccessControl,
18
+ ensureRouterAssociations,
18
19
  ensureRouterFunction,
19
20
  } from "./reconcile/platform.ts";
20
21
  import { buckets } from "./reconcile/s3.ts";
@@ -64,6 +65,7 @@ export async function bootstrap(options: BootstrapOptions = {}) {
64
65
  routerArn,
65
66
  certificateArn,
66
67
  });
68
+ const associations = existing ? await ensureRouterAssociations(distribution.id, routerArn) : { changed: false };
67
69
  await Promise.all([
68
70
  allowDistribution(sites, distribution.arn, "*"),
69
71
  allowDistribution(blobs, distribution.arn, "*/*/public/*"),
@@ -88,7 +90,7 @@ export async function bootstrap(options: BootstrapOptions = {}) {
88
90
  await ensureBudgets(email);
89
91
  await ensureAnomalyAlerts(email);
90
92
 
91
- if (!existing) {
93
+ if (!existing || associations.changed) {
92
94
  log("\nwaiting for the distribution to deploy (10–15 minutes the first time)");
93
95
  await waitDeployed(distribution.id);
94
96
  }
@@ -1,10 +1,10 @@
1
1
  import { CloudError, log } from "../aws.ts";
2
2
  import { deleteStageEnv } from "../env.ts";
3
- import { stage as names } from "../names.ts";
3
+ import { headersKey, stage as names } from "../names.ts";
4
4
  import { deleteRecord } from "../reconcile/cloudflare.ts";
5
5
  import { needsDns, reconcileAliases } from "../reconcile/edge.ts";
6
6
  import { deleteRole } from "../reconcile/iam.ts";
7
- import { updateKeys } from "../reconcile/kvs.ts";
7
+ import { keysWithPrefix, updateKeys } from "../reconcile/kvs.ts";
8
8
  import { deleteFunction } from "../reconcile/lambda.ts";
9
9
  import { deleteLogGroup } from "../reconcile/logs.ts";
10
10
  import { buckets, deletePrefix } from "../reconcile/s3.ts";
@@ -18,7 +18,7 @@ export async function destroy(project: string | undefined, stageFlag: string | u
18
18
  const { target, recorded } = await targetByName(project, stageFlag);
19
19
  log(`Destroying ${project} ${target.stage}`);
20
20
 
21
- await updateKeys({}, recorded.hostnames);
21
+ await updateKeys({}, [...recorded.hostnames, ...(await keysWithPrefix(headersKey(project, `${target.stage}/`)))]);
22
22
  log(" routes removed");
23
23
  const custom = recorded.hostnames.filter(needsDns);
24
24
  if (custom.length) {
@@ -1,5 +1,6 @@
1
1
  import { CloudError, log } from "../aws.ts";
2
- import { getRoute, putRoutes } from "../reconcile/kvs.ts";
2
+ import { headersKey } from "../names.ts";
3
+ import { getRoute, hasKey, putRoutes } from "../reconcile/kvs.ts";
3
4
  import { pointLiveAlias } from "../reconcile/lambda.ts";
4
5
  import { listDeploys, readCurrent, readDeploy, writeCurrent, writeDeploy } from "../records.ts";
5
6
  import { targetByName } from "./context.ts";
@@ -28,10 +29,13 @@ export async function rollback(project: string, stageFlag: string | undefined, t
28
29
  const started = Date.now();
29
30
  log(`Rolling ${project} ${target.stage} back from ${current.deployId} to ${to.id} (${to.message})`);
30
31
 
31
- await pointLiveAlias(target.functionName, to.version);
32
32
  const route = await getRoute(recorded.hostnames[0] as string);
33
33
  if (!route) throw new CloudError(`No route record for ${recorded.hostnames[0]} — deploy once before rolling back`);
34
- const moved = { ...route, d: `${target.stage}/${to.id}` };
34
+ // A static site has no function, so its route names none.
35
+ if (route.fn) await pointLiveAlias(target.functionName, to.version);
36
+ const d = `${target.stage}/${to.id}`;
37
+ const { h: _, ...rest } = route;
38
+ const moved = (await hasKey(headersKey(project, d))) ? { ...rest, d, h: 1 as const } : { ...rest, d };
35
39
  await putRoutes(Object.fromEntries(recorded.hostnames.map((host) => [host, moved])));
36
40
 
37
41
  const now = new Date().toISOString();
@@ -1,6 +1,7 @@
1
1
  // The Devkit Cloud router: one CloudFront Function (JavaScript runtime 2.0) on the viewer-request
2
- // event of the shared distribution. It reads the hostname's route record from the KeyValueStore
3
- // and sends the request to that project's function, its deploy's static files, or its public blobs.
2
+ // and viewer-response events of the shared distribution. On a request it reads the hostname's route
3
+ // record from the KeyValueStore and sends the request to that project's function, its deploy's
4
+ // static files, or its public blobs. On a response it adds the deploy's configured headers, if any.
4
5
  //
5
6
  // Deployed by `devkit cloud bootstrap`; the header and key names are the ones in src/cloud/names.ts.
6
7
  import cf from "cloudfront";
@@ -13,6 +14,7 @@ var HOST_HEADER = "x-dk-host";
13
14
  var PROJECT_HEADER = "x-dk-project";
14
15
  var BLOB_PREFIX = "/_blob/";
15
16
  var SSR_ASSETS_PREFIX = "/_app/";
17
+ var HEADERS_PREFIX = "_h/";
16
18
  var NO_SITE = {
17
19
  statusCode: 404,
18
20
  statusDescription: "Not Found",
@@ -21,6 +23,7 @@ var NO_SITE = {
21
23
  };
22
24
 
23
25
  async function handler(event) {
26
+ if (event.context.eventType === "viewer-response") return withHeaders(event);
24
27
  var request = event.request;
25
28
  var host = request.headers.host ? request.headers.host.value.toLowerCase() : "";
26
29
 
@@ -84,3 +87,25 @@ function staticPath(uri, mode) {
84
87
  if (mode === "pages") return (uri.slice(-1) === "/" ? uri : uri + "/") + "index.html";
85
88
  return "/index.html";
86
89
  }
90
+
91
+ // The request here is the viewer's own, before the rewrite, so rules match the path the viewer asked for.
92
+ async function withHeaders(event) {
93
+ var response = event.response;
94
+ var request = event.request;
95
+ var host = request.headers.host ? request.headers.host.value.toLowerCase() : "";
96
+ try {
97
+ var record = await routes.get(host);
98
+ var route = JSON.parse(record);
99
+ if (!route.h) return response;
100
+ var raw = await routes.get(HEADERS_PREFIX + route.p + "/" + route.d);
101
+ var rules = JSON.parse(raw);
102
+ for (var i = 0; i < rules.length; i++) {
103
+ if (!new RegExp(rules[i][0]).test(request.uri)) continue;
104
+ var names = Object.keys(rules[i][1]);
105
+ for (var j = 0; j < names.length; j++) response.headers[names[j]] = { value: rules[i][1][names[j]] };
106
+ }
107
+ } catch (e) {
108
+ // No route or no headers: the response goes out as the origin made it.
109
+ }
110
+ return response;
111
+ }
@@ -0,0 +1,64 @@
1
+ import { CloudError } from "./aws.ts";
2
+
3
+ /**
4
+ * Response headers for the paths matching `source`, as in `vercel.json`: `headers` is a map of
5
+ * name to value, or Vercel's `[{ key, value }]` list so a `vercel.json` block can be pasted as is.
6
+ */
7
+ export interface HeaderRule {
8
+ /** `/bar.js`, `/icons/(.*)`, `/:path*` — `:name` is one segment, `:name*` any depth, `( )` a raw regex. */
9
+ source: string;
10
+ headers: Record<string, string> | { key: string; value: string }[];
11
+ }
12
+
13
+ /** A KVS value may be 1 KB at most, and the compiled rules are one value. */
14
+ const MAX_BYTES = 1024;
15
+
16
+ /**
17
+ * Compiles the rules into what the edge reads on every response: `[[regex, { name: value }], ...]`
18
+ * with lowercase names, or nothing when there are no rules.
19
+ */
20
+ export function compileHeaders(rules: readonly HeaderRule[] = []): string | undefined {
21
+ if (!rules.length) return undefined;
22
+ const compiled = rules.map((rule) => {
23
+ const pairs = Array.isArray(rule.headers)
24
+ ? rule.headers.map(({ key, value }) => [key, value] as const)
25
+ : Object.entries(rule.headers);
26
+ const headers = Object.fromEntries(pairs.map(([name, value]) => [name.toLowerCase(), String(value)]));
27
+ return [sourceToRegex(rule.source), headers];
28
+ });
29
+ const value = JSON.stringify(compiled);
30
+ if (new TextEncoder().encode(value).byteLength > MAX_BYTES)
31
+ throw new CloudError(`"headers" compile to more than ${MAX_BYTES} bytes — merge rules or shorten values`);
32
+ return value;
33
+ }
34
+
35
+ /** Vercel's path syntax as an anchored regex source. */
36
+ export function sourceToRegex(source: string) {
37
+ if (!source.startsWith("/")) throw new CloudError(`Header source "${source}" must start with "/"`);
38
+ let out = "";
39
+ for (let i = 0; i < source.length; ) {
40
+ const char = source[i] as string;
41
+ if (char === "(") {
42
+ let depth = 0;
43
+ let j = i;
44
+ for (; j < source.length; j++) {
45
+ if (source[j] === "(") depth++;
46
+ else if (source[j] === ")" && --depth === 0) break;
47
+ }
48
+ if (depth) throw new CloudError(`Header source "${source}" has an unclosed "("`);
49
+ out += source.slice(i, j + 1);
50
+ i = j + 1;
51
+ continue;
52
+ }
53
+ const param = char === ":" ? /^:[A-Za-z_][A-Za-z0-9_]*([*+?])?/.exec(source.slice(i)) : null;
54
+ if (param) {
55
+ const modifier = param[1];
56
+ out += modifier === "*" ? ".*" : modifier === "+" ? ".+" : modifier === "?" ? "[^/]*" : "[^/]+";
57
+ i += param[0].length;
58
+ continue;
59
+ }
60
+ out += /[.*+?^${}|[\]\\]/.test(char) ? `\\${char}` : char;
61
+ i++;
62
+ }
63
+ return `^${out}$`;
64
+ }
@@ -51,6 +51,10 @@ export const ORIGINS = { sites: "sites", blobs: "blobs" } as const;
51
51
  /** The KVS key whose value is the edge secret, and the header that carries it to the function. */
52
52
  export const EDGE_SECRET_KEY = "_edge";
53
53
  export const EDGE_HEADER = "x-dk-edge";
54
+ /** KVS keys starting with this hold no route: the edge secret, a deploy's response headers. */
55
+ export const RESERVED_KEY_PREFIX = "_";
56
+ /** The KVS key holding one deploy's compiled response headers; `deployPath` is the route record's `d`. */
57
+ export const headersKey = (project: string, deployPath: string) => `_h/${project}/${deployPath}`;
54
58
  /** The viewer's hostname, carried to the function because the origin request drops `Host`. */
55
59
  export const HOST_HEADER = "x-dk-host";
56
60
  /** Set by the router per request so two projects' identical API paths never share a cache entry. */
@@ -1,6 +1,7 @@
1
1
  import { $ } from "bun";
2
2
  import type { WebBuildOptions } from "../build-targets/web.ts";
3
3
  import { CloudError } from "./aws.ts";
4
+ import type { HeaderRule } from "./headers.ts";
4
5
  import { stage as names, PRODUCTION, STAGE_BRANCHES } from "./names.ts";
5
6
 
6
7
  /** One scheduled call into the API, in the shape Vercel's `vercel.json` used. */
@@ -21,7 +22,7 @@ export interface Permission {
21
22
  }
22
23
 
23
24
  /** The `aws` deploy target's options, in the `devkit.deploy.<name>.options` block. */
24
- export interface AwsTargetOptions extends Omit<WebBuildOptions, "host"> {
25
+ export interface AwsTargetOptions extends Omit<WebBuildOptions, "host" | "static"> {
25
26
  /** Project name: resource names, bucket prefixes and the default hostname (default: repo name). */
26
27
  project?: string;
27
28
  /** How the edge serves paths that aren't the API (default `spa`). */
@@ -47,6 +48,8 @@ export interface AwsTargetOptions extends Omit<WebBuildOptions, "host"> {
47
48
  terraform?: TerraformConfig;
48
49
  /** Extra IAM statements for the function — an app that reads other AWS resources, like the dashboard. */
49
50
  permissions?: Permission[];
51
+ /** Response headers by path, as in `vercel.json` — CORS on a script other sites load, say. */
52
+ headers?: HeaderRule[];
50
53
  }
51
54
 
52
55
  /**
@@ -125,6 +128,13 @@ export class Target {
125
128
  get permissions(): readonly Permission[] {
126
129
  return this.options.permissions ?? [];
127
130
  }
131
+ get headers(): readonly HeaderRule[] {
132
+ return this.options.headers ?? [];
133
+ }
134
+ /** No API paths and no SSR: only static files, so no function, role, schedules or health check. */
135
+ get isStatic() {
136
+ return this.apiPrefixes.length === 0 && this.routing !== "ssr";
137
+ }
128
138
  get isProduction() {
129
139
  return this.stage === PRODUCTION;
130
140
  }
@@ -77,17 +77,33 @@ function zoneFor(hostname: string) {
77
77
  */
78
78
  export async function ensureRecord(name: string, type: "CNAME" | "TXT", content: string) {
79
79
  const zone = await zoneFor(name);
80
- const existing = await findRecord(zone, name, type);
80
+ // A CNAME can't sit beside an A or AAAA record, so it takes one over in place (a host moving from
81
+ // another provider's IP): the same record changes type, with no moment the name doesn't resolve.
82
+ const existing =
83
+ (await findRecord(zone, name, type)) ?? (type === "CNAME" ? await findAddress(zone, name) : undefined);
81
84
  const body = { type, name, content, proxied: false, ttl: 300, comment: "devkit cloud" };
82
85
  if (!existing) {
83
86
  await call("POST", `/zones/${zone}/dns_records`, body);
84
87
  return "created";
85
88
  }
86
- if (matches(existing, content)) return "unchanged";
89
+ if (existing.type === type && matches(existing, content)) return "unchanged";
87
90
  await call("PUT", `/zones/${zone}/dns_records/${existing.id}`, body);
91
+ if (existing.type !== type) {
92
+ for (const other of await call<CloudflareRecord[]>(
93
+ "GET",
94
+ `/zones/${zone}/dns_records?name=${encodeURIComponent(name)}`,
95
+ ))
96
+ if ((other.type === "A" || other.type === "AAAA") && other.id !== existing.id)
97
+ await call("DELETE", `/zones/${zone}/dns_records/${other.id}`);
98
+ return `replaced ${existing.type}`;
99
+ }
88
100
  return "updated";
89
101
  }
90
102
 
103
+ async function findAddress(zone: string, name: string) {
104
+ return (await findRecord(zone, name, "A")) ?? (await findRecord(zone, name, "AAAA"));
105
+ }
106
+
91
107
  async function findRecord(zone: string, name: string, type: string) {
92
108
  const found = await call<CloudflareRecord[]>(
93
109
  "GET",
@@ -7,7 +7,7 @@ import {
7
7
  UpdateKeysCommand,
8
8
  } from "@aws-sdk/client-cloudfront-keyvaluestore";
9
9
  import { CloudError, cloudfront, kvs, orUndefined } from "../aws.ts";
10
- import { EDGE_SECRET_KEY, platform } from "../names.ts";
10
+ import { EDGE_SECRET_KEY, platform, RESERVED_KEY_PREFIX } from "../names.ts";
11
11
  import type { Routing } from "../project.ts";
12
12
 
13
13
  /**
@@ -22,6 +22,8 @@ export interface RouteRecord {
22
22
  fn: string;
23
23
  api: readonly string[];
24
24
  mode: Routing;
25
+ /** Set when the deploy has response headers, kept under `headersKey(p, d)`. */
26
+ h?: 1;
25
27
  }
26
28
 
27
29
  let storeArn: Promise<string> | undefined;
@@ -78,6 +80,27 @@ export async function deleteRoute(host: string) {
78
80
  );
79
81
  }
80
82
 
83
+ export async function hasKey(key: string) {
84
+ const result = await orUndefined(
85
+ kvs().send(new GetKeyCommand({ KvsARN: await routeStoreArn(), Key: key })),
86
+ "ResourceNotFoundException",
87
+ );
88
+ return result?.Value !== undefined;
89
+ }
90
+
91
+ /** Every key starting with `prefix` — the store has no prefix query, so this lists them all. */
92
+ export async function keysWithPrefix(prefix: string) {
93
+ const arn = await routeStoreArn();
94
+ const keys: string[] = [];
95
+ let NextToken: string | undefined;
96
+ do {
97
+ const page = await kvs().send(new ListKeysCommand({ KvsARN: arn, NextToken }));
98
+ for (const item of page.Items ?? []) if (item.Key?.startsWith(prefix)) keys.push(item.Key);
99
+ NextToken = page.NextToken;
100
+ } while (NextToken);
101
+ return keys;
102
+ }
103
+
81
104
  /** Every hostname → record pair in the store, for `status` across projects. */
82
105
  export async function listRoutes(): Promise<Record<string, RouteRecord>> {
83
106
  const arn = await routeStoreArn();
@@ -86,7 +109,8 @@ export async function listRoutes(): Promise<Record<string, RouteRecord>> {
86
109
  do {
87
110
  const page = await kvs().send(new ListKeysCommand({ KvsARN: arn, NextToken }));
88
111
  for (const item of page.Items ?? []) {
89
- if (item.Key && item.Value && item.Key !== EDGE_SECRET_KEY) routes[item.Key] = JSON.parse(item.Value);
112
+ if (item.Key && item.Value && !item.Key.startsWith(RESERVED_KEY_PREFIX))
113
+ routes[item.Key] = JSON.parse(item.Value);
90
114
  }
91
115
  NextToken = page.NextToken;
92
116
  } while (NextToken);
@@ -7,9 +7,11 @@ import {
7
7
  DescribeFunctionCommand,
8
8
  DescribeKeyValueStoreCommand,
9
9
  type DistributionConfig,
10
+ GetDistributionConfigCommand,
10
11
  ListCachePoliciesCommand,
11
12
  ListOriginAccessControlsCommand,
12
13
  PublishFunctionCommand,
14
+ UpdateDistributionCommand,
13
15
  UpdateFunctionCommand,
14
16
  } from "@aws-sdk/client-cloudfront";
15
17
  import {
@@ -255,7 +257,7 @@ export function distributionConfig(inputs: DistributionInputs): DistributionConf
255
257
  Compress: true,
256
258
  CachePolicyId: inputs.cachePolicyId,
257
259
  OriginRequestPolicyId: platform.originRequestPolicyId,
258
- FunctionAssociations: { Quantity: 1, Items: [{ EventType: "viewer-request", FunctionARN: inputs.routerArn }] },
260
+ FunctionAssociations: routerAssociations(inputs.routerArn),
259
261
  },
260
262
  DefaultRootObject: "",
261
263
  Restrictions: { GeoRestriction: { RestrictionType: "none", Quantity: 0 } },
@@ -279,3 +281,42 @@ export async function createDistribution(inputs: DistributionInputs): Promise<Di
279
281
  aliases: distribution.DistributionConfig?.Aliases?.Items ?? [],
280
282
  };
281
283
  }
284
+
285
+ /** The router runs on both viewer events: routing the request, then adding the deploy's response headers. */
286
+ function routerAssociations(routerArn: string) {
287
+ return {
288
+ Quantity: 2,
289
+ Items: [
290
+ { EventType: "viewer-request" as const, FunctionARN: routerArn },
291
+ { EventType: "viewer-response" as const, FunctionARN: routerArn },
292
+ ],
293
+ };
294
+ }
295
+
296
+ /** Brings an existing distribution's router associations up to date — one made before response headers existed. */
297
+ export async function ensureRouterAssociations(distributionId: string, routerArn: string) {
298
+ const { DistributionConfig: config, ETag } = await cloudfront().send(
299
+ new GetDistributionConfigCommand({ Id: distributionId }),
300
+ );
301
+ if (!config?.DefaultCacheBehavior || !ETag) throw new CloudError("CloudFront returned no distribution config");
302
+ const wanted = routerAssociations(routerArn);
303
+ const current = config.DefaultCacheBehavior.FunctionAssociations?.Items ?? [];
304
+ const same =
305
+ current.length === wanted.Items.length &&
306
+ wanted.Items.every((item) =>
307
+ current.some((have) => have.EventType === item.EventType && have.FunctionARN === item.FunctionARN),
308
+ );
309
+ if (same) return { changed: false };
310
+ await cloudfront().send(
311
+ new UpdateDistributionCommand({
312
+ Id: distributionId,
313
+ IfMatch: ETag,
314
+ DistributionConfig: {
315
+ ...config,
316
+ DefaultCacheBehavior: { ...config.DefaultCacheBehavior, FunctionAssociations: wanted },
317
+ },
318
+ }),
319
+ );
320
+ log(" router on viewer-request and viewer-response");
321
+ return { changed: true };
322
+ }
@@ -7,11 +7,12 @@ import { AWS_OUTPUT } from "../build-targets/web.ts";
7
7
  import { CloudError, log, s3 } from "./aws.ts";
8
8
  import { type Env, ensureSecret, readParameter, readStageEnv } from "./env.ts";
9
9
  import { startGithubDeployment } from "./github.ts";
10
- import { EDGE_HEADER, HOST_HEADER, stage as names, platform } from "./names.ts";
10
+ import { compileHeaders } from "./headers.ts";
11
+ import { EDGE_HEADER, HOST_HEADER, headersKey, stage as names, platform } from "./names.ts";
11
12
  import type { Target } from "./project.ts";
12
13
  import { pointDns, reconcileAliases } from "./reconcile/edge.ts";
13
14
  import { ensureFunctionRole, ensureSchedulerRole } from "./reconcile/iam.ts";
14
- import { putRoutes, type RouteRecord } from "./reconcile/kvs.ts";
15
+ import { keysWithPrefix, type RouteRecord, updateKeys } from "./reconcile/kvs.ts";
15
16
  import {
16
17
  deleteVersion,
17
18
  ensureFunctionUrl,
@@ -63,7 +64,10 @@ export async function release(target: Target, options: ReleaseOptions) {
63
64
  const deployId = `${timestamp()}-${git.sha.slice(0, 7)}`;
64
65
  const staticDir = join(options.root, AWS_OUTPUT, "static");
65
66
  const serverDir = join(options.root, AWS_OUTPUT, "server");
66
- if (!existsSync(serverDir)) throw new CloudError(`No build at ${serverDir} — the web build runs first`);
67
+ if (!target.isStatic && !existsSync(serverDir))
68
+ throw new CloudError(`No build at ${serverDir} — the web build runs first`);
69
+ if (target.isStatic && target.crons.length) throw new CloudError('"crons" need an API — "apiPrefixes" is empty');
70
+ const headers = compileHeaders(target.headers);
67
71
 
68
72
  const { sites, blobs } = await buckets();
69
73
  const hostnames = await deployHostnames(target);
@@ -86,12 +90,36 @@ export async function release(target: Target, options: ReleaseOptions) {
86
90
  log(`Deploying ${target.project} ${target.stage} as ${deployId}`);
87
91
 
88
92
  step("reconcile");
89
- const [role, schedulerRole] = await Promise.all([ensureFunctionRole(target), ensureSchedulerRole()]);
90
- await Promise.all([ensureLogGroup(target.logGroup), ensureScheduleGroup()]);
91
93
  const aliasesChanged = target.domains.length ? (await reconcileAliases(target.domains)).changed : false;
92
94
  const edgeSecret = await readParameter(platform.edgeSecretParameter);
93
95
  if (!edgeSecret) throw new CloudError("No edge secret in SSM — run `devkit cloud bootstrap`");
94
96
 
97
+ if (target.isStatic) {
98
+ step("upload");
99
+ const previous = await readCurrent(target);
100
+ await uploadStatic(target, staticDir, deployId, previous?.deployId);
101
+ record.version = STATIC_VERSION;
102
+
103
+ step("flip");
104
+ const deployPath = `${target.stage}/${deployId}`;
105
+ const route: RouteRecord = {
106
+ p: target.project,
107
+ s: target.stage,
108
+ d: deployPath,
109
+ fn: "",
110
+ api: [],
111
+ mode: target.routing,
112
+ };
113
+ await flipRoutes(target, hostnames, route, headers);
114
+ await pointDns(target.domains, aliasesChanged);
115
+
116
+ await finish(target, record, previous?.deployId, "", hostnames, github, sites, started);
117
+ return;
118
+ }
119
+
120
+ const [role, schedulerRole] = await Promise.all([ensureFunctionRole(target), ensureSchedulerRole()]);
121
+ await Promise.all([ensureLogGroup(target.logGroup), ensureScheduleGroup()]);
122
+
95
123
  step("env");
96
124
  const stageEnv = await readStageEnv(target);
97
125
  if (target.crons.length && !stageEnv.CRON_SECRET) {
@@ -112,12 +140,7 @@ export async function release(target: Target, options: ReleaseOptions) {
112
140
 
113
141
  step("upload");
114
142
  const previous = await readCurrent(target);
115
- const prefix = names.deployPrefix(target.project, target.stage, deployId);
116
- const previousPrefix = previous ? names.deployPrefix(target.project, target.stage, previous.deployId) : undefined;
117
- const uploaded = existsSync(staticDir)
118
- ? await uploadDirectory(sites, staticDir, prefix, previousPrefix)
119
- : { uploaded: 0, copied: 0, bytes: 0 };
120
- log(` ${uploaded.uploaded} files uploaded (${formatBytes(uploaded.bytes)}), ${uploaded.copied} unchanged`);
143
+ await uploadStatic(target, staticDir, deployId, previous?.deployId);
121
144
 
122
145
  step("publish");
123
146
  const codeKey = names.codeKey(target.project, target.stage, deployId);
@@ -158,26 +181,12 @@ export async function release(target: Target, options: ReleaseOptions) {
158
181
  api: target.apiPrefixes,
159
182
  mode: target.routing,
160
183
  };
161
- await putRoutes(Object.fromEntries(hostnames.map((host) => [host, route])));
184
+ await flipRoutes(target, hostnames, route, headers);
162
185
  await pointDns(target.domains, aliasesChanged);
163
186
  const schedules = await reconcileSchedules(target, alias.arn, schedulerRole.arn);
164
187
  if (schedules.kept || schedules.removed) log(` ${schedules.kept} schedules, ${schedules.removed} removed`);
165
188
 
166
- step("record");
167
- record.status = "live";
168
- record.finishedAt = new Date().toISOString();
169
- await Promise.all([
170
- writeDeploy(record),
171
- writeCurrent(target, { deployId, version, since: record.finishedAt }),
172
- updateRegistry(target, functionUrl, hostnames),
173
- previous ? supersede(target, previous.deployId) : Promise.resolve(),
174
- ]);
175
- await github.succeed(`https://${target.primaryHost}`);
176
- await prune(target, sites);
177
-
178
- const seconds = Math.round((Date.now() - started) / 1000);
179
- log(`\nLive at https://${target.primaryHost} (${seconds}s)`);
180
- for (const host of hostnames.slice(1)) log(` https://${host}`);
189
+ await finish(target, record, previous?.deployId, functionUrl, hostnames, github, sites, started);
181
190
  } catch (error) {
182
191
  record.finishedAt = new Date().toISOString();
183
192
  record.error = error instanceof Error ? error.message : String(error);
@@ -187,6 +196,59 @@ export async function release(target: Target, options: ReleaseOptions) {
187
196
  }
188
197
  }
189
198
 
199
+ /** What a static deploy records as its version: there is no function to have one. */
200
+ export const STATIC_VERSION = "static";
201
+
202
+ async function uploadStatic(target: Target, staticDir: string, deployId: string, previousId: string | undefined) {
203
+ const { sites } = await buckets();
204
+ const prefix = names.deployPrefix(target.project, target.stage, deployId);
205
+ const previousPrefix = previousId ? names.deployPrefix(target.project, target.stage, previousId) : undefined;
206
+ const uploaded = existsSync(staticDir)
207
+ ? await uploadDirectory(sites, staticDir, prefix, previousPrefix)
208
+ : { uploaded: 0, copied: 0, bytes: 0 };
209
+ log(` ${uploaded.uploaded} files uploaded (${formatBytes(uploaded.bytes)}), ${uploaded.copied} unchanged`);
210
+ }
211
+
212
+ /** The deploy's headers and every hostname's route in one write: the stage flips at once. */
213
+ async function flipRoutes(
214
+ target: Target,
215
+ hostnames: readonly string[],
216
+ route: RouteRecord,
217
+ headers: string | undefined,
218
+ ) {
219
+ const record = JSON.stringify(headers ? { ...route, h: 1 } : route);
220
+ const puts: Record<string, string> = Object.fromEntries(hostnames.map((host) => [host, record]));
221
+ if (headers) puts[headersKey(target.project, route.d)] = headers;
222
+ await updateKeys(puts);
223
+ }
224
+
225
+ async function finish(
226
+ target: Target,
227
+ record: DeployRecord,
228
+ previousId: string | undefined,
229
+ functionUrl: string,
230
+ hostnames: readonly string[],
231
+ github: Awaited<ReturnType<typeof startGithubDeployment>>,
232
+ sites: string,
233
+ started: number,
234
+ ) {
235
+ step("record");
236
+ record.status = "live";
237
+ record.finishedAt = new Date().toISOString();
238
+ await Promise.all([
239
+ writeDeploy(record),
240
+ writeCurrent(target, { deployId: record.id, version: record.version, since: record.finishedAt }),
241
+ updateRegistry(target, functionUrl, hostnames),
242
+ previousId ? supersede(target, previousId) : Promise.resolve(),
243
+ ]);
244
+ await github.succeed(`https://${target.primaryHost}`);
245
+ await prune(target, sites);
246
+
247
+ const seconds = Math.round((Date.now() - started) / 1000);
248
+ log(`\nLive at https://${target.primaryHost} (${seconds}s)`);
249
+ for (const host of hostnames.slice(1)) log(` https://${host}`);
250
+ }
251
+
190
252
  /** Calls the new version straight through Lambda with the request the edge would make. */
191
253
  async function healthCheck(target: Target, version: string, edgeSecret: string) {
192
254
  const path = target.healthPath;
@@ -227,12 +289,24 @@ async function prune(target: Target, sites: string) {
227
289
  await deletePrefix(sites, names.codeKey(target.project, target.stage, deploy.id));
228
290
  await deletePrefix(sites, names.deployRecord(target.project, target.stage, deploy.id));
229
291
  }
292
+ // Headers of deploys no route can point at again.
293
+ const keptHeaders = new Set(
294
+ deploys.slice(0, KEEP_DEPLOYS).map((deploy) => headersKey(target.project, `${target.stage}/${deploy.id}`)),
295
+ );
296
+ const staleHeaders = (await keysWithPrefix(headersKey(target.project, `${target.stage}/`))).filter(
297
+ (key) => !keptHeaders.has(key),
298
+ );
299
+ if (staleHeaders.length) await updateKeys({}, staleHeaders);
230
300
  // Static prefixes with no record — a deploy that died before recording — go too.
231
301
  const known = new Set(deploys.slice(0, KEEP_DEPLOYS).map((deploy) => deploy.id));
232
302
  for (const prefix of await listPrefixes(sites, names.stagePrefix(target.project, target.stage))) {
233
303
  const id = prefix.slice(names.stagePrefix(target.project, target.stage).length, -1);
234
304
  if (!known.has(id)) await deletePrefix(sites, prefix);
235
305
  }
306
+ if (target.isStatic) {
307
+ if (stale.length) log(` pruned ${stale.length} old deploys`);
308
+ return;
309
+ }
236
310
  const live = await liveVersion(target.functionName);
237
311
  for (const version of await listVersions(target.functionName)) {
238
312
  if (version !== live && staleVersions.has(version)) await deleteVersion(target.functionName, version);
@@ -21,6 +21,6 @@ export async function deployAws(options: AwsTargetOptions) {
21
21
  // exactly as they do from a laptop's `.env`.
22
22
  Object.assign(process.env, await readStageEnv(target));
23
23
 
24
- await buildWeb({ ...options, host: "aws" });
24
+ await buildWeb({ ...options, host: "aws", static: target.isStatic });
25
25
  await release(target, { root });
26
26
  }