hono 3.2.0-rc.4 → 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
 
@@ -155,6 +155,31 @@ class Hono extends defineDynamicClass() {
155
155
  );
156
156
  });
157
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
+ }
158
183
  addRoute(method, path, handler) {
159
184
  method = method.toUpperCase();
160
185
  if (this._basePath) {
@@ -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
@@ -133,6 +133,31 @@ var Hono = class extends defineDynamicClass() {
133
133
  );
134
134
  });
135
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
+ }
136
161
  addRoute(method, path, handler) {
137
162
  method = method.toUpperCase();
138
163
  if (this._basePath) {
@@ -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;
@@ -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.4",
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",