@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.
- package/README.md +18 -14
- package/lib/{State-CuktZaOh.js → State-DVC61yrQ.js} +53 -52
- package/lib/{State-CuktZaOh.js.map → State-DVC61yrQ.js.map} +1 -1
- package/lib/cli.js +1 -1
- package/lib/index.d.ts +11 -6
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +7 -11
- package/lib/index.js.map +1 -1
- package/lib/{sealedSecret-Bf8AeIp_.js → sealedSecret-B7HlvIg2.js} +2 -10
- package/lib/sealedSecret-B7HlvIg2.js.map +1 -0
- package/lib/sealedSecret.d.ts +0 -2
- package/lib/sealedSecret.d.ts.map +1 -1
- package/lib/sealedSecret.js +1 -1
- package/package.json +1 -1
- package/src/RunRecord.ts +62 -5
- package/src/SharedHubClient.ts +14 -32
- package/src/State.ts +13 -67
- package/src/store/component/_generated/api.ts +0 -2
- package/src/store/component/_generated/component.ts +5 -74
- package/src/store/component/reader.ts +46 -55
- package/src/store/component/schema.ts +4 -2
- package/src/store/component/state.ts +23 -72
- package/src/store/convex/state.ts +8 -25
- package/src/store/convex/version.ts +3 -1
- package/src/store/sealedSecret.ts +0 -6
- package/lib/sealedSecret-Bf8AeIp_.js.map +0 -1
- package/src/store/component/runRecord.ts +0 -57
package/src/State.ts
CHANGED
|
@@ -33,13 +33,12 @@
|
|
|
33
33
|
// with a store of its own version. An older store is pushed over; a newer one
|
|
34
34
|
// fails the run.
|
|
35
35
|
//
|
|
36
|
-
// The
|
|
37
|
-
//
|
|
38
|
-
//
|
|
39
|
-
//
|
|
36
|
+
// The store holds Alchemy's state interface and nothing else, so a stack can
|
|
37
|
+
// use the state store of Alchemy's Cloudflare package in its place. The run
|
|
38
|
+
// record that Samebase reads is a layer over any state: see `withRunRecord` in
|
|
39
|
+
// src/RunRecord.ts.
|
|
40
40
|
import { dirname, join } from "node:path";
|
|
41
41
|
import { fileURLToPath } from "node:url";
|
|
42
|
-
import * as Clock from "effect/Clock";
|
|
43
42
|
import * as Effect from "effect/Effect";
|
|
44
43
|
import * as Layer from "effect/Layer";
|
|
45
44
|
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
|
|
@@ -81,7 +80,6 @@ import {
|
|
|
81
80
|
withPushKey,
|
|
82
81
|
writeVariables,
|
|
83
82
|
} from "./Push.ts";
|
|
84
|
-
import { type RunRecord, runRecord, withRunRecord } from "./RunRecord.ts";
|
|
85
83
|
import { stateKeyName } from "./store/convex/sealing.ts";
|
|
86
84
|
import { STORE_VERSION } from "./store/convex/version.ts";
|
|
87
85
|
import { newKey, newKeyId } from "./store/sealedSecret.ts";
|
|
@@ -173,7 +171,7 @@ export interface FunctionApi {
|
|
|
173
171
|
/** POST /api/function, the call that `convex run` makes: the reply, which can be a function error. */
|
|
174
172
|
readonly call: (
|
|
175
173
|
path: string,
|
|
176
|
-
args: Record<string, string | boolean |
|
|
174
|
+
args: Record<string, string | boolean | undefined>,
|
|
177
175
|
) => Effect.Effect<Reply, StateStoreError>;
|
|
178
176
|
}
|
|
179
177
|
|
|
@@ -193,20 +191,9 @@ const valueOf = (reply: Reply) =>
|
|
|
193
191
|
: Effect.fail(failure(reply.errorMessage.trim()));
|
|
194
192
|
|
|
195
193
|
/** The functions of the store with Alchemy's encoding: the JSON text of encodeState. */
|
|
196
|
-
const remoteOf = (api: FunctionApi
|
|
197
|
-
const run = (name: string, args: Record<string, string |
|
|
194
|
+
const remoteOf = (api: FunctionApi) => {
|
|
195
|
+
const run = (name: string, args: Record<string, string | undefined>) =>
|
|
198
196
|
api.call(`state:${name}`, args).pipe(Effect.flatMap(valueOf));
|
|
199
|
-
const write = (name: string, args: Record<string, string>) =>
|
|
200
|
-
Effect.gen(function* () {
|
|
201
|
-
const currentRun =
|
|
202
|
-
record === undefined
|
|
203
|
-
? undefined
|
|
204
|
-
: {
|
|
205
|
-
...record,
|
|
206
|
-
recordedAt: new Date(yield* Clock.currentTimeMillis).toISOString(),
|
|
207
|
-
};
|
|
208
|
-
return yield* run(name, { ...args, run: currentRun });
|
|
209
|
-
});
|
|
210
197
|
const strings = (name: string, args: Record<string, string>) =>
|
|
211
198
|
run(name, args).pipe(Effect.flatMap(decode(Schema.Array(Schema.String), name)));
|
|
212
199
|
const optional = (name: string, args: Record<string, string>) =>
|
|
@@ -235,13 +222,13 @@ const remoteOf = (api: FunctionApi, record: RunRecord | undefined) => {
|
|
|
235
222
|
Effect.map((text): unknown => (text === null ? undefined : JSON.parse(text, reviveState))),
|
|
236
223
|
),
|
|
237
224
|
set: (r: { stack: string; stage: string; fqn: string; value: PersistedState }) =>
|
|
238
|
-
|
|
225
|
+
run("set", { stack: r.stack, stage: r.stage, fqn: r.fqn, value: encode(r.value) }),
|
|
239
226
|
delete: (r: { stack: string; stage: string; fqn: string }) =>
|
|
240
|
-
|
|
227
|
+
run("remove", { stack: r.stack, stage: r.stage, fqn: r.fqn }),
|
|
241
228
|
deleteStack: (r: { stack: string; stage?: string }) =>
|
|
242
229
|
run("removeStack", { stack: r.stack, stage: r.stage }),
|
|
243
230
|
setOutput: (r: { stack: string; stage: string; value: unknown }) =>
|
|
244
|
-
|
|
231
|
+
run("setOutput", { stack: r.stack, stage: r.stage, value: encode(r.value) }),
|
|
245
232
|
};
|
|
246
233
|
};
|
|
247
234
|
|
|
@@ -275,8 +262,6 @@ export interface StoreOptions {
|
|
|
275
262
|
readonly scope: Scope.Scope;
|
|
276
263
|
/** The functions directory that an outdated store gets: {@link storeDirectory}. */
|
|
277
264
|
readonly store: string;
|
|
278
|
-
/** The run recorded with resource writes and output, or undefined for a run without one. */
|
|
279
|
-
readonly run: RunRecord | undefined;
|
|
280
265
|
}
|
|
281
266
|
|
|
282
267
|
/**
|
|
@@ -452,22 +437,7 @@ export const makeConvexState = (options: StoreOptions) =>
|
|
|
452
437
|
|
|
453
438
|
yield* ensureSealed(options, deployment, functions);
|
|
454
439
|
|
|
455
|
-
const remote = remoteOf(functions
|
|
456
|
-
/** The stack's output plus the record, when the run has one and the output has a place for it. */
|
|
457
|
-
const documentOf = (request: { stack: string; stage: string; value: unknown }) =>
|
|
458
|
-
Effect.gen(function* () {
|
|
459
|
-
const { run } = options;
|
|
460
|
-
if (run === undefined) return request.value;
|
|
461
|
-
const recordedAt = new Date(yield* Clock.currentTimeMillis).toISOString();
|
|
462
|
-
const document = withRunRecord(request.value, { ...run, recordedAt });
|
|
463
|
-
if (document === undefined) {
|
|
464
|
-
yield* Effect.logWarning(
|
|
465
|
-
`Convex state: the output of ${request.stack}/${request.stage} is not a plain object, so it carries no run record.`,
|
|
466
|
-
);
|
|
467
|
-
return request.value;
|
|
468
|
-
}
|
|
469
|
-
return document;
|
|
470
|
-
});
|
|
440
|
+
const remote = remoteOf(functions);
|
|
471
441
|
const service: StateService = {
|
|
472
442
|
id: "convex",
|
|
473
443
|
getVersion: () => Effect.succeed(STATE_STORE_VERSION),
|
|
@@ -480,13 +450,7 @@ export const makeConvexState = (options: StoreOptions) =>
|
|
|
480
450
|
set: (request) => remote.set(request).pipe(Effect.as(request.value)),
|
|
481
451
|
delete: (request) => remote.delete(request).pipe(Effect.asVoid),
|
|
482
452
|
deleteStack: (request) => remote.deleteStack(request).pipe(Effect.asVoid),
|
|
483
|
-
|
|
484
|
-
setOutput: (request) =>
|
|
485
|
-
Effect.gen(function* () {
|
|
486
|
-
const value = yield* documentOf(request);
|
|
487
|
-
yield* remote.setOutput({ stack: request.stack, stage: request.stage, value });
|
|
488
|
-
return value;
|
|
489
|
-
}),
|
|
453
|
+
setOutput: (request) => remote.setOutput(request).pipe(Effect.as(request.value)),
|
|
490
454
|
};
|
|
491
455
|
return service;
|
|
492
456
|
});
|
|
@@ -511,25 +475,7 @@ export const state = (): Layer.Layer<State, never, ChildProcessSpawner.ChildProc
|
|
|
511
475
|
const api = yield* ManagementApi.pipe(
|
|
512
476
|
Effect.provide(ManagementApiLive().pipe(Layer.provide(fromConfig()))),
|
|
513
477
|
);
|
|
514
|
-
|
|
515
|
-
// that gives none, such as a tarball without git, a checkout
|
|
516
|
-
// without origin, or a detached HEAD, still reads, plans, and
|
|
517
|
-
// destroys: its outputs are written without the record, and the
|
|
518
|
-
// log says why.
|
|
519
|
-
const run = yield* runRecord({ cwd: process.cwd(), env: process.env }).pipe(
|
|
520
|
-
Effect.catch((error) =>
|
|
521
|
-
Effect.logWarning(
|
|
522
|
-
`Convex state: the stage outputs of this run carry no record. ${error.message}`,
|
|
523
|
-
).pipe(Effect.as(undefined)),
|
|
524
|
-
),
|
|
525
|
-
);
|
|
526
|
-
return yield* makeConvexState({
|
|
527
|
-
api,
|
|
528
|
-
spawner,
|
|
529
|
-
scope,
|
|
530
|
-
store: storeDirectory(),
|
|
531
|
-
run,
|
|
532
|
-
});
|
|
478
|
+
return yield* makeConvexState({ api, spawner, scope, store: storeDirectory() });
|
|
533
479
|
}).pipe(Effect.provideContext(context), Effect.orDie),
|
|
534
480
|
);
|
|
535
481
|
}),
|
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
|
|
11
11
|
import type * as keys from "../keys.js";
|
|
12
12
|
import type * as reader from "../reader.js";
|
|
13
|
-
import type * as runRecord from "../runRecord.js";
|
|
14
13
|
import type * as state from "../state.js";
|
|
15
14
|
|
|
16
15
|
import type {
|
|
@@ -23,7 +22,6 @@ import { anyApi, componentsGeneric } from "convex/server";
|
|
|
23
22
|
const fullApi: ApiFromModules<{
|
|
24
23
|
keys: typeof keys;
|
|
25
24
|
reader: typeof reader;
|
|
26
|
-
runRecord: typeof runRecord;
|
|
27
25
|
state: typeof state;
|
|
28
26
|
}> = anyApi as any;
|
|
29
27
|
|
|
@@ -62,20 +62,8 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
62
62
|
stacks: Array<{
|
|
63
63
|
name: string;
|
|
64
64
|
stages: Array<{
|
|
65
|
-
deployedHeadCommit: string | null;
|
|
66
65
|
name: string;
|
|
67
|
-
output: string;
|
|
68
|
-
record: {
|
|
69
|
-
branch: string;
|
|
70
|
-
headCommit: string;
|
|
71
|
-
pullRequest: number | null;
|
|
72
|
-
recordedAt: string;
|
|
73
|
-
repository: string;
|
|
74
|
-
runUrl: string | null;
|
|
75
|
-
source: "github-actions" | "checkout";
|
|
76
|
-
stackFile: string;
|
|
77
|
-
version: 1;
|
|
78
|
-
};
|
|
66
|
+
output: string | null;
|
|
79
67
|
resources: Array<{ fqn: string; value: string }>;
|
|
80
68
|
}>;
|
|
81
69
|
}>;
|
|
@@ -123,20 +111,9 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
123
111
|
{ stack: string },
|
|
124
112
|
{
|
|
125
113
|
stages: Array<{
|
|
114
|
+
name: string;
|
|
126
115
|
output: string | null;
|
|
127
|
-
record: {
|
|
128
|
-
branch: string;
|
|
129
|
-
headCommit: string;
|
|
130
|
-
pullRequest: number | null;
|
|
131
|
-
recordedAt: string;
|
|
132
|
-
repository: string;
|
|
133
|
-
runUrl: string | null;
|
|
134
|
-
source: "github-actions" | "checkout";
|
|
135
|
-
stackFile: string;
|
|
136
|
-
version: 1;
|
|
137
|
-
} | null;
|
|
138
116
|
resources: Array<{ fqn: string; value: string }>;
|
|
139
|
-
stage: string;
|
|
140
117
|
}>;
|
|
141
118
|
},
|
|
142
119
|
Name
|
|
@@ -144,22 +121,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
144
121
|
remove: FunctionReference<
|
|
145
122
|
"mutation",
|
|
146
123
|
"internal",
|
|
147
|
-
{
|
|
148
|
-
fqn: string;
|
|
149
|
-
run: {
|
|
150
|
-
branch: string;
|
|
151
|
-
headCommit: string;
|
|
152
|
-
pullRequest: number | null;
|
|
153
|
-
recordedAt: string;
|
|
154
|
-
repository: string;
|
|
155
|
-
runUrl: string | null;
|
|
156
|
-
source: "github-actions" | "checkout";
|
|
157
|
-
stackFile: string;
|
|
158
|
-
version: 1;
|
|
159
|
-
} | null;
|
|
160
|
-
stack: string;
|
|
161
|
-
stage: string;
|
|
162
|
-
},
|
|
124
|
+
{ fqn: string; stack: string; stage: string },
|
|
163
125
|
null,
|
|
164
126
|
Name
|
|
165
127
|
>;
|
|
@@ -223,45 +185,14 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
223
185
|
set: FunctionReference<
|
|
224
186
|
"mutation",
|
|
225
187
|
"internal",
|
|
226
|
-
{
|
|
227
|
-
fqn: string;
|
|
228
|
-
run: {
|
|
229
|
-
branch: string;
|
|
230
|
-
headCommit: string;
|
|
231
|
-
pullRequest: number | null;
|
|
232
|
-
recordedAt: string;
|
|
233
|
-
repository: string;
|
|
234
|
-
runUrl: string | null;
|
|
235
|
-
source: "github-actions" | "checkout";
|
|
236
|
-
stackFile: string;
|
|
237
|
-
version: 1;
|
|
238
|
-
} | null;
|
|
239
|
-
stack: string;
|
|
240
|
-
stage: string;
|
|
241
|
-
value: string;
|
|
242
|
-
},
|
|
188
|
+
{ fqn: string; stack: string; stage: string; value: string },
|
|
243
189
|
null,
|
|
244
190
|
Name
|
|
245
191
|
>;
|
|
246
192
|
setOutput: FunctionReference<
|
|
247
193
|
"mutation",
|
|
248
194
|
"internal",
|
|
249
|
-
{
|
|
250
|
-
run: {
|
|
251
|
-
branch: string;
|
|
252
|
-
headCommit: string;
|
|
253
|
-
pullRequest: number | null;
|
|
254
|
-
recordedAt: string;
|
|
255
|
-
repository: string;
|
|
256
|
-
runUrl: string | null;
|
|
257
|
-
source: "github-actions" | "checkout";
|
|
258
|
-
stackFile: string;
|
|
259
|
-
version: 1;
|
|
260
|
-
} | null;
|
|
261
|
-
stack: string;
|
|
262
|
-
stage: string;
|
|
263
|
-
value: string;
|
|
264
|
-
},
|
|
195
|
+
{ stack: string; stage: string; value: string },
|
|
265
196
|
null,
|
|
266
197
|
Name
|
|
267
198
|
>;
|
|
@@ -1,35 +1,36 @@
|
|
|
1
1
|
// The redacted read of one repository. The app checks the reader's verified
|
|
2
2
|
// token and passes the repository that the token names: a component has no
|
|
3
|
-
// `ctx.auth`.
|
|
4
|
-
|
|
3
|
+
// `ctx.auth`. The reply is Alchemy state only, in the stage shape of
|
|
4
|
+
// `readStack`: the reader takes anything else, such as a run record, from the
|
|
5
|
+
// outputs.
|
|
6
|
+
import { v } from "convex/values";
|
|
5
7
|
// oxlint-disable-next-line samebase/no-raw-public-convex-function-builders -- Store entrypoints have no app wrapper; the reader and the deploy key enforce access.
|
|
6
8
|
import { query } from "./_generated/server.js";
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
const stage = v.object({
|
|
10
|
-
name: v.string(),
|
|
11
|
-
record: runRecord,
|
|
12
|
-
deployedHeadCommit: v.union(v.string(), v.null()),
|
|
13
|
-
resources: v.array(v.object({ fqn: v.string(), value: v.string() })),
|
|
14
|
-
output: v.string(),
|
|
15
|
-
});
|
|
9
|
+
import { storedStage } from "./state.ts";
|
|
16
10
|
|
|
17
11
|
/** The reply of {@link repository}. */
|
|
18
12
|
export const repositoryReply = v.object({
|
|
19
13
|
store: v.literal("found"),
|
|
20
|
-
stacks: v.array(v.object({ name: v.string(), stages: v.array(
|
|
14
|
+
stacks: v.array(v.object({ name: v.string(), stages: v.array(storedStage) })),
|
|
21
15
|
});
|
|
22
16
|
|
|
23
17
|
/** Alchemy encodes a secret as an object with __redacted__; discard that whole object's contents. */
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
const withoutSecrets = (text: string) =>
|
|
19
|
+
JSON.stringify(
|
|
20
|
+
JSON.parse(text, (_key, value: unknown) =>
|
|
21
|
+
typeof value === "object" && value !== null && "__redacted__" in value
|
|
22
|
+
? { __redacted__: null }
|
|
23
|
+
: value,
|
|
24
|
+
),
|
|
29
25
|
);
|
|
30
|
-
}
|
|
31
26
|
|
|
32
|
-
|
|
27
|
+
const byName = <T extends { name: string }>(a: T, b: T) =>
|
|
28
|
+
a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The stacks of `repository`: the stack of that name and the stacks below `repository + "/"`, each
|
|
32
|
+
* with every stage that has a resource or an output, in name order. Resources stay in fqn order.
|
|
33
|
+
*/
|
|
33
34
|
export const repository = query({
|
|
34
35
|
args: { repository: v.string() },
|
|
35
36
|
returns: repositoryReply,
|
|
@@ -55,51 +56,41 @@ export const repository = query({
|
|
|
55
56
|
.withIndex("by_stage", (q) => q.gte("stack", `${name}/`).lt("stack", `${name}0`))
|
|
56
57
|
.collect(),
|
|
57
58
|
]);
|
|
58
|
-
const stacks = new Map<
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
let stages = stacks.get(row.stack);
|
|
59
|
+
const stacks = new Map<
|
|
60
|
+
string,
|
|
61
|
+
Map<
|
|
62
|
+
string,
|
|
63
|
+
{ name: string; resources: { fqn: string; value: string }[]; output: string | null }
|
|
64
|
+
>
|
|
65
|
+
>();
|
|
66
|
+
const stageOf = (stack: string, stage: string) => {
|
|
67
|
+
let stages = stacks.get(stack);
|
|
69
68
|
if (stages === undefined) {
|
|
70
69
|
stages = new Map();
|
|
71
|
-
stacks.set(
|
|
70
|
+
stacks.set(stack, stages);
|
|
72
71
|
}
|
|
73
|
-
stages.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
72
|
+
let found = stages.get(stage);
|
|
73
|
+
if (found === undefined) {
|
|
74
|
+
found = { name: stage, resources: [], output: null };
|
|
75
|
+
stages.set(stage, found);
|
|
76
|
+
}
|
|
77
|
+
return found;
|
|
78
|
+
};
|
|
79
|
+
for (const row of [...rootResources, ...childResources]) {
|
|
80
|
+
stageOf(row.stack, row.stage).resources.push({
|
|
81
|
+
fqn: row.fqn,
|
|
82
|
+
value: withoutSecrets(row.value),
|
|
82
83
|
});
|
|
83
84
|
}
|
|
84
|
-
for (const row of [...
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
?.get(row.stage)
|
|
88
|
-
?.resources.push({
|
|
89
|
-
fqn: row.fqn,
|
|
90
|
-
value: JSON.stringify(withoutSecrets(row.value)),
|
|
91
|
-
});
|
|
85
|
+
for (const row of [...rootOutputs, ...childOutputs]) {
|
|
86
|
+
const stage = stageOf(row.stack, row.stage);
|
|
87
|
+
if (row.value !== undefined) stage.output = withoutSecrets(row.value);
|
|
92
88
|
}
|
|
93
89
|
return {
|
|
94
90
|
store: "found" as const,
|
|
95
91
|
stacks: [...stacks]
|
|
96
|
-
.
|
|
97
|
-
.
|
|
98
|
-
name,
|
|
99
|
-
stages: [...stages.values()].sort((a, b) =>
|
|
100
|
-
a.name < b.name ? -1 : a.name > b.name ? 1 : 0,
|
|
101
|
-
),
|
|
102
|
-
})),
|
|
92
|
+
.map(([stack, stages]) => ({ name: stack, stages: [...stages.values()].sort(byName) }))
|
|
93
|
+
.sort(byName),
|
|
103
94
|
};
|
|
104
95
|
},
|
|
105
96
|
});
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { defineSchema, defineTable } from "convex/server";
|
|
2
2
|
import { v } from "convex/values";
|
|
3
|
-
import { runRecord } from "./runRecord.ts";
|
|
4
3
|
|
|
5
4
|
// `value` is the JSON text of Alchemy's encodeState(...): the same text that
|
|
6
5
|
// Alchemy's local state writes to a file. A Convex object would reject keys
|
|
@@ -15,8 +14,11 @@ export default defineSchema({
|
|
|
15
14
|
outputs: defineTable({
|
|
16
15
|
stack: v.string(),
|
|
17
16
|
stage: v.string(),
|
|
17
|
+
// Store version 6 and older wrote `currentRun` on each resource write, and an output row that
|
|
18
|
+
// had only that field had no `value`. Nothing reads or writes `currentRun` now. A push checks
|
|
19
|
+
// every row against this schema, so both declarations stay for the rows of an older store.
|
|
18
20
|
value: v.optional(v.string()),
|
|
19
|
-
currentRun: v.optional(
|
|
21
|
+
currentRun: v.optional(v.any()),
|
|
20
22
|
}).index("by_stage", ["stack", "stage"]),
|
|
21
23
|
// One row: the key that seals new secrets, the count of its rotations, and the last count for
|
|
22
24
|
// which a reseal finished.
|
|
@@ -5,12 +5,11 @@
|
|
|
5
5
|
//
|
|
6
6
|
// A stage holds one row for each resource of its stack, so the reads of one
|
|
7
7
|
// stage collect all rows.
|
|
8
|
-
import { v, type
|
|
9
|
-
import type {
|
|
8
|
+
import { v, type Validator } from "convex/values";
|
|
9
|
+
import type { QueryCtx } from "./_generated/server.js";
|
|
10
10
|
// oxlint-disable-next-line samebase/no-raw-public-convex-function-builders -- Store entrypoints have no app wrapper; the reader and the deploy key enforce access.
|
|
11
11
|
import { mutation, query } from "./_generated/server.js";
|
|
12
12
|
import { generationOf } from "./keys.ts";
|
|
13
|
-
import { runChanged, runRecord } from "./runRecord.ts";
|
|
14
13
|
|
|
15
14
|
const stage = { stack: v.string(), stage: v.string() };
|
|
16
15
|
const resource = { ...stage, fqn: v.string() };
|
|
@@ -35,41 +34,6 @@ const outputOf = (ctx: QueryCtx, args: { stack: string; stage: string }) =>
|
|
|
35
34
|
.withIndex("by_stage", (q) => q.eq("stack", args.stack).eq("stage", args.stage))
|
|
36
35
|
.unique();
|
|
37
36
|
|
|
38
|
-
/**
|
|
39
|
-
* The run record of a write, or null for a write without one. The app's
|
|
40
|
-
* functions take it as an optional field; across the component boundary it is
|
|
41
|
-
* a required field, because the generated component API types an optional
|
|
42
|
-
* field without `undefined`.
|
|
43
|
-
*/
|
|
44
|
-
const run = v.union(runRecord, v.null());
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* A resource write and its current run become visible in one transaction. The output row of the
|
|
48
|
-
* stage changes only when the run changes (see `runChanged`). Alchemy writes the resources of a
|
|
49
|
-
* deploy at the same time, and a write to the one output row of the stage made all of them conflict
|
|
50
|
-
* until Convex stopped the retries. A write of the same run only reads the row, and reads do not
|
|
51
|
-
* conflict. So `recordedAt` stays the time of the first write of the run, until `setOutput` writes
|
|
52
|
-
* it again.
|
|
53
|
-
*/
|
|
54
|
-
const writeCurrentRun = async (
|
|
55
|
-
ctx: MutationCtx,
|
|
56
|
-
args: { stack: string; stage: string; run: Infer<typeof run> },
|
|
57
|
-
) => {
|
|
58
|
-
const output = await outputOf(ctx, args);
|
|
59
|
-
if (output === null) {
|
|
60
|
-
if (args.run !== null)
|
|
61
|
-
await ctx.db.insert("outputs", {
|
|
62
|
-
stack: args.stack,
|
|
63
|
-
stage: args.stage,
|
|
64
|
-
currentRun: args.run,
|
|
65
|
-
});
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
// A write without a run record clears the record of the previous run.
|
|
69
|
-
if (runChanged(output.currentRun, args.run))
|
|
70
|
-
await ctx.db.patch("outputs", output._id, { currentRun: args.run ?? undefined });
|
|
71
|
-
};
|
|
72
|
-
|
|
73
37
|
/** The sorted union of two lists of names. */
|
|
74
38
|
const union = (a: readonly string[], b: readonly string[]) => [...new Set([...a, ...b])].sort();
|
|
75
39
|
|
|
@@ -178,8 +142,9 @@ export const replaced = query({
|
|
|
178
142
|
}),
|
|
179
143
|
});
|
|
180
144
|
|
|
145
|
+
/** A resource write reads and writes only the row of that resource. */
|
|
181
146
|
export const set = mutation({
|
|
182
|
-
args: { ...resource, value: v.string()
|
|
147
|
+
args: { ...resource, value: v.string() },
|
|
183
148
|
returns: v.null(),
|
|
184
149
|
handler: async (ctx, args) => {
|
|
185
150
|
const row = await rowOf(ctx, args);
|
|
@@ -191,18 +156,16 @@ export const set = mutation({
|
|
|
191
156
|
value: args.value,
|
|
192
157
|
});
|
|
193
158
|
else await ctx.db.patch("resources", row._id, { value: args.value });
|
|
194
|
-
await writeCurrentRun(ctx, args);
|
|
195
159
|
return null;
|
|
196
160
|
},
|
|
197
161
|
});
|
|
198
162
|
|
|
199
163
|
export const remove = mutation({
|
|
200
|
-
args:
|
|
164
|
+
args: resource,
|
|
201
165
|
returns: v.null(),
|
|
202
166
|
handler: async (ctx, args) => {
|
|
203
167
|
const row = await rowOf(ctx, args);
|
|
204
168
|
if (row !== null) await ctx.db.delete("resources", row._id);
|
|
205
|
-
await writeCurrentRun(ctx, args);
|
|
206
169
|
return null;
|
|
207
170
|
},
|
|
208
171
|
});
|
|
@@ -238,23 +201,13 @@ export const getOutput = query({
|
|
|
238
201
|
});
|
|
239
202
|
|
|
240
203
|
export const setOutput = mutation({
|
|
241
|
-
args: { ...stage, value: v.string()
|
|
204
|
+
args: { ...stage, value: v.string() },
|
|
242
205
|
returns: v.null(),
|
|
243
206
|
handler: async (ctx, args) => {
|
|
244
207
|
const row = await outputOf(ctx, args);
|
|
245
|
-
|
|
246
|
-
if (row !== null)
|
|
247
|
-
await ctx.db.patch("outputs", row._id, {
|
|
248
|
-
value: args.value,
|
|
249
|
-
currentRun: args.run ?? undefined,
|
|
250
|
-
});
|
|
208
|
+
if (row !== null) await ctx.db.patch("outputs", row._id, { value: args.value });
|
|
251
209
|
else
|
|
252
|
-
await ctx.db.insert("outputs", {
|
|
253
|
-
stack: args.stack,
|
|
254
|
-
stage: args.stage,
|
|
255
|
-
value: args.value,
|
|
256
|
-
...(args.run === null ? {} : { currentRun: args.run }),
|
|
257
|
-
});
|
|
210
|
+
await ctx.db.insert("outputs", { stack: args.stack, stage: args.stage, value: args.value });
|
|
258
211
|
return null;
|
|
259
212
|
},
|
|
260
213
|
});
|
|
@@ -286,7 +239,7 @@ export const resourcePage = query({
|
|
|
286
239
|
},
|
|
287
240
|
});
|
|
288
241
|
|
|
289
|
-
/** One bounded page of the outputs. A row
|
|
242
|
+
/** One bounded page of the outputs. A row that store version 6 left without a value is left out. */
|
|
290
243
|
export const outputPage = query({
|
|
291
244
|
args: { cursor: v.union(v.string(), v.null()) },
|
|
292
245
|
returns: pageOf(v.object({ ...stage, value: v.string() })),
|
|
@@ -310,10 +263,7 @@ export const outputPage = query({
|
|
|
310
263
|
*/
|
|
311
264
|
const replaceResult = v.union(v.literal("replaced"), v.literal("changed"), v.literal("stale"));
|
|
312
265
|
|
|
313
|
-
/**
|
|
314
|
-
* Replaces the value of a row that still holds `expected`, for a reseal of `generation`: the row
|
|
315
|
-
* keeps its run record.
|
|
316
|
-
*/
|
|
266
|
+
/** Replaces the value of a row that still holds `expected`, for a reseal of `generation`. */
|
|
317
267
|
export const replaceResource = mutation({
|
|
318
268
|
args: { ...resource, expected: v.string(), value: v.string(), generation: v.number() },
|
|
319
269
|
returns: replaceResult,
|
|
@@ -339,18 +289,20 @@ export const replaceOutput = mutation({
|
|
|
339
289
|
},
|
|
340
290
|
});
|
|
341
291
|
|
|
342
|
-
/**
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
),
|
|
292
|
+
/**
|
|
293
|
+
* One stage as the store holds it: its resources in fqn order and its output, as JSON texts, with
|
|
294
|
+
* `output: null` for a stage without one. {@link readStack} and the repository reader give stages in
|
|
295
|
+
* this shape, and the shape is Alchemy state only: a reader takes anything else from the output.
|
|
296
|
+
*/
|
|
297
|
+
export const storedStage = v.object({
|
|
298
|
+
name: v.string(),
|
|
299
|
+
resources: v.array(v.object({ fqn: v.string(), value: v.string() })),
|
|
300
|
+
output: v.union(v.string(), v.null()),
|
|
352
301
|
});
|
|
353
302
|
|
|
303
|
+
/** The reply of {@link readStack}. */
|
|
304
|
+
export const stackReply = v.object({ stages: v.array(storedStage) });
|
|
305
|
+
|
|
354
306
|
/**
|
|
355
307
|
* One call for a reader: every stage of a stack in name order, each with its
|
|
356
308
|
* resources in fqn order and its output, as the JSON texts that the rows
|
|
@@ -381,12 +333,11 @@ export const readStack = query({
|
|
|
381
333
|
stages: stages.map((stage) => {
|
|
382
334
|
const output = outputs.find((row) => row.stage === stage);
|
|
383
335
|
return {
|
|
384
|
-
stage,
|
|
336
|
+
name: stage,
|
|
385
337
|
resources: resources
|
|
386
338
|
.filter((row) => row.stage === stage)
|
|
387
339
|
.map((row) => ({ fqn: row.fqn, value: row.value })),
|
|
388
340
|
output: output?.value ?? null,
|
|
389
|
-
record: output?.currentRun ?? null,
|
|
390
341
|
};
|
|
391
342
|
}),
|
|
392
343
|
};
|