@elumixor/devkit 0.12.3 → 0.12.5

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.
Files changed (43) hide show
  1. package/README.md +87 -0
  2. package/package.json +26 -2
  3. package/src/bin/devkit.ts +32 -1
  4. package/src/blob/index.ts +169 -0
  5. package/src/build-targets/web.ts +66 -20
  6. package/src/cloud/aws.ts +108 -0
  7. package/src/cloud/bootstrap.ts +114 -0
  8. package/src/cloud/cli.ts +56 -0
  9. package/src/cloud/commands/context.ts +42 -0
  10. package/src/cloud/commands/destroy.ts +43 -0
  11. package/src/cloud/commands/domains.ts +68 -0
  12. package/src/cloud/commands/env.ts +59 -0
  13. package/src/cloud/commands/init.ts +74 -0
  14. package/src/cloud/commands/logs.ts +35 -0
  15. package/src/cloud/commands/rollback.ts +54 -0
  16. package/src/cloud/commands/status.ts +50 -0
  17. package/src/cloud/edge/router.js +86 -0
  18. package/src/cloud/env.ts +93 -0
  19. package/src/cloud/github.ts +92 -0
  20. package/src/cloud/index.ts +9 -0
  21. package/src/cloud/log-events.ts +56 -0
  22. package/src/cloud/mcp.ts +150 -0
  23. package/src/cloud/names.ts +98 -0
  24. package/src/cloud/nitro-build.ts +42 -0
  25. package/src/cloud/overview.ts +105 -0
  26. package/src/cloud/project.ts +146 -0
  27. package/src/cloud/reconcile/acm.ts +97 -0
  28. package/src/cloud/reconcile/cloudflare.ts +103 -0
  29. package/src/cloud/reconcile/edge.ts +108 -0
  30. package/src/cloud/reconcile/guardrails.ts +112 -0
  31. package/src/cloud/reconcile/iam.ts +328 -0
  32. package/src/cloud/reconcile/kvs.ts +98 -0
  33. package/src/cloud/reconcile/lambda.ts +305 -0
  34. package/src/cloud/reconcile/logs.ts +24 -0
  35. package/src/cloud/reconcile/platform.ts +281 -0
  36. package/src/cloud/reconcile/s3.ts +155 -0
  37. package/src/cloud/reconcile/scheduler.ts +100 -0
  38. package/src/cloud/records.ts +129 -0
  39. package/src/cloud/release.ts +268 -0
  40. package/src/cloud/runtime-wrapper.ts +84 -0
  41. package/src/deploy-targets/aws.ts +26 -0
  42. package/src/run-target.ts +2 -0
  43. package/src/runtime/index.ts +50 -0
@@ -0,0 +1,84 @@
1
+ import { EDGE_HEADER, HOST_HEADER } from "./names.ts";
2
+
3
+ /** Where `@elumixor/devkit/runtime` finds the wrapper's `waitUntil` at run time. */
4
+ export const WAIT_UNTIL_SYMBOL = "dk.waitUntil";
5
+ export const WRAPPER_FILE = "dk-handler.mjs";
6
+
7
+ /**
8
+ * The file written next to Nitro's `index.mjs` in every deploy. It is the Lambda handler: it keeps
9
+ * direct callers out, turns a schedule's event into the HTTP call it stands for, restores the
10
+ * viewer's hostname, and holds the invocation open for `waitUntil` work after the response ends.
11
+ *
12
+ * Plain JavaScript with no imports beyond Node, so it needs nothing installed in the bundle.
13
+ */
14
+ export const RUNTIME_WRAPPER = `import { AsyncLocalStorage } from "node:async_hooks";
15
+ import { handler as nitro } from "./index.mjs";
16
+
17
+ const EDGE_SECRET = process.env.DK_EDGE_SECRET;
18
+ const CRON_SECRET = process.env.CRON_SECRET;
19
+ const EDGE_HEADER = ${JSON.stringify(EDGE_HEADER)};
20
+ const HOST_HEADER = ${JSON.stringify(HOST_HEADER)};
21
+ const pendingWork = new AsyncLocalStorage();
22
+
23
+ globalThis[Symbol.for(${JSON.stringify(WAIT_UNTIL_SYMBOL)})] = (promise) => {
24
+ const pending = pendingWork.getStore();
25
+ if (pending) pending.push(Promise.resolve(promise));
26
+ else console.warn("waitUntil called outside a request; the promise is not awaited");
27
+ };
28
+
29
+ export const handler = awslambda.streamifyResponse(async (rawEvent, responseStream, context) => {
30
+ const event = rawEvent.dk === "cron" ? cronRequest(rawEvent) : rawEvent;
31
+ if (!EDGE_SECRET || event.headers?.[EDGE_HEADER] !== EDGE_SECRET) return reject(responseStream, 403, "Forbidden");
32
+
33
+ const viewerHost = event.headers[HOST_HEADER];
34
+ if (viewerHost) {
35
+ event.headers.host = viewerHost;
36
+ event.headers["x-forwarded-host"] = viewerHost;
37
+ if (event.requestContext) event.requestContext.domainName = viewerHost;
38
+ }
39
+ delete event.headers[EDGE_HEADER];
40
+ delete event.headers[HOST_HEADER];
41
+
42
+ const pending = [];
43
+ await pendingWork.run(pending, () => nitro(event, responseStream, context));
44
+ if (pending.length) {
45
+ const results = await Promise.allSettled(pending);
46
+ for (const result of results) if (result.status === "rejected") console.error("waitUntil task failed", result.reason);
47
+ }
48
+ });
49
+
50
+ // A schedule delivers {"dk":"cron","path":"/api/cron/x"}; the app sees the same GET Vercel would send.
51
+ function cronRequest(event) {
52
+ const path = event.path;
53
+ return {
54
+ version: "2.0",
55
+ routeKey: "$default",
56
+ rawPath: path,
57
+ rawQueryString: "",
58
+ headers: {
59
+ host: process.env.DK_HOST ?? "localhost",
60
+ accept: "*/*",
61
+ "user-agent": "devkit-cloud-cron",
62
+ ...(CRON_SECRET ? { authorization: "Bearer " + CRON_SECRET } : {}),
63
+ [EDGE_HEADER]: EDGE_SECRET,
64
+ },
65
+ requestContext: {
66
+ domainName: process.env.DK_HOST ?? "localhost",
67
+ http: { method: "GET", path, protocol: "HTTP/1.1", sourceIp: "127.0.0.1", userAgent: "devkit-cloud-cron" },
68
+ requestId: event.requestId ?? "cron",
69
+ time: new Date().toUTCString(),
70
+ timeEpoch: Date.now(),
71
+ },
72
+ isBase64Encoded: false,
73
+ };
74
+ }
75
+
76
+ function reject(responseStream, statusCode, message) {
77
+ const stream = awslambda.HttpResponseStream.from(responseStream, {
78
+ statusCode,
79
+ headers: { "content-type": "text/plain" },
80
+ });
81
+ stream.write(message);
82
+ stream.end();
83
+ }
84
+ `;
@@ -0,0 +1,26 @@
1
+ import { buildWeb } from "../build-targets/web.ts";
2
+ import { readStageEnv } from "../cloud/env.ts";
3
+ import { STAGE_BRANCHES } from "../cloud/names.ts";
4
+ import { type AwsTargetOptions, projectName, resolveStage, Target } from "../cloud/project.ts";
5
+ import { release } from "../cloud/release.ts";
6
+
7
+ /** How `--stage` reaches a deploy step, which the graph runs as its own process. */
8
+ export const STAGE_ENV = "DEVKIT_STAGE";
9
+
10
+ /**
11
+ * Devkit Cloud: build here, then upload the static files to S3 and the API to Lambda behind the
12
+ * shared CloudFront distribution. The same command runs on a laptop and in GitHub Actions.
13
+ */
14
+ export async function deployAws(options: AwsTargetOptions) {
15
+ const root = process.cwd();
16
+ const project = await projectName(options, root);
17
+ const stage = await resolveStage(process.env[STAGE_ENV], options.stages ?? STAGE_BRANCHES, root);
18
+ const target = new Target(project, stage, options);
19
+
20
+ // The stage's variables are the build's too, so `VITE_*` values reach the frontend bundle in CI
21
+ // exactly as they do from a laptop's `.env`.
22
+ Object.assign(process.env, await readStageEnv(target));
23
+
24
+ await buildWeb({ ...options, host: "aws" });
25
+ await release(target, { root });
26
+ }
package/src/run-target.ts CHANGED
@@ -3,6 +3,7 @@ import { buildTauriMacos } from "./build-targets/tauri-macos.ts";
3
3
  import { buildWeb } from "./build-targets/web.ts";
4
4
  import { buildXcode } from "./build-targets/xcode.ts";
5
5
  import { loadConfig, type Phase, phaseOf } from "./config.ts";
6
+ import { deployAws } from "./deploy-targets/aws.ts";
6
7
  import { deployFastlaneIos } from "./deploy-targets/fastlane-ios.ts";
7
8
  import { deployTauriMacos } from "./deploy-targets/tauri-macos.ts";
8
9
  import { deployVercel } from "./deploy-targets/vercel.ts";
@@ -38,6 +39,7 @@ const TARGETS: Record<Phase, Record<string, Target>> = {
38
39
  },
39
40
  deploy: {
40
41
  vercel: deployVercel,
42
+ aws: deployAws,
41
43
  "tauri-macos": deployTauriMacos,
42
44
  "fastlane-ios": deployFastlaneIos,
43
45
  },
@@ -0,0 +1,50 @@
1
+ /**
2
+ * `@elumixor/devkit/runtime`: what an app imports to keep working after the response has gone out.
3
+ *
4
+ * On Devkit Cloud the Lambda wrapper registers the collector; on Vercel the request context does.
5
+ * Code can switch its import before the hosting moves.
6
+ */
7
+
8
+ const DEVKIT_WAIT_UNTIL = Symbol.for("dk.waitUntil");
9
+ const VERCEL_REQUEST_CONTEXT = Symbol.for("@vercel/request-context");
10
+
11
+ type WaitUntil = (promise: Promise<unknown>) => void;
12
+
13
+ interface VercelRequestContext {
14
+ get?: () => { waitUntil?: WaitUntil } | undefined;
15
+ }
16
+
17
+ /**
18
+ * Keeps the invocation alive until `promise` settles, after the client has its response.
19
+ *
20
+ * Outside any host that supports it (local dev, tests) the promise simply runs — nothing waits,
21
+ * which is what a long-lived dev server does anyway.
22
+ */
23
+ export function waitUntil(promise: Promise<unknown>): void {
24
+ const scope = globalThis as Record<symbol, unknown>;
25
+ const devkit = scope[DEVKIT_WAIT_UNTIL] as WaitUntil | undefined;
26
+ const vercel = (scope[VERCEL_REQUEST_CONTEXT] as VercelRequestContext | undefined)?.get?.()?.waitUntil;
27
+ const host = devkit ?? vercel;
28
+ if (host) host(promise);
29
+ else promise.catch((error: unknown) => console.error("waitUntil task failed", error));
30
+ }
31
+
32
+ /** The deploy the running code belongs to, for tagging errors and logs. */
33
+ export const deployment = {
34
+ get project() {
35
+ return process.env.DK_PROJECT;
36
+ },
37
+ get stage() {
38
+ return process.env.DK_STAGE;
39
+ },
40
+ get id() {
41
+ return process.env.DK_DEPLOY_ID;
42
+ },
43
+ get host() {
44
+ return process.env.DK_HOST;
45
+ },
46
+ /** True when running on Devkit Cloud, as opposed to Vercel or a laptop. */
47
+ get isCloud() {
48
+ return Boolean(process.env.DK_PROJECT);
49
+ },
50
+ };