@geekmidas/cloud 10.0.0-alpha.131 → 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.131",
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/manifest": "10.0.0-alpha.131",
45
- "@geekmidas/envkit": "10.0.0-alpha.131"
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.131",
62
- "@geekmidas/storage": "^10.0.0-alpha.131",
63
- "@geekmidas/events": "^10.0.0-alpha.131",
64
- "@geekmidas/db": "^10.0.0-alpha.131"
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
@@ -63,3 +63,18 @@ export class UnresolvedDependency extends Error {
63
63
  this.available = available;
64
64
  }
65
65
  }
66
+
67
+ /**
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
+ */
72
+ export class WildcardSiteNeedsDomain extends Error {
73
+ constructor(readonly id: string) {
74
+ super(
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
+ );
78
+ this.name = 'WildcardSiteNeedsDomain';
79
+ }
80
+ }
@@ -28,6 +28,7 @@ import {
28
28
  cookieDomain,
29
29
  DEFAULT_POSTGRES_VERSION,
30
30
  dependentsOf,
31
+ isWildcardSite,
31
32
  mobileOrigins,
32
33
  PUBLIC,
33
34
  PUBLIC_PREFIX,
@@ -117,6 +118,8 @@ export interface ProvisionContext {
117
118
  */
118
119
  cache?: 'upstash' | 'elasticache' | 'db';
119
120
  email?: 'resend' | 'ses' | 'smtp';
121
+ /** The labels no tenant of a wildcard site may take. */
122
+ protectedSubdomains?: readonly string[];
120
123
  /**
121
124
  * One deferred caller list per surface, resolved once everything exists.
122
125
  *
@@ -186,7 +189,15 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
186
189
  return new StaticSite(stack, d.id, {
187
190
  path: d.app.path,
188
191
  variant: d.variant,
189
- 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
+ : {}),
190
201
  ...props,
191
202
  });
192
203
  },
@@ -441,10 +452,25 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
441
452
  export function siteEnvironment(
442
453
  declaration: SiteDeclaration,
443
454
  context: ProvisionContext,
455
+ stack?: StackType,
444
456
  ): Record<string, $util.Input<string>> {
445
457
  const prefix = PUBLIC_PREFIX[declaration.variant];
446
458
  const environment: Record<string, $util.Input<string>> = {};
447
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
+
448
474
  for (const edge of declaration.dependencies) {
449
475
  const target = context.manifest[edge.target];
450
476
  if (!target) continue;
@@ -1078,6 +1104,9 @@ export function fromManifest<
1078
1104
  bootstraps: new Map<string, DatabaseBootstrap>(),
1079
1105
  ...(backends.cache ? { cache: backends.cache } : {}),
1080
1106
  ...(backends.email ? { email: backends.email } : {}),
1107
+ ...(backends.protectedSubdomains
1108
+ ? { protectedSubdomains: backends.protectedSubdomains }
1109
+ : {}),
1081
1110
  };
1082
1111
 
1083
1112
  for (const id of [...rest, ...sites]) {
package/src/sst/index.ts CHANGED
@@ -59,6 +59,7 @@ export {
59
59
  ProvidesMismatch,
60
60
  UnknownDeclarationKind,
61
61
  UnresolvedDependency,
62
+ WildcardSiteNeedsDomain,
62
63
  } from './errors';
63
64
  export {
64
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'