@notation/factory-cloudflare 0.0.0-stage → 0.1.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.
Files changed (56) hide show
  1. package/LICENSE +21 -0
  2. package/image/Dockerfile +17 -0
  3. package/implementations/bindings.ts +8 -0
  4. package/implementations/code.ts +18 -0
  5. package/implementations/deployer.ts +212 -0
  6. package/implementations/index.ts +4 -0
  7. package/implementations/operator-auth.ts +47 -0
  8. package/implementations/router.ts +173 -0
  9. package/implementations/vault.ts +59 -0
  10. package/lib/access.ts +37 -0
  11. package/lib/artifacts.ts +74 -0
  12. package/lib/builds-token.ts +95 -0
  13. package/lib/cloudflare-api.ts +55 -0
  14. package/lib/outpost-workers.ts +41 -0
  15. package/lib/router-config.ts +27 -0
  16. package/package.json +39 -3
  17. package/resources/access.ts +74 -0
  18. package/resources/artifacts.ts +55 -0
  19. package/resources/builds-token.ts +55 -0
  20. package/resources/outpost-workers.ts +43 -0
  21. package/workers/build-watch.ts +128 -0
  22. package/workers/channel.ts +83 -0
  23. package/workers/cloudflare-workers.d.ts +34 -0
  24. package/workers/deployer.test.ts +97 -0
  25. package/workers/deployer.ts +106 -0
  26. package/workers/loader.ts +59 -0
  27. package/workers/outpost-api.ts +16 -0
  28. package/workers/outpost-settings.ts +23 -0
  29. package/workers/outpost-source.test.ts +44 -0
  30. package/workers/outpost-source.ts +59 -0
  31. package/workers/outpost.gen.ts +2 -0
  32. package/workers/outpost.ts +171 -0
  33. package/workers/outposts.ts +315 -0
  34. package/workers/router.ts +37 -0
  35. package/workers/test/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite +0 -0
  36. package/workers/test/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-shm +0 -0
  37. package/workers/test/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-wal +0 -0
  38. package/workers/test/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/metadata.sqlite +0 -0
  39. package/workers/test/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/metadata.sqlite-shm +0 -0
  40. package/workers/test/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/metadata.sqlite-wal +0 -0
  41. package/workers/test/.wrangler/state/v3/kv/miniflare-KVNamespaceObject/metadata.sqlite +0 -0
  42. package/workers/test/.wrangler/state/v3/kv/miniflare-KVNamespaceObject/metadata.sqlite-shm +0 -0
  43. package/workers/test/.wrangler/state/v3/kv/miniflare-KVNamespaceObject/metadata.sqlite-wal +0 -0
  44. package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/a590acd76969f996ec6e4b599c3c09f58c283a76f2d61392b5d3046caf557602.sqlite +0 -0
  45. package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/a590acd76969f996ec6e4b599c3c09f58c283a76f2d61392b5d3046caf557602.sqlite-shm +0 -0
  46. package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/a590acd76969f996ec6e4b599c3c09f58c283a76f2d61392b5d3046caf557602.sqlite-wal +0 -0
  47. package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite +0 -0
  48. package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite-shm +0 -0
  49. package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite-wal +0 -0
  50. package/workers/test/.wrangler/state/v3/r2/miniflare-R2BucketObject/metadata.sqlite +0 -0
  51. package/workers/test/.wrangler/state/v3/r2/miniflare-R2BucketObject/metadata.sqlite-shm +0 -0
  52. package/workers/test/.wrangler/state/v3/r2/miniflare-R2BucketObject/metadata.sqlite-wal +0 -0
  53. package/workers/test/evaluate-host.ts +22 -0
  54. package/workers/test/wrangler.jsonc +7 -0
  55. package/workers/types.ts +44 -0
  56. package/README.md +0 -3
@@ -0,0 +1,95 @@
1
+ import { execFile } from "node:child_process";
2
+ import { randomBytes } from "node:crypto";
3
+ import { createServer } from "node:http";
4
+ import type { AddressInfo } from "node:net";
5
+ import { cloudflare } from "./cloudflare-api.ts";
6
+
7
+ /** What the deployer's builds token may do: create outpost Workers and their Artifacts repos, and build them. */
8
+ const PERMISSIONS = [
9
+ ["workers_scripts", "edit"],
10
+ ["containers", "edit"],
11
+ ["cloudchamber", "edit"],
12
+ ["workers_ci", "edit"],
13
+ ["artifacts", "read"],
14
+ ["artifacts", "edit"],
15
+ ["account_settings", "read"],
16
+ ].map(([key, type]) => ({ key, type }));
17
+
18
+ const openBrowser = (url: string) => {
19
+ const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "explorer" : "xdg-open";
20
+ execFile(cmd, [url], () => {});
21
+ };
22
+
23
+ const escape = (text: string) => text.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
24
+
25
+ /** The id of a user API token, which also checks that the token is active. */
26
+ export const verifyUserToken = async (token: string) =>
27
+ (await cloudflare<{ id: string; status: string }>(token, "GET", "/user/tokens/verify")).id;
28
+
29
+ /**
30
+ * Asks you for a user API token, since Cloudflare has no API that creates
31
+ * one for an account token:
32
+ * 1. A local server serves a page with a link to the dashboard's create-token
33
+ * page, filled in with the token's name, account and permissions.
34
+ * 2. You create the token, and paste it into the local page.
35
+ * 3. The token is checked against the API.
36
+ */
37
+ export const askForBuildsToken = async (params: { name: string; accountId: string }) => {
38
+ const query = new URLSearchParams({
39
+ permissionGroupKeys: JSON.stringify(PERMISSIONS),
40
+ accountId: params.accountId,
41
+ zoneId: "all",
42
+ name: params.name,
43
+ });
44
+ const dashboard = `https://dash.cloudflare.com/profile/api-tokens?${query}`;
45
+ const state = randomBytes(16).toString("hex");
46
+ let resolve!: (created: { TokenId: string; Value: string }) => void;
47
+ const promise = new Promise<{ TokenId: string; Value: string }>((res) => {
48
+ resolve = res;
49
+ });
50
+ const page = (note = "") => `<!doctype html><meta charset="utf-8"><title>Notation Factory</title>
51
+ <body style="font-family:system-ui;max-width:40rem;margin:3rem auto;line-height:1.5">
52
+ <h1>Cloudflare API token for the deployer</h1>
53
+ <p>Workers Builds needs a user API token. ${note}</p>
54
+ <ol>
55
+ <li><a href="${escape(dashboard)}" target="_blank" rel="noopener">Create the token</a>. The page fills in its name, account and permissions. Keep them as they are.</li>
56
+ <li>Paste the token here.</li>
57
+ </ol>
58
+ <form method="post" action="/token?state=${state}">
59
+ <input name="token" type="password" autocomplete="off" required style="width:100%;font-size:1rem;padding:.4rem">
60
+ <p><button type="submit">Save token</button></p>
61
+ </form></body>`;
62
+
63
+ const server = createServer(async (req, res) => {
64
+ const url = new URL(req.url ?? "/", "http://localhost");
65
+ const send = (status: number, body: string) => {
66
+ res.writeHead(status, { "content-type": "text/html; charset=utf-8" });
67
+ res.end(body);
68
+ };
69
+ if (req.method === "GET" && url.pathname === "/") return send(200, page());
70
+ if (req.method !== "POST" || url.pathname !== "/token" || url.searchParams.get("state") !== state) {
71
+ return send(404, "not found");
72
+ }
73
+ const chunks: Buffer[] = [];
74
+ for await (const chunk of req) chunks.push(chunk as Buffer);
75
+ const value = new URLSearchParams(Buffer.concat(chunks).toString("utf8")).get("token")?.trim() ?? "";
76
+ try {
77
+ const TokenId = await verifyUserToken(value);
78
+ send(200, "<p style='font-family:system-ui'>Token saved. You can close this tab.</p>");
79
+ resolve({ TokenId, Value: value });
80
+ } catch {
81
+ send(400, page("<strong>Cloudflare didn't accept that token. Check it, and paste it again.</strong>"));
82
+ }
83
+ });
84
+
85
+ await new Promise<void>((listening) => server.listen(0, "localhost", listening));
86
+ const { port } = server.address() as AddressInfo;
87
+ const start = `http://localhost:${port}/`;
88
+ console.error(`Create the deployer's Cloudflare API token in your browser: ${start}`);
89
+ openBrowser(start);
90
+ try {
91
+ return await promise;
92
+ } finally {
93
+ server.close();
94
+ }
95
+ };
@@ -0,0 +1,55 @@
1
+ const API = "https://api.cloudflare.com/client/v4";
2
+
3
+ type Envelope<T> = { success: boolean; result: T; errors?: { code: number; message: string }[] };
4
+
5
+ /** An error from the Cloudflare API, with the response's status. */
6
+ export class CloudflareApiError extends Error {
7
+ readonly status: number;
8
+
9
+ constructor(status: number, message: string) {
10
+ super(message);
11
+ this.status = status;
12
+ }
13
+ }
14
+
15
+ /**
16
+ * Calls the Cloudflare API with `token`, and returns the envelope's result.
17
+ * `path` is relative to `/client/v4`. A JSON `body` is sent as JSON, and a
18
+ * `FormData` as a multipart form. Throws a `CloudflareApiError` for a status
19
+ * other than 2xx.
20
+ */
21
+ export const cloudflare = async <T = unknown>(
22
+ token: string,
23
+ method: string,
24
+ path: string,
25
+ body?: unknown,
26
+ ): Promise<T> => {
27
+ const headers: Record<string, string> = { authorization: `Bearer ${token}` };
28
+ let payload: BodyInit | undefined;
29
+ if (body instanceof FormData) payload = body;
30
+ else if (body !== undefined) {
31
+ headers["content-type"] = "application/json";
32
+ payload = JSON.stringify(body);
33
+ }
34
+ const response = await fetch(`${API}${path}`, { method, headers, body: payload });
35
+ const text = await response.text();
36
+ let envelope: Envelope<T> | undefined;
37
+ try {
38
+ envelope = text ? (JSON.parse(text) as Envelope<T>) : undefined;
39
+ } catch {}
40
+ if (!response.ok || envelope?.success === false) {
41
+ const errors = envelope?.errors?.map((e) => `${e.code} ${e.message}`).join("; ") || text.slice(0, 500);
42
+ throw new CloudflareApiError(response.status, `${method} ${path} failed with status ${response.status}: ${errors}`);
43
+ }
44
+ return envelope?.result as T;
45
+ };
46
+
47
+ /** Like `cloudflare`, but resolves to `undefined` for a 404. */
48
+ export const cloudflareOrNone = async <T = unknown>(token: string, method: string, path: string, body?: unknown) => {
49
+ try {
50
+ return await cloudflare<T>(token, method, path, body);
51
+ } catch (error) {
52
+ if (error instanceof CloudflareApiError && error.status === 404) return undefined;
53
+ throw error;
54
+ }
55
+ };
@@ -0,0 +1,41 @@
1
+ import { cloudflare, cloudflareOrNone } from "./cloudflare-api.ts";
2
+
3
+ type Application = { id: string; name: string };
4
+
5
+ // Cloudflare refuses a container application's name with `--`, so the
6
+ // outpost config collapses dashes, and names are compared the same way.
7
+ const collapse = (name: string) => name.replace(/-{2,}/g, "-");
8
+
9
+ const applications = (token: string, accountId: string) =>
10
+ cloudflare<Application[]>(token, "GET", `/accounts/${accountId}/containers/applications`);
11
+
12
+ const deleteApplication = (token: string, accountId: string, application: Application) =>
13
+ cloudflareOrNone(token, "DELETE", `/accounts/${accountId}/containers/applications/${application.id}`);
14
+
15
+ /**
16
+ * Deletes the outpost Worker `worker` and its container application.
17
+ * Deleting a Worker leaves its container application in place.
18
+ */
19
+ export const deleteOutpostWorker = async (token: string, accountId: string, worker: string) => {
20
+ await cloudflareOrNone(token, "DELETE", `/accounts/${accountId}/workers/scripts/${worker}?force=true`);
21
+ const name = collapse(worker);
22
+ for (const application of await applications(token, accountId)) {
23
+ if (application.name === name || application.name.startsWith(`${name}-`)) {
24
+ await deleteApplication(token, accountId, application);
25
+ }
26
+ }
27
+ };
28
+
29
+ /** Deletes every outpost Worker whose name starts with `${prefix}-`, and every container application of one. */
30
+ export const deleteOutpostWorkers = async (token: string, accountId: string, prefix: string) => {
31
+ const scripts = await cloudflare<{ id: string }[]>(token, "GET", `/accounts/${accountId}/workers/scripts`);
32
+ for (const { id } of scripts) {
33
+ if (id.startsWith(`${prefix}-`)) {
34
+ await cloudflareOrNone(token, "DELETE", `/accounts/${accountId}/workers/scripts/${id}?force=true`);
35
+ }
36
+ }
37
+ const start = `${collapse(prefix)}-`;
38
+ for (const application of await applications(token, accountId)) {
39
+ if (application.name.startsWith(start)) await deleteApplication(token, accountId, application);
40
+ }
41
+ };
@@ -0,0 +1,27 @@
1
+ import * as v from "valibot";
2
+
3
+ /** The KV key that holds the router's config. */
4
+ export const ROUTER_CONFIG_KEY = "config";
5
+
6
+ const connectionEntry = v.object({
7
+ provider: v.string(),
8
+ source: v.string(),
9
+ credentials: v.record(v.string(), v.string()),
10
+ channel: v.optional(v.string()),
11
+ });
12
+
13
+ const codeHostEntry = v.object({
14
+ provider: v.string(),
15
+ connection: v.string(),
16
+ allow: v.array(v.object({ owner: v.string(), repo: v.optional(v.string()) })),
17
+ });
18
+
19
+ // The fields other than `provider` are the provider's own, such as GitHub's logins.
20
+ const operatorsEntry = v.looseObject({ provider: v.string() });
21
+
22
+ /** The router's config, as the router's Worker reads it from KV. */
23
+ export const RouterConfigSchema = v.object({
24
+ connections: v.optional(v.record(v.string(), connectionEntry)),
25
+ codeHost: v.optional(codeHostEntry),
26
+ operators: v.optional(operatorsEntry),
27
+ });
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@notation/factory-cloudflare",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "author": "Daniel Grant",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/notationlabs/factory.git",
9
+ "directory": "packages/cloudflare"
10
+ },
11
+ "type": "module",
12
+ "exports": {
13
+ ".": "./implementations/index.ts"
14
+ },
15
+ "files": [
16
+ "image",
17
+ "implementations",
18
+ "lib",
19
+ "resources",
20
+ "workers"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "dependencies": {
26
+ "@cloudflare/containers": "0.3.7",
27
+ "@notation/build": "0.28.0",
28
+ "@notation/cloudflare": "0.28.0",
29
+ "@notation/core": "0.28.0",
30
+ "@notation/resource": "0.28.0",
31
+ "@notation/std": "0.28.0",
32
+ "isomorphic-git": "1.44.16",
33
+ "memfs": "4.80.1",
34
+ "valibot": "^1.1.0"
35
+ },
36
+ "peerDependencies": {
37
+ "@notation/factory": "^0.1.0"
38
+ },
39
+ "devDependencies": {
40
+ "@notation/factory": "0.1.0"
41
+ }
6
42
  }
@@ -0,0 +1,74 @@
1
+ import { resource } from "@notation/resource";
2
+ import * as v from "valibot";
3
+ import {
4
+ createApplication,
5
+ deleteApplication,
6
+ getApplication,
7
+ updateApplication,
8
+ } from "../lib/access.ts";
9
+
10
+ type AccessApplicationApi = {
11
+ Key: { AccountId: string; ApplicationId: string };
12
+ CreateParams: {
13
+ AccountId: string;
14
+ Name: string;
15
+ Domain: string;
16
+ Emails: string[];
17
+ };
18
+ UpdateParams: { Domain: string; Emails: string[] };
19
+ ReadResult: { Aud: string };
20
+ };
21
+
22
+ const accessApplication = resource<AccessApplicationApi>({
23
+ type: "cloudflare/access/AccessApplication",
24
+ provider: "@notation/factory-cloudflare",
25
+ });
26
+
27
+ const accessApplicationSchema = accessApplication.defineSchema({
28
+ AccountId: {
29
+ propertyType: "param",
30
+ presence: "required",
31
+ immutable: true,
32
+ secondaryKey: true,
33
+ value: v.string(),
34
+ },
35
+ ApplicationId: {
36
+ propertyType: "computed",
37
+ presence: "required",
38
+ primaryKey: true,
39
+ value: v.string(),
40
+ },
41
+ Name: {
42
+ propertyType: "param",
43
+ presence: "required",
44
+ immutable: true,
45
+ value: v.string(),
46
+ },
47
+ Domain: {
48
+ propertyType: "param",
49
+ presence: "required",
50
+ value: v.string(),
51
+ },
52
+ Emails: {
53
+ propertyType: "param",
54
+ presence: "required",
55
+ value: v.array(v.string()),
56
+ },
57
+ Aud: {
58
+ propertyType: "computed",
59
+ presence: "required",
60
+ value: v.string(),
61
+ },
62
+ } as const);
63
+
64
+ /** An Access application in front of the router's admin routes. The router verifies Access's token against `Aud`. */
65
+ export const AccessApplication = accessApplicationSchema
66
+ .defineOperations({
67
+ create: async (params) => createApplication(params),
68
+ read: async (key) => getApplication(key),
69
+ update: async (key, _patch, { Domain, Emails }) =>
70
+ updateApplication(key, { Domain, Emails }),
71
+ delete: async (key) => deleteApplication(key),
72
+ });
73
+
74
+ export type AccessApplicationInstance = InstanceType<typeof AccessApplication>;
@@ -0,0 +1,55 @@
1
+ import { defineResource, ResourceNotFoundError } from "@notation/resource";
2
+ import * as v from "valibot";
3
+ import { createNamespace, deleteNamespace, getNamespace } from "../lib/artifacts.ts";
4
+
5
+ type ArtifactsNamespaceApi = {
6
+ Key: { AccountId: string; Name: string };
7
+ CreateParams: { AccountId: string; Name: string };
8
+ UpdateParams: {};
9
+ ReadResult: {};
10
+ };
11
+
12
+ const apiToken = () => {
13
+ const token = process.env.CLOUDFLARE_API_TOKEN;
14
+ if (!token) throw new Error("An Artifacts namespace needs CLOUDFLARE_API_TOKEN, with Artifacts Edit.");
15
+ return token;
16
+ };
17
+
18
+ /**
19
+ * An Artifacts namespace. Its name gets a random ending, since Artifacts
20
+ * never frees a deleted namespace's name. Delete deletes its repos too.
21
+ */
22
+ export const ArtifactsNamespace = defineResource<ArtifactsNamespaceApi>({
23
+ type: "cloudflare/artifacts/Namespace",
24
+ provider: "@notation/factory-cloudflare",
25
+ })
26
+ .defineSchema({
27
+ AccountId: {
28
+ propertyType: "param",
29
+ presence: "required",
30
+ immutable: true,
31
+ secondaryKey: true,
32
+ value: v.string(),
33
+ },
34
+ Name: {
35
+ propertyType: "param",
36
+ presence: "required",
37
+ immutable: true,
38
+ primaryKey: true,
39
+ value: v.string(),
40
+ physicalName: { maxLength: 63 },
41
+ },
42
+ } as const)
43
+ .defineOperations({
44
+ create: async ({ AccountId, Name }) => {
45
+ await createNamespace(apiToken(), AccountId, Name);
46
+ },
47
+ read: async ({ AccountId, Name }) => {
48
+ if (!(await getNamespace(apiToken(), AccountId, Name))) throw new ResourceNotFoundError(`There's no Artifacts namespace ${Name}.`);
49
+ return {};
50
+ },
51
+ update: async () => {},
52
+ delete: async ({ AccountId, Name }) => deleteNamespace(apiToken(), AccountId, Name),
53
+ });
54
+
55
+ export type ArtifactsNamespaceInstance = InstanceType<typeof ArtifactsNamespace>;
@@ -0,0 +1,55 @@
1
+ import { defineResource } from "@notation/resource";
2
+ import * as v from "valibot";
3
+ import { askForBuildsToken } from "../lib/builds-token.ts";
4
+
5
+ type BuildsTokenApi = {
6
+ Key: { TokenId: string };
7
+ CreateParams: { AccountId: string; Name: string };
8
+ UpdateParams: {};
9
+ ReadResult: {};
10
+ };
11
+
12
+ /**
13
+ * A user API token that the deployer builds outpost Workers with, since the
14
+ * Workers Builds API refuses account tokens. Create asks you for it in the
15
+ * browser.
16
+ */
17
+ export const BuildsToken = defineResource<BuildsTokenApi>({
18
+ type: "cloudflare/builds/BuildsToken",
19
+ provider: "@notation/factory-cloudflare",
20
+ })
21
+ .defineSchema({
22
+ AccountId: {
23
+ propertyType: "param",
24
+ presence: "required",
25
+ immutable: true,
26
+ value: v.string(),
27
+ },
28
+ Name: {
29
+ propertyType: "param",
30
+ presence: "required",
31
+ immutable: true,
32
+ value: v.string(),
33
+ },
34
+ TokenId: {
35
+ propertyType: "computed",
36
+ presence: "required",
37
+ primaryKey: true,
38
+ value: v.string(),
39
+ },
40
+ /** The token. Cloudflare shows it once, when you create it. */
41
+ Value: { propertyType: "credential", value: v.string() },
42
+ } as const)
43
+ .defineOperations({
44
+ create: async ({ AccountId, Name }) => askForBuildsToken({ name: Name, accountId: AccountId }),
45
+ rotateManually: {
46
+ Value: "Roll the token on https://dash.cloudflare.com/profile/api-tokens, or create another.",
47
+ },
48
+ read: async () => ({}),
49
+ update: async () => {},
50
+ delete: async ({ TokenId }) => {
51
+ console.error(`Delete the deployer's builds token ${TokenId} on https://dash.cloudflare.com/profile/api-tokens`);
52
+ },
53
+ });
54
+
55
+ export type BuildsTokenInstance = InstanceType<typeof BuildsToken>;
@@ -0,0 +1,43 @@
1
+ import { defineResource } from "@notation/resource";
2
+ import * as v from "valibot";
3
+ import { deleteOutpostWorkers } from "../lib/outpost-workers.ts";
4
+
5
+ type OutpostWorkersApi = {
6
+ Key: { AccountId: string; Prefix: string };
7
+ CreateParams: { AccountId: string; Prefix: string; Deployer: string };
8
+ UpdateParams: { Deployer: string };
9
+ ReadResult: {};
10
+ };
11
+
12
+ const apiToken = () => {
13
+ const token = process.env.CLOUDFLARE_API_TOKEN;
14
+ if (!token) {
15
+ throw new Error("Deleting outpost Workers needs CLOUDFLARE_API_TOKEN, with Workers Scripts Edit and Containers Edit.");
16
+ }
17
+ return token;
18
+ };
19
+
20
+ /**
21
+ * The outpost Workers that a deployer creates at run time, and their
22
+ * container applications. Create does nothing. Delete deletes every Worker
23
+ * whose name starts with `Prefix`.
24
+ */
25
+ export const OutpostWorkers = defineResource<OutpostWorkersApi>({
26
+ type: "cloudflare/outposts/Workers",
27
+ provider: "@notation/factory-cloudflare",
28
+ })
29
+ .defineSchema({
30
+ AccountId: { propertyType: "param", presence: "required", immutable: true, secondaryKey: true, value: v.string() },
31
+ Prefix: { propertyType: "param", presence: "required", immutable: true, primaryKey: true, value: v.string() },
32
+ /**
33
+ * The deployer Worker's name. Outpost Workers bind the deployer as a
34
+ * service, so this makes deploy delete them before the deployer.
35
+ */
36
+ Deployer: { propertyType: "param", presence: "required", value: v.string() },
37
+ } as const)
38
+ .defineOperations({
39
+ create: async () => {},
40
+ read: async () => ({}),
41
+ update: async () => {},
42
+ delete: async ({ AccountId, Prefix }) => deleteOutpostWorkers(apiToken(), AccountId, Prefix),
43
+ });
@@ -0,0 +1,128 @@
1
+ import { DurableObject } from "cloudflare:workers";
2
+ import { type BuildReport, signBuildReport } from "@notation/factory/router";
3
+ import { cloudflare } from "../lib/cloudflare-api.ts";
4
+ import { type OutpostsEnv, settingsOf, sourceKey } from "./outposts.ts";
5
+
6
+ /** A build that the deployer started, by pushing `commit` to the Artifacts repo of the outpost Worker `worker`. */
7
+ export type StartedBuild = {
8
+ container: string;
9
+ worker: string;
10
+ /** The Worker's tag, which Workers Builds lists its builds by. */
11
+ tag: string;
12
+ commit: string;
13
+ /** The hash of the source that the deployer pushed. */
14
+ hash: string;
15
+ /**
16
+ * The uuid of the build, when the deployer started it itself. A build
17
+ * started by branch, not by a push, may not carry the pushed commit.
18
+ */
19
+ build?: string;
20
+ };
21
+
22
+ /** A started build, with the code host's repo and the commit that `.factory/` was read at. */
23
+ export type WatchedBuild = StartedBuild & { repo: string; ref: string };
24
+
25
+ type Stored = WatchedBuild & { since: number; url?: string };
26
+
27
+ type Build = {
28
+ build_uuid: string;
29
+ status: "queued" | "initializing" | "running" | "stopped";
30
+ build_outcome: "success" | "fail" | "skipped" | "cancelled" | "terminated" | null;
31
+ build_trigger_metadata?: { commit_hash?: string };
32
+ };
33
+
34
+ const POLL_MS = 30_000;
35
+
36
+ /** How long a build may take to start and finish before the deployer stops watching it. */
37
+ const GIVE_UP_MS = 2 * 60 * 60 * 1000;
38
+
39
+ const REPORT_TTL_MS = 5 * 60 * 1000;
40
+
41
+ const buildUrl = (accountId: string, worker: string, uuid: string) =>
42
+ `https://dash.cloudflare.com/${accountId}/workers/services/view/${worker}/production/builds/${uuid}`;
43
+
44
+ /**
45
+ * Watches the builds that the deployer started, and reports each one's
46
+ * progress to the router's `/builds` route. The deployer hosts one object.
47
+ */
48
+ export class BuildWatch extends DurableObject<OutpostsEnv> {
49
+ async watch(builds: WatchedBuild[]) {
50
+ for (const build of builds) {
51
+ await this.ctx.storage.put(`${build.worker}/${build.commit}`, { ...build, since: Date.now() });
52
+ }
53
+ if (!(await this.ctx.storage.getAlarm())) await this.ctx.storage.setAlarm(Date.now() + POLL_MS);
54
+ }
55
+
56
+ async alarm() {
57
+ for (const [key, build] of (await this.ctx.storage.list()) as Map<string, Stored>) {
58
+ try {
59
+ const next = await this.#poll(build);
60
+ if (next) await this.ctx.storage.put(key, next);
61
+ else await this.ctx.storage.delete(key);
62
+ } catch (error) {
63
+ console.error(JSON.stringify({ worker: build.worker, commit: build.commit, error: String(error) }));
64
+ if (Date.now() - build.since > GIVE_UP_MS) await this.ctx.storage.delete(key);
65
+ }
66
+ }
67
+ if ((await this.ctx.storage.list({ limit: 1 })).size) await this.ctx.storage.setAlarm(Date.now() + POLL_MS);
68
+ }
69
+
70
+ /**
71
+ * Reports the build of `watched` once it runs and once it stops. Resolves
72
+ * to the build to keep watching, or `undefined` when it's done.
73
+ */
74
+ async #poll(watched: Stored): Promise<Stored | undefined> {
75
+ const settings = await settingsOf(this.env);
76
+ const token = await this.env.CLOUDFLARE_BUILDS_TOKEN.get();
77
+ const builds = await cloudflare<Build[]>(
78
+ token,
79
+ "GET",
80
+ `/accounts/${settings.accountId}/builds/workers/${watched.tag}/builds?per_page=10`,
81
+ );
82
+ const build = builds.find((b) =>
83
+ watched.build ? b.build_uuid === watched.build : b.build_trigger_metadata?.commit_hash === watched.commit,
84
+ );
85
+ const late = Date.now() - watched.since > GIVE_UP_MS;
86
+ if (!build || build.status !== "stopped") {
87
+ if (late) {
88
+ await this.#failed(watched);
89
+ await this.#report(settings.router, watched, { state: "failure", summary: "The build didn't finish within 2 hours." });
90
+ return undefined;
91
+ }
92
+ if (!build || watched.url) return watched;
93
+ const url = buildUrl(settings.accountId, watched.worker, build.build_uuid);
94
+ await this.#report(settings.router, watched, { state: "in_progress", url });
95
+ return { ...watched, url };
96
+ }
97
+ const url = buildUrl(settings.accountId, watched.worker, build.build_uuid);
98
+ const state = build.build_outcome === "success" ? "success" : build.build_outcome === "cancelled" ? "cancelled" : "failure";
99
+ if (state !== "success") await this.#failed(watched);
100
+ await this.#report(settings.router, watched, { state, url, summary: `Workers Builds: ${build.build_outcome ?? "no outcome"}.` });
101
+ return undefined;
102
+ }
103
+
104
+ /** Forgets the source of a failed build, so that the next push builds it again. A newer source stays. */
105
+ async #failed(watched: Stored) {
106
+ const key = sourceKey(watched.worker);
107
+ if ((await this.env.FACTORY_KV.get(key)) === watched.hash) await this.env.FACTORY_KV.delete(key);
108
+ }
109
+
110
+ async #report(router: string, watched: Stored, fields: Pick<BuildReport, "state" | "url" | "summary">) {
111
+ const report: BuildReport = {
112
+ repo: watched.repo,
113
+ ref: watched.ref,
114
+ container: watched.container,
115
+ ...fields,
116
+ expires: Date.now() + REPORT_TTL_MS,
117
+ };
118
+ const signature = await signBuildReport(await this.env.FACTORY_ROOT_KEY.get(), report);
119
+ const response = await fetch(`${router}/builds`, {
120
+ method: "POST",
121
+ headers: { "content-type": "application/json" },
122
+ body: JSON.stringify({ report, signature }),
123
+ });
124
+ if (!response.ok) {
125
+ throw new Error(`The router refused the build report with status ${response.status}: ${(await response.text()).slice(0, 500)}`);
126
+ }
127
+ }
128
+ }