hono-adapter-aws-lambda 0.2.7
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/LICENSE +21 -0
- package/README.md +65 -0
- package/dist/index.d.mts +35 -0
- package/dist/index.mjs +385 -0
- package/package.json +88 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 NamesMT <https://github.com/namesmt>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# hono-adapter-aws-lambda 
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![Codecov][codecov-src]][codecov-href]
|
|
6
|
+
[![Bundlejs][bundlejs-src]][bundlejs-href]
|
|
7
|
+
[![jsDocs.io][jsDocs-src]][jsDocs-href]
|
|
8
|
+
|
|
9
|
+
**hono-adapter-aws-lambda** is a fork of [hono](https://hono.dev/)'s `aws-lambda` adapter, experimenting and adding some extra features
|
|
10
|
+
|
|
11
|
+
## Features & Roadmap
|
|
12
|
+
- [x] add router support for trigger events.
|
|
13
|
+
- > I.e, support for S3, SQS, etc. triggers, which would also support a simpler cross-function call interface.
|
|
14
|
+
- ~~Support is added with a few notices~~
|
|
15
|
+
- ~~Must use `getTriggerPath()` when defining a trigger route~~
|
|
16
|
+
- ~~Must use `fixTriggerRoute()` to support basePath / grouping~~
|
|
17
|
+
- A refactor of the trigger routing support have been released, it now supports multiple routes on the same eventSource, uses a factory pattern, and decoupled the trigger context (middlewares, env bindings) from our main Hono app, see [#10](https://github.com/NamesMT/hono-adapter-aws-lambda/issues/10) for more information.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
### Install package:
|
|
21
|
+
```sh
|
|
22
|
+
# pnpm (recommended)
|
|
23
|
+
pnpm install hono-adapter-aws-lambda
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Import:
|
|
27
|
+
```ts
|
|
28
|
+
// ESM
|
|
29
|
+
import { handle, streamHandle } from 'hono-adapter-aws-lambda'
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Examples:
|
|
33
|
+
Fast example of accepting an S3 trigger event
|
|
34
|
+
```ts
|
|
35
|
+
import type { S3Event } from 'aws-lambda' // You need to install `@types/aws-lambda`
|
|
36
|
+
import { createTriggerFactory, handle, streamHandle } from 'hono-adapter-aws-lambda'
|
|
37
|
+
|
|
38
|
+
interface Bindings {
|
|
39
|
+
event: { Records: Array<{ eventName: string }> }
|
|
40
|
+
}
|
|
41
|
+
const app = new Hono<{ Bindings: Bindings }>()
|
|
42
|
+
const triggerFactory = createTriggerFactory(app)
|
|
43
|
+
|
|
44
|
+
triggerFactory.on('aws:s3', '$!', c => c.text((c.env.event as S3Event).Records[0].eventName))
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
See some more examples in the test file: [test/index.test.ts](test/index.test.ts)
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
[MIT](./LICENSE) License © 2024 [NamesMT](https://github.com/NamesMT)
|
|
51
|
+
|
|
52
|
+
<!-- Badges -->
|
|
53
|
+
|
|
54
|
+
[npm-version-src]: https://img.shields.io/npm/v/hono-adapter-aws-lambda?labelColor=18181B&color=F0DB4F
|
|
55
|
+
[npm-version-href]: https://npmjs.com/package/hono-adapter-aws-lambda
|
|
56
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/hono-adapter-aws-lambda?labelColor=18181B&color=F0DB4F
|
|
57
|
+
[npm-downloads-href]: https://npmjs.com/package/hono-adapter-aws-lambda
|
|
58
|
+
[codecov-src]: https://img.shields.io/codecov/c/gh/namesmt/hono-adapter-aws-lambda/main?labelColor=18181B&color=F0DB4F
|
|
59
|
+
[codecov-href]: https://codecov.io/gh/namesmt/hono-adapter-aws-lambda
|
|
60
|
+
[license-src]: https://img.shields.io/github/license/namesmt/hono-adapter-aws-lambda.svg?labelColor=18181B&color=F0DB4F
|
|
61
|
+
[license-href]: https://github.com/namesmt/hono-adapter-aws-lambda/blob/main/LICENSE
|
|
62
|
+
[bundlejs-src]: https://img.shields.io/bundlejs/size/hono-adapter-aws-lambda?labelColor=18181B&color=F0DB4F
|
|
63
|
+
[bundlejs-href]: https://bundlejs.com/?q=hono-adapter-aws-lambda
|
|
64
|
+
[jsDocs-src]: https://img.shields.io/badge/Check_out-jsDocs.io---?labelColor=18181B&color=F0DB4F
|
|
65
|
+
[jsDocs-href]: https://www.jsdocs.io/package/hono-adapter-aws-lambda
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Env, Schema, Hono } from 'hono';
|
|
2
|
+
import { LambdaEvent, LambdaTriggerEvent } from '@namesmt/utils-lambda';
|
|
3
|
+
export { LambdaEvent, LambdaRequestEvent, LambdaTriggerEvent } from '@namesmt/utils-lambda';
|
|
4
|
+
import { Handler, APIGatewayProxyResult, APIGatewayProxyStructuredResultV2 } from 'aws-lambda';
|
|
5
|
+
export { Context as LambdaContext } from 'aws-lambda';
|
|
6
|
+
import * as hono_types from 'hono/types';
|
|
7
|
+
import { H } from 'hono/types';
|
|
8
|
+
|
|
9
|
+
type LambdaHandler<TEvent = any, TResult = any> = Handler<TEvent, TResult>;
|
|
10
|
+
type LambdaHandlerResult = APIGatewayProxyResult | APIGatewayProxyStructuredResultV2;
|
|
11
|
+
|
|
12
|
+
declare function streamHandle<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(app: Hono<E, S, BasePath>): LambdaHandler<LambdaEvent>;
|
|
13
|
+
/**
|
|
14
|
+
* Accepts events from API Gateway/ELB(`APIGatewayProxyEvent`) and directly through Function Url(`APIGatewayProxyEventV2`)
|
|
15
|
+
*/
|
|
16
|
+
declare function handle<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(app: Hono<E, S, BasePath>): LambdaHandler<LambdaEvent, LambdaHandlerResult>;
|
|
17
|
+
|
|
18
|
+
declare class TriggerFactory<IE extends Env, HE extends Env> {
|
|
19
|
+
private simpleRouter;
|
|
20
|
+
honoApp: Hono<HE>;
|
|
21
|
+
internalApp: Hono<IE, hono_types.BlankSchema, "/">;
|
|
22
|
+
constructor(app: Hono<HE>);
|
|
23
|
+
on: (eventSource: string, id: string, ...handlers: H<IE>[]) => this;
|
|
24
|
+
}
|
|
25
|
+
type ExtractHonoEnv<A extends Hono> = A extends Hono<infer E> ? E : never;
|
|
26
|
+
declare function createTriggerFactory<E extends Env, A extends Hono<any, any, '/'>>(app: A): TriggerFactory<E extends unknown ? {
|
|
27
|
+
Bindings: {
|
|
28
|
+
event: LambdaTriggerEvent;
|
|
29
|
+
};
|
|
30
|
+
Variables: Record<string, unknown>;
|
|
31
|
+
} : E, ExtractHonoEnv<A>>;
|
|
32
|
+
declare const triggerPathUUID: string;
|
|
33
|
+
declare function getTriggerPath(path: string): string;
|
|
34
|
+
|
|
35
|
+
export { type LambdaHandler, type LambdaHandlerResult, TriggerFactory, createTriggerFactory, getTriggerPath, handle, streamHandle, triggerPathUUID };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import { Readable } from 'node:stream';
|
|
3
|
+
import { pipeline } from 'node:stream/promises';
|
|
4
|
+
import { decodeBase64, encodeBase64 } from 'hono/utils/encode';
|
|
5
|
+
import { Hono } from 'hono';
|
|
6
|
+
import { mergePath } from 'hono/utils/url';
|
|
7
|
+
|
|
8
|
+
class RequestEventProcessor {
|
|
9
|
+
createRequest(event) {
|
|
10
|
+
const queryString = this.getQueryString(event);
|
|
11
|
+
const domainName = event.requestContext && "domainName" in event.requestContext ? event.requestContext.domainName : event.headers?.host ?? event.multiValueHeaders?.host?.[0];
|
|
12
|
+
const path = this.getPath(event);
|
|
13
|
+
const urlPath = `https://${domainName}${path}`;
|
|
14
|
+
const url = queryString ? `${urlPath}?${queryString}` : urlPath;
|
|
15
|
+
const headers = this.getHeaders(event);
|
|
16
|
+
const method = this.getMethod(event);
|
|
17
|
+
const requestInit = {
|
|
18
|
+
headers,
|
|
19
|
+
method
|
|
20
|
+
};
|
|
21
|
+
if (event.body) {
|
|
22
|
+
requestInit.body = event.isBase64Encoded ? decodeBase64(event.body) : event.body;
|
|
23
|
+
}
|
|
24
|
+
return new Request(url, requestInit);
|
|
25
|
+
}
|
|
26
|
+
async createResult(event, res) {
|
|
27
|
+
const contentType = res.headers.get("content-type");
|
|
28
|
+
let isBase64Encoded = !!(contentType && isContentTypeBinary(contentType));
|
|
29
|
+
if (!isBase64Encoded) {
|
|
30
|
+
const contentEncoding = res.headers.get("content-encoding");
|
|
31
|
+
isBase64Encoded = isContentEncodingBinary(contentEncoding);
|
|
32
|
+
}
|
|
33
|
+
const body = isBase64Encoded ? encodeBase64(await res.arrayBuffer()) : await res.text();
|
|
34
|
+
const result = {
|
|
35
|
+
body,
|
|
36
|
+
headers: {},
|
|
37
|
+
multiValueHeaders: void 0,
|
|
38
|
+
statusCode: res.status,
|
|
39
|
+
isBase64Encoded
|
|
40
|
+
};
|
|
41
|
+
if ("multiValueHeaders" in event) {
|
|
42
|
+
result.multiValueHeaders = {};
|
|
43
|
+
}
|
|
44
|
+
this.setCookies(event, res, result);
|
|
45
|
+
res.headers.forEach((value, key) => {
|
|
46
|
+
result.headers[key] = value;
|
|
47
|
+
if ("multiValueHeaders" in event) {
|
|
48
|
+
result.multiValueHeaders[key] = [value];
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
setCookies(event, res, result) {
|
|
54
|
+
if (res.headers.has("set-cookie")) {
|
|
55
|
+
const cookies = res.headers.getSetCookie ? res.headers.getSetCookie() : Array.from(res.headers.entries()).filter(([k]) => k === "set-cookie").map(([, v]) => v);
|
|
56
|
+
if (Array.isArray(cookies)) {
|
|
57
|
+
this.setCookiesToResult(event, result, cookies);
|
|
58
|
+
res.headers.delete("set-cookie");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
class EventV2Processor extends RequestEventProcessor {
|
|
64
|
+
getPath(event) {
|
|
65
|
+
return event.rawPath;
|
|
66
|
+
}
|
|
67
|
+
getMethod(event) {
|
|
68
|
+
return event.requestContext.http.method;
|
|
69
|
+
}
|
|
70
|
+
getQueryString(event) {
|
|
71
|
+
return event.rawQueryString;
|
|
72
|
+
}
|
|
73
|
+
getCookies(event, headers) {
|
|
74
|
+
if (Array.isArray(event.cookies)) {
|
|
75
|
+
headers.set("Cookie", event.cookies.join("; "));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
setCookiesToResult(_, result, cookies) {
|
|
79
|
+
result.cookies = cookies;
|
|
80
|
+
}
|
|
81
|
+
getHeaders(event) {
|
|
82
|
+
const headers = new Headers();
|
|
83
|
+
this.getCookies(event, headers);
|
|
84
|
+
if (event.headers) {
|
|
85
|
+
for (const [k, v] of Object.entries(event.headers)) {
|
|
86
|
+
if (v) {
|
|
87
|
+
headers.set(k, v);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return headers;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const v2Processor = new EventV2Processor();
|
|
95
|
+
class EventV1Processor extends RequestEventProcessor {
|
|
96
|
+
getPath(event) {
|
|
97
|
+
return event.path;
|
|
98
|
+
}
|
|
99
|
+
getMethod(event) {
|
|
100
|
+
return event.httpMethod;
|
|
101
|
+
}
|
|
102
|
+
getQueryString(event) {
|
|
103
|
+
return Object.entries(event.queryStringParameters || {}).filter(([, value]) => value).map(([key, value]) => `${key}=${value}`).join("&");
|
|
104
|
+
}
|
|
105
|
+
getCookies(event, headers) {
|
|
106
|
+
}
|
|
107
|
+
getHeaders(event) {
|
|
108
|
+
const headers = new Headers();
|
|
109
|
+
this.getCookies(event, headers);
|
|
110
|
+
if (event.headers) {
|
|
111
|
+
for (const [k, v] of Object.entries(event.headers)) {
|
|
112
|
+
if (v) {
|
|
113
|
+
headers.set(k, v);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (event.multiValueHeaders) {
|
|
118
|
+
for (const [k, values] of Object.entries(event.multiValueHeaders)) {
|
|
119
|
+
if (values) {
|
|
120
|
+
const foundK = headers.get(k);
|
|
121
|
+
values.forEach((v) => (!foundK || !foundK.includes(v)) && headers.append(k, v));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return headers;
|
|
126
|
+
}
|
|
127
|
+
setCookiesToResult(_, result, cookies) {
|
|
128
|
+
result.multiValueHeaders = {
|
|
129
|
+
"set-cookie": cookies
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const v1Processor = new EventV1Processor();
|
|
134
|
+
class ALBProcessor extends RequestEventProcessor {
|
|
135
|
+
getHeaders(event) {
|
|
136
|
+
const headers = new Headers();
|
|
137
|
+
if (event.multiValueHeaders) {
|
|
138
|
+
for (const [key, values] of Object.entries(event.multiValueHeaders)) {
|
|
139
|
+
if (values && Array.isArray(values)) {
|
|
140
|
+
headers.set(key, values.join("; "));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
for (const [key, value] of Object.entries(event.headers ?? {})) {
|
|
145
|
+
if (value) {
|
|
146
|
+
headers.set(key, value);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return headers;
|
|
151
|
+
}
|
|
152
|
+
getPath(event) {
|
|
153
|
+
return event.path;
|
|
154
|
+
}
|
|
155
|
+
getMethod(event) {
|
|
156
|
+
return event.httpMethod;
|
|
157
|
+
}
|
|
158
|
+
getQueryString(event) {
|
|
159
|
+
if (event.multiValueQueryStringParameters) {
|
|
160
|
+
return Object.entries(event.multiValueQueryStringParameters || {}).filter(([, value]) => value).map(([key, value]) => `${key}=${value?.join(`&${key}=`)}`).join("&");
|
|
161
|
+
} else {
|
|
162
|
+
return Object.entries(event.queryStringParameters || {}).filter(([, value]) => value).map(([key, value]) => `${key}=${value}`).join("&");
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
getCookies(event, headers) {
|
|
166
|
+
let cookie;
|
|
167
|
+
if (event.multiValueHeaders) {
|
|
168
|
+
cookie = event.multiValueHeaders.cookie?.join("; ");
|
|
169
|
+
} else {
|
|
170
|
+
cookie = event.headers ? event.headers.cookie : void 0;
|
|
171
|
+
}
|
|
172
|
+
if (cookie) {
|
|
173
|
+
headers.append("Cookie", cookie);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
setCookiesToResult(event, result, cookies) {
|
|
177
|
+
if (event.multiValueHeaders && result.multiValueHeaders) {
|
|
178
|
+
result.multiValueHeaders["set-cookie"] = cookies;
|
|
179
|
+
} else {
|
|
180
|
+
result.headers["set-cookie"] = cookies.join(", ");
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const albProcessor = new ALBProcessor();
|
|
185
|
+
function isProxyEventALB(event) {
|
|
186
|
+
return Object.hasOwn(event, "requestContext") && Object.hasOwn(event.requestContext, "elb");
|
|
187
|
+
}
|
|
188
|
+
function isProxyEventV2(event) {
|
|
189
|
+
return Object.hasOwn(event, "rawPath");
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
var __defProp = Object.defineProperty;
|
|
193
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
194
|
+
var __publicField = (obj, key, value) => {
|
|
195
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
196
|
+
return value;
|
|
197
|
+
};
|
|
198
|
+
const METHOD = "TRIGGER";
|
|
199
|
+
class TriggerFactory {
|
|
200
|
+
constructor(app) {
|
|
201
|
+
__publicField(this, "simpleRouter", {});
|
|
202
|
+
__publicField(this, "honoApp");
|
|
203
|
+
__publicField(this, "internalApp", new Hono());
|
|
204
|
+
__publicField(this, "on", (eventSource, id, ...handlers) => {
|
|
205
|
+
let thisEventSource = this.simpleRouter[eventSource];
|
|
206
|
+
if (!thisEventSource) {
|
|
207
|
+
this.simpleRouter[eventSource] = thisEventSource = {};
|
|
208
|
+
this.honoApp.on(METHOD, getTriggerPath(eventSource), async (c) => {
|
|
209
|
+
if (thisEventSource["$!"]) {
|
|
210
|
+
return this.internalApp.fetch(makeLocalRequest(METHOD, `/${eventSource}/$!`));
|
|
211
|
+
}
|
|
212
|
+
const resObj = {};
|
|
213
|
+
for (const route in thisEventSource) {
|
|
214
|
+
if (route[0] === "$")
|
|
215
|
+
continue;
|
|
216
|
+
const res = await this.internalApp.fetch(makeLocalRequest(METHOD, `/${eventSource}/${route}`));
|
|
217
|
+
resObj[route] = /^application\/json/.test(res.headers.get("content-type") || "") ? await res.json() : await res.text();
|
|
218
|
+
}
|
|
219
|
+
if (thisEventSource["$="]) {
|
|
220
|
+
return this.internalApp.fetch(makeLocalRequest(METHOD, `/${eventSource}/$=`));
|
|
221
|
+
}
|
|
222
|
+
return c.json(resObj);
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (thisEventSource[id])
|
|
226
|
+
throw new Error(`Route ID "${id}" already exists for event source "${eventSource}"`);
|
|
227
|
+
this.internalApp.on(METHOD, `/${eventSource}/${id}`, ...handlers);
|
|
228
|
+
thisEventSource[id] = true;
|
|
229
|
+
return this;
|
|
230
|
+
});
|
|
231
|
+
this.honoApp = app;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function createTriggerFactory(app) {
|
|
235
|
+
return new TriggerFactory(app);
|
|
236
|
+
}
|
|
237
|
+
class TriggerEventProcessor {
|
|
238
|
+
createRequest(event) {
|
|
239
|
+
const path = getTriggerPath(getEventSource(event));
|
|
240
|
+
return makeLocalRequest(METHOD, path);
|
|
241
|
+
}
|
|
242
|
+
async createResult(event, res) {
|
|
243
|
+
const contentType = res.headers.get("content-type");
|
|
244
|
+
let isBase64Encoded = !!(contentType && isContentTypeBinary(contentType));
|
|
245
|
+
if (!isBase64Encoded) {
|
|
246
|
+
const contentEncoding = res.headers.get("content-encoding");
|
|
247
|
+
isBase64Encoded = isContentEncodingBinary(contentEncoding);
|
|
248
|
+
}
|
|
249
|
+
const body = isBase64Encoded ? encodeBase64(await res.arrayBuffer()) : await res.text();
|
|
250
|
+
const result = {
|
|
251
|
+
body,
|
|
252
|
+
headers: {},
|
|
253
|
+
statusCode: res.status,
|
|
254
|
+
isBase64Encoded
|
|
255
|
+
};
|
|
256
|
+
res.headers.forEach((value, key) => {
|
|
257
|
+
result.headers[key] = value;
|
|
258
|
+
});
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const triggerProcessor = new TriggerEventProcessor();
|
|
263
|
+
const triggerPathUUID = `${process.env.SECRET_SALT}-${Date.now()}-${globalThis.crypto.randomUUID()}`;
|
|
264
|
+
function getTriggerPath(path) {
|
|
265
|
+
return mergePath(triggerPathUUID, path);
|
|
266
|
+
}
|
|
267
|
+
function getEventSource(event) {
|
|
268
|
+
const eventSource = event?.eventSource || event?.Name || event?.Records?.[0]?.eventSource || event?.Records?.[0]?.EventSource || event?.source || event?.triggerSource;
|
|
269
|
+
if (!eventSource)
|
|
270
|
+
throw new Error("Invalid `event`: not LambdaTriggerEvent");
|
|
271
|
+
return eventSource;
|
|
272
|
+
}
|
|
273
|
+
function isTriggerEvent(event) {
|
|
274
|
+
try {
|
|
275
|
+
return Boolean(getEventSource(event));
|
|
276
|
+
} catch {
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
function makeLocalRequest(method, path) {
|
|
281
|
+
return new Request(`http://127.0.0.1${path}`, { method });
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function isContentTypeBinary(contentType) {
|
|
285
|
+
return !/^(?:text\/(?:plain|html|css|javascript|csv).*|application\/(?:.*json|.*xml).*|image\/svg\+xml.*)$/.test(
|
|
286
|
+
contentType
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
function isContentEncodingBinary(contentEncoding) {
|
|
290
|
+
if (contentEncoding === null) {
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
return /^(?:gzip|deflate|compress|br)/.test(contentEncoding);
|
|
294
|
+
}
|
|
295
|
+
function getProcessor(event) {
|
|
296
|
+
if (isTriggerEvent(event))
|
|
297
|
+
return triggerProcessor;
|
|
298
|
+
if (isProxyEventALB(event))
|
|
299
|
+
return albProcessor;
|
|
300
|
+
if (isProxyEventV2(event))
|
|
301
|
+
return v2Processor;
|
|
302
|
+
return v1Processor;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
globalThis.crypto ?? (globalThis.crypto = crypto);
|
|
306
|
+
async function writableWriteReadable(writer, reader) {
|
|
307
|
+
let readResult = await reader.read();
|
|
308
|
+
while (!readResult.done) {
|
|
309
|
+
writer.write(readResult.value);
|
|
310
|
+
readResult = await reader.read();
|
|
311
|
+
}
|
|
312
|
+
writer.end();
|
|
313
|
+
}
|
|
314
|
+
function stringToReadable(str) {
|
|
315
|
+
return Readable.from(Buffer.from(str));
|
|
316
|
+
}
|
|
317
|
+
function resultToStreamMetadata(result) {
|
|
318
|
+
return {
|
|
319
|
+
statusCode: result.statusCode,
|
|
320
|
+
headers: result.headers,
|
|
321
|
+
cookies: result.cookies
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
function responseToStreamMetadata(res) {
|
|
325
|
+
const headers = {};
|
|
326
|
+
const cookies = [];
|
|
327
|
+
res.headers.forEach((value, name) => {
|
|
328
|
+
if (name === "set-cookie")
|
|
329
|
+
cookies.push(value);
|
|
330
|
+
else
|
|
331
|
+
headers[name] = value;
|
|
332
|
+
});
|
|
333
|
+
return {
|
|
334
|
+
statusCode: res.status,
|
|
335
|
+
headers,
|
|
336
|
+
cookies
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
function streamHandle(app) {
|
|
340
|
+
return awslambda.streamifyResponse(
|
|
341
|
+
async (event, responseStream, context) => {
|
|
342
|
+
const processor = getProcessor(event);
|
|
343
|
+
try {
|
|
344
|
+
const req = processor.createRequest(event);
|
|
345
|
+
const res = await app.fetch(req, {
|
|
346
|
+
event,
|
|
347
|
+
context
|
|
348
|
+
});
|
|
349
|
+
if (res.headers.get("$HAAL-returnBody")) {
|
|
350
|
+
const result = await res.json();
|
|
351
|
+
responseStream = awslambda.HttpResponseStream.from(responseStream, resultToStreamMetadata(result));
|
|
352
|
+
const bodyStream = stringToReadable(result.body || "");
|
|
353
|
+
await pipeline(bodyStream, responseStream);
|
|
354
|
+
} else {
|
|
355
|
+
responseStream = awslambda.HttpResponseStream.from(responseStream, responseToStreamMetadata(res));
|
|
356
|
+
if (res.body) {
|
|
357
|
+
await writableWriteReadable(responseStream, res.body.getReader());
|
|
358
|
+
} else {
|
|
359
|
+
responseStream.write("");
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
} catch (error) {
|
|
363
|
+
console.error("Error processing request:", error);
|
|
364
|
+
responseStream.write("Internal Server Error");
|
|
365
|
+
} finally {
|
|
366
|
+
responseStream.end();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
function handle(app) {
|
|
372
|
+
return async (event, lambdaContext) => {
|
|
373
|
+
const processor = getProcessor(event);
|
|
374
|
+
const req = processor.createRequest(event);
|
|
375
|
+
const res = await app.fetch(req, {
|
|
376
|
+
event,
|
|
377
|
+
lambdaContext
|
|
378
|
+
});
|
|
379
|
+
if (res.headers.get("$HAAL-returnBody"))
|
|
380
|
+
return await res.json();
|
|
381
|
+
return processor.createResult(event, res);
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export { TriggerFactory, createTriggerFactory, getTriggerPath, handle, streamHandle, triggerPathUUID };
|
package/package.json
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "hono-adapter-aws-lambda",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.2.7",
|
|
5
|
+
"packageManager": "pnpm@9.5.0",
|
|
6
|
+
"description": "",
|
|
7
|
+
"author": "NamesMT <dangquoctrung123@gmail.com>",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"funding": "https://github.com/sponsors/namesmt",
|
|
10
|
+
"homepage": "https://github.com/namesmt/hono-adapter-aws-lambda#readme",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/namesmt/hono-adapter-aws-lambda.git"
|
|
14
|
+
},
|
|
15
|
+
"bugs": "https://github.com/namesmt/hono-adapter-aws-lambda/issues",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"hono",
|
|
18
|
+
"adapter",
|
|
19
|
+
"aws",
|
|
20
|
+
"lambda"
|
|
21
|
+
],
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.mts",
|
|
26
|
+
"import": "./dist/index.mjs"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"source": "./src/index.ts",
|
|
30
|
+
"main": "./dist/index.mjs",
|
|
31
|
+
"module": "./dist/index.mjs",
|
|
32
|
+
"types": "./dist/index.d.mts",
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=18.20.3"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"start": "NODE_ENV=dev tsx src/index.ts",
|
|
41
|
+
"watch": "NODE_ENV=dev tsx watch src/index.ts",
|
|
42
|
+
"stub": "unbuild --stub",
|
|
43
|
+
"dev": "pnpm run watch",
|
|
44
|
+
"play": "pnpm run stub && pnpm run --filter playground dev",
|
|
45
|
+
"play:useBuild": "pnpm run build && pnpm run --filter playground dev",
|
|
46
|
+
"lint": "eslint .",
|
|
47
|
+
"test": "vitest",
|
|
48
|
+
"test:types": "tsc --noEmit --skipLibCheck",
|
|
49
|
+
"check": "pnpm lint && pnpm test:types && vitest run --coverage",
|
|
50
|
+
"build": "unbuild",
|
|
51
|
+
"release": "pnpm dlx changelogen@latest --release --push --publish",
|
|
52
|
+
"prepare": "simple-git-hooks",
|
|
53
|
+
"prepublishOnly": "pnpm run build"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@namesmt/utils-lambda": "^0.0.2",
|
|
57
|
+
"@types/aws-lambda": "^8.10.141",
|
|
58
|
+
"consola": "^3.2.3",
|
|
59
|
+
"hono": "^4.5.0",
|
|
60
|
+
"std-env": "^3.7.0"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@antfu/eslint-config": "^2.23.0",
|
|
64
|
+
"@types/node": "^20.14.11",
|
|
65
|
+
"@unocss/eslint-plugin": "^0.61.5",
|
|
66
|
+
"@vitest/coverage-v8": "^2.0.3",
|
|
67
|
+
"eslint": "^9.7.0",
|
|
68
|
+
"klona": "^2.0.6",
|
|
69
|
+
"lint-staged": "^15.2.7",
|
|
70
|
+
"simple-git-hooks": "^2.11.1",
|
|
71
|
+
"tsx": "^4.16.2",
|
|
72
|
+
"typescript": "^5.5.3",
|
|
73
|
+
"unbuild": "^2.0.0",
|
|
74
|
+
"vitest": "^2.0.3"
|
|
75
|
+
},
|
|
76
|
+
"pnpm": {
|
|
77
|
+
"overrides": {
|
|
78
|
+
"hasown": "npm:@nolyfill/hasown@^1",
|
|
79
|
+
"is-core-module": "npm:@nolyfill/is-core-module@^1"
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
"simple-git-hooks": {
|
|
83
|
+
"pre-commit": "pnpm lint-staged"
|
|
84
|
+
},
|
|
85
|
+
"lint-staged": {
|
|
86
|
+
"*": "eslint --fix"
|
|
87
|
+
}
|
|
88
|
+
}
|