@samebase/alchemy-convex 0.17.0 → 0.19.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/NOTICE +3 -0
- package/README.md +123 -69
- package/lib/{HubClient-BZKweNfD.js → HubClient-C0XxgkK3.js} +42 -7
- package/lib/HubClient-C0XxgkK3.js.map +1 -0
- package/lib/cli.js +215 -9
- package/lib/cli.js.map +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/sealedSecret-Bf8AeIp_.js +86 -0
- package/lib/sealedSecret-Bf8AeIp_.js.map +1 -0
- package/lib/sealedSecret.d.ts +37 -0
- package/lib/sealedSecret.d.ts.map +1 -0
- package/lib/sealedSecret.js +1 -0
- package/package.json +20 -19
- package/src/Project.ts +1 -1
- package/src/Push.ts +1 -1
- package/src/RunRecord.ts +1 -0
- package/src/StackComment.ts +308 -0
- package/src/State.ts +54 -1
- package/src/cli.ts +50 -32
- package/src/store/component/_generated/api.ts +2 -0
- package/src/store/component/_generated/component.ts +85 -1
- package/src/store/component/keys.ts +73 -0
- package/src/store/component/reader.ts +1 -0
- package/src/store/component/runRecord.ts +1 -0
- package/src/store/component/schema.ts +7 -0
- package/src/store/component/state.ts +86 -4
- package/src/store/convex/_generated/api.d.ts +2 -0
- package/src/store/convex/reader.ts +1 -0
- package/src/store/convex/sealing.ts +102 -0
- package/src/store/convex/state.ts +248 -22
- package/src/store/convex/version.ts +3 -1
- package/src/store/sealedSecret.ts +129 -0
- package/lib/HubClient-BZKweNfD.js.map +0 -1
package/lib/cli.js
CHANGED
|
@@ -1,9 +1,207 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { I as ManagementApiLive, M as installedCli, V as fromConfig, a as registerDevelopmentClient } from "./HubClient-
|
|
2
|
+
import { I as ManagementApiLive, M as installedCli, V as fromConfig, a as registerDevelopmentClient, c as RUN_RECORD_KEY, s as state, u as runRecord } from "./HubClient-C0XxgkK3.js";
|
|
3
3
|
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Schema from "effect/Schema";
|
|
4
5
|
import * as Layer from "effect/Layer";
|
|
5
6
|
import { parseArgs } from "node:util";
|
|
7
|
+
import { State } from "alchemy/State";
|
|
6
8
|
import { execFileSync } from "node:child_process";
|
|
9
|
+
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
10
|
+
//#region src/StackComment.ts
|
|
11
|
+
/** The first text of the comment, which finds it again. */
|
|
12
|
+
const MARKER = "<!-- samebase-stacks -->";
|
|
13
|
+
/** GitHub does not proxy this host, so a new icon needs no purge of its image cache. */
|
|
14
|
+
const ICONS = "https://raw.githubusercontent.com/samebase/samebase/main/packages/alchemy-convex/assets/stack-comment";
|
|
15
|
+
const GITHUB_API = "https://api.github.com";
|
|
16
|
+
/** The resources that a card shows. A row of any other type, or without `attr` from an interrupted deploy, is left out. */
|
|
17
|
+
const Resource = Schema.Union([
|
|
18
|
+
Schema.Struct({
|
|
19
|
+
resourceType: Schema.Literal("Convex.Deployment"),
|
|
20
|
+
attr: Schema.Struct({
|
|
21
|
+
name: Schema.String,
|
|
22
|
+
siteUrl: Schema.String
|
|
23
|
+
})
|
|
24
|
+
}),
|
|
25
|
+
Schema.Struct({
|
|
26
|
+
resourceType: Schema.Literal("Cloudflare.Worker"),
|
|
27
|
+
attr: Schema.Struct({
|
|
28
|
+
workerName: Schema.String,
|
|
29
|
+
url: Schema.optional(Schema.String)
|
|
30
|
+
})
|
|
31
|
+
}),
|
|
32
|
+
Schema.Struct({
|
|
33
|
+
resourceType: Schema.Literal("Cloudflare.R2.Bucket"),
|
|
34
|
+
attr: Schema.Struct({
|
|
35
|
+
bucketName: Schema.String,
|
|
36
|
+
accountId: Schema.String,
|
|
37
|
+
jurisdiction: Schema.String
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
]);
|
|
41
|
+
/** The fields of the run record that a card shows. */
|
|
42
|
+
const StageOutput = Schema.Struct({ [RUN_RECORD_KEY]: Schema.Struct({
|
|
43
|
+
headCommit: Schema.String,
|
|
44
|
+
runUrl: Schema.NullOr(Schema.String),
|
|
45
|
+
recordedAt: Schema.String
|
|
46
|
+
}) });
|
|
47
|
+
var StackCommentError = class extends Schema.TaggedError()("StackCommentError", { reason: Schema.String }) {
|
|
48
|
+
get message() {
|
|
49
|
+
return `The stack comment cannot be updated: ${this.reason}`;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
/** The cards of the stacks of `repository` that have `stage`, in name order. */
|
|
53
|
+
const readStackCards = ({ repository, stage }) => Effect.gen(function* () {
|
|
54
|
+
const state = yield* yield* State;
|
|
55
|
+
const names = (yield* state.listStacks()).filter((name) => name === repository || name.startsWith(`${repository}/`));
|
|
56
|
+
const cards = [];
|
|
57
|
+
for (const name of names) {
|
|
58
|
+
if (!(yield* state.listStages(name)).includes(stage)) continue;
|
|
59
|
+
const request = {
|
|
60
|
+
stack: name,
|
|
61
|
+
stage
|
|
62
|
+
};
|
|
63
|
+
const rows = yield* Effect.forEach(yield* state.list(request), (fqn) => state.get({
|
|
64
|
+
...request,
|
|
65
|
+
fqn
|
|
66
|
+
}));
|
|
67
|
+
const output = yield* state.getOutput(request);
|
|
68
|
+
cards.push({
|
|
69
|
+
name,
|
|
70
|
+
stage,
|
|
71
|
+
resources: rows.filter(Schema.is(Resource)),
|
|
72
|
+
run: Schema.is(StageOutput)(output) ? output : null
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return cards;
|
|
76
|
+
});
|
|
77
|
+
/** A stack can create a deployment in one resource and push to it in another: a card shows it once. */
|
|
78
|
+
const withoutRepeats = (resources) => {
|
|
79
|
+
const deployments = /* @__PURE__ */ new Set();
|
|
80
|
+
return resources.filter((resource) => {
|
|
81
|
+
if (resource.resourceType !== "Convex.Deployment") return true;
|
|
82
|
+
const repeated = deployments.has(resource.attr.name);
|
|
83
|
+
deployments.add(resource.attr.name);
|
|
84
|
+
return !repeated;
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
const escapeHtml = (text) => text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """);
|
|
88
|
+
const link = (href, text) => `<a href="${escapeHtml(href)}">${text}</a>`;
|
|
89
|
+
const named = (href, icon, alt, name) => {
|
|
90
|
+
const image = `<img src="${ICONS}/${icon}.svg" width="16" align="absmiddle" alt="${alt}">`;
|
|
91
|
+
return `${link(href, image)} ${link(href, escapeHtml(name))}`;
|
|
92
|
+
};
|
|
93
|
+
const hostOf = (url) => escapeHtml(new URL(url).host);
|
|
94
|
+
/** Where a stack serves: its Convex deployment, else its first Worker with a URL. */
|
|
95
|
+
const addressOf = (resources) => {
|
|
96
|
+
for (const resource of resources) if (resource.resourceType === "Convex.Deployment") return resource.attr.siteUrl;
|
|
97
|
+
for (const resource of resources) if (resource.resourceType === "Cloudflare.Worker" && resource.attr.url !== void 0) return resource.attr.url;
|
|
98
|
+
return null;
|
|
99
|
+
};
|
|
100
|
+
const resourceRow = (resource, address) => {
|
|
101
|
+
switch (resource.resourceType) {
|
|
102
|
+
case "Convex.Deployment": {
|
|
103
|
+
const { name } = resource.attr;
|
|
104
|
+
return named(`https://dashboard.convex.dev/d/${encodeURIComponent(name)}`, "convex", "Convex", name);
|
|
105
|
+
}
|
|
106
|
+
case "Cloudflare.Worker": {
|
|
107
|
+
const { url, workerName } = resource.attr;
|
|
108
|
+
const row = named(`https://dash.cloudflare.com/?to=/:account/workers/services/view/${encodeURIComponent(workerName)}`, "worker", "Worker", workerName);
|
|
109
|
+
return url === void 0 || url === address ? row : `${row}<br><sub>${link(url, hostOf(url))}</sub>`;
|
|
110
|
+
}
|
|
111
|
+
case "Cloudflare.R2.Bucket": {
|
|
112
|
+
const { accountId, bucketName, jurisdiction } = resource.attr;
|
|
113
|
+
return named(`https://dash.cloudflare.com/${encodeURIComponent(accountId)}/r2/${encodeURIComponent(jurisdiction)}/buckets/${encodeURIComponent(bucketName)}`, "r2", "R2 bucket", bucketName);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const runLine = ({ run }) => {
|
|
118
|
+
if (run === null) return "";
|
|
119
|
+
const { headCommit, recordedAt, runUrl } = run[RUN_RECORD_KEY];
|
|
120
|
+
const commit = escapeHtml(headCommit.slice(0, 7));
|
|
121
|
+
const time = `${recordedAt.slice(0, 16).replace("T", " ")} UTC`;
|
|
122
|
+
return `<br><sub>${runUrl === null ? commit : link(runUrl, commit)} ${escapeHtml(time)}</sub>`;
|
|
123
|
+
};
|
|
124
|
+
const SPACER = `<div><img src="${ICONS}/spacer.svg" width="460" align="left" alt=""></div>`;
|
|
125
|
+
/** `alone`: the one card of the repository. A card at the root then has no folder, as on the repository page. */
|
|
126
|
+
const renderCard = (card, repository, alone) => {
|
|
127
|
+
const folder = card.name.slice(repository.length + 1);
|
|
128
|
+
const heading = [folder === "" && alone ? "" : `<b>${escapeHtml(folder || "/")}</b> `, `<sub>${escapeHtml(card.stage)}</sub>`].join("");
|
|
129
|
+
const resources = withoutRepeats(card.resources);
|
|
130
|
+
const address = addressOf(resources);
|
|
131
|
+
const addressLine = address === null ? "" : `<br>${link(address, hostOf(address))}`;
|
|
132
|
+
const rows = resources.map((resource) => `<tbody><tr><td>${resourceRow(resource, address)}</td></tr></tbody>`).join("\n");
|
|
133
|
+
return `<table>
|
|
134
|
+
<tbody><tr><td>${SPACER}${heading}${addressLine}${runLine(card)}</td></tr></tbody>
|
|
135
|
+
${rows}
|
|
136
|
+
</table>`;
|
|
137
|
+
};
|
|
138
|
+
const renderStackComment = (cards, repository) => `${MARKER}
|
|
139
|
+
${cards.map((card) => renderCard(card, repository, cards.length === 1)).join("\n\n")}
|
|
140
|
+
`;
|
|
141
|
+
const Comments = Schema.Array(Schema.Struct({
|
|
142
|
+
id: Schema.Number,
|
|
143
|
+
body: Schema.String
|
|
144
|
+
}));
|
|
145
|
+
const SavedComment = Schema.Struct({ html_url: Schema.String });
|
|
146
|
+
const github = (token, path, init) => Effect.tryPromise({
|
|
147
|
+
try: async () => {
|
|
148
|
+
const response = await fetch(`${GITHUB_API}${path}`, {
|
|
149
|
+
...init,
|
|
150
|
+
headers: {
|
|
151
|
+
Accept: "application/vnd.github+json",
|
|
152
|
+
Authorization: `Bearer ${token}`,
|
|
153
|
+
"Content-Type": "application/json",
|
|
154
|
+
"X-GitHub-Api-Version": "2022-11-28"
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
158
|
+
return await response.json();
|
|
159
|
+
},
|
|
160
|
+
catch: (error) => new StackCommentError({ reason: `GitHub ${init.method} ${path} failed (${error instanceof Error ? error.message : String(error)})` })
|
|
161
|
+
});
|
|
162
|
+
const decode = (schema, what) => (value) => Schema.decodeUnknownEffect(schema)(value).pipe(Effect.mapError(() => new StackCommentError({ reason: `GitHub gave a bad ${what}` })));
|
|
163
|
+
const findComment = (token, path) => Effect.gen(function* () {
|
|
164
|
+
for (let page = 1;; page += 1) {
|
|
165
|
+
const comments = yield* github(token, `${path}?per_page=100&page=${page}`, { method: "GET" }).pipe(Effect.flatMap(decode(Comments, "comment list")));
|
|
166
|
+
const comment = comments.find(({ body }) => body.startsWith(MARKER));
|
|
167
|
+
if (comment !== void 0 || comments.length < 100) return comment;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
/** Replaces the body of the comment of this command, or posts it on the first run, and gives its URL. */
|
|
171
|
+
const saveStackComment = (args) => Effect.gen(function* () {
|
|
172
|
+
const { body, pullRequest, repository, token } = args;
|
|
173
|
+
const comments = `/repos/${repository}/issues/${pullRequest}/comments`;
|
|
174
|
+
const comment = yield* findComment(token, comments);
|
|
175
|
+
const reply = yield* comment === void 0 ? github(token, comments, {
|
|
176
|
+
method: "POST",
|
|
177
|
+
body: JSON.stringify({ body })
|
|
178
|
+
}) : github(token, `/repos/${repository}/issues/comments/${comment.id}`, {
|
|
179
|
+
method: "PATCH",
|
|
180
|
+
body: JSON.stringify({ body })
|
|
181
|
+
});
|
|
182
|
+
return (yield* decode(SavedComment, "comment")(reply)).html_url;
|
|
183
|
+
});
|
|
184
|
+
/** The step of a workflow: the run record names the repository and the pull request of the run. */
|
|
185
|
+
const updateStackComment = Effect.gen(function* () {
|
|
186
|
+
const { pullRequest, repository } = yield* runRecord({
|
|
187
|
+
cwd: process.cwd(),
|
|
188
|
+
env: process.env
|
|
189
|
+
});
|
|
190
|
+
if (pullRequest === null) return yield* new StackCommentError({ reason: "this run has no pull request" });
|
|
191
|
+
const token = process.env["GITHUB_TOKEN"];
|
|
192
|
+
if (token === void 0 || token === "") return yield* new StackCommentError({ reason: "GITHUB_TOKEN is not set" });
|
|
193
|
+
const cards = yield* readStackCards({
|
|
194
|
+
repository,
|
|
195
|
+
stage: `pr-${pullRequest}`
|
|
196
|
+
});
|
|
197
|
+
return yield* saveStackComment({
|
|
198
|
+
token,
|
|
199
|
+
repository,
|
|
200
|
+
pullRequest,
|
|
201
|
+
body: renderStackComment(cards, repository)
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
//#endregion
|
|
7
205
|
//#region src/cli.ts
|
|
8
206
|
const { positionals, values } = parseArgs({
|
|
9
207
|
allowPositionals: true,
|
|
@@ -14,16 +212,13 @@ const { positionals, values } = parseArgs({
|
|
|
14
212
|
}
|
|
15
213
|
});
|
|
16
214
|
const { team, app, "callback-path": callbackPath } = values;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const program = Effect.gen(function* () {
|
|
215
|
+
const USAGE = `Usage:
|
|
216
|
+
alchemy-convex hub-client --team <team> --app <app> --callback-path <path>
|
|
217
|
+
alchemy-convex stack-comment [--team <team>]`;
|
|
218
|
+
const hubClient = (hub) => Effect.gen(function* () {
|
|
22
219
|
const [command, ...prefix] = yield* installedCli(process.cwd());
|
|
23
220
|
const callbackUrl = yield* registerDevelopmentClient({
|
|
24
|
-
|
|
25
|
-
app,
|
|
26
|
-
callbackPath,
|
|
221
|
+
...hub,
|
|
27
222
|
convex: (args, input) => execFileSync(command, [...prefix, ...args], {
|
|
28
223
|
encoding: "utf8",
|
|
29
224
|
input,
|
|
@@ -36,6 +231,17 @@ const program = Effect.gen(function* () {
|
|
|
36
231
|
});
|
|
37
232
|
console.log(callbackUrl === void 0 ? "Hub client already registered." : `Hub client registered for ${callbackUrl}.`);
|
|
38
233
|
}).pipe(Effect.provide(ManagementApiLive().pipe(Layer.provide(fromConfig()))));
|
|
234
|
+
const stackComment = (options) => updateStackComment.pipe(Effect.tap((url) => Effect.sync(() => console.log(url))), Effect.provide(state(options).pipe(Layer.provide(NodeServices.layer))));
|
|
235
|
+
const subcommand = positionals.join(" ");
|
|
236
|
+
const program = subcommand === "stack-comment" ? stackComment(team === void 0 ? {} : { team }) : subcommand === "hub-client" && team !== void 0 && app !== void 0 && callbackPath !== void 0 ? hubClient({
|
|
237
|
+
team,
|
|
238
|
+
app,
|
|
239
|
+
callbackPath
|
|
240
|
+
}) : void 0;
|
|
241
|
+
if (program === void 0) {
|
|
242
|
+
console.error(USAGE);
|
|
243
|
+
process.exit(1);
|
|
244
|
+
}
|
|
39
245
|
await Effect.runPromise(program).catch((error) => {
|
|
40
246
|
console.error(error instanceof Error ? error.message : String(error));
|
|
41
247
|
process.exit(1);
|
package/lib/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","names":[],"sources":["../src/cli.ts"],"sourcesContent":["#!/usr/bin/env node\n// The command of the package:\n//\n// alchemy-convex hub-client --team <team> --app <app> --callback-path <path>\n//\n// It runs in the directory of a Convex app, and gives the development deployment of that directory\n// a client of the identity hub of the team: see registerDevelopmentClient. It uses\n// CONVEX_ACCESS_TOKEN or the Convex CLI login.\nimport { execFileSync } from \"node:child_process\";\nimport { parseArgs } from \"node:util\";\nimport * as Effect from \"effect/Effect\";\nimport * as Layer from \"effect/Layer\";\nimport { fromConfig } from \"./Credentials.ts\";\nimport { registerDevelopmentClient } from \"./HubClient.ts\";\nimport { ManagementApiLive } from \"./ManagementApi.ts\";\nimport { installedCli } from \"./Push.ts\";\n\nconst { positionals, values } = parseArgs({\n allowPositionals: true,\n options: {\n team: { type: \"string\" },\n app: { type: \"string\" },\n \"callback-path\": { type: \"string\" },\n },\n});\nconst { team, app, \"callback-path\": callbackPath } = values;\nif (\n positionals.join(\" \") !== \"hub-client\" ||\n team === undefined ||\n app === undefined ||\n callbackPath === undefined\n) {\n console.error(\n \"Usage: alchemy-convex hub-client --team <team> --app <app> --callback-path <path>\",\n );\n process.exit(1);\n}\n\nconst program = Effect.gen(function* () {\n const [command, ...prefix] = yield* installedCli(process.cwd());\n const callbackUrl = yield* registerDevelopmentClient({\n team,\n app,\n callbackPath,\n convex: (args, input) =>\n execFileSync(command, [...prefix, ...args], {\n encoding: \"utf8\",\n input,\n stdio: [\"pipe\", \"pipe\", \"inherit\"],\n }).trim(),\n });\n console.log(\n callbackUrl === undefined\n ? \"Hub client already registered.\"\n : `Hub client registered for ${callbackUrl}.`,\n );\n}).pipe(Effect.provide(ManagementApiLive().pipe(Layer.provide(fromConfig()))));\n\nawait Effect.runPromise(program).catch((error: unknown) => {\n console.error(error instanceof Error ? error.message : String(error));\n process.exit(1);\n});\n"],"mappings":";;;;;;;AAiBA,MAAM,EAAE,aAAa,WAAW,UAAU;CACxC,kBAAkB;CAClB,SAAS;EACP,MAAM,EAAE,MAAM,SAAS;EACvB,KAAK,EAAE,MAAM,SAAS;EACtB,iBAAiB,EAAE,MAAM,SAAS;CACpC;AACF,CAAC;AACD,MAAM,EAAE,MAAM,KAAK,iBAAiB,iBAAiB;AACrD,IACE,YAAY,KAAK,GAAG,MAAM,gBAC1B,SAAS,KAAA,KACT,QAAQ,KAAA,KACR,iBAAiB,KAAA,GACjB;CACA,QAAQ,MACN,mFACF;CACA,QAAQ,KAAK,CAAC;AAChB;AAEA,MAAM,UAAU,OAAO,IAAI,aAAa;CACtC,MAAM,CAAC,SAAS,GAAG,UAAU,OAAO,aAAa,QAAQ,IAAI,CAAC;CAC9D,MAAM,cAAc,OAAO,0BAA0B;EACnD;EACA;EACA;EACA,SAAS,MAAM,UACb,aAAa,SAAS,CAAC,GAAG,QAAQ,GAAG,IAAI,GAAG;GAC1C,UAAU;GACV;GACA,OAAO;IAAC;IAAQ;IAAQ;GAAS;EACnC,CAAC,CAAC,CAAC,KAAK;CACZ,CAAC;CACD,QAAQ,IACN,gBAAgB,KAAA,IACZ,mCACA,6BAA6B,YAAY,EAC/C;AACF,CAAC,CAAC,CAAC,KAAK,OAAO,QAAQ,kBAAkB,CAAC,CAAC,KAAK,MAAM,QAAQ,WAAW,CAAC,CAAC,CAAC,CAAC;AAE7E,MAAM,OAAO,WAAW,OAAO,CAAC,CAAC,OAAO,UAAmB;CACzD,QAAQ,MAAM,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC;CACpE,QAAQ,KAAK,CAAC;AAChB,CAAC"}
|
|
1
|
+
{"version":3,"file":"cli.js","names":[],"sources":["../src/StackComment.ts","../src/cli.ts"],"sourcesContent":["// The command `alchemy-convex stack-comment`: one comment on a pull request with the stacks of the\n// repository, read from the state store after the deploys.\n//\n// A stack sees only itself, so a comment that a stack declares cannot show the other stacks of the\n// repository. This command runs as a step after them and reads each stack named `owner/name` or\n// `owner/name/<folder>` that has the stage `pr-<number>`. A stack without that stage has no card. A\n// run replaces the whole body, so the last run shows the newest state.\n//\n// It reads only the fields of the rows below, because Alchemy state holds secrets in clear text.\n//\n// GitHub keeps a fixed set of HTML in a comment and removes `style` and `class`, so every choice of\n// the markup is a fact about the sanitizer or the CSS of github.com:\n// - A table has `display: block` and `width: max-content`, so a `width` on it does nothing. The\n// spacer, a floated image one pixel high in a block of its own, gives every card one width, and\n// it shrinks on a narrow page.\n// - The CSS colors the even rows of a group, so each row has its own `tbody`.\n// - An HTML block ends at a blank line, so a table has none and is not indented.\n// - A bare image links to its raw file, and one link around an image and text underlines the space\n// between them, so the icon and the name are two links to one place.\n// - An image with `height` gets a gray background, so each icon is square and has only `width`.\n// - `align=\"middle\"` is `-webkit-baseline-middle` in Chrome, which is below the text.\n// `align=\"absmiddle\"` is the CSS value `middle`.\nimport { State } from \"alchemy/State\";\nimport * as Effect from \"effect/Effect\";\nimport * as Schema from \"effect/Schema\";\nimport { RUN_RECORD_KEY, runRecord } from \"./RunRecord.ts\";\n\n/** The first text of the comment, which finds it again. */\nconst MARKER = \"<!-- samebase-stacks -->\";\n\n/** GitHub does not proxy this host, so a new icon needs no purge of its image cache. */\nconst ICONS =\n \"https://raw.githubusercontent.com/samebase/samebase/main/packages/alchemy-convex/assets/stack-comment\";\n\nconst GITHUB_API = \"https://api.github.com\";\n\n/** The resources that a card shows. A row of any other type, or without `attr` from an interrupted deploy, is left out. */\nconst Resource = Schema.Union([\n Schema.Struct({\n resourceType: Schema.Literal(\"Convex.Deployment\"),\n attr: Schema.Struct({ name: Schema.String, siteUrl: Schema.String }),\n }),\n Schema.Struct({\n resourceType: Schema.Literal(\"Cloudflare.Worker\"),\n attr: Schema.Struct({ workerName: Schema.String, url: Schema.optional(Schema.String) }),\n }),\n Schema.Struct({\n resourceType: Schema.Literal(\"Cloudflare.R2.Bucket\"),\n attr: Schema.Struct({\n bucketName: Schema.String,\n accountId: Schema.String,\n jurisdiction: Schema.String,\n }),\n }),\n]);\ntype Resource = typeof Resource.Type;\n\n/** The fields of the run record that a card shows. */\nconst StageOutput = Schema.Struct({\n [RUN_RECORD_KEY]: Schema.Struct({\n headCommit: Schema.String,\n runUrl: Schema.NullOr(Schema.String),\n recordedAt: Schema.String,\n }),\n});\n\nexport interface StackCard {\n readonly name: string;\n readonly stage: string;\n readonly resources: readonly Resource[];\n readonly run: typeof StageOutput.Type | null;\n}\n\nexport class StackCommentError extends Schema.TaggedError<StackCommentError>()(\n \"StackCommentError\",\n { reason: Schema.String },\n) {\n override get message() {\n return `The stack comment cannot be updated: ${this.reason}`;\n }\n}\n\n/** The cards of the stacks of `repository` that have `stage`, in name order. */\nexport const readStackCards = ({ repository, stage }: { repository: string; stage: string }) =>\n Effect.gen(function* () {\n const state = yield* yield* State;\n const names = (yield* state.listStacks()).filter(\n (name) => name === repository || name.startsWith(`${repository}/`),\n );\n const cards: StackCard[] = [];\n for (const name of names) {\n if (!(yield* state.listStages(name)).includes(stage)) {\n continue;\n }\n const request = { stack: name, stage };\n const rows = yield* Effect.forEach(yield* state.list(request), (fqn) =>\n state.get({ ...request, fqn }),\n );\n const output = yield* state.getOutput(request);\n cards.push({\n name,\n stage,\n resources: rows.filter(Schema.is(Resource)),\n run: Schema.is(StageOutput)(output) ? output : null,\n });\n }\n return cards;\n });\n\n/** A stack can create a deployment in one resource and push to it in another: a card shows it once. */\nconst withoutRepeats = (resources: readonly Resource[]) => {\n const deployments = new Set<string>();\n return resources.filter((resource) => {\n if (resource.resourceType !== \"Convex.Deployment\") {\n return true;\n }\n const repeated = deployments.has(resource.attr.name);\n deployments.add(resource.attr.name);\n return !repeated;\n });\n};\n\nconst escapeHtml = (text: string) =>\n text\n .replaceAll(\"&\", \"&\")\n .replaceAll(\"<\", \"<\")\n .replaceAll(\">\", \">\")\n .replaceAll('\"', \""\");\n\nconst link = (href: string, text: string) => `<a href=\"${escapeHtml(href)}\">${text}</a>`;\n\nconst named = (href: string, icon: string, alt: string, name: string) => {\n const image = `<img src=\"${ICONS}/${icon}.svg\" width=\"16\" align=\"absmiddle\" alt=\"${alt}\">`;\n return `${link(href, image)} ${link(href, escapeHtml(name))}`;\n};\n\nconst hostOf = (url: string) => escapeHtml(new URL(url).host);\n\n/** Where a stack serves: its Convex deployment, else its first Worker with a URL. */\nconst addressOf = (resources: readonly Resource[]) => {\n for (const resource of resources) {\n if (resource.resourceType === \"Convex.Deployment\") {\n return resource.attr.siteUrl;\n }\n }\n for (const resource of resources) {\n if (resource.resourceType === \"Cloudflare.Worker\" && resource.attr.url !== undefined) {\n return resource.attr.url;\n }\n }\n return null;\n};\n\nconst resourceRow = (resource: Resource, address: string | null) => {\n switch (resource.resourceType) {\n case \"Convex.Deployment\": {\n const { name } = resource.attr;\n return named(\n `https://dashboard.convex.dev/d/${encodeURIComponent(name)}`,\n \"convex\",\n \"Convex\",\n name,\n );\n }\n case \"Cloudflare.Worker\": {\n const { url, workerName } = resource.attr;\n const row = named(\n `https://dash.cloudflare.com/?to=/:account/workers/services/view/${encodeURIComponent(workerName)}`,\n \"worker\",\n \"Worker\",\n workerName,\n );\n return url === undefined || url === address\n ? row\n : `${row}<br><sub>${link(url, hostOf(url))}</sub>`;\n }\n case \"Cloudflare.R2.Bucket\": {\n const { accountId, bucketName, jurisdiction } = resource.attr;\n return named(\n `https://dash.cloudflare.com/${encodeURIComponent(accountId)}/r2/${encodeURIComponent(jurisdiction)}/buckets/${encodeURIComponent(bucketName)}`,\n \"r2\",\n \"R2 bucket\",\n bucketName,\n );\n }\n }\n};\n\nconst runLine = ({ run }: StackCard) => {\n if (run === null) {\n return \"\";\n }\n const { headCommit, recordedAt, runUrl } = run[RUN_RECORD_KEY];\n const commit = escapeHtml(headCommit.slice(0, 7));\n const time = `${recordedAt.slice(0, 16).replace(\"T\", \" \")} UTC`;\n return `<br><sub>${runUrl === null ? commit : link(runUrl, commit)} ${escapeHtml(time)}</sub>`;\n};\n\nconst SPACER = `<div><img src=\"${ICONS}/spacer.svg\" width=\"460\" align=\"left\" alt=\"\"></div>`;\n\n/** `alone`: the one card of the repository. A card at the root then has no folder, as on the repository page. */\nconst renderCard = (card: StackCard, repository: string, alone: boolean) => {\n const folder = card.name.slice(repository.length + 1);\n const heading = [\n folder === \"\" && alone ? \"\" : `<b>${escapeHtml(folder || \"/\")}</b> `,\n `<sub>${escapeHtml(card.stage)}</sub>`,\n ].join(\"\");\n const resources = withoutRepeats(card.resources);\n const address = addressOf(resources);\n const addressLine = address === null ? \"\" : `<br>${link(address, hostOf(address))}`;\n const rows = resources\n .map((resource) => `<tbody><tr><td>${resourceRow(resource, address)}</td></tr></tbody>`)\n .join(\"\\n\");\n return `<table>\n<tbody><tr><td>${SPACER}${heading}${addressLine}${runLine(card)}</td></tr></tbody>\n${rows}\n</table>`;\n};\n\nexport const renderStackComment = (cards: readonly StackCard[], repository: string) => `${MARKER}\n${cards.map((card) => renderCard(card, repository, cards.length === 1)).join(\"\\n\\n\")}\n`;\n\nconst Comments = Schema.Array(Schema.Struct({ id: Schema.Number, body: Schema.String }));\nconst SavedComment = Schema.Struct({ html_url: Schema.String });\n\nconst github = (token: string, path: string, init: { method: string; body?: string }) =>\n Effect.tryPromise({\n try: async (): Promise<unknown> => {\n const response = await fetch(`${GITHUB_API}${path}`, {\n ...init,\n headers: {\n Accept: \"application/vnd.github+json\",\n Authorization: `Bearer ${token}`,\n \"Content-Type\": \"application/json\",\n \"X-GitHub-Api-Version\": \"2022-11-28\",\n },\n });\n if (!response.ok) {\n throw new Error(`HTTP ${response.status}`);\n }\n return await response.json();\n },\n catch: (error) =>\n new StackCommentError({\n reason: `GitHub ${init.method} ${path} failed (${error instanceof Error ? error.message : String(error)})`,\n }),\n });\n\nconst decode =\n <S extends Schema.Codec<unknown, unknown>>(schema: S, what: string) =>\n (value: unknown) =>\n Schema.decodeUnknownEffect(schema)(value).pipe(\n Effect.mapError(() => new StackCommentError({ reason: `GitHub gave a bad ${what}` })),\n );\n\nconst findComment = (token: string, path: string) =>\n Effect.gen(function* () {\n for (let page = 1; ; page += 1) {\n const comments = yield* github(token, `${path}?per_page=100&page=${page}`, {\n method: \"GET\",\n }).pipe(Effect.flatMap(decode(Comments, \"comment list\")));\n const comment = comments.find(({ body }) => body.startsWith(MARKER));\n if (comment !== undefined || comments.length < 100) {\n return comment;\n }\n }\n });\n\n/** Replaces the body of the comment of this command, or posts it on the first run, and gives its URL. */\nexport const saveStackComment = (args: {\n readonly token: string;\n readonly repository: string;\n readonly pullRequest: number;\n readonly body: string;\n}) =>\n Effect.gen(function* () {\n const { body, pullRequest, repository, token } = args;\n const comments = `/repos/${repository}/issues/${pullRequest}/comments`;\n const comment = yield* findComment(token, comments);\n const reply = yield* comment === undefined\n ? github(token, comments, { method: \"POST\", body: JSON.stringify({ body }) })\n : github(token, `/repos/${repository}/issues/comments/${comment.id}`, {\n method: \"PATCH\",\n body: JSON.stringify({ body }),\n });\n const saved = yield* decode(SavedComment, \"comment\")(reply);\n return saved.html_url;\n });\n\n/** The step of a workflow: the run record names the repository and the pull request of the run. */\nexport const updateStackComment = Effect.gen(function* () {\n const { pullRequest, repository } = yield* runRecord({ cwd: process.cwd(), env: process.env });\n if (pullRequest === null) {\n return yield* new StackCommentError({ reason: \"this run has no pull request\" });\n }\n const token = process.env[\"GITHUB_TOKEN\"];\n if (token === undefined || token === \"\") {\n return yield* new StackCommentError({ reason: \"GITHUB_TOKEN is not set\" });\n }\n const cards = yield* readStackCards({ repository, stage: `pr-${pullRequest}` });\n return yield* saveStackComment({\n token,\n repository,\n pullRequest,\n body: renderStackComment(cards, repository),\n });\n});\n","#!/usr/bin/env node\n// The commands of the package:\n//\n// alchemy-convex hub-client --team <team> --app <app> --callback-path <path>\n// alchemy-convex stack-comment [--team <team>]\n//\n// hub-client runs in the directory of a Convex app, and gives the development deployment of that\n// directory a client of the identity hub of the team: see registerDevelopmentClient. stack-comment\n// runs as a step of a pull request workflow after the deploys, and keeps one comment with the stacks\n// of the repository: see src/StackComment.ts. It needs GITHUB_TOKEN and, with a team access token,\n// no --team. Both use CONVEX_ACCESS_TOKEN or the Convex CLI login.\nimport { execFileSync } from \"node:child_process\";\nimport { parseArgs } from \"node:util\";\nimport * as NodeServices from \"@effect/platform-node/NodeServices\";\nimport * as Effect from \"effect/Effect\";\nimport * as Layer from \"effect/Layer\";\nimport { fromConfig } from \"./Credentials.ts\";\nimport { registerDevelopmentClient } from \"./HubClient.ts\";\nimport { ManagementApiLive } from \"./ManagementApi.ts\";\nimport { installedCli } from \"./Push.ts\";\nimport { updateStackComment } from \"./StackComment.ts\";\nimport { state } from \"./State.ts\";\n\nconst { positionals, values } = parseArgs({\n allowPositionals: true,\n options: {\n team: { type: \"string\" },\n app: { type: \"string\" },\n \"callback-path\": { type: \"string\" },\n },\n});\nconst { team, app, \"callback-path\": callbackPath } = values;\nconst USAGE = `Usage:\n alchemy-convex hub-client --team <team> --app <app> --callback-path <path>\n alchemy-convex stack-comment [--team <team>]`;\n\nconst hubClient = (hub: { team: string; app: string; callbackPath: string }) =>\n Effect.gen(function* () {\n const [command, ...prefix] = yield* installedCli(process.cwd());\n const callbackUrl = yield* registerDevelopmentClient({\n ...hub,\n convex: (args, input) =>\n execFileSync(command, [...prefix, ...args], {\n encoding: \"utf8\",\n input,\n stdio: [\"pipe\", \"pipe\", \"inherit\"],\n }).trim(),\n });\n console.log(\n callbackUrl === undefined\n ? \"Hub client already registered.\"\n : `Hub client registered for ${callbackUrl}.`,\n );\n }).pipe(Effect.provide(ManagementApiLive().pipe(Layer.provide(fromConfig()))));\n\nconst stackComment = (options: { team?: string }) =>\n updateStackComment.pipe(\n Effect.tap((url) => Effect.sync(() => console.log(url))),\n Effect.provide(state(options).pipe(Layer.provide(NodeServices.layer))),\n );\n\nconst subcommand = positionals.join(\" \");\nconst program: Effect.Effect<unknown, unknown> | undefined =\n subcommand === \"stack-comment\"\n ? stackComment(team === undefined ? {} : { team })\n : subcommand === \"hub-client\" &&\n team !== undefined &&\n app !== undefined &&\n callbackPath !== undefined\n ? hubClient({ team, app, callbackPath })\n : undefined;\nif (program === undefined) {\n console.error(USAGE);\n process.exit(1);\n}\n\nawait Effect.runPromise(program).catch((error: unknown) => {\n console.error(error instanceof Error ? error.message : String(error));\n process.exit(1);\n});\n"],"mappings":";;;;;;;;;;;AA4BA,MAAM,SAAS;;AAGf,MAAM,QACJ;AAEF,MAAM,aAAa;;AAGnB,MAAM,WAAW,OAAO,MAAM;CAC5B,OAAO,OAAO;EACZ,cAAc,OAAO,QAAQ,mBAAmB;EAChD,MAAM,OAAO,OAAO;GAAE,MAAM,OAAO;GAAQ,SAAS,OAAO;EAAO,CAAC;CACrE,CAAC;CACD,OAAO,OAAO;EACZ,cAAc,OAAO,QAAQ,mBAA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AAO,WAAW,OAAO,CAAC,CAAC,OAAO,UAAmB;CACzD,QAAQ,MAAM,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC;CACpE,QAAQ,KAAK,CAAC;AAChB,CAAC"}
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/Credentials.ts","../src/ManagementApi.ts","../src/Providers.ts","../src/Push.ts","../src/Deployment.ts","../src/Component.ts","../src/Project.ts","../src/HubClient.ts","../src/RunRecord.ts","../src/State.ts"],"mappings":";;;;;;;;;;;;;;;;UA6BiB;;WAEN,aAAa,SAAS;;WAEtB;;UAGM;;WAEN,aAAa,OAAO,OAAO;;WAE3B,gBAAgB,OAAO;;WAEvB,WAAW,OAAO,OAAO,SAAS;;;qBAGhC,oBAAoB;;qBAKpB;;qBAGA;;;;qBAOA,yBAAyB;;;;;;;;;qBAiCzB,kBAAiB,MAAM,MAAM;;qBA0B7B,aAAc;WAChB,cAAc,SAAS;WACvB,YAAY,SAAS;MAC5B,MAAM,MAAM;;;KC3GJ,mBAAmB,kBAAkB;KACrC,mBAAmB,kBAAkB;;UAGhC,YAAY;WAClB,OAAO;WACP;WACA,UAAU;;;;;;;;;;;;;qBAQR,uBAAuB;UAuBnB;;WAEN,UAAU,GACjB,mBACA,MAAM,QAAQ,qBAAqB,QAAQ,YAAY,QACpD,OAAO,OAAO,GAAG;;WAEb,cACP,mBACA,MAAM,QAAQ,qBAAqB,QAAQ,0BACxC,OAAO,aAAa;;WAEhB,oBAAoB,GAC3B,mBACA,6BACA,WAAW,SAAS,kBACpB,MAAM,QAAQ,qBAAqB,QAAQ,YAAY,QACpD,OAAO,OAAO,GAAG;;;;;;;;WAQb,gBACP,sCACG,OAAO,eAAe;;;;;;;;WAQlB,oBAAoB,mBAAmB,OAAO;;;;;WAK9C,YAAY,OAAO,SAAS,qBAAqB;;;qBAM/C,sBAAsB;;;;qBCzFtB,kBAAkB;;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;;qBASF,gCAAgC;MAS9B;;;;;;;;qBAOF,yBAAyB;MAKvB;;;;;;qBAMF,+BAA+B;MAI7B;;;;;;;qBAMF,wBAAwB;MAItB;;;;;qBC/FF;KAED;UAEK;;WAEN;;WAEA;;WAEA;WACA,gBAAgB;;WAEhB;;WAEA;;UAGM;;WAEN;WACA;WACA,gBAAgB;;WAEhB;WACA;;WAEA,cAAc,SAAS;;YAGtB,YAAY,+BAEtB,gBACA,4BAEA;qBAEW,6BAAS,cAAA;;;;;;qBAGT,wBAAwB;MAItB;;;;;qBCzDF;;UAMI;WACN;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;WACA;;WAEA;;;;;qBAGE,uBAAuB;MAGrB;;;UAME;WACN;WACA,KAAK,SAAS;;;;;;;;qBAuIZ,YAAa,OAAO,mBAAiB,OAAO,OAAO,WAAW;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/Credentials.ts","../src/ManagementApi.ts","../src/Providers.ts","../src/Push.ts","../src/Deployment.ts","../src/Component.ts","../src/Project.ts","../src/HubClient.ts","../src/RunRecord.ts","../src/State.ts"],"mappings":";;;;;;;;;;;;;;;;UA6BiB;;WAEN,aAAa,SAAS;;WAEtB;;UAGM;;WAEN,aAAa,OAAO,OAAO;;WAE3B,gBAAgB,OAAO;;WAEvB,WAAW,OAAO,OAAO,SAAS;;;qBAGhC,oBAAoB;;qBAKpB;;qBAGA;;;;qBAOA,yBAAyB;;;;;;;;;qBAiCzB,kBAAiB,MAAM,MAAM;;qBA0B7B,aAAc;WAChB,cAAc,SAAS;WACvB,YAAY,SAAS;MAC5B,MAAM,MAAM;;;KC3GJ,mBAAmB,kBAAkB;KACrC,mBAAmB,kBAAkB;;UAGhC,YAAY;WAClB,OAAO;WACP;WACA,UAAU;;;;;;;;;;;;;qBAQR,uBAAuB;UAuBnB;;WAEN,UAAU,GACjB,mBACA,MAAM,QAAQ,qBAAqB,QAAQ,YAAY,QACpD,OAAO,OAA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;;;;;;;qBAMF,4BAA4B;MAS1B;;;;;UC1OE;WACN;WACA;WACA;;;;;;;;;qBAUE,gBAAiB;EAC5B,YAAY;EACZ;EACA,WAAW;MACZ,OAAA,yBAAA;;;;KCWW,mBAAmB;KAEnB;;WAGG;;WAEA;;WAEA,mBAAmB;;WAEnB;;;WAIA;;WAEA;;UAGE;WACN;WACA;WACA;WACA;WACA;WACA;;YAGC,UAAU,2BAA2B,cAAc,0BAA0B;qBAC5E,2BAAO,cAAA;;;;;;;qBAGP,0BAA0B;MAIxB;;;;;;;;;;;;qBASF,gCAAgC;MAS9B;;;;;;;;qBAOF,yBAAyB;MAKvB;;;;;;qBAMF,+BAA+B;MAI7B;;;;;;;qBAMF,wBAAwB;MAItB;;;;;qBC/FF;KAED;UAEK;;WAEN;;WAEA;;WAEA;WACA,gBAAgB;;WAEhB;;WAEA;;UAGM;;WAEN;WACA;WACA,gBAAgB;;WAEhB;WACA;;WAEA,cAAc,SAAS;;YAGtB,YAAY,+BAEtB,gBACA,4BAEA;qBAEW,6BAAS,cAAA;;;;;;qBAGT,wBAAwB;MAItB;;;;;qBCzDF;;UAMI;WACN;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;WACA;;WAEA;;;;;qBAGE,uBAAuB;MAGrB;;;UAME;WACN;WACA,KAAK,SAAS;;;;;;;;qBAuIZ,YAAa,OAAO,mBAAiB,OAAO,OAAO,WAAW;;;;;;;qBAiB9D,gBAAiB,iBAAiB,QAAQ;;;;qBClH1C;;;;;;;;;qBA8ZA,QACX;WAAoB;MACnB,MAAM,MAAM,cAAc,oBAAoB"}
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { A as PushTargetMismatch, B as DEPLOY_KEY_ENV, C as DeploymentProvider, D as ConvexAppNotFound, E as CodePushFailed, F as ManagementApi, H as fromValues, I as ManagementApiLive, L as ACCESS_TOKEN_ENV, N as parseDeployOutput, O as ConvexCliNotFound, P as ConvexApiError, R as Credentials, S as DeploymentIdentityChange, T as ProductionDeploymentNotFound, V as fromConfig, _ as ProjectProvider, b as AmbiguousDeployment, c as RUN_RECORD_KEY, d as withRunRecord, f as AmbiguousProject, g as ProjectNotFound, h as ProjectDeploymentChange, i as HubClientProvider, j as VariablesOwnedBySomeoneElse, k as EnvironmentNeedsDeployKey, l as RunRecordError, m as Project, n as HubClient, o as STORE_PROJECT_NAME, p as CreatedProjectNotFound, r as HubClientFailed, s as state, t as HUB_PROJECT_NAME, u as runRecord, v as ProjectTeamChange, w as DeploymentResource, x as Deployment, y as AbsoluteRootDir, z as CredentialsError } from "./HubClient-
|
|
1
|
+
import { A as PushTargetMismatch, B as DEPLOY_KEY_ENV, C as DeploymentProvider, D as ConvexAppNotFound, E as CodePushFailed, F as ManagementApi, H as fromValues, I as ManagementApiLive, L as ACCESS_TOKEN_ENV, N as parseDeployOutput, O as ConvexCliNotFound, P as ConvexApiError, R as Credentials, S as DeploymentIdentityChange, T as ProductionDeploymentNotFound, V as fromConfig, _ as ProjectProvider, b as AmbiguousDeployment, c as RUN_RECORD_KEY, d as withRunRecord, f as AmbiguousProject, g as ProjectNotFound, h as ProjectDeploymentChange, i as HubClientProvider, j as VariablesOwnedBySomeoneElse, k as EnvironmentNeedsDeployKey, l as RunRecordError, m as Project, n as HubClient, o as STORE_PROJECT_NAME, p as CreatedProjectNotFound, r as HubClientFailed, s as state, t as HUB_PROJECT_NAME, u as runRecord, v as ProjectTeamChange, w as DeploymentResource, x as Deployment, y as AbsoluteRootDir, z as CredentialsError } from "./HubClient-C0XxgkK3.js";
|
|
2
2
|
import { toPath } from "alchemy/FQN";
|
|
3
3
|
import { Stack } from "alchemy/Stack";
|
|
4
4
|
import * as Effect from "effect/Effect";
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
//#region src/store/sealedSecret.ts
|
|
2
|
+
const toBase64 = (bytes) => btoa(Array.from(bytes, (byte) => String.fromCharCode(byte)).join(""));
|
|
3
|
+
const toBase64Url = (bytes) => toBase64(bytes).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
|
|
4
|
+
/** The bytes of base64 or base64url text. */
|
|
5
|
+
const fromBase64 = (text) => {
|
|
6
|
+
const standard = text.replaceAll("-", "+").replaceAll("_", "/");
|
|
7
|
+
const padded = standard.padEnd(standard.length + (4 - standard.length % 4) % 4, "=");
|
|
8
|
+
return Uint8Array.from(atob(padded), (character) => character.charCodeAt(0));
|
|
9
|
+
};
|
|
10
|
+
/** A key of 32 bytes in base64 is 43 characters and one `=`. */
|
|
11
|
+
const KEY_TEXT = /^[A-Za-z0-9+/]{43}=$/;
|
|
12
|
+
/**
|
|
13
|
+
* The key ring in the text of the variable `name`. It fails closed when the text is missing or
|
|
14
|
+
* malformed, or when the current key id has no key. An error never quotes the text, which holds
|
|
15
|
+
* the keys.
|
|
16
|
+
*/
|
|
17
|
+
const parseKeyRing = (name, text) => {
|
|
18
|
+
let value;
|
|
19
|
+
try {
|
|
20
|
+
value = JSON.parse(text ?? "");
|
|
21
|
+
} catch {
|
|
22
|
+
throw new Error(`${name} is missing or is not JSON.`);
|
|
23
|
+
}
|
|
24
|
+
const malformed = /* @__PURE__ */ new Error(`${name} must be {"current":"<keyId>","keys":{"<keyId>":"<base64 of 32 bytes>"}}.`);
|
|
25
|
+
if (typeof value !== "object" || value === null) throw malformed;
|
|
26
|
+
if (!("current" in value) || !("keys" in value)) throw malformed;
|
|
27
|
+
const { current, keys: keyTexts } = value;
|
|
28
|
+
if (typeof current !== "string" || typeof keyTexts !== "object" || keyTexts === null) throw malformed;
|
|
29
|
+
const keys = /* @__PURE__ */ new Map();
|
|
30
|
+
for (const [keyId, key] of Object.entries(keyTexts)) {
|
|
31
|
+
if (typeof key !== "string" || !KEY_TEXT.test(key)) throw malformed;
|
|
32
|
+
keys.set(keyId, fromBase64(key));
|
|
33
|
+
}
|
|
34
|
+
if (!keys.has(current)) throw new Error(`${name} has no key for its current key id.`);
|
|
35
|
+
return {
|
|
36
|
+
current,
|
|
37
|
+
keys
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
/** A random key id of 12 hexadecimal characters, which is also safe in a variable name. */
|
|
41
|
+
const newKeyId = () => Array.from(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(6)), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
42
|
+
/** A random key of 32 bytes in base64. It needs real randomness, like `sealSecret`. */
|
|
43
|
+
const newKey = () => toBase64(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(32)));
|
|
44
|
+
/** The text of a new key ring with one random key. */
|
|
45
|
+
const newKeyRing = () => {
|
|
46
|
+
const keyId = newKeyId();
|
|
47
|
+
return JSON.stringify({
|
|
48
|
+
current: keyId,
|
|
49
|
+
keys: { [keyId]: newKey() }
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
const importKey = (ring, keyId) => {
|
|
53
|
+
const bytes = ring.keys.get(keyId);
|
|
54
|
+
if (bytes === void 0) throw new Error(`The key ring has no key "${keyId}".`);
|
|
55
|
+
return crypto.subtle.importKey("raw", bytes, "AES-GCM", false, ["encrypt", "decrypt"]);
|
|
56
|
+
};
|
|
57
|
+
/** Seals `plaintext` with the current key. `aad` names the owner, so the value opens only there. */
|
|
58
|
+
const sealSecret = async (ring, args) => {
|
|
59
|
+
const key = await importKey(ring, ring.current);
|
|
60
|
+
const iv = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(12));
|
|
61
|
+
const ciphertext = await crypto.subtle.encrypt({
|
|
62
|
+
name: "AES-GCM",
|
|
63
|
+
iv,
|
|
64
|
+
additionalData: new TextEncoder().encode(args.aad)
|
|
65
|
+
}, key, new TextEncoder().encode(args.plaintext));
|
|
66
|
+
return {
|
|
67
|
+
keyId: ring.current,
|
|
68
|
+
iv: toBase64Url(iv),
|
|
69
|
+
ciphertext: toBase64Url(new Uint8Array(ciphertext))
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
/** Opens a sealed value with the key it names. A different `aad` or a changed value fails. */
|
|
73
|
+
const openSecret = async (ring, args) => {
|
|
74
|
+
const { sealed } = args;
|
|
75
|
+
const key = await importKey(ring, sealed.keyId);
|
|
76
|
+
const plaintext = await crypto.subtle.decrypt({
|
|
77
|
+
name: "AES-GCM",
|
|
78
|
+
iv: fromBase64(sealed.iv),
|
|
79
|
+
additionalData: new TextEncoder().encode(args.aad)
|
|
80
|
+
}, key, fromBase64(sealed.ciphertext));
|
|
81
|
+
return new TextDecoder().decode(plaintext);
|
|
82
|
+
};
|
|
83
|
+
//#endregion
|
|
84
|
+
export { parseKeyRing as a, openSecret as i, newKeyId as n, sealSecret as o, newKeyRing as r, newKey as t };
|
|
85
|
+
|
|
86
|
+
//# sourceMappingURL=sealedSecret-Bf8AeIp_.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sealedSecret-Bf8AeIp_.js","names":[],"sources":["../src/store/sealedSecret.ts"],"sourcesContent":["// AES-256-GCM sealing under a key ring, with Web Crypto only, so that it runs in Convex and in Node.\n//\n// A key ring is the JSON text `{\"current\":\"<keyId>\",\"keys\":{\"<keyId>\":\"<base64 of 32 bytes>\"}}`,\n// which a deployment keeps in one environment variable. `sealSecret` seals with the `current` key.\n// `openSecret` opens with the key that the sealed object names, so an old key stays in the ring\n// until a reseal moves its values to the current key.\n//\n// Seal only where randomness is real: in a Convex action, or outside Convex. In a Convex query or\n// mutation `crypto.getRandomValues` is a seeded deterministic generator, and a repeated IV under one\n// key breaks AES-GCM. Opening draws no randomness, so a query can open.\n\n/** A value sealed with AES-256-GCM under the key `keyId` of a key ring. */\nexport interface SealedSecret {\n readonly keyId: string;\n /** 12 random bytes, base64url. */\n readonly iv: string;\n /** The AES-GCM output with its 16-byte tag, base64url. */\n readonly ciphertext: string;\n}\n\nexport interface KeyRing {\n readonly current: string;\n readonly keys: ReadonlyMap<string, Uint8Array<ArrayBuffer>>;\n}\n\nconst toBase64 = (bytes: Uint8Array) =>\n btoa(Array.from(bytes, (byte) => String.fromCharCode(byte)).join(\"\"));\n\nconst toBase64Url = (bytes: Uint8Array) =>\n toBase64(bytes).replaceAll(\"+\", \"-\").replaceAll(\"/\", \"_\").replace(/=+$/, \"\");\n\n/** The bytes of base64 or base64url text. */\nconst fromBase64 = (text: string) => {\n const standard = text.replaceAll(\"-\", \"+\").replaceAll(\"_\", \"/\");\n const padded = standard.padEnd(standard.length + ((4 - (standard.length % 4)) % 4), \"=\");\n return Uint8Array.from(atob(padded), (character) => character.charCodeAt(0));\n};\n\n/** A key of 32 bytes in base64 is 43 characters and one `=`. */\nconst KEY_TEXT = /^[A-Za-z0-9+/]{43}=$/;\n\n/**\n * The key ring in the text of the variable `name`. It fails closed when the text is missing or\n * malformed, or when the current key id has no key. An error never quotes the text, which holds\n * the keys.\n */\nexport const parseKeyRing = (name: string, text: string | undefined): KeyRing => {\n let value: unknown;\n try {\n value = JSON.parse(text ?? \"\");\n } catch {\n throw new Error(`${name} is missing or is not JSON.`);\n }\n const malformed = new Error(\n `${name} must be {\"current\":\"<keyId>\",\"keys\":{\"<keyId>\":\"<base64 of 32 bytes>\"}}.`,\n );\n if (typeof value !== \"object\" || value === null) throw malformed;\n if (!(\"current\" in value) || !(\"keys\" in value)) throw malformed;\n const { current, keys: keyTexts } = value;\n if (typeof current !== \"string\" || typeof keyTexts !== \"object\" || keyTexts === null) {\n throw malformed;\n }\n const keys = new Map<string, Uint8Array<ArrayBuffer>>();\n for (const [keyId, key] of Object.entries(keyTexts)) {\n if (typeof key !== \"string\" || !KEY_TEXT.test(key)) throw malformed;\n keys.set(keyId, fromBase64(key));\n }\n if (!keys.has(current)) throw new Error(`${name} has no key for its current key id.`);\n return { current, keys };\n};\n\n/** A random key id of 12 hexadecimal characters, which is also safe in a variable name. */\nexport const newKeyId = (): string =>\n Array.from(crypto.getRandomValues(new Uint8Array(6)), (byte) =>\n byte.toString(16).padStart(2, \"0\"),\n ).join(\"\");\n\n/** A random key of 32 bytes in base64. It needs real randomness, like `sealSecret`. */\nexport const newKey = (): string => toBase64(crypto.getRandomValues(new Uint8Array(32)));\n\n/** The text of a new key ring with one random key. */\nexport const newKeyRing = (): string => {\n const keyId = newKeyId();\n return JSON.stringify({ current: keyId, keys: { [keyId]: newKey() } });\n};\n\nconst importKey = (ring: KeyRing, keyId: string) => {\n const bytes = ring.keys.get(keyId);\n if (bytes === undefined) throw new Error(`The key ring has no key \"${keyId}\".`);\n return crypto.subtle.importKey(\"raw\", bytes, \"AES-GCM\", false, [\"encrypt\", \"decrypt\"]);\n};\n\n/** Seals `plaintext` with the current key. `aad` names the owner, so the value opens only there. */\nexport const sealSecret = async (\n ring: KeyRing,\n args: { readonly plaintext: string; readonly aad: string },\n): Promise<SealedSecret> => {\n const key = await importKey(ring, ring.current);\n const iv = crypto.getRandomValues(new Uint8Array(12));\n const ciphertext = await crypto.subtle.encrypt(\n { name: \"AES-GCM\", iv, additionalData: new TextEncoder().encode(args.aad) },\n key,\n new TextEncoder().encode(args.plaintext),\n );\n return {\n keyId: ring.current,\n iv: toBase64Url(iv),\n ciphertext: toBase64Url(new Uint8Array(ciphertext)),\n };\n};\n\n/** Opens a sealed value with the key it names. A different `aad` or a changed value fails. */\nexport const openSecret = async (\n ring: KeyRing,\n args: { readonly sealed: SealedSecret; readonly aad: string },\n): Promise<string> => {\n const { sealed } = args;\n const key = await importKey(ring, sealed.keyId);\n const plaintext = await crypto.subtle.decrypt(\n {\n name: \"AES-GCM\",\n iv: fromBase64(sealed.iv),\n additionalData: new TextEncoder().encode(args.aad),\n },\n key,\n fromBase64(sealed.ciphertext),\n );\n return new TextDecoder().decode(plaintext);\n};\n"],"mappings":";AAyBA,MAAM,YAAY,UAChB,KAAK,MAAM,KAAK,QAAQ,SAAS,OAAO,aAAa,IAAI,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;AAEtE,MAAM,eAAe,UACnB,SAAS,KAAK,CAAC,CAAC,WAAW,KAAK,GAAG,CAAC,CAAC,WAAW,KAAK,GAAG,CAAC,CAAC,QAAQ,OAAO,EA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ts
|
|
2
|
+
/** A value sealed with AES-256-GCM under the key `keyId` of a key ring. */
|
|
3
|
+
export interface SealedSecret {
|
|
4
|
+
readonly keyId: string;
|
|
5
|
+
/** 12 random bytes, base64url. */
|
|
6
|
+
readonly iv: string;
|
|
7
|
+
/** The AES-GCM output with its 16-byte tag, base64url. */
|
|
8
|
+
readonly ciphertext: string;
|
|
9
|
+
}
|
|
10
|
+
export interface KeyRing {
|
|
11
|
+
readonly current: string;
|
|
12
|
+
readonly keys: ReadonlyMap<string, Uint8Array<ArrayBuffer>>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The key ring in the text of the variable `name`. It fails closed when the text is missing or
|
|
16
|
+
* malformed, or when the current key id has no key. An error never quotes the text, which holds
|
|
17
|
+
* the keys.
|
|
18
|
+
*/
|
|
19
|
+
export declare const parseKeyRing: (name: string, text: string | undefined) => KeyRing;
|
|
20
|
+
/** A random key id of 12 hexadecimal characters, which is also safe in a variable name. */
|
|
21
|
+
export declare const newKeyId: () => string;
|
|
22
|
+
/** A random key of 32 bytes in base64. It needs real randomness, like `sealSecret`. */
|
|
23
|
+
export declare const newKey: () => string;
|
|
24
|
+
/** The text of a new key ring with one random key. */
|
|
25
|
+
export declare const newKeyRing: () => string;
|
|
26
|
+
/** Seals `plaintext` with the current key. `aad` names the owner, so the value opens only there. */
|
|
27
|
+
export declare const sealSecret: (ring: KeyRing, args: {
|
|
28
|
+
readonly plaintext: string;
|
|
29
|
+
readonly aad: string;
|
|
30
|
+
}) => Promise<SealedSecret>;
|
|
31
|
+
/** Opens a sealed value with the key it names. A different `aad` or a changed value fails. */
|
|
32
|
+
export declare const openSecret: (ring: KeyRing, args: {
|
|
33
|
+
readonly sealed: SealedSecret;
|
|
34
|
+
readonly aad: string;
|
|
35
|
+
}) => Promise<string>;
|
|
36
|
+
//#endregion
|
|
37
|
+
//# sourceMappingURL=sealedSecret.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sealedSecret.d.ts","names":[],"sources":["../src/store/sealedSecret.ts"],"mappings":";;iBAYiB;WACN;;WAEA;;WAEA;;iBAGM;WACN;WACA,MAAM,oBAAoB,WAAW;;;;;;;qBAwBnC,eAAgB,cAAc,6BAA2B;;qBA0BzD;;qBAMA;;qBAGA;;qBAYA,aACX,MAAM,SACN;WAAiB;WAA4B;MAC5C,QAAQ;;qBAgBE,aACX,MAAM,SACN;WAAiB,QAAQ;WAAuB;MAC/C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as newKey, n as newKeyId, r as newKeyRing, i as openSecret, a as parseKeyRing, o as sealSecret } from "./sealedSecret-Bf8AeIp_.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samebase/alchemy-convex",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "The Convex plane for Alchemy v2: projects, deployments, environment variables, and the push of the functions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"alchemy",
|
|
@@ -9,14 +9,15 @@
|
|
|
9
9
|
"effect",
|
|
10
10
|
"infrastructure-as-code"
|
|
11
11
|
],
|
|
12
|
-
"homepage": "https://github.com/samebase/alchemy-convex#readme",
|
|
12
|
+
"homepage": "https://github.com/samebase/samebase/tree/main/packages/alchemy-convex#readme",
|
|
13
13
|
"bugs": {
|
|
14
|
-
"url": "https://github.com/samebase/
|
|
14
|
+
"url": "https://github.com/samebase/samebase/issues"
|
|
15
15
|
},
|
|
16
16
|
"license": "Apache-2.0",
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
|
19
|
-
"url": "git+https://github.com/samebase/
|
|
19
|
+
"url": "git+https://github.com/samebase/samebase.git",
|
|
20
|
+
"directory": "packages/alchemy-convex"
|
|
20
21
|
},
|
|
21
22
|
"bin": {
|
|
22
23
|
"alchemy-convex": "./lib/cli.js"
|
|
@@ -34,22 +35,15 @@
|
|
|
34
35
|
".": {
|
|
35
36
|
"types": "./lib/index.d.ts",
|
|
36
37
|
"import": "./lib/index.js"
|
|
38
|
+
},
|
|
39
|
+
"./sealed-secret": {
|
|
40
|
+
"types": "./lib/sealedSecret.d.ts",
|
|
41
|
+
"import": "./lib/sealedSecret.js"
|
|
37
42
|
}
|
|
38
43
|
},
|
|
39
44
|
"publishConfig": {
|
|
40
45
|
"access": "public"
|
|
41
46
|
},
|
|
42
|
-
"scripts": {
|
|
43
|
-
"check": "vp fmt --check && vp lint && pnpm run typecheck && vp test --run",
|
|
44
|
-
"typecheck": "tsc --noEmit",
|
|
45
|
-
"test": "vp test --run",
|
|
46
|
-
"test:live": "node ./scripts/test-live.ts",
|
|
47
|
-
"format": "vp fmt",
|
|
48
|
-
"lint": "vp lint",
|
|
49
|
-
"build": "vp pack",
|
|
50
|
-
"prepack": "pnpm run build",
|
|
51
|
-
"prepare": "vp config --no-agent"
|
|
52
|
-
},
|
|
53
47
|
"dependencies": {
|
|
54
48
|
"@convex-dev/platform": "0.1.11"
|
|
55
49
|
},
|
|
@@ -65,8 +59,8 @@
|
|
|
65
59
|
"effect": "4.0.5",
|
|
66
60
|
"publint": "0.3.25",
|
|
67
61
|
"typescript": "6.0.3",
|
|
68
|
-
"vite-plus": "1.
|
|
69
|
-
"vitest": "5.0.
|
|
62
|
+
"vite-plus": "1.3.0",
|
|
63
|
+
"vitest": "5.0.5"
|
|
70
64
|
},
|
|
71
65
|
"peerDependencies": {
|
|
72
66
|
"@effect/platform-node": "^4.0.5",
|
|
@@ -77,5 +71,12 @@
|
|
|
77
71
|
"engines": {
|
|
78
72
|
"node": ">=22"
|
|
79
73
|
},
|
|
80
|
-
"
|
|
81
|
-
|
|
74
|
+
"scripts": {
|
|
75
|
+
"check": "pnpm run typecheck && pnpm run lint && pnpm run test && pnpm run build",
|
|
76
|
+
"typecheck": "tsc --noEmit",
|
|
77
|
+
"test": "pnpm --dir ../.. exec vp test run --project alchemy-convex",
|
|
78
|
+
"test:live": "node ./scripts/test-live.ts",
|
|
79
|
+
"lint": "pnpm --dir ../.. exec vp lint --tsconfig packages/alchemy-convex/tsconfig.json --report-unused-disable-directives packages/alchemy-convex",
|
|
80
|
+
"build": "vp pack"
|
|
81
|
+
}
|
|
82
|
+
}
|
package/src/Project.ts
CHANGED
|
@@ -249,7 +249,7 @@ export const listProjectsByName = (api: ManagementApiService, teamId: number, na
|
|
|
249
249
|
const matches: ProjectDetails[] = [];
|
|
250
250
|
const seenCursors = new Set<string>();
|
|
251
251
|
let cursor: string | undefined;
|
|
252
|
-
|
|
252
|
+
for (;;) {
|
|
253
253
|
const body = yield* api.request("list projects", (client) =>
|
|
254
254
|
client.GET("/teams/{team_id}/projects", {
|
|
255
255
|
params: {
|
package/src/Push.ts
CHANGED
|
@@ -458,7 +458,7 @@ const listVariables = (url: string, key: Redacted.Redacted<string>) =>
|
|
|
458
458
|
);
|
|
459
459
|
|
|
460
460
|
/** One call that sets variables, or removes those whose value is null. */
|
|
461
|
-
const writeVariables = (
|
|
461
|
+
export const writeVariables = (
|
|
462
462
|
url: string,
|
|
463
463
|
key: Redacted.Redacted<string>,
|
|
464
464
|
changes: ReadonlyArray<{ readonly name: string; readonly value: string | null }>,
|
package/src/RunRecord.ts
CHANGED
|
@@ -188,6 +188,7 @@ export const runRecord = (where: RunEnvironment): Effect.Effect<RunRecord, RunRe
|
|
|
188
188
|
where.env["GITHUB_ACTIONS"] === "true" ? fromGitHubActions(where) : fromGit(where);
|
|
189
189
|
|
|
190
190
|
/** A plain object, which a spread keeps whole. A Date, a Duration, or a Redacted value is an object too, and a spread loses it. */
|
|
191
|
+
// eslint-disable-next-line samebase/no-hand-written-type-predicate -- This published package depends on no app package; the body checks the prototype.
|
|
191
192
|
const isPlainObject = (value: unknown): value is Record<string, unknown> => {
|
|
192
193
|
if (typeof value !== "object" || value === null) return false;
|
|
193
194
|
const prototype: unknown = Object.getPrototypeOf(value);
|