@geekmidas/cloud 10.0.0-alpha.13 → 10.0.0-alpha.131

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.
@@ -19,6 +19,8 @@ import type {
19
19
  Declaration,
20
20
  DeclarationKind,
21
21
  Dependency,
22
+ SecretDeclaration,
23
+ SecretFormat,
22
24
  SiteDeclaration,
23
25
  } from '@geekmidas/manifest';
24
26
  import {
@@ -26,11 +28,16 @@ import {
26
28
  cookieDomain,
27
29
  DEFAULT_POSTGRES_VERSION,
28
30
  dependentsOf,
31
+ mobileOrigins,
29
32
  PUBLIC,
30
33
  PUBLIC_PREFIX,
31
34
  providedKeyFor,
32
35
  provisionOrder,
36
+ SECRET_LENGTH,
37
+ SECRET_SYMBOLS,
38
+ schemeBase,
33
39
  } from '@geekmidas/manifest';
40
+ import { RandomPassword } from '@pulumi/random';
34
41
  import {
35
42
  Cache,
36
43
  CacheIsAmbiguous,
@@ -39,15 +46,19 @@ import {
39
46
  withCacheTable,
40
47
  } from './aws/Cache';
41
48
  import { Credential } from './aws/Credential';
42
- import { Database, DatabaseNeedsVpc } from './aws/Database';
49
+ import { Cron, type CronSchedule } from './aws/Cron';
50
+ import { Database, DatabaseNeedsVpc, type DatabaseProps } from './aws/Database';
43
51
  import { DatabaseBootstrap } from './aws/DatabaseBootstrap';
44
52
  import { DatabaseReader, DatabaseSchema } from './aws/DerivedDatabase';
45
53
  import { Email, EmailNeedsSender } from './aws/Email';
54
+ import { Encryption, type EncryptionProps } from './aws/Encryption';
55
+ import { ExternalApi, type ExternalApiProps } from './aws/ExternalApi';
46
56
  import { FileServer } from './aws/FileServer';
57
+ import { Function } from './aws/Function';
47
58
  import { ObjectStorage } from './aws/ObjectStorage';
48
59
  import { Queue } from './aws/Queue';
49
60
  import { RestApiSurface } from './aws/RestApiSurface';
50
- import { Secret } from './aws/Secret';
61
+ import { Secret, type SecretProps } from './aws/Secret';
51
62
  import { StaticSite } from './aws/StaticSite';
52
63
  import { Topic } from './aws/Topic';
53
64
  import {
@@ -56,6 +67,7 @@ import {
56
67
  UnresolvedDependency,
57
68
  } from './errors';
58
69
  import type { GkmLinkable } from './Linkable';
70
+ import type { ManifestBackends, ManifestOverrides } from './overrides';
59
71
  import type { StackType } from './Stack';
60
72
 
61
73
  /**
@@ -133,21 +145,6 @@ type Provisioner = (
133
145
  context: ProvisionContext,
134
146
  ) => Provisioned;
135
147
 
136
- /**
137
- * Provider-specific props, per construct id.
138
- *
139
- * Neutral options — `versioned`, and later `cdn` — travel in the declaration,
140
- * because the app legitimately has an opinion about them. Anything with S3 in
141
- * its name does not belong in application code, so lifecycle rules, CORS, and
142
- * canned ACLs are supplied here, in the deploy layer, keyed by the id they
143
- * apply to and typed against the component that receives them.
144
- *
145
- * A third escape hatch needs no API at all: `fromManifest` returns the
146
- * components, so `provisioned.Uploads.nodes.bucket` is reachable for anything
147
- * neither route covers.
148
- */
149
- export type ComponentOverrides = Record<string, Record<string, unknown>>;
150
-
151
148
  /**
152
149
  * Which component provisions which kind.
153
150
  *
@@ -220,7 +217,11 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
220
217
  // rather than whatever Aurora defaults to that month. Local ran 18
221
218
  // while this ran 17.7, and neither was written down anywhere.
222
219
  version: String(d.version ?? DEFAULT_POSTGRES_VERSION),
223
- ...(props as unknown as sst.aws.PostgresArgs),
220
+ // `DatabaseProps`, not `sst.aws.PostgresArgs`: the database needs the
221
+ // `Vpc` component itself (its bootstrap function runs in the same VPC
222
+ // and needs the security groups too), where RDS would accept bare
223
+ // subnet ids. TS 7 checks the spread; 6 let the wider type through.
224
+ ...(props as unknown as Omit<DatabaseProps, 'schema'>),
224
225
  });
225
226
 
226
227
  // A database needs its own roles, not just its tenants'. Registering only
@@ -237,6 +238,10 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
237
238
  schema: d.schema,
238
239
  runtime,
239
240
  owner: ownerRole(runtime),
241
+ ownsDatabase: true,
242
+ // RDS ships PostGIS, so the image is not a question here — only the
243
+ // `CREATE EXTENSION`, which the master can run and the owner cannot.
244
+ ...(d.extensions?.length ? { extensions: d.extensions } : {}),
240
245
  ...(hasReader(d.id, context) ? { reader: readerRole(runtime) } : {}),
241
246
  });
242
247
 
@@ -370,17 +375,12 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
370
375
  },
371
376
 
372
377
  /**
373
- * The surface, without its routes.
374
- *
375
- * An API Gateway with no routes 404s everything, and that is the honest
376
- * state: the *surface* is what this kind declares, and mounting handlers on
377
- * it is the endpoint merge that has not landed — routes still reach the
378
- * deploy target through the separate `RouteInfo[]` pipeline.
378
+ * The surface, without its routes yet.
379
379
  *
380
- * Provisioning it anyway is not ceremony. Its address is what a site inlines
381
- * as `VITE_API_URL`, what an auth server puts on its trusted-origin list, and
382
- * what the cookie domain derives from — so everything downstream of the API
383
- * is blocked on the API *existing*, not on it answering.
380
+ * Its address is what a site inlines as `VITE_API_URL`, what an auth server
381
+ * puts on its trusted-origin list, and what the cookie domain derives from —
382
+ * so it is provisioned in order, and its endpoints are mounted by
383
+ * {@link mountRoutes} once everything they link to exists.
384
384
  */
385
385
  'rest-api': (stack, d, props, context) =>
386
386
  new RestApiSurface(stack, d.id, {
@@ -388,10 +388,41 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
388
388
  callers: context.callers?.get(d.id)?.promise,
389
389
  }),
390
390
 
391
- secret: (stack, d, props) => new Secret(stack, d.id, props),
391
+ // Generated on the first deploy and kept in the stage's state, as a server
392
+ // stage keeps it in its secrets: every later deploy reads the same value
393
+ // back. One set with `sst secret set` wins over it.
394
+ secret: (stack, d, props) =>
395
+ new Secret(stack, d.id, {
396
+ placeholder: generatedSecret(d.id, d as SecretDeclaration),
397
+ ...(props as SecretProps),
398
+ }),
392
399
 
393
400
  // The same storage as a secret, under a different role — see the component.
394
401
  credential: (stack, d, props) => new Credential(stack, d.id, props),
402
+
403
+ // In the stage's region, as mail is: a key in another region is a key
404
+ // every call crosses the world to reach.
405
+ encryption: (stack, d, props) =>
406
+ new Encryption(stack, d.id, {
407
+ ...(props as Partial<EncryptionProps>),
408
+ region:
409
+ (props as Partial<EncryptionProps>).region ??
410
+ $app.providers?.aws?.region ??
411
+ 'us-east-1',
412
+ }),
413
+
414
+ // Nothing to provision: the URL this stage calls, and the credentials kept
415
+ // as a secret.
416
+ 'external-api': (stack, d, props) => {
417
+ if (d.kind !== 'external-api') {
418
+ throw new UnknownDeclarationKind(d.kind, []);
419
+ }
420
+
421
+ return new ExternalApi(stack, d.id, {
422
+ ...(props as Partial<ExternalApiProps>),
423
+ url: d.url,
424
+ });
425
+ },
395
426
  };
396
427
 
397
428
  /**
@@ -572,9 +603,7 @@ function rootCluster(component: Provisioned): Database {
572
603
  * printed: one line per route, naming what it can reach.
573
604
  *
574
605
  * A surface with no routes says so rather than printing nothing, because "no
575
- * routes yet" and "this printed nothing" look identical otherwise — and for an
576
- * application's own API that is currently the true state, pending the endpoint
577
- * merge.
606
+ * routes" and "this printed nothing" look identical otherwise.
578
607
  *
579
608
  * Pure, so what gets printed can be asserted without a deploy.
580
609
  */
@@ -643,6 +672,8 @@ async function callersOf(
643
672
  id: string,
644
673
  manifest: ConstructManifest,
645
674
  provisioned: ProvisionedManifest,
675
+ /** The app's name — a mobile caller's scheme base when it gives none. */
676
+ project: string,
646
677
  ): Promise<{ trustedOrigins: string; cookieDomain?: string }> {
647
678
  // Every address here is a Pulumi output, not a string — a CloudFront domain
648
679
  // and an API Gateway endpoint are both known only after their resource
@@ -667,8 +698,17 @@ async function callersOf(
667
698
  const own = await resolved(provisioned[id]?.provides().url);
668
699
  const domain = cookieDomain([...(own ? [own] : []), ...origins]);
669
700
 
701
+ // A mobile caller is reached by its scheme — the bare one, deployed: what
702
+ // the store build registers.
703
+ const schemes = dependentsOf(manifest, id).flatMap((caller) => {
704
+ const app = manifest[caller];
705
+ return app?.kind === 'mobile-app'
706
+ ? mobileOrigins(schemeBase(project, app.scheme))
707
+ : [];
708
+ });
709
+
670
710
  return {
671
- trustedOrigins: origins.join(','),
711
+ trustedOrigins: [...origins, ...schemes].join(','),
672
712
  ...(domain ? { cookieDomain: domain } : {}),
673
713
  };
674
714
  }
@@ -718,6 +758,229 @@ export function isServed(id: string, manifest: ConstructManifest): boolean {
718
758
  );
719
759
  }
720
760
 
761
+ /**
762
+ * Kinds the in-order pass skips.
763
+ *
764
+ * - `mobile-app` ships through EAS and the stores. It is in the manifest for
765
+ * what it makes the surfaces trust, which `callersOf` reads off the graph.
766
+ * - `worker` is a process, not a resource: what it runs is provisioned as
767
+ * what it is — a queue's consumer by {@link subscribeConsumers}.
768
+ * - `cron` and `function` are Lambdas linked to what they depend on, so they
769
+ * are provisioned once all of it exists, by {@link provisionCompute}.
770
+ * - `telemetry` provisions nothing: where it goes is the stage's
771
+ * `deploy.telemetry`, a provider somebody else runs.
772
+ */
773
+ export const PROVISIONED_ELSEWHERE: ReadonlySet<DeclarationKind> =
774
+ new Set<DeclarationKind>([
775
+ 'mobile-app',
776
+ 'worker',
777
+ 'cron',
778
+ 'function',
779
+ 'telemetry',
780
+ ]);
781
+
782
+ /**
783
+ * Every function and cron: a Lambda running the handler the build wrote,
784
+ * linked to exactly what it depends on, in the database's network when it
785
+ * reaches one. A cron is that Lambda on its schedule; a function is reached
786
+ * at its URL, which only callers granted IAM access can invoke.
787
+ *
788
+ * After everything else is provisioned, because each links to things
789
+ * `provisionOrder` does not order before it.
790
+ */
791
+ export function provisionCompute(
792
+ stack: StackType,
793
+ manifest: ConstructManifest,
794
+ provisioned: ProvisionedManifest,
795
+ ): Cron[] {
796
+ const crons: Cron[] = [];
797
+
798
+ for (const [id, declaration] of Object.entries(manifest)) {
799
+ if (declaration?.kind !== 'function' && declaration?.kind !== 'cron') {
800
+ continue;
801
+ }
802
+
803
+ const { link } = resolveEdges(declaration.dependencies, provisioned);
804
+ const vpc = vpcFor(link);
805
+ const lambda = new Function(
806
+ stack,
807
+ declaration.kind === 'cron' ? `${id}Function` : id,
808
+ {
809
+ name: stack.logicalPrefixedName(id),
810
+ handler: declaration.handler,
811
+ link,
812
+ ...(vpc ? { vpc } : {}),
813
+ ...(declaration.kind === 'function'
814
+ ? { url: { authorization: 'iam' } }
815
+ : {}),
816
+ },
817
+ );
818
+
819
+ if (declaration.kind === 'cron') {
820
+ crons.push(
821
+ new Cron(stack, id, {
822
+ processor: lambda,
823
+ schedule: declaration.schedule as CronSchedule,
824
+ }),
825
+ );
826
+ continue;
827
+ }
828
+
829
+ assertProvides(
830
+ id,
831
+ declaration.provides,
832
+ Object.keys(lambda.provides()).map((role) =>
833
+ providedKeyFor(id, declaration.kind, role),
834
+ ),
835
+ );
836
+ provisioned[id] = lambda;
837
+ }
838
+
839
+ return crons;
840
+ }
841
+
842
+ /**
843
+ * Every file server's processors: each a Lambda behind a function URL with
844
+ * IAM auth, served through the file server's router on its pattern's prefix
845
+ * — so the CDN signs each request (origin access control) and the URL cannot
846
+ * be called around the cache. Linked to the bucket it reads and what it
847
+ * depends on; its native packages installed for the Lambda, not bundled.
848
+ *
849
+ * After everything else, for the reason {@link provisionCompute} is.
850
+ */
851
+ export function provisionProcessors(
852
+ stack: StackType,
853
+ manifest: ConstructManifest,
854
+ provisioned: ProvisionedManifest,
855
+ ): Function[] {
856
+ const lambdas: Function[] = [];
857
+
858
+ for (const [id, declaration] of Object.entries(manifest)) {
859
+ if (declaration?.kind !== 'file-server') continue;
860
+ const router = provisioned[id];
861
+ if (!router) continue;
862
+
863
+ for (const processor of declaration.processors ?? []) {
864
+ const { link } = resolveEdges(processor.dependencies, provisioned);
865
+ const vpc = vpcFor(link);
866
+ const native = processor.native ?? [];
867
+ lambdas.push(
868
+ new Function(stack, `${id}${processor.id}`, {
869
+ name: stack.logicalPrefixedName(`${id}${processor.id}`),
870
+ handler: processor.handler,
871
+ link,
872
+ ...(vpc ? { vpc } : {}),
873
+ ...(processor.timeout !== undefined
874
+ ? {
875
+ timeout:
876
+ `${Math.ceil(processor.timeout / 1000)} seconds` as sst.aws.FunctionArgs['timeout'],
877
+ }
878
+ : {}),
879
+ ...(processor.memorySize !== undefined
880
+ ? {
881
+ memory:
882
+ `${processor.memorySize} MB` as sst.aws.FunctionArgs['memory'],
883
+ }
884
+ : {}),
885
+ ...(native.length ? { nodejs: { install: [...native] } } : {}),
886
+ url: {
887
+ authorization: 'iam',
888
+ router: {
889
+ instance: router as unknown as sst.aws.Router,
890
+ path: processor.prefix.replace(/\/$/, ''),
891
+ },
892
+ },
893
+ }),
894
+ );
895
+ }
896
+ }
897
+
898
+ return lambdas;
899
+ }
900
+
901
+ /** A provisioned queue: what {@link subscribeConsumers} subscribes through. */
902
+ interface Consumable {
903
+ consume(consumer: {
904
+ handler: string;
905
+ link?: unknown[];
906
+ vpc?: sst.aws.Vpc;
907
+ }): unknown;
908
+ }
909
+
910
+ /**
911
+ * The network a function has to run in to reach what it links to.
912
+ *
913
+ * A database is private to its VPC, so a handler linked to one — or to a
914
+ * schema tenant or reader of one — runs inside it, or its connection times out.
915
+ */
916
+ export function vpcFor(link: readonly Provisioned[]): sst.aws.Vpc | undefined {
917
+ for (const component of link) {
918
+ const cluster = rootCluster(component);
919
+ if (cluster instanceof Database) return cluster.vpc;
920
+ }
921
+
922
+ return undefined;
923
+ }
924
+
925
+ /**
926
+ * Mount every surface's endpoints — the handlers the build wrote, each linked
927
+ * to exactly what that endpoint depends on.
928
+ *
929
+ * After every construct is provisioned, for the reason consumers are: an
930
+ * endpoint links to things `provisionOrder` does not order before its surface.
931
+ */
932
+ export function mountRoutes(
933
+ manifest: ConstructManifest,
934
+ provisioned: ProvisionedManifest,
935
+ ): void {
936
+ for (const [id, declaration] of Object.entries(manifest)) {
937
+ if (declaration?.kind !== 'rest-api') continue;
938
+
939
+ const surface = provisioned[id];
940
+ if (!(surface instanceof RestApiSurface)) continue;
941
+
942
+ for (const endpoint of declaration.endpoints) {
943
+ const { link } = resolveEdges(endpoint.dependencies, provisioned);
944
+ const vpc = vpcFor(link);
945
+ surface.mount({
946
+ method: endpoint.method,
947
+ path: endpoint.path,
948
+ handler: endpoint.handler,
949
+ link,
950
+ ...(vpc ? { vpc } : {}),
951
+ ...(endpoint.authorizer ? { authorizer: endpoint.authorizer } : {}),
952
+ });
953
+ }
954
+ }
955
+ }
956
+
957
+ /**
958
+ * Subscribe every queue's one consumer — the handler the build wrote for it,
959
+ * linked to what that consumer depends on.
960
+ *
961
+ * After every construct is provisioned, because a consumer links to things
962
+ * that `provisionOrder` does not order before the queue.
963
+ */
964
+ export function subscribeConsumers(
965
+ manifest: ConstructManifest,
966
+ provisioned: ProvisionedManifest,
967
+ ): void {
968
+ for (const [id, declaration] of Object.entries(manifest)) {
969
+ if (declaration?.kind !== 'queue') continue;
970
+
971
+ const queue = provisioned[id] as unknown as Consumable | undefined;
972
+ if (!queue) continue;
973
+
974
+ const { link } = resolveEdges(declaration.worker.dependencies, provisioned);
975
+ const vpc = vpcFor(link);
976
+ queue.consume({
977
+ handler: declaration.worker.handler,
978
+ link,
979
+ ...(vpc ? { vpc } : {}),
980
+ });
981
+ }
982
+ }
983
+
721
984
  /** The provisioner for a kind. Pure — the lookup is testable without Pulumi. */
722
985
  export function provisionerFor(kind: DeclarationKind): Provisioner {
723
986
  const provisioner = PROVISIONERS[kind];
@@ -756,20 +1019,34 @@ export function assertProvides(
756
1019
  * tenant, a read replica, and the surface over a bucket all resolve an address
757
1020
  * off something else, and a pass in map order would find it half the time.
758
1021
  */
759
- export function fromManifest(
1022
+ export function fromManifest<
1023
+ const M extends ConstructManifest,
1024
+ const B extends ManifestBackends = {},
1025
+ >(
760
1026
  stack: StackType,
761
- manifest: ConstructManifest,
762
- overrides: ComponentOverrides = {},
1027
+ manifest: M,
1028
+ /**
1029
+ * Provider-specific props, per construct id — typed from the manifest, so
1030
+ * only its ids are keys, each takes what its kind's component reads, and
1031
+ * what the synth cannot guess (a database's `vpc`, mail's `from`) is
1032
+ * required. See {@link ManifestOverrides}.
1033
+ *
1034
+ * Neutral options — `versioned`, `fifo` — travel in the declaration,
1035
+ * because the app legitimately has an opinion about them. Anything with S3
1036
+ * in its name does not belong in application code, so it is supplied here.
1037
+ * A third escape hatch needs no API: the components are returned, so
1038
+ * `provisioned.Uploads.nodes.bucket` is reachable for anything else.
1039
+ */
1040
+ overrides: ManifestOverrides<M, B>,
763
1041
  /**
764
1042
  * The backend choices that are config rather than declaration.
765
1043
  *
766
1044
  * Defaulted the same way the local target defaults them, so a stage deployed
767
- * without saying gets the same backend a developer ran against.
1045
+ * without saying gets the same backend a developer ran against. Typed
1046
+ * literally, because the backend decides what is required above: an
1047
+ * ElastiCache cache needs a `vpc`, Resend or SMTP mail a `url`.
768
1048
  */
769
- backends: {
770
- cache?: 'upstash' | 'elasticache' | 'db';
771
- email?: 'resend' | 'ses' | 'smtp';
772
- } = {},
1049
+ backends: B = {} as B,
773
1050
  ): ProvisionedManifest {
774
1051
  const provisioned: ProvisionedManifest = {};
775
1052
 
@@ -806,11 +1083,12 @@ export function fromManifest(
806
1083
  for (const id of [...rest, ...sites]) {
807
1084
  const declaration = manifest[id];
808
1085
  if (!declaration) continue;
1086
+ if (PROVISIONED_ELSEWHERE.has(declaration.kind)) continue;
809
1087
 
810
1088
  const component = provisionerFor(declaration.kind)(
811
1089
  stack,
812
1090
  declaration,
813
- overrides[id] ?? {},
1091
+ (overrides as Record<string, Record<string, unknown>>)[id] ?? {},
814
1092
  context,
815
1093
  );
816
1094
 
@@ -829,6 +1107,13 @@ export function fromManifest(
829
1107
  provisioned[id] = component;
830
1108
  }
831
1109
 
1110
+ // The functions and crons, each queue's one consumer and each surface's
1111
+ // endpoints, once everything they link to exists.
1112
+ provisionCompute(stack, manifest, provisioned);
1113
+ provisionProcessors(stack, manifest, provisioned);
1114
+ subscribeConsumers(manifest, provisioned);
1115
+ mountRoutes(manifest, provisioned);
1116
+
832
1117
  for (const line of describeRoutes(manifest)) console.log(line);
833
1118
 
834
1119
  // Now everything exists, so every surface's callers can be resolved — the
@@ -836,7 +1121,7 @@ export function fromManifest(
836
1121
  for (const [id, deferred] of callers) {
837
1122
  // Resolved with the promise itself: the addresses it needs are Pulumi
838
1123
  // outputs, so the answer arrives when they do.
839
- deferred.resolve(callersOf(id, manifest, provisioned));
1124
+ deferred.resolve(callersOf(id, manifest, provisioned, $app.name));
840
1125
  }
841
1126
 
842
1127
  // Last, and only now: the roles exist as passwords and secrets from the
@@ -851,6 +1136,30 @@ export function fromManifest(
851
1136
  return provisioned;
852
1137
  }
853
1138
 
1139
+ /**
1140
+ * A secret in its declared format — the alphabet `drawSecret` draws from on
1141
+ * every other stage, with one symbol guaranteed as it guarantees one.
1142
+ *
1143
+ * The format is fixed at the first deploy, as a server stage's is: changing it
1144
+ * would replace the password, and every later deploy is meant to read back the
1145
+ * one it had.
1146
+ */
1147
+ function generatedSecret(id: string, format: SecretFormat) {
1148
+ const symbols = [...new Set(format.symbols ?? SECRET_SYMBOLS)].join('');
1149
+
1150
+ return new RandomPassword(
1151
+ `${id}Generated`,
1152
+ {
1153
+ length: format.length ?? SECRET_LENGTH,
1154
+ special: symbols.length > 0,
1155
+ ...(symbols ? { overrideSpecial: symbols, minSpecial: 1 } : {}),
1156
+ },
1157
+ {
1158
+ ignoreChanges: ['length', 'special', 'overrideSpecial', 'minSpecial'],
1159
+ },
1160
+ ).result;
1161
+ }
1162
+
854
1163
  /** What one function receives from its edges. */
855
1164
  export interface ResolvedEdges {
856
1165
  /** The components it may reach. SST turns these into IAM *and* injects their
package/src/sst/index.ts CHANGED
@@ -34,6 +34,7 @@ export {
34
34
  } from './aws/DatabaseBootstrap';
35
35
  export { DatabaseReader, DatabaseSchema } from './aws/DerivedDatabase';
36
36
  export { Email, EmailNeedsUrl, type EmailProps } from './aws/Email';
37
+ export { Encryption, type EncryptionProps } from './aws/Encryption';
37
38
  export { FileServer, type FileServerProps } from './aws/FileServer';
38
39
  export { Function, type FunctionProps } from './aws/Function';
39
40
  export {
@@ -61,7 +62,6 @@ export {
61
62
  } from './errors';
62
63
  export {
63
64
  assertProvides,
64
- type ComponentOverrides,
65
65
  describeRoutes,
66
66
  fromManifest,
67
67
  isServed,
@@ -75,4 +75,9 @@ export {
75
75
  } from './fromManifest';
76
76
  export { type GkmLinkable, ResourceType } from './Linkable';
77
77
  export { kebab, prefixedName, regionOfArn } from './naming';
78
+ export type {
79
+ ManifestBackends,
80
+ ManifestOverrides,
81
+ OverrideFor,
82
+ } from './overrides';
78
83
  export { Stack, type StackType } from './Stack';
@@ -0,0 +1,133 @@
1
+ /**
2
+ * What a deploy can say about each construct, inferred from the manifest.
3
+ *
4
+ * `fromManifest` used to take `Record<string, Record<string, unknown>>`, so a
5
+ * misspelt id, a prop the component never reads, and a missing VPC all passed
6
+ * the type checker — the first two silently, the last at synth, mid-deploy.
7
+ * The manifest is emitted `as const`, so every id's kind is known statically,
8
+ * and the kind decides what can be overridden:
9
+ *
10
+ * - only the manifest's own ids are keys;
11
+ * - each takes the props its kind's provisioner reads, minus those the
12
+ * declaration already decides (a site's `path`, a file server's `origin`);
13
+ * - what the synth refuses to guess is required — a database's `vpc`, mail's
14
+ * `from`, and, by backend, an ElastiCache `vpc` or a non-SES mail `url`;
15
+ * - a kind with nothing to override takes no key at all.
16
+ */
17
+
18
+ import type { ConstructManifest, DeclarationKind } from '@geekmidas/manifest';
19
+ import type { CacheProps } from './aws/Cache';
20
+ import type { CredentialProps } from './aws/Credential';
21
+ import type { DatabaseProps } from './aws/Database';
22
+ import type { EncryptionProps } from './aws/Encryption';
23
+ import type { ExternalApiProps } from './aws/ExternalApi';
24
+ import type { FileServerProps } from './aws/FileServer';
25
+ import type { ObjectStorageProps } from './aws/ObjectStorage';
26
+ import type { QueueProps } from './aws/Queue';
27
+ import type { SecretProps } from './aws/Secret';
28
+ import type { StaticSiteProps } from './aws/StaticSite';
29
+ import type { TopicProps } from './aws/Topic';
30
+
31
+ /** The backend choices that are config rather than declaration. */
32
+ export interface ManifestBackends {
33
+ cache?: 'upstash' | 'elasticache' | 'db';
34
+ email?: 'resend' | 'ses' | 'smtp';
35
+ }
36
+
37
+ /** The backend a choice resolves to — the provisioner's default when unset. */
38
+ type CacheBackendOf<B extends ManifestBackends> =
39
+ B['cache'] extends NonNullable<ManifestBackends['cache']>
40
+ ? B['cache']
41
+ : 'upstash';
42
+ type EmailBackendOf<B extends ManifestBackends> =
43
+ B['email'] extends NonNullable<ManifestBackends['email']>
44
+ ? B['email']
45
+ : 'ses';
46
+
47
+ type CacheOverride<Backend> = Backend extends 'elasticache'
48
+ ? { vpc: NonNullable<CacheProps['vpc']> }
49
+ : Backend extends 'upstash'
50
+ ? { url?: CacheProps['url']; region?: string }
51
+ : never;
52
+
53
+ type EmailOverride<Backend> = {
54
+ /** Every provider rejects an unverified sender, so there is no default. */
55
+ from: $util.Input<string>;
56
+ region?: $util.Input<string>;
57
+ } & (Backend extends 'ses'
58
+ ? { url?: $util.Input<string> }
59
+ : // Resend and a relay are accounts somebody created: their URL is theirs.
60
+ { url: $util.Input<string> });
61
+
62
+ /**
63
+ * What one declaration takes, by its kind and the backends; `never` for a kind
64
+ * with nothing to override, which drops its id from the keys.
65
+ */
66
+ export type OverrideFor<D, B extends ManifestBackends = {}> = D extends {
67
+ kind: infer K extends DeclarationKind;
68
+ }
69
+ ? K extends 'objects'
70
+ ? Partial<ObjectStorageProps>
71
+ : K extends 'file-server'
72
+ ? Partial<Omit<FileServerProps, 'origin'>>
73
+ : K extends 'site'
74
+ ? Partial<Omit<StaticSiteProps, 'path' | 'variant' | 'environment'>>
75
+ : K extends 'queue'
76
+ ? Partial<Omit<QueueProps, 'fifo'>>
77
+ : K extends 'topic'
78
+ ? Partial<TopicProps>
79
+ : K extends 'database'
80
+ ? Omit<DatabaseProps, 'schema' | 'version'>
81
+ : K extends 'cache'
82
+ ? // In a database: same address, one more table, nothing to say.
83
+ D extends { of: string }
84
+ ? never
85
+ : CacheOverride<CacheBackendOf<B>>
86
+ : K extends 'email'
87
+ ? EmailOverride<EmailBackendOf<B>>
88
+ : K extends 'rest-api'
89
+ ? Partial<sst.aws.ApiGatewayV2Args>
90
+ : K extends 'secret'
91
+ ? SecretProps
92
+ : K extends 'credential'
93
+ ? CredentialProps
94
+ : K extends 'encryption'
95
+ ? Partial<EncryptionProps>
96
+ : K extends 'external-api'
97
+ ? Omit<ExternalApiProps, 'url'>
98
+ : // Derived databases, compute, mobile apps, OIDC.
99
+ never
100
+ : never;
101
+
102
+ /** Whether `{}` satisfies an override — whether the id can be left out. */
103
+ type IsOptional<T> = {} extends T ? true : false;
104
+
105
+ type Overridable<M, B extends ManifestBackends> = {
106
+ [Id in keyof M]: [OverrideFor<M[Id], B>] extends [never] ? never : Id;
107
+ }[keyof M];
108
+
109
+ type RequiredIds<M, B extends ManifestBackends> = {
110
+ [Id in Overridable<M, B>]: IsOptional<OverrideFor<M[Id], B>> extends true
111
+ ? never
112
+ : Id;
113
+ }[Overridable<M, B>];
114
+
115
+ /**
116
+ * The overrides a manifest accepts, keyed by its own construct ids.
117
+ *
118
+ * A manifest typed only as `ConstructManifest` — ids unknown — falls back to
119
+ * the untyped record, and the provisioners' synth-time checks still apply.
120
+ */
121
+ export type ManifestOverrides<
122
+ M extends ConstructManifest,
123
+ B extends ManifestBackends = {},
124
+ > = string extends keyof M
125
+ ? Record<string, Record<string, unknown>>
126
+ : {
127
+ [Id in RequiredIds<M, B>]: OverrideFor<M[Id], B>;
128
+ } & {
129
+ [Id in Exclude<Overridable<M, B>, RequiredIds<M, B>>]?: OverrideFor<
130
+ M[Id],
131
+ B
132
+ >;
133
+ };