hono 3.2.0-rc.3 → 3.2.0

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/README.md CHANGED
@@ -47,11 +47,11 @@ npm create hono@latest my-app
47
47
 
48
48
  ## Features
49
49
 
50
- - **Ultrafast** - The router _RegExpRouter_ is really fast. Not using linear loops. Fast.
51
- - **Multi-runtime** - Works on Cloudflare Workers, Fastly Compute@Edge, Deno, Bun, Lagon, AWS Lambda or Node.js. The same code runs on all platforms.
52
- - **Batteries Included** - Hono has built-in middleware, custom middleware, and third-party middleware. Batteries included.
53
- - **Delightful DX** - First-class TypeScript support. Now, we've got "Types".
54
- - **Small** - The `hono/tiny` preset is under 12kB. Hono has zero dependencies and uses only the Web Standard API.
50
+ - **Ultrafast** 🚀 - The router `RegExpRouter` is really fast. Not using linear loops. Fast.
51
+ - **Lightweight** ðŸŠķ - The `hono/tiny` preset is under 12kB. Hono has zero dependencies and uses only the Web Standard API.
52
+ - **Multi-runtime** 🌍 - Works on Cloudflare Workers, Fastly Compute@Edge, Deno, Bun, Lagon, AWS Lambda, or Node.js. The same code runs on all platforms.
53
+ - **Batteries Included** 🔋 - Hono has built-in middleware, custom middleware, and third-party middleware. Batteries included.
54
+ - **Delightful DX** 🛠ïļ - Super clean APIs. First-class TypeScript support. Now, we've got "Types".
55
55
 
56
56
  ## Benchmarks
57
57
 
@@ -67,6 +67,11 @@ class Hono extends defineDynamicClass() {
67
67
  const req = new Request(path, requestInit);
68
68
  return await this.fetch(req);
69
69
  };
70
+ this.fire = () => {
71
+ addEventListener("fetch", (event) => {
72
+ void event.respondWith(this.handleEvent(event));
73
+ });
74
+ };
70
75
  const allMethods = [...import_router.METHODS, import_router.METHOD_NAME_ALL_LOWERCASE];
71
76
  allMethods.map((method) => {
72
77
  this[method] = (args1, ...args) => {
@@ -150,6 +155,31 @@ class Hono extends defineDynamicClass() {
150
155
  );
151
156
  });
152
157
  }
158
+ mount(path, applicationHandler, optionHandler) {
159
+ const pathPrefixLength = (0, import_url.mergePath)(this._basePath, path).length;
160
+ const handler = async (c, next) => {
161
+ let executionContext = void 0;
162
+ try {
163
+ executionContext = c.executionCtx;
164
+ } catch {
165
+ }
166
+ const options = optionHandler ? optionHandler(c) : [c.env, executionContext];
167
+ const optionsArray = Array.isArray(options) ? options : [options];
168
+ const res = await applicationHandler(
169
+ new Request(new URL(c.req.path.slice(pathPrefixLength) || "/", c.req.url), c.req.raw),
170
+ ...optionsArray
171
+ );
172
+ if (res)
173
+ return res;
174
+ await next();
175
+ };
176
+ this.addRoute(import_router.METHOD_NAME_ALL, (0, import_url.mergePath)(path, "*"), handler);
177
+ return this;
178
+ }
179
+ get routerName() {
180
+ this.matchRoute("GET", "/");
181
+ return this.router.name;
182
+ }
153
183
  addRoute(method, path, handler) {
154
184
  method = method.toUpperCase();
155
185
  if (this._basePath) {
package/dist/cjs/index.js CHANGED
@@ -22,11 +22,6 @@ __export(src_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(src_exports);
24
24
  var import_hono = require("./hono");
25
- import_hono.Hono.prototype.fire = function() {
26
- addEventListener("fetch", (event) => {
27
- void event.respondWith(this.handleEvent(event));
28
- });
29
- };
30
25
  // Annotate the CommonJS export names for ESM import in node:
31
26
  0 && (module.exports = {
32
27
  Hono
@@ -24,8 +24,8 @@ 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
26
  class Hono extends import_hono_base.HonoBase {
27
- constructor() {
28
- super();
27
+ constructor(init = {}) {
28
+ super(init);
29
29
  this.router = new import_linear_router.LinearRouter();
30
30
  }
31
31
  }
@@ -24,8 +24,8 @@ module.exports = __toCommonJS(tiny_exports);
24
24
  var import_hono_base = require("../hono-base");
25
25
  var import_pattern_router = require("../router/pattern-router");
26
26
  class Hono extends import_hono_base.HonoBase {
27
- constructor() {
28
- super();
27
+ constructor(init = {}) {
28
+ super(init);
29
29
  this.router = new import_pattern_router.PatternRouter();
30
30
  }
31
31
  }
@@ -26,6 +26,7 @@ const splitPathRe = /\/(:\w+(?:{[^}]+})?)|\/[^\/\?]+|(\?)/g;
26
26
  const splitByStarRe = /\*/;
27
27
  class LinearRouter {
28
28
  constructor() {
29
+ this.name = "LinearRouter";
29
30
  this.routes = [];
30
31
  }
31
32
  add(method, path, handler) {
@@ -24,6 +24,7 @@ module.exports = __toCommonJS(router_exports);
24
24
  var import_router = require("../../router");
25
25
  class PatternRouter {
26
26
  constructor() {
27
+ this.name = "PatternRouter";
27
28
  this.routes = [];
28
29
  this.dNames = {};
29
30
  }
@@ -32,7 +33,7 @@ class PatternRouter {
32
33
  if (endsWithWildcard) {
33
34
  path = path.slice(0, -2);
34
35
  }
35
- const parts = path.match(/\/(:\w+(?:{[^}]+})?)|\/[^\/\?]+|(\?)/g) || [];
36
+ const parts = path.match(/\/?(:\w+(?:{[^}]+})?)|\/?[^\/\?]+|(\?)/g) || [];
36
37
  if (parts[parts.length - 1] === "?") {
37
38
  this.add(method, parts.slice(0, parts.length - 2).join(""), handler);
38
39
  parts.pop();
@@ -93,6 +93,7 @@ function findMiddleware(middleware, path) {
93
93
  }
94
94
  class RegExpRouter {
95
95
  constructor() {
96
+ this.name = "RegExpRouter";
96
97
  this.middleware = { [import_router.METHOD_NAME_ALL]: {} };
97
98
  this.routes = { [import_router.METHOD_NAME_ALL]: {} };
98
99
  }
@@ -24,6 +24,7 @@ module.exports = __toCommonJS(router_exports);
24
24
  var import_router = require("../../router");
25
25
  class SmartRouter {
26
26
  constructor(init) {
27
+ this.name = "SmartRouter";
27
28
  this.routers = [];
28
29
  this.routes = [];
29
30
  Object.assign(this, init);
@@ -63,6 +64,7 @@ class SmartRouter {
63
64
  if (i === len) {
64
65
  throw new Error("Fatal error");
65
66
  }
67
+ this.name = `SmartRouter + ${this.activeRouter.name}`;
66
68
  return res || null;
67
69
  }
68
70
  get activeRouter() {
@@ -25,6 +25,7 @@ var import_url = require("../../utils/url");
25
25
  var import_node = require("./node");
26
26
  class TrieRouter {
27
27
  constructor() {
28
+ this.name = "TrieRouter";
28
29
  this.node = new import_node.Node();
29
30
  }
30
31
  add(method, path, handler) {
package/dist/hono-base.js CHANGED
@@ -45,6 +45,11 @@ var Hono = class extends defineDynamicClass() {
45
45
  const req = new Request(path, requestInit);
46
46
  return await this.fetch(req);
47
47
  };
48
+ this.fire = () => {
49
+ addEventListener("fetch", (event) => {
50
+ void event.respondWith(this.handleEvent(event));
51
+ });
52
+ };
48
53
  const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE];
49
54
  allMethods.map((method) => {
50
55
  this[method] = (args1, ...args) => {
@@ -128,6 +133,31 @@ var Hono = class extends defineDynamicClass() {
128
133
  );
129
134
  });
130
135
  }
136
+ mount(path, applicationHandler, optionHandler) {
137
+ const pathPrefixLength = mergePath(this._basePath, path).length;
138
+ const handler = async (c, next) => {
139
+ let executionContext = void 0;
140
+ try {
141
+ executionContext = c.executionCtx;
142
+ } catch {
143
+ }
144
+ const options = optionHandler ? optionHandler(c) : [c.env, executionContext];
145
+ const optionsArray = Array.isArray(options) ? options : [options];
146
+ const res = await applicationHandler(
147
+ new Request(new URL(c.req.path.slice(pathPrefixLength) || "/", c.req.url), c.req.raw),
148
+ ...optionsArray
149
+ );
150
+ if (res)
151
+ return res;
152
+ await next();
153
+ };
154
+ this.addRoute(METHOD_NAME_ALL, mergePath(path, "*"), handler);
155
+ return this;
156
+ }
157
+ get routerName() {
158
+ this.matchRoute("GET", "/");
159
+ return this.router.name;
160
+ }
131
161
  addRoute(method, path, handler) {
132
162
  method = method.toUpperCase();
133
163
  if (this._basePath) {
package/dist/index.js CHANGED
@@ -1,10 +1,5 @@
1
1
  // src/index.ts
2
2
  import { Hono } from "./hono.js";
3
- Hono.prototype.fire = function() {
4
- addEventListener("fetch", (event) => {
5
- void event.respondWith(this.handleEvent(event));
6
- });
7
- };
8
3
  export {
9
4
  Hono
10
5
  };
@@ -2,8 +2,8 @@
2
2
  import { HonoBase } from "../hono-base.js";
3
3
  import { LinearRouter } from "../router/linear-router/index.js";
4
4
  var Hono = class extends HonoBase {
5
- constructor() {
6
- super();
5
+ constructor(init = {}) {
6
+ super(init);
7
7
  this.router = new LinearRouter();
8
8
  }
9
9
  };
@@ -2,8 +2,8 @@
2
2
  import { HonoBase } from "../hono-base.js";
3
3
  import { PatternRouter } from "../router/pattern-router/index.js";
4
4
  var Hono = class extends HonoBase {
5
- constructor() {
6
- super();
5
+ constructor(init = {}) {
6
+ super(init);
7
7
  this.router = new PatternRouter();
8
8
  }
9
9
  };
@@ -4,6 +4,7 @@ var splitPathRe = /\/(:\w+(?:{[^}]+})?)|\/[^\/\?]+|(\?)/g;
4
4
  var splitByStarRe = /\*/;
5
5
  var LinearRouter = class {
6
6
  constructor() {
7
+ this.name = "LinearRouter";
7
8
  this.routes = [];
8
9
  }
9
10
  add(method, path, handler) {
@@ -2,6 +2,7 @@
2
2
  import { METHOD_NAME_ALL } from "../../router.js";
3
3
  var PatternRouter = class {
4
4
  constructor() {
5
+ this.name = "PatternRouter";
5
6
  this.routes = [];
6
7
  this.dNames = {};
7
8
  }
@@ -10,7 +11,7 @@ var PatternRouter = class {
10
11
  if (endsWithWildcard) {
11
12
  path = path.slice(0, -2);
12
13
  }
13
- const parts = path.match(/\/(:\w+(?:{[^}]+})?)|\/[^\/\?]+|(\?)/g) || [];
14
+ const parts = path.match(/\/?(:\w+(?:{[^}]+})?)|\/?[^\/\?]+|(\?)/g) || [];
14
15
  if (parts[parts.length - 1] === "?") {
15
16
  this.add(method, parts.slice(0, parts.length - 2).join(""), handler);
16
17
  parts.pop();
@@ -71,6 +71,7 @@ function findMiddleware(middleware, path) {
71
71
  }
72
72
  var RegExpRouter = class {
73
73
  constructor() {
74
+ this.name = "RegExpRouter";
74
75
  this.middleware = { [METHOD_NAME_ALL]: {} };
75
76
  this.routes = { [METHOD_NAME_ALL]: {} };
76
77
  }
@@ -2,6 +2,7 @@
2
2
  import { UnsupportedPathError } from "../../router.js";
3
3
  var SmartRouter = class {
4
4
  constructor(init) {
5
+ this.name = "SmartRouter";
5
6
  this.routers = [];
6
7
  this.routes = [];
7
8
  Object.assign(this, init);
@@ -41,6 +42,7 @@ var SmartRouter = class {
41
42
  if (i === len) {
42
43
  throw new Error("Fatal error");
43
44
  }
45
+ this.name = `SmartRouter + ${this.activeRouter.name}`;
44
46
  return res || null;
45
47
  }
46
48
  get activeRouter() {
@@ -3,6 +3,7 @@ import { checkOptionalParameter } from "../../utils/url.js";
3
3
  import { Node } from "./node.js";
4
4
  var TrieRouter = class {
5
5
  constructor() {
6
+ this.name = "TrieRouter";
6
7
  this.node = new Node();
7
8
  }
8
9
  add(method, path, handler) {
@@ -1,3 +1,4 @@
1
+ import { Context } from './context';
1
2
  import type { ExecutionContext } from './context';
2
3
  import type { Router } from './router';
3
4
  import type { Env, ErrorHandler, H, HandlerInterface, MiddlewareHandlerInterface, NotFoundHandler, OnHandlerInterface, MergePath, MergeSchemaPath } from './types';
@@ -43,6 +44,13 @@ declare class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> e
43
44
  onError(handler: ErrorHandler<E>): this;
44
45
  notFound(handler: NotFoundHandler<E>): this;
45
46
  showRoutes(): void;
47
+ /**
48
+ * @experimental
49
+ * `app.mount()` is an experimental feature.
50
+ * The API might be changed.
51
+ */
52
+ mount(path: string, applicationHandler: (request: Request, ...args: any) => Response | Promise<Response>, optionHandler?: (c: Context) => unknown): Hono<E, S, BasePath>;
53
+ get routerName(): string;
46
54
  private addRoute;
47
55
  private matchRoute;
48
56
  private handleError;
@@ -50,5 +58,6 @@ declare class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> e
50
58
  handleEvent: (event: FetchEvent) => Response | Promise<Response>;
51
59
  fetch: (request: Request, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext) => Response | Promise<Response>;
52
60
  request: (input: Request | string | URL, requestInit?: RequestInit) => Promise<Response>;
61
+ fire: () => void;
53
62
  }
54
63
  export { Hono as HonoBase };
@@ -2,9 +2,4 @@ import { Hono } from './hono';
2
2
  export type { Env, ErrorHandler, Handler, MiddlewareHandler, Next, NotFoundHandler, ValidationTargets, Input, } from './types';
3
3
  export type { Context, ContextVariableMap } from './context';
4
4
  export type { HonoRequest } from './request';
5
- declare module './hono-base' {
6
- interface HonoBase {
7
- fire(): void;
8
- }
9
- }
10
5
  export { Hono };
@@ -1,5 +1,7 @@
1
1
  import { HonoBase } from '../hono-base';
2
2
  import type { Env } from '../types';
3
3
  export declare class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends HonoBase<E, S, BasePath> {
4
- constructor();
4
+ constructor(init?: Partial<Pick<Hono, 'getPath'> & {
5
+ strict: boolean;
6
+ }>);
5
7
  }
@@ -1,5 +1,7 @@
1
1
  import { HonoBase } from '../hono-base';
2
2
  import type { Env } from '../types';
3
3
  export declare class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends HonoBase<E, S, BasePath> {
4
- constructor();
4
+ constructor(init?: Partial<Pick<Hono, 'getPath'> & {
5
+ strict: boolean;
6
+ }>);
5
7
  }
@@ -1,5 +1,6 @@
1
1
  import type { Router, Result } from '../../router';
2
2
  export declare class LinearRouter<T> implements Router<T> {
3
+ name: string;
3
4
  routes: [string, string, T][];
4
5
  add(method: string, path: string, handler: T): void;
5
6
  match(method: string, path: string): Result<T> | null;
@@ -1,5 +1,6 @@
1
1
  import type { Result, Router } from '../../router';
2
2
  export declare class PatternRouter<T> implements Router<T> {
3
+ name: string;
3
4
  private routes;
4
5
  private dNames;
5
6
  add(method: string, path: string, handler: T): void;
@@ -1,5 +1,6 @@
1
1
  import type { Router, Result } from '../../router';
2
2
  export declare class RegExpRouter<T> implements Router<T> {
3
+ name: string;
3
4
  middleware?: Record<string, Record<string, T[]>>;
4
5
  routes?: Record<string, Record<string, T[]>>;
5
6
  constructor();
@@ -1,5 +1,6 @@
1
1
  import type { Router, Result } from '../../router';
2
2
  export declare class SmartRouter<T> implements Router<T> {
3
+ name: string;
3
4
  routers: Router<T>[];
4
5
  routes?: [string, string, T][];
5
6
  constructor(init: Pick<SmartRouter<T>, 'routers'>);
@@ -1,6 +1,7 @@
1
1
  import type { Result, Router } from '../../router';
2
2
  import { Node } from './node';
3
3
  export declare class TrieRouter<T> implements Router<T> {
4
+ name: string;
4
5
  node: Node<T>;
5
6
  constructor();
6
7
  add(method: string, path: string, handler: T): void;
@@ -2,6 +2,7 @@ export declare const METHOD_NAME_ALL: "ALL";
2
2
  export declare const METHOD_NAME_ALL_LOWERCASE: "all";
3
3
  export declare const METHODS: readonly ["get", "post", "put", "delete", "head", "options", "patch"];
4
4
  export interface Router<T> {
5
+ name: string;
5
6
  add(method: string, path: string, handler: T): void;
6
7
  match(method: string, path: string): Result<T> | null;
7
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "3.2.0-rc.3",
3
+ "version": "3.2.0",
4
4
  "description": "Ultrafast web framework for the Edges",
5
5
  "main": "dist/cjs/index.js",
6
6
  "type": "module",