-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.ts
145 lines (138 loc) · 4.67 KB
/
lambda.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* eslint-disable camelcase */
/* eslint-disable @typescript-eslint/no-explicit-any */
import transform from 'immuton/transform';
import type { HttpMethod, HttpRequest, HttpRequestHeaders, HttpResponse, HttpStatus } from './http';
export interface LambdaCallback {
(error: null | undefined, result: LambdaHttpResponse): void;
(error: Error, result?: null): void;
}
export interface LambdaHttpRequestContext {
accountId: string;
resourceId: string;
stage: string;
requestId: string;
identity: {
cognitoIdentityPoolId: string;
accountId: string;
cognitoIdentityId: string;
caller: string;
apiKey: string;
sourceIp: string;
cognitoAuthenticationType: string;
cognitoAuthenticationProvider: string;
userArn: string;
userAgent: string;
user: string;
};
resourcePath: string;
httpMethod: string;
apiId: string;
authorizer?: {
claims?: {
'aud': string;
'cognito:groups': string;
'cognito:username': string;
'email': string;
'exp': string;
'iat': string;
'identities': string;
'iss': string;
'name': string;
'sub': string;
'picture': string | null;
'token_use': string;
};
};
}
export interface LambdaHttpRequest {
resource: string;
httpMethod: HttpMethod;
path: string;
queryStringParameters: { [parameter: string]: string };
pathParameters: { [parameter: string]: string };
headers: HttpRequestHeaders;
stageVariables: { [variable: string]: string } | void;
requestContext: LambdaHttpRequestContext;
body?: string;
isBase64Encoded?: boolean;
}
export interface LambdaHttpResponse {
statusCode: HttpStatus;
multiValueHeaders: { [header: string]: string[] };
isBase64Encoded: boolean;
body: string;
}
export type LambdaHttpHandler = (
request: LambdaHttpRequest,
context: LambdaHttpRequestContext,
) => Promise<LambdaHttpResponse>;
export function lambdaMiddleware<P extends any[]>(
handler: (request: HttpRequest, ...params: P) => Promise<HttpResponse>,
): (request: LambdaHttpRequest, ...params: P) => Promise<LambdaHttpResponse> {
async function handleLambdaRequest(lambdaRequest: LambdaHttpRequest, ...params: P): Promise<LambdaHttpResponse> {
const { httpMethod, isBase64Encoded, body } = lambdaRequest;
const queryParameters = lambdaRequest.queryStringParameters || {};
const headers = lambdaRequest.headers || {};
const bodyBuffer = body == null ? body : Buffer.from(body, isBase64Encoded ? 'base64' : 'utf8');
const environment = lambdaRequest.stageVariables || {};
const region = environment.Region;
if (!region) {
throw new Error(`The Region stage variable is missing!`);
}
const serverOrigin = environment.ServerOrigin;
if (!serverOrigin) {
throw new Error(`The ServerOrigin stage variable is missing!`);
}
const serverRoot = environment.ServerRoot;
if (!serverRoot) {
throw new Error(`The ServerRoot stage variable is missing!`);
}
const request = {
method: httpMethod,
path: lambdaRequest.path,
queryParameters,
headers,
body: bodyBuffer,
environment,
region,
serverRoot,
serverOrigin,
// Auth will be set by another middleware
auth: null,
// Read the directory path from environment variables
// directoryPath: process.env.LAMBDA_TASK_ROOT as string,
};
const response = await handler(request, ...params);
const responseHeaders = transform(response.headers, (headerValue) =>
Array.isArray(headerValue) ? headerValue : [headerValue],
);
return {
statusCode: response.statusCode,
multiValueHeaders: responseHeaders,
isBase64Encoded: false,
body: response.body,
};
}
return handleLambdaRequest;
}
export interface LambdaS3Event {
Records: {
eventName: string;
eventTime: string;
s3: {
bucket: {
name: string;
// arn: string;
};
object: {
key: string;
size: number;
// eTag: string;
// versionId: string | null;
// sequencer?: string | null;
};
};
}[];
}
export type LambdaEvent = LambdaS3Event;
export type LambdaEventHandler = (event: LambdaEvent) => Promise<void>;