@geekmidas/cloud 10.0.0-alpha.15 → 10.0.0-alpha.151
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -6
- package/package.json +9 -8
- package/src/sst/__tests__/api.spec.ts +0 -46
- package/src/sst/__tests__/bootstrap.spec.ts +15 -0
- package/src/sst/__tests__/encryption.spec.ts +90 -0
- package/src/sst/__tests__/fromManifest.inbox.spec.ts +160 -0
- package/src/sst/__tests__/fromManifest.integration.spec.ts +86 -2
- package/src/sst/__tests__/fromManifest.secret.spec.ts +79 -0
- package/src/sst/__tests__/fromManifest.spec.ts +379 -5
- package/src/sst/__tests__/function.spec.ts +0 -71
- package/src/sst/__tests__/surfaces.spec.ts +17 -0
- package/src/sst/__tests__/wildcardSite.spec.ts +195 -0
- package/src/sst/__type-tests__/overrides.type-test.ts +152 -0
- package/src/sst/aws/Api.ts +0 -43
- package/src/sst/aws/Credential.ts +42 -9
- package/src/sst/aws/Cron.ts +5 -45
- package/src/sst/aws/DatabaseBootstrap.ts +6 -0
- package/src/sst/aws/Encryption.ts +91 -0
- package/src/sst/aws/ExternalApi.ts +58 -0
- package/src/sst/aws/Function.ts +9 -38
- package/src/sst/aws/Inbox.ts +138 -0
- package/src/sst/aws/Queue.ts +34 -1
- package/src/sst/aws/RestApiSurface.ts +36 -14
- package/src/sst/aws/Secret.ts +5 -3
- package/src/sst/aws/StaticSite.ts +44 -1
- package/src/sst/aws/bootstrap/handler.ts +12 -1
- package/src/sst/errors.ts +15 -0
- package/src/sst/fromManifest.ts +472 -50
- package/src/sst/index.ts +13 -1
- package/src/sst/overrides.ts +147 -0
- package/src/sst/__type-tests__/manifest.type-test.ts +0 -77
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { externalApiUrl } from '@geekmidas/manifest';
|
|
2
|
+
import { type GkmLinkable, ResourceType } from '../Linkable';
|
|
3
|
+
import type { StackType } from '../Stack';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `ExternalApi` — the infra half of the `external-api` kind.
|
|
7
|
+
*
|
|
8
|
+
* Nothing is provisioned: the API is somebody else's. What this holds is the
|
|
9
|
+
* two facts a function needs to call it on this stage — the URL the
|
|
10
|
+
* declaration names for it, and the credentials the provider issued, kept as
|
|
11
|
+
* an SST secret and set out of band:
|
|
12
|
+
*
|
|
13
|
+
* ```
|
|
14
|
+
* sst secret set PolarCredentials '{"clientId":"…","clientSecret":"…"}' --stage prod
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export class ExternalApi<
|
|
18
|
+
TStage extends string = string,
|
|
19
|
+
TDomain extends string = string,
|
|
20
|
+
> implements GkmLinkable
|
|
21
|
+
{
|
|
22
|
+
readonly _id: string;
|
|
23
|
+
|
|
24
|
+
private readonly url: string;
|
|
25
|
+
|
|
26
|
+
private readonly credentials: sst.Secret;
|
|
27
|
+
|
|
28
|
+
get _type() {
|
|
29
|
+
return ResourceType.ExternalApi;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
stack: StackType<TStage, TDomain>,
|
|
34
|
+
name: string,
|
|
35
|
+
props: ExternalApiProps,
|
|
36
|
+
) {
|
|
37
|
+
this._id = name;
|
|
38
|
+
// Read at synth, so a stage the declaration has no URL for fails the
|
|
39
|
+
// deploy — with the stage named — rather than the first request.
|
|
40
|
+
this.url = externalApiUrl({ id: name, url: props.url }, stack.stage);
|
|
41
|
+
this.credentials = new sst.Secret(`${name}Credentials`, props.placeholder);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
provides(): Record<string, $util.Input<string>> {
|
|
45
|
+
return { url: this.url, credentials: this.credentials.value };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
getSSTLink() {
|
|
49
|
+
return { properties: { ...this.provides() } };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ExternalApiProps {
|
|
54
|
+
/** The declaration's `url` — one for every stage, or one per stage name. */
|
|
55
|
+
url: string | Readonly<Record<string, string>>;
|
|
56
|
+
/** A value for the credentials secret until one is set, for a preview stage. */
|
|
57
|
+
placeholder?: string;
|
|
58
|
+
}
|
package/src/sst/aws/Function.ts
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
import type { EnvValidator, ValidationResult } from '@geekmidas/envkit/sst';
|
|
2
|
-
import {
|
|
3
|
-
type FunctionInfo,
|
|
4
|
-
flattenManifestField,
|
|
5
|
-
type ManifestField,
|
|
6
|
-
} from '@geekmidas/manifest';
|
|
7
2
|
import { type GkmLinkable, ResourceType } from '../Linkable';
|
|
8
3
|
import { LinkedEnvironment } from '../LinkedEnvironment';
|
|
9
4
|
import type { StackType } from '../Stack';
|
|
@@ -66,9 +61,10 @@ export class Function<
|
|
|
66
61
|
super(id, {
|
|
67
62
|
...fnArgs,
|
|
68
63
|
environment: mergedEnvironment,
|
|
69
|
-
//
|
|
70
|
-
//
|
|
71
|
-
link:
|
|
64
|
+
// Resolved from `links` when `envVars` names what it needs; otherwise
|
|
65
|
+
// the `link` it was given — the edges a manifest declared.
|
|
66
|
+
link:
|
|
67
|
+
envVars.length > 0 ? linked.resolveLink(envVars) : (fnArgs.link ?? []),
|
|
72
68
|
runtime: runtime ?? 'nodejs24.x',
|
|
73
69
|
logging: logging ?? { format: 'json' },
|
|
74
70
|
});
|
|
@@ -78,40 +74,15 @@ export class Function<
|
|
|
78
74
|
this.envVars = envVars;
|
|
79
75
|
}
|
|
80
76
|
|
|
77
|
+
/** Its address, for whatever declared an edge to it. */
|
|
78
|
+
provides(): Record<string, $util.Input<string>> {
|
|
79
|
+
return { url: this.url };
|
|
80
|
+
}
|
|
81
|
+
|
|
81
82
|
/** Re-runs validation and returns the result (does not throw). */
|
|
82
83
|
validate(): ValidationResult {
|
|
83
84
|
return this.validator.validate(this.envVars);
|
|
84
85
|
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Build one `Function` per entry in a `gkm build` manifest's `functions`
|
|
88
|
-
* field (flat or partitioned). Shared `props` (e.g. `links`) apply to every
|
|
89
|
-
* function.
|
|
90
|
-
*
|
|
91
|
-
* ```ts
|
|
92
|
-
* Function.fromManifest(stack, manifest.functions, { links: [db] });
|
|
93
|
-
* ```
|
|
94
|
-
*/
|
|
95
|
-
static fromManifest<
|
|
96
|
-
TStage extends string = string,
|
|
97
|
-
TDomain extends string = string,
|
|
98
|
-
>(
|
|
99
|
-
stack: StackType<TStage, TDomain>,
|
|
100
|
-
functions: ManifestField<FunctionInfo>,
|
|
101
|
-
props: Omit<FunctionProps, 'handler'> = {},
|
|
102
|
-
): Function<TStage, TDomain>[] {
|
|
103
|
-
return flattenManifestField(functions).map(
|
|
104
|
-
(fn) =>
|
|
105
|
-
new Function(stack, fn.name, {
|
|
106
|
-
...props,
|
|
107
|
-
name: stack.logicalPrefixedName(fn.name),
|
|
108
|
-
handler: fn.handler,
|
|
109
|
-
envVars: fn.environment,
|
|
110
|
-
timeout: fn.timeout ? `${fn.timeout} seconds` : undefined,
|
|
111
|
-
memory: fn.memorySize ? `${fn.memorySize} MB` : undefined,
|
|
112
|
-
}),
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
86
|
}
|
|
116
87
|
|
|
117
88
|
export interface FunctionProps extends sst.aws.FunctionArgs {
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// The codec alone — see the note in `Queue.ts`.
|
|
2
|
+
import * as snsUrl from '@geekmidas/events/sns/url';
|
|
3
|
+
import { provideKey } from '@geekmidas/manifest';
|
|
4
|
+
import { type GkmLinkable, ResourceType } from '../Linkable';
|
|
5
|
+
import { regionOfArn } from '../naming';
|
|
6
|
+
import type { StackType } from '../Stack';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* `Inbox` — mail received, as SST links it. It provisions nothing.
|
|
10
|
+
*
|
|
11
|
+
* What receives the mail is the inbox's provider's, created by `gkm deploy`
|
|
12
|
+
* before SST runs, as it is on a compose stage: the SES receipt rule, the
|
|
13
|
+
* bucket the raw messages are stored in, the topic each is announced on, the
|
|
14
|
+
* domain's MX and DKIM records. The deploy hands SST what that made, read
|
|
15
|
+
* from the stage's secrets by the rule every target reads it by, as env:
|
|
16
|
+
*
|
|
17
|
+
* - `<ID>_URL` — where the raw messages are read, with the provider's key;
|
|
18
|
+
* - `<ID>_DOMAIN` — the stage's domain, which mail is addressed to;
|
|
19
|
+
* - `<TOPIC>_PUBLISHER_CONNECTION_STRING` — the topic, whose ARN its
|
|
20
|
+
* subscribers are subscribed to.
|
|
21
|
+
*
|
|
22
|
+
* Each can be passed instead (`fromManifest(…, { Requests: { url, domain,
|
|
23
|
+
* topic } })`), which is what a test does.
|
|
24
|
+
*/
|
|
25
|
+
export class Inbox<
|
|
26
|
+
TStage extends string = string,
|
|
27
|
+
TDomain extends string = string,
|
|
28
|
+
> implements GkmLinkable
|
|
29
|
+
{
|
|
30
|
+
readonly _id: string;
|
|
31
|
+
|
|
32
|
+
/** The ARN of the topic the provider publishes each receipt on. */
|
|
33
|
+
readonly topicArn: string;
|
|
34
|
+
|
|
35
|
+
private readonly url: string;
|
|
36
|
+
|
|
37
|
+
private readonly domain: string;
|
|
38
|
+
|
|
39
|
+
get _type() {
|
|
40
|
+
return ResourceType.Inbox;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
constructor(
|
|
44
|
+
_stack: StackType<TStage, TDomain>,
|
|
45
|
+
name: string,
|
|
46
|
+
props: InboxProps,
|
|
47
|
+
) {
|
|
48
|
+
this._id = name;
|
|
49
|
+
const env = props.env ?? process.env;
|
|
50
|
+
const read = (given: string | undefined, key: string): string => {
|
|
51
|
+
const value = given ?? env[key];
|
|
52
|
+
if (!value) throw new InboxNotProvisioned(name, key);
|
|
53
|
+
return value;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
this.url = read(props.url, provideKey(name, 'url'));
|
|
57
|
+
this.domain = read(props.domain, provideKey(name, 'domain'));
|
|
58
|
+
this.topicArn = snsUrl.parse(
|
|
59
|
+
read(props.topic, provideKey(props.topicId, 'publisherConnectionString')),
|
|
60
|
+
).topicArn;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
provides(): Record<string, $util.Input<string>> {
|
|
64
|
+
return { url: this.url, domain: this.domain };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
getSSTLink() {
|
|
68
|
+
return { properties: { ...this.provides() } };
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface InboxProps {
|
|
73
|
+
/** The id of the topic the inbox declared — the provider's, by name. */
|
|
74
|
+
topicId: string;
|
|
75
|
+
/** Where the raw messages are read. `<ID>_URL` from the deploy otherwise. */
|
|
76
|
+
url?: string;
|
|
77
|
+
/** The domain mail is addressed to. `<ID>_DOMAIN` from the deploy otherwise. */
|
|
78
|
+
domain?: string;
|
|
79
|
+
/** The topic's `sns://` string. `<TOPIC>_PUBLISHER_CONNECTION_STRING` otherwise. */
|
|
80
|
+
topic?: string;
|
|
81
|
+
/** Where the deploy's values are read; `process.env` by default. */
|
|
82
|
+
env?: Readonly<Record<string, string | undefined>>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The topic an inbox declared, as its provider made it: referenced, never
|
|
87
|
+
* created, so SST cannot make a second topic SES never publishes to.
|
|
88
|
+
*
|
|
89
|
+
* Nothing depends on it — only SES publishes to it — so what it provides is
|
|
90
|
+
* there for the declaration's contract, not for any producer.
|
|
91
|
+
*/
|
|
92
|
+
export class InboxTopic implements GkmLinkable {
|
|
93
|
+
readonly _id: string;
|
|
94
|
+
|
|
95
|
+
get _type() {
|
|
96
|
+
return ResourceType.SnsTopic;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
constructor(
|
|
100
|
+
name: string,
|
|
101
|
+
private readonly inbox: Inbox,
|
|
102
|
+
) {
|
|
103
|
+
this._id = name;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get arn(): string {
|
|
107
|
+
return this.inbox.topicArn;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
provides(): Record<string, $util.Input<string>> {
|
|
111
|
+
return {
|
|
112
|
+
publisherConnectionString: snsUrl.build({
|
|
113
|
+
topicArn: this.arn,
|
|
114
|
+
region: regionOfArn(this.arn),
|
|
115
|
+
}),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
getSSTLink() {
|
|
120
|
+
return { properties: { arn: this.arn, ...this.provides() } };
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** An inbox's value is neither passed nor in the deploy's env. */
|
|
125
|
+
export class InboxNotProvisioned extends Error {
|
|
126
|
+
constructor(
|
|
127
|
+
readonly id: string,
|
|
128
|
+
readonly key: string,
|
|
129
|
+
) {
|
|
130
|
+
super(
|
|
131
|
+
`'${id}' receives mail, and ${key} is not set. Its provider makes it: ` +
|
|
132
|
+
`deploy with \`gkm deploy --target sst\`, which runs the stage's ` +
|
|
133
|
+
`inbox provider (deploy.inbox.<stage> = { provider: 'ses' }) and ` +
|
|
134
|
+
`hands SST what it made — or pass it to fromManifest.`,
|
|
135
|
+
);
|
|
136
|
+
this.name = 'InboxNotProvisioned';
|
|
137
|
+
}
|
|
138
|
+
}
|
package/src/sst/aws/Queue.ts
CHANGED
|
@@ -11,7 +11,8 @@ import type { StackType } from '../Stack';
|
|
|
11
11
|
* work queue. Link it to a producer and the runtime resolves `<NAME>_URL`,
|
|
12
12
|
* `<NAME>_ARN`, and a `<NAME>_PUBLISHER_CONNECTION_STRING` (`sqs://?queueUrl=…`)
|
|
13
13
|
* that `@geekmidas/events`'s `Publisher.fromConnectionString` consumes. Its
|
|
14
|
-
*
|
|
14
|
+
* one consumer — the handler the queue was declared with — is subscribed by
|
|
15
|
+
* {@link Queue.consume}.
|
|
15
16
|
*
|
|
16
17
|
* SST's native `Queue` link exposes only `url`, so `getSSTLink` is overridden to
|
|
17
18
|
* also expose `arn` (what the resolver needs). `QueueProps` extends
|
|
@@ -73,6 +74,26 @@ export class Queue<
|
|
|
73
74
|
};
|
|
74
75
|
}
|
|
75
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Subscribe the queue's one consumer: a Lambda running `handler`, linked to
|
|
79
|
+
* what the consumer depends on and to this queue itself.
|
|
80
|
+
*
|
|
81
|
+
* A queue has exactly one consumer, so this is called once per queue — by
|
|
82
|
+
* `fromManifest`, from the handler the build wrote for it.
|
|
83
|
+
*/
|
|
84
|
+
consume(consumer: QueueConsumer) {
|
|
85
|
+
return this.subscribe(
|
|
86
|
+
{
|
|
87
|
+
handler: consumer.handler,
|
|
88
|
+
link: [this, ...(consumer.link ?? [])],
|
|
89
|
+
runtime: 'nodejs24.x',
|
|
90
|
+
...(consumer.vpc ? { vpc: consumer.vpc } : {}),
|
|
91
|
+
...(consumer.timeout ? { timeout: consumer.timeout } : {}),
|
|
92
|
+
},
|
|
93
|
+
consumer.batchSize ? { batch: { size: consumer.batchSize } } : {},
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
76
97
|
override getSSTLink() {
|
|
77
98
|
const link = super.getSSTLink();
|
|
78
99
|
return {
|
|
@@ -82,6 +103,18 @@ export class Queue<
|
|
|
82
103
|
}
|
|
83
104
|
}
|
|
84
105
|
|
|
106
|
+
/** The queue's one consumer, as {@link Queue.consume} subscribes it. */
|
|
107
|
+
export interface QueueConsumer {
|
|
108
|
+
/** The built handler — `apps/api/.gkm/aws/queues/emails.handler`. */
|
|
109
|
+
handler: string;
|
|
110
|
+
/** What the consumer depends on; the queue itself is always linked. */
|
|
111
|
+
link?: unknown[];
|
|
112
|
+
/** The database's network, when the consumer reaches one. */
|
|
113
|
+
vpc?: sst.aws.Vpc;
|
|
114
|
+
timeout?: `${number} seconds` | `${number} minutes`;
|
|
115
|
+
batchSize?: number;
|
|
116
|
+
}
|
|
117
|
+
|
|
85
118
|
export interface QueueProps extends sst.aws.QueueArgs {
|
|
86
119
|
/**
|
|
87
120
|
* The physical queue name, where the auto-generated one will not do.
|
|
@@ -4,20 +4,10 @@ import type { StackType } from '../Stack';
|
|
|
4
4
|
/**
|
|
5
5
|
* `RestApiSurface` — an HTTP API, and the infra half of the `rest-api` kind.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* So what this provisions is the surface and nothing on it. An API Gateway with
|
|
13
|
-
* no routes 404s everything, which is the honest state of things — and it is
|
|
14
|
-
* still worth provisioning, because the *address* is what everything downstream
|
|
15
|
-
* needs: a site inlines it as `VITE_API_URL`, an auth server puts it on its
|
|
16
|
-
* trusted-origin list, and the cookie domain derives from it. Those are blocked
|
|
17
|
-
* on the API existing, not on it answering.
|
|
18
|
-
*
|
|
19
|
-
* The two collapse into one component when the endpoint merge lands and the
|
|
20
|
-
* surface node carries its routes.
|
|
7
|
+
* Provisioned before its routes, because its *address* is what everything
|
|
8
|
+
* downstream needs: a site inlines it as `VITE_API_URL`, an auth server puts it
|
|
9
|
+
* on its trusted-origin list, and the cookie domain derives from it. The routes
|
|
10
|
+
* are mounted once everything they link to exists — see {@link mount}.
|
|
21
11
|
*/
|
|
22
12
|
|
|
23
13
|
export interface RestApiSurfaceProps extends sst.aws.ApiGatewayV2Args {
|
|
@@ -82,6 +72,25 @@ export class RestApiSurface<
|
|
|
82
72
|
};
|
|
83
73
|
}
|
|
84
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Mount one endpoint: a Lambda running the handler the build wrote for it,
|
|
77
|
+
* linked to exactly what that endpoint depends on.
|
|
78
|
+
*/
|
|
79
|
+
mount(endpoint: SurfaceRoute) {
|
|
80
|
+
return this.route(
|
|
81
|
+
`${endpoint.method} ${endpoint.path}`,
|
|
82
|
+
{
|
|
83
|
+
handler: endpoint.handler,
|
|
84
|
+
link: endpoint.link ?? [],
|
|
85
|
+
runtime: 'nodejs24.x',
|
|
86
|
+
...(endpoint.vpc ? { vpc: endpoint.vpc } : {}),
|
|
87
|
+
},
|
|
88
|
+
// `iam` is the one authorizer the gateway enforces itself; every other
|
|
89
|
+
// is the endpoint's own, checked in the handler.
|
|
90
|
+
endpoint.authorizer === 'iam' ? { auth: { iam: true } } : {},
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
85
94
|
override getSSTLink() {
|
|
86
95
|
const link = super.getSSTLink();
|
|
87
96
|
return {
|
|
@@ -90,3 +99,16 @@ export class RestApiSurface<
|
|
|
90
99
|
};
|
|
91
100
|
}
|
|
92
101
|
}
|
|
102
|
+
|
|
103
|
+
/** One endpoint, as {@link RestApiSurface.mount} mounts it. */
|
|
104
|
+
export interface SurfaceRoute {
|
|
105
|
+
method: string;
|
|
106
|
+
path: string;
|
|
107
|
+
/** The built handler — `apps/api/.gkm/aws/routes/getUser.handler`. */
|
|
108
|
+
handler: string;
|
|
109
|
+
/** What the endpoint depends on, and nothing else. */
|
|
110
|
+
link?: unknown[];
|
|
111
|
+
/** The database's network, when the endpoint reaches one. */
|
|
112
|
+
vpc?: sst.aws.Vpc;
|
|
113
|
+
authorizer?: string;
|
|
114
|
+
}
|
package/src/sst/aws/Secret.ts
CHANGED
|
@@ -15,8 +15,10 @@ import type { StackType } from '../Stack';
|
|
|
15
15
|
* signing key, the thing that stores it, and the thing that reads it are three
|
|
16
16
|
* different systems deployed, and one derived string locally.
|
|
17
17
|
*
|
|
18
|
-
* Usable on its own. `Stack.fromManifest` translates a manifest into these,
|
|
19
|
-
*
|
|
18
|
+
* Usable on its own. `Stack.fromManifest` translates a manifest into these, and
|
|
19
|
+
* hands each a generated placeholder, so a manifest's secret is generated on
|
|
20
|
+
* its first deploy rather than set by hand; the component on its own takes
|
|
21
|
+
* whatever placeholder it is given, so it works in a hand-written
|
|
20
22
|
* `sst.config.ts` with no manifest anywhere.
|
|
21
23
|
*/
|
|
22
24
|
export class Secret<
|
|
@@ -72,5 +74,5 @@ export interface SecretProps {
|
|
|
72
74
|
* letting the deploy fail — is the correct handling for a real secret. It
|
|
73
75
|
* exists for the values that are secrets by habit rather than by need.
|
|
74
76
|
*/
|
|
75
|
-
placeholder?: string
|
|
77
|
+
placeholder?: $util.Input<string>;
|
|
76
78
|
}
|
|
@@ -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
|
/**
|
|
@@ -28,6 +28,13 @@ export interface BootstrapTenant {
|
|
|
28
28
|
runtime: string;
|
|
29
29
|
owner: string;
|
|
30
30
|
reader?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The database's own roles rather than a tenant's: its owner may create in
|
|
33
|
+
* the database, for trusted extensions.
|
|
34
|
+
*/
|
|
35
|
+
ownsDatabase?: boolean;
|
|
36
|
+
/** Extensions the database declared, created as the master. */
|
|
37
|
+
extensions?: readonly string[];
|
|
31
38
|
passwords: { runtime: string; owner: string; reader?: string };
|
|
32
39
|
}
|
|
33
40
|
|
|
@@ -75,7 +82,11 @@ export async function handler(event: BootstrapEvent): Promise<BootstrapResult> {
|
|
|
75
82
|
|
|
76
83
|
try {
|
|
77
84
|
for (const tenant of event.tenants) {
|
|
78
|
-
|
|
85
|
+
const spec = {
|
|
86
|
+
...tenant,
|
|
87
|
+
...(tenant.ownsDatabase ? { database: event.master.database } : {}),
|
|
88
|
+
};
|
|
89
|
+
for (const statement of roleStatements(spec)) {
|
|
79
90
|
if (statement.exists) {
|
|
80
91
|
const { rows } = await client.query(
|
|
81
92
|
statement.exists.sql,
|
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
|
+
}
|