hono 0.2.4 → 0.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/README.md CHANGED
@@ -35,7 +35,7 @@ Fastest is hono
35
35
 
36
36
  Below is a demonstration to create an application of Cloudflare Workers with Hono.
37
37
 
38
- ![Demo](https://user-images.githubusercontent.com/10682/151102477-be0f950e-8d23-49c5-b6d8-d8ecb6b7484e.gif)
38
+ ![Demo](https://user-images.githubusercontent.com/10682/151973526-342644f9-71c5-4fee-81f4-64a7558bb192.gif)
39
39
 
40
40
  ## Install
41
41
 
@@ -138,19 +138,24 @@ app.get('/fetch-url', async (c) => {
138
138
  ### Builtin Middleware
139
139
 
140
140
  ```js
141
- import { Hono, Middleware } from 'hono'
141
+ import { Hono } from 'hono'
142
+ import { poweredBy } from 'hono/powered-by'
143
+ import { logger } from 'hono/logger'
144
+ import { basicAuth } from 'hono/basicAuth'
142
145
 
143
- ...
146
+ const app = new Hono()
144
147
 
145
- app.use('*', Middleware.poweredBy())
146
- app.use('*', Middleware.logger())
148
+ app.use('*', poweredBy())
149
+ app.use('*', logger())
147
150
  app.use(
148
151
  '/auth/*',
149
- Middleware.basicAuth({
152
+ basicAuth({
150
153
  username: 'hono',
151
154
  password: 'acoolproject',
152
155
  })
153
156
  )
157
+
158
+ ...
154
159
  ```
155
160
 
156
161
  Available builtin middleware are listed on [src/middleware](https://github.com/yusukebe/hono/tree/master/src/middleware).
package/dist/compose.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.compose = void 0;
4
4
  // Based on the code in the MIT licensed `koa-compose` package.
5
5
  const compose = (middleware) => {
6
+ const errors = [];
6
7
  return function (context, next) {
7
8
  let index = -1;
8
9
  return dispatch(0);
@@ -16,7 +17,10 @@ const compose = (middleware) => {
16
17
  if (!fn)
17
18
  return Promise.resolve();
18
19
  try {
19
- return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
20
+ return Promise.resolve(fn(context, dispatch.bind(null, i + 1))).catch((e) => {
21
+ errors.push(e);
22
+ throw errors[0]; // XXX
23
+ });
20
24
  }
21
25
  catch (err) {
22
26
  return Promise.reject(err);
package/dist/hono.d.ts CHANGED
@@ -8,6 +8,7 @@ declare global {
8
8
  param: (key: string) => string;
9
9
  query: (key: string) => string | null;
10
10
  header: (name: string) => string;
11
+ parsedBody: any;
11
12
  }
12
13
  }
13
14
  export declare type Handler = (c: Context, next?: Function) => Response | Promise<Response>;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export { Hono } from './hono';
2
2
  export type { Handler, MiddlewareHandler } from './hono';
3
- export { Middleware } from './middleware';
4
3
  export { Context } from './context';
5
4
  export type { Env } from './context';
package/dist/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Context = exports.Middleware = exports.Hono = void 0;
3
+ exports.Context = exports.Hono = void 0;
4
4
  var hono_1 = require("./hono");
5
5
  Object.defineProperty(exports, "Hono", { enumerable: true, get: function () { return hono_1.Hono; } });
6
- var middleware_1 = require("./middleware");
7
- Object.defineProperty(exports, "Middleware", { enumerable: true, get: function () { return middleware_1.Middleware; } });
8
6
  var context_1 = require("./context");
9
7
  Object.defineProperty(exports, "Context", { enumerable: true, get: function () { return context_1.Context; } });
@@ -18,23 +18,20 @@ const auth = (req) => {
18
18
  if (!match) {
19
19
  return undefined;
20
20
  }
21
- const userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1]));
21
+ const userPass = USER_PASS_REGEXP.exec((0, buffer_1.decodeBase64)(match[1]));
22
22
  if (!userPass) {
23
23
  return undefined;
24
24
  }
25
25
  return { username: userPass[1], password: userPass[2] };
26
26
  };
27
- function decodeBase64(str) {
28
- return Buffer.from(str, 'base64').toString();
29
- }
30
27
  const basicAuth = (options) => {
31
28
  if (!options.realm) {
32
29
  options.realm = 'Secure Area';
33
30
  }
34
31
  return async (ctx, next) => {
35
32
  const user = auth(ctx.req);
36
- const usernameEqual = user && await (0, buffer_1.timingSafeEqual)(options.username, user.username);
37
- const passwordEqual = user && await (0, buffer_1.timingSafeEqual)(options.password, user.password);
33
+ const usernameEqual = user && (await (0, buffer_1.timingSafeEqual)(options.username, user.username));
34
+ const passwordEqual = user && (await (0, buffer_1.timingSafeEqual)(options.password, user.password));
38
35
  if (!user || !usernameEqual || !passwordEqual) {
39
36
  ctx.res = new Response('Unauthorized', {
40
37
  status: 401,
@@ -1,7 +1,2 @@
1
1
  import type { Context } from '../../context';
2
- declare global {
3
- interface Request {
4
- parsedBody: any;
5
- }
6
- }
7
2
  export declare const bodyParse: () => (ctx: Context, next: Function) => Promise<void>;
@@ -4,16 +4,13 @@ exports.mustache = void 0;
4
4
  const cloudflare_1 = require("../../utils/cloudflare");
5
5
  const EXTENSION = '.mustache';
6
6
  const mustache = () => {
7
- let Mustache;
8
- try {
9
- Mustache = require('mustache');
10
- }
11
- catch (_a) {
12
- // Do nothing.
13
- }
14
7
  return async (c, next) => {
15
- if (!Mustache) {
16
- throw new Error('If you want to use Mustache Middleware, install mustache module.');
8
+ let Mustache;
9
+ try {
10
+ Mustache = require('mustache');
11
+ }
12
+ catch (_a) {
13
+ throw new Error('If you want to use Mustache Middleware, install "mustache" package first.');
17
14
  }
18
15
  c.render = async (filename, view = {}, options) => {
19
16
  const buffer = await (0, cloudflare_1.getContentFromKVAsset)(`${filename}${EXTENSION}`);
@@ -1,28 +1,3 @@
1
1
  export declare class Middleware {
2
2
  static default: (c: import("./context").Context, next: Function) => Promise<void>;
3
- static poweredBy: () => (c: import("./context").Context, next: Function) => Promise<void>;
4
- static logger: (fn?: {
5
- (...data: any[]): void;
6
- (...data: any[]): void;
7
- (message?: any, ...optionalParams: any[]): void;
8
- }) => (c: import("./context").Context, next: Function) => Promise<void>;
9
- static basicAuth: (options: {
10
- username: string;
11
- password: string;
12
- realm?: string;
13
- }) => (ctx: import("./context").Context, next: Function) => Promise<any>;
14
- static bodyParse: () => (ctx: import("./context").Context, next: Function) => Promise<void>;
15
- static cors: (options?: {
16
- origin: string;
17
- allowMethods?: string[];
18
- allowHeaders?: string[];
19
- maxAge?: number;
20
- credentials?: boolean;
21
- exposeHeaders?: string[];
22
- }) => (c: import("./context").Context, next: Function) => Promise<void>;
23
- static cookie: () => (c: import("./context").Context, next: Function) => Promise<void>;
24
- static mustache: () => (c: import("./context").Context, next: Function) => Promise<void>;
25
- static serveStatic: (opt?: {
26
- root: string;
27
- }) => (c: import("./context").Context, next: Function) => Promise<void>;
28
3
  }
@@ -2,23 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Middleware = void 0;
4
4
  const default_1 = require("./middleware/default");
5
- const powered_by_1 = require("./middleware/powered-by/powered-by");
6
- const logger_1 = require("./middleware/logger/logger");
7
- const basic_auth_1 = require("./middleware/basic-auth/basic-auth");
8
- const body_parse_1 = require("./middleware/body-parse/body-parse");
9
- const cors_1 = require("./middleware/cors/cors");
10
- const cookie_1 = require("./middleware/cookie/cookie");
11
- const mustache_1 = require("./middleware/mustache/mustache");
12
- const serve_static_1 = require("./middleware/serve-static/serve-static");
13
5
  class Middleware {
14
6
  }
15
7
  exports.Middleware = Middleware;
16
8
  Middleware.default = default_1.defaultMiddleware;
17
- Middleware.poweredBy = powered_by_1.poweredBy;
18
- Middleware.logger = logger_1.logger;
19
- Middleware.basicAuth = basic_auth_1.basicAuth;
20
- Middleware.bodyParse = body_parse_1.bodyParse;
21
- Middleware.cors = cors_1.cors;
22
- Middleware.cookie = cookie_1.cookie;
23
- Middleware.mustache = mustache_1.mustache;
24
- Middleware.serveStatic = serve_static_1.serveStatic;
@@ -1,2 +1,4 @@
1
1
  export declare const equal: (a: ArrayBuffer, b: ArrayBuffer) => boolean;
2
+ export declare const decodeBase64: (str: string) => any;
3
+ export declare const sha256: (a: string) => Promise<string>;
2
4
  export declare const timingSafeEqual: (a: any, b: any) => Promise<boolean>;
@@ -1,6 +1,25 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
2
21
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.timingSafeEqual = exports.equal = void 0;
22
+ exports.timingSafeEqual = exports.sha256 = exports.decodeBase64 = exports.equal = void 0;
4
23
  const equal = (a, b) => {
5
24
  if (a === b) {
6
25
  return true;
@@ -19,13 +38,50 @@ const equal = (a, b) => {
19
38
  return true;
20
39
  };
21
40
  exports.equal = equal;
41
+ const decodeBase64 = (str) => {
42
+ try {
43
+ const text = atob(str);
44
+ const length = text.length;
45
+ const bytes = new Uint8Array(length);
46
+ for (let i = 0; i < length; i++) {
47
+ bytes[i] = text.charCodeAt(i);
48
+ }
49
+ const decoder = new TextDecoder();
50
+ return decoder.decode(bytes);
51
+ }
52
+ catch (_a) { }
53
+ try {
54
+ const { Buffer } = require('buffer');
55
+ return Buffer.from(str, 'base64').toString();
56
+ }
57
+ catch (e) {
58
+ console.error('If you want to do "decodeBase64", polyfill "buffer" module.');
59
+ throw e;
60
+ }
61
+ };
62
+ exports.decodeBase64 = decodeBase64;
63
+ const sha256 = async (a) => {
64
+ if (crypto && crypto.subtle) {
65
+ const buffer = await crypto.subtle.digest({
66
+ name: 'SHA-256',
67
+ }, new TextEncoder().encode(String(a)));
68
+ const hash = Array.prototype.map.call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2)).join('');
69
+ return hash;
70
+ }
71
+ try {
72
+ const crypto = await Promise.resolve().then(() => __importStar(require('crypto')));
73
+ const hash = crypto.createHash('sha256').update(a).digest('hex');
74
+ return hash;
75
+ }
76
+ catch (e) {
77
+ console.error('If you want to do "sha256", polyfill "crypto" module.');
78
+ throw e;
79
+ }
80
+ };
81
+ exports.sha256 = sha256;
22
82
  const timingSafeEqual = async (a, b) => {
23
- const sa = await crypto.subtle.digest({
24
- name: 'SHA-256',
25
- }, new TextEncoder().encode(String(a)));
26
- const sb = await crypto.subtle.digest({
27
- name: 'SHA-256',
28
- }, new TextEncoder().encode(String(b)));
29
- return (0, exports.equal)(sa, sb) && a === b;
83
+ const sa = await (0, exports.sha256)(a);
84
+ const sb = await (0, exports.sha256)(b);
85
+ return sa === sb && a === b;
30
86
  };
31
87
  exports.timingSafeEqual = timingSafeEqual;
package/package.json CHANGED
@@ -1,12 +1,51 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "0.2.4",
3
+ "version": "0.3.3",
4
4
  "description": "[炎] Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
8
8
  "dist"
9
9
  ],
10
+ "exports": {
11
+ ".": "./dist/index.js",
12
+ "./basic-auth": "./dist/middleware/basic-auth/basic-auth.js",
13
+ "./body-parse": "./dist/middleware/body-parse/body-parse.js",
14
+ "./cookie": "./dist/middleware/cookie/cookie.js",
15
+ "./cors": "./dist/middleware/cors/cors.js",
16
+ "./logger": "./dist/middleware/logger/logger.js",
17
+ "./mustache": "./dist/middleware/mustache/mustache.js",
18
+ "./powered-by": "./dist/middleware/powered-by/powered-by.js",
19
+ "./serve-static": "./dist/middleware/serve-static/serve-static.js"
20
+ },
21
+ "typesVersions": {
22
+ "*": {
23
+ "basic-auth": [
24
+ "./dist/middleware/basic-auth/basic-auth.d.ts"
25
+ ],
26
+ "body-parse": [
27
+ "./dist/middleware/body-parse/body-parse.d.ts"
28
+ ],
29
+ "cookie": [
30
+ "./dist/middleware/cookie/cookie.d.ts"
31
+ ],
32
+ "cors": [
33
+ "./dist/middleware/cors/cors.d.ts"
34
+ ],
35
+ "logger": [
36
+ "./dist/middleware/logger/logger.d.ts"
37
+ ],
38
+ "mustache": [
39
+ "./dist/middleware/mustache/mustache.d.ts"
40
+ ],
41
+ "powered-by": [
42
+ "./dist/middleware/powered-by/powered-by.d.ts"
43
+ ],
44
+ "serve-static": [
45
+ "./dist/middleware/serve-static/serve-static.d.ts"
46
+ ]
47
+ }
48
+ },
10
49
  "scripts": {
11
50
  "test": "jest",
12
51
  "lint": "eslint --ext js,ts src .eslintrc.js test",
@@ -55,7 +94,7 @@
55
94
  "mustache": "^4.2.0",
56
95
  "rimraf": "^3.0.2",
57
96
  "ts-jest": "^27.1.2",
58
- "typescript": "^4.5.4"
97
+ "typescript": "^4.5.5"
59
98
  },
60
99
  "engines": {
61
100
  "node": ">=11.0.0"