sst-http 2.0.0-beta.1 → 2.0.0-beta.10
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 +32 -77
- package/dist/infra.d.cts +4 -23
- package/dist/infra.d.ts +4 -23
- package/dist/infra.js +31 -75
- 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,11 @@ function resolveHandlerInput(handler) {
|
|
|
85
78
|
|
|
86
79
|
// src/bus/infra.ts
|
|
87
80
|
var MAX_SUBSCRIBER_NAME_LENGTH = 60;
|
|
88
|
-
function createBus() {
|
|
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
|
-
}
|
|
96
81
|
function wireEventsFromManifest(events, opts) {
|
|
97
82
|
if (!events || events.length === 0) {
|
|
98
83
|
return;
|
|
99
84
|
}
|
|
100
|
-
const
|
|
101
|
-
const busMap = normalizeBusInput(opts.buses);
|
|
85
|
+
const aws2 = ensureSstAws(opts.source);
|
|
102
86
|
const subscriber = resolveHandlerInput(opts.handler);
|
|
103
87
|
const seen = /* @__PURE__ */ new Set();
|
|
104
88
|
for (const event of events) {
|
|
@@ -107,57 +91,11 @@ function wireEventsFromManifest(events, opts) {
|
|
|
107
91
|
continue;
|
|
108
92
|
}
|
|
109
93
|
seen.add(key);
|
|
110
|
-
const bus =
|
|
94
|
+
const bus = aws2.Bus.get("default", "default");
|
|
111
95
|
const subscriberName = buildSubscriberName(event.event);
|
|
112
96
|
subscribeToBus(bus, subscriberName, subscriber, event.event);
|
|
113
97
|
}
|
|
114
98
|
}
|
|
115
|
-
function normalizeBusInput(input) {
|
|
116
|
-
if (!input) {
|
|
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
99
|
function subscribeToBus(bus, subscriberName, subscriber, eventName) {
|
|
162
100
|
if (typeof bus.subscribe !== "function") {
|
|
163
101
|
throw new Error("Bus instance does not support subscribe().");
|
|
@@ -199,8 +137,8 @@ function wireRoutesFromManifest(manifest, opts) {
|
|
|
199
137
|
}
|
|
200
138
|
}
|
|
201
139
|
function httpApiAdapter(args) {
|
|
202
|
-
const
|
|
203
|
-
const api = args?.api ?? new
|
|
140
|
+
const aws2 = args?.api ? void 0 : ensureSstAws(args);
|
|
141
|
+
const api = args?.api ?? new aws2.ApiGatewayV2(args?.apiName ?? "HttpApi", args?.apiArgs);
|
|
204
142
|
const ensureJwtAuthorizer = createHttpAuthorizerManager(api);
|
|
205
143
|
const registerRoute = createRouteRegistrar(api, "ApiGatewayV2");
|
|
206
144
|
return {
|
|
@@ -210,8 +148,8 @@ function httpApiAdapter(args) {
|
|
|
210
148
|
};
|
|
211
149
|
}
|
|
212
150
|
function restApiAdapter(args) {
|
|
213
|
-
const
|
|
214
|
-
const api = args?.api ?? new
|
|
151
|
+
const aws2 = args?.api ? void 0 : ensureSstAws(args);
|
|
152
|
+
const api = args?.api ?? new aws2.ApiGateway(args?.apiName ?? "RestApi", args?.apiArgs);
|
|
215
153
|
const ensureJwtAuthorizer = createRestAuthorizerManager(api);
|
|
216
154
|
const registerRoute = createRouteRegistrar(api, "ApiGateway");
|
|
217
155
|
return {
|
|
@@ -372,6 +310,7 @@ function wireApiFromManifest(manifest, opts) {
|
|
|
372
310
|
if (!manifest || !Array.isArray(manifest.routes)) {
|
|
373
311
|
throw new Error("Invalid routes manifest");
|
|
374
312
|
}
|
|
313
|
+
setHandlerBus(opts.handler);
|
|
375
314
|
wireRoutesFromManifest(manifest, {
|
|
376
315
|
handler: opts.handler,
|
|
377
316
|
firebaseProjectId: opts.firebaseProjectId,
|
|
@@ -379,8 +318,7 @@ function wireApiFromManifest(manifest, opts) {
|
|
|
379
318
|
ensureJwtAuthorizer: opts.ensureJwtAuthorizer
|
|
380
319
|
});
|
|
381
320
|
wireEventsFromManifest(manifest.events, {
|
|
382
|
-
handler: opts.handler
|
|
383
|
-
buses: opts.buses
|
|
321
|
+
handler: opts.handler
|
|
384
322
|
});
|
|
385
323
|
}
|
|
386
324
|
function loadRoutesManifest(filePath) {
|
|
@@ -392,12 +330,29 @@ function loadRoutesManifest(filePath) {
|
|
|
392
330
|
}
|
|
393
331
|
return manifest;
|
|
394
332
|
}
|
|
333
|
+
var publisherHandlerCount = 0;
|
|
334
|
+
function setHandlerBus(handler) {
|
|
335
|
+
const aws2 = ensureSstAws();
|
|
336
|
+
new aws2.iam.RolePolicy("PublisherHandlerPolicy" + (publisherHandlerCount ? publisherHandlerCount : ""), {
|
|
337
|
+
role: handler.nodes.role.name,
|
|
338
|
+
policy: {
|
|
339
|
+
Version: "2012-10-17",
|
|
340
|
+
Statement: [
|
|
341
|
+
{
|
|
342
|
+
Action: ["events:PutEvents"],
|
|
343
|
+
Resource: ["*"],
|
|
344
|
+
Effect: "Allow"
|
|
345
|
+
}
|
|
346
|
+
]
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
publisherHandlerCount++;
|
|
350
|
+
}
|
|
395
351
|
// Annotate the CommonJS export names for ESM import in node:
|
|
396
352
|
0 && (module.exports = {
|
|
397
|
-
createBus,
|
|
398
|
-
getBus,
|
|
399
353
|
httpApiAdapter,
|
|
400
354
|
loadRoutesManifest,
|
|
401
355
|
restApiAdapter,
|
|
356
|
+
setHandlerBus,
|
|
402
357
|
wireApiFromManifest
|
|
403
358
|
});
|
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,11 @@ function resolveHandlerInput(handler) {
|
|
|
60
54
|
|
|
61
55
|
// src/bus/infra.ts
|
|
62
56
|
var MAX_SUBSCRIBER_NAME_LENGTH = 60;
|
|
63
|
-
function createBus() {
|
|
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
|
-
}
|
|
71
57
|
function wireEventsFromManifest(events, opts) {
|
|
72
58
|
if (!events || events.length === 0) {
|
|
73
59
|
return;
|
|
74
60
|
}
|
|
75
|
-
const
|
|
76
|
-
const busMap = normalizeBusInput(opts.buses);
|
|
61
|
+
const aws2 = ensureSstAws(opts.source);
|
|
77
62
|
const subscriber = resolveHandlerInput(opts.handler);
|
|
78
63
|
const seen = /* @__PURE__ */ new Set();
|
|
79
64
|
for (const event of events) {
|
|
@@ -82,57 +67,11 @@ function wireEventsFromManifest(events, opts) {
|
|
|
82
67
|
continue;
|
|
83
68
|
}
|
|
84
69
|
seen.add(key);
|
|
85
|
-
const bus =
|
|
70
|
+
const bus = aws2.Bus.get("default", "default");
|
|
86
71
|
const subscriberName = buildSubscriberName(event.event);
|
|
87
72
|
subscribeToBus(bus, subscriberName, subscriber, event.event);
|
|
88
73
|
}
|
|
89
74
|
}
|
|
90
|
-
function normalizeBusInput(input) {
|
|
91
|
-
if (!input) {
|
|
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
75
|
function subscribeToBus(bus, subscriberName, subscriber, eventName) {
|
|
137
76
|
if (typeof bus.subscribe !== "function") {
|
|
138
77
|
throw new Error("Bus instance does not support subscribe().");
|
|
@@ -168,8 +107,8 @@ function wireRoutesFromManifest(manifest, opts) {
|
|
|
168
107
|
}
|
|
169
108
|
}
|
|
170
109
|
function httpApiAdapter(args) {
|
|
171
|
-
const
|
|
172
|
-
const api = args?.api ?? new
|
|
110
|
+
const aws2 = args?.api ? void 0 : ensureSstAws(args);
|
|
111
|
+
const api = args?.api ?? new aws2.ApiGatewayV2(args?.apiName ?? "HttpApi", args?.apiArgs);
|
|
173
112
|
const ensureJwtAuthorizer = createHttpAuthorizerManager(api);
|
|
174
113
|
const registerRoute = createRouteRegistrar(api, "ApiGatewayV2");
|
|
175
114
|
return {
|
|
@@ -179,8 +118,8 @@ function httpApiAdapter(args) {
|
|
|
179
118
|
};
|
|
180
119
|
}
|
|
181
120
|
function restApiAdapter(args) {
|
|
182
|
-
const
|
|
183
|
-
const api = args?.api ?? new
|
|
121
|
+
const aws2 = args?.api ? void 0 : ensureSstAws(args);
|
|
122
|
+
const api = args?.api ?? new aws2.ApiGateway(args?.apiName ?? "RestApi", args?.apiArgs);
|
|
184
123
|
const ensureJwtAuthorizer = createRestAuthorizerManager(api);
|
|
185
124
|
const registerRoute = createRouteRegistrar(api, "ApiGateway");
|
|
186
125
|
return {
|
|
@@ -341,6 +280,7 @@ function wireApiFromManifest(manifest, opts) {
|
|
|
341
280
|
if (!manifest || !Array.isArray(manifest.routes)) {
|
|
342
281
|
throw new Error("Invalid routes manifest");
|
|
343
282
|
}
|
|
283
|
+
setHandlerBus(opts.handler);
|
|
344
284
|
wireRoutesFromManifest(manifest, {
|
|
345
285
|
handler: opts.handler,
|
|
346
286
|
firebaseProjectId: opts.firebaseProjectId,
|
|
@@ -348,8 +288,7 @@ function wireApiFromManifest(manifest, opts) {
|
|
|
348
288
|
ensureJwtAuthorizer: opts.ensureJwtAuthorizer
|
|
349
289
|
});
|
|
350
290
|
wireEventsFromManifest(manifest.events, {
|
|
351
|
-
handler: opts.handler
|
|
352
|
-
buses: opts.buses
|
|
291
|
+
handler: opts.handler
|
|
353
292
|
});
|
|
354
293
|
}
|
|
355
294
|
function loadRoutesManifest(filePath) {
|
|
@@ -361,11 +300,28 @@ function loadRoutesManifest(filePath) {
|
|
|
361
300
|
}
|
|
362
301
|
return manifest;
|
|
363
302
|
}
|
|
303
|
+
var publisherHandlerCount = 0;
|
|
304
|
+
function setHandlerBus(handler) {
|
|
305
|
+
const aws2 = ensureSstAws();
|
|
306
|
+
new aws2.iam.RolePolicy("PublisherHandlerPolicy" + (publisherHandlerCount ? publisherHandlerCount : ""), {
|
|
307
|
+
role: handler.nodes.role.name,
|
|
308
|
+
policy: {
|
|
309
|
+
Version: "2012-10-17",
|
|
310
|
+
Statement: [
|
|
311
|
+
{
|
|
312
|
+
Action: ["events:PutEvents"],
|
|
313
|
+
Resource: ["*"],
|
|
314
|
+
Effect: "Allow"
|
|
315
|
+
}
|
|
316
|
+
]
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
publisherHandlerCount++;
|
|
320
|
+
}
|
|
364
321
|
export {
|
|
365
|
-
createBus,
|
|
366
|
-
getBus,
|
|
367
322
|
httpApiAdapter,
|
|
368
323
|
loadRoutesManifest,
|
|
369
324
|
restApiAdapter,
|
|
325
|
+
setHandlerBus,
|
|
370
326
|
wireApiFromManifest
|
|
371
327
|
};
|
|
@@ -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 };
|