@elumixor/devkit 0.12.2 → 0.12.4
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 +83 -0
- package/package.json +26 -2
- package/src/bin/devkit.ts +32 -1
- package/src/blob/index.ts +169 -0
- package/src/build-targets/web.ts +66 -20
- package/src/cloud/aws.ts +108 -0
- package/src/cloud/bootstrap.ts +114 -0
- package/src/cloud/cli.ts +56 -0
- package/src/cloud/commands/context.ts +42 -0
- package/src/cloud/commands/destroy.ts +43 -0
- package/src/cloud/commands/domains.ts +68 -0
- package/src/cloud/commands/env.ts +59 -0
- package/src/cloud/commands/init.ts +74 -0
- package/src/cloud/commands/logs.ts +35 -0
- package/src/cloud/commands/rollback.ts +54 -0
- package/src/cloud/commands/status.ts +50 -0
- package/src/cloud/edge/router.js +86 -0
- package/src/cloud/env.ts +93 -0
- package/src/cloud/github.ts +92 -0
- package/src/cloud/index.ts +9 -0
- package/src/cloud/log-events.ts +56 -0
- package/src/cloud/mcp.ts +150 -0
- package/src/cloud/names.ts +98 -0
- package/src/cloud/nitro-build.ts +42 -0
- package/src/cloud/overview.ts +105 -0
- package/src/cloud/project.ts +143 -0
- package/src/cloud/reconcile/acm.ts +97 -0
- package/src/cloud/reconcile/cloudflare.ts +103 -0
- package/src/cloud/reconcile/edge.ts +97 -0
- package/src/cloud/reconcile/guardrails.ts +112 -0
- package/src/cloud/reconcile/iam.ts +328 -0
- package/src/cloud/reconcile/kvs.ts +98 -0
- package/src/cloud/reconcile/lambda.ts +305 -0
- package/src/cloud/reconcile/logs.ts +24 -0
- package/src/cloud/reconcile/platform.ts +281 -0
- package/src/cloud/reconcile/s3.ts +155 -0
- package/src/cloud/reconcile/scheduler.ts +100 -0
- package/src/cloud/records.ts +129 -0
- package/src/cloud/release.ts +268 -0
- package/src/cloud/runtime-wrapper.ts +84 -0
- package/src/deploy-targets/aws.ts +26 -0
- package/src/dev/board.tsx +18 -11
- package/src/run-target.ts +2 -0
- package/src/runtime/index.ts +50 -0
package/README.md
CHANGED
|
@@ -218,6 +218,11 @@ provisioning profiles can still tell you whether the app compiles.
|
|
|
218
218
|
**`vercel`** builds here and uploads the finished output, rather than handing Vercel the repo and
|
|
219
219
|
waiting for it to build the same thing again. Options: `cwd`.
|
|
220
220
|
|
|
221
|
+
**`aws`** is Devkit Cloud: the same web build, served from our own AWS account instead of Vercel.
|
|
222
|
+
The static files go to S3, the Nitro API to one Lambda function behind a shared CloudFront
|
|
223
|
+
distribution, and the stage is live at `<project>.devkit.atmagaming.com` (or `<project>-dev.…` for
|
|
224
|
+
`development`). See [Devkit Cloud](#devkit-cloud).
|
|
225
|
+
|
|
221
226
|
**`tauri-macos`** builds, signs and notarizes the macOS app if this machine has a Developer ID
|
|
222
227
|
certificate, stages the DMG and a manifest for the web deploy to hand out, and refreshes the local
|
|
223
228
|
install if there is one. Options: `frontendDir`, `stageDir`, `installIfPresent`, and the App Store
|
|
@@ -228,6 +233,84 @@ and hands the archive to fastlane, which signs the app, the watch app and the wi
|
|
|
228
233
|
TestFlight. Options: `frontendDir`, `iosDir`, `lane`, `defaultApiBaseUrl`, `apiBaseUrlEnv`, and the
|
|
229
234
|
key env var names.
|
|
230
235
|
|
|
236
|
+
## Devkit Cloud
|
|
237
|
+
|
|
238
|
+
Our Vercel replacement: a deploy target plus a handful of AWS services, with nothing running
|
|
239
|
+
between requests. One CloudFront distribution serves every project; a CloudFront Function reads
|
|
240
|
+
the hostname's route record from a KeyValueStore and sends the request to the project's Lambda
|
|
241
|
+
Function URL for API paths, or to that deploy's prefix in S3 for everything else. Every deploy is
|
|
242
|
+
immutable — its own S3 prefix and Lambda version — so a rollback is two writes.
|
|
243
|
+
|
|
244
|
+
```jsonc
|
|
245
|
+
"deploy": {
|
|
246
|
+
"web": {
|
|
247
|
+
"type": "aws",
|
|
248
|
+
"options": {
|
|
249
|
+
"project": "kenoverse", // default: the repo name
|
|
250
|
+
"apiPrefixes": ["api"],
|
|
251
|
+
"routing": "spa", // spa | pages | ssr
|
|
252
|
+
"memory": 512, "timeout": 120, // MB, seconds
|
|
253
|
+
"crons": [{ "path": "/api/cron/sync", "schedule": "*/5 * * * *" }],
|
|
254
|
+
"domains": { "production": ["kenoverse.app"], "development": [] },
|
|
255
|
+
"predeploy": "bun run db:migrate"
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
`devkit deploy` builds, then: reconciles the function, role, log group, URL, schedules and domains;
|
|
262
|
+
uploads the static files (unchanged files are copied server-side); publishes a new function
|
|
263
|
+
version; runs `predeploy` with the stage's env; invokes the new version's `/api/health`; and only
|
|
264
|
+
then moves the `live` alias and the route records. A failed health check leaves the old deploy
|
|
265
|
+
serving. `main` deploys `production`, `develop` deploys `development`; `--stage` picks by hand.
|
|
266
|
+
|
|
267
|
+
```bash
|
|
268
|
+
devkit cloud bootstrap # once per account: buckets, edge, roles, budgets ($5 and $20)
|
|
269
|
+
devkit cloud init # once per repo: the aws target and .github/workflows/deploy.yml
|
|
270
|
+
devkit cloud env push # the root .env into the stage (SSM Parameter Store)
|
|
271
|
+
devkit cloud status # what is live, with calls, errors and p95 for the last 24 h
|
|
272
|
+
devkit cloud logs --follow # CloudWatch logs for the stage
|
|
273
|
+
devkit cloud deploys <project> # the last ten deploys
|
|
274
|
+
devkit cloud rollback <project> # back to the previous deploy, in seconds
|
|
275
|
+
devkit cloud domains add <project> <host> # cert, alias, Cloudflare DNS, route; later deploys keep it
|
|
276
|
+
devkit cloud destroy <project> --stage development
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
Env vars live in SSM at `/dk/<project>/<stage>/<KEY>` and are copied into the function at deploy,
|
|
280
|
+
and into the build's environment, so `VITE_*` values reach the frontend in CI too. In the function,
|
|
281
|
+
`import { waitUntil } from "@elumixor/devkit/runtime"` keeps the invocation alive after the response
|
|
282
|
+
(it delegates to Vercel's `waitUntil` when the code still runs there), and
|
|
283
|
+
`import { put, list, del, head, createUploadUrl } from "@elumixor/devkit/blob"` covers what we used
|
|
284
|
+
of `@vercel/blob`: public blobs are served at `https://<host>/_blob/<path>`.
|
|
285
|
+
|
|
286
|
+
CI assumes the `dk-deploy` role through GitHub's OIDC provider, so no AWS keys are stored anywhere.
|
|
287
|
+
`bootstrap` also writes a scoped `devkit` AWS profile for deploying from a laptop. `examples/hello`
|
|
288
|
+
exercises every feature; it and the dashboard are what `.github/workflows/deploy-cloud.yml` deploys.
|
|
289
|
+
|
|
290
|
+
### Dashboard and MCP
|
|
291
|
+
|
|
292
|
+
[devkit.atmagaming.com](https://devkit.atmagaming.com) (`apps/dashboard`) shows every project and
|
|
293
|
+
stage, what is live, the last day's calls, errors and p95, the deploy history and the function
|
|
294
|
+
logs. It is itself a project on the platform; its function may read the deploy records, CloudWatch
|
|
295
|
+
metrics and `dk-*` log groups through the `permissions` option. Sign-in is the desk suite's Clerk
|
|
296
|
+
account: Clerk's Frontend API at clerk.desk.atmagaming.com is same-site with devkit.atmagaming.com,
|
|
297
|
+
so the session carries over without a satellite domain. The API accepts a session whose email is on
|
|
298
|
+
`DASHBOARD_EMAILS`, or `DASHBOARD_TOKEN` as a bearer for scripts. Read-only: deploys and rollbacks
|
|
299
|
+
stay with the CLI and MCP.
|
|
300
|
+
|
|
301
|
+
`devkit mcp` serves the same over the Model Context Protocol on stdio — `cloud_status`,
|
|
302
|
+
`cloud_deploys`, `cloud_logs`, `cloud_rollback`, `cloud_env` (keys only), `cloud_domains`,
|
|
303
|
+
`cloud_domain_add` and `cloud_domain_remove` — so
|
|
304
|
+
an agent can check and undo a deploy. Register it once:
|
|
305
|
+
|
|
306
|
+
```bash
|
|
307
|
+
claude mcp add --scope user devkit-cloud -- devkit mcp
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
`permissions` on the `aws` target appends IAM `Allow` statements to the function's role for apps
|
|
311
|
+
that read other AWS resources; everything else keeps the default role (its own logs and blob
|
|
312
|
+
prefix only).
|
|
313
|
+
|
|
231
314
|
## Versions
|
|
232
315
|
|
|
233
316
|
`devkit version` reads one version and writes it into every other file that declares one, so the
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elumixor/devkit",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.4",
|
|
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",
|
|
@@ -13,6 +13,12 @@
|
|
|
13
13
|
"devkit": "src/bin/devkit.ts",
|
|
14
14
|
"free-port": "src/bin/free-port.ts"
|
|
15
15
|
},
|
|
16
|
+
"exports": {
|
|
17
|
+
"./runtime": "./src/runtime/index.ts",
|
|
18
|
+
"./blob": "./src/blob/index.ts",
|
|
19
|
+
"./package.json": "./package.json",
|
|
20
|
+
"./cloud": "./src/cloud/index.ts"
|
|
21
|
+
},
|
|
16
22
|
"files": [
|
|
17
23
|
"src"
|
|
18
24
|
],
|
|
@@ -26,9 +32,27 @@
|
|
|
26
32
|
"release:major": "npm version major -m \"Release v%s\" && git push --follow-tags"
|
|
27
33
|
},
|
|
28
34
|
"dependencies": {
|
|
35
|
+
"@aws-sdk/client-acm": "^3.1161.0",
|
|
36
|
+
"@aws-sdk/client-budgets": "^3.1161.0",
|
|
37
|
+
"@aws-sdk/client-cloudfront": "^3.1161.0",
|
|
38
|
+
"@aws-sdk/client-cloudfront-keyvaluestore": "^3.1161.0",
|
|
39
|
+
"@aws-sdk/client-cloudwatch": "^3.1161.0",
|
|
40
|
+
"@aws-sdk/client-cloudwatch-logs": "^3.1161.0",
|
|
41
|
+
"@aws-sdk/client-cost-explorer": "^3.1161.0",
|
|
42
|
+
"@aws-sdk/client-iam": "^3.1161.0",
|
|
43
|
+
"@aws-sdk/client-lambda": "^3.1161.0",
|
|
44
|
+
"@aws-sdk/client-s3": "^3.1161.0",
|
|
45
|
+
"@aws-sdk/client-scheduler": "^3.1161.0",
|
|
46
|
+
"@aws-sdk/client-ssm": "^3.1161.0",
|
|
47
|
+
"@aws-sdk/client-sts": "^3.1161.0",
|
|
48
|
+
"@aws-sdk/credential-providers": "^3.1161.0",
|
|
49
|
+
"@aws-sdk/s3-request-presigner": "^3.1161.0",
|
|
50
|
+
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
51
|
+
"@smithy/signature-v4a": "^3.6.2",
|
|
29
52
|
"ink": "7.1.1",
|
|
30
53
|
"react": "19.2.8",
|
|
31
|
-
"wrap-ansi": "10.0.2"
|
|
54
|
+
"wrap-ansi": "10.0.2",
|
|
55
|
+
"zod": "^4.6.7"
|
|
32
56
|
},
|
|
33
57
|
"devDependencies": {
|
|
34
58
|
"@biomejs/biome": "^2.4.8",
|
package/src/bin/devkit.ts
CHANGED
|
@@ -4,8 +4,11 @@ import { parseArgs } from "node:util";
|
|
|
4
4
|
import pkg from "../../package.json" with { type: "json" };
|
|
5
5
|
import { runBuild } from "../build.ts";
|
|
6
6
|
import { runClone } from "../clone.ts";
|
|
7
|
+
import { CLOUD_USAGE, runCloud } from "../cloud/cli.ts";
|
|
8
|
+
import { runMcp } from "../cloud/mcp.ts";
|
|
7
9
|
import { PHASES, type Phase } from "../config.ts";
|
|
8
10
|
import { runDeploy } from "../deploy.ts";
|
|
11
|
+
import { STAGE_ENV } from "../deploy-targets/aws.ts";
|
|
9
12
|
import { runDev } from "../dev.ts";
|
|
10
13
|
import { runRelease } from "../release.ts";
|
|
11
14
|
import { runTarget } from "../run-target.ts";
|
|
@@ -22,6 +25,12 @@ const { values, positionals } = parseArgs({
|
|
|
22
25
|
device: { type: "string" },
|
|
23
26
|
dry: { type: "boolean", default: false },
|
|
24
27
|
verbose: { type: "boolean", default: false },
|
|
28
|
+
stage: { type: "string" },
|
|
29
|
+
since: { type: "string" },
|
|
30
|
+
grep: { type: "string" },
|
|
31
|
+
follow: { type: "boolean", default: false },
|
|
32
|
+
to: { type: "string" },
|
|
33
|
+
email: { type: "string" },
|
|
25
34
|
version: { type: "boolean", short: "v", default: false },
|
|
26
35
|
help: { type: "boolean", short: "h", default: false },
|
|
27
36
|
},
|
|
@@ -39,6 +48,8 @@ const usage = `usage: devkit dev [platforms...] run the dev processes
|
|
|
39
48
|
devkit build:<platform> build one platform
|
|
40
49
|
devkit deploy [platforms...] deploy every platform, in dependency order
|
|
41
50
|
devkit deploy:<platform> deploy one platform
|
|
51
|
+
devkit cloud <command> Devkit Cloud: bootstrap, init, status, logs, rollback, env, domains
|
|
52
|
+
devkit mcp Devkit Cloud as an MCP server over stdio (status, deploys, logs, rollback)
|
|
42
53
|
devkit setup [root] [--dry] validate secrets, install, terraform init, run steps
|
|
43
54
|
devkit clone <owner/repo> [dir] clone, decrypt secrets, set up
|
|
44
55
|
devkit version write the source version into every file that declares one
|
|
@@ -53,9 +64,12 @@ const usage = `usage: devkit dev [platforms...] run the dev processes
|
|
|
53
64
|
--plain dev: print prefixed lines instead of the live board (automatic when not a terminal)
|
|
54
65
|
--dry print what would run without running it
|
|
55
66
|
--verbose stream every line instead of drawing the live board (build, deploy)
|
|
67
|
+
--stage which stage an aws deploy or cloud command targets (default: the current branch's)
|
|
56
68
|
--real run a simulator against the real API instead of sample data
|
|
57
69
|
--device simulator to run on, overriding the configured one
|
|
58
|
-
--version print the devkit version
|
|
70
|
+
--version print the devkit version
|
|
71
|
+
|
|
72
|
+
${CLOUD_USAGE}`;
|
|
59
73
|
|
|
60
74
|
if (values.help) {
|
|
61
75
|
console.log(usage);
|
|
@@ -87,9 +101,26 @@ try {
|
|
|
87
101
|
break;
|
|
88
102
|
}
|
|
89
103
|
case "deploy": {
|
|
104
|
+
// Steps run as their own processes, so the stage travels to them through the environment.
|
|
105
|
+
if (values.stage) process.env[STAGE_ENV] = values.stage;
|
|
90
106
|
await runDeploy(platforms, { dry: values.dry, verbose: values.verbose });
|
|
91
107
|
break;
|
|
92
108
|
}
|
|
109
|
+
case "mcp": {
|
|
110
|
+
await runMcp();
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case "cloud": {
|
|
114
|
+
await runCloud(rest, {
|
|
115
|
+
stage: values.stage,
|
|
116
|
+
since: values.since,
|
|
117
|
+
grep: values.grep,
|
|
118
|
+
follow: values.follow,
|
|
119
|
+
to: values.to,
|
|
120
|
+
email: values.email,
|
|
121
|
+
});
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
93
124
|
// Internal: how a `type`d step actually runs — spawned as its own process by the graph (or by
|
|
94
125
|
// the dev supervisor) so it gets that step's own line on the board and its own log file.
|
|
95
126
|
case "_run": {
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DeleteObjectCommand,
|
|
3
|
+
GetObjectCommand,
|
|
4
|
+
HeadObjectCommand,
|
|
5
|
+
ListObjectsV2Command,
|
|
6
|
+
PutObjectCommand,
|
|
7
|
+
S3Client,
|
|
8
|
+
} from "@aws-sdk/client-s3";
|
|
9
|
+
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `@elumixor/devkit/blob`: the part of `@vercel/blob` our apps call, on the platform's blob bucket.
|
|
13
|
+
*
|
|
14
|
+
* Keys are `<project>/<stage>/<access>/<path>`. Public blobs are served through the edge at
|
|
15
|
+
* `https://<host>/_blob/<path>`; private ones only through presigned URLs that expire.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export type Access = "public" | "private";
|
|
19
|
+
|
|
20
|
+
export interface PutOptions {
|
|
21
|
+
access?: Access;
|
|
22
|
+
contentType?: string;
|
|
23
|
+
cacheControl?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface Blob {
|
|
27
|
+
pathname: string;
|
|
28
|
+
url: string;
|
|
29
|
+
access: Access;
|
|
30
|
+
size: number;
|
|
31
|
+
uploadedAt: Date;
|
|
32
|
+
contentType?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ListOptions {
|
|
36
|
+
prefix?: string;
|
|
37
|
+
access?: Access;
|
|
38
|
+
limit?: number;
|
|
39
|
+
cursor?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ListResult {
|
|
43
|
+
blobs: Blob[];
|
|
44
|
+
cursor?: string;
|
|
45
|
+
hasMore: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class BlobError extends Error {
|
|
49
|
+
override readonly name = "BlobError";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const PRESIGN_SECONDS = 15 * 60;
|
|
53
|
+
const DEFAULT_ACCESS: Access = "public";
|
|
54
|
+
|
|
55
|
+
let client: S3Client | undefined;
|
|
56
|
+
|
|
57
|
+
function settings() {
|
|
58
|
+
const bucket = process.env.DK_BLOB_BUCKET;
|
|
59
|
+
const project = process.env.DK_PROJECT;
|
|
60
|
+
const stage = process.env.DK_STAGE;
|
|
61
|
+
const host = process.env.DK_HOST;
|
|
62
|
+
if (!bucket || !project || !stage || !host)
|
|
63
|
+
throw new BlobError(
|
|
64
|
+
"Blob storage needs DK_BLOB_BUCKET, DK_PROJECT, DK_STAGE and DK_HOST — set by Devkit Cloud at deploy",
|
|
65
|
+
);
|
|
66
|
+
client ??= new S3Client({ region: process.env.AWS_REGION ?? "eu-central-1" });
|
|
67
|
+
return { bucket, prefix: `${project}/${stage}/`, host, client };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const normalize = (pathname: string) => pathname.replace(/^\/+/, "");
|
|
71
|
+
const keyFor = (prefix: string, access: Access, pathname: string) => `${prefix}${access}/${normalize(pathname)}`;
|
|
72
|
+
|
|
73
|
+
function urlFor(access: Access, pathname: string): Promise<string> {
|
|
74
|
+
const { bucket, prefix, host, client } = settings();
|
|
75
|
+
if (access === "public") return Promise.resolve(`https://${host}/_blob/${normalize(pathname)}`);
|
|
76
|
+
return getSignedUrl(client, new GetObjectCommand({ Bucket: bucket, Key: keyFor(prefix, access, pathname) }), {
|
|
77
|
+
expiresIn: PRESIGN_SECONDS,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Stores a blob and returns where it can be read. */
|
|
82
|
+
export async function put(
|
|
83
|
+
pathname: string,
|
|
84
|
+
body: string | Uint8Array | ArrayBuffer | ReadableStream,
|
|
85
|
+
options: PutOptions = {},
|
|
86
|
+
) {
|
|
87
|
+
const { bucket, prefix, client } = settings();
|
|
88
|
+
const access = options.access ?? DEFAULT_ACCESS;
|
|
89
|
+
const bytes =
|
|
90
|
+
body instanceof ReadableStream
|
|
91
|
+
? new Uint8Array(await new Response(body).arrayBuffer())
|
|
92
|
+
: body instanceof ArrayBuffer
|
|
93
|
+
? new Uint8Array(body)
|
|
94
|
+
: body;
|
|
95
|
+
await client.send(
|
|
96
|
+
new PutObjectCommand({
|
|
97
|
+
Bucket: bucket,
|
|
98
|
+
Key: keyFor(prefix, access, pathname),
|
|
99
|
+
Body: bytes,
|
|
100
|
+
ContentType: options.contentType,
|
|
101
|
+
CacheControl: options.cacheControl ?? (access === "public" ? "public, max-age=31536000, immutable" : undefined),
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
return { pathname: normalize(pathname), url: await urlFor(access, pathname), access };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function del(pathname: string, access: Access = DEFAULT_ACCESS) {
|
|
108
|
+
const { bucket, prefix, client } = settings();
|
|
109
|
+
await client.send(new DeleteObjectCommand({ Bucket: bucket, Key: keyFor(prefix, access, pathname) }));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export async function head(pathname: string, access: Access = DEFAULT_ACCESS): Promise<Blob | undefined> {
|
|
113
|
+
const { bucket, prefix, client } = settings();
|
|
114
|
+
try {
|
|
115
|
+
const result = await client.send(new HeadObjectCommand({ Bucket: bucket, Key: keyFor(prefix, access, pathname) }));
|
|
116
|
+
return {
|
|
117
|
+
pathname: normalize(pathname),
|
|
118
|
+
url: await urlFor(access, pathname),
|
|
119
|
+
access,
|
|
120
|
+
size: result.ContentLength ?? 0,
|
|
121
|
+
uploadedAt: result.LastModified ?? new Date(0),
|
|
122
|
+
contentType: result.ContentType,
|
|
123
|
+
};
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (error instanceof Error && (error.name === "NotFound" || error.name === "NoSuchKey")) return undefined;
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export async function list(options: ListOptions = {}): Promise<ListResult> {
|
|
131
|
+
const { bucket, prefix, client } = settings();
|
|
132
|
+
const access = options.access ?? DEFAULT_ACCESS;
|
|
133
|
+
const scope = `${prefix}${access}/`;
|
|
134
|
+
const page = await client.send(
|
|
135
|
+
new ListObjectsV2Command({
|
|
136
|
+
Bucket: bucket,
|
|
137
|
+
Prefix: scope + normalize(options.prefix ?? ""),
|
|
138
|
+
MaxKeys: options.limit,
|
|
139
|
+
ContinuationToken: options.cursor,
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
142
|
+
const blobs = await Promise.all(
|
|
143
|
+
(page.Contents ?? [])
|
|
144
|
+
.filter((item) => item.Key)
|
|
145
|
+
.map(async (item) => {
|
|
146
|
+
const pathname = (item.Key as string).slice(scope.length);
|
|
147
|
+
return {
|
|
148
|
+
pathname,
|
|
149
|
+
url: await urlFor(access, pathname),
|
|
150
|
+
access,
|
|
151
|
+
size: item.Size ?? 0,
|
|
152
|
+
uploadedAt: item.LastModified ?? new Date(0),
|
|
153
|
+
} satisfies Blob;
|
|
154
|
+
}),
|
|
155
|
+
);
|
|
156
|
+
return { blobs, cursor: page.NextContinuationToken, hasMore: Boolean(page.IsTruncated) };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** A URL a browser may `PUT` the file to directly, so bodies larger than a function call allows never touch the function. */
|
|
160
|
+
export async function createUploadUrl(pathname: string, options: PutOptions = {}) {
|
|
161
|
+
const { bucket, prefix, client } = settings();
|
|
162
|
+
const access = options.access ?? DEFAULT_ACCESS;
|
|
163
|
+
const uploadUrl = await getSignedUrl(
|
|
164
|
+
client,
|
|
165
|
+
new PutObjectCommand({ Bucket: bucket, Key: keyFor(prefix, access, pathname), ContentType: options.contentType }),
|
|
166
|
+
{ expiresIn: PRESIGN_SECONDS },
|
|
167
|
+
);
|
|
168
|
+
return { uploadUrl, pathname: normalize(pathname), url: await urlFor(access, pathname), access };
|
|
169
|
+
}
|
package/src/build-targets/web.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
|
-
import { cp, rename, rm } from "node:fs/promises";
|
|
2
|
+
import { cp, mkdir, rename, rm } from "node:fs/promises";
|
|
3
3
|
import { join, resolve } from "node:path";
|
|
4
4
|
import { $ } from "bun";
|
|
5
|
+
import { RUNTIME_WRAPPER, WRAPPER_FILE } from "../cloud/runtime-wrapper.ts";
|
|
5
6
|
|
|
6
|
-
/** A client-rendered web app and a Nitro API packed into one
|
|
7
|
+
/** A client-rendered web app and a Nitro API packed into one deployment. */
|
|
7
8
|
export interface WebBuildOptions {
|
|
8
9
|
/** Web app dir, run with `bun run build` (default `apps/frontend`). */
|
|
9
10
|
frontendDir?: string;
|
|
10
|
-
/** Nitro dir
|
|
11
|
+
/** Nitro dir (default `apps/backend`). */
|
|
11
12
|
backendDir?: string;
|
|
12
13
|
/** Where the web build lands inside `frontendDir` (default `build`). */
|
|
13
14
|
webBuildDir?: string;
|
|
@@ -15,29 +16,82 @@ export interface WebBuildOptions {
|
|
|
15
16
|
apiPrefixes: string[];
|
|
16
17
|
/** Extra staged directories to serve, as `{ "<source dir>": "<path under the site>" }`. */
|
|
17
18
|
include?: Record<string, string>;
|
|
19
|
+
/** Who serves the result: Vercel's build output API, or Devkit Cloud's S3 + Lambda (default `vercel`). */
|
|
20
|
+
host?: "vercel" | "aws";
|
|
18
21
|
}
|
|
19
22
|
|
|
23
|
+
/** Where the `aws` host's build lands, relative to the repo root. */
|
|
24
|
+
export const AWS_OUTPUT = ".devkit/out";
|
|
25
|
+
|
|
20
26
|
/**
|
|
21
|
-
* Build a client-rendered web app and a Nitro API into a single
|
|
22
|
-
*
|
|
23
|
-
*
|
|
27
|
+
* Build a client-rendered web app and a Nitro API into a single deployment: the app as static
|
|
28
|
+
* files, the API as the one serverless function behind them. Both end up at the same origin, so
|
|
29
|
+
* the web client calls the API without CORS and there is one URL to remember.
|
|
24
30
|
*/
|
|
25
31
|
export async function buildWeb(options: WebBuildOptions) {
|
|
26
32
|
const root = process.cwd();
|
|
27
33
|
const frontend = resolve(root, options.frontendDir ?? "apps/frontend");
|
|
28
34
|
const backend = resolve(root, options.backendDir ?? "apps/backend");
|
|
35
|
+
const webBuild = join(frontend, options.webBuildDir ?? "build");
|
|
36
|
+
|
|
37
|
+
await $`bun run build`.cwd(frontend);
|
|
38
|
+
if (!existsSync(webBuild)) throw new Error(`No web build at ${webBuild} — did the web build land?`);
|
|
39
|
+
|
|
40
|
+
if (options.host === "aws") return buildForAws(root, backend, webBuild, options);
|
|
41
|
+
return buildForVercel(root, backend, webBuild, options);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function buildForVercel(root: string, backend: string, webBuild: string, options: WebBuildOptions) {
|
|
29
45
|
const webDir = join(backend, "public");
|
|
30
46
|
const nitroOutput = join(backend, ".vercel/output");
|
|
31
47
|
const finalOutput = join(root, ".vercel/output");
|
|
32
48
|
|
|
33
|
-
await $`bun run build`.cwd(frontend);
|
|
34
49
|
await rm(webDir, { recursive: true, force: true });
|
|
35
|
-
await cp(
|
|
50
|
+
await cp(webBuild, webDir, { recursive: true });
|
|
51
|
+
await copyIncluded(root, webDir, options.include);
|
|
52
|
+
|
|
53
|
+
await rm(nitroOutput, { recursive: true, force: true });
|
|
54
|
+
await $`bunx nitro build --preset vercel`.cwd(backend);
|
|
36
55
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
56
|
+
await rm(finalOutput, { recursive: true, force: true });
|
|
57
|
+
await rename(nitroOutput, finalOutput);
|
|
58
|
+
|
|
59
|
+
await routeUnmatchedPathsToTheApp(finalOutput, options.apiPrefixes ?? []);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* For Devkit Cloud the edge serves the static files from S3 and the function only answers the API,
|
|
64
|
+
* so the web build never enters the Nitro bundle: `.devkit/out/static` is uploaded, `.devkit/out/server`
|
|
65
|
+
* is zipped, with the runtime wrapper written in as the Lambda entry.
|
|
66
|
+
*/
|
|
67
|
+
async function buildForAws(root: string, backend: string, webBuild: string, options: WebBuildOptions) {
|
|
68
|
+
const output = join(root, AWS_OUTPUT);
|
|
69
|
+
const staticDir = join(output, "static");
|
|
70
|
+
const serverDir = join(output, "server");
|
|
71
|
+
const nitroOutput = join(backend, ".output");
|
|
72
|
+
|
|
73
|
+
await rm(output, { recursive: true, force: true });
|
|
74
|
+
await mkdir(output, { recursive: true });
|
|
75
|
+
await cp(webBuild, staticDir, { recursive: true });
|
|
76
|
+
await copyIncluded(root, staticDir, options.include);
|
|
77
|
+
|
|
78
|
+
await rm(nitroOutput, { recursive: true, force: true });
|
|
79
|
+
await $`bun ${join(import.meta.dir, "../cloud/nitro-build.ts")} ${backend}`;
|
|
80
|
+
await rename(join(nitroOutput, "server"), serverDir);
|
|
81
|
+
// Nitro's own public assets (an openapi page, say) are served by the edge as well.
|
|
82
|
+
if (existsSync(join(nitroOutput, "public"))) await cp(join(nitroOutput, "public"), staticDir, { recursive: true });
|
|
83
|
+
|
|
84
|
+
await Bun.write(join(serverDir, WRAPPER_FILE), RUNTIME_WRAPPER);
|
|
85
|
+
console.log(`Static files in ${AWS_OUTPUT}/static, function in ${AWS_OUTPUT}/server`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Anything a sibling platform staged for the site to hand out — an installer, say — rides along
|
|
90
|
+
* with the web files rather than living in the frontend's static folder, which other builds copy
|
|
91
|
+
* wholesale.
|
|
92
|
+
*/
|
|
93
|
+
async function copyIncluded(root: string, webDir: string, include: Record<string, string> = {}) {
|
|
94
|
+
for (const [from, to] of Object.entries(include)) {
|
|
41
95
|
const staged = resolve(root, from);
|
|
42
96
|
if (!existsSync(staged)) {
|
|
43
97
|
console.log(`Nothing staged at ${from} — the site will serve no ${to}`);
|
|
@@ -46,14 +100,6 @@ export async function buildWeb(options: WebBuildOptions) {
|
|
|
46
100
|
await cp(staged, join(webDir, to), { recursive: true });
|
|
47
101
|
console.log(`Serving ${from} at /${to}`);
|
|
48
102
|
}
|
|
49
|
-
|
|
50
|
-
await rm(nitroOutput, { recursive: true, force: true });
|
|
51
|
-
await $`bunx nitro build --preset vercel`.cwd(backend);
|
|
52
|
-
|
|
53
|
-
await rm(finalOutput, { recursive: true, force: true });
|
|
54
|
-
await rename(nitroOutput, finalOutput);
|
|
55
|
-
|
|
56
|
-
await routeUnmatchedPathsToTheApp(finalOutput, options.apiPrefixes ?? []);
|
|
57
103
|
}
|
|
58
104
|
|
|
59
105
|
/**
|
package/src/cloud/aws.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// The KeyValueStore API signs with SigV4A; this side-effect import registers the pure-JS signer so
|
|
2
|
+
// the client works under Bun, where the CRT native module does not load.
|
|
3
|
+
import "@smithy/signature-v4a";
|
|
4
|
+
import { ACMClient } from "@aws-sdk/client-acm";
|
|
5
|
+
import { BudgetsClient } from "@aws-sdk/client-budgets";
|
|
6
|
+
import { CloudFrontClient } from "@aws-sdk/client-cloudfront";
|
|
7
|
+
import { CloudFrontKeyValueStoreClient } from "@aws-sdk/client-cloudfront-keyvaluestore";
|
|
8
|
+
import { CloudWatchClient } from "@aws-sdk/client-cloudwatch";
|
|
9
|
+
import { CloudWatchLogsClient } from "@aws-sdk/client-cloudwatch-logs";
|
|
10
|
+
import { CostExplorerClient } from "@aws-sdk/client-cost-explorer";
|
|
11
|
+
import { IAMClient } from "@aws-sdk/client-iam";
|
|
12
|
+
import { LambdaClient } from "@aws-sdk/client-lambda";
|
|
13
|
+
import { S3Client } from "@aws-sdk/client-s3";
|
|
14
|
+
import { SchedulerClient } from "@aws-sdk/client-scheduler";
|
|
15
|
+
import { SSMClient } from "@aws-sdk/client-ssm";
|
|
16
|
+
import { GetCallerIdentityCommand, STSClient } from "@aws-sdk/client-sts";
|
|
17
|
+
import { GLOBAL_REGION, REGION } from "./names.ts";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* One client per service, made on first use. Credentials come from the default chain, so the same
|
|
21
|
+
* code runs with a laptop profile and with the OIDC role GitHub Actions assumes.
|
|
22
|
+
*/
|
|
23
|
+
function lazy<T>(make: () => T) {
|
|
24
|
+
let instance: T | undefined;
|
|
25
|
+
return () => (instance ??= make());
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const lambda = lazy(() => new LambdaClient({ region: REGION }));
|
|
29
|
+
export const s3 = lazy(() => new S3Client({ region: REGION }));
|
|
30
|
+
export const ssm = lazy(() => new SSMClient({ region: REGION }));
|
|
31
|
+
export const iam = lazy(() => new IAMClient({ region: REGION }));
|
|
32
|
+
export const logs = lazy(() => new CloudWatchLogsClient({ region: REGION }));
|
|
33
|
+
export const cloudwatch = lazy(() => new CloudWatchClient({ region: REGION }));
|
|
34
|
+
export const scheduler = lazy(() => new SchedulerClient({ region: REGION }));
|
|
35
|
+
export const sts = lazy(() => new STSClient({ region: REGION }));
|
|
36
|
+
export const cloudfront = lazy(() => new CloudFrontClient({ region: GLOBAL_REGION }));
|
|
37
|
+
export const kvs = lazy(() => new CloudFrontKeyValueStoreClient({ region: GLOBAL_REGION }));
|
|
38
|
+
export const acm = lazy(() => new ACMClient({ region: GLOBAL_REGION }));
|
|
39
|
+
export const budgets = lazy(() => new BudgetsClient({ region: GLOBAL_REGION }));
|
|
40
|
+
export const costExplorer = lazy(() => new CostExplorerClient({ region: GLOBAL_REGION }));
|
|
41
|
+
|
|
42
|
+
let account: Promise<string> | undefined;
|
|
43
|
+
|
|
44
|
+
/** The account id, which bucket names and ARNs need; asked of STS once per run. */
|
|
45
|
+
export function accountId() {
|
|
46
|
+
account ??= sts()
|
|
47
|
+
.send(new GetCallerIdentityCommand({}))
|
|
48
|
+
.then((identity) => {
|
|
49
|
+
if (!identity.Account) throw new CloudError("STS returned no account id — are AWS credentials configured?");
|
|
50
|
+
return identity.Account;
|
|
51
|
+
});
|
|
52
|
+
return account;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Anything the platform cannot continue past: a missing resource, a failed check, a bad config. */
|
|
56
|
+
export class CloudError extends Error {
|
|
57
|
+
override readonly name = "CloudError";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** AWS SDK errors carry their kind in `name`; this is the only way to tell "missing" from "broken". */
|
|
61
|
+
export function isAwsError(error: unknown, ...names: string[]): error is Error {
|
|
62
|
+
return error instanceof Error && names.includes(error.name);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Resolves with the result, or `undefined` when the call failed with one of the named errors. */
|
|
66
|
+
export async function orUndefined<T>(call: Promise<T>, ...names: string[]): Promise<T | undefined> {
|
|
67
|
+
try {
|
|
68
|
+
return await call;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (isAwsError(error, ...names)) return undefined;
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Polls `check` until it returns a value, or gives up with a clear message. */
|
|
76
|
+
export async function waitFor<T>(
|
|
77
|
+
what: string,
|
|
78
|
+
check: () => Promise<T | undefined>,
|
|
79
|
+
{ everyMs = 3000, timeoutMs = 10 * 60 * 1000 } = {},
|
|
80
|
+
): Promise<T> {
|
|
81
|
+
const deadline = Date.now() + timeoutMs;
|
|
82
|
+
while (true) {
|
|
83
|
+
const result = await check();
|
|
84
|
+
if (result !== undefined) return result;
|
|
85
|
+
if (Date.now() > deadline) throw new CloudError(`Timed out waiting for ${what}`);
|
|
86
|
+
await Bun.sleep(everyMs);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Retries a call while it fails with one of the named errors — IAM roles are visible a few seconds after they exist. */
|
|
91
|
+
export async function retryWhile<T>(call: () => Promise<T>, names: string[], { times = 10, everyMs = 3000 } = {}) {
|
|
92
|
+
for (let attempt = 1; ; attempt++) {
|
|
93
|
+
try {
|
|
94
|
+
return await call();
|
|
95
|
+
} catch (error) {
|
|
96
|
+
if (attempt >= times || !isAwsError(error, ...names)) throw error;
|
|
97
|
+
await Bun.sleep(everyMs);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The `Tags` list most create calls take. */
|
|
103
|
+
export const platformTags = (extra: Record<string, string> = {}) =>
|
|
104
|
+
Object.entries({ dk: "platform", ...extra }).map(([Key, Value]) => ({ Key, Value }));
|
|
105
|
+
|
|
106
|
+
export function log(message: string) {
|
|
107
|
+
console.log(message);
|
|
108
|
+
}
|