@samebase/alchemy-convex 0.33.0 → 0.35.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 +17 -10
- package/lib/{State-CuktZaOh.js → State-ceE-WFI3.js} +52 -51
- package/lib/{State-CuktZaOh.js.map → State-ceE-WFI3.js.map} +1 -1
- package/lib/cli.js +1 -1
- package/lib/index.d.ts +30 -7
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +16 -15
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/RunRecord.ts +62 -5
- package/src/SharedHubClient.ts +58 -36
- package/src/State.ts +13 -67
- package/src/index.ts +1 -0
- 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/component/runRecord.ts +0 -57
package/src/SharedHubClient.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// Convex.SharedHubClient: one deployment of an app as a client of the
|
|
2
|
-
// app that deploys in its own GitHub Actions run, on any host, and in development as a
|
|
3
|
-
// same stack.
|
|
1
|
+
// Convex.SharedHubClient: one deployment of an app as a client of the identity hub that the stack
|
|
2
|
+
// names, for an app that deploys in its own GitHub Actions run, on any host, and in development as a
|
|
3
|
+
// stage of the same stack.
|
|
4
4
|
//
|
|
5
5
|
// A run in GitHub Actions asks the hub for a new client with a GitHub OIDC token, so the stack needs
|
|
6
6
|
// no key and no Samebase account. The token names the repository. The hub reads
|
|
@@ -51,9 +51,28 @@ const OFF_VALUES = { issuer: "http://127.0.0.1:1", clientId: "no-hub", clientSec
|
|
|
51
51
|
/** The name of a connector on the hub. */
|
|
52
52
|
const CONNECTOR_NAME = /^[a-z0-9-]{1,64}$/;
|
|
53
53
|
|
|
54
|
+
/**
|
|
55
|
+
* An identity hub of @samebase/identity-broker, such as Samebase's:
|
|
56
|
+
* `{ issuer: "https://auth.samebase.com", terms: "https://samebase.com/terms" }`.
|
|
57
|
+
*/
|
|
58
|
+
export interface Hub {
|
|
59
|
+
/**
|
|
60
|
+
* The hub's issuer, such as `https://auth.example.com`. The run asks at `<issuer>/hub-clients`,
|
|
61
|
+
* with a GitHub token whose audience is the issuer, so no other hub can use that token.
|
|
62
|
+
*/
|
|
63
|
+
readonly issuer: string;
|
|
64
|
+
/**
|
|
65
|
+
* The terms of the hub's sign-in service, which its answer must name. The log prints them for a
|
|
66
|
+
* new client, so no value that the answer chose reaches the log.
|
|
67
|
+
*/
|
|
68
|
+
readonly terms: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
54
71
|
interface CommonProps {
|
|
55
72
|
/** The stable name of the app, set once in the stack. */
|
|
56
73
|
readonly app: string;
|
|
74
|
+
/** The hub to register at. A changed hub registers a new client there; the old one stays. */
|
|
75
|
+
readonly hub: Hub;
|
|
57
76
|
/**
|
|
58
77
|
* The connectors the client signs in through, by their names on the hub, such as
|
|
59
78
|
* `["google", "github"]`: at least one, each of `[a-z0-9-]{1,64}`. The hub refuses a name it does
|
|
@@ -191,11 +210,8 @@ const NewClient = Schema.Struct({
|
|
|
191
210
|
redirect_uri: Schema.NonEmptyString,
|
|
192
211
|
/** The set the hub recorded, which must be the set the request sent. */
|
|
193
212
|
connectors: Schema.Array(Schema.String),
|
|
194
|
-
/**
|
|
195
|
-
|
|
196
|
-
* this page: the log line must not print a value that the answer chose.
|
|
197
|
-
*/
|
|
198
|
-
terms: Schema.Literal("https://samebase.com/terms"),
|
|
213
|
+
/** The terms of the hub's sign-in service, which must be the `terms` of the stack's `hub`. */
|
|
214
|
+
terms: Schema.String,
|
|
199
215
|
/** Only in the answer to a production registration with the GitHub token. */
|
|
200
216
|
initial_access_token: Schema.optionalKey(Schema.NonEmptyString),
|
|
201
217
|
repository: Schema.optionalKey(Schema.String.check(Schema.isPattern(/^\d+$/))),
|
|
@@ -293,12 +309,15 @@ const decode =
|
|
|
293
309
|
Effect.mapError(() => new SharedHubClientFailed({ reason: `${who} gave a bad answer` })),
|
|
294
310
|
);
|
|
295
311
|
|
|
296
|
-
/**
|
|
297
|
-
|
|
312
|
+
/**
|
|
313
|
+
* The OIDC token of this run for `audience`, read right before the call, because it lives for
|
|
314
|
+
* minutes only.
|
|
315
|
+
*/
|
|
316
|
+
const readOidcToken = (requestUrl: string, requestToken: string, audience: string) =>
|
|
298
317
|
Effect.gen(function* () {
|
|
299
318
|
const { body, status } = yield* send(
|
|
300
319
|
"GitHub",
|
|
301
|
-
`${requestUrl}&audience
|
|
320
|
+
`${requestUrl}&audience=${encodeURIComponent(audience)}`,
|
|
302
321
|
requestToken,
|
|
303
322
|
{ method: "GET" },
|
|
304
323
|
);
|
|
@@ -317,20 +336,16 @@ const requestNewClient = (
|
|
|
317
336
|
bearer: string,
|
|
318
337
|
) =>
|
|
319
338
|
Effect.gen(function* () {
|
|
320
|
-
const {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
connectors,
|
|
331
|
-
}),
|
|
332
|
-
},
|
|
333
|
-
);
|
|
339
|
+
const { hub } = props;
|
|
340
|
+
const { body, status } = yield* send("The identity hub", `${hub.issuer}/hub-clients`, bearer, {
|
|
341
|
+
method: "POST",
|
|
342
|
+
body: JSON.stringify({
|
|
343
|
+
app: props.app,
|
|
344
|
+
redirectUri: props.redirectUri,
|
|
345
|
+
softwareVersion: props.softwareVersion ?? "unknown",
|
|
346
|
+
connectors,
|
|
347
|
+
}),
|
|
348
|
+
});
|
|
334
349
|
if (status !== 201) {
|
|
335
350
|
const reason =
|
|
336
351
|
status === 401 && props.deploymentType === "development"
|
|
@@ -339,19 +354,25 @@ const requestNewClient = (
|
|
|
339
354
|
return yield* new SharedHubClientFailed({ reason: `${reason} (HTTP ${status})` });
|
|
340
355
|
}
|
|
341
356
|
const client = yield* decode(NewClient, "The identity hub")(body);
|
|
342
|
-
// The hub registers the address and the connectors that the request sent
|
|
343
|
-
// hold the
|
|
344
|
-
|
|
357
|
+
// The hub registers the address and the connectors that the request sent, under its own issuer
|
|
358
|
+
// and the terms that the stack names. The log and the state hold the stack's, so no value that
|
|
359
|
+
// the answer chose is printed or kept.
|
|
360
|
+
if (
|
|
361
|
+
client.issuer !== hub.issuer ||
|
|
362
|
+
client.redirect_uri !== props.redirectUri ||
|
|
363
|
+
!sameSet(client.connectors, connectors) ||
|
|
364
|
+
client.terms !== hub.terms
|
|
365
|
+
) {
|
|
345
366
|
return yield* new SharedHubClientFailed({ reason: "The identity hub gave a bad answer" });
|
|
346
367
|
}
|
|
347
368
|
yield* Effect.logInfo(
|
|
348
|
-
`Convex.SharedHubClient: ${props.app} on ${props.redirectUri} has a new client of the identity hub. Its use is under ${
|
|
369
|
+
`Convex.SharedHubClient: ${props.app} on ${props.redirectUri} has a new client of the identity hub. Its use is under ${hub.terms}.`,
|
|
349
370
|
);
|
|
350
371
|
// Only a production registration gives the app's token (RFC 7591); any other answer's is dropped.
|
|
351
372
|
const production = props.deploymentType === "production";
|
|
352
373
|
return {
|
|
353
374
|
status: "on" as const,
|
|
354
|
-
issuer:
|
|
375
|
+
issuer: hub.issuer,
|
|
355
376
|
clientId: client.client_id,
|
|
356
377
|
clientSecret: Redacted.make(client.client_secret),
|
|
357
378
|
registrationAccessToken: Redacted.make(client.registration_access_token),
|
|
@@ -398,7 +419,7 @@ const newClient = (props: SharedHubClientProps, connectors: readonly string[]) =
|
|
|
398
419
|
: Effect.logInfo(`${label}: this run has no GitHub OIDC token.`)
|
|
399
420
|
).pipe(Effect.as(off));
|
|
400
421
|
}
|
|
401
|
-
return request(readOidcToken(requestUrl, requestToken));
|
|
422
|
+
return request(readOidcToken(requestUrl, requestToken, props.hub.issuer));
|
|
402
423
|
};
|
|
403
424
|
|
|
404
425
|
/**
|
|
@@ -533,10 +554,10 @@ export const SharedHubClientProvider = () =>
|
|
|
533
554
|
|
|
534
555
|
read: ({ output }) => Effect.succeed(output),
|
|
535
556
|
|
|
536
|
-
// Only a client of the same return address, type, and connector set is
|
|
537
|
-
// without this version's shape, such as one that 0.32 or earlier saved, is
|
|
538
|
-
// asks for a new one, which replaces the saved client, and for a production
|
|
539
|
-
// replaces the app's initial access token.
|
|
557
|
+
// Only a client of the same hub (its issuer), return address, type, and connector set is
|
|
558
|
+
// renewed. A saved row without this version's shape, such as one that 0.32 or earlier saved, is
|
|
559
|
+
// no client: the run asks for a new one, which replaces the saved client, and for a production
|
|
560
|
+
// client also replaces the app's initial access token.
|
|
540
561
|
reconcile: Effect.fn(function* ({ news, output }) {
|
|
541
562
|
// A bad prop is a mistake of the stack, not of the hub: the run fails, with no request.
|
|
542
563
|
if (
|
|
@@ -558,7 +579,8 @@ export const SharedHubClientProvider = () =>
|
|
|
558
579
|
);
|
|
559
580
|
}
|
|
560
581
|
const renewed =
|
|
561
|
-
saved?.
|
|
582
|
+
saved?.issuer === news.hub.issuer &&
|
|
583
|
+
saved.redirectUri === news.redirectUri &&
|
|
562
584
|
saved.deploymentType === news.deploymentType &&
|
|
563
585
|
sameSet(saved.connectors, connectors)
|
|
564
586
|
? yield* renew(saved, news.required === true)
|
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
|
}),
|
package/src/index.ts
CHANGED
|
@@ -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.
|