@geekmidas/cloud 10.0.0-alpha.132 → 10.0.0-alpha.133

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geekmidas/cloud",
3
- "version": "10.0.0-alpha.132",
3
+ "version": "10.0.0-alpha.133",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -41,8 +41,8 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "lodash.snakecase": "~4.1.1",
44
- "@geekmidas/envkit": "10.0.0-alpha.132",
45
- "@geekmidas/manifest": "10.0.0-alpha.132"
44
+ "@geekmidas/envkit": "10.0.0-alpha.133",
45
+ "@geekmidas/manifest": "10.0.0-alpha.133"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@types/lodash.get": "~4.4.9",
@@ -58,10 +58,10 @@
58
58
  "@pulumi/aws": ">=7.47.0",
59
59
  "@pulumi/random": ">=4.21.2",
60
60
  "@pulumi/pulumi": ">=3.263.0",
61
- "@geekmidas/manifest": "^10.0.0-alpha.132",
62
- "@geekmidas/storage": "^10.0.0-alpha.132",
63
- "@geekmidas/events": "^10.0.0-alpha.132",
64
- "@geekmidas/db": "^10.0.0-alpha.132"
61
+ "@geekmidas/manifest": "^10.0.0-alpha.133",
62
+ "@geekmidas/storage": "^10.0.0-alpha.133",
63
+ "@geekmidas/events": "^10.0.0-alpha.133",
64
+ "@geekmidas/db": "^10.0.0-alpha.133"
65
65
  },
66
66
  "peerDependenciesMeta": {
67
67
  "@geekmidas/manifest": {
@@ -0,0 +1,195 @@
1
+ import {
2
+ type ConstructManifest,
3
+ PUBLIC_PREFIX,
4
+ providedKeyFor,
5
+ trustsOrigin,
6
+ } from '@geekmidas/manifest';
7
+ import { afterEach, describe, expect, it, vi } from 'vitest';
8
+ import { App } from '../App';
9
+ import { WildcardSiteNeedsDomain } from '../errors';
10
+ import { fromManifest } from '../fromManifest';
11
+
12
+ /**
13
+ * A site on every subdomain, on AWS: one distribution under `*.<domain>`, a
14
+ * wildcard certificate validated through the stack's zone, one alias record,
15
+ * and nothing per tenant.
16
+ */
17
+
18
+ const keys = (id: string, kind: string, roles: string[]) =>
19
+ roles.map((role) => providedKeyFor(id, kind as never, role));
20
+
21
+ function manifest(): ConstructManifest {
22
+ return {
23
+ Api: {
24
+ kind: 'rest-api',
25
+ id: 'Api',
26
+ path: 'apps/api',
27
+ endpoints: [
28
+ {
29
+ id: 'TenantHosts',
30
+ handler: 'hosts.handler',
31
+ method: 'GET',
32
+ path: '/tenants/hosts',
33
+ dependencies: [],
34
+ },
35
+ ],
36
+ provides: keys('Api', 'rest-api', [
37
+ 'url',
38
+ 'trustedOrigins',
39
+ 'cookieDomain',
40
+ ]),
41
+ },
42
+ Admin: {
43
+ kind: 'site',
44
+ id: 'Admin',
45
+ variant: 'static',
46
+ subdomain: 'admin',
47
+ app: { path: 'apps/admin' },
48
+ dependencies: [{ target: 'Api', kind: 'rest-api' }],
49
+ provides: keys('Admin', 'site', ['url']),
50
+ },
51
+ App: {
52
+ kind: 'site',
53
+ id: 'App',
54
+ variant: 'next',
55
+ subdomain: '*',
56
+ hosts: { surface: 'Api', path: '/tenants/hosts' },
57
+ app: { path: 'apps/web' },
58
+ dependencies: [{ target: 'Api', kind: 'rest-api' }],
59
+ provides: keys('App', 'site', ['url', 'domain', 'protectedSubdomains']),
60
+ },
61
+ } as unknown as ConstructManifest;
62
+ }
63
+
64
+ const stackOn = (domain: string, hostedZoneId: string = 'Z123') =>
65
+ new App({
66
+ name: 'shop',
67
+ stage: 'production',
68
+ domain,
69
+ hostedZoneId,
70
+ region: 'eu-west-1',
71
+ }).stack('shop');
72
+
73
+ const argsOf = (component: unknown) =>
74
+ (component as { args: Record<string, any> }).args;
75
+
76
+ const settle = async (value: unknown): Promise<unknown> =>
77
+ typeof value === 'object' && value && 'apply' in value
78
+ ? (value as { apply: (fn: (v: unknown) => unknown) => unknown }).apply(
79
+ (v) => v,
80
+ )
81
+ : value;
82
+
83
+ const PROTECTED = ['admin', 'api', 'www'];
84
+
85
+ describe('a wildcard site on AWS', () => {
86
+ afterEach(() => vi.restoreAllMocks());
87
+
88
+ it('is one distribution under *.<domain>, its certificate and alias through the stack’s zone', () => {
89
+ vi.spyOn(console, 'log').mockImplementation(() => {});
90
+
91
+ const provisioned = fromManifest(
92
+ stackOn('example.com'),
93
+ manifest(),
94
+ {},
95
+ {
96
+ protectedSubdomains: PROTECTED,
97
+ },
98
+ );
99
+
100
+ expect(argsOf(provisioned.App).domain).toEqual({
101
+ name: '*.example.com',
102
+ dns: { dns: { zone: 'Z123' } },
103
+ });
104
+ // A named site keeps its own: nothing about it changed.
105
+ expect(argsOf(provisioned.Admin).domain).toBeUndefined();
106
+ });
107
+
108
+ it('looks its zone up by name when the stack has none resolved', () => {
109
+ vi.spyOn(console, 'log').mockImplementation(() => {});
110
+
111
+ const provisioned = fromManifest(
112
+ stackOn('example.com', ''),
113
+ manifest(),
114
+ {},
115
+ { protectedSubdomains: PROTECTED },
116
+ );
117
+
118
+ expect(argsOf(provisioned.App).domain.dns).toEqual({ dns: {} });
119
+ });
120
+
121
+ it('provides its address, domain and protected labels, and inlines the last two', () => {
122
+ vi.spyOn(console, 'log').mockImplementation(() => {});
123
+
124
+ const provisioned = fromManifest(
125
+ stackOn('example.com'),
126
+ manifest(),
127
+ {},
128
+ {
129
+ protectedSubdomains: PROTECTED,
130
+ },
131
+ );
132
+
133
+ expect(provisioned.App!.provides()).toEqual({
134
+ url: 'https://*.example.com',
135
+ domain: 'example.com',
136
+ protectedSubdomains: 'admin,api,www',
137
+ });
138
+
139
+ const env = argsOf(provisioned.App).environment as Record<string, unknown>;
140
+ const prefix = PUBLIC_PREFIX.next;
141
+ expect(env[`${prefix}${providedKeyFor('App', 'site', 'domain')}`]).toBe(
142
+ 'example.com',
143
+ );
144
+ expect(
145
+ env[`${prefix}${providedKeyFor('App', 'site', 'protectedSubdomains')}`],
146
+ ).toBe('admin,api,www');
147
+ });
148
+
149
+ it('has the surfaces it calls trust every tenant’s origin, one label deep', async () => {
150
+ vi.spyOn(console, 'log').mockImplementation(() => {});
151
+
152
+ const provisioned = fromManifest(
153
+ stackOn('example.com'),
154
+ manifest(),
155
+ {},
156
+ {
157
+ protectedSubdomains: PROTECTED,
158
+ },
159
+ );
160
+ const trusted = String(
161
+ await settle(provisioned.Api!.provides().trustedOrigins),
162
+ ).split(',');
163
+
164
+ expect(trusted).toContain('https://*.example.com');
165
+ expect(trustsOrigin('https://acme.example.com', trusted)).toBe(true);
166
+ expect(trustsOrigin('https://a.b.example.com', trusted)).toBe(false);
167
+ expect(trustsOrigin('https://example.com', trusted)).toBe(false);
168
+ });
169
+
170
+ it('lets an override win, as every other prop does', () => {
171
+ vi.spyOn(console, 'log').mockImplementation(() => {});
172
+
173
+ const domain = { name: '*.tenants.example.com' };
174
+ const provisioned = fromManifest(
175
+ stackOn('example.com'),
176
+ manifest(),
177
+ { App: { domain } } as never,
178
+ { protectedSubdomains: PROTECTED },
179
+ );
180
+
181
+ expect(argsOf(provisioned.App).domain).toBe(domain);
182
+ });
183
+
184
+ it('is refused on a stack with no domain, naming the site', () => {
185
+ vi.spyOn(console, 'log').mockImplementation(() => {});
186
+
187
+ expect.assertions(2);
188
+ try {
189
+ fromManifest(stackOn(''), manifest(), {}, {});
190
+ } catch (error) {
191
+ expect(error).toBeInstanceOf(WildcardSiteNeedsDomain);
192
+ expect((error as WildcardSiteNeedsDomain).id).toBe('App');
193
+ }
194
+ });
195
+ });
@@ -1,3 +1,4 @@
1
+ import { WildcardSiteNeedsDomain } from '../errors';
1
2
  import { type GkmLinkable, ResourceType } from '../Linkable';
2
3
  import type { StackType } from '../Stack';
3
4
 
@@ -14,6 +15,14 @@ import type { StackType } from '../Stack';
14
15
  * which provided values may be shipped to a browser, and renamed by the
15
16
  * variant's prefix. That derivation is shared with the local target, so a site
16
17
  * built by `gkm dev` and the same site built by a deploy inline the same names.
18
+ *
19
+ * A site on every subdomain (`tenants`) is the same distribution under one
20
+ * `*.<domain>` alternate domain name: a wildcard certificate in us-east-1,
21
+ * validated through the stack's hosted zone at deploy time, and one
22
+ * `*.<domain>` alias record. Nothing is created per tenant — a tenant exists
23
+ * because the app says so, and its `hosts` route is what answers an unknown
24
+ * one. Every named host keeps its own record, which Route 53 prefers over the
25
+ * wildcard, so `api.<domain>` is never the tenants' site.
17
26
  */
18
27
  export class StaticSite<
19
28
  TStage extends string = string,
@@ -23,6 +32,7 @@ export class StaticSite<
23
32
  implements GkmLinkable
24
33
  {
25
34
  readonly _id!: string;
35
+ private readonly tenants?: { domain: string; protectedSubdomains: string };
26
36
 
27
37
  get _type() {
28
38
  return ResourceType.StaticSite;
@@ -33,7 +43,8 @@ export class StaticSite<
33
43
  name: string,
34
44
  props: StaticSiteProps = {},
35
45
  ) {
36
- const { variant = 'static', ...args } = props;
46
+ const { variant = 'static', tenants, ...args } = props;
47
+ if (tenants && !_stack.domain) throw new WildcardSiteNeedsDomain(name);
37
48
 
38
49
  super(name, {
39
50
  // The variant's build, unless the caller supplied one. A site is
@@ -42,9 +53,30 @@ export class StaticSite<
42
53
  // exactly what the variant knows and the declaration deliberately does
43
54
  // not.
44
55
  build: BUILDS[variant],
56
+ // Every tenant's host on one distribution. SST requests the `*.`
57
+ // certificate and validates it through the zone, then writes the
58
+ // alias; a zone left unresolved is looked up by the domain's name.
59
+ ...(tenants
60
+ ? {
61
+ domain: {
62
+ name: `*.${_stack.domain}`,
63
+ dns: sst.aws.dns(
64
+ _stack.app.hostedZoneId
65
+ ? { zone: _stack.app.hostedZoneId }
66
+ : {},
67
+ ),
68
+ },
69
+ }
70
+ : {}),
45
71
  ...args,
46
72
  });
47
73
  this._id = name;
74
+ if (tenants) {
75
+ this.tenants = {
76
+ domain: _stack.domain,
77
+ protectedSubdomains: tenants.protectedSubdomains.join(','),
78
+ };
79
+ }
48
80
  }
49
81
 
50
82
  /**
@@ -56,6 +88,11 @@ export class StaticSite<
56
88
  * site was meant to remove.
57
89
  */
58
90
  provides(): Record<string, $util.Input<string>> {
91
+ // A tenants' site is at `https://*.<domain>`, the address every target
92
+ // gives it: what the surfaces it calls trust, one label in for the `*`.
93
+ if (this.tenants) {
94
+ return { url: `https://*.${this.tenants.domain}`, ...this.tenants };
95
+ }
59
96
  return { url: this.url };
60
97
  }
61
98
 
@@ -71,6 +108,12 @@ export class StaticSite<
71
108
  export interface StaticSiteProps extends sst.aws.StaticSiteArgs {
72
109
  /** Which framework builds it, for the default build command. */
73
110
  variant?: 'static' | 'next' | 'tanstack';
111
+ /**
112
+ * Serve it on every subdomain of the stack's domain — a site declared
113
+ * `subdomain: '*'`. `protectedSubdomains` are the labels no tenant may
114
+ * take, provided to the site so it refuses them as slugs.
115
+ */
116
+ tenants?: { protectedSubdomains: readonly string[] };
74
117
  }
75
118
 
76
119
  /**
package/src/sst/errors.ts CHANGED
@@ -65,16 +65,16 @@ export class UnresolvedDependency extends Error {
65
65
  }
66
66
 
67
67
  /**
68
- * A site declared `subdomain: '*'`, which the AWS target cannot serve yet: a
69
- * distribution answers on the hosts it was given, and a tenant's host is
70
- * created by the running app.
68
+ * A site declared `subdomain: '*'` on a stack with no domain: its tenants'
69
+ * hosts are `<slug>.<domain>`, and without the domain there is nothing for
70
+ * the `*` to stand in front of.
71
71
  */
72
- export class WildcardSiteNotSupported extends Error {
72
+ export class WildcardSiteNeedsDomain extends Error {
73
73
  constructor(readonly id: string) {
74
74
  super(
75
- `'${id}' answers on every subdomain, which the AWS target cannot ` +
76
- `serve yet. Give it a subdomain of its own to deploy it here.`,
75
+ `'${id}' answers on every subdomain, so its stack needs a domain: ` +
76
+ `pass the stage's domain (and its Route 53 hosted zone) to \`new App({ domain, hostedZoneId })\` in sst.config.ts.`,
77
77
  );
78
- this.name = 'WildcardSiteNotSupported';
78
+ this.name = 'WildcardSiteNeedsDomain';
79
79
  }
80
80
  }
@@ -66,7 +66,6 @@ import {
66
66
  ProvidesMismatch,
67
67
  UnknownDeclarationKind,
68
68
  UnresolvedDependency,
69
- WildcardSiteNotSupported,
70
69
  } from './errors';
71
70
  import type { GkmLinkable } from './Linkable';
72
71
  import type { ManifestBackends, ManifestOverrides } from './overrides';
@@ -119,6 +118,8 @@ export interface ProvisionContext {
119
118
  */
120
119
  cache?: 'upstash' | 'elasticache' | 'db';
121
120
  email?: 'resend' | 'ses' | 'smtp';
121
+ /** The labels no tenant of a wildcard site may take. */
122
+ protectedSubdomains?: readonly string[];
122
123
  /**
123
124
  * One deferred caller list per surface, resolved once everything exists.
124
125
  *
@@ -184,13 +185,19 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
184
185
 
185
186
  site: (stack, d, props, context) => {
186
187
  if (d.kind !== 'site') throw new UnknownDeclarationKind(d.kind, []);
187
- // Routing every subdomain through CloudFront is #299's.
188
- if (isWildcardSite(d)) throw new WildcardSiteNotSupported(d.id);
189
188
 
190
189
  return new StaticSite(stack, d.id, {
191
190
  path: d.app.path,
192
191
  variant: d.variant,
193
- environment: siteEnvironment(d, context),
192
+ environment: siteEnvironment(d, context, stack),
193
+ // Every subdomain, on one distribution: see `StaticSite`.
194
+ ...(isWildcardSite(d)
195
+ ? {
196
+ tenants: {
197
+ protectedSubdomains: context.protectedSubdomains ?? [],
198
+ },
199
+ }
200
+ : {}),
194
201
  ...props,
195
202
  });
196
203
  },
@@ -445,10 +452,25 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
445
452
  export function siteEnvironment(
446
453
  declaration: SiteDeclaration,
447
454
  context: ProvisionContext,
455
+ stack?: StackType,
448
456
  ): Record<string, $util.Input<string>> {
449
457
  const prefix = PUBLIC_PREFIX[declaration.variant];
450
458
  const environment: Record<string, $util.Input<string>> = {};
451
459
 
460
+ // A wildcard site takes its tenant off the host it is reached on, so it
461
+ // inlines the domain to take the label off against, and the labels no
462
+ // tenant may be — the same two keys `publicEnvFor` names locally.
463
+ if (isWildcardSite(declaration) && stack?.domain) {
464
+ const own = {
465
+ domain: stack.domain,
466
+ protectedSubdomains: (context.protectedSubdomains ?? []).join(','),
467
+ };
468
+ for (const [role, value] of Object.entries(own)) {
469
+ environment[`${prefix}${providedKeyFor(declaration.id, 'site', role)}`] =
470
+ value;
471
+ }
472
+ }
473
+
452
474
  for (const edge of declaration.dependencies) {
453
475
  const target = context.manifest[edge.target];
454
476
  if (!target) continue;
@@ -1082,6 +1104,9 @@ export function fromManifest<
1082
1104
  bootstraps: new Map<string, DatabaseBootstrap>(),
1083
1105
  ...(backends.cache ? { cache: backends.cache } : {}),
1084
1106
  ...(backends.email ? { email: backends.email } : {}),
1107
+ ...(backends.protectedSubdomains
1108
+ ? { protectedSubdomains: backends.protectedSubdomains }
1109
+ : {}),
1085
1110
  };
1086
1111
 
1087
1112
  for (const id of [...rest, ...sites]) {
package/src/sst/index.ts CHANGED
@@ -59,7 +59,7 @@ export {
59
59
  ProvidesMismatch,
60
60
  UnknownDeclarationKind,
61
61
  UnresolvedDependency,
62
- WildcardSiteNotSupported,
62
+ WildcardSiteNeedsDomain,
63
63
  } from './errors';
64
64
  export {
65
65
  assertProvides,
@@ -32,6 +32,11 @@ import type { TopicProps } from './aws/Topic';
32
32
  export interface ManifestBackends {
33
33
  cache?: 'upstash' | 'elasticache' | 'db';
34
34
  email?: 'resend' | 'ses' | 'smtp';
35
+ /**
36
+ * The labels no tenant of a wildcard site may take: every app's own and
37
+ * the workspace's `protectedSubdomains`, as `gkm build` derived them.
38
+ */
39
+ protectedSubdomains?: readonly string[];
35
40
  }
36
41
 
37
42
  /** The backend a choice resolves to — the provisioner's default when unset. */
@@ -71,7 +76,12 @@ export type OverrideFor<D, B extends ManifestBackends = {}> = D extends {
71
76
  : K extends 'file-server'
72
77
  ? Partial<Omit<FileServerProps, 'origin'>>
73
78
  : K extends 'site'
74
- ? Partial<Omit<StaticSiteProps, 'path' | 'variant' | 'environment'>>
79
+ ? Partial<
80
+ Omit<
81
+ StaticSiteProps,
82
+ 'path' | 'variant' | 'environment' | 'tenants'
83
+ >
84
+ >
75
85
  : K extends 'queue'
76
86
  ? Partial<Omit<QueueProps, 'fifo'>>
77
87
  : K extends 'topic'