hono 3.3.0 → 3.3.2

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.
@@ -45,7 +45,7 @@ var createRequest = (event) => {
45
45
  method
46
46
  };
47
47
  if (event.body) {
48
- requestInit.body = event.isBase64Encoded ? atob(event.body) : event.body;
48
+ requestInit.body = event.isBase64Encoded ? Buffer.from(event.body, "base64") : event.body;
49
49
  }
50
50
  return new Request(url, requestInit);
51
51
  };
@@ -1,7 +1,7 @@
1
1
  // src/adapter/deno/serve-static.ts
2
2
  import { getFilePath } from "../../utils/filepath.js";
3
3
  import { getMimeType } from "../../utils/mime.js";
4
- var { readFile } = Deno;
4
+ var { open } = Deno;
5
5
  var DEFAULT_DOCUMENT = "index.html";
6
6
  var serveStatic = (options = { root: "" }) => {
7
7
  return async (c, next) => {
@@ -17,18 +17,18 @@ var serveStatic = (options = { root: "" }) => {
17
17
  defaultDocument: DEFAULT_DOCUMENT
18
18
  });
19
19
  path = `./${path}`;
20
- let content;
20
+ let file;
21
21
  try {
22
- content = await readFile(path);
22
+ file = await open(path);
23
23
  } catch (e) {
24
24
  console.warn(`${e}`);
25
25
  }
26
- if (content) {
26
+ if (file) {
27
27
  const mimeType = getMimeType(path);
28
28
  if (mimeType) {
29
29
  c.header("Content-Type", mimeType);
30
30
  }
31
- return c.body(content);
31
+ return c.body(file.readable);
32
32
  } else {
33
33
  console.warn(`Static file: ${path} is not found`);
34
34
  await next();
@@ -41,7 +41,7 @@ var createRequest = (event) => {
41
41
  method
42
42
  };
43
43
  const requestBody = event.Records[0].cf.request.body;
44
- requestInit.body = requestBody?.encoding === "base64" && requestBody?.data ? atob(requestBody.data) : requestBody?.data;
44
+ requestInit.body = requestBody?.encoding === "base64" && requestBody?.data ? Buffer.from(requestBody.data, "base64") : requestBody?.data;
45
45
  return new Request(url, requestInit);
46
46
  };
47
47
  var extractQueryString = (event) => {
@@ -74,7 +74,7 @@ const createRequest = (event) => {
74
74
  method
75
75
  };
76
76
  if (event.body) {
77
- requestInit.body = event.isBase64Encoded ? atob(event.body) : event.body;
77
+ requestInit.body = event.isBase64Encoded ? Buffer.from(event.body, "base64") : event.body;
78
78
  }
79
79
  return new Request(url, requestInit);
80
80
  };
@@ -23,7 +23,7 @@ __export(serve_static_exports, {
23
23
  module.exports = __toCommonJS(serve_static_exports);
24
24
  var import_filepath = require("../../utils/filepath");
25
25
  var import_mime = require("../../utils/mime");
26
- const { readFile } = Deno;
26
+ const { open } = Deno;
27
27
  const DEFAULT_DOCUMENT = "index.html";
28
28
  const serveStatic = (options = { root: "" }) => {
29
29
  return async (c, next) => {
@@ -39,18 +39,18 @@ const serveStatic = (options = { root: "" }) => {
39
39
  defaultDocument: DEFAULT_DOCUMENT
40
40
  });
41
41
  path = `./${path}`;
42
- let content;
42
+ let file;
43
43
  try {
44
- content = await readFile(path);
44
+ file = await open(path);
45
45
  } catch (e) {
46
46
  console.warn(`${e}`);
47
47
  }
48
- if (content) {
48
+ if (file) {
49
49
  const mimeType = (0, import_mime.getMimeType)(path);
50
50
  if (mimeType) {
51
51
  c.header("Content-Type", mimeType);
52
52
  }
53
- return c.body(content);
53
+ return c.body(file.readable);
54
54
  } else {
55
55
  console.warn(`Static file: ${path} is not found`);
56
56
  await next();
@@ -70,7 +70,7 @@ const createRequest = (event) => {
70
70
  method
71
71
  };
72
72
  const requestBody = event.Records[0].cf.request.body;
73
- requestInit.body = requestBody?.encoding === "base64" && requestBody?.data ? atob(requestBody.data) : requestBody?.data;
73
+ requestInit.body = requestBody?.encoding === "base64" && requestBody?.data ? Buffer.from(requestBody.data, "base64") : requestBody?.data;
74
74
  return new Request(url, requestInit);
75
75
  };
76
76
  const extractQueryString = (event) => {
@@ -35,6 +35,9 @@ const cache = (options) => {
35
35
  const response = await cache2.match(key);
36
36
  if (!response) {
37
37
  await next();
38
+ if (!c.res.ok) {
39
+ return;
40
+ }
38
41
  addHeader(c.res);
39
42
  const response2 = c.res.clone();
40
43
  if (options.wait) {
@@ -43,7 +46,7 @@ const cache = (options) => {
43
46
  c.executionCtx.waitUntil(cache2.put(key, response2));
44
47
  }
45
48
  } else {
46
- return response;
49
+ return new Response(response.body, response);
47
50
  }
48
51
  };
49
52
  };
@@ -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
- undisturbedRes = res.clone();
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 undisturbedRes.blob();
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
  };
@@ -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 import_linear_router.LinearRouter();
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
- if (isRegExp) {
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(node.children[key], method));
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(node.children[key], method));
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(node.children[key]);
165
+ tempNodes.push(child);
162
166
  }
163
167
  }
164
168
  if (typeof name === "string" && !matched) {
@@ -13,6 +13,9 @@ var cache = (options) => {
13
13
  const response = await cache2.match(key);
14
14
  if (!response) {
15
15
  await next();
16
+ if (!c.res.ok) {
17
+ return;
18
+ }
16
19
  addHeader(c.res);
17
20
  const response2 = c.res.clone();
18
21
  if (options.wait) {
@@ -21,7 +24,7 @@ var cache = (options) => {
21
24
  c.executionCtx.waitUntil(cache2.put(key, response2));
22
25
  }
23
26
  } else {
24
- return response;
27
+ return new Response(response.body, response);
25
28
  }
26
29
  };
27
30
  };
@@ -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
- undisturbedRes = res.clone();
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 undisturbedRes.blob();
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
  };
@@ -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 LinearRouter();
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
- if (isRegExp) {
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(node.children[key], method));
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(node.children[key], method));
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(node.children[key]);
143
+ tempNodes.push(child);
140
144
  }
141
145
  }
142
146
  if (typeof name === "string" && !matched) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "3.3.0",
3
+ "version": "3.3.2",
4
4
  "description": "Ultrafast web framework for the Edges",
5
5
  "main": "dist/cjs/index.js",
6
6
  "type": "module",