hono 1.6.0 → 1.6.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/README.md CHANGED
@@ -13,8 +13,9 @@
13
13
  [![npm type definitions](https://img.shields.io/npm/types/hono)](https://www.npmjs.com/package/hono)
14
14
  [![GitHub commit activity](https://img.shields.io/github/commit-activity/m/honojs/hono)](https://github.com/honojs/hono/pulse)
15
15
  [![GitHub last commit](https://img.shields.io/github/last-commit/honojs/hono)](https://github.com/honojs/hono/commits/master)
16
+ [![Deno badge](https://img.shields.io/endpoint?url=https%3A%2F%2Fdeno-visualizer.danopia.net%2Fshields%2Flatest-version%2Fx%2Fhono%2Fmod.ts)](https://doc.deno.land/https/deno.land/x/hono/mod.ts)
16
17
 
17
- Hono - _**[炎] means flame🔥 in Japanese**_ - is a small, simple, and ultrafast web framework for Cloudflare Workers or Service Worker based serverless such as Fastly Compute@Edge.
18
+ Hono - _**[炎] means flame🔥 in Japanese**_ - is a small, simple, and ultrafast web framework for Cloudflare Workers, Deno, Bun, and others.
18
19
 
19
20
  ```ts
20
21
  import { Hono } from 'hono'
@@ -31,8 +32,7 @@ app.fire()
31
32
  - **Zero-dependencies** - using only Service Worker and Web Standard API.
32
33
  - **Middleware** - built-in middleware and ability to extend with your own middleware.
33
34
  - **TypeScript** - first-class TypeScript support.
34
- - **Optimized** - for Cloudflare Workers.
35
- - **Deno** - support for Deno (Experimental).
35
+ - **Multi-platform** - works on Cloudflare Workers, Fastly Compute@Edge, Deno, or Bun.
36
36
 
37
37
  ## Benchmarks
38
38
 
@@ -69,6 +69,8 @@ Fastest is hono - regexp-router
69
69
  | oak | 10.5.1 | 2385k requests in 40.02s, 403 MB read |
70
70
  | opine | 2.2.0 | 1491k requests in 40.02s, 346 MB read |
71
71
 
72
+ Another benchmark result: [denosaurs/bench](https://github.com/denosaurs/bench)
73
+
72
74
  ## Why so fast?
73
75
 
74
76
  Routers used in Hono are really smart.
@@ -101,7 +103,7 @@ Built-in middleware make _"**Write Less, do more**"_ in reality. You can use a l
101
103
  - [Logger](https://github.com/honojs/hono/tree/master/src/middleware/logger/)
102
104
  - [Mustache template engine](https://github.com/honojs/hono/tree/master/src/middleware/mustache/) (Only for Cloudflare Workers)
103
105
  - [JSON pretty printing](https://github.com/honojs/hono/tree/master/src/middleware/pretty-json/)
104
- - [Serving static files](https://github.com/honojs/hono/tree/master/src/middleware/serve-static/) (Only for Cloudflare Workers)
106
+ - [Serving static files](https://github.com/honojs/hono/tree/master/src/middleware/serve-static/) (Only for Cloudflare Workers and Deno)
105
107
 
106
108
  To enable logger and Etag middleware with just this code.
107
109
 
@@ -758,30 +760,42 @@ export default app
758
760
 
759
761
  ## Deno
760
762
 
761
- Hono also works with Deno. This feature is still experimental.
763
+ Hono also works on Deno. This feature is still experimental.
762
764
 
763
765
  ```tsx
764
766
  /** @jsx jsx */
765
- import { serve } from 'https://deno.land/std@0.146.0/http/server.ts'
766
- import { Hono, logger, poweredBy, basicAuth, jsx } from 'https://deno.land/x/hono/mod.ts'
767
+ import { serve } from 'https://deno.land/std/http/server.ts'
768
+ import { Hono, logger, poweredBy, serveStatic, jsx } from 'https://deno.land/x/hono/mod.ts'
767
769
 
768
770
  const app = new Hono()
769
771
 
770
772
  app.use('*', logger(), poweredBy())
771
- app.get(
772
- '/auth/*',
773
- basicAuth({
774
- username: 'deno',
775
- password: 'isacool',
776
- })
777
- )
778
773
 
774
+ app.get('/favicon.ico', serveStatic({ path: './public/favicon.ico' }))
779
775
  app.get('/', (c) => {
780
776
  return c.html(<h1>Hello Deno!</h1>)
781
777
  })
782
- app.get('/auth/abc', (c) => c.text('You are authorized'))
783
778
 
784
- serve(app.fire())
779
+ serve(app.fetch)
780
+ ```
781
+
782
+ ## Bun
783
+
784
+ Hono also works on Bun. This feature is still experimental.
785
+
786
+ ```ts
787
+ import { Hono } from 'hono'
788
+
789
+ const app = new Hono()
790
+
791
+ app.get('/', (c) => {
792
+ return c.json({ message: 'Hello Bun!' })
793
+ })
794
+
795
+ export default {
796
+ port: 3000,
797
+ fetch: app.fetch,
798
+ }
785
799
  ```
786
800
 
787
801
  ## Related projects
package/dist/hono.d.ts CHANGED
@@ -48,7 +48,7 @@ export declare class Hono<E extends Env = Env, P extends string = '/'> extends H
48
48
  private matchRoute;
49
49
  private dispatch;
50
50
  handleEvent(event: FetchEvent): Promise<Response>;
51
- fetch(request: Request, env?: E, executionCtx?: ExecutionContext): Promise<Response>;
51
+ fetch: (request: Request, env?: E | undefined, executionCtx?: ExecutionContext | undefined) => Promise<Response>;
52
52
  request(input: RequestInfo, requestInit?: RequestInit): Promise<Response>;
53
53
  }
54
54
  export {};
package/dist/hono.js CHANGED
@@ -29,6 +29,9 @@ class Hono extends defineDynamicClass() {
29
29
  const message = 'Internal Server Error';
30
30
  return c.text(message, 500);
31
31
  };
32
+ this.fetch = async (request, env, executionCtx) => {
33
+ return this.dispatch(request, executionCtx, env);
34
+ };
32
35
  (0, request_1.extendRequestPrototype)();
33
36
  const allMethods = [...methods, router_1.METHOD_NAME_ALL_LOWERCASE];
34
37
  allMethods.map((method) => {
@@ -117,9 +120,6 @@ class Hono extends defineDynamicClass() {
117
120
  async handleEvent(event) {
118
121
  return this.dispatch(event.request, event);
119
122
  }
120
- async fetch(request, env, executionCtx) {
121
- return this.dispatch(request, executionCtx, env);
122
- }
123
123
  request(input, requestInit) {
124
124
  const req = input instanceof Request ? input : new Request(input, requestInit);
125
125
  return this.dispatch(req);
@@ -7,13 +7,14 @@ exports.mustache = void 0;
7
7
  const mustache_1 = __importDefault(require("mustache"));
8
8
  const buffer_1 = require("../../utils/buffer");
9
9
  const cloudflare_1 = require("../../utils/cloudflare");
10
+ const filepath_1 = require("../../utils/filepath");
10
11
  const EXTENSION = '.mustache';
11
12
  const DEFAULT_DOCUMENT = 'index.mustache';
12
13
  const mustache = (init = { root: '' }) => {
13
14
  const { root } = init;
14
15
  return async (c, next) => {
15
16
  c.render = async (filename, params = {}, options) => {
16
- const path = (0, cloudflare_1.getKVFilePath)({
17
+ const path = (0, filepath_1.getFilePath)({
17
18
  filename: `${filename}${EXTENSION}`,
18
19
  root: root,
19
20
  defaultDocument: DEFAULT_DOCUMENT,
@@ -31,7 +32,7 @@ const mustache = (init = { root: '' }) => {
31
32
  if (options) {
32
33
  const partials = options;
33
34
  for (const key of Object.keys(partials)) {
34
- const partialPath = (0, cloudflare_1.getKVFilePath)({
35
+ const partialPath = (0, filepath_1.getFilePath)({
35
36
  filename: `${partials[key]}${EXTENSION}`,
36
37
  root: root,
37
38
  defaultDocument: DEFAULT_DOCUMENT,
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.serveStatic = void 0;
4
4
  const cloudflare_1 = require("../../utils/cloudflare");
5
+ const filepath_1 = require("../../utils/filepath");
5
6
  const mime_1 = require("../../utils/mime");
6
7
  const DEFAULT_DOCUMENT = 'index.html';
7
8
  // This middleware is available only on Cloudflare Workers.
@@ -12,7 +13,7 @@ const serveStatic = (options = { root: '' }) => {
12
13
  await next();
13
14
  }
14
15
  const url = new URL(c.req.url);
15
- const path = (0, cloudflare_1.getKVFilePath)({
16
+ const path = (0, filepath_1.getFilePath)({
16
17
  filename: options.path ?? url.pathname,
17
18
  root: options.root,
18
19
  defaultDocument: DEFAULT_DOCUMENT,
@@ -4,10 +4,3 @@ export declare type KVAssetOptions = {
4
4
  namespace?: KVNamespace;
5
5
  };
6
6
  export declare const getContentFromKVAsset: (path: string, options?: KVAssetOptions | undefined) => Promise<ArrayBuffer | null>;
7
- declare type FilePathOptions = {
8
- filename: string;
9
- root?: string;
10
- defaultDocument?: string;
11
- };
12
- export declare const getKVFilePath: (options: FilePathOptions) => string;
13
- export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getKVFilePath = exports.getContentFromKVAsset = void 0;
3
+ exports.getContentFromKVAsset = void 0;
4
4
  const getContentFromKVAsset = async (path, options) => {
5
5
  let ASSET_MANIFEST = {};
6
6
  if (options && options.manifest) {
@@ -37,25 +37,3 @@ const getContentFromKVAsset = async (path, options) => {
37
37
  return content;
38
38
  };
39
39
  exports.getContentFromKVAsset = getContentFromKVAsset;
40
- const getKVFilePath = (options) => {
41
- let filename = options.filename;
42
- let root = options.root || '';
43
- const defaultDocument = options.defaultDocument || 'index.html';
44
- if (filename.endsWith('/')) {
45
- // /top/ => /top/index.html
46
- filename = filename.concat(defaultDocument);
47
- }
48
- else if (!filename.match(/\.[a-zA-Z0-9]+$/)) {
49
- // /top => /top/index.html
50
- filename = filename.concat('/' + defaultDocument);
51
- }
52
- // /foo.html => foo.html
53
- filename = filename.replace(/^\.?\//, '');
54
- // assets/ => assets
55
- root = root.replace(/\/$/, '');
56
- // ./assets/foo.html => assets/foo.html
57
- let path = root ? root + '/' + filename : filename;
58
- path = path.replace(/^\.?\//, '');
59
- return path;
60
- };
61
- exports.getKVFilePath = getKVFilePath;
@@ -0,0 +1,7 @@
1
+ declare type FilePathOptions = {
2
+ filename: string;
3
+ root?: string;
4
+ defaultDocument?: string;
5
+ };
6
+ export declare const getFilePath: (options: FilePathOptions) => string;
7
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getFilePath = void 0;
4
+ const getFilePath = (options) => {
5
+ let filename = options.filename;
6
+ let root = options.root || '';
7
+ const defaultDocument = options.defaultDocument || 'index.html';
8
+ if (filename.endsWith('/')) {
9
+ // /top/ => /top/index.html
10
+ filename = filename.concat(defaultDocument);
11
+ }
12
+ else if (!filename.match(/\.[a-zA-Z0-9]+$/)) {
13
+ // /top => /top/index.html
14
+ filename = filename.concat('/' + defaultDocument);
15
+ }
16
+ // /foo.html => foo.html
17
+ filename = filename.replace(/^\.?\//, '');
18
+ // assets/ => assets
19
+ root = root.replace(/\/$/, '');
20
+ // ./assets/foo.html => assets/foo.html
21
+ let path = root ? root + '/' + filename : filename;
22
+ path = path.replace(/^\.?\//, '');
23
+ return path;
24
+ };
25
+ exports.getFilePath = getFilePath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "1.6.0",
3
+ "version": "1.6.3",
4
4
  "description": "Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,7 +11,7 @@
11
11
  "test": "jest",
12
12
  "lint": "eslint --ext js,ts src .eslintrc.js",
13
13
  "lint:fix": "eslint --ext js,ts src .eslintrc.js --fix",
14
- "denoify": "rimraf deno_dist && denoify",
14
+ "denoify": "rimraf deno_dist && denoify && rimraf 'deno_dist/**/*.test.ts'",
15
15
  "build": "rimraf dist && tsc --project tsconfig.build.esm.json && tsc --project tsconfig.build.json",
16
16
  "watch": "tsc --project tsconfig.build.json -w",
17
17
  "prerelease": "yarn denoify && yarn build",