@samebase/alchemy-convex 0.34.0 → 0.36.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.
@@ -4,7 +4,9 @@
4
4
  //
5
5
  // The rows are in the alchemyState component (src/store/component). Each
6
6
  // function here forwards to the component function of the same name, with the
7
- // same results; an absent `run` or `stage` goes to the component as null.
7
+ // same results; an absent `stage` goes to the component as null. The functions
8
+ // hold Alchemy's state interface and nothing else, the same as the Cloudflare
9
+ // state store of Alchemy.
8
10
  // These names and arguments are the wire contract of STORE_VERSION. Samebase
9
11
  // reads `stacks`, `getOutput`, and `readStack` by name: a renamed function is
10
12
  // a version bump of the store and of Samebase.
@@ -14,7 +16,6 @@
14
16
  // open them for the run. `readStack` gives the rows as they are stored.
15
17
  import type { FunctionReturnType } from "convex/server";
16
18
  import { v } from "convex/values";
17
- import { runRecord } from "../component/runRecord.ts";
18
19
  import { stackReply } from "../component/state.ts";
19
20
  import { components, internal } from "./_generated/api.js";
20
21
  import {
@@ -119,13 +120,7 @@ export const replaced = internalQuery({
119
120
  },
120
121
  });
121
122
 
122
- const setArgs = {
123
- stack: v.string(),
124
- stage: v.string(),
125
- fqn: v.string(),
126
- value: v.string(),
127
- run: v.optional(runRecord),
128
- };
123
+ const setArgs = { stack: v.string(), stage: v.string(), fqn: v.string(), value: v.string() };
129
124
 
130
125
  export const set = internalAction({
131
126
  args: setArgs,
@@ -147,22 +142,16 @@ export const writeResource = internalMutation({
147
142
  stage: args.stage,
148
143
  fqn: args.fqn,
149
144
  value: args.value,
150
- run: args.run ?? null,
151
145
  });
152
146
  return true;
153
147
  },
154
148
  });
155
149
 
156
150
  export const remove = internalMutation({
157
- args: { stack: v.string(), stage: v.string(), fqn: v.string(), run: v.optional(runRecord) },
151
+ args: { stack: v.string(), stage: v.string(), fqn: v.string() },
158
152
  returns: v.null(),
159
153
  handler: (ctx, args) =>
160
- ctx.runMutation(state.remove, {
161
- stack: args.stack,
162
- stage: args.stage,
163
- fqn: args.fqn,
164
- run: args.run ?? null,
165
- }),
154
+ ctx.runMutation(state.remove, { stack: args.stack, stage: args.stage, fqn: args.fqn }),
166
155
  });
167
156
 
168
157
  /** Deletes the resources and outputs of one stage, or of every stage of the stack. */
@@ -183,12 +172,7 @@ export const getOutput = internalQuery({
183
172
  },
184
173
  });
185
174
 
186
- const setOutputArgs = {
187
- stack: v.string(),
188
- stage: v.string(),
189
- value: v.string(),
190
- run: v.optional(runRecord),
191
- };
175
+ const setOutputArgs = { stack: v.string(), stage: v.string(), value: v.string() };
192
176
 
193
177
  export const setOutput = internalAction({
194
178
  args: setOutputArgs,
@@ -209,13 +193,12 @@ export const writeOutput = internalMutation({
209
193
  stack: args.stack,
210
194
  stage: args.stage,
211
195
  value: args.value,
212
- run: args.run ?? null,
213
196
  });
214
197
  return true;
215
198
  },
216
199
  });
217
200
 
218
- /** Every stage of a stack with its resources, output, and current record, in one call. Since store version 2. */
201
+ /** Every stage of a stack with its resources and output, in one call. Since store version 2. */
219
202
  export const readStack = internalQuery({
220
203
  args: { stack: v.string() },
221
204
  returns: stackReply,
@@ -13,4 +13,6 @@
13
13
  // 5: rows hold their secrets sealed: `set` and `setOutput` are actions, with `sealing`, `chooseKey`,
14
14
  // `rotateKey`, and `reseal` (0.19.0).
15
15
  // 6: a run record's `source` is `github-actions` or `checkout`, no longer `laptop` (0.32.0).
16
- export const STORE_VERSION = 6;
16
+ // 7: Alchemy's state interface only: `set`, `remove`, and `setOutput` take no `run`, `readStack`
17
+ // and `reader:repository` give stages as `{ name, resources, output }` (0.35.0).
18
+ export const STORE_VERSION = 7;
@@ -78,12 +78,6 @@ export const newKeyId = (): string =>
78
78
  /** A random key of 32 bytes in base64. It needs real randomness, like `sealSecret`. */
79
79
  export const newKey = (): string => toBase64(crypto.getRandomValues(new Uint8Array(32)));
80
80
 
81
- /** The text of a new key ring with one random key. */
82
- export const newKeyRing = (): string => {
83
- const keyId = newKeyId();
84
- return JSON.stringify({ current: keyId, keys: { [keyId]: newKey() } });
85
- };
86
-
87
81
  const importKey = (ring: KeyRing, keyId: string) => {
88
82
  const bytes = ring.keys.get(keyId);
89
83
  if (bytes === undefined) throw new Error(`The key ring has no key "${keyId}".`);
@@ -1 +0,0 @@
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,EAAE;;AAG7E,MAAM,cAAc,SAAiB;CACnC,MAAM,WAAW,KAAK,WAAW,KAAK,GAAG,CAAC,CAAC,WAAW,KAAK,GAAG;CAC9D,MAAM,SAAS,SAAS,OAAO,SAAS,UAAW,IAAK,SAAS,SAAS,KAAM,GAAI,GAAG;CACvF,OAAO,WAAW,KAAK,KAAK,MAAM,IAAI,cAAc,UAAU,WAAW,CAAC,CAAC;AAC7E;;AAGA,MAAM,WAAW;;;;;;AAOjB,MAAa,gBAAgB,MAAc,SAAsC;CAC/E,IAAI;C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UAAU,OAAO,OAAO,WAAW,OAAO,CAAC,WAAW,SAAS,CAAC;AACvF;;AAGA,MAAa,aAAa,OACxB,MACA,SAC0B;CAC1B,MAAM,MAAM,MAAM,UAAU,MAAM,KAAK,OAAO;CAC9C,MAAM,KAAK,OAAO,gCAAgB,IAAI,WAAW,EAAE,CAAC;CACpD,MAAM,aAAa,MAAM,OAAO,OAAO,QACrC;EAAE,MAAM;EAAW;EAAI,gBAAgB,IAAI,YAAY,CAAC,CAAC,OAAO,KAAK,GAAG;CAAE,GAC1E,KACA,IAAI,YAAY,CAAC,CAAC,OAAO,KAAK,SAAS,CACzC;CACA,OAAO;EACL,OAAO,KAAK;EACZ,IAAI,YAAY,EAAE;EAClB,YAAY,YAAY,IAAI,WAAW,UAAU,CAAC;CACpD;AACF;;AAGA,MAAa,aAAa,OACxB,MACA,SACoB;CACpB,MAAM,EAAE,WAAW;CACnB,MAAM,MAAM,MAAM,UAAU,MAAM,OAAO,KAAK;CAC9C,MAAM,YAAY,MAAM,OAAO,OAAO,QACpC;EACE,MAAM;EACN,IAAI,WAAW,OAAO,EAAE;EACxB,gBAAgB,IAAI,YAAY,CAAC,CAAC,OAAO,KAAK,GAAG;CACnD,GACA,KACA,WAAW,OAAO,UAAU,CAC9B;CACA,OAAO,IAAI,YAAY,CAAC,CAAC,OAAO,SAAS;AAC3C"}
@@ -1,57 +0,0 @@
1
- import { v, type GenericValidator, type Infer } from "convex/values";
2
-
3
- export const runRecord = v.object({
4
- version: v.literal(1),
5
- repository: v.string(),
6
- stackFile: v.string(),
7
- branch: v.string(),
8
- pullRequest: v.union(v.number(), v.null()),
9
- headCommit: v.string(),
10
- runUrl: v.union(v.string(), v.null()),
11
- source: v.union(v.literal("github-actions"), v.literal("checkout")),
12
- recordedAt: v.string(),
13
- });
14
-
15
- type RunRecord = Infer<typeof runRecord>;
16
-
17
- /**
18
- * True when a write with `next` must replace `current`, the record of the stage. Records that differ
19
- * only in `recordedAt`, the time of one write, name the same run. Two checkout deploys of one commit
20
- * have no `runUrl`, so they are the same run too. A write without a record (`null`) clears one.
21
- */
22
- export const runChanged = (current: RunRecord | undefined, next: RunRecord | null) => {
23
- if (next === null || current === undefined) return next !== null || current !== undefined;
24
- const other = new Map(Object.entries(next));
25
- return Object.entries(current).some(
26
- ([field, value]) => field !== "recordedAt" && other.get(field) !== value,
27
- );
28
- };
29
-
30
- /** Whether `value` passes `validator`. Only the kinds of {@link runRecord} pass; another kind fails. */
31
- export const validate = <V extends GenericValidator>(
32
- validator: V,
33
- value: unknown,
34
- // eslint-disable-next-line samebase/no-hand-written-type-predicate -- A component imports no app package, and the switch walks the validator itself.
35
- ): value is Infer<V> => {
36
- switch (validator.kind) {
37
- case "object": {
38
- if (typeof value !== "object" || value === null) return false;
39
- const fields = new Map(Object.entries(value));
40
- return Object.entries(validator.fields).every(([key, field]) =>
41
- validate(field, fields.get(key)),
42
- );
43
- }
44
- case "union":
45
- return validator.members.some((member) => validate(member, value));
46
- case "literal":
47
- return value === validator.value;
48
- case "string":
49
- return typeof value === "string";
50
- case "float64":
51
- return typeof value === "number";
52
- case "null":
53
- return value === null;
54
- default:
55
- return false;
56
- }
57
- };