hono 3.3.1 → 3.3.3
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/adapter/lambda-edge/handler.js +23 -22
- package/dist/cjs/adapter/lambda-edge/handler.js +23 -22
- package/dist/cjs/hono-base.js +1 -1
- package/dist/cjs/middleware/etag/index.js +2 -5
- package/dist/cjs/preset/quick.js +5 -1
- package/dist/cjs/router/pattern-router/router.js +1 -4
- package/dist/cjs/router/trie-router/node.js +7 -3
- package/dist/hono-base.js +1 -1
- package/dist/middleware/etag/index.js +2 -5
- package/dist/preset/quick.js +5 -1
- package/dist/router/pattern-router/router.js +1 -4
- package/dist/router/trie-router/node.js +7 -3
- package/dist/types/adapter/lambda-edge/handler.d.ts +6 -4
- package/package.json +1 -1
|
@@ -2,14 +2,23 @@
|
|
|
2
2
|
import crypto from "crypto";
|
|
3
3
|
import { encodeBase64 } from "../../utils/encode.js";
|
|
4
4
|
globalThis.crypto ?? (globalThis.crypto = crypto);
|
|
5
|
+
var convertHeaders = (headers) => {
|
|
6
|
+
const cfHeaders = {};
|
|
7
|
+
headers.forEach((value, key) => {
|
|
8
|
+
cfHeaders[key.toLowerCase()] = [{ key: key.toLowerCase(), value }];
|
|
9
|
+
});
|
|
10
|
+
return cfHeaders;
|
|
11
|
+
};
|
|
5
12
|
var handle = (app) => {
|
|
6
13
|
return async (event, context, callback) => {
|
|
7
|
-
const
|
|
8
|
-
const res = await app.fetch(req, {
|
|
14
|
+
const res = await app.fetch(createRequest(event), {
|
|
9
15
|
event,
|
|
10
16
|
context,
|
|
11
|
-
callback,
|
|
12
|
-
|
|
17
|
+
callback: (err, result) => {
|
|
18
|
+
callback?.(err, result);
|
|
19
|
+
},
|
|
20
|
+
request: event.Records[0].cf.request,
|
|
21
|
+
response: event.Records[0].cf.response
|
|
13
22
|
});
|
|
14
23
|
return createResult(res);
|
|
15
24
|
};
|
|
@@ -17,35 +26,27 @@ var handle = (app) => {
|
|
|
17
26
|
var createResult = async (res) => {
|
|
18
27
|
const isBase64Encoded = isContentTypeBinary(res.headers.get("content-type") || "");
|
|
19
28
|
const body = isBase64Encoded ? encodeBase64(await res.arrayBuffer()) : await res.text();
|
|
20
|
-
const headers = {};
|
|
21
|
-
res.headers.forEach((value, key) => {
|
|
22
|
-
headers[key.toLowerCase()] = [{ key: key.toLowerCase(), value }];
|
|
23
|
-
});
|
|
24
29
|
return {
|
|
25
30
|
status: res.status.toString(),
|
|
26
|
-
headers,
|
|
31
|
+
headers: convertHeaders(res.headers),
|
|
27
32
|
body
|
|
28
33
|
};
|
|
29
34
|
};
|
|
30
35
|
var createRequest = (event) => {
|
|
31
|
-
const queryString =
|
|
36
|
+
const queryString = event.Records[0].cf.request.querystring;
|
|
32
37
|
const urlPath = `https://${event.Records[0].cf.config.distributionDomainName}${event.Records[0].cf.request.uri}`;
|
|
33
38
|
const url = queryString ? `${urlPath}?${queryString}` : urlPath;
|
|
34
39
|
const headers = new Headers();
|
|
35
|
-
|
|
40
|
+
Object.entries(event.Records[0].cf.request.headers).forEach(([k, v]) => {
|
|
36
41
|
v.forEach((header) => headers.set(k, header.value));
|
|
37
|
-
}
|
|
38
|
-
const method = event.Records[0].cf.request.method;
|
|
39
|
-
const requestInit = {
|
|
40
|
-
headers,
|
|
41
|
-
method
|
|
42
|
-
};
|
|
42
|
+
});
|
|
43
43
|
const requestBody = event.Records[0].cf.request.body;
|
|
44
|
-
|
|
45
|
-
return new Request(url,
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
const body = requestBody?.encoding === "base64" && requestBody?.data ? Buffer.from(requestBody.data, "base64") : requestBody?.data;
|
|
45
|
+
return new Request(url, {
|
|
46
|
+
headers,
|
|
47
|
+
method: event.Records[0].cf.request.method,
|
|
48
|
+
body
|
|
49
|
+
});
|
|
49
50
|
};
|
|
50
51
|
var isContentTypeBinary = (contentType) => {
|
|
51
52
|
return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(
|
|
@@ -31,14 +31,23 @@ module.exports = __toCommonJS(handler_exports);
|
|
|
31
31
|
var import_crypto = __toESM(require("crypto"), 1);
|
|
32
32
|
var import_encode = require("../../utils/encode");
|
|
33
33
|
globalThis.crypto ?? (globalThis.crypto = import_crypto.default);
|
|
34
|
+
const convertHeaders = (headers) => {
|
|
35
|
+
const cfHeaders = {};
|
|
36
|
+
headers.forEach((value, key) => {
|
|
37
|
+
cfHeaders[key.toLowerCase()] = [{ key: key.toLowerCase(), value }];
|
|
38
|
+
});
|
|
39
|
+
return cfHeaders;
|
|
40
|
+
};
|
|
34
41
|
const handle = (app) => {
|
|
35
42
|
return async (event, context, callback) => {
|
|
36
|
-
const
|
|
37
|
-
const res = await app.fetch(req, {
|
|
43
|
+
const res = await app.fetch(createRequest(event), {
|
|
38
44
|
event,
|
|
39
45
|
context,
|
|
40
|
-
callback,
|
|
41
|
-
|
|
46
|
+
callback: (err, result) => {
|
|
47
|
+
callback?.(err, result);
|
|
48
|
+
},
|
|
49
|
+
request: event.Records[0].cf.request,
|
|
50
|
+
response: event.Records[0].cf.response
|
|
42
51
|
});
|
|
43
52
|
return createResult(res);
|
|
44
53
|
};
|
|
@@ -46,35 +55,27 @@ const handle = (app) => {
|
|
|
46
55
|
const createResult = async (res) => {
|
|
47
56
|
const isBase64Encoded = isContentTypeBinary(res.headers.get("content-type") || "");
|
|
48
57
|
const body = isBase64Encoded ? (0, import_encode.encodeBase64)(await res.arrayBuffer()) : await res.text();
|
|
49
|
-
const headers = {};
|
|
50
|
-
res.headers.forEach((value, key) => {
|
|
51
|
-
headers[key.toLowerCase()] = [{ key: key.toLowerCase(), value }];
|
|
52
|
-
});
|
|
53
58
|
return {
|
|
54
59
|
status: res.status.toString(),
|
|
55
|
-
headers,
|
|
60
|
+
headers: convertHeaders(res.headers),
|
|
56
61
|
body
|
|
57
62
|
};
|
|
58
63
|
};
|
|
59
64
|
const createRequest = (event) => {
|
|
60
|
-
const queryString =
|
|
65
|
+
const queryString = event.Records[0].cf.request.querystring;
|
|
61
66
|
const urlPath = `https://${event.Records[0].cf.config.distributionDomainName}${event.Records[0].cf.request.uri}`;
|
|
62
67
|
const url = queryString ? `${urlPath}?${queryString}` : urlPath;
|
|
63
68
|
const headers = new Headers();
|
|
64
|
-
|
|
69
|
+
Object.entries(event.Records[0].cf.request.headers).forEach(([k, v]) => {
|
|
65
70
|
v.forEach((header) => headers.set(k, header.value));
|
|
66
|
-
}
|
|
67
|
-
const method = event.Records[0].cf.request.method;
|
|
68
|
-
const requestInit = {
|
|
69
|
-
headers,
|
|
70
|
-
method
|
|
71
|
-
};
|
|
71
|
+
});
|
|
72
72
|
const requestBody = event.Records[0].cf.request.body;
|
|
73
|
-
|
|
74
|
-
return new Request(url,
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
73
|
+
const body = requestBody?.encoding === "base64" && requestBody?.data ? Buffer.from(requestBody.data, "base64") : requestBody?.data;
|
|
74
|
+
return new Request(url, {
|
|
75
|
+
headers,
|
|
76
|
+
method: event.Records[0].cf.request.method,
|
|
77
|
+
body
|
|
78
|
+
});
|
|
78
79
|
};
|
|
79
80
|
const isContentTypeBinary = (contentType) => {
|
|
80
81
|
return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(
|
package/dist/cjs/hono-base.js
CHANGED
|
@@ -117,7 +117,7 @@ class Hono extends defineDynamicClass() {
|
|
|
117
117
|
const strict = init.strict ?? true;
|
|
118
118
|
delete init.strict;
|
|
119
119
|
Object.assign(this, init);
|
|
120
|
-
this.getPath
|
|
120
|
+
this.getPath = strict ? init.getPath ?? import_url.getPath : import_url.getPathNoStrict;
|
|
121
121
|
}
|
|
122
122
|
clone() {
|
|
123
123
|
const clone = new Hono({
|
|
@@ -40,15 +40,13 @@ const etag = (options) => {
|
|
|
40
40
|
const ifNoneMatch = c.req.headers.get("If-None-Match");
|
|
41
41
|
await next();
|
|
42
42
|
const res = c.res;
|
|
43
|
-
let undisturbedRes = res;
|
|
44
43
|
let etag2 = res.headers.get("ETag");
|
|
45
44
|
if (!etag2) {
|
|
46
|
-
|
|
47
|
-
const hash = await (0, import_crypto.sha1)(res.body || "");
|
|
45
|
+
const hash = await (0, import_crypto.sha1)(res.clone().body || "");
|
|
48
46
|
etag2 = weak ? `W/"${hash}"` : `"${hash}"`;
|
|
49
47
|
}
|
|
50
48
|
if (etagMatches(etag2, ifNoneMatch)) {
|
|
51
|
-
await
|
|
49
|
+
await c.res.blob();
|
|
52
50
|
c.res = new Response(null, {
|
|
53
51
|
status: 304,
|
|
54
52
|
statusText: "Not Modified",
|
|
@@ -62,7 +60,6 @@ const etag = (options) => {
|
|
|
62
60
|
}
|
|
63
61
|
});
|
|
64
62
|
} else {
|
|
65
|
-
c.res = new Response(undisturbedRes.body, undisturbedRes);
|
|
66
63
|
c.res.headers.set("ETag", etag2);
|
|
67
64
|
}
|
|
68
65
|
};
|
package/dist/cjs/preset/quick.js
CHANGED
|
@@ -23,10 +23,14 @@ __export(quick_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(quick_exports);
|
|
24
24
|
var import_hono_base = require("../hono-base");
|
|
25
25
|
var import_linear_router = require("../router/linear-router");
|
|
26
|
+
var import_smart_router = require("../router/smart-router");
|
|
27
|
+
var import_trie_router = require("../router/trie-router");
|
|
26
28
|
class Hono extends import_hono_base.HonoBase {
|
|
27
29
|
constructor(init = {}) {
|
|
28
30
|
super(init);
|
|
29
|
-
this.router = new
|
|
31
|
+
this.router = new import_smart_router.SmartRouter({
|
|
32
|
+
routers: [new import_linear_router.LinearRouter(), new import_trie_router.TrieRouter()]
|
|
33
|
+
});
|
|
30
34
|
}
|
|
31
35
|
}
|
|
32
36
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -64,14 +64,11 @@ class PatternRouter {
|
|
|
64
64
|
const handlers = [];
|
|
65
65
|
const params = {};
|
|
66
66
|
for (const [pattern, routeMethod, handler] of this.routes) {
|
|
67
|
-
const isRegExp = pattern.source.charCodeAt(pattern.source.length - 1) === 36;
|
|
68
67
|
if (routeMethod === import_router.METHOD_NAME_ALL || routeMethod === method) {
|
|
69
68
|
const match = pattern.exec(path);
|
|
70
69
|
if (match) {
|
|
71
70
|
handlers.push(handler);
|
|
72
|
-
|
|
73
|
-
Object.assign(params, match.groups);
|
|
74
|
-
}
|
|
71
|
+
Object.assign(params, match.groups);
|
|
75
72
|
}
|
|
76
73
|
}
|
|
77
74
|
}
|
|
@@ -147,18 +147,22 @@ class Node {
|
|
|
147
147
|
if (part === "")
|
|
148
148
|
continue;
|
|
149
149
|
const [key, name, matcher] = pattern;
|
|
150
|
+
const child = node.children[key];
|
|
150
151
|
const restPathString = parts.slice(i).join("/");
|
|
151
152
|
if (matcher instanceof RegExp && matcher.test(restPathString)) {
|
|
152
|
-
handlerSets.push(...this.gHSets(
|
|
153
|
+
handlerSets.push(...this.gHSets(child, method));
|
|
153
154
|
params[name] = restPathString;
|
|
154
155
|
continue;
|
|
155
156
|
}
|
|
156
157
|
if (matcher === true || matcher instanceof RegExp && matcher.test(part)) {
|
|
157
158
|
if (typeof key === "string") {
|
|
158
159
|
if (isLast === true) {
|
|
159
|
-
handlerSets.push(...this.gHSets(
|
|
160
|
+
handlerSets.push(...this.gHSets(child, method));
|
|
161
|
+
if (child.children["*"]) {
|
|
162
|
+
handlerSets.push(...this.gHSets(child.children["*"], method));
|
|
163
|
+
}
|
|
160
164
|
} else {
|
|
161
|
-
tempNodes.push(
|
|
165
|
+
tempNodes.push(child);
|
|
162
166
|
}
|
|
163
167
|
}
|
|
164
168
|
if (typeof name === "string" && !matched) {
|
package/dist/hono-base.js
CHANGED
|
@@ -95,7 +95,7 @@ var Hono = class extends defineDynamicClass() {
|
|
|
95
95
|
const strict = init.strict ?? true;
|
|
96
96
|
delete init.strict;
|
|
97
97
|
Object.assign(this, init);
|
|
98
|
-
this.getPath
|
|
98
|
+
this.getPath = strict ? init.getPath ?? getPath : getPathNoStrict;
|
|
99
99
|
}
|
|
100
100
|
clone() {
|
|
101
101
|
const clone = new Hono({
|
|
@@ -18,15 +18,13 @@ var etag = (options) => {
|
|
|
18
18
|
const ifNoneMatch = c.req.headers.get("If-None-Match");
|
|
19
19
|
await next();
|
|
20
20
|
const res = c.res;
|
|
21
|
-
let undisturbedRes = res;
|
|
22
21
|
let etag2 = res.headers.get("ETag");
|
|
23
22
|
if (!etag2) {
|
|
24
|
-
|
|
25
|
-
const hash = await sha1(res.body || "");
|
|
23
|
+
const hash = await sha1(res.clone().body || "");
|
|
26
24
|
etag2 = weak ? `W/"${hash}"` : `"${hash}"`;
|
|
27
25
|
}
|
|
28
26
|
if (etagMatches(etag2, ifNoneMatch)) {
|
|
29
|
-
await
|
|
27
|
+
await c.res.blob();
|
|
30
28
|
c.res = new Response(null, {
|
|
31
29
|
status: 304,
|
|
32
30
|
statusText: "Not Modified",
|
|
@@ -40,7 +38,6 @@ var etag = (options) => {
|
|
|
40
38
|
}
|
|
41
39
|
});
|
|
42
40
|
} else {
|
|
43
|
-
c.res = new Response(undisturbedRes.body, undisturbedRes);
|
|
44
41
|
c.res.headers.set("ETag", etag2);
|
|
45
42
|
}
|
|
46
43
|
};
|
package/dist/preset/quick.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
// src/preset/quick.ts
|
|
2
2
|
import { HonoBase } from "../hono-base.js";
|
|
3
3
|
import { LinearRouter } from "../router/linear-router/index.js";
|
|
4
|
+
import { SmartRouter } from "../router/smart-router/index.js";
|
|
5
|
+
import { TrieRouter } from "../router/trie-router/index.js";
|
|
4
6
|
var Hono = class extends HonoBase {
|
|
5
7
|
constructor(init = {}) {
|
|
6
8
|
super(init);
|
|
7
|
-
this.router = new
|
|
9
|
+
this.router = new SmartRouter({
|
|
10
|
+
routers: [new LinearRouter(), new TrieRouter()]
|
|
11
|
+
});
|
|
8
12
|
}
|
|
9
13
|
};
|
|
10
14
|
export {
|
|
@@ -42,14 +42,11 @@ var PatternRouter = class {
|
|
|
42
42
|
const handlers = [];
|
|
43
43
|
const params = {};
|
|
44
44
|
for (const [pattern, routeMethod, handler] of this.routes) {
|
|
45
|
-
const isRegExp = pattern.source.charCodeAt(pattern.source.length - 1) === 36;
|
|
46
45
|
if (routeMethod === METHOD_NAME_ALL || routeMethod === method) {
|
|
47
46
|
const match = pattern.exec(path);
|
|
48
47
|
if (match) {
|
|
49
48
|
handlers.push(handler);
|
|
50
|
-
|
|
51
|
-
Object.assign(params, match.groups);
|
|
52
|
-
}
|
|
49
|
+
Object.assign(params, match.groups);
|
|
53
50
|
}
|
|
54
51
|
}
|
|
55
52
|
}
|
|
@@ -125,18 +125,22 @@ var Node = class {
|
|
|
125
125
|
if (part === "")
|
|
126
126
|
continue;
|
|
127
127
|
const [key, name, matcher] = pattern;
|
|
128
|
+
const child = node.children[key];
|
|
128
129
|
const restPathString = parts.slice(i).join("/");
|
|
129
130
|
if (matcher instanceof RegExp && matcher.test(restPathString)) {
|
|
130
|
-
handlerSets.push(...this.gHSets(
|
|
131
|
+
handlerSets.push(...this.gHSets(child, method));
|
|
131
132
|
params[name] = restPathString;
|
|
132
133
|
continue;
|
|
133
134
|
}
|
|
134
135
|
if (matcher === true || matcher instanceof RegExp && matcher.test(part)) {
|
|
135
136
|
if (typeof key === "string") {
|
|
136
137
|
if (isLast === true) {
|
|
137
|
-
handlerSets.push(...this.gHSets(
|
|
138
|
+
handlerSets.push(...this.gHSets(child, method));
|
|
139
|
+
if (child.children["*"]) {
|
|
140
|
+
handlerSets.push(...this.gHSets(child.children["*"], method));
|
|
141
|
+
}
|
|
138
142
|
} else {
|
|
139
|
-
tempNodes.push(
|
|
143
|
+
tempNodes.push(child);
|
|
140
144
|
}
|
|
141
145
|
}
|
|
142
146
|
if (typeof name === "string" && !matched) {
|
|
@@ -32,6 +32,11 @@ export interface CloudFrontRequest {
|
|
|
32
32
|
custom: CloudFrontCustomOrigin;
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
|
+
export interface CloudFrontResponse {
|
|
36
|
+
headers: CloudFrontHeaders;
|
|
37
|
+
status: string;
|
|
38
|
+
statusDescription?: string;
|
|
39
|
+
}
|
|
35
40
|
interface CloudFrontConfig {
|
|
36
41
|
distributionDomainName: string;
|
|
37
42
|
distributionId: string;
|
|
@@ -42,6 +47,7 @@ interface CloudFrontEvent {
|
|
|
42
47
|
cf: {
|
|
43
48
|
config: CloudFrontConfig;
|
|
44
49
|
request: CloudFrontRequest;
|
|
50
|
+
response?: CloudFrontResponse;
|
|
45
51
|
};
|
|
46
52
|
}
|
|
47
53
|
export interface CloudFrontEdgeEvent {
|
|
@@ -63,10 +69,6 @@ interface CloudFrontResult {
|
|
|
63
69
|
body?: string;
|
|
64
70
|
bodyEncoding?: 'text' | 'base64';
|
|
65
71
|
}
|
|
66
|
-
/**
|
|
67
|
-
* Accepts events from 'Lambda@Edge' event
|
|
68
|
-
* https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html
|
|
69
|
-
*/
|
|
70
72
|
export declare const handle: (app: Hono<any>) => (event: CloudFrontEdgeEvent, context?: CloudFrontContext, callback?: Callback) => Promise<CloudFrontResult>;
|
|
71
73
|
export declare const isContentTypeBinary: (contentType: string) => boolean;
|
|
72
74
|
export {};
|