hono 1.3.1 → 1.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
@@ -1,9 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compose = void 0;
4
- const context_1 = require("./context");
1
+ import { Context } from './context';
5
2
  // Based on the code in the MIT licensed `koa-compose` package.
6
- const compose = (middleware, onError, onNotFound) => {
3
+ export const compose = (middleware, onError, onNotFound) => {
7
4
  return async (context, next) => {
8
5
  let index = -1;
9
6
  return dispatch(0);
@@ -16,7 +13,7 @@ const compose = (middleware, onError, onNotFound) => {
16
13
  if (i === middleware.length)
17
14
  handler = next;
18
15
  if (handler === undefined) {
19
- if (context instanceof context_1.Context && context.res === undefined) {
16
+ if (context instanceof Context && context.res === undefined) {
20
17
  context.res = onNotFound(context);
21
18
  }
22
19
  return Promise.resolve(context);
@@ -24,14 +21,14 @@ const compose = (middleware, onError, onNotFound) => {
24
21
  return Promise.resolve(handler(context, dispatch.bind(null, i + 1)))
25
22
  .then(async (res) => {
26
23
  // If handler return Response like `return c.text('foo')`
27
- if (res && context instanceof context_1.Context) {
24
+ if (res && context instanceof Context) {
28
25
  context.res = res;
29
26
  dispatch(i + 1); // <--- Call next()
30
27
  }
31
28
  return context;
32
29
  })
33
30
  .catch((err) => {
34
- if (onError && context instanceof context_1.Context) {
31
+ if (onError && context instanceof Context) {
35
32
  if (err instanceof Error) {
36
33
  context.res = onError(err, context);
37
34
  }
@@ -44,4 +41,3 @@ const compose = (middleware, onError, onNotFound) => {
44
41
  }
45
42
  };
46
43
  };
47
- exports.compose = compose;
package/dist/context.d.ts CHANGED
@@ -28,7 +28,7 @@ export declare class Context<RequestParamKeyType extends string = string, E = En
28
28
  get(key: string): any;
29
29
  pretty(prettyJSON: boolean, space?: number): void;
30
30
  newResponse(data: Data, init?: ResponseInit): Response;
31
- body(data: Data, status?: StatusCode, headers?: Headers): Response;
31
+ body(data: Data | null, status?: StatusCode, headers?: Headers): Response;
32
32
  text(text: string, status?: StatusCode, headers?: Headers): Response;
33
33
  json(object: object, status?: StatusCode, headers?: Headers): Response;
34
34
  html(html: string, status?: StatusCode, headers?: Headers): Response;
package/dist/context.js CHANGED
@@ -1,9 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Context = void 0;
4
- const http_status_1 = require("./utils/http-status");
5
- const url_1 = require("./utils/url");
6
- class Context {
1
+ import { getStatusText } from './utils/http-status';
2
+ import { isAbsoluteURL } from './utils/url';
3
+ export class Context {
7
4
  constructor(req, opts) {
8
5
  this.res = undefined;
9
6
  this._headers = {};
@@ -51,7 +48,7 @@ class Context {
51
48
  }
52
49
  status(status) {
53
50
  this._status = status;
54
- this._statusText = (0, http_status_1.getStatusText)(status);
51
+ this._statusText = getStatusText(status);
55
52
  }
56
53
  set(key, value) {
57
54
  this._map[key] = value;
@@ -66,8 +63,8 @@ class Context {
66
63
  newResponse(data, init = {}) {
67
64
  init.status = init.status || this._status || 200;
68
65
  init.statusText =
69
- init.statusText || this._statusText || (0, http_status_1.getStatusText)(init.status);
70
- init.headers = Object.assign(Object.assign({}, this._headers), init.headers);
66
+ init.statusText || this._statusText || getStatusText(init.status);
67
+ init.headers = { ...this._headers, ...init.headers };
71
68
  return new Response(data, init);
72
69
  }
73
70
  body(data, status = this._status, headers = this._headers) {
@@ -104,7 +101,7 @@ class Context {
104
101
  if (typeof location !== 'string') {
105
102
  throw new TypeError('location must be a string!');
106
103
  }
107
- if (!(0, url_1.isAbsoluteURL)(location)) {
104
+ if (!isAbsoluteURL(location)) {
108
105
  const url = new URL(this.req.url);
109
106
  url.pathname = location;
110
107
  location = url.toString();
@@ -117,4 +114,3 @@ class Context {
117
114
  });
118
115
  }
119
116
  }
120
- exports.Context = Context;
package/dist/hono.js CHANGED
@@ -1,21 +1,18 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Hono = void 0;
4
- const compose_1 = require("./compose");
5
- const context_1 = require("./context");
6
- const router_1 = require("./router");
7
- const router_2 = require("./router");
8
- const trie_router_1 = require("./router/trie-router"); // Default Router
9
- const url_1 = require("./utils/url");
1
+ import { compose } from './compose';
2
+ import { Context } from './context';
3
+ import { METHOD_NAME_ALL } from './router';
4
+ import { METHOD_NAME_ALL_LOWERCASE } from './router';
5
+ import { TrieRouter } from './router/trie-router'; // Default Router
6
+ import { getPathFromURL, mergePath } from './utils/url';
10
7
  const methods = ['get', 'post', 'put', 'delete', 'head', 'options', 'patch'];
11
8
  function defineDynamicClass() {
12
9
  return class {
13
10
  };
14
11
  }
15
- class Hono extends defineDynamicClass() {
12
+ export class Hono extends defineDynamicClass() {
16
13
  constructor(init = {}) {
17
14
  super();
18
- this.routerClass = trie_router_1.TrieRouter;
15
+ this.routerClass = TrieRouter;
19
16
  this.strict = true; // strict routing - default is true
20
17
  this.path = '/';
21
18
  this.routes = [];
@@ -28,7 +25,7 @@ class Hono extends defineDynamicClass() {
28
25
  const message = 'Internal Server Error';
29
26
  return c.text(message, 500);
30
27
  };
31
- const allMethods = [...methods, router_2.METHOD_NAME_ALL_LOWERCASE];
28
+ const allMethods = [...methods, METHOD_NAME_ALL_LOWERCASE];
32
29
  allMethods.map((method) => {
33
30
  this[method] = (args1, ...args) => {
34
31
  if (typeof args1 === 'string') {
@@ -55,6 +52,7 @@ class Hono extends defineDynamicClass() {
55
52
  app.routes.map((r) => {
56
53
  this.addRoute(r.method, r.path, r.handler);
57
54
  });
55
+ this._tempPath = null;
58
56
  }
59
57
  return this;
60
58
  }
@@ -66,7 +64,7 @@ class Hono extends defineDynamicClass() {
66
64
  handlers.unshift(arg1);
67
65
  }
68
66
  handlers.map((handler) => {
69
- this.addRoute(router_1.METHOD_NAME_ALL, this.path, handler);
67
+ this.addRoute(METHOD_NAME_ALL, this.path, handler);
70
68
  });
71
69
  return this;
72
70
  }
@@ -81,7 +79,7 @@ class Hono extends defineDynamicClass() {
81
79
  addRoute(method, path, handler) {
82
80
  method = method.toUpperCase();
83
81
  if (this._tempPath) {
84
- path = (0, url_1.mergePath)(this._tempPath, path);
82
+ path = mergePath(this._tempPath, path);
85
83
  }
86
84
  this._router.add(method, path, handler);
87
85
  const r = { path: path, method: method, handler: handler };
@@ -91,7 +89,7 @@ class Hono extends defineDynamicClass() {
91
89
  return this._router.match(method, path);
92
90
  }
93
91
  async dispatch(request, event, env) {
94
- const path = (0, url_1.getPathFromURL)(request.url, { strict: this.strict });
92
+ const path = getPathFromURL(request.url, { strict: this.strict });
95
93
  const method = request.method;
96
94
  const result = await this.matchRoute(method, path);
97
95
  request.param = ((key) => {
@@ -105,9 +103,9 @@ class Hono extends defineDynamicClass() {
105
103
  }
106
104
  });
107
105
  const handlers = result ? result.handlers : [this.notFoundHandler];
108
- const c = new context_1.Context(request, { env: env, event: event, res: undefined });
106
+ const c = new Context(request, { env: env, event: event, res: undefined });
109
107
  c.notFound = () => this.notFoundHandler(c);
110
- const composed = (0, compose_1.compose)(handlers, this.errorHandler, this.notFoundHandler);
108
+ const composed = compose(handlers, this.errorHandler, this.notFoundHandler);
111
109
  let context;
112
110
  try {
113
111
  context = await composed(c);
@@ -137,4 +135,3 @@ class Hono extends defineDynamicClass() {
137
135
  });
138
136
  }
139
137
  }
140
- exports.Hono = Hono;
@@ -1,7 +1 @@
1
- import type { Context } from '../../context';
2
- import type { Next } from '../../hono';
3
- declare type Init = {
4
- root: string;
5
- };
6
- export declare const mustache: (init?: Init) => (c: Context, next: Next) => Promise<void>;
7
- export {};
1
+ export { mustache } from './mustache';
@@ -1,51 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mustache = void 0;
4
- const buffer_1 = require("../../utils/buffer");
5
- const cloudflare_1 = require("../../utils/cloudflare");
6
- const EXTENSION = '.mustache';
7
- const DEFAULT_DOCUMENT = 'index.mustache';
8
- const mustache = (init = { root: '' }) => {
9
- const { root } = init;
10
- return async (c, next) => {
11
- let Mustache;
12
- try {
13
- Mustache = require('mustache');
14
- }
15
- catch (_a) {
16
- throw new Error('If you want to use Mustache Middleware, install "mustache" package first.');
17
- }
18
- c.render = async (filename, params = {}, options) => {
19
- const path = (0, cloudflare_1.getKVFilePath)({
20
- filename: `${filename}${EXTENSION}`,
21
- root: root,
22
- defaultDocument: DEFAULT_DOCUMENT,
23
- });
24
- const buffer = await (0, cloudflare_1.getContentFromKVAsset)(path);
25
- if (!buffer) {
26
- throw new Error(`Template "${path}" is not found or blank.`);
27
- }
28
- const content = (0, buffer_1.bufferToString)(buffer);
29
- const partialArgs = {};
30
- if (options) {
31
- const partials = options;
32
- for (const key of Object.keys(partials)) {
33
- const partialPath = (0, cloudflare_1.getKVFilePath)({
34
- filename: `${partials[key]}${EXTENSION}`,
35
- root: root,
36
- defaultDocument: DEFAULT_DOCUMENT,
37
- });
38
- const partialBuffer = await (0, cloudflare_1.getContentFromKVAsset)(partialPath);
39
- if (!partialBuffer) {
40
- throw new Error(`Partial Template "${partialPath}" is not found or blank.`);
41
- }
42
- partialArgs[key] = (0, buffer_1.bufferToString)(partialBuffer);
43
- }
44
- }
45
- const output = Mustache.render(content, params, partialArgs);
46
- return c.html(output);
47
- };
48
- await next();
49
- };
50
- };
51
- exports.mustache = mustache;
4
+ var mustache_1 = require("./mustache");
5
+ Object.defineProperty(exports, "mustache", { enumerable: true, get: function () { return mustache_1.mustache; } });
@@ -0,0 +1,3 @@
1
+ import type { MustacheOptions } from './mustache';
2
+ declare const module: (options?: MustacheOptions) => import("../../hono").Handler<string, import("../../context").Env>;
3
+ export { module as mustache };
@@ -0,0 +1,12 @@
1
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2
+ // @ts-ignore
3
+ // For ES module mode
4
+ import manifest from '__STATIC_CONTENT_MANIFEST';
5
+ import { mustache } from './mustache';
6
+ const module = (options = { root: '' }) => {
7
+ return mustache({
8
+ root: options.root,
9
+ manifest: options.manifest ? options.manifest : manifest,
10
+ });
11
+ };
12
+ export { module as mustache };
@@ -0,0 +1,8 @@
1
+ /// <reference types="@cloudflare/workers-types" />
2
+ import type { Handler } from '../../hono';
3
+ export declare type MustacheOptions = {
4
+ root: string;
5
+ manifest?: object | string;
6
+ namespace?: KVNamespace;
7
+ };
8
+ export declare const mustache: (init?: MustacheOptions) => Handler;
@@ -0,0 +1,51 @@
1
+ import { bufferToString } from '../../utils/buffer';
2
+ import { getContentFromKVAsset, getKVFilePath } from '../../utils/cloudflare';
3
+ const EXTENSION = '.mustache';
4
+ const DEFAULT_DOCUMENT = 'index.mustache';
5
+ export const mustache = (init = { root: '' }) => {
6
+ const { root } = init;
7
+ return async (c, next) => {
8
+ let Mustache;
9
+ try {
10
+ Mustache = require('mustache');
11
+ }
12
+ catch {
13
+ throw new Error('If you want to use Mustache Middleware, install "mustache" package first.');
14
+ }
15
+ c.render = async (filename, params = {}, options) => {
16
+ const path = getKVFilePath({
17
+ filename: `${filename}${EXTENSION}`,
18
+ root: root,
19
+ defaultDocument: DEFAULT_DOCUMENT,
20
+ });
21
+ const kvAssetOptions = {
22
+ manifest: init.manifest,
23
+ namespace: init.namespace ? init.namespace : c.env ? c.env.__STATIC_CONTENT : undefined,
24
+ };
25
+ const buffer = await getContentFromKVAsset(path, kvAssetOptions);
26
+ if (!buffer) {
27
+ throw new Error(`Template "${path}" is not found or blank.`);
28
+ }
29
+ const content = bufferToString(buffer);
30
+ const partialArgs = {};
31
+ if (options) {
32
+ const partials = options;
33
+ for (const key of Object.keys(partials)) {
34
+ const partialPath = getKVFilePath({
35
+ filename: `${partials[key]}${EXTENSION}`,
36
+ root: root,
37
+ defaultDocument: DEFAULT_DOCUMENT,
38
+ });
39
+ const partialBuffer = await getContentFromKVAsset(partialPath, kvAssetOptions);
40
+ if (!partialBuffer) {
41
+ throw new Error(`Partial Template "${partialPath}" is not found or blank.`);
42
+ }
43
+ partialArgs[key] = bufferToString(partialBuffer);
44
+ }
45
+ }
46
+ const output = Mustache.render(content, params, partialArgs);
47
+ return c.html(output);
48
+ };
49
+ await next();
50
+ };
51
+ };
@@ -1,7 +1 @@
1
- import type { Context } from '../../context';
2
- import type { Next } from '../../hono';
3
- declare type Options = {
4
- root: string;
5
- };
6
- export declare const serveStatic: (opt?: Options) => (c: Context, next: Next) => Promise<void>;
7
- export {};
1
+ export { serveStatic } from './serve-static';
@@ -1,30 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.serveStatic = void 0;
4
- const cloudflare_1 = require("../../utils/cloudflare");
5
- const mime_1 = require("../../utils/mime");
6
- const DEFAULT_DOCUMENT = 'index.html';
7
- // This middleware is available only on Cloudflare Workers.
8
- const serveStatic = (opt = { root: '' }) => {
9
- return async (c, next) => {
10
- await next();
11
- const url = new URL(c.req.url);
12
- const path = (0, cloudflare_1.getKVFilePath)({
13
- filename: url.pathname,
14
- root: opt.root,
15
- defaultDocument: DEFAULT_DOCUMENT,
16
- });
17
- const content = await (0, cloudflare_1.getContentFromKVAsset)(path);
18
- if (content) {
19
- const mimeType = (0, mime_1.getMimeType)(path);
20
- if (mimeType) {
21
- c.header('Content-Type', mimeType);
22
- }
23
- c.res = c.body(content);
24
- }
25
- else {
26
- console.warn(`Static file: ${path} is not found`);
27
- }
28
- };
29
- };
30
- exports.serveStatic = serveStatic;
4
+ var serve_static_1 = require("./serve-static");
5
+ Object.defineProperty(exports, "serveStatic", { enumerable: true, get: function () { return serve_static_1.serveStatic; } });
@@ -0,0 +1,3 @@
1
+ import type { ServeStaticOptions } from './serve-static';
2
+ declare const module: (options?: ServeStaticOptions) => import("../../hono").Handler<string, import("../../context").Env>;
3
+ export { module as serveStatic };
@@ -0,0 +1,12 @@
1
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2
+ // @ts-ignore
3
+ // For ES module mode
4
+ import manifest from '__STATIC_CONTENT_MANIFEST';
5
+ import { serveStatic } from './serve-static';
6
+ const module = (options = { root: '' }) => {
7
+ return serveStatic({
8
+ root: options.root,
9
+ manifest: options.manifest ? options.manifest : manifest,
10
+ });
11
+ };
12
+ export { module as serveStatic };
@@ -0,0 +1,8 @@
1
+ /// <reference types="@cloudflare/workers-types" />
2
+ import type { Handler } from '../../hono';
3
+ export declare type ServeStaticOptions = {
4
+ root: string;
5
+ manifest?: object | string;
6
+ namespace?: KVNamespace;
7
+ };
8
+ export declare const serveStatic: (options?: ServeStaticOptions) => Handler;
@@ -0,0 +1,34 @@
1
+ import { getContentFromKVAsset, getKVFilePath } from '../../utils/cloudflare';
2
+ import { getMimeType } from '../../utils/mime';
3
+ const DEFAULT_DOCUMENT = 'index.html';
4
+ // This middleware is available only on Cloudflare Workers.
5
+ export const serveStatic = (options = { root: '' }) => {
6
+ return async (c, next) => {
7
+ // Do nothing if Response is already set
8
+ if (c.res) {
9
+ await next();
10
+ }
11
+ const url = new URL(c.req.url);
12
+ const path = getKVFilePath({
13
+ filename: url.pathname,
14
+ root: options.root,
15
+ defaultDocument: DEFAULT_DOCUMENT,
16
+ });
17
+ const content = await getContentFromKVAsset(path, {
18
+ manifest: options.manifest,
19
+ namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : undefined,
20
+ });
21
+ if (content) {
22
+ const mimeType = getMimeType(path);
23
+ if (mimeType) {
24
+ c.header('Content-Type', mimeType);
25
+ }
26
+ // Return Response object
27
+ return c.body(content);
28
+ }
29
+ else {
30
+ console.warn(`Static file: ${path} is not found`);
31
+ await next();
32
+ }
33
+ };
34
+ };
@@ -1,5 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TrieRouter = void 0;
4
- var router_1 = require("./router");
5
- Object.defineProperty(exports, "TrieRouter", { enumerable: true, get: function () { return router_1.TrieRouter; } });
1
+ export { TrieRouter } from './router';
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Node = void 0;
4
- const router_1 = require("../../router");
5
- const url_1 = require("../../utils/url");
1
+ import { Result, METHOD_NAME_ALL } from '../../router';
2
+ import { splitPath, getPattern } from '../../utils/url';
6
3
  function findParam(node, name) {
7
4
  for (let i = 0, len = node.patterns.length; i < len; i++) {
8
5
  if (typeof node.patterns[i] === 'object' && node.patterns[i][1] === name) {
@@ -17,7 +14,7 @@ function findParam(node, name) {
17
14
  }
18
15
  return false;
19
16
  }
20
- class Node {
17
+ export class Node {
21
18
  constructor(method, handler, children) {
22
19
  this.children = children || {};
23
20
  this.methods = [];
@@ -31,7 +28,7 @@ class Node {
31
28
  insert(method, path, handler) {
32
29
  // eslint-disable-next-line @typescript-eslint/no-this-alias
33
30
  let curNode = this;
34
- const parts = (0, url_1.splitPath)(path);
31
+ const parts = splitPath(path);
35
32
  const parentPatterns = [];
36
33
  const errorMessage = (name) => {
37
34
  return `Duplicate param name, use another name instead of '${name}' - ${method} ${path} <--- '${name}'`;
@@ -44,7 +41,7 @@ class Node {
44
41
  continue;
45
42
  }
46
43
  curNode.children[p] = new Node();
47
- const pattern = (0, url_1.getPattern)(p);
44
+ const pattern = getPattern(p);
48
45
  if (pattern) {
49
46
  if (typeof pattern === 'object') {
50
47
  for (let j = 0, len = parentPatterns.length; j < len; j++) {
@@ -78,7 +75,7 @@ class Node {
78
75
  handlers.push(handler);
79
76
  return;
80
77
  }
81
- handler = m[router_1.METHOD_NAME_ALL];
78
+ handler = m[METHOD_NAME_ALL];
82
79
  if (handler !== undefined) {
83
80
  handlers.push(handler);
84
81
  return;
@@ -142,7 +139,7 @@ class Node {
142
139
  // eslint-disable-next-line @typescript-eslint/no-this-alias
143
140
  const curNode = this;
144
141
  let curNodes = [curNode];
145
- const parts = (0, url_1.splitPath)(path);
142
+ const parts = splitPath(path);
146
143
  for (let i = 0, len = parts.length; i < len; i++) {
147
144
  const p = parts[i];
148
145
  const isLast = i === len - 1;
@@ -160,7 +157,6 @@ class Node {
160
157
  }
161
158
  if (handlers.length <= 0)
162
159
  return null;
163
- return new router_1.Result(handlers, params);
160
+ return new Result(handlers, params);
164
161
  }
165
162
  }
166
- exports.Node = Node;
@@ -1,12 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TrieRouter = void 0;
4
- const router_1 = require("../../router");
5
- const node_1 = require("./node");
6
- class TrieRouter extends router_1.Router {
1
+ import { Router } from '../../router';
2
+ import { Node } from './node';
3
+ export class TrieRouter extends Router {
7
4
  constructor() {
8
5
  super();
9
- this.node = new node_1.Node();
6
+ this.node = new Node();
10
7
  }
11
8
  add(method, path, handler) {
12
9
  this.node.insert(method, path, handler);
@@ -15,4 +12,3 @@ class TrieRouter extends router_1.Router {
15
12
  return this.node.search(method, path);
16
13
  }
17
14
  }
18
- exports.TrieRouter = TrieRouter;
package/dist/router.js CHANGED
@@ -1,15 +1,10 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Result = exports.Router = exports.METHOD_NAME_ALL_LOWERCASE = exports.METHOD_NAME_ALL = void 0;
4
- exports.METHOD_NAME_ALL = 'ALL';
5
- exports.METHOD_NAME_ALL_LOWERCASE = 'all';
6
- class Router {
1
+ export const METHOD_NAME_ALL = 'ALL';
2
+ export const METHOD_NAME_ALL_LOWERCASE = 'all';
3
+ export class Router {
7
4
  }
8
- exports.Router = Router;
9
- class Result {
5
+ export class Result {
10
6
  constructor(handlers, params) {
11
7
  this.handlers = handlers;
12
8
  this.params = params;
13
9
  }
14
10
  }
15
- exports.Result = Result;
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.bufferToString = exports.timingSafeEqual = exports.equal = void 0;
4
- const crypto_1 = require("./crypto");
5
- const equal = (a, b) => {
1
+ import { sha256 } from './crypto';
2
+ export const equal = (a, b) => {
6
3
  if (a === b) {
7
4
  return true;
8
5
  }
@@ -19,21 +16,18 @@ const equal = (a, b) => {
19
16
  }
20
17
  return true;
21
18
  };
22
- exports.equal = equal;
23
- const timingSafeEqual = async (a, b, hashFunction) => {
19
+ export const timingSafeEqual = async (a, b, hashFunction) => {
24
20
  if (!hashFunction) {
25
- hashFunction = crypto_1.sha256;
21
+ hashFunction = sha256;
26
22
  }
27
23
  const sa = await hashFunction(a);
28
24
  const sb = await hashFunction(b);
29
25
  return sa === sb && a === b;
30
26
  };
31
- exports.timingSafeEqual = timingSafeEqual;
32
- const bufferToString = (buffer) => {
27
+ export const bufferToString = (buffer) => {
33
28
  if (buffer instanceof ArrayBuffer) {
34
29
  const enc = new TextDecoder('utf-8');
35
30
  return enc.decode(buffer);
36
31
  }
37
32
  return buffer;
38
33
  };
39
- exports.bufferToString = bufferToString;
@@ -1,8 +1,13 @@
1
- export declare const getContentFromKVAsset: (path: string) => Promise<ArrayBuffer>;
2
- declare type Options = {
1
+ /// <reference types="@cloudflare/workers-types" />
2
+ export declare type KVAssetOptions = {
3
+ manifest?: object | string;
4
+ namespace?: KVNamespace;
5
+ };
6
+ export declare const getContentFromKVAsset: (path: string, options?: KVAssetOptions) => Promise<ArrayBuffer>;
7
+ declare type FilePathOptions = {
3
8
  filename: string;
4
9
  root?: string;
5
10
  defaultDocument?: string;
6
11
  };
7
- export declare const getKVFilePath: (opt: Options) => string;
12
+ export declare const getKVFilePath: (options: FilePathOptions) => string;
8
13
  export {};
@@ -1,15 +1,28 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getKVFilePath = exports.getContentFromKVAsset = void 0;
4
- const getContentFromKVAsset = async (path) => {
5
- let ASSET_MANIFEST;
6
- if (typeof __STATIC_CONTENT_MANIFEST === 'string') {
7
- ASSET_MANIFEST = JSON.parse(__STATIC_CONTENT_MANIFEST);
1
+ export const getContentFromKVAsset = async (path, options) => {
2
+ let ASSET_MANIFEST = {};
3
+ if (options && options.manifest) {
4
+ if (typeof options.manifest === 'string') {
5
+ ASSET_MANIFEST = JSON.parse(options.manifest);
6
+ }
7
+ else {
8
+ ASSET_MANIFEST = options.manifest;
9
+ }
8
10
  }
9
11
  else {
10
- ASSET_MANIFEST = __STATIC_CONTENT_MANIFEST;
12
+ if (typeof __STATIC_CONTENT_MANIFEST === 'string') {
13
+ ASSET_MANIFEST = JSON.parse(__STATIC_CONTENT_MANIFEST);
14
+ }
15
+ else {
16
+ ASSET_MANIFEST = __STATIC_CONTENT_MANIFEST;
17
+ }
18
+ }
19
+ let ASSET_NAMESPACE;
20
+ if (options && options.namespace) {
21
+ ASSET_NAMESPACE = options.namespace;
22
+ }
23
+ else {
24
+ ASSET_NAMESPACE = __STATIC_CONTENT;
11
25
  }
12
- const ASSET_NAMESPACE = __STATIC_CONTENT;
13
26
  const key = ASSET_MANIFEST[path] || path;
14
27
  if (!key) {
15
28
  return;
@@ -20,11 +33,10 @@ const getContentFromKVAsset = async (path) => {
20
33
  }
21
34
  return content;
22
35
  };
23
- exports.getContentFromKVAsset = getContentFromKVAsset;
24
- const getKVFilePath = (opt) => {
25
- let filename = opt.filename;
26
- let root = opt.root || '';
27
- const defaultDocument = opt.defaultDocument || 'index.html';
36
+ export const getKVFilePath = (options) => {
37
+ let filename = options.filename;
38
+ let root = options.root || '';
39
+ const defaultDocument = options.defaultDocument || 'index.html';
28
40
  if (filename.endsWith('/')) {
29
41
  // /top/ => /top/index.html
30
42
  filename = filename.concat(defaultDocument);
@@ -42,4 +54,3 @@ const getKVFilePath = (opt) => {
42
54
  path = path.replace(/^\.?\//, '');
43
55
  return path;
44
56
  };
45
- exports.getKVFilePath = getKVFilePath;
@@ -1,19 +1,14 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createHash = exports.sha1 = exports.sha256 = void 0;
4
- const sha256 = async (data) => {
1
+ export const sha256 = async (data) => {
5
2
  const algorithm = { name: 'SHA-256', alias: 'sha256' };
6
- const hash = await (0, exports.createHash)(data, algorithm);
3
+ const hash = await createHash(data, algorithm);
7
4
  return hash;
8
5
  };
9
- exports.sha256 = sha256;
10
- const sha1 = async (data) => {
6
+ export const sha1 = async (data) => {
11
7
  const algorithm = { name: 'SHA-1', alias: 'sha1' };
12
- const hash = await (0, exports.createHash)(data, algorithm);
8
+ const hash = await createHash(data, algorithm);
13
9
  return hash;
14
10
  };
15
- exports.sha1 = sha1;
16
- const createHash = async (data, algorithm) => {
11
+ export const createHash = async (data, algorithm) => {
17
12
  if (crypto && crypto.subtle) {
18
13
  const buffer = await crypto.subtle.digest({
19
14
  name: algorithm.name,
@@ -33,4 +28,3 @@ const createHash = async (data, algorithm) => {
33
28
  throw e;
34
29
  }
35
30
  };
36
- exports.createHash = createHash;
@@ -1,11 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStatusText = void 0;
4
- const getStatusText = (statusCode) => {
1
+ export const getStatusText = (statusCode) => {
5
2
  const text = statuses[statusCode];
6
3
  return text;
7
4
  };
8
- exports.getStatusText = getStatusText;
9
5
  const statuses = {
10
6
  100: 'Continue',
11
7
  101: 'Switching Protocols',
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMimeType = void 0;
4
- const getMimeType = (filename) => {
1
+ export const getMimeType = (filename) => {
5
2
  const regexp = /\.([a-zA-Z0-9]+?)$/;
6
3
  const match = filename.match(regexp);
7
4
  if (!match)
@@ -12,7 +9,6 @@ const getMimeType = (filename) => {
12
9
  }
13
10
  return mimeType;
14
11
  };
15
- exports.getMimeType = getMimeType;
16
12
  const mimes = {
17
13
  aac: 'audio/aac',
18
14
  abw: 'application/x-abiword',
package/dist/utils/url.js CHANGED
@@ -1,17 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mergePath = exports.isAbsoluteURL = exports.getPathFromURL = exports.getPattern = exports.splitPath = void 0;
4
1
  const URL_REGEXP = /^https?:\/\/[a-zA-Z0-9\-\.:]+(\/?[^?#]*)/;
5
- const splitPath = (path) => {
2
+ export const splitPath = (path) => {
6
3
  const paths = path.split(/\//); // faster than path.split('/')
7
4
  if (paths[0] === '') {
8
5
  paths.shift();
9
6
  }
10
7
  return paths;
11
8
  };
12
- exports.splitPath = splitPath;
13
9
  const patternCache = {};
14
- const getPattern = (label) => {
10
+ export const getPattern = (label) => {
15
11
  // * => wildcard
16
12
  // :id{[0-9]+} => ([0-9]+)
17
13
  // :id => (.+)
@@ -33,8 +29,7 @@ const getPattern = (label) => {
33
29
  }
34
30
  return null;
35
31
  };
36
- exports.getPattern = getPattern;
37
- const getPathFromURL = (url, params = { strict: true }) => {
32
+ export const getPathFromURL = (url, params = { strict: true }) => {
38
33
  // if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same
39
34
  // default is true
40
35
  if (params.strict === false && url.endsWith('/')) {
@@ -46,16 +41,14 @@ const getPathFromURL = (url, params = { strict: true }) => {
46
41
  }
47
42
  return '';
48
43
  };
49
- exports.getPathFromURL = getPathFromURL;
50
- const isAbsoluteURL = (url) => {
44
+ export const isAbsoluteURL = (url) => {
51
45
  const match = url.match(URL_REGEXP);
52
46
  if (match) {
53
47
  return true;
54
48
  }
55
49
  return false;
56
50
  };
57
- exports.isAbsoluteURL = isAbsoluteURL;
58
- const mergePath = (...paths) => {
51
+ export const mergePath = (...paths) => {
59
52
  let p = '';
60
53
  let endsWithSlash = false;
61
54
  for (let path of paths) {
@@ -82,4 +75,3 @@ const mergePath = (...paths) => {
82
75
  }
83
76
  return p;
84
77
  };
85
- exports.mergePath = mergePath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "1.3.1",
3
+ "version": "1.3.4",
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
- "build": "rimraf dist && tsc --project tsconfig.build.json",
14
+ "build": "rimraf dist && tsc --project tsconfig.build.json && tsc --project tsconfig.build.esm.json",
15
15
  "watch": "tsc --project tsconfig.build.json -w",
16
16
  "prepublishOnly": "yarn build"
17
17
  },
@@ -26,9 +26,11 @@
26
26
  "./jwt": "./dist/middleware/jwt/index.js",
27
27
  "./logger": "./dist/middleware/logger/index.js",
28
28
  "./mustache": "./dist/middleware/mustache/index.js",
29
+ "./mustache.module": "./dist/middleware/mustache/module.mjs",
29
30
  "./powered-by": "./dist/middleware/powered-by/index.js",
30
31
  "./pretty-json": "./dist/middleware/pretty-json/index.js",
31
32
  "./serve-static": "./dist/middleware/serve-static/index.js",
33
+ "./serve-static.module": "./dist/middleware/serve-static/module.mjs",
32
34
  "./router/trie-router": "./dist/router/trie-router/index.js",
33
35
  "./router/reg-exp-router": "./dist/router/reg-exp-router/index.js",
34
36
  "./utils/jwt": "./dist/utils/jwt/index.js",
@@ -63,6 +65,9 @@
63
65
  "mustache": [
64
66
  "./dist/middleware/mustache"
65
67
  ],
68
+ "mustache.module": [
69
+ "./dist/middleware/mustache/module.d.mts"
70
+ ],
66
71
  "powered-by": [
67
72
  "./dist/middleware/powered-by"
68
73
  ],
@@ -70,7 +75,10 @@
70
75
  "./dist/middleware/pretty-json"
71
76
  ],
72
77
  "serve-static": [
73
- "./dist/middleware/serve-static"
78
+ "./dist/middleware/serve-static/index.d.ts"
79
+ ],
80
+ "serve-static.module": [
81
+ "./dist/middleware/serve-static/module.d.mts"
74
82
  ],
75
83
  "router/trie-router": [
76
84
  "./dist/router/trie-router/router.d.ts"