hono 0.3.0 → 0.3.4

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/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);
@@ -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,
@@ -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,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,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.timingSafeEqual = exports.equal = void 0;
3
+ exports.timingSafeEqual = exports.sha256 = exports.decodeBase64 = exports.equal = void 0;
4
4
  const equal = (a, b) => {
5
5
  if (a === b) {
6
6
  return true;
@@ -19,13 +19,50 @@ const equal = (a, b) => {
19
19
  return true;
20
20
  };
21
21
  exports.equal = equal;
22
+ const decodeBase64 = (str) => {
23
+ try {
24
+ const text = atob(str);
25
+ const length = text.length;
26
+ const bytes = new Uint8Array(length);
27
+ for (let i = 0; i < length; i++) {
28
+ bytes[i] = text.charCodeAt(i);
29
+ }
30
+ const decoder = new TextDecoder();
31
+ return decoder.decode(bytes);
32
+ }
33
+ catch (_a) { }
34
+ try {
35
+ const { Buffer } = require('buffer');
36
+ return Buffer.from(str, 'base64').toString();
37
+ }
38
+ catch (e) {
39
+ console.error('If you want to do "decodeBase64", polyfill "buffer" module.');
40
+ throw e;
41
+ }
42
+ };
43
+ exports.decodeBase64 = decodeBase64;
44
+ const sha256 = async (a) => {
45
+ if (crypto && crypto.subtle) {
46
+ const buffer = await crypto.subtle.digest({
47
+ name: 'SHA-256',
48
+ }, new TextEncoder().encode(String(a)));
49
+ const hash = Array.prototype.map.call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2)).join('');
50
+ return hash;
51
+ }
52
+ try {
53
+ const crypto = require('crypto');
54
+ const hash = crypto.createHash('sha256').update(a).digest('hex');
55
+ return hash;
56
+ }
57
+ catch (e) {
58
+ console.error('If you want to do "sha256", polyfill "crypto" module.');
59
+ throw e;
60
+ }
61
+ };
62
+ exports.sha256 = sha256;
22
63
  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;
64
+ const sa = await (0, exports.sha256)(a);
65
+ const sb = await (0, exports.sha256)(b);
66
+ return sa === sb && a === b;
30
67
  };
31
68
  exports.timingSafeEqual = timingSafeEqual;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "0.3.0",
3
+ "version": "0.3.4",
4
4
  "description": "[炎] Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",