@geekmidas/cloud 10.0.0-alpha.155 → 10.0.0-alpha.157
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 +12 -0
- package/package.json +7 -7
- package/src/sst/__tests__/websocket.spec.ts +207 -0
- package/src/sst/aws/WebSocketApiSurface.ts +160 -0
- package/src/sst/fromManifest.ts +70 -1
- package/src/sst/index.ts +7 -0
- package/src/sst/overrides.ts +15 -13
package/README.md
CHANGED
|
@@ -132,3 +132,15 @@ import { buildResourceEnv } from '@geekmidas/cloud';
|
|
|
132
132
|
|
|
133
133
|
`buildResourceEnv` turns a record of SST `Resource` links into flat environment
|
|
134
134
|
variables at runtime, using the shared `ResourceType` vocabulary.
|
|
135
|
+
|
|
136
|
+
## Optional peers
|
|
137
|
+
|
|
138
|
+
<!-- optional-peers:start -->
|
|
139
|
+
|
|
140
|
+
<!-- Generated by `node scripts/check-optional-peers.mjs --write-docs`; CI fails when it drifts. -->
|
|
141
|
+
|
|
142
|
+
No entry imports an optional peer when it loads. `@geekmidas/db`, `@geekmidas/events`, `@geekmidas/manifest`, `@geekmidas/storage`, `@pulumi/aws`, `@pulumi/pulumi`, `@pulumi/random`, `pg` are loaded only when a feature that uses one runs (an `import()` inside a `try`, whose error names the package to install), or named only in types.
|
|
143
|
+
|
|
144
|
+
`@geekmidas/cloud/sst`, `@geekmidas/cloud/dokploy` ship as TypeScript source for SST's loader, which provides the `@pulumi/*` packages, so they are not covered by the statement above.
|
|
145
|
+
|
|
146
|
+
<!-- optional-peers:end -->
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/cloud",
|
|
3
|
-
"version": "10.0.0-alpha.
|
|
3
|
+
"version": "10.0.0-alpha.157",
|
|
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.
|
|
45
|
-
"@geekmidas/manifest": "10.0.0-alpha.
|
|
44
|
+
"@geekmidas/envkit": "10.0.0-alpha.157",
|
|
45
|
+
"@geekmidas/manifest": "10.0.0-alpha.157"
|
|
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": "
|
|
62
|
-
"@geekmidas/storage": "
|
|
63
|
-
"@geekmidas/events": "
|
|
64
|
-
"@geekmidas/db": "
|
|
61
|
+
"@geekmidas/manifest": ">=10.0.0-alpha.157 <10.0.0",
|
|
62
|
+
"@geekmidas/storage": ">=10.0.0-alpha.157 <10.0.0",
|
|
63
|
+
"@geekmidas/events": ">=10.0.0-alpha.157 <10.0.0",
|
|
64
|
+
"@geekmidas/db": ">=10.0.0-alpha.157 <10.0.0"
|
|
65
65
|
},
|
|
66
66
|
"peerDependenciesMeta": {
|
|
67
67
|
"@geekmidas/manifest": {
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { resolveEnvKeys } from '@geekmidas/envkit/sst';
|
|
2
|
+
import {
|
|
3
|
+
type ConstructManifest,
|
|
4
|
+
parseApiGatewayWebSocketUrl,
|
|
5
|
+
providedKeyFor,
|
|
6
|
+
} from '@geekmidas/manifest';
|
|
7
|
+
import { describe, expect, it } from 'vitest';
|
|
8
|
+
import { RestApiSurface } from '../aws/RestApiSurface';
|
|
9
|
+
import {
|
|
10
|
+
regionOfEndpoint,
|
|
11
|
+
routeSelectionExpression,
|
|
12
|
+
type WebSocketApiSurface,
|
|
13
|
+
} from '../aws/WebSocketApiSurface';
|
|
14
|
+
import {
|
|
15
|
+
assertProvides,
|
|
16
|
+
describeRoutes,
|
|
17
|
+
mountWebSocketRoutes,
|
|
18
|
+
type ProvisionedManifest,
|
|
19
|
+
provisionerFor,
|
|
20
|
+
} from '../fromManifest';
|
|
21
|
+
|
|
22
|
+
const stack = {} as never;
|
|
23
|
+
|
|
24
|
+
const SURFACE_KEYS = [
|
|
25
|
+
'REALTIME_URL',
|
|
26
|
+
'REALTIME_TRUSTED_ORIGINS',
|
|
27
|
+
'REALTIME_PUBLISHER_CONNECTION_STRING',
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
const manifest = {
|
|
31
|
+
Realtime: {
|
|
32
|
+
kind: 'websocket-api',
|
|
33
|
+
id: 'Realtime',
|
|
34
|
+
path: 'apps/realtime',
|
|
35
|
+
discriminator: 'type',
|
|
36
|
+
auth: { target: 'Auth', kind: 'rest-api' },
|
|
37
|
+
routes: [
|
|
38
|
+
{
|
|
39
|
+
id: 'Realtime$connect',
|
|
40
|
+
action: '$connect',
|
|
41
|
+
handler: 'apps/realtime/.gkm/aws/websocket/connect.handler',
|
|
42
|
+
dependencies: [],
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
id: 'Realtimechat.send',
|
|
46
|
+
action: 'chat.send',
|
|
47
|
+
handler: 'apps/realtime/.gkm/aws/websocket/route-chat_send.handler',
|
|
48
|
+
dependencies: [{ target: 'Api', kind: 'rest-api' }],
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
provides: SURFACE_KEYS,
|
|
52
|
+
},
|
|
53
|
+
Api: {
|
|
54
|
+
kind: 'rest-api',
|
|
55
|
+
id: 'Api',
|
|
56
|
+
path: 'apps/api',
|
|
57
|
+
endpoints: [],
|
|
58
|
+
provides: ['API_URL', 'API_TRUSTED_ORIGINS', 'API_COOKIE_DOMAIN'],
|
|
59
|
+
},
|
|
60
|
+
Auth: {
|
|
61
|
+
kind: 'rest-api',
|
|
62
|
+
id: 'Auth',
|
|
63
|
+
path: 'apps/auth',
|
|
64
|
+
endpoints: [],
|
|
65
|
+
provides: ['AUTH_URL', 'AUTH_TRUSTED_ORIGINS', 'AUTH_COOKIE_DOMAIN'],
|
|
66
|
+
},
|
|
67
|
+
} as const satisfies ConstructManifest;
|
|
68
|
+
|
|
69
|
+
function provision(): ProvisionedManifest {
|
|
70
|
+
const context = { manifest, provisioned: {}, bootstraps: new Map() };
|
|
71
|
+
return {
|
|
72
|
+
Realtime: provisionerFor('websocket-api')(
|
|
73
|
+
stack,
|
|
74
|
+
manifest.Realtime,
|
|
75
|
+
{},
|
|
76
|
+
context,
|
|
77
|
+
),
|
|
78
|
+
Api: new RestApiSurface(stack, 'Api'),
|
|
79
|
+
Auth: new RestApiSurface(stack, 'Auth'),
|
|
80
|
+
} as ProvisionedManifest;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
describe('WebSocketApiSurface', () => {
|
|
84
|
+
it('provides the keys a websocket-api declares', () => {
|
|
85
|
+
const surface = provision().Realtime!;
|
|
86
|
+
|
|
87
|
+
expect(() =>
|
|
88
|
+
assertProvides(
|
|
89
|
+
'Realtime',
|
|
90
|
+
manifest.Realtime.provides,
|
|
91
|
+
Object.keys(surface.provides()).map((role) =>
|
|
92
|
+
providedKeyFor('Realtime', 'websocket-api', role),
|
|
93
|
+
),
|
|
94
|
+
),
|
|
95
|
+
).not.toThrow();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('resolves to the same keys a linked function reads', () => {
|
|
99
|
+
const surface = provision().Realtime!;
|
|
100
|
+
|
|
101
|
+
expect(
|
|
102
|
+
[...resolveEnvKeys({ Realtime: { type: surface._type } })].sort(),
|
|
103
|
+
).toEqual([...SURFACE_KEYS].sort());
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('hands its publisher the management API and its own connection table', () => {
|
|
107
|
+
const surface = provision().Realtime as unknown as WebSocketApiSurface;
|
|
108
|
+
|
|
109
|
+
expect(
|
|
110
|
+
parseApiGatewayWebSocketUrl(
|
|
111
|
+
surface.provides().publisherConnectionString as string,
|
|
112
|
+
),
|
|
113
|
+
).toEqual({
|
|
114
|
+
region: 'stub-region',
|
|
115
|
+
endpoint:
|
|
116
|
+
'https://Realtime.execute-api.stub-region.amazonaws.com/$default',
|
|
117
|
+
table: 'RealtimeConnections',
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('grants a linked function the management API and the table, with the keys', () => {
|
|
122
|
+
const surface = provision().Realtime as unknown as WebSocketApiSurface;
|
|
123
|
+
const link = surface.getSSTLink();
|
|
124
|
+
|
|
125
|
+
expect(link.include).toEqual([
|
|
126
|
+
{ permission: { actions: ['execute-api:ManageConnections'] } },
|
|
127
|
+
{ permission: { actions: ['dynamodb:*'] } },
|
|
128
|
+
]);
|
|
129
|
+
expect(link.properties).toMatchObject({
|
|
130
|
+
url: 'wss://Realtime.execute-api.stub-region.amazonaws.com/$default',
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('routes on the key its frames name their action under', () => {
|
|
135
|
+
const surface = provision().Realtime as unknown as {
|
|
136
|
+
args: { transform: { api: (api: object) => void } };
|
|
137
|
+
};
|
|
138
|
+
const api: { routeSelectionExpression?: string } = {};
|
|
139
|
+
|
|
140
|
+
surface.args.transform.api(api);
|
|
141
|
+
|
|
142
|
+
expect(api.routeSelectionExpression).toBe('$request.body.type');
|
|
143
|
+
expect(routeSelectionExpression()).toBe('$request.body.action');
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe('mountWebSocketRoutes', () => {
|
|
148
|
+
const mounted = () => {
|
|
149
|
+
const provisioned = provision();
|
|
150
|
+
mountWebSocketRoutes(manifest, provisioned);
|
|
151
|
+
const surface = provisioned.Realtime as unknown as {
|
|
152
|
+
routes: { key: string; handler: { handler: string; link: unknown[] } }[];
|
|
153
|
+
};
|
|
154
|
+
return { provisioned, routes: surface.routes };
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
it('mounts every route on its key, running the handler the build wrote', () => {
|
|
158
|
+
expect(
|
|
159
|
+
mounted().routes.map(({ key, handler }) => [key, handler.handler]),
|
|
160
|
+
).toEqual([
|
|
161
|
+
['$connect', 'apps/realtime/.gkm/aws/websocket/connect.handler'],
|
|
162
|
+
['chat.send', 'apps/realtime/.gkm/aws/websocket/route-chat_send.handler'],
|
|
163
|
+
]);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('links a route to its surface and what it depends on, and nothing else', () => {
|
|
167
|
+
const { provisioned, routes } = mounted();
|
|
168
|
+
const send = routes.find((route) => route.key === 'chat.send')!;
|
|
169
|
+
|
|
170
|
+
expect(send.handler.link).toEqual([provisioned.Realtime, provisioned.Api]);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('links $connect to what authenticates the surface', () => {
|
|
174
|
+
const { provisioned, routes } = mounted();
|
|
175
|
+
const connect = routes.find((route) => route.key === '$connect')!;
|
|
176
|
+
|
|
177
|
+
expect(connect.handler.link).toEqual([
|
|
178
|
+
provisioned.Realtime,
|
|
179
|
+
provisioned.Auth,
|
|
180
|
+
]);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe('describeRoutes', () => {
|
|
185
|
+
it('prints what each WebSocket route reaches', () => {
|
|
186
|
+
expect(describeRoutes(manifest)).toEqual(
|
|
187
|
+
expect.arrayContaining([
|
|
188
|
+
'Realtime:',
|
|
189
|
+
' $connect → its connections',
|
|
190
|
+
' chat.send → Api (rest-api)',
|
|
191
|
+
' auth Auth ($connect verifies)',
|
|
192
|
+
]),
|
|
193
|
+
);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
describe('regionOfEndpoint', () => {
|
|
198
|
+
it('reads the region out of a management endpoint', () => {
|
|
199
|
+
expect(
|
|
200
|
+
regionOfEndpoint('https://abc.execute-api.eu-west-1.amazonaws.com/prod'),
|
|
201
|
+
).toBe('eu-west-1');
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('is undefined for a custom domain', () => {
|
|
205
|
+
expect(regionOfEndpoint('https://ws.example.com')).toBeUndefined();
|
|
206
|
+
});
|
|
207
|
+
});
|
|
@@ -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
|
+
}
|
package/src/sst/fromManifest.ts
CHANGED
|
@@ -63,6 +63,7 @@ import { RestApiSurface } from './aws/RestApiSurface';
|
|
|
63
63
|
import { Secret, type SecretProps } from './aws/Secret';
|
|
64
64
|
import { StaticSite } from './aws/StaticSite';
|
|
65
65
|
import { Topic } from './aws/Topic';
|
|
66
|
+
import { WebSocketApiSurface } from './aws/WebSocketApiSurface';
|
|
66
67
|
import {
|
|
67
68
|
ProvidesMismatch,
|
|
68
69
|
UnknownDeclarationKind,
|
|
@@ -406,6 +407,20 @@ const PROVISIONERS: Partial<Record<DeclarationKind, Provisioner>> = {
|
|
|
406
407
|
callers: context.callers?.get(d.id)?.promise,
|
|
407
408
|
}),
|
|
408
409
|
|
|
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
|
+
|
|
409
424
|
// Generated on the first deploy and kept in the stage's state, as a server
|
|
410
425
|
// stage keeps it in its secrets: every later deploy reads the same value
|
|
411
426
|
// back. One set with `sst secret set` wins over it.
|
|
@@ -644,6 +659,19 @@ export function describeRoutes(manifest: ConstructManifest): string[] {
|
|
|
644
659
|
const lines: string[] = [];
|
|
645
660
|
|
|
646
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
|
+
}
|
|
647
675
|
if (declaration.kind !== 'rest-api') continue;
|
|
648
676
|
|
|
649
677
|
lines.push(`${id}:`);
|
|
@@ -987,6 +1015,41 @@ export function mountRoutes(
|
|
|
987
1015
|
}
|
|
988
1016
|
}
|
|
989
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
|
+
|
|
990
1053
|
/**
|
|
991
1054
|
* Subscribe every queue's one consumer — the handler the build wrote for it,
|
|
992
1055
|
* linked to what that consumer depends on.
|
|
@@ -1171,7 +1234,12 @@ export function fromManifest<
|
|
|
1171
1234
|
// the things that call it exist.
|
|
1172
1235
|
const callers = new Map<string, Deferred>();
|
|
1173
1236
|
for (const [id, declaration] of Object.entries(manifest)) {
|
|
1174
|
-
if (
|
|
1237
|
+
if (
|
|
1238
|
+
declaration.kind === 'rest-api' ||
|
|
1239
|
+
declaration.kind === 'websocket-api'
|
|
1240
|
+
) {
|
|
1241
|
+
callers.set(id, defer());
|
|
1242
|
+
}
|
|
1175
1243
|
}
|
|
1176
1244
|
|
|
1177
1245
|
const context: ProvisionContext = {
|
|
@@ -1226,6 +1294,7 @@ export function fromManifest<
|
|
|
1226
1294
|
provisionProcessors(stack, manifest, provisioned);
|
|
1227
1295
|
subscribeConsumers(manifest, provisioned);
|
|
1228
1296
|
mountRoutes(manifest, provisioned);
|
|
1297
|
+
mountWebSocketRoutes(manifest, provisioned);
|
|
1229
1298
|
|
|
1230
1299
|
for (const line of describeRoutes(manifest)) console.log(line);
|
|
1231
1300
|
|
package/src/sst/index.ts
CHANGED
|
@@ -61,6 +61,13 @@ export {
|
|
|
61
61
|
smtpPassword,
|
|
62
62
|
} from './aws/ses';
|
|
63
63
|
export { Topic, type TopicProps } from './aws/Topic';
|
|
64
|
+
export {
|
|
65
|
+
regionOfEndpoint,
|
|
66
|
+
routeSelectionExpression,
|
|
67
|
+
WebSocketApiSurface,
|
|
68
|
+
type WebSocketApiSurfaceProps,
|
|
69
|
+
type WebSocketSurfaceRoute,
|
|
70
|
+
} from './aws/WebSocketApiSurface';
|
|
64
71
|
export {
|
|
65
72
|
ProvidesMismatch,
|
|
66
73
|
UnknownDeclarationKind,
|
package/src/sst/overrides.ts
CHANGED
|
@@ -98,19 +98,21 @@ export type OverrideFor<D, B extends ManifestBackends = {}> = D extends {
|
|
|
98
98
|
? EmailOverride<EmailBackendOf<B>>
|
|
99
99
|
: K extends 'rest-api'
|
|
100
100
|
? Partial<sst.aws.ApiGatewayV2Args>
|
|
101
|
-
: K extends '
|
|
102
|
-
?
|
|
103
|
-
: K extends '
|
|
104
|
-
?
|
|
105
|
-
: K extends '
|
|
106
|
-
?
|
|
107
|
-
: K extends '
|
|
108
|
-
?
|
|
109
|
-
: K extends '
|
|
110
|
-
?
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
101
|
+
: K extends 'websocket-api'
|
|
102
|
+
? Partial<sst.aws.ApiGatewayWebSocketArgs>
|
|
103
|
+
: K extends 'secret'
|
|
104
|
+
? SecretProps
|
|
105
|
+
: K extends 'credential'
|
|
106
|
+
? CredentialProps
|
|
107
|
+
: K extends 'encryption'
|
|
108
|
+
? Partial<EncryptionProps>
|
|
109
|
+
: K extends 'external-api'
|
|
110
|
+
? Omit<ExternalApiProps, 'url'>
|
|
111
|
+
: K extends 'inbox'
|
|
112
|
+
? // Its provider's values; the deploy's env otherwise.
|
|
113
|
+
Partial<Omit<InboxProps, 'topicId'>>
|
|
114
|
+
: // Derived databases, compute, mobile apps, OIDC.
|
|
115
|
+
never
|
|
114
116
|
: never;
|
|
115
117
|
|
|
116
118
|
/** Whether `{}` satisfies an override — whether the id can be left out. */
|