@cedarjs/api 4.0.0 → 4.0.1-next.67
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/dist/cjs/runtime.d.ts +37 -0
- package/dist/cjs/runtime.d.ts.map +1 -0
- package/dist/cjs/runtime.js +238 -0
- package/dist/runtime.d.ts +37 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +198 -0
- package/package.json +16 -5
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { APIGatewayProxyEvent, APIGatewayProxyResult, Context as LambdaContext } from 'aws-lambda';
|
|
2
|
+
import { getAuthenticationContext } from './auth/index.js';
|
|
3
|
+
export interface CedarRequestContext {
|
|
4
|
+
params: Record<string, string>;
|
|
5
|
+
query: URLSearchParams;
|
|
6
|
+
cookies: ReadonlyMap<string, string>;
|
|
7
|
+
serverAuthState?: Awaited<ReturnType<typeof getAuthenticationContext>>;
|
|
8
|
+
}
|
|
9
|
+
export type CedarHandler = (request: Request, ctx: CedarRequestContext) => Promise<Response> | Response;
|
|
10
|
+
export type CedarMiddleware = (request: Request, ctx: CedarRequestContext, next: () => Promise<Response>) => Promise<Response>;
|
|
11
|
+
export interface CedarRouteRecord {
|
|
12
|
+
path: string;
|
|
13
|
+
methods: string[];
|
|
14
|
+
type: 'graphql' | 'auth' | 'function' | 'health';
|
|
15
|
+
entry: string;
|
|
16
|
+
}
|
|
17
|
+
export interface BuildCedarContextOptions {
|
|
18
|
+
params?: Record<string, string>;
|
|
19
|
+
authDecoder?: Parameters<typeof getAuthenticationContext>[0]['authDecoder'];
|
|
20
|
+
lambdaContext?: LambdaContext;
|
|
21
|
+
}
|
|
22
|
+
export interface LegacyHandlerContext {
|
|
23
|
+
event: APIGatewayProxyEvent;
|
|
24
|
+
context: LambdaContext;
|
|
25
|
+
request: Request;
|
|
26
|
+
cedarContext: CedarRequestContext;
|
|
27
|
+
}
|
|
28
|
+
export type LegacyHandlerResult = APIGatewayProxyResult | Response;
|
|
29
|
+
export type LegacyHandler = (event: APIGatewayProxyEvent, context: LambdaContext) => Promise<LegacyHandlerResult> | LegacyHandlerResult;
|
|
30
|
+
export declare function buildCedarContext(request: Request, options?: BuildCedarContextOptions): Promise<CedarRequestContext>;
|
|
31
|
+
export declare function composeCedarMiddleware(handler: CedarHandler, middleware: CedarMiddleware[]): CedarHandler;
|
|
32
|
+
export declare function createRouteManifest(routes: CedarRouteRecord[]): CedarRouteRecord[];
|
|
33
|
+
export declare function routeManifestToJSON(routes: CedarRouteRecord[]): string;
|
|
34
|
+
export declare function wrapLegacyHandler(legacyHandler: LegacyHandler, options?: BuildCedarContextOptions): CedarHandler;
|
|
35
|
+
export declare function requestToLegacyEvent(request: Request, ctx: CedarRequestContext): Promise<APIGatewayProxyEvent>;
|
|
36
|
+
export declare function legacyResultToResponse(result: LegacyHandlerResult): Response;
|
|
37
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/runtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EAEpB,qBAAqB,EACrB,OAAO,IAAI,aAAa,EACzB,MAAM,YAAY,CAAA;AAInB,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAA;AAE1D,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC9B,KAAK,EAAE,eAAe,CAAA;IACtB,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACpC,eAAe,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,wBAAwB,CAAC,CAAC,CAAA;CACvE;AAED,MAAM,MAAM,YAAY,GAAG,CACzB,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,mBAAmB,KACrB,OAAO,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAA;AAEj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EAAE,CAOpB;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,gBAAgB,EAAE,GAAG,MAAM,CAEtE;AAED,wBAAgB,iBAAiB,CAC/B,aAAa,EAAE,aAAa,EAC5B,OAAO,GAAE,wBAA6B,GACrC,YAAY,CAQd;AAED,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,oBAAoB,CAAC,CA0D/B;AAED,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,mBAAmB,GAAG,QAAQ,CA4C5E"}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var runtime_exports = {};
|
|
30
|
+
__export(runtime_exports, {
|
|
31
|
+
buildCedarContext: () => buildCedarContext,
|
|
32
|
+
composeCedarMiddleware: () => composeCedarMiddleware,
|
|
33
|
+
createRouteManifest: () => createRouteManifest,
|
|
34
|
+
legacyResultToResponse: () => legacyResultToResponse,
|
|
35
|
+
requestToLegacyEvent: () => requestToLegacyEvent,
|
|
36
|
+
routeManifestToJSON: () => routeManifestToJSON,
|
|
37
|
+
wrapLegacyHandler: () => wrapLegacyHandler
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(runtime_exports);
|
|
40
|
+
var cookie = __toESM(require("cookie"), 1);
|
|
41
|
+
var import_picoquery = require("picoquery");
|
|
42
|
+
var import_auth = require("./auth/index.js");
|
|
43
|
+
const DEFAULT_LAMBDA_CONTEXT = {
|
|
44
|
+
callbackWaitsForEmptyEventLoop: false,
|
|
45
|
+
functionName: "cedar",
|
|
46
|
+
functionVersion: "$LATEST",
|
|
47
|
+
invokedFunctionArn: "cedar",
|
|
48
|
+
memoryLimitInMB: "0",
|
|
49
|
+
awsRequestId: "cedar-request",
|
|
50
|
+
logGroupName: "cedar",
|
|
51
|
+
logStreamName: "cedar",
|
|
52
|
+
getRemainingTimeInMillis() {
|
|
53
|
+
return 0;
|
|
54
|
+
},
|
|
55
|
+
done() {
|
|
56
|
+
return void 0;
|
|
57
|
+
},
|
|
58
|
+
fail() {
|
|
59
|
+
return void 0;
|
|
60
|
+
},
|
|
61
|
+
succeed() {
|
|
62
|
+
return void 0;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
async function buildCedarContext(request, options = {}) {
|
|
66
|
+
const url = new URL(request.url);
|
|
67
|
+
const query = url.searchParams;
|
|
68
|
+
const cookies = new Map(
|
|
69
|
+
Object.entries(cookie.parse(request.headers.get("cookie") ?? "")).filter(
|
|
70
|
+
(entry) => {
|
|
71
|
+
return entry[1] !== void 0;
|
|
72
|
+
}
|
|
73
|
+
)
|
|
74
|
+
);
|
|
75
|
+
const params = options.params ?? {};
|
|
76
|
+
const serverAuthState = await (0, import_auth.getAuthenticationContext)({
|
|
77
|
+
authDecoder: options.authDecoder,
|
|
78
|
+
event: request,
|
|
79
|
+
context: options.lambdaContext
|
|
80
|
+
});
|
|
81
|
+
return {
|
|
82
|
+
params,
|
|
83
|
+
query,
|
|
84
|
+
cookies,
|
|
85
|
+
serverAuthState
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function composeCedarMiddleware(handler, middleware) {
|
|
89
|
+
return middleware.reduceRight((next, current) => {
|
|
90
|
+
return (request, ctx) => {
|
|
91
|
+
return current(request, ctx, async () => next(request, ctx));
|
|
92
|
+
};
|
|
93
|
+
}, handler);
|
|
94
|
+
}
|
|
95
|
+
function createRouteManifest(routes) {
|
|
96
|
+
return routes.map((route) => {
|
|
97
|
+
return {
|
|
98
|
+
...route,
|
|
99
|
+
methods: [...route.methods]
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function routeManifestToJSON(routes) {
|
|
104
|
+
return JSON.stringify(createRouteManifest(routes), null, 2);
|
|
105
|
+
}
|
|
106
|
+
function wrapLegacyHandler(legacyHandler, options = {}) {
|
|
107
|
+
return async (request, ctx) => {
|
|
108
|
+
const lambdaContext = options.lambdaContext ?? DEFAULT_LAMBDA_CONTEXT;
|
|
109
|
+
const event = await requestToLegacyEvent(request, ctx);
|
|
110
|
+
const result = await legacyHandler(event, lambdaContext);
|
|
111
|
+
return legacyResultToResponse(result);
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
async function requestToLegacyEvent(request, ctx) {
|
|
115
|
+
const url = new URL(request.url);
|
|
116
|
+
const bodyText = await request.clone().text();
|
|
117
|
+
const headers = Object.fromEntries(request.headers.entries());
|
|
118
|
+
const queryStringParameters = (0, import_picoquery.parse)(url.search ? url.search.slice(1) : "", {
|
|
119
|
+
nestingSyntax: "index",
|
|
120
|
+
arrayRepeat: true,
|
|
121
|
+
arrayRepeatSyntax: "bracket"
|
|
122
|
+
});
|
|
123
|
+
return {
|
|
124
|
+
body: bodyText || null,
|
|
125
|
+
headers,
|
|
126
|
+
multiValueHeaders: toMultiValueHeaders(request.headers) ?? {},
|
|
127
|
+
httpMethod: request.method,
|
|
128
|
+
isBase64Encoded: false,
|
|
129
|
+
path: url.pathname,
|
|
130
|
+
pathParameters: Object.keys(ctx.params).length > 0 ? ctx.params : null,
|
|
131
|
+
queryStringParameters,
|
|
132
|
+
multiValueQueryStringParameters: toMultiValueQueryStringParameters(url),
|
|
133
|
+
stageVariables: null,
|
|
134
|
+
requestContext: {
|
|
135
|
+
accountId: "cedar",
|
|
136
|
+
apiId: "cedar",
|
|
137
|
+
authorizer: void 0,
|
|
138
|
+
protocol: "HTTP/1.1",
|
|
139
|
+
identity: {
|
|
140
|
+
accessKey: null,
|
|
141
|
+
accountId: null,
|
|
142
|
+
apiKey: null,
|
|
143
|
+
apiKeyId: null,
|
|
144
|
+
caller: null,
|
|
145
|
+
clientCert: null,
|
|
146
|
+
cognitoAuthenticationProvider: null,
|
|
147
|
+
cognitoAuthenticationType: null,
|
|
148
|
+
cognitoIdentityId: null,
|
|
149
|
+
cognitoIdentityPoolId: null,
|
|
150
|
+
principalOrgId: null,
|
|
151
|
+
sourceIp: "",
|
|
152
|
+
user: null,
|
|
153
|
+
userAgent: request.headers.get("user-agent"),
|
|
154
|
+
userArn: null
|
|
155
|
+
},
|
|
156
|
+
path: url.pathname,
|
|
157
|
+
stage: "",
|
|
158
|
+
requestId: "cedar-request",
|
|
159
|
+
requestTimeEpoch: Date.now(),
|
|
160
|
+
resourceId: "cedar",
|
|
161
|
+
resourcePath: url.pathname,
|
|
162
|
+
httpMethod: request.method
|
|
163
|
+
},
|
|
164
|
+
resource: url.pathname
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function legacyResultToResponse(result) {
|
|
168
|
+
if (result instanceof Response) {
|
|
169
|
+
return result;
|
|
170
|
+
}
|
|
171
|
+
const headers = new Headers();
|
|
172
|
+
if (result.headers) {
|
|
173
|
+
for (const [name, value] of Object.entries(result.headers)) {
|
|
174
|
+
if (Array.isArray(value)) {
|
|
175
|
+
for (const item of value) {
|
|
176
|
+
headers.append(name, item);
|
|
177
|
+
}
|
|
178
|
+
} else if (value !== void 0) {
|
|
179
|
+
headers.set(name, String(value));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (result.multiValueHeaders) {
|
|
184
|
+
for (const [name, values] of Object.entries(result.multiValueHeaders)) {
|
|
185
|
+
if (!values) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
for (const value of values) {
|
|
189
|
+
headers.append(name, String(value));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const body = result.body ?? "";
|
|
194
|
+
if (result.isBase64Encoded) {
|
|
195
|
+
return new Response(Buffer.from(body, "base64"), {
|
|
196
|
+
status: result.statusCode ?? 200,
|
|
197
|
+
headers
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return new Response(body, {
|
|
201
|
+
status: result.statusCode ?? 200,
|
|
202
|
+
headers
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
function toMultiValueHeaders(headers) {
|
|
206
|
+
const values = /* @__PURE__ */ new Map();
|
|
207
|
+
for (const [name, value] of headers.entries()) {
|
|
208
|
+
const existing = values.get(name) ?? [];
|
|
209
|
+
existing.push(value);
|
|
210
|
+
values.set(name, existing);
|
|
211
|
+
}
|
|
212
|
+
if (values.size === 0) {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
return Object.fromEntries(values.entries());
|
|
216
|
+
}
|
|
217
|
+
function toMultiValueQueryStringParameters(url) {
|
|
218
|
+
const values = /* @__PURE__ */ new Map();
|
|
219
|
+
for (const [name, value] of url.searchParams.entries()) {
|
|
220
|
+
const existing = values.get(name) ?? [];
|
|
221
|
+
existing.push(value);
|
|
222
|
+
values.set(name, existing);
|
|
223
|
+
}
|
|
224
|
+
if (values.size === 0) {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
return Object.fromEntries(values.entries());
|
|
228
|
+
}
|
|
229
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
230
|
+
0 && (module.exports = {
|
|
231
|
+
buildCedarContext,
|
|
232
|
+
composeCedarMiddleware,
|
|
233
|
+
createRouteManifest,
|
|
234
|
+
legacyResultToResponse,
|
|
235
|
+
requestToLegacyEvent,
|
|
236
|
+
routeManifestToJSON,
|
|
237
|
+
wrapLegacyHandler
|
|
238
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { APIGatewayProxyEvent, APIGatewayProxyResult, Context as LambdaContext } from 'aws-lambda';
|
|
2
|
+
import { getAuthenticationContext } from './auth/index.js';
|
|
3
|
+
export interface CedarRequestContext {
|
|
4
|
+
params: Record<string, string>;
|
|
5
|
+
query: URLSearchParams;
|
|
6
|
+
cookies: ReadonlyMap<string, string>;
|
|
7
|
+
serverAuthState?: Awaited<ReturnType<typeof getAuthenticationContext>>;
|
|
8
|
+
}
|
|
9
|
+
export type CedarHandler = (request: Request, ctx: CedarRequestContext) => Promise<Response> | Response;
|
|
10
|
+
export type CedarMiddleware = (request: Request, ctx: CedarRequestContext, next: () => Promise<Response>) => Promise<Response>;
|
|
11
|
+
export interface CedarRouteRecord {
|
|
12
|
+
path: string;
|
|
13
|
+
methods: string[];
|
|
14
|
+
type: 'graphql' | 'auth' | 'function' | 'health';
|
|
15
|
+
entry: string;
|
|
16
|
+
}
|
|
17
|
+
export interface BuildCedarContextOptions {
|
|
18
|
+
params?: Record<string, string>;
|
|
19
|
+
authDecoder?: Parameters<typeof getAuthenticationContext>[0]['authDecoder'];
|
|
20
|
+
lambdaContext?: LambdaContext;
|
|
21
|
+
}
|
|
22
|
+
export interface LegacyHandlerContext {
|
|
23
|
+
event: APIGatewayProxyEvent;
|
|
24
|
+
context: LambdaContext;
|
|
25
|
+
request: Request;
|
|
26
|
+
cedarContext: CedarRequestContext;
|
|
27
|
+
}
|
|
28
|
+
export type LegacyHandlerResult = APIGatewayProxyResult | Response;
|
|
29
|
+
export type LegacyHandler = (event: APIGatewayProxyEvent, context: LambdaContext) => Promise<LegacyHandlerResult> | LegacyHandlerResult;
|
|
30
|
+
export declare function buildCedarContext(request: Request, options?: BuildCedarContextOptions): Promise<CedarRequestContext>;
|
|
31
|
+
export declare function composeCedarMiddleware(handler: CedarHandler, middleware: CedarMiddleware[]): CedarHandler;
|
|
32
|
+
export declare function createRouteManifest(routes: CedarRouteRecord[]): CedarRouteRecord[];
|
|
33
|
+
export declare function routeManifestToJSON(routes: CedarRouteRecord[]): string;
|
|
34
|
+
export declare function wrapLegacyHandler(legacyHandler: LegacyHandler, options?: BuildCedarContextOptions): CedarHandler;
|
|
35
|
+
export declare function requestToLegacyEvent(request: Request, ctx: CedarRequestContext): Promise<APIGatewayProxyEvent>;
|
|
36
|
+
export declare function legacyResultToResponse(result: LegacyHandlerResult): Response;
|
|
37
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EAEpB,qBAAqB,EACrB,OAAO,IAAI,aAAa,EACzB,MAAM,YAAY,CAAA;AAInB,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAA;AAE1D,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC9B,KAAK,EAAE,eAAe,CAAA;IACtB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BvD,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,wBAA6B,GACrC,OAAO,CAAC,mBAAmB,CAAC,CAwB9B;AAED,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,YAAY,EACrB,UAAU,EAAE,eAAe,EAAE,GAC5B,YAAY,CAMd;AAED,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,gBAAgB,EAAE,GACzB,gBAAgB,EAAE,CAOpB;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,gBAAgB,EAAE,GAAG,MAAM,CAEtE;AAED,wBAAgB,iBAAiB,CAC/B,aAAa,EAAE,aAAa,EAC5B,OAAO,GAAE,wBAA6B,GACrC,YAAY,CAQd;AAED,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,oBAAoB,CAAC,CA0D/B;AAED,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,mBAAmB,GAAG,QAAQ,CA4C5E"}
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import * as cookie from "cookie";
|
|
2
|
+
import { parse } from "picoquery";
|
|
3
|
+
import { getAuthenticationContext } from "./auth/index.js";
|
|
4
|
+
const DEFAULT_LAMBDA_CONTEXT = {
|
|
5
|
+
callbackWaitsForEmptyEventLoop: false,
|
|
6
|
+
functionName: "cedar",
|
|
7
|
+
functionVersion: "$LATEST",
|
|
8
|
+
invokedFunctionArn: "cedar",
|
|
9
|
+
memoryLimitInMB: "0",
|
|
10
|
+
awsRequestId: "cedar-request",
|
|
11
|
+
logGroupName: "cedar",
|
|
12
|
+
logStreamName: "cedar",
|
|
13
|
+
getRemainingTimeInMillis() {
|
|
14
|
+
return 0;
|
|
15
|
+
},
|
|
16
|
+
done() {
|
|
17
|
+
return void 0;
|
|
18
|
+
},
|
|
19
|
+
fail() {
|
|
20
|
+
return void 0;
|
|
21
|
+
},
|
|
22
|
+
succeed() {
|
|
23
|
+
return void 0;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
async function buildCedarContext(request, options = {}) {
|
|
27
|
+
const url = new URL(request.url);
|
|
28
|
+
const query = url.searchParams;
|
|
29
|
+
const cookies = new Map(
|
|
30
|
+
Object.entries(cookie.parse(request.headers.get("cookie") ?? "")).filter(
|
|
31
|
+
(entry) => {
|
|
32
|
+
return entry[1] !== void 0;
|
|
33
|
+
}
|
|
34
|
+
)
|
|
35
|
+
);
|
|
36
|
+
const params = options.params ?? {};
|
|
37
|
+
const serverAuthState = await getAuthenticationContext({
|
|
38
|
+
authDecoder: options.authDecoder,
|
|
39
|
+
event: request,
|
|
40
|
+
context: options.lambdaContext
|
|
41
|
+
});
|
|
42
|
+
return {
|
|
43
|
+
params,
|
|
44
|
+
query,
|
|
45
|
+
cookies,
|
|
46
|
+
serverAuthState
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function composeCedarMiddleware(handler, middleware) {
|
|
50
|
+
return middleware.reduceRight((next, current) => {
|
|
51
|
+
return (request, ctx) => {
|
|
52
|
+
return current(request, ctx, async () => next(request, ctx));
|
|
53
|
+
};
|
|
54
|
+
}, handler);
|
|
55
|
+
}
|
|
56
|
+
function createRouteManifest(routes) {
|
|
57
|
+
return routes.map((route) => {
|
|
58
|
+
return {
|
|
59
|
+
...route,
|
|
60
|
+
methods: [...route.methods]
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function routeManifestToJSON(routes) {
|
|
65
|
+
return JSON.stringify(createRouteManifest(routes), null, 2);
|
|
66
|
+
}
|
|
67
|
+
function wrapLegacyHandler(legacyHandler, options = {}) {
|
|
68
|
+
return async (request, ctx) => {
|
|
69
|
+
const lambdaContext = options.lambdaContext ?? DEFAULT_LAMBDA_CONTEXT;
|
|
70
|
+
const event = await requestToLegacyEvent(request, ctx);
|
|
71
|
+
const result = await legacyHandler(event, lambdaContext);
|
|
72
|
+
return legacyResultToResponse(result);
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
async function requestToLegacyEvent(request, ctx) {
|
|
76
|
+
const url = new URL(request.url);
|
|
77
|
+
const bodyText = await request.clone().text();
|
|
78
|
+
const headers = Object.fromEntries(request.headers.entries());
|
|
79
|
+
const queryStringParameters = parse(url.search ? url.search.slice(1) : "", {
|
|
80
|
+
nestingSyntax: "index",
|
|
81
|
+
arrayRepeat: true,
|
|
82
|
+
arrayRepeatSyntax: "bracket"
|
|
83
|
+
});
|
|
84
|
+
return {
|
|
85
|
+
body: bodyText || null,
|
|
86
|
+
headers,
|
|
87
|
+
multiValueHeaders: toMultiValueHeaders(request.headers) ?? {},
|
|
88
|
+
httpMethod: request.method,
|
|
89
|
+
isBase64Encoded: false,
|
|
90
|
+
path: url.pathname,
|
|
91
|
+
pathParameters: Object.keys(ctx.params).length > 0 ? ctx.params : null,
|
|
92
|
+
queryStringParameters,
|
|
93
|
+
multiValueQueryStringParameters: toMultiValueQueryStringParameters(url),
|
|
94
|
+
stageVariables: null,
|
|
95
|
+
requestContext: {
|
|
96
|
+
accountId: "cedar",
|
|
97
|
+
apiId: "cedar",
|
|
98
|
+
authorizer: void 0,
|
|
99
|
+
protocol: "HTTP/1.1",
|
|
100
|
+
identity: {
|
|
101
|
+
accessKey: null,
|
|
102
|
+
accountId: null,
|
|
103
|
+
apiKey: null,
|
|
104
|
+
apiKeyId: null,
|
|
105
|
+
caller: null,
|
|
106
|
+
clientCert: null,
|
|
107
|
+
cognitoAuthenticationProvider: null,
|
|
108
|
+
cognitoAuthenticationType: null,
|
|
109
|
+
cognitoIdentityId: null,
|
|
110
|
+
cognitoIdentityPoolId: null,
|
|
111
|
+
principalOrgId: null,
|
|
112
|
+
sourceIp: "",
|
|
113
|
+
user: null,
|
|
114
|
+
userAgent: request.headers.get("user-agent"),
|
|
115
|
+
userArn: null
|
|
116
|
+
},
|
|
117
|
+
path: url.pathname,
|
|
118
|
+
stage: "",
|
|
119
|
+
requestId: "cedar-request",
|
|
120
|
+
requestTimeEpoch: Date.now(),
|
|
121
|
+
resourceId: "cedar",
|
|
122
|
+
resourcePath: url.pathname,
|
|
123
|
+
httpMethod: request.method
|
|
124
|
+
},
|
|
125
|
+
resource: url.pathname
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function legacyResultToResponse(result) {
|
|
129
|
+
if (result instanceof Response) {
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
const headers = new Headers();
|
|
133
|
+
if (result.headers) {
|
|
134
|
+
for (const [name, value] of Object.entries(result.headers)) {
|
|
135
|
+
if (Array.isArray(value)) {
|
|
136
|
+
for (const item of value) {
|
|
137
|
+
headers.append(name, item);
|
|
138
|
+
}
|
|
139
|
+
} else if (value !== void 0) {
|
|
140
|
+
headers.set(name, String(value));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (result.multiValueHeaders) {
|
|
145
|
+
for (const [name, values] of Object.entries(result.multiValueHeaders)) {
|
|
146
|
+
if (!values) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
for (const value of values) {
|
|
150
|
+
headers.append(name, String(value));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const body = result.body ?? "";
|
|
155
|
+
if (result.isBase64Encoded) {
|
|
156
|
+
return new Response(Buffer.from(body, "base64"), {
|
|
157
|
+
status: result.statusCode ?? 200,
|
|
158
|
+
headers
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return new Response(body, {
|
|
162
|
+
status: result.statusCode ?? 200,
|
|
163
|
+
headers
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
function toMultiValueHeaders(headers) {
|
|
167
|
+
const values = /* @__PURE__ */ new Map();
|
|
168
|
+
for (const [name, value] of headers.entries()) {
|
|
169
|
+
const existing = values.get(name) ?? [];
|
|
170
|
+
existing.push(value);
|
|
171
|
+
values.set(name, existing);
|
|
172
|
+
}
|
|
173
|
+
if (values.size === 0) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
return Object.fromEntries(values.entries());
|
|
177
|
+
}
|
|
178
|
+
function toMultiValueQueryStringParameters(url) {
|
|
179
|
+
const values = /* @__PURE__ */ new Map();
|
|
180
|
+
for (const [name, value] of url.searchParams.entries()) {
|
|
181
|
+
const existing = values.get(name) ?? [];
|
|
182
|
+
existing.push(value);
|
|
183
|
+
values.set(name, existing);
|
|
184
|
+
}
|
|
185
|
+
if (values.size === 0) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
return Object.fromEntries(values.entries());
|
|
189
|
+
}
|
|
190
|
+
export {
|
|
191
|
+
buildCedarContext,
|
|
192
|
+
composeCedarMiddleware,
|
|
193
|
+
createRouteManifest,
|
|
194
|
+
legacyResultToResponse,
|
|
195
|
+
requestToLegacyEvent,
|
|
196
|
+
routeManifestToJSON,
|
|
197
|
+
wrapLegacyHandler
|
|
198
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cedarjs/api",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.1-next.67+ad97a60c79",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/cedarjs/cedar.git",
|
|
@@ -20,6 +20,16 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"./package.json": "./package.json",
|
|
23
|
+
"./runtime": {
|
|
24
|
+
"import": {
|
|
25
|
+
"types": "./dist/runtime.d.ts",
|
|
26
|
+
"default": "./dist/runtime.js"
|
|
27
|
+
},
|
|
28
|
+
"require": {
|
|
29
|
+
"types": "./dist/cjs/runtime.d.ts",
|
|
30
|
+
"default": "./dist/cjs/runtime.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
23
33
|
"./auth": {
|
|
24
34
|
"import": {
|
|
25
35
|
"types": "./dist/auth/index.d.ts",
|
|
@@ -93,11 +103,12 @@
|
|
|
93
103
|
"humanize-string": "2.1.0",
|
|
94
104
|
"jsonwebtoken": "9.0.3",
|
|
95
105
|
"pascalcase": "1.0.0",
|
|
106
|
+
"picoquery": "2.5.0",
|
|
96
107
|
"pino": "9.7.0",
|
|
97
108
|
"title-case": "3.0.3"
|
|
98
109
|
},
|
|
99
110
|
"devDependencies": {
|
|
100
|
-
"@cedarjs/framework-tools": "
|
|
111
|
+
"@cedarjs/framework-tools": "4.0.1-next.67",
|
|
101
112
|
"@types/aws-lambda": "8.10.161",
|
|
102
113
|
"@types/jsonwebtoken": "9.0.10",
|
|
103
114
|
"@types/memjs": "1",
|
|
@@ -106,7 +117,7 @@
|
|
|
106
117
|
"concurrently": "9.2.1",
|
|
107
118
|
"memjs": "1.3.2",
|
|
108
119
|
"publint": "0.3.18",
|
|
109
|
-
"redis": "5.
|
|
120
|
+
"redis": "5.12.1",
|
|
110
121
|
"split2": "4.2.0",
|
|
111
122
|
"ts-toolbelt": "9.6.0",
|
|
112
123
|
"tsx": "4.21.0",
|
|
@@ -115,7 +126,7 @@
|
|
|
115
126
|
},
|
|
116
127
|
"peerDependencies": {
|
|
117
128
|
"memjs": "1.3.2",
|
|
118
|
-
"redis": "5.
|
|
129
|
+
"redis": "5.12.1"
|
|
119
130
|
},
|
|
120
131
|
"peerDependenciesMeta": {
|
|
121
132
|
"memjs": {
|
|
@@ -131,5 +142,5 @@
|
|
|
131
142
|
"publishConfig": {
|
|
132
143
|
"access": "public"
|
|
133
144
|
},
|
|
134
|
-
"gitHead": "
|
|
145
|
+
"gitHead": "ad97a60c793c21b3263b4178e339d89406f7a7a3"
|
|
135
146
|
}
|