@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.
- package/LICENSE +21 -0
- package/image/Dockerfile +17 -0
- package/implementations/bindings.ts +8 -0
- package/implementations/code.ts +18 -0
- package/implementations/deployer.ts +212 -0
- package/implementations/index.ts +4 -0
- package/implementations/operator-auth.ts +47 -0
- package/implementations/router.ts +173 -0
- package/implementations/vault.ts +59 -0
- package/lib/access.ts +37 -0
- package/lib/artifacts.ts +74 -0
- package/lib/builds-token.ts +95 -0
- package/lib/cloudflare-api.ts +55 -0
- package/lib/outpost-workers.ts +41 -0
- package/lib/router-config.ts +27 -0
- package/package.json +39 -3
- package/resources/access.ts +74 -0
- package/resources/artifacts.ts +55 -0
- package/resources/builds-token.ts +55 -0
- package/resources/outpost-workers.ts +43 -0
- package/workers/build-watch.ts +128 -0
- package/workers/channel.ts +83 -0
- package/workers/cloudflare-workers.d.ts +34 -0
- package/workers/deployer.test.ts +97 -0
- package/workers/deployer.ts +106 -0
- package/workers/loader.ts +59 -0
- package/workers/outpost-api.ts +16 -0
- package/workers/outpost-settings.ts +23 -0
- package/workers/outpost-source.test.ts +44 -0
- package/workers/outpost-source.ts +59 -0
- package/workers/outpost.gen.ts +2 -0
- package/workers/outpost.ts +171 -0
- package/workers/outposts.ts +315 -0
- package/workers/router.ts +37 -0
- package/workers/test/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/metadata.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/kv/miniflare-KVNamespaceObject/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/kv/miniflare-KVNamespaceObject/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/kv/miniflare-KVNamespaceObject/metadata.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/a590acd76969f996ec6e4b599c3c09f58c283a76f2d61392b5d3046caf557602.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/a590acd76969f996ec6e4b599c3c09f58c283a76f2d61392b5d3046caf557602.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/a590acd76969f996ec6e4b599c3c09f58c283a76f2d61392b5d3046caf557602.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/r2/miniflare-R2BucketObject/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/r2/miniflare-R2BucketObject/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/r2/miniflare-R2BucketObject/metadata.sqlite-wal +0 -0
- package/workers/test/evaluate-host.ts +22 -0
- package/workers/test/wrangler.jsonc +7 -0
- package/workers/types.ts +44 -0
- package/README.md +0 -3
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { WorkerEntrypoint } from "cloudflare:workers";
|
|
2
|
+
import type {
|
|
3
|
+
BuildCheck,
|
|
4
|
+
ChannelContext,
|
|
5
|
+
Delivery,
|
|
6
|
+
FactoryStatus,
|
|
7
|
+
} from "@notation/factory/router";
|
|
8
|
+
import { channel } from "factory:channel";
|
|
9
|
+
import type { SecretsStoreSecret } from "./types.ts";
|
|
10
|
+
|
|
11
|
+
type Env = Record<string, unknown>;
|
|
12
|
+
|
|
13
|
+
// The secrets and values that the connection declared are bound under their
|
|
14
|
+
// own names.
|
|
15
|
+
const context = (env: Env): ChannelContext => ({
|
|
16
|
+
secret: async (name) => {
|
|
17
|
+
const binding = env[name] as SecretsStoreSecret | undefined;
|
|
18
|
+
if (!binding) throw new Error(`no secret bound as ${name}`);
|
|
19
|
+
return binding.get();
|
|
20
|
+
},
|
|
21
|
+
value: (name) => {
|
|
22
|
+
const value = env[name];
|
|
23
|
+
if (typeof value !== "string") throw new Error(`no value bound as ${name}`);
|
|
24
|
+
return value;
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
/** One connection's channel, which the router calls over its service binding. */
|
|
29
|
+
export default class ChannelWorker extends WorkerEntrypoint<Env> {
|
|
30
|
+
// Cloudflare refuses a Worker whose default export has no event handler,
|
|
31
|
+
// and RPC methods don't count as one.
|
|
32
|
+
fetch() {
|
|
33
|
+
return new Response("not found", { status: 404 });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
receive(request: Request) {
|
|
37
|
+
return channel(context(this.env)).receive(request);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
match(selector: unknown, delivery: Delivery<unknown>) {
|
|
41
|
+
return channel(context(this.env)).match(selector, delivery);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
sync(url: string) {
|
|
45
|
+
return channel(context(this.env)).sync(url);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
read(repo: string, dir: string, ref?: string) {
|
|
49
|
+
const { read } = channel(context(this.env));
|
|
50
|
+
if (!read) throw new Error("This connection's channel can't read repos.");
|
|
51
|
+
return read(repo, dir, ref);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
writeStatus(repo: string, status: FactoryStatus) {
|
|
55
|
+
const { writeStatus } = channel(context(this.env));
|
|
56
|
+
if (!writeStatus) throw new Error("This connection's channel can't write a repo's status.");
|
|
57
|
+
return writeStatus(repo, status);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
writeCheck(repo: string, ref: string, check: BuildCheck) {
|
|
61
|
+
const { writeCheck } = channel(context(this.env));
|
|
62
|
+
if (!writeCheck) throw new Error("This connection's channel can't write checks.");
|
|
63
|
+
return writeCheck(repo, ref, check);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
conversation(delivery: Delivery<unknown>) {
|
|
67
|
+
const { conversation } = channel(context(this.env));
|
|
68
|
+
if (!conversation) throw new Error("This connection's channel can't start runs.");
|
|
69
|
+
return conversation(delivery);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
reply(thread: unknown, text: string) {
|
|
73
|
+
const { reply } = channel(context(this.env));
|
|
74
|
+
if (!reply) throw new Error("This connection's channel can't reply.");
|
|
75
|
+
return reply(thread, text);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
token(repo: string, permissions: Record<string, "read" | "write">) {
|
|
79
|
+
const { token } = channel(context(this.env));
|
|
80
|
+
if (!token) throw new Error("This connection's channel can't mint tokens.");
|
|
81
|
+
return token(repo, permissions);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
declare module "cloudflare:workers" {
|
|
2
|
+
export class WorkerEntrypoint<Env = unknown> {
|
|
3
|
+
constructor(ctx: unknown, env: Env);
|
|
4
|
+
protected env: Env;
|
|
5
|
+
}
|
|
6
|
+
export abstract class DurableObject<Env = unknown> {
|
|
7
|
+
constructor(ctx: DurableObjectState, env: Env);
|
|
8
|
+
protected ctx: DurableObjectState;
|
|
9
|
+
protected env: Env;
|
|
10
|
+
}
|
|
11
|
+
type DurableObjectState = { container?: { running: boolean } } & Record<string, any>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Generated by the router for each channel bundle. It re-exports the
|
|
15
|
+
// provider's channel module.
|
|
16
|
+
declare module "factory:channel" {
|
|
17
|
+
export const channel: import("@notation/factory/router").ChannelModule["channel"];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Generated by the deployer. Its default export is the runner's source.
|
|
21
|
+
declare module "factory:runner" {
|
|
22
|
+
const source: string;
|
|
23
|
+
export default source;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Generated by the deployer. Its default export is Factory's default outpost Dockerfile.
|
|
27
|
+
declare module "factory:dockerfile" {
|
|
28
|
+
const source: string;
|
|
29
|
+
export default source;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare class FixedLengthStream extends TransformStream<Uint8Array, Uint8Array> {
|
|
33
|
+
constructor(length: number);
|
|
34
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { after, before, test } from "node:test";
|
|
5
|
+
import { unstable_startWorker } from "wrangler";
|
|
6
|
+
|
|
7
|
+
const outpostDir = join(import.meta.dirname, "../../../examples/outpost-1/.factory");
|
|
8
|
+
const REPO = "djgrant/notation-factory-test-outpost-1";
|
|
9
|
+
|
|
10
|
+
let worker: Awaited<ReturnType<typeof unstable_startWorker>>;
|
|
11
|
+
|
|
12
|
+
before(async () => {
|
|
13
|
+
worker = await unstable_startWorker({
|
|
14
|
+
config: join(import.meta.dirname, "test/wrangler.jsonc"),
|
|
15
|
+
dev: { server: { port: 0 }, inspector: false, logLevel: "error" },
|
|
16
|
+
});
|
|
17
|
+
await worker.ready;
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
after(() => worker?.dispose());
|
|
21
|
+
|
|
22
|
+
const evaluate = async (files: Record<string, string>) => {
|
|
23
|
+
const response = await worker.fetch("http://deployer/", {
|
|
24
|
+
method: "POST",
|
|
25
|
+
body: JSON.stringify({ repo: REPO, files }),
|
|
26
|
+
});
|
|
27
|
+
return (await response.json()) as { outpost?: any; error?: string };
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const exampleFiles = () =>
|
|
31
|
+
Object.fromEntries(
|
|
32
|
+
readdirSync(outpostDir)
|
|
33
|
+
.filter((file) => file.endsWith(".ts"))
|
|
34
|
+
.map((file) => [file, readFileSync(join(outpostDir, file), "utf8")]),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
test("evaluates an outpost's config in a loaded Worker", async () => {
|
|
38
|
+
const { outpost, error } = await evaluate(exampleFiles());
|
|
39
|
+
assert.equal(error, undefined);
|
|
40
|
+
assert.deepEqual(outpost.channels.issues, {
|
|
41
|
+
provider: "github",
|
|
42
|
+
connection: "gh",
|
|
43
|
+
scope: REPO,
|
|
44
|
+
});
|
|
45
|
+
assert.deepEqual(outpost.containers.dev.runtimes, ["claude", "droid"]);
|
|
46
|
+
assert.equal(outpost.agents["droid-coder"]?.runtime, "droid");
|
|
47
|
+
assert.equal(outpost.triggers["droid-mention"]?.agent, "droid-coder");
|
|
48
|
+
assert.equal(outpost.agents.coder.runtime, "claude");
|
|
49
|
+
assert.equal(outpost.triggers.mention.event, "issue_comment");
|
|
50
|
+
assert.equal(outpost.triggers.mention.agent, "coder");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("resolves imports between the config's own files", async () => {
|
|
54
|
+
const { outpost, error } = await evaluate({
|
|
55
|
+
"config.ts": `import { Policy } from "@notation/factory-github";\nimport { id } from "./lib/ids.ts";\nnew Policy(id, { permissions: { contents: "read" } });`,
|
|
56
|
+
"lib/ids.ts": `export const id: string = "read";`,
|
|
57
|
+
});
|
|
58
|
+
assert.equal(error, undefined);
|
|
59
|
+
assert.equal(outpost.policies.read.provider, "github");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("returns the config's error", async () => {
|
|
63
|
+
const { error } = await evaluate({ "config.ts": `throw new Error("bad config");` });
|
|
64
|
+
assert.match(error!, /bad config/);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("gives the config no network", async () => {
|
|
68
|
+
const { error } = await evaluate({
|
|
69
|
+
"config.ts": `await fetch("https://example.com");`,
|
|
70
|
+
});
|
|
71
|
+
assert.ok(error);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("refuses a hub-only export", async () => {
|
|
75
|
+
const { error } = await evaluate({
|
|
76
|
+
"config.ts": `import { RandomKey } from "@notation/factory";\nnew RandomKey("k", { config: {} });`,
|
|
77
|
+
});
|
|
78
|
+
assert.match(error!, /RandomKey/);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const branch = async (trigger: string, payload: unknown) => {
|
|
82
|
+
const response = await worker.fetch("http://deployer/", {
|
|
83
|
+
method: "POST",
|
|
84
|
+
body: JSON.stringify({ repo: REPO, files: exampleFiles(), trigger, payload }),
|
|
85
|
+
});
|
|
86
|
+
return (await response.json()) as { branch?: string | null; error?: string };
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
test("calls a trigger's worktree function with the event, as often as it's asked", async () => {
|
|
90
|
+
assert.deepEqual(await branch("mention", { issue: { number: 5 } }), { branch: "issue-5" });
|
|
91
|
+
assert.deepEqual(await branch("mention", { issue: { number: 6 } }), { branch: "issue-6" });
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("a trigger the config doesn't declare is an error", async () => {
|
|
95
|
+
const { error } = await branch("missing", {});
|
|
96
|
+
assert.match(error!, /no trigger missing/);
|
|
97
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { WorkerEntrypoint } from "cloudflare:workers";
|
|
2
|
+
import { deploy, evaluateOutpost, worktree } from "@notation/factory/deployer";
|
|
3
|
+
import { type OutpostConfig, outpostKey, type Run, type SourceFiles } from "@notation/factory/router";
|
|
4
|
+
import type { BuildWatch } from "./build-watch.ts";
|
|
5
|
+
import { loaderContext } from "./loader.ts";
|
|
6
|
+
import type { DeployerService } from "./outpost-api.ts";
|
|
7
|
+
import { deployOutposts, type OutpostsEnv, postRun, verifyRun } from "./outposts.ts";
|
|
8
|
+
import type { DurableObjectNamespace, R2Bucket, SecretsStoreSecret, WorkerLoader } from "./types.ts";
|
|
9
|
+
|
|
10
|
+
type Env = OutpostsEnv & {
|
|
11
|
+
LOADER: WorkerLoader;
|
|
12
|
+
FACTORY_BUCKET: R2Bucket;
|
|
13
|
+
BUILDS: DurableObjectNamespace<BuildWatch>;
|
|
14
|
+
} & Record<string, unknown>;
|
|
15
|
+
|
|
16
|
+
/** The KV key of the `.factory/` files that a repo's outpost was deployed from. */
|
|
17
|
+
const filesKey = (repo: string) => `outpost-files/${repo.toLowerCase()}`;
|
|
18
|
+
|
|
19
|
+
/** The bucket key of an object in a container's state store. */
|
|
20
|
+
const stateKey = (container: string, key: string) => `outposts/${container}/${key}`;
|
|
21
|
+
|
|
22
|
+
/** The hub's deployer, which the router calls over its service binding. */
|
|
23
|
+
export default class DeployerWorker extends WorkerEntrypoint<Env> implements DeployerService {
|
|
24
|
+
// Cloudflare refuses a Worker whose default export has no event handler,
|
|
25
|
+
// and RPC methods don't count as one.
|
|
26
|
+
fetch() {
|
|
27
|
+
return new Response("not found", { status: 404 });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
#context() {
|
|
31
|
+
const kv = this.env.FACTORY_KV;
|
|
32
|
+
return {
|
|
33
|
+
...loaderContext(this.env.LOADER),
|
|
34
|
+
putOutpost: async (repo: string, outpost: OutpostConfig, files: Record<string, string>, ref: string) => {
|
|
35
|
+
const started = await deployOutposts(this.env, repo, outpost, files);
|
|
36
|
+
await kv.put(filesKey(repo), JSON.stringify(files));
|
|
37
|
+
await kv.put(outpostKey(repo), JSON.stringify(outpost));
|
|
38
|
+
if (started.length) {
|
|
39
|
+
const watch = this.env.BUILDS.get(this.env.BUILDS.idFromName("builds"));
|
|
40
|
+
await watch.watch(started.map((build) => ({ ...build, repo, ref })));
|
|
41
|
+
}
|
|
42
|
+
return started.map((build) => build.container);
|
|
43
|
+
},
|
|
44
|
+
getFiles: async (repo: string) =>
|
|
45
|
+
((await kv.get(filesKey(repo), "json")) ?? undefined) as Record<string, string> | undefined,
|
|
46
|
+
deleteOutpost: async (repo: string) => {
|
|
47
|
+
await deployOutposts(this.env, repo, undefined, {});
|
|
48
|
+
await kv.delete(outpostKey(repo));
|
|
49
|
+
await kv.delete(filesKey(repo));
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Evaluates a repo's `.factory/` files, keyed by their path in `.factory/`, and returns its outpost config. */
|
|
55
|
+
evaluate(repo: string, files: Record<string, string>) {
|
|
56
|
+
return evaluateOutpost(repo, files, this.#context());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
deploy(repo: string, source: SourceFiles) {
|
|
60
|
+
return deploy(repo, source, this.#context());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
remove(repo: string) {
|
|
64
|
+
return this.#context().deleteOutpost(repo);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
worktree(repo: string, trigger: string, payload: unknown) {
|
|
68
|
+
return worktree(repo, trigger, payload, this.#context());
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Hands `run` to the outpost Worker of its repo and container id. */
|
|
72
|
+
run(run: Run) {
|
|
73
|
+
return postRun(this.env, run);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
verifyRun(worker: string, timestamp: string, body: string, signature: string) {
|
|
77
|
+
return verifyRun(this.env, worker, timestamp, body, signature);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async credentials(runtimes: string[]) {
|
|
81
|
+
const env: Record<string, string> = {};
|
|
82
|
+
for (const runtime of runtimes) {
|
|
83
|
+
const account = this.env.ACCOUNTS[runtime];
|
|
84
|
+
if (!account) throw new Error(`The hub has no account for the runtime ${runtime}.`);
|
|
85
|
+
for (const name of account.secrets) env[name] = await (this.env[name] as SecretsStoreSecret).get();
|
|
86
|
+
}
|
|
87
|
+
return env;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async getState(container: string, key: string) {
|
|
91
|
+
return (await this.env.FACTORY_BUCKET.get(stateKey(container, key)))?.body ?? null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// A stream that comes over RPC has no length, and R2 needs one.
|
|
95
|
+
async putState(container: string, key: string, body: ReadableStream | null, length: number) {
|
|
96
|
+
if (!body) {
|
|
97
|
+
await this.env.FACTORY_BUCKET.put(stateKey(container, key), null);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const { readable, writable } = new FixedLengthStream(length);
|
|
101
|
+
const piped = body.pipeTo(writable);
|
|
102
|
+
await Promise.all([this.env.FACTORY_BUCKET.put(stateKey(container, key), readable), piped]);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export { BuildWatch } from "./build-watch.ts";
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { DeployerContext, Program } from "@notation/factory/deployer";
|
|
2
|
+
import type { WorkerLoader } from "./types.ts";
|
|
3
|
+
|
|
4
|
+
const COMPATIBILITY_DATE = "2026-10-12";
|
|
5
|
+
|
|
6
|
+
/** The module that the loader starts: it calls the program's main function with the request's JSON body, and answers with its result or its error. */
|
|
7
|
+
const HOST = "factory-host.js";
|
|
8
|
+
|
|
9
|
+
const hostModule = (main: string) =>
|
|
10
|
+
[
|
|
11
|
+
`import run from "./${main}";`,
|
|
12
|
+
`export default {`,
|
|
13
|
+
` async fetch(request) {`,
|
|
14
|
+
` try {`,
|
|
15
|
+
` return Response.json({ ok: true, value: await run(await request.json()) });`,
|
|
16
|
+
` } catch (error) {`,
|
|
17
|
+
` return Response.json({ ok: false, error: error instanceof Error ? error.stack ?? error.message : String(error) });`,
|
|
18
|
+
` }`,
|
|
19
|
+
` },`,
|
|
20
|
+
`};`,
|
|
21
|
+
].join("\n");
|
|
22
|
+
|
|
23
|
+
const sha256 = async (text: string) => {
|
|
24
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
|
|
25
|
+
return Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Runs programs with the Worker Loader. The loader keeps an isolate for each
|
|
30
|
+
* id. An isolate imports a config's modules once, and a config declares its
|
|
31
|
+
* resources only when it's imported, so each call gets an id of its own.
|
|
32
|
+
*/
|
|
33
|
+
export const loaderContext = (
|
|
34
|
+
loader: WorkerLoader,
|
|
35
|
+
): Pick<DeployerContext, "evaluate"> => ({
|
|
36
|
+
async evaluate(program: Program) {
|
|
37
|
+
const modules: Record<string, { js: string }> = {
|
|
38
|
+
[HOST]: { js: hostModule(program.main) },
|
|
39
|
+
};
|
|
40
|
+
for (const [name, js] of Object.entries(program.modules)) modules[name] = { js };
|
|
41
|
+
const id = `${await sha256(JSON.stringify({ main: program.main, modules: program.modules }))}:${crypto.randomUUID()}`;
|
|
42
|
+
const worker = loader.get(id, () => ({
|
|
43
|
+
compatibilityDate: COMPATIBILITY_DATE,
|
|
44
|
+
compatibilityFlags: ["nodejs_compat"],
|
|
45
|
+
mainModule: HOST,
|
|
46
|
+
modules,
|
|
47
|
+
globalOutbound: null,
|
|
48
|
+
env: {},
|
|
49
|
+
}));
|
|
50
|
+
const response = await worker
|
|
51
|
+
.getEntrypoint()
|
|
52
|
+
.fetch(new Request("http://outpost/", { method: "POST", body: JSON.stringify(program.input ?? null) }));
|
|
53
|
+
const result = (await response.json()) as
|
|
54
|
+
| { ok: true; value: unknown }
|
|
55
|
+
| { ok: false; error: string };
|
|
56
|
+
if (!result.ok) throw new Error(result.error);
|
|
57
|
+
return result.value;
|
|
58
|
+
},
|
|
59
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** What an outpost Worker calls on the hub's deployer, over its service binding. */
|
|
2
|
+
export type DeployerService = {
|
|
3
|
+
/** Whether `signature` is the deployer's signature of a run for the outpost Worker `worker`. */
|
|
4
|
+
verifyRun(worker: string, timestamp: string, body: string, signature: string): Promise<boolean>;
|
|
5
|
+
/** The secrets of the accounts of `runtimes`, by the env var names that containers read them as. */
|
|
6
|
+
credentials(runtimes: string[]): Promise<Record<string, string>>;
|
|
7
|
+
getState(container: string, key: string): Promise<ReadableStream | null>;
|
|
8
|
+
/** Stores `length` bytes of `body`. */
|
|
9
|
+
putState(container: string, key: string, body: ReadableStream | null, length: number): Promise<void>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** The headers that the deployer signs a run with. */
|
|
13
|
+
export const RUN_HEADERS = { timestamp: "x-factory-timestamp", signature: "x-factory-signature" } as const;
|
|
14
|
+
|
|
15
|
+
/** The class of an outpost Worker's container, which its `wrangler.json` names. */
|
|
16
|
+
export const CONTAINER_CLASS = "OutpostContainer";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as v from "valibot";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* How the deployer builds outpost Workers. `cloudflare.Deployer` writes it
|
|
5
|
+
* to KV rather than binding it, since it holds the deployer Worker's
|
|
6
|
+
* generated name.
|
|
7
|
+
*/
|
|
8
|
+
export const OutpostSettingsSchema = v.object({
|
|
9
|
+
accountId: v.string(),
|
|
10
|
+
/** The Artifacts namespace that holds each outpost Worker's source. */
|
|
11
|
+
namespace: v.string(),
|
|
12
|
+
/** The start of each outpost Worker's name. */
|
|
13
|
+
prefix: v.string(),
|
|
14
|
+
/** The deployer Worker's name, which outpost Workers bind as a service. */
|
|
15
|
+
deployer: v.string(),
|
|
16
|
+
/** The router's URL, which the deployer reports builds to. */
|
|
17
|
+
router: v.string(),
|
|
18
|
+
compatibilityDate: v.string(),
|
|
19
|
+
sleepAfter: v.string(),
|
|
20
|
+
regions: v.optional(v.array(v.string())),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export type OutpostSettings = v.InferOutput<typeof OutpostSettingsSchema>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { imageSource } from "./outpost-source.ts";
|
|
4
|
+
|
|
5
|
+
const DOCKERFILE = "FROM oven/bun:1-debian\n# factory:runtimes\nUSER factory\n";
|
|
6
|
+
const accounts = { claude: { setup: "install claude\n" }, droid: { setup: "install droid\n" } };
|
|
7
|
+
const container = { runtimes: ["claude"], policies: [], files: {} };
|
|
8
|
+
|
|
9
|
+
test("a container with no image gets the default Dockerfile with its runtimes' setups", () => {
|
|
10
|
+
const { source, image, context } = imageSource(container, {}, accounts, DOCKERFILE);
|
|
11
|
+
assert.equal(image, "./Dockerfile");
|
|
12
|
+
assert.equal(context, ".");
|
|
13
|
+
assert.equal(source["factory-runtimes/claude.sh"], "install claude\n");
|
|
14
|
+
assert.match(source.Dockerfile!, /COPY factory-runtimes \/tmp\/factory-runtimes/);
|
|
15
|
+
assert.doesNotMatch(source.Dockerfile!, /# factory:runtimes/);
|
|
16
|
+
assert.match(source.Dockerfile!, /USER factory/);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("a container with no runtimes gets the default Dockerfile with no setups", () => {
|
|
20
|
+
const { source } = imageSource({ ...container, runtimes: [] }, {}, accounts, DOCKERFILE);
|
|
21
|
+
assert.match(source.Dockerfile!, /lists no runtimes/);
|
|
22
|
+
assert.equal(source["factory-runtimes/claude.sh"], undefined);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("a container with its own image gets the setups in its .factory context", () => {
|
|
26
|
+
const { source, image, context } = imageSource(
|
|
27
|
+
{ ...container, image: "./Dockerfile" },
|
|
28
|
+
{ Dockerfile: "FROM oven/bun:1-debian" },
|
|
29
|
+
accounts,
|
|
30
|
+
DOCKERFILE,
|
|
31
|
+
);
|
|
32
|
+
assert.equal(image, "./.factory/Dockerfile");
|
|
33
|
+
assert.equal(context, "./.factory");
|
|
34
|
+
assert.equal(source[".factory/factory-runtimes/claude.sh"], "install claude\n");
|
|
35
|
+
assert.equal(source.Dockerfile, undefined);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("a container with no image and a runtime that has no setup fails its deploy", () => {
|
|
39
|
+
assert.throws(() => imageSource(container, {}, {}, DOCKERFILE), /no setup for the runtime claude/);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("a default Dockerfile with no marker fails its deploy", () => {
|
|
43
|
+
assert.throws(() => imageSource(container, {}, accounts, "FROM oven/bun:1-debian\n"), /no `# factory:runtimes` line/);
|
|
44
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { OutpostConfig } from "@notation/factory/router";
|
|
2
|
+
|
|
3
|
+
/** A runtime's account, as the deployer binds it. `setup` is the script that installs the runtime's CLI. */
|
|
4
|
+
export type RuntimeAccount = { setup?: string };
|
|
5
|
+
|
|
6
|
+
/** Where the default image's Dockerfile takes the container's runtimes' setups. */
|
|
7
|
+
const MARKER = "# factory:runtimes";
|
|
8
|
+
|
|
9
|
+
/** The layer that runs every runtime's setup, as root, before the image's user. */
|
|
10
|
+
const RUNTIMES_LAYER = `# The container's runtimes' setups, from the hub's plugins.
|
|
11
|
+
COPY factory-runtimes /tmp/factory-runtimes
|
|
12
|
+
RUN for setup in /tmp/factory-runtimes/*.sh; do sh "$setup"; done \\
|
|
13
|
+
&& rm -rf /tmp/factory-runtimes`;
|
|
14
|
+
|
|
15
|
+
/** The default image's Dockerfile, with the container's runtimes' setups at its marker. */
|
|
16
|
+
const withRuntimes = (dockerfile: string, runtimes: number) => {
|
|
17
|
+
if (!dockerfile.includes(MARKER)) throw new Error(`The default image's Dockerfile has no \`${MARKER}\` line.`);
|
|
18
|
+
return dockerfile.replace(MARKER, runtimes === 0 ? "# The container lists no runtimes." : RUNTIMES_LAYER);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The files the deployer adds to a container's build context for its image,
|
|
23
|
+
* and where the build finds the Dockerfile:
|
|
24
|
+
* - A container with no `image` gets the default Dockerfile, with each of
|
|
25
|
+
* its runtimes' setups at the marker, as `factory-runtimes/<id>.sh`.
|
|
26
|
+
* - A container with an `image` gets the setups under its `.factory/`
|
|
27
|
+
* context, and its Dockerfile runs them with the contract's two lines.
|
|
28
|
+
*/
|
|
29
|
+
export const imageSource = (
|
|
30
|
+
container: OutpostConfig["containers"][string],
|
|
31
|
+
files: Record<string, string>,
|
|
32
|
+
accounts: Record<string, RuntimeAccount>,
|
|
33
|
+
defaultDockerfile: string,
|
|
34
|
+
) => {
|
|
35
|
+
const source: Record<string, string> = {};
|
|
36
|
+
const setups = container.runtimes.map((id) => ({ id, setup: accounts[id]?.setup }));
|
|
37
|
+
let image = "./Dockerfile";
|
|
38
|
+
let context = ".";
|
|
39
|
+
if (container.image) {
|
|
40
|
+
const path = container.image.replace(/^\.\//, "");
|
|
41
|
+
if (!(path in files)) throw new Error(`The image .factory/${path} doesn't exist.`);
|
|
42
|
+
image = `./.factory/${path}`;
|
|
43
|
+
context = "./.factory";
|
|
44
|
+
for (const { id, setup } of setups) {
|
|
45
|
+
if (setup) source[`.factory/factory-runtimes/${id}.sh`] = setup;
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
for (const { id, setup } of setups) {
|
|
49
|
+
if (!setup) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`The hub has no setup for the runtime ${id}. Give the container an image that installs it, or deploy the hub with an account for ${id}.`,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
source[`factory-runtimes/${id}.sh`] = setup;
|
|
55
|
+
}
|
|
56
|
+
source.Dockerfile = withRuntimes(defaultDockerfile, setups.length);
|
|
57
|
+
}
|
|
58
|
+
return { source, image, context };
|
|
59
|
+
};
|