@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.
- package/README.md +26 -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__/websocket.spec.ts +207 -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/WebSocketApiSurface.ts +160 -0
- package/src/sst/aws/bootstrap/handler.ts +12 -1
- package/src/sst/errors.ts +15 -0
- package/src/sst/fromManifest.ts +536 -49
- package/src/sst/index.ts +20 -1
- package/src/sst/overrides.ts +149 -0
- package/src/sst/__type-tests__/manifest.type-test.ts +0 -77
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { apiGatewayWebSocketUrl } from '@geekmidas/manifest';
|
|
2
|
+
import { type GkmLinkable, ResourceType } from '../Linkable';
|
|
3
|
+
import type { StackType } from '../Stack';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `WebSocketApiSurface` — an API Gateway WebSocket API, and the infra half of
|
|
7
|
+
* the `websocket-api` kind on AWS.
|
|
8
|
+
*
|
|
9
|
+
* The same surface a compose stage runs as a Node process: there the process
|
|
10
|
+
* holds the sockets and Redis carries a send to whichever replica holds one;
|
|
11
|
+
* here API Gateway holds them, and each route is a Lambda the build wrote
|
|
12
|
+
* (`AmazonApiGatewayWebSocket`). What the gateway does not keep — a
|
|
13
|
+
* connection's `data`, and the list a broadcast fans out to — goes in a
|
|
14
|
+
* DynamoDB table this component makes beside the API, so a surface declares
|
|
15
|
+
* nothing more to deploy here.
|
|
16
|
+
*
|
|
17
|
+
* Linked, it hands a function its `<ID>_URL` (`wss://`), its
|
|
18
|
+
* `<ID>_TRUSTED_ORIGINS`, and an `apigateway://` publisher string naming the
|
|
19
|
+
* management endpoint and the table — with `execute-api:ManageConnections`
|
|
20
|
+
* and the table's access, so `services.<surface>` works from any Lambda that
|
|
21
|
+
* declared the edge, and from the surface's own routes.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export interface WebSocketApiSurfaceProps
|
|
25
|
+
extends sst.aws.ApiGatewayWebSocketArgs {
|
|
26
|
+
/** Who may open it, resolved once everything is provisioned. */
|
|
27
|
+
callers?: Promise<{ trustedOrigins: string; cookieDomain?: string }>;
|
|
28
|
+
/**
|
|
29
|
+
* The key a frame names its action under — the surface's
|
|
30
|
+
* `frame.discriminator`. Absent, `action`.
|
|
31
|
+
*/
|
|
32
|
+
discriminator?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class WebSocketApiSurface<
|
|
36
|
+
TStage extends string = string,
|
|
37
|
+
TDomain extends string = string,
|
|
38
|
+
>
|
|
39
|
+
extends sst.aws.ApiGatewayWebSocket
|
|
40
|
+
implements GkmLinkable
|
|
41
|
+
{
|
|
42
|
+
readonly _id!: string;
|
|
43
|
+
|
|
44
|
+
/** Where its open connections are kept. */
|
|
45
|
+
readonly connections: sst.aws.Dynamo;
|
|
46
|
+
|
|
47
|
+
get _type() {
|
|
48
|
+
return ResourceType.WebSocketApi;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private readonly callers: Promise<{ trustedOrigins: string }>;
|
|
52
|
+
|
|
53
|
+
constructor(
|
|
54
|
+
_stack: StackType<TStage, TDomain>,
|
|
55
|
+
name: string,
|
|
56
|
+
props: WebSocketApiSurfaceProps = {},
|
|
57
|
+
) {
|
|
58
|
+
const { callers, discriminator, ...args } = props;
|
|
59
|
+
const own = args.transform?.api;
|
|
60
|
+
// API Gateway routes on the key the surface's frames name their action
|
|
61
|
+
// under — the route a frame reaches is the one `message()` would
|
|
62
|
+
// dispatch it to. The caller's own transform still runs after it.
|
|
63
|
+
const api = ((
|
|
64
|
+
apiArgs: { routeSelectionExpression?: unknown },
|
|
65
|
+
...rest: unknown[]
|
|
66
|
+
) => {
|
|
67
|
+
apiArgs.routeSelectionExpression =
|
|
68
|
+
routeSelectionExpression(discriminator);
|
|
69
|
+
if (typeof own === 'function') {
|
|
70
|
+
(own as (...a: unknown[]) => void)(apiArgs, ...rest);
|
|
71
|
+
} else if (own) {
|
|
72
|
+
Object.assign(apiArgs, own);
|
|
73
|
+
}
|
|
74
|
+
}) as NonNullable<
|
|
75
|
+
NonNullable<sst.aws.ApiGatewayWebSocketArgs['transform']>['api']
|
|
76
|
+
>;
|
|
77
|
+
|
|
78
|
+
super(name, { ...args, transform: { ...args.transform, api } });
|
|
79
|
+
this._id = name;
|
|
80
|
+
this.callers = callers ?? Promise.resolve({ trustedOrigins: '' });
|
|
81
|
+
this.connections = new sst.aws.Dynamo(`${name}Connections`, {
|
|
82
|
+
fields: { connectionId: 'string' },
|
|
83
|
+
primaryIndex: { hashKey: 'connectionId' },
|
|
84
|
+
// API Gateway closes a connection after two hours; a row whose
|
|
85
|
+
// `$disconnect` never ran expires after that.
|
|
86
|
+
ttl: 'expiresAt',
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
provides(): Record<string, $util.Input<string>> {
|
|
91
|
+
return {
|
|
92
|
+
url: this.url,
|
|
93
|
+
// Deferred for the reason a REST surface's are: a caller's address
|
|
94
|
+
// is known only once it is provisioned. See `RestApiSurface`.
|
|
95
|
+
trustedOrigins: $util.output(this.callers.then((c) => c.trustedOrigins)),
|
|
96
|
+
publisherConnectionString: $util
|
|
97
|
+
.all([this.managementEndpoint, this.connections.name])
|
|
98
|
+
.apply(([endpoint, table]) =>
|
|
99
|
+
apiGatewayWebSocketUrl({
|
|
100
|
+
region:
|
|
101
|
+
regionOfEndpoint(endpoint as string) ??
|
|
102
|
+
$app.providers?.aws?.region ??
|
|
103
|
+
'us-east-1',
|
|
104
|
+
endpoint: endpoint as string,
|
|
105
|
+
table: table as string,
|
|
106
|
+
}),
|
|
107
|
+
),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Mount one route: a Lambda running the handler the build wrote for it,
|
|
113
|
+
* linked to what that route depends on and to the surface itself — its
|
|
114
|
+
* table and its management API, which every route reaches.
|
|
115
|
+
*/
|
|
116
|
+
mount(route: WebSocketSurfaceRoute) {
|
|
117
|
+
return this.route(route.action, {
|
|
118
|
+
handler: route.handler,
|
|
119
|
+
link: [this, ...(route.link ?? []).filter((link) => link !== this)],
|
|
120
|
+
runtime: 'nodejs24.x',
|
|
121
|
+
...(route.vpc ? { vpc: route.vpc } : {}),
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
override getSSTLink() {
|
|
126
|
+
const own = super.getSSTLink();
|
|
127
|
+
const table = this.connections.getSSTLink();
|
|
128
|
+
return {
|
|
129
|
+
...own,
|
|
130
|
+
properties: { ...own.properties, ...this.provides() },
|
|
131
|
+
include: [...(own.include ?? []), ...(table.include ?? [])],
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** One route, as {@link WebSocketApiSurface.mount} mounts it. */
|
|
137
|
+
export interface WebSocketSurfaceRoute {
|
|
138
|
+
/** The route key: an action, `$connect`, `$disconnect` or `$default`. */
|
|
139
|
+
action: string;
|
|
140
|
+
/** The built handler — `apps/realtime/.gkm/aws/websocket/connect.handler`. */
|
|
141
|
+
handler: string;
|
|
142
|
+
/** What the route depends on; the surface itself is added. */
|
|
143
|
+
link?: unknown[];
|
|
144
|
+
/** The database's network, when the route reaches one. */
|
|
145
|
+
vpc?: sst.aws.Vpc;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** `$request.body.<key>`, the key a surface's frames name their action under. */
|
|
149
|
+
export function routeSelectionExpression(discriminator?: string): string {
|
|
150
|
+
return `$request.body.${discriminator ?? 'action'}`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The region in a management endpoint's host: `<api>.execute-api.<region>.…`. */
|
|
154
|
+
export function regionOfEndpoint(endpoint: string): string | undefined {
|
|
155
|
+
try {
|
|
156
|
+
return /\.execute-api\.([a-z0-9-]+)\./.exec(new URL(endpoint).host)?.[1];
|
|
157
|
+
} catch {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -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
|
+
}
|