@geekmidas/cloud 10.0.0-alpha.16 → 10.0.0-alpha.160

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.
Files changed (33) hide show
  1. package/README.md +26 -6
  2. package/package.json +9 -8
  3. package/src/sst/__tests__/api.spec.ts +0 -46
  4. package/src/sst/__tests__/bootstrap.spec.ts +15 -0
  5. package/src/sst/__tests__/encryption.spec.ts +90 -0
  6. package/src/sst/__tests__/fromManifest.inbox.spec.ts +160 -0
  7. package/src/sst/__tests__/fromManifest.integration.spec.ts +86 -2
  8. package/src/sst/__tests__/fromManifest.secret.spec.ts +79 -0
  9. package/src/sst/__tests__/fromManifest.spec.ts +379 -5
  10. package/src/sst/__tests__/function.spec.ts +0 -71
  11. package/src/sst/__tests__/surfaces.spec.ts +17 -0
  12. package/src/sst/__tests__/websocket.spec.ts +207 -0
  13. package/src/sst/__tests__/wildcardSite.spec.ts +195 -0
  14. package/src/sst/__type-tests__/overrides.type-test.ts +152 -0
  15. package/src/sst/aws/Api.ts +0 -43
  16. package/src/sst/aws/Credential.ts +42 -9
  17. package/src/sst/aws/Cron.ts +5 -45
  18. package/src/sst/aws/DatabaseBootstrap.ts +6 -0
  19. package/src/sst/aws/Encryption.ts +91 -0
  20. package/src/sst/aws/ExternalApi.ts +58 -0
  21. package/src/sst/aws/Function.ts +9 -38
  22. package/src/sst/aws/Inbox.ts +138 -0
  23. package/src/sst/aws/Queue.ts +34 -1
  24. package/src/sst/aws/RestApiSurface.ts +36 -14
  25. package/src/sst/aws/Secret.ts +5 -3
  26. package/src/sst/aws/StaticSite.ts +44 -1
  27. package/src/sst/aws/WebSocketApiSurface.ts +160 -0
  28. package/src/sst/aws/bootstrap/handler.ts +12 -1
  29. package/src/sst/errors.ts +15 -0
  30. package/src/sst/fromManifest.ts +536 -49
  31. package/src/sst/index.ts +20 -1
  32. package/src/sst/overrides.ts +149 -0
  33. package/src/sst/__type-tests__/manifest.type-test.ts +0 -77
@@ -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,17 @@ import {
26
28
  cookieDomain,
27
29
  DEFAULT_POSTGRES_VERSION,
28
30
  dependentsOf,
31
+ isWildcardSite,
32
+ mobileOrigins,
29
33
  PUBLIC,
30
34
  PUBLIC_PREFIX,
31
35
  providedKeyFor,
32
36
  provisionOrder,
37
+ SECRET_LENGTH,
38
+ SECRET_SYMBOLS,
39
+ schemeBase,
33
40
  } from '@geekmidas/manifest';
41
+ import { RandomPassword } from '@pulumi/random';
34
42
  import {
35
43
  Cache,
36
44
  CacheIsAmbiguous,
@@ -39,23 +47,30 @@ import {
39
47
  withCacheTable,
40
48
  } from './aws/Cache';
41
49
  import { Credential } from './aws/Credential';
50
+ import { Cron, type CronSchedule } from './aws/Cron';
42
51
  import { Database, DatabaseNeedsVpc, type DatabaseProps } from './aws/Database';
43
52
  import { DatabaseBootstrap } from './aws/DatabaseBootstrap';
44
53
  import { DatabaseReader, DatabaseSchema } from './aws/DerivedDatabase';
45
54
  import { Email, EmailNeedsSender } from './aws/Email';
55
+ import { Encryption, type EncryptionProps } from './aws/Encryption';
56
+ import { ExternalApi, type ExternalApiProps } from './aws/ExternalApi';
46
57
  import { FileServer } from './aws/FileServer';
58
+ import { Function } from './aws/Function';
59
+ import { Inbox, InboxTopic } from './aws/Inbox';
47
60
  import { ObjectStorage } from './aws/ObjectStorage';
48
61
  import { Queue } from './aws/Queue';
49
62
  import { RestApiSurface } from './aws/RestApiSurface';
50
- import { Secret } from './aws/Secret';
63
+ import { Secret, type SecretProps } from './aws/Secret';
51
64
  import { StaticSite } from './aws/StaticSite';
52
65
  import { Topic } from './aws/Topic';
66
+ import { WebSocketApiSurface } from './aws/WebSocketApiSurface';
53
67
  import {
54
68
  ProvidesMismatch,
55
69
  UnknownDeclarationKind,
56
70
  UnresolvedDependency,
57
71
  } from './errors';
58
72
  import type { GkmLinkable } from './Linkable';
73
+ import type { ManifestBackends, ManifestOverrides } from './overrides';
59
74
  import type { StackType } from './Stack';
60
75
 
61
76
  /**
@@ -105,6 +120,8 @@ export interface ProvisionContext {
105
120
  */
106
121
  cache?: 'upstash' | 'elasticache' | 'db';
107
122
  email?: 'resend' | 'ses' | 'smtp';
123
+ /** The labels no tenant of a wildcard site may take. */
124
+ protectedSubdomains?: readonly string[];
108
125
  /**
109
126
  * One deferred caller list per surface, resolved once everything exists.
110
127
  *
@@ -133,21 +150,6 @@ type Provisioner = (
133
150
  context: ProvisionContext,
134
151
  ) => Provisioned;
135
152
 
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
153
  /**
152
154
  * Which component provisions which kind.
153
155
  *
@@ -189,7 +191,15 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
189
191
  return new StaticSite(stack, d.id, {
190
192
  path: d.app.path,
191
193
  variant: d.variant,
192
- environment: siteEnvironment(d, context),
194
+ environment: siteEnvironment(d, context, stack),
195
+ // Every subdomain, on one distribution: see `StaticSite`.
196
+ ...(isWildcardSite(d)
197
+ ? {
198
+ tenants: {
199
+ protectedSubdomains: context.protectedSubdomains ?? [],
200
+ },
201
+ }
202
+ : {}),
193
203
  ...props,
194
204
  });
195
205
  },
@@ -206,6 +216,12 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
206
216
  // producer's and subscribers' business, and SNS has no per-event
207
217
  // configuration to map it onto.
208
218
  topic: (stack, d, props) => new Topic(stack, d.id, props),
219
+ // What receives the mail is its provider's, made before SST runs; the
220
+ // inbox is the values that made, linked. See `aws/Inbox`.
221
+ inbox: (stack, d, props) => {
222
+ if (d.kind !== 'inbox') throw new UnknownDeclarationKind(d.kind, []);
223
+ return new Inbox(stack, d.id, { ...props, topicId: d.topic });
224
+ },
209
225
 
210
226
  database: (stack, d, props, context) => {
211
227
  if (d.kind !== 'database') throw new UnknownDeclarationKind(d.kind, []);
@@ -241,6 +257,10 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
241
257
  schema: d.schema,
242
258
  runtime,
243
259
  owner: ownerRole(runtime),
260
+ ownsDatabase: true,
261
+ // RDS ships PostGIS, so the image is not a question here — only the
262
+ // `CREATE EXTENSION`, which the master can run and the owner cannot.
263
+ ...(d.extensions?.length ? { extensions: d.extensions } : {}),
244
264
  ...(hasReader(d.id, context) ? { reader: readerRole(runtime) } : {}),
245
265
  });
246
266
 
@@ -374,17 +394,12 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
374
394
  },
375
395
 
376
396
  /**
377
- * The surface, without its routes.
397
+ * The surface, without its routes yet.
378
398
  *
379
- * An API Gateway with no routes 404s everything, and that is the honest
380
- * state: the *surface* is what this kind declares, and mounting handlers on
381
- * it is the endpoint merge that has not landed — routes still reach the
382
- * deploy target through the separate `RouteInfo[]` pipeline.
383
- *
384
- * Provisioning it anyway is not ceremony. Its address is what a site inlines
385
- * as `VITE_API_URL`, what an auth server puts on its trusted-origin list, and
386
- * what the cookie domain derives from — so everything downstream of the API
387
- * is blocked on the API *existing*, not on it answering.
399
+ * Its address is what a site inlines as `VITE_API_URL`, what an auth server
400
+ * puts on its trusted-origin list, and what the cookie domain derives from —
401
+ * so it is provisioned in order, and its endpoints are mounted by
402
+ * {@link mountRoutes} once everything they link to exists.
388
403
  */
389
404
  'rest-api': (stack, d, props, context) =>
390
405
  new RestApiSurface(stack, d.id, {
@@ -392,10 +407,55 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
392
407
  callers: context.callers?.get(d.id)?.promise,
393
408
  }),
394
409
 
395
- secret: (stack, d, props) => new Secret(stack, d.id, props),
410
+ /**
411
+ * An API Gateway WebSocket API and its connection table, without its
412
+ * routes yet — mounted by {@link mountWebSocketRoutes} once everything
413
+ * they link to exists, as a REST surface's endpoints are.
414
+ */
415
+ 'websocket-api': (stack, d, props, context) =>
416
+ new WebSocketApiSurface(stack, d.id, {
417
+ ...(props as sst.aws.ApiGatewayWebSocketArgs),
418
+ callers: context.callers?.get(d.id)?.promise,
419
+ ...(d.kind === 'websocket-api' && d.discriminator
420
+ ? { discriminator: d.discriminator }
421
+ : {}),
422
+ }),
423
+
424
+ // Generated on the first deploy and kept in the stage's state, as a server
425
+ // stage keeps it in its secrets: every later deploy reads the same value
426
+ // back. One set with `sst secret set` wins over it.
427
+ secret: (stack, d, props) =>
428
+ new Secret(stack, d.id, {
429
+ placeholder: generatedSecret(d.id, d as SecretDeclaration),
430
+ ...(props as SecretProps),
431
+ }),
396
432
 
397
433
  // The same storage as a secret, under a different role — see the component.
398
434
  credential: (stack, d, props) => new Credential(stack, d.id, props),
435
+
436
+ // In the stage's region, as mail is: a key in another region is a key
437
+ // every call crosses the world to reach.
438
+ encryption: (stack, d, props) =>
439
+ new Encryption(stack, d.id, {
440
+ ...(props as Partial<EncryptionProps>),
441
+ region:
442
+ (props as Partial<EncryptionProps>).region ??
443
+ $app.providers?.aws?.region ??
444
+ 'us-east-1',
445
+ }),
446
+
447
+ // Nothing to provision: the URL this stage calls, and the credentials kept
448
+ // as a secret.
449
+ 'external-api': (stack, d, props) => {
450
+ if (d.kind !== 'external-api') {
451
+ throw new UnknownDeclarationKind(d.kind, []);
452
+ }
453
+
454
+ return new ExternalApi(stack, d.id, {
455
+ ...(props as Partial<ExternalApiProps>),
456
+ url: d.url,
457
+ });
458
+ },
399
459
  };
400
460
 
401
461
  /**
@@ -414,10 +474,25 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
414
474
  export function siteEnvironment(
415
475
  declaration: SiteDeclaration,
416
476
  context: ProvisionContext,
477
+ stack?: StackType,
417
478
  ): Record<string, $util.Input<string>> {
418
479
  const prefix = PUBLIC_PREFIX[declaration.variant];
419
480
  const environment: Record<string, $util.Input<string>> = {};
420
481
 
482
+ // A wildcard site takes its tenant off the host it is reached on, so it
483
+ // inlines the domain to take the label off against, and the labels no
484
+ // tenant may be — the same two keys `publicEnvFor` names locally.
485
+ if (isWildcardSite(declaration) && stack?.domain) {
486
+ const own = {
487
+ domain: stack.domain,
488
+ protectedSubdomains: (context.protectedSubdomains ?? []).join(','),
489
+ };
490
+ for (const [role, value] of Object.entries(own)) {
491
+ environment[`${prefix}${providedKeyFor(declaration.id, 'site', role)}`] =
492
+ value;
493
+ }
494
+ }
495
+
421
496
  for (const edge of declaration.dependencies) {
422
497
  const target = context.manifest[edge.target];
423
498
  if (!target) continue;
@@ -576,9 +651,7 @@ function rootCluster(component: Provisioned): Database {
576
651
  * printed: one line per route, naming what it can reach.
577
652
  *
578
653
  * A surface with no routes says so rather than printing nothing, because "no
579
- * routes yet" and "this printed nothing" look identical otherwise — and for an
580
- * application's own API that is currently the true state, pending the endpoint
581
- * merge.
654
+ * routes" and "this printed nothing" look identical otherwise.
582
655
  *
583
656
  * Pure, so what gets printed can be asserted without a deploy.
584
657
  */
@@ -586,13 +659,26 @@ export function describeRoutes(manifest: ConstructManifest): string[] {
586
659
  const lines: string[] = [];
587
660
 
588
661
  for (const [id, declaration] of Object.entries(manifest)) {
662
+ if (declaration.kind === 'websocket-api') {
663
+ lines.push(`${id}:`);
664
+ for (const route of declaration.routes ?? []) {
665
+ const reaches = route.dependencies
666
+ .map((edge) => `${edge.target} (${edge.kind})`)
667
+ .join(', ');
668
+ lines.push(` ${route.action} → ${reaches || 'its connections'}`);
669
+ }
670
+ if (declaration.auth) {
671
+ lines.push(` auth ${declaration.auth.target} ($connect verifies)`);
672
+ }
673
+ continue;
674
+ }
589
675
  if (declaration.kind !== 'rest-api') continue;
590
676
 
591
677
  lines.push(`${id}:`);
592
678
 
593
679
  if (declaration.endpoints.length === 0) {
594
- // Empty is now genuinely empty: the build folds discovered routes into
595
- // the surface that serves them, so a surface with none has none.
680
+ // Empty is genuinely empty: discovery nests every route in the
681
+ // surface that serves it, so a surface with none has none.
596
682
  lines.push(' (no routes)');
597
683
  }
598
684
 
@@ -606,11 +692,11 @@ export function describeRoutes(manifest: ConstructManifest): string[] {
606
692
  );
607
693
  }
608
694
 
609
- // A caller relationship, printed apart from the routes because it grants
610
- // nothing — see `RestApiDeclaration.calls`.
611
- if (declaration.calls?.length) {
695
+ // What authenticates it, printed apart from the routes because it grants
696
+ // nothing: the auth server learns this surface's origin.
697
+ if (declaration.auth) {
612
698
  lines.push(
613
- ` calls ${declaration.calls.map((edge) => edge.target).join(', ')} (origin only; grants nothing)`,
699
+ ` auth ${declaration.auth.target} (origin only; grants nothing)`,
614
700
  );
615
701
  }
616
702
  }
@@ -647,6 +733,8 @@ async function callersOf(
647
733
  id: string,
648
734
  manifest: ConstructManifest,
649
735
  provisioned: ProvisionedManifest,
736
+ /** The app's name — a mobile caller's scheme base when it gives none. */
737
+ project: string,
650
738
  ): Promise<{ trustedOrigins: string; cookieDomain?: string }> {
651
739
  // Every address here is a Pulumi output, not a string — a CloudFront domain
652
740
  // and an API Gateway endpoint are both known only after their resource
@@ -671,8 +759,17 @@ async function callersOf(
671
759
  const own = await resolved(provisioned[id]?.provides().url);
672
760
  const domain = cookieDomain([...(own ? [own] : []), ...origins]);
673
761
 
762
+ // A mobile caller is reached by its scheme — the bare one, deployed: what
763
+ // the store build registers.
764
+ const schemes = dependentsOf(manifest, id).flatMap((caller) => {
765
+ const app = manifest[caller];
766
+ return app?.kind === 'mobile-app'
767
+ ? mobileOrigins(schemeBase(project, app.scheme))
768
+ : [];
769
+ });
770
+
674
771
  return {
675
- trustedOrigins: origins.join(','),
772
+ trustedOrigins: [...origins, ...schemes].join(','),
676
773
  ...(domain ? { cookieDomain: domain } : {}),
677
774
  };
678
775
  }
@@ -722,6 +819,334 @@ export function isServed(id: string, manifest: ConstructManifest): boolean {
722
819
  );
723
820
  }
724
821
 
822
+ /**
823
+ * Kinds the in-order pass skips.
824
+ *
825
+ * - `mobile-app` ships through EAS and the stores. It is in the manifest for
826
+ * what it makes the surfaces trust, which `callersOf` reads off the graph.
827
+ * - `worker` is a process, not a resource: what it runs is provisioned as
828
+ * what it is — a queue's consumer by {@link subscribeConsumers}.
829
+ * - `cron` and `function` are Lambdas linked to what they depend on, so they
830
+ * are provisioned once all of it exists, by {@link provisionCompute}.
831
+ * - `telemetry` provisions nothing: where it goes is the stage's
832
+ * `deploy.telemetry`, a provider somebody else runs.
833
+ */
834
+ export const PROVISIONED_ELSEWHERE: ReadonlySet<DeclarationKind> =
835
+ new Set<DeclarationKind>([
836
+ 'mobile-app',
837
+ 'worker',
838
+ 'cron',
839
+ 'function',
840
+ 'telemetry',
841
+ ]);
842
+
843
+ /**
844
+ * Every function and cron: a Lambda running the handler the build wrote,
845
+ * linked to exactly what it depends on, in the database's network when it
846
+ * reaches one. A cron is that Lambda on its schedule; a function is reached
847
+ * at its URL, which only callers granted IAM access can invoke.
848
+ *
849
+ * After everything else is provisioned, because each links to things
850
+ * `provisionOrder` does not order before it.
851
+ */
852
+ export function provisionCompute(
853
+ stack: StackType,
854
+ manifest: ConstructManifest,
855
+ provisioned: ProvisionedManifest,
856
+ ): Cron[] {
857
+ const crons: Cron[] = [];
858
+
859
+ for (const [id, declaration] of Object.entries(manifest)) {
860
+ if (declaration?.kind !== 'function' && declaration?.kind !== 'cron') {
861
+ continue;
862
+ }
863
+
864
+ const { link } = resolveEdges(declaration.dependencies, provisioned);
865
+ const vpc = vpcFor(link);
866
+ const lambda = new Function(
867
+ stack,
868
+ declaration.kind === 'cron' ? `${id}Function` : id,
869
+ {
870
+ name: stack.logicalPrefixedName(id),
871
+ handler: declaration.handler,
872
+ link,
873
+ ...(vpc ? { vpc } : {}),
874
+ ...(declaration.kind === 'function'
875
+ ? { url: { authorization: 'iam' } }
876
+ : {}),
877
+ },
878
+ );
879
+
880
+ if (declaration.kind === 'cron') {
881
+ crons.push(
882
+ new Cron(stack, id, {
883
+ processor: lambda,
884
+ schedule: declaration.schedule as CronSchedule,
885
+ }),
886
+ );
887
+ continue;
888
+ }
889
+
890
+ assertProvides(
891
+ id,
892
+ declaration.provides,
893
+ Object.keys(lambda.provides()).map((role) =>
894
+ providedKeyFor(id, declaration.kind, role),
895
+ ),
896
+ );
897
+ provisioned[id] = lambda;
898
+ }
899
+
900
+ return crons;
901
+ }
902
+
903
+ /**
904
+ * Every file server's processors: each a Lambda behind a function URL with
905
+ * IAM auth, served through the file server's router on its pattern's prefix
906
+ * — so the CDN signs each request (origin access control) and the URL cannot
907
+ * be called around the cache. Linked to the bucket it reads and what it
908
+ * depends on; its native packages installed for the Lambda, not bundled.
909
+ *
910
+ * After everything else, for the reason {@link provisionCompute} is.
911
+ */
912
+ export function provisionProcessors(
913
+ stack: StackType,
914
+ manifest: ConstructManifest,
915
+ provisioned: ProvisionedManifest,
916
+ ): Function[] {
917
+ const lambdas: Function[] = [];
918
+
919
+ for (const [id, declaration] of Object.entries(manifest)) {
920
+ if (declaration?.kind !== 'file-server') continue;
921
+ const router = provisioned[id];
922
+ if (!router) continue;
923
+
924
+ for (const processor of declaration.processors ?? []) {
925
+ const { link } = resolveEdges(processor.dependencies, provisioned);
926
+ const vpc = vpcFor(link);
927
+ const native = processor.native ?? [];
928
+ lambdas.push(
929
+ new Function(stack, `${id}${processor.id}`, {
930
+ name: stack.logicalPrefixedName(`${id}${processor.id}`),
931
+ handler: processor.handler,
932
+ link,
933
+ ...(vpc ? { vpc } : {}),
934
+ ...(processor.timeout !== undefined
935
+ ? {
936
+ timeout:
937
+ `${Math.ceil(processor.timeout / 1000)} seconds` as sst.aws.FunctionArgs['timeout'],
938
+ }
939
+ : {}),
940
+ ...(processor.memorySize !== undefined
941
+ ? {
942
+ memory:
943
+ `${processor.memorySize} MB` as sst.aws.FunctionArgs['memory'],
944
+ }
945
+ : {}),
946
+ ...(native.length ? { nodejs: { install: [...native] } } : {}),
947
+ url: {
948
+ authorization: 'iam',
949
+ router: {
950
+ instance: router as unknown as sst.aws.Router,
951
+ path: processor.prefix.replace(/\/$/, ''),
952
+ },
953
+ },
954
+ }),
955
+ );
956
+ }
957
+ }
958
+
959
+ return lambdas;
960
+ }
961
+
962
+ /** A provisioned queue: what {@link subscribeConsumers} subscribes through. */
963
+ interface Consumable {
964
+ consume(consumer: {
965
+ handler: string;
966
+ link?: unknown[];
967
+ vpc?: sst.aws.Vpc;
968
+ }): unknown;
969
+ }
970
+
971
+ /**
972
+ * The network a function has to run in to reach what it links to.
973
+ *
974
+ * A database is private to its VPC, so a handler linked to one — or to a
975
+ * schema tenant or reader of one — runs inside it, or its connection times out.
976
+ */
977
+ export function vpcFor(link: readonly Provisioned[]): sst.aws.Vpc | undefined {
978
+ for (const component of link) {
979
+ const cluster = rootCluster(component);
980
+ if (cluster instanceof Database) return cluster.vpc;
981
+ }
982
+
983
+ return undefined;
984
+ }
985
+
986
+ /**
987
+ * Mount every surface's endpoints — the handlers the build wrote, each linked
988
+ * to exactly what that endpoint depends on.
989
+ *
990
+ * After every construct is provisioned, for the reason consumers are: an
991
+ * endpoint links to things `provisionOrder` does not order before its surface.
992
+ */
993
+ export function mountRoutes(
994
+ manifest: ConstructManifest,
995
+ provisioned: ProvisionedManifest,
996
+ ): void {
997
+ for (const [id, declaration] of Object.entries(manifest)) {
998
+ if (declaration?.kind !== 'rest-api') continue;
999
+
1000
+ const surface = provisioned[id];
1001
+ if (!(surface instanceof RestApiSurface)) continue;
1002
+
1003
+ for (const endpoint of declaration.endpoints) {
1004
+ const { link } = resolveEdges(endpoint.dependencies, provisioned);
1005
+ const vpc = vpcFor(link);
1006
+ surface.mount({
1007
+ method: endpoint.method,
1008
+ path: endpoint.path,
1009
+ handler: endpoint.handler,
1010
+ link,
1011
+ ...(vpc ? { vpc } : {}),
1012
+ ...(endpoint.authorizer ? { authorizer: endpoint.authorizer } : {}),
1013
+ });
1014
+ }
1015
+ }
1016
+ }
1017
+
1018
+ /**
1019
+ * Mount every WebSocket surface's routes — the Lambdas the build wrote, each
1020
+ * linked to what that route depends on and to the surface itself (its
1021
+ * connection table and management API). `$connect` is also linked to what
1022
+ * authenticates the surface, since it verifies the handshake's session.
1023
+ *
1024
+ * After every construct is provisioned, for the reason endpoints are.
1025
+ */
1026
+ export function mountWebSocketRoutes(
1027
+ manifest: ConstructManifest,
1028
+ provisioned: ProvisionedManifest,
1029
+ ): void {
1030
+ for (const [id, declaration] of Object.entries(manifest)) {
1031
+ if (declaration?.kind !== 'websocket-api') continue;
1032
+
1033
+ const surface = provisioned[id];
1034
+ if (!(surface instanceof WebSocketApiSurface)) continue;
1035
+
1036
+ for (const route of declaration.routes ?? []) {
1037
+ const edges =
1038
+ route.action === '$connect' && declaration.auth
1039
+ ? [...route.dependencies, declaration.auth]
1040
+ : route.dependencies;
1041
+ const { link } = resolveEdges(edges, provisioned);
1042
+ const vpc = vpcFor(link);
1043
+ surface.mount({
1044
+ action: route.action,
1045
+ handler: route.handler,
1046
+ link,
1047
+ ...(vpc ? { vpc } : {}),
1048
+ });
1049
+ }
1050
+ }
1051
+ }
1052
+
1053
+ /**
1054
+ * Subscribe every queue's one consumer — the handler the build wrote for it,
1055
+ * linked to what that consumer depends on.
1056
+ *
1057
+ * After every construct is provisioned, because a consumer links to things
1058
+ * that `provisionOrder` does not order before the queue.
1059
+ */
1060
+ export function subscribeConsumers(
1061
+ manifest: ConstructManifest,
1062
+ provisioned: ProvisionedManifest,
1063
+ ): void {
1064
+ for (const [id, declaration] of Object.entries(manifest)) {
1065
+ if (declaration?.kind !== 'queue') continue;
1066
+
1067
+ const queue = provisioned[id] as unknown as Consumable | undefined;
1068
+ if (!queue) continue;
1069
+
1070
+ const { link } = resolveEdges(declaration.worker.dependencies, provisioned);
1071
+ const vpc = vpcFor(link);
1072
+ queue.consume({
1073
+ handler: declaration.worker.handler,
1074
+ link,
1075
+ ...(vpc ? { vpc } : {}),
1076
+ });
1077
+ }
1078
+ }
1079
+
1080
+ /** The topics inboxes declared: their provider's, never SST's to create. */
1081
+ export function inboxTopicIds(manifest: ConstructManifest): Set<string> {
1082
+ return new Set(
1083
+ Object.values(manifest).flatMap((declaration) =>
1084
+ declaration.kind === 'inbox' ? [declaration.topic] : [],
1085
+ ),
1086
+ );
1087
+ }
1088
+
1089
+ /**
1090
+ * Each inbox's topic, as the provider made it — the ARN the inbox read from
1091
+ * the deploy — under the topic's own id, so its declaration's contract is
1092
+ * checked like any other.
1093
+ */
1094
+ export function referenceInboxTopics(
1095
+ manifest: ConstructManifest,
1096
+ provisioned: ProvisionedManifest,
1097
+ ): void {
1098
+ for (const declaration of Object.values(manifest)) {
1099
+ if (declaration.kind !== 'inbox') continue;
1100
+ const inbox = provisioned[declaration.id];
1101
+ if (!(inbox instanceof Inbox)) continue;
1102
+
1103
+ const topic = new InboxTopic(declaration.topic, inbox);
1104
+ const topicDeclaration = manifest[declaration.topic];
1105
+ assertProvides(
1106
+ declaration.topic,
1107
+ topicDeclaration?.provides,
1108
+ Object.keys(topic.provides()).map((role) =>
1109
+ providedKeyFor(declaration.topic, 'topic', role),
1110
+ ),
1111
+ );
1112
+ provisioned[declaration.topic] = topic as unknown as Provisioned;
1113
+ }
1114
+ }
1115
+
1116
+ /**
1117
+ * Subscribe each inbox subscriber's Lambda to its inbox's topic — the
1118
+ * provider's, by ARN — linked to what it depends on, its inbox included.
1119
+ *
1120
+ * Unfiltered: SES publishes its receipt notification with no `type` attribute
1121
+ * to filter on, and every message on an inbox's topic is for its subscribers.
1122
+ * The subscriber's adaptor reads the notification as `email.received`.
1123
+ */
1124
+ export function subscribeInboxes(
1125
+ manifest: ConstructManifest,
1126
+ provisioned: ProvisionedManifest,
1127
+ ): void {
1128
+ for (const declaration of Object.values(manifest)) {
1129
+ if (declaration.kind !== 'inbox') continue;
1130
+ const inbox = provisioned[declaration.id];
1131
+ const topic = manifest[declaration.topic];
1132
+ if (!(inbox instanceof Inbox) || topic?.kind !== 'topic') continue;
1133
+
1134
+ for (const subscriber of topic.subscribers) {
1135
+ const { link } = resolveEdges(subscriber.dependencies, provisioned);
1136
+ const vpc = vpcFor(link);
1137
+ sst.aws.SnsTopic.subscribe(
1138
+ `${subscriber.id}Subscription`,
1139
+ inbox.topicArn,
1140
+ {
1141
+ handler: subscriber.handler,
1142
+ link,
1143
+ ...(vpc ? { vpc } : {}),
1144
+ },
1145
+ );
1146
+ }
1147
+ }
1148
+ }
1149
+
725
1150
  /** The provisioner for a kind. Pure — the lookup is testable without Pulumi. */
726
1151
  export function provisionerFor(kind: DeclarationKind): Provisioner {
727
1152
  const provisioner = PROVISIONERS[kind];
@@ -760,20 +1185,34 @@ export function assertProvides(
760
1185
  * tenant, a read replica, and the surface over a bucket all resolve an address
761
1186
  * off something else, and a pass in map order would find it half the time.
762
1187
  */
763
- export function fromManifest(
1188
+ export function fromManifest<
1189
+ const M extends ConstructManifest,
1190
+ const B extends ManifestBackends = {},
1191
+ >(
764
1192
  stack: StackType,
765
- manifest: ConstructManifest,
766
- overrides: ComponentOverrides = {},
1193
+ manifest: M,
1194
+ /**
1195
+ * Provider-specific props, per construct id — typed from the manifest, so
1196
+ * only its ids are keys, each takes what its kind's component reads, and
1197
+ * what the synth cannot guess (a database's `vpc`, mail's `from`) is
1198
+ * required. See {@link ManifestOverrides}.
1199
+ *
1200
+ * Neutral options — `versioned`, `fifo` — travel in the declaration,
1201
+ * because the app legitimately has an opinion about them. Anything with S3
1202
+ * in its name does not belong in application code, so it is supplied here.
1203
+ * A third escape hatch needs no API: the components are returned, so
1204
+ * `provisioned.Uploads.nodes.bucket` is reachable for anything else.
1205
+ */
1206
+ overrides: ManifestOverrides<M, B>,
767
1207
  /**
768
1208
  * The backend choices that are config rather than declaration.
769
1209
  *
770
1210
  * Defaulted the same way the local target defaults them, so a stage deployed
771
- * without saying gets the same backend a developer ran against.
1211
+ * without saying gets the same backend a developer ran against. Typed
1212
+ * literally, because the backend decides what is required above: an
1213
+ * ElastiCache cache needs a `vpc`, Resend or SMTP mail a `url`.
772
1214
  */
773
- backends: {
774
- cache?: 'upstash' | 'elasticache' | 'db';
775
- email?: 'resend' | 'ses' | 'smtp';
776
- } = {},
1215
+ backends: B = {} as B,
777
1216
  ): ProvisionedManifest {
778
1217
  const provisioned: ProvisionedManifest = {};
779
1218
 
@@ -795,7 +1234,12 @@ export function fromManifest(
795
1234
  // the things that call it exist.
796
1235
  const callers = new Map<string, Deferred>();
797
1236
  for (const [id, declaration] of Object.entries(manifest)) {
798
- if (declaration.kind === 'rest-api') callers.set(id, defer());
1237
+ if (
1238
+ declaration.kind === 'rest-api' ||
1239
+ declaration.kind === 'websocket-api'
1240
+ ) {
1241
+ callers.set(id, defer());
1242
+ }
799
1243
  }
800
1244
 
801
1245
  const context: ProvisionContext = {
@@ -805,16 +1249,24 @@ export function fromManifest(
805
1249
  bootstraps: new Map<string, DatabaseBootstrap>(),
806
1250
  ...(backends.cache ? { cache: backends.cache } : {}),
807
1251
  ...(backends.email ? { email: backends.email } : {}),
1252
+ ...(backends.protectedSubdomains
1253
+ ? { protectedSubdomains: backends.protectedSubdomains }
1254
+ : {}),
808
1255
  };
809
1256
 
1257
+ const inboxTopics = inboxTopicIds(manifest);
1258
+
810
1259
  for (const id of [...rest, ...sites]) {
811
1260
  const declaration = manifest[id];
812
1261
  if (!declaration) continue;
1262
+ if (PROVISIONED_ELSEWHERE.has(declaration.kind)) continue;
1263
+ // Its provider made it: referenced once its inbox is, below.
1264
+ if (inboxTopics.has(id)) continue;
813
1265
 
814
1266
  const component = provisionerFor(declaration.kind)(
815
1267
  stack,
816
1268
  declaration,
817
- overrides[id] ?? {},
1269
+ (overrides as Record<string, Record<string, unknown>>)[id] ?? {},
818
1270
  context,
819
1271
  );
820
1272
 
@@ -833,6 +1285,17 @@ export function fromManifest(
833
1285
  provisioned[id] = component;
834
1286
  }
835
1287
 
1288
+ referenceInboxTopics(manifest, provisioned);
1289
+
1290
+ // The functions and crons, each queue's one consumer and each surface's
1291
+ // endpoints, once everything they link to exists.
1292
+ provisionCompute(stack, manifest, provisioned);
1293
+ subscribeInboxes(manifest, provisioned);
1294
+ provisionProcessors(stack, manifest, provisioned);
1295
+ subscribeConsumers(manifest, provisioned);
1296
+ mountRoutes(manifest, provisioned);
1297
+ mountWebSocketRoutes(manifest, provisioned);
1298
+
836
1299
  for (const line of describeRoutes(manifest)) console.log(line);
837
1300
 
838
1301
  // Now everything exists, so every surface's callers can be resolved — the
@@ -840,7 +1303,7 @@ export function fromManifest(
840
1303
  for (const [id, deferred] of callers) {
841
1304
  // Resolved with the promise itself: the addresses it needs are Pulumi
842
1305
  // outputs, so the answer arrives when they do.
843
- deferred.resolve(callersOf(id, manifest, provisioned));
1306
+ deferred.resolve(callersOf(id, manifest, provisioned, $app.name));
844
1307
  }
845
1308
 
846
1309
  // Last, and only now: the roles exist as passwords and secrets from the
@@ -855,6 +1318,30 @@ export function fromManifest(
855
1318
  return provisioned;
856
1319
  }
857
1320
 
1321
+ /**
1322
+ * A secret in its declared format — the alphabet `drawSecret` draws from on
1323
+ * every other stage, with one symbol guaranteed as it guarantees one.
1324
+ *
1325
+ * The format is fixed at the first deploy, as a server stage's is: changing it
1326
+ * would replace the password, and every later deploy is meant to read back the
1327
+ * one it had.
1328
+ */
1329
+ function generatedSecret(id: string, format: SecretFormat) {
1330
+ const symbols = [...new Set(format.symbols ?? SECRET_SYMBOLS)].join('');
1331
+
1332
+ return new RandomPassword(
1333
+ `${id}Generated`,
1334
+ {
1335
+ length: format.length ?? SECRET_LENGTH,
1336
+ special: symbols.length > 0,
1337
+ ...(symbols ? { overrideSpecial: symbols, minSpecial: 1 } : {}),
1338
+ },
1339
+ {
1340
+ ignoreChanges: ['length', 'special', 'overrideSpecial', 'minSpecial'],
1341
+ },
1342
+ ).result;
1343
+ }
1344
+
858
1345
  /** What one function receives from its edges. */
859
1346
  export interface ResolvedEdges {
860
1347
  /** The components it may reach. SST turns these into IAM *and* injects their