sst-http 2.0.0-beta.1 → 2.0.0-beta.12
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 +3 -7
- package/dist/http/index.d.cts +2 -2
- package/dist/http/index.d.ts +2 -2
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/infra.cjs +36 -80
- package/dist/infra.d.cts +4 -23
- package/dist/infra.d.ts +4 -23
- package/dist/infra.js +35 -78
- package/dist/{types-w1A7o_rd.d.cts → types-o33yWNSY.d.cts} +1 -1
- package/dist/{types-w1A7o_rd.d.ts → types-o33yWNSY.d.ts} +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -113,7 +113,7 @@ Helpers such as `json()`, `text()`, and `noContent()` are available for concise
|
|
|
113
113
|
|
|
114
114
|
## Event bus
|
|
115
115
|
|
|
116
|
-
Use `@On()` to register EventBridge handlers and `publish()` to emit events.
|
|
116
|
+
Use `@On()` to register EventBridge handlers and `publish()` to emit events. Handlers decorated with `@On()` are automatically subscribed to the default EventBridge bus when you call `wireApiFromManifest()` with a manifest that includes events.
|
|
117
117
|
|
|
118
118
|
```ts
|
|
119
119
|
// src/events/user-events.ts
|
|
@@ -158,7 +158,7 @@ Optional roles and optional-auth flags flow into the adapter so you can fine-tun
|
|
|
158
158
|
|
|
159
159
|
## Wire API Gateway + EventBridge
|
|
160
160
|
|
|
161
|
-
`sst-http/infra` ships with a manifest-driven wiring utility plus adapters for HTTP API (ApiGatewayV2) and REST API (ApiGateway). The example below wires all routes to a single Lambda function inside `sst.config.ts` and connects event subscriptions from the same manifest.
|
|
161
|
+
`sst-http/infra` ships with a manifest-driven wiring utility plus adapters for HTTP API (ApiGatewayV2) and REST API (ApiGateway). The example below wires all routes to a single Lambda function inside `sst.config.ts` and automatically connects event subscriptions from the same manifest.
|
|
162
162
|
|
|
163
163
|
```ts
|
|
164
164
|
// sst.config.ts
|
|
@@ -171,7 +171,6 @@ export default $config({
|
|
|
171
171
|
loadRoutesManifest,
|
|
172
172
|
wireApiFromManifest,
|
|
173
173
|
httpApiAdapter,
|
|
174
|
-
createBus,
|
|
175
174
|
} = await import("sst-http/infra");
|
|
176
175
|
|
|
177
176
|
const manifest = loadRoutesManifest("routes.manifest.json");
|
|
@@ -184,14 +183,11 @@ export default $config({
|
|
|
184
183
|
memory: "512 MB",
|
|
185
184
|
});
|
|
186
185
|
|
|
187
|
-
const bus = createBus();
|
|
188
|
-
|
|
189
186
|
wireApiFromManifest(manifest, {
|
|
190
187
|
handler,
|
|
191
188
|
firebaseProjectId: process.env.FIREBASE_PROJECT_ID!,
|
|
192
189
|
registerRoute,
|
|
193
190
|
ensureJwtAuthorizer,
|
|
194
|
-
buses: [bus],
|
|
195
191
|
});
|
|
196
192
|
|
|
197
193
|
return { ApiUrl: api.url };
|
|
@@ -199,7 +195,7 @@ export default $config({
|
|
|
199
195
|
});
|
|
200
196
|
```
|
|
201
197
|
|
|
202
|
-
Swap in `restApiAdapter` if you prefer API Gateway REST APIs—the wiring contract is identical.
|
|
198
|
+
When the manifest contains events (from `@On()` decorators), handlers are automatically subscribed to the default EventBridge bus. Swap in `restApiAdapter` if you prefer API Gateway REST APIs—the wiring contract is identical.
|
|
203
199
|
|
|
204
200
|
## Publishing
|
|
205
201
|
|
package/dist/http/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { APIGatewayProxyEvent, APIGatewayProxyEventV2, APIGatewayProxyResult, APIGatewayProxyResultV2 } from 'aws-lambda';
|
|
2
|
-
import { R as ResponseLike, F as FirebaseAuthOptions, e as RouteOptions } from '../types-
|
|
3
|
-
export { c as FirebaseAuthMetadata, d as FirebaseClaims, H as Handler, a as HandlerContext, b as HttpMethod } from '../types-
|
|
2
|
+
import { R as ResponseLike, F as FirebaseAuthOptions, e as RouteOptions } from '../types-o33yWNSY.cjs';
|
|
3
|
+
export { c as FirebaseAuthMetadata, d as FirebaseClaims, H as Handler, a as HandlerContext, b as HttpMethod } from '../types-o33yWNSY.cjs';
|
|
4
4
|
import { ZodTypeAny } from 'zod/v4';
|
|
5
5
|
import { L as LegacyDecorator, a as LegacyParameterDecorator } from '../handler-DaM4Racx.cjs';
|
|
6
6
|
|
package/dist/http/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { APIGatewayProxyEvent, APIGatewayProxyEventV2, APIGatewayProxyResult, APIGatewayProxyResultV2 } from 'aws-lambda';
|
|
2
|
-
import { R as ResponseLike, F as FirebaseAuthOptions, e as RouteOptions } from '../types-
|
|
3
|
-
export { c as FirebaseAuthMetadata, d as FirebaseClaims, H as Handler, a as HandlerContext, b as HttpMethod } from '../types-
|
|
2
|
+
import { R as ResponseLike, F as FirebaseAuthOptions, e as RouteOptions } from '../types-o33yWNSY.js';
|
|
3
|
+
export { c as FirebaseAuthMetadata, d as FirebaseClaims, H as Handler, a as HandlerContext, b as HttpMethod } from '../types-o33yWNSY.js';
|
|
4
4
|
import { ZodTypeAny } from 'zod/v4';
|
|
5
5
|
import { L as LegacyDecorator, a as LegacyParameterDecorator } from '../handler-DaM4Racx.js';
|
|
6
6
|
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Auth, Body, Delete, FirebaseAuth, Get, Head, Header, Headers, HttpError, Options, Param, Patch, Post, Put, Query, Req, Res, configureRoutes, createHandler, handleError, json, noContent, text } from './http/index.cjs';
|
|
2
|
-
export { c as FirebaseAuthMetadata, F as FirebaseAuthOptions, d as FirebaseClaims, H as Handler, a as HandlerContext, b as HttpMethod, R as ResponseLike, e as RouteOptions } from './types-
|
|
2
|
+
export { c as FirebaseAuthMetadata, F as FirebaseAuthOptions, d as FirebaseClaims, H as Handler, a as HandlerContext, b as HttpMethod, R as ResponseLike, e as RouteOptions } from './types-o33yWNSY.cjs';
|
|
3
3
|
export { On, publish } from './bus/index.cjs';
|
|
4
4
|
import 'aws-lambda';
|
|
5
5
|
import 'zod/v4';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Auth, Body, Delete, FirebaseAuth, Get, Head, Header, Headers, HttpError, Options, Param, Patch, Post, Put, Query, Req, Res, configureRoutes, createHandler, handleError, json, noContent, text } from './http/index.js';
|
|
2
|
-
export { c as FirebaseAuthMetadata, F as FirebaseAuthOptions, d as FirebaseClaims, H as Handler, a as HandlerContext, b as HttpMethod, R as ResponseLike, e as RouteOptions } from './types-
|
|
2
|
+
export { c as FirebaseAuthMetadata, F as FirebaseAuthOptions, d as FirebaseClaims, H as Handler, a as HandlerContext, b as HttpMethod, R as ResponseLike, e as RouteOptions } from './types-o33yWNSY.js';
|
|
3
3
|
export { On, publish } from './bus/index.js';
|
|
4
4
|
import 'aws-lambda';
|
|
5
5
|
import 'zod/v4';
|
package/dist/infra.cjs
CHANGED
|
@@ -20,11 +20,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/infra.ts
|
|
21
21
|
var infra_exports = {};
|
|
22
22
|
__export(infra_exports, {
|
|
23
|
-
createBus: () => createBus,
|
|
24
|
-
getBus: () => getBus,
|
|
25
23
|
httpApiAdapter: () => httpApiAdapter,
|
|
26
24
|
loadRoutesManifest: () => loadRoutesManifest,
|
|
27
25
|
restApiAdapter: () => restApiAdapter,
|
|
26
|
+
setHandlerBus: () => setHandlerBus,
|
|
28
27
|
wireApiFromManifest: () => wireApiFromManifest
|
|
29
28
|
});
|
|
30
29
|
module.exports = __toCommonJS(infra_exports);
|
|
@@ -42,13 +41,6 @@ function getFunction(value, key) {
|
|
|
42
41
|
const candidate = value[key];
|
|
43
42
|
return typeof candidate === "function" ? candidate : void 0;
|
|
44
43
|
}
|
|
45
|
-
function getStringProp(value, key) {
|
|
46
|
-
if (!isRecord(value)) {
|
|
47
|
-
return void 0;
|
|
48
|
-
}
|
|
49
|
-
const candidate = value[key];
|
|
50
|
-
return typeof candidate === "string" && candidate.length > 0 ? candidate : void 0;
|
|
51
|
-
}
|
|
52
44
|
function ensureRecord(value, message) {
|
|
53
45
|
if (!isRecord(value)) {
|
|
54
46
|
throw new Error(message);
|
|
@@ -56,13 +48,14 @@ function ensureRecord(value, message) {
|
|
|
56
48
|
return value;
|
|
57
49
|
}
|
|
58
50
|
function ensureSstAws(source) {
|
|
59
|
-
const
|
|
60
|
-
if (!
|
|
51
|
+
const awsData = typeof sst !== "undefined" ? sst.aws : source?.sst?.aws ?? globalThis.sst?.aws;
|
|
52
|
+
if (!awsData) {
|
|
61
53
|
throw new Error(
|
|
62
54
|
"SST aws namespace is not available. Ensure this code runs within an SST config."
|
|
63
55
|
);
|
|
64
56
|
}
|
|
65
|
-
|
|
57
|
+
awsData.iam = aws.iam;
|
|
58
|
+
return awsData;
|
|
66
59
|
}
|
|
67
60
|
function resolveHandlerInput(handler) {
|
|
68
61
|
if (handler === void 0) {
|
|
@@ -85,20 +78,12 @@ function resolveHandlerInput(handler) {
|
|
|
85
78
|
|
|
86
79
|
// src/bus/infra.ts
|
|
87
80
|
var MAX_SUBSCRIBER_NAME_LENGTH = 60;
|
|
88
|
-
|
|
89
|
-
const aws = ensureSstAws();
|
|
90
|
-
return new aws.Bus("default");
|
|
91
|
-
}
|
|
92
|
-
function getBus() {
|
|
93
|
-
const aws = ensureSstAws();
|
|
94
|
-
return aws.Bus.get("default", "default");
|
|
95
|
-
}
|
|
81
|
+
var bus;
|
|
96
82
|
function wireEventsFromManifest(events, opts) {
|
|
97
83
|
if (!events || events.length === 0) {
|
|
98
84
|
return;
|
|
99
85
|
}
|
|
100
|
-
const
|
|
101
|
-
const busMap = normalizeBusInput(opts.buses);
|
|
86
|
+
const aws2 = ensureSstAws(opts.source);
|
|
102
87
|
const subscriber = resolveHandlerInput(opts.handler);
|
|
103
88
|
const seen = /* @__PURE__ */ new Set();
|
|
104
89
|
for (const event of events) {
|
|
@@ -106,63 +91,17 @@ function wireEventsFromManifest(events, opts) {
|
|
|
106
91
|
if (seen.has(key)) {
|
|
107
92
|
continue;
|
|
108
93
|
}
|
|
94
|
+
if (!bus) bus = aws2.Bus.get("default", "default");
|
|
109
95
|
seen.add(key);
|
|
110
|
-
const bus = resolveBusForEvent(busMap, aws);
|
|
111
96
|
const subscriberName = buildSubscriberName(event.event);
|
|
112
97
|
subscribeToBus(bus, subscriberName, subscriber, event.event);
|
|
113
98
|
}
|
|
114
99
|
}
|
|
115
|
-
function
|
|
116
|
-
if (
|
|
117
|
-
return void 0;
|
|
118
|
-
}
|
|
119
|
-
const map = /* @__PURE__ */ new Map();
|
|
120
|
-
if (Array.isArray(input)) {
|
|
121
|
-
for (const bus of input) {
|
|
122
|
-
const key2 = getBusKey(bus);
|
|
123
|
-
if (key2) {
|
|
124
|
-
map.set(key2, bus);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
if (map.size === 1 && !map.has("default")) {
|
|
128
|
-
const [bus] = map.values();
|
|
129
|
-
map.set("default", bus);
|
|
130
|
-
}
|
|
131
|
-
return map;
|
|
132
|
-
}
|
|
133
|
-
if (isBusRecord(input)) {
|
|
134
|
-
for (const [key2, bus] of Object.entries(input)) {
|
|
135
|
-
map.set(key2, bus);
|
|
136
|
-
}
|
|
137
|
-
return map;
|
|
138
|
-
}
|
|
139
|
-
const key = getBusKey(input) ?? "default";
|
|
140
|
-
map.set(key, input);
|
|
141
|
-
return map;
|
|
142
|
-
}
|
|
143
|
-
function isBusRecord(input) {
|
|
144
|
-
return !Array.isArray(input) && isRecord(input) && !("subscribe" in input);
|
|
145
|
-
}
|
|
146
|
-
function getBusKey(bus) {
|
|
147
|
-
return getStringProp(bus, "name") ?? getStringProp(bus, "constructorName");
|
|
148
|
-
}
|
|
149
|
-
function resolveBusForEvent(busMap, aws) {
|
|
150
|
-
if (busMap && busMap.size > 0) {
|
|
151
|
-
const direct = busMap.get("default");
|
|
152
|
-
if (direct) {
|
|
153
|
-
return direct;
|
|
154
|
-
}
|
|
155
|
-
if (busMap.size === 1) {
|
|
156
|
-
return busMap.values().next().value;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return aws.Bus.get("default", "default");
|
|
160
|
-
}
|
|
161
|
-
function subscribeToBus(bus, subscriberName, subscriber, eventName) {
|
|
162
|
-
if (typeof bus.subscribe !== "function") {
|
|
100
|
+
function subscribeToBus(bus2, subscriberName, subscriber, eventName) {
|
|
101
|
+
if (typeof bus2.subscribe !== "function") {
|
|
163
102
|
throw new Error("Bus instance does not support subscribe().");
|
|
164
103
|
}
|
|
165
|
-
|
|
104
|
+
bus2.subscribe(subscriberName, subscriber, {
|
|
166
105
|
pattern: {
|
|
167
106
|
detailType: [eventName]
|
|
168
107
|
}
|
|
@@ -199,8 +138,8 @@ function wireRoutesFromManifest(manifest, opts) {
|
|
|
199
138
|
}
|
|
200
139
|
}
|
|
201
140
|
function httpApiAdapter(args) {
|
|
202
|
-
const
|
|
203
|
-
const api = args?.api ?? new
|
|
141
|
+
const aws2 = args?.api ? void 0 : ensureSstAws(args);
|
|
142
|
+
const api = args?.api ?? new aws2.ApiGatewayV2(args?.apiName ?? "HttpApi", args?.apiArgs);
|
|
204
143
|
const ensureJwtAuthorizer = createHttpAuthorizerManager(api);
|
|
205
144
|
const registerRoute = createRouteRegistrar(api, "ApiGatewayV2");
|
|
206
145
|
return {
|
|
@@ -210,8 +149,8 @@ function httpApiAdapter(args) {
|
|
|
210
149
|
};
|
|
211
150
|
}
|
|
212
151
|
function restApiAdapter(args) {
|
|
213
|
-
const
|
|
214
|
-
const api = args?.api ?? new
|
|
152
|
+
const aws2 = args?.api ? void 0 : ensureSstAws(args);
|
|
153
|
+
const api = args?.api ?? new aws2.ApiGateway(args?.apiName ?? "RestApi", args?.apiArgs);
|
|
215
154
|
const ensureJwtAuthorizer = createRestAuthorizerManager(api);
|
|
216
155
|
const registerRoute = createRouteRegistrar(api, "ApiGateway");
|
|
217
156
|
return {
|
|
@@ -372,6 +311,7 @@ function wireApiFromManifest(manifest, opts) {
|
|
|
372
311
|
if (!manifest || !Array.isArray(manifest.routes)) {
|
|
373
312
|
throw new Error("Invalid routes manifest");
|
|
374
313
|
}
|
|
314
|
+
setHandlerBus(opts.handler);
|
|
375
315
|
wireRoutesFromManifest(manifest, {
|
|
376
316
|
handler: opts.handler,
|
|
377
317
|
firebaseProjectId: opts.firebaseProjectId,
|
|
@@ -379,8 +319,7 @@ function wireApiFromManifest(manifest, opts) {
|
|
|
379
319
|
ensureJwtAuthorizer: opts.ensureJwtAuthorizer
|
|
380
320
|
});
|
|
381
321
|
wireEventsFromManifest(manifest.events, {
|
|
382
|
-
handler: opts.handler
|
|
383
|
-
buses: opts.buses
|
|
322
|
+
handler: opts.handler
|
|
384
323
|
});
|
|
385
324
|
}
|
|
386
325
|
function loadRoutesManifest(filePath) {
|
|
@@ -392,12 +331,29 @@ function loadRoutesManifest(filePath) {
|
|
|
392
331
|
}
|
|
393
332
|
return manifest;
|
|
394
333
|
}
|
|
334
|
+
var publisherHandlerCount = 0;
|
|
335
|
+
function setHandlerBus(handler) {
|
|
336
|
+
const aws2 = ensureSstAws();
|
|
337
|
+
new aws2.iam.RolePolicy("PublisherHandlerPolicy" + (publisherHandlerCount ? publisherHandlerCount : ""), {
|
|
338
|
+
role: handler.nodes.role.name,
|
|
339
|
+
policy: {
|
|
340
|
+
Version: "2012-10-17",
|
|
341
|
+
Statement: [
|
|
342
|
+
{
|
|
343
|
+
Action: ["events:PutEvents"],
|
|
344
|
+
Resource: ["*"],
|
|
345
|
+
Effect: "Allow"
|
|
346
|
+
}
|
|
347
|
+
]
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
publisherHandlerCount++;
|
|
351
|
+
}
|
|
395
352
|
// Annotate the CommonJS export names for ESM import in node:
|
|
396
353
|
0 && (module.exports = {
|
|
397
|
-
createBus,
|
|
398
|
-
getBus,
|
|
399
354
|
httpApiAdapter,
|
|
400
355
|
loadRoutesManifest,
|
|
401
356
|
restApiAdapter,
|
|
357
|
+
setHandlerBus,
|
|
402
358
|
wireApiFromManifest
|
|
403
359
|
});
|
package/dist/infra.d.cts
CHANGED
|
@@ -1,32 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { b as HttpMethod, f as RoutesManifest } from './types-o33yWNSY.cjs';
|
|
2
|
+
export { h as RoutesManifestAuth, i as RoutesManifestEvent, g as RoutesManifestRoute } from './types-o33yWNSY.cjs';
|
|
3
3
|
import 'aws-lambda';
|
|
4
4
|
|
|
5
|
-
type BusSubscriberArgs = {
|
|
6
|
-
pattern?: {
|
|
7
|
-
detailType?: string[];
|
|
8
|
-
};
|
|
9
|
-
};
|
|
10
|
-
type BusLike = {
|
|
11
|
-
name?: unknown;
|
|
12
|
-
arn?: unknown;
|
|
13
|
-
subscribe: (name: string, subscriber: unknown, args?: BusSubscriberArgs) => unknown;
|
|
14
|
-
};
|
|
15
5
|
type AwsSource = {
|
|
16
6
|
sst?: {
|
|
17
7
|
aws?: unknown;
|
|
18
8
|
};
|
|
19
9
|
};
|
|
20
10
|
|
|
21
|
-
type BusInput = BusLike | BusLike[] | Record<string, BusLike>;
|
|
22
|
-
declare function createBus(): BusLike;
|
|
23
|
-
declare function getBus(): BusLike;
|
|
24
|
-
declare function wireEventsFromManifest(events: RoutesManifestEvent[] | undefined, opts: {
|
|
25
|
-
handler: unknown;
|
|
26
|
-
buses?: BusInput;
|
|
27
|
-
source?: AwsSource;
|
|
28
|
-
}): void;
|
|
29
|
-
|
|
30
11
|
type RegisterRouteConfig = {
|
|
31
12
|
handler: unknown;
|
|
32
13
|
protected: boolean;
|
|
@@ -63,8 +44,8 @@ declare function wireApiFromManifest(manifest: RoutesManifest, opts: {
|
|
|
63
44
|
firebaseProjectId: string;
|
|
64
45
|
registerRoute: RegisterRoute;
|
|
65
46
|
ensureJwtAuthorizer: EnsureJwtAuthorizer;
|
|
66
|
-
buses?: Parameters<typeof wireEventsFromManifest>[1]["buses"];
|
|
67
47
|
}): void;
|
|
68
48
|
declare function loadRoutesManifest(filePath: string): RoutesManifest;
|
|
49
|
+
declare function setHandlerBus(handler: unknown): void;
|
|
69
50
|
|
|
70
|
-
export { type EnsureJwtAuthorizer, type RegisterRoute, type RegisterRouteConfig, RoutesManifest,
|
|
51
|
+
export { type EnsureJwtAuthorizer, type RegisterRoute, type RegisterRouteConfig, RoutesManifest, httpApiAdapter, loadRoutesManifest, restApiAdapter, setHandlerBus, wireApiFromManifest };
|
package/dist/infra.d.ts
CHANGED
|
@@ -1,32 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { b as HttpMethod, f as RoutesManifest } from './types-o33yWNSY.js';
|
|
2
|
+
export { h as RoutesManifestAuth, i as RoutesManifestEvent, g as RoutesManifestRoute } from './types-o33yWNSY.js';
|
|
3
3
|
import 'aws-lambda';
|
|
4
4
|
|
|
5
|
-
type BusSubscriberArgs = {
|
|
6
|
-
pattern?: {
|
|
7
|
-
detailType?: string[];
|
|
8
|
-
};
|
|
9
|
-
};
|
|
10
|
-
type BusLike = {
|
|
11
|
-
name?: unknown;
|
|
12
|
-
arn?: unknown;
|
|
13
|
-
subscribe: (name: string, subscriber: unknown, args?: BusSubscriberArgs) => unknown;
|
|
14
|
-
};
|
|
15
5
|
type AwsSource = {
|
|
16
6
|
sst?: {
|
|
17
7
|
aws?: unknown;
|
|
18
8
|
};
|
|
19
9
|
};
|
|
20
10
|
|
|
21
|
-
type BusInput = BusLike | BusLike[] | Record<string, BusLike>;
|
|
22
|
-
declare function createBus(): BusLike;
|
|
23
|
-
declare function getBus(): BusLike;
|
|
24
|
-
declare function wireEventsFromManifest(events: RoutesManifestEvent[] | undefined, opts: {
|
|
25
|
-
handler: unknown;
|
|
26
|
-
buses?: BusInput;
|
|
27
|
-
source?: AwsSource;
|
|
28
|
-
}): void;
|
|
29
|
-
|
|
30
11
|
type RegisterRouteConfig = {
|
|
31
12
|
handler: unknown;
|
|
32
13
|
protected: boolean;
|
|
@@ -63,8 +44,8 @@ declare function wireApiFromManifest(manifest: RoutesManifest, opts: {
|
|
|
63
44
|
firebaseProjectId: string;
|
|
64
45
|
registerRoute: RegisterRoute;
|
|
65
46
|
ensureJwtAuthorizer: EnsureJwtAuthorizer;
|
|
66
|
-
buses?: Parameters<typeof wireEventsFromManifest>[1]["buses"];
|
|
67
47
|
}): void;
|
|
68
48
|
declare function loadRoutesManifest(filePath: string): RoutesManifest;
|
|
49
|
+
declare function setHandlerBus(handler: unknown): void;
|
|
69
50
|
|
|
70
|
-
export { type EnsureJwtAuthorizer, type RegisterRoute, type RegisterRouteConfig, RoutesManifest,
|
|
51
|
+
export { type EnsureJwtAuthorizer, type RegisterRoute, type RegisterRouteConfig, RoutesManifest, httpApiAdapter, loadRoutesManifest, restApiAdapter, setHandlerBus, wireApiFromManifest };
|
package/dist/infra.js
CHANGED
|
@@ -17,13 +17,6 @@ function getFunction(value, key) {
|
|
|
17
17
|
const candidate = value[key];
|
|
18
18
|
return typeof candidate === "function" ? candidate : void 0;
|
|
19
19
|
}
|
|
20
|
-
function getStringProp(value, key) {
|
|
21
|
-
if (!isRecord(value)) {
|
|
22
|
-
return void 0;
|
|
23
|
-
}
|
|
24
|
-
const candidate = value[key];
|
|
25
|
-
return typeof candidate === "string" && candidate.length > 0 ? candidate : void 0;
|
|
26
|
-
}
|
|
27
20
|
function ensureRecord(value, message) {
|
|
28
21
|
if (!isRecord(value)) {
|
|
29
22
|
throw new Error(message);
|
|
@@ -31,13 +24,14 @@ function ensureRecord(value, message) {
|
|
|
31
24
|
return value;
|
|
32
25
|
}
|
|
33
26
|
function ensureSstAws(source) {
|
|
34
|
-
const
|
|
35
|
-
if (!
|
|
27
|
+
const awsData = typeof sst !== "undefined" ? sst.aws : source?.sst?.aws ?? globalThis.sst?.aws;
|
|
28
|
+
if (!awsData) {
|
|
36
29
|
throw new Error(
|
|
37
30
|
"SST aws namespace is not available. Ensure this code runs within an SST config."
|
|
38
31
|
);
|
|
39
32
|
}
|
|
40
|
-
|
|
33
|
+
awsData.iam = aws.iam;
|
|
34
|
+
return awsData;
|
|
41
35
|
}
|
|
42
36
|
function resolveHandlerInput(handler) {
|
|
43
37
|
if (handler === void 0) {
|
|
@@ -60,20 +54,12 @@ function resolveHandlerInput(handler) {
|
|
|
60
54
|
|
|
61
55
|
// src/bus/infra.ts
|
|
62
56
|
var MAX_SUBSCRIBER_NAME_LENGTH = 60;
|
|
63
|
-
|
|
64
|
-
const aws = ensureSstAws();
|
|
65
|
-
return new aws.Bus("default");
|
|
66
|
-
}
|
|
67
|
-
function getBus() {
|
|
68
|
-
const aws = ensureSstAws();
|
|
69
|
-
return aws.Bus.get("default", "default");
|
|
70
|
-
}
|
|
57
|
+
var bus;
|
|
71
58
|
function wireEventsFromManifest(events, opts) {
|
|
72
59
|
if (!events || events.length === 0) {
|
|
73
60
|
return;
|
|
74
61
|
}
|
|
75
|
-
const
|
|
76
|
-
const busMap = normalizeBusInput(opts.buses);
|
|
62
|
+
const aws2 = ensureSstAws(opts.source);
|
|
77
63
|
const subscriber = resolveHandlerInput(opts.handler);
|
|
78
64
|
const seen = /* @__PURE__ */ new Set();
|
|
79
65
|
for (const event of events) {
|
|
@@ -81,63 +67,17 @@ function wireEventsFromManifest(events, opts) {
|
|
|
81
67
|
if (seen.has(key)) {
|
|
82
68
|
continue;
|
|
83
69
|
}
|
|
70
|
+
if (!bus) bus = aws2.Bus.get("default", "default");
|
|
84
71
|
seen.add(key);
|
|
85
|
-
const bus = resolveBusForEvent(busMap, aws);
|
|
86
72
|
const subscriberName = buildSubscriberName(event.event);
|
|
87
73
|
subscribeToBus(bus, subscriberName, subscriber, event.event);
|
|
88
74
|
}
|
|
89
75
|
}
|
|
90
|
-
function
|
|
91
|
-
if (
|
|
92
|
-
return void 0;
|
|
93
|
-
}
|
|
94
|
-
const map = /* @__PURE__ */ new Map();
|
|
95
|
-
if (Array.isArray(input)) {
|
|
96
|
-
for (const bus of input) {
|
|
97
|
-
const key2 = getBusKey(bus);
|
|
98
|
-
if (key2) {
|
|
99
|
-
map.set(key2, bus);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
if (map.size === 1 && !map.has("default")) {
|
|
103
|
-
const [bus] = map.values();
|
|
104
|
-
map.set("default", bus);
|
|
105
|
-
}
|
|
106
|
-
return map;
|
|
107
|
-
}
|
|
108
|
-
if (isBusRecord(input)) {
|
|
109
|
-
for (const [key2, bus] of Object.entries(input)) {
|
|
110
|
-
map.set(key2, bus);
|
|
111
|
-
}
|
|
112
|
-
return map;
|
|
113
|
-
}
|
|
114
|
-
const key = getBusKey(input) ?? "default";
|
|
115
|
-
map.set(key, input);
|
|
116
|
-
return map;
|
|
117
|
-
}
|
|
118
|
-
function isBusRecord(input) {
|
|
119
|
-
return !Array.isArray(input) && isRecord(input) && !("subscribe" in input);
|
|
120
|
-
}
|
|
121
|
-
function getBusKey(bus) {
|
|
122
|
-
return getStringProp(bus, "name") ?? getStringProp(bus, "constructorName");
|
|
123
|
-
}
|
|
124
|
-
function resolveBusForEvent(busMap, aws) {
|
|
125
|
-
if (busMap && busMap.size > 0) {
|
|
126
|
-
const direct = busMap.get("default");
|
|
127
|
-
if (direct) {
|
|
128
|
-
return direct;
|
|
129
|
-
}
|
|
130
|
-
if (busMap.size === 1) {
|
|
131
|
-
return busMap.values().next().value;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return aws.Bus.get("default", "default");
|
|
135
|
-
}
|
|
136
|
-
function subscribeToBus(bus, subscriberName, subscriber, eventName) {
|
|
137
|
-
if (typeof bus.subscribe !== "function") {
|
|
76
|
+
function subscribeToBus(bus2, subscriberName, subscriber, eventName) {
|
|
77
|
+
if (typeof bus2.subscribe !== "function") {
|
|
138
78
|
throw new Error("Bus instance does not support subscribe().");
|
|
139
79
|
}
|
|
140
|
-
|
|
80
|
+
bus2.subscribe(subscriberName, subscriber, {
|
|
141
81
|
pattern: {
|
|
142
82
|
detailType: [eventName]
|
|
143
83
|
}
|
|
@@ -168,8 +108,8 @@ function wireRoutesFromManifest(manifest, opts) {
|
|
|
168
108
|
}
|
|
169
109
|
}
|
|
170
110
|
function httpApiAdapter(args) {
|
|
171
|
-
const
|
|
172
|
-
const api = args?.api ?? new
|
|
111
|
+
const aws2 = args?.api ? void 0 : ensureSstAws(args);
|
|
112
|
+
const api = args?.api ?? new aws2.ApiGatewayV2(args?.apiName ?? "HttpApi", args?.apiArgs);
|
|
173
113
|
const ensureJwtAuthorizer = createHttpAuthorizerManager(api);
|
|
174
114
|
const registerRoute = createRouteRegistrar(api, "ApiGatewayV2");
|
|
175
115
|
return {
|
|
@@ -179,8 +119,8 @@ function httpApiAdapter(args) {
|
|
|
179
119
|
};
|
|
180
120
|
}
|
|
181
121
|
function restApiAdapter(args) {
|
|
182
|
-
const
|
|
183
|
-
const api = args?.api ?? new
|
|
122
|
+
const aws2 = args?.api ? void 0 : ensureSstAws(args);
|
|
123
|
+
const api = args?.api ?? new aws2.ApiGateway(args?.apiName ?? "RestApi", args?.apiArgs);
|
|
184
124
|
const ensureJwtAuthorizer = createRestAuthorizerManager(api);
|
|
185
125
|
const registerRoute = createRouteRegistrar(api, "ApiGateway");
|
|
186
126
|
return {
|
|
@@ -341,6 +281,7 @@ function wireApiFromManifest(manifest, opts) {
|
|
|
341
281
|
if (!manifest || !Array.isArray(manifest.routes)) {
|
|
342
282
|
throw new Error("Invalid routes manifest");
|
|
343
283
|
}
|
|
284
|
+
setHandlerBus(opts.handler);
|
|
344
285
|
wireRoutesFromManifest(manifest, {
|
|
345
286
|
handler: opts.handler,
|
|
346
287
|
firebaseProjectId: opts.firebaseProjectId,
|
|
@@ -348,8 +289,7 @@ function wireApiFromManifest(manifest, opts) {
|
|
|
348
289
|
ensureJwtAuthorizer: opts.ensureJwtAuthorizer
|
|
349
290
|
});
|
|
350
291
|
wireEventsFromManifest(manifest.events, {
|
|
351
|
-
handler: opts.handler
|
|
352
|
-
buses: opts.buses
|
|
292
|
+
handler: opts.handler
|
|
353
293
|
});
|
|
354
294
|
}
|
|
355
295
|
function loadRoutesManifest(filePath) {
|
|
@@ -361,11 +301,28 @@ function loadRoutesManifest(filePath) {
|
|
|
361
301
|
}
|
|
362
302
|
return manifest;
|
|
363
303
|
}
|
|
304
|
+
var publisherHandlerCount = 0;
|
|
305
|
+
function setHandlerBus(handler) {
|
|
306
|
+
const aws2 = ensureSstAws();
|
|
307
|
+
new aws2.iam.RolePolicy("PublisherHandlerPolicy" + (publisherHandlerCount ? publisherHandlerCount : ""), {
|
|
308
|
+
role: handler.nodes.role.name,
|
|
309
|
+
policy: {
|
|
310
|
+
Version: "2012-10-17",
|
|
311
|
+
Statement: [
|
|
312
|
+
{
|
|
313
|
+
Action: ["events:PutEvents"],
|
|
314
|
+
Resource: ["*"],
|
|
315
|
+
Effect: "Allow"
|
|
316
|
+
}
|
|
317
|
+
]
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
publisherHandlerCount++;
|
|
321
|
+
}
|
|
364
322
|
export {
|
|
365
|
-
createBus,
|
|
366
|
-
getBus,
|
|
367
323
|
httpApiAdapter,
|
|
368
324
|
loadRoutesManifest,
|
|
369
325
|
restApiAdapter,
|
|
326
|
+
setHandlerBus,
|
|
370
327
|
wireApiFromManifest
|
|
371
328
|
};
|
|
@@ -55,4 +55,4 @@ type RoutesManifest = {
|
|
|
55
55
|
events?: RoutesManifestEvent[];
|
|
56
56
|
};
|
|
57
57
|
|
|
58
|
-
export type { FirebaseAuthOptions as F, Handler as H, ResponseLike as R, HandlerContext as a, HttpMethod as b, FirebaseAuthMetadata as c, FirebaseClaims as d, RouteOptions as e,
|
|
58
|
+
export type { FirebaseAuthOptions as F, Handler as H, ResponseLike as R, HandlerContext as a, HttpMethod as b, FirebaseAuthMetadata as c, FirebaseClaims as d, RouteOptions as e, RoutesManifest as f, RoutesManifestRoute as g, RoutesManifestAuth as h, RoutesManifestEvent as i };
|
|
@@ -55,4 +55,4 @@ type RoutesManifest = {
|
|
|
55
55
|
events?: RoutesManifestEvent[];
|
|
56
56
|
};
|
|
57
57
|
|
|
58
|
-
export type { FirebaseAuthOptions as F, Handler as H, ResponseLike as R, HandlerContext as a, HttpMethod as b, FirebaseAuthMetadata as c, FirebaseClaims as d, RouteOptions as e,
|
|
58
|
+
export type { FirebaseAuthOptions as F, Handler as H, ResponseLike as R, HandlerContext as a, HttpMethod as b, FirebaseAuthMetadata as c, FirebaseClaims as d, RouteOptions as e, RoutesManifest as f, RoutesManifestRoute as g, RoutesManifestAuth as h, RoutesManifestEvent as i };
|