hono 0.0.16 → 0.1.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/dist/context.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Context = void 0;
4
- const util_1 = require("./util");
4
+ const url_1 = require("./utils/url");
5
5
  class Context {
6
6
  constructor(req, opts) {
7
7
  this.req = req;
@@ -49,7 +49,7 @@ class Context {
49
49
  if (typeof location !== 'string') {
50
50
  throw new TypeError('location must be a string!');
51
51
  }
52
- if (!(0, util_1.isAbsoluteURL)(location)) {
52
+ if (!(0, url_1.isAbsoluteURL)(location)) {
53
53
  const url = new URL(this.req.url);
54
54
  url.pathname = location;
55
55
  location = url.toString();
package/dist/hono.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Hono = exports.Router = void 0;
4
4
  const node_1 = require("./node");
5
5
  const compose_1 = require("./compose");
6
- const util_1 = require("./util");
6
+ const url_1 = require("./utils/url");
7
7
  const middleware_1 = require("./middleware");
8
8
  const context_1 = require("./context");
9
9
  const METHOD_NAME_OF_ALL = 'ALL';
@@ -94,7 +94,7 @@ class Hono {
94
94
  return this.router.match(method, path);
95
95
  }
96
96
  async dispatch(request, env, event) {
97
- const [method, path] = [request.method, (0, util_1.getPathFromURL)(request.url)];
97
+ const [method, path] = [request.method, (0, url_1.getPathFromURL)(request.url)];
98
98
  const result = await this.matchRoute(method, path);
99
99
  request.params = (key) => {
100
100
  if (result) {
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.basicAuth = void 0;
4
- const util_1 = require("../../util");
4
+ const buffer_1 = require("../../utils/buffer");
5
5
  const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/;
6
6
  const USER_PASS_REGEXP = /^([^:]*):(.*)$/;
7
7
  const auth = (req) => {
@@ -33,8 +33,8 @@ const basicAuth = (options) => {
33
33
  }
34
34
  return async (ctx, next) => {
35
35
  const user = auth(ctx.req);
36
- const usernameEqual = user && await (0, util_1.timingSafeEqual)(options.username, user.username);
37
- const passwordEqual = user && await (0, util_1.timingSafeEqual)(options.password, user.password);
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);
38
38
  if (!user || !usernameEqual || !passwordEqual) {
39
39
  ctx.res = new Response('Unauthorized', {
40
40
  status: 401,
@@ -8,21 +8,16 @@ const defaultMiddleware = async (c, next) => {
8
8
  return url.searchParams.get(key);
9
9
  };
10
10
  await next();
11
- /*
12
- TODO:
13
- Adding Content-Length header make it more slower.
14
- This should not be default middleware...
15
11
  if (c.res.body) {
16
- // Do not clone Response, ex: c.res.clone().arrayBuffer()
17
- const response = new Response(c.res.body, {
18
- status: c.res.status,
19
- statusText: c.res.statusText,
20
- headers: c.res.headers,
21
- })
22
- c.res = response
23
- const buff = await c.res.clone().arrayBuffer()
24
- c.res.headers.append('Content-Length', buff.byteLength.toString())
12
+ // Do not clone Response, ex: c.res.clone().arrayBuffer()
13
+ const buffer = await c.res.arrayBuffer();
14
+ const res = new Response(buffer, {
15
+ status: c.res.status,
16
+ statusText: c.res.statusText,
17
+ headers: c.res.headers,
18
+ });
19
+ res.headers.append('Content-Length', buffer.byteLength.toString());
20
+ c.res = res;
25
21
  }
26
- */
27
22
  };
28
23
  exports.defaultMiddleware = defaultMiddleware;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.logger = void 0;
4
- const util_1 = require("../../util");
4
+ const url_1 = require("../../utils/url");
5
5
  const humanize = (n, opts) => {
6
6
  const options = opts || {};
7
7
  const d = options.delimiter || ',';
@@ -33,16 +33,16 @@ const colorStatus = (status = 0) => {
33
33
  };
34
34
  return out[(status / 100) | 0];
35
35
  };
36
- function log(fn, prefix, method, path, status, elasped) {
36
+ function log(fn, prefix, method, path, status, elasped, contentLength) {
37
37
  const out = prefix === LogPrefix.Incoming
38
38
  ? ` ${prefix} ${method} ${path}`
39
- : ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elasped}`;
39
+ : ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elasped} ${contentLength}`;
40
40
  fn(out);
41
41
  }
42
42
  const logger = (fn = console.log) => {
43
43
  return async (c, next) => {
44
44
  const { method } = c.req;
45
- const path = (0, util_1.getPathFromURL)(c.req.url);
45
+ const path = (0, url_1.getPathFromURL)(c.req.url);
46
46
  log(fn, LogPrefix.Incoming, method, path);
47
47
  const start = Date.now();
48
48
  try {
@@ -52,7 +52,13 @@ const logger = (fn = console.log) => {
52
52
  log(fn, LogPrefix.Error, method, path, c.res.status || 500, time(start));
53
53
  throw e;
54
54
  }
55
- log(fn, LogPrefix.Outgoing, method, path, c.res.status, time(start));
55
+ const len = parseFloat(c.res.headers.get('Content-Length'));
56
+ const contentLength = isNaN(len)
57
+ ? '0'
58
+ : len < 1024
59
+ ? `${len}b`
60
+ : `${len / 1024}kB`;
61
+ log(fn, LogPrefix.Outgoing, method, path, c.res.status, time(start), contentLength);
56
62
  };
57
63
  };
58
64
  exports.logger = logger;
package/dist/node.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Node = exports.Result = void 0;
4
- const util_1 = require("./util");
4
+ const url_1 = require("./utils/url");
5
5
  const METHOD_NAME_OF_ALL = 'ALL';
6
6
  class Result {
7
7
  constructor(handler, params) {
@@ -25,7 +25,7 @@ class Node {
25
25
  insert(method, path, handler) {
26
26
  // eslint-disable-next-line @typescript-eslint/no-this-alias
27
27
  let curNode = this;
28
- const parts = (0, util_1.splitPath)(path);
28
+ const parts = (0, url_1.splitPath)(path);
29
29
  for (let i = 0, len = parts.length; i < len; i++) {
30
30
  const p = parts[i];
31
31
  if (Object.keys(curNode.children).includes(p)) {
@@ -42,7 +42,7 @@ class Node {
42
42
  // eslint-disable-next-line @typescript-eslint/no-this-alias
43
43
  let curNode = this;
44
44
  const params = {};
45
- const parts = (0, util_1.splitPath)(path);
45
+ const parts = (0, url_1.splitPath)(path);
46
46
  for (let i = 0, len = parts.length; i < len; i++) {
47
47
  const p = parts[i];
48
48
  // '*' => match any path
@@ -74,7 +74,7 @@ class Node {
74
74
  isWildcard = true;
75
75
  break;
76
76
  }
77
- const pattern = (0, util_1.getPattern)(key);
77
+ const pattern = (0, url_1.getPattern)(key);
78
78
  // Named match
79
79
  if (pattern) {
80
80
  const match = p.match(new RegExp(pattern[1]));
@@ -0,0 +1,2 @@
1
+ export declare const equal: (a: ArrayBuffer, b: ArrayBuffer) => boolean;
2
+ export declare const timingSafeEqual: (a: any, b: any) => Promise<boolean>;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.timingSafeEqual = exports.equal = void 0;
4
+ const equal = (a, b) => {
5
+ if (a === b) {
6
+ return true;
7
+ }
8
+ if (a.byteLength !== b.byteLength) {
9
+ return false;
10
+ }
11
+ const va = new DataView(a);
12
+ const vb = new DataView(b);
13
+ let i = va.byteLength;
14
+ while (i--) {
15
+ if (va.getUint8(i) !== vb.getUint8(i)) {
16
+ return false;
17
+ }
18
+ }
19
+ return true;
20
+ };
21
+ exports.equal = equal;
22
+ 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;
30
+ };
31
+ exports.timingSafeEqual = timingSafeEqual;
@@ -2,4 +2,3 @@ export declare const splitPath: (path: string) => string[];
2
2
  export declare const getPattern: (label: string) => string[] | null;
3
3
  export declare const getPathFromURL: (url: string) => string;
4
4
  export declare const isAbsoluteURL: (url: string) => boolean;
5
- 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.isAbsoluteURL = exports.getPathFromURL = exports.getPattern = exports.splitPath = void 0;
3
+ exports.isAbsoluteURL = exports.getPathFromURL = exports.getPattern = exports.splitPath = void 0;
4
4
  const URL_REGEXP = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;
5
5
  const splitPath = (path) => {
6
6
  const paths = path.split(/\//); // faster than path.split('/')
@@ -27,7 +27,6 @@ const getPattern = (label) => {
27
27
  };
28
28
  exports.getPattern = getPattern;
29
29
  const getPathFromURL = (url) => {
30
- // XXX
31
30
  const match = url.match(URL_REGEXP);
32
31
  if (match) {
33
32
  return match[5];
@@ -43,30 +42,3 @@ const isAbsoluteURL = (url) => {
43
42
  return false;
44
43
  };
45
44
  exports.isAbsoluteURL = isAbsoluteURL;
46
- const bufferEqual = (a, b) => {
47
- if (a === b) {
48
- return true;
49
- }
50
- if (a.byteLength !== b.byteLength) {
51
- return false;
52
- }
53
- const va = new DataView(a);
54
- const vb = new DataView(b);
55
- let i = va.byteLength;
56
- while (i--) {
57
- if (va.getUint8(i) !== vb.getUint8(i)) {
58
- return false;
59
- }
60
- }
61
- return true;
62
- };
63
- const timingSafeEqual = async (a, b) => {
64
- const sa = await crypto.subtle.digest({
65
- name: 'SHA-256',
66
- }, new TextEncoder().encode(String(a)));
67
- const sb = await crypto.subtle.digest({
68
- name: 'SHA-256',
69
- }, new TextEncoder().encode(String(b)));
70
- return bufferEqual(sa, sb) && a === b;
71
- };
72
- exports.timingSafeEqual = timingSafeEqual;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "0.0.16",
3
+ "version": "0.1.0",
4
4
  "description": "[炎] Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",