hono 1.4.5 → 1.4.6

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
@@ -73,6 +73,7 @@ Built-in middleware make _"**Write Less, do more**"_ in reality. You can use a l
73
73
  - [CORS](https://github.com/honojs/hono/tree/master/src/middleware/cors/)
74
74
  - [ETag](https://github.com/honojs/hono/tree/master/src/middleware/etag/)
75
75
  - [GraphQL Server](https://github.com/honojs/hono/tree/master/src/middleware/graphql-server/)
76
+ - [JSX](https://github.com/honojs/hono/tree/master/src/middleware/jsx/)
76
77
  - [JWT Authentication](https://github.com/honojs/hono/tree/master/src/middleware/jwt/)
77
78
  - [Logger](https://github.com/honojs/hono/tree/master/src/middleware/logger/)
78
79
  - [Mustache template engine](https://github.com/honojs/hono/tree/master/src/middleware/mustache/) (Only for Cloudflare Workers)
@@ -495,6 +496,8 @@ app.get('/foo', async (c) => {
495
496
 
496
497
  ### c.env
497
498
 
499
+ Environment variables, secrets, and KV namespaces are known as bindings. Regardless of type, bindings are always available as global variables and can be accessed via the context `c.env.BINDING_KEY`.
500
+
498
501
  ```ts
499
502
  // Environment object for Cloudflare Workers
500
503
  app.get('*', async c => {
package/dist/compose.js CHANGED
@@ -17,7 +17,7 @@ const compose = (middleware, onError, onNotFound) => {
17
17
  handler = next;
18
18
  if (!handler) {
19
19
  if (context instanceof context_1.Context && context.finalized === false && onNotFound) {
20
- context.res = onNotFound(context);
20
+ context.res = await onNotFound(context);
21
21
  }
22
22
  return Promise.resolve(context);
23
23
  }
package/dist/context.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
+ import type { NotFoundHandler } from './hono';
2
3
  import type { StatusCode } from './utils/http-status';
3
4
  declare type Headers = Record<string, string>;
4
5
  export declare type Data = string | ArrayBuffer | ReadableStream;
@@ -15,8 +16,8 @@ export declare class Context<RequestParamKeyType extends string = string, E = En
15
16
  private _headers;
16
17
  private _res;
17
18
  private notFoundHandler;
18
- render: (template: string, params?: object, options?: object) => Promise<Response>;
19
- constructor(req: Request<RequestParamKeyType>, env?: E | undefined, event?: FetchEvent | undefined, notFoundHandler?: (c: Context<string, E>) => Response);
19
+ render: (content: string, params?: object, options?: object) => Response | Promise<Response>;
20
+ constructor(req: Request<RequestParamKeyType>, env?: E | undefined, event?: FetchEvent | undefined, notFoundHandler?: NotFoundHandler);
20
21
  get res(): Response;
21
22
  set res(_res: Response);
22
23
  header(name: string, value: string): void;
package/dist/hono.d.ts CHANGED
@@ -3,7 +3,7 @@ import { Context } from './context';
3
3
  import type { Env } from './context';
4
4
  import type { Router } from './router';
5
5
  export declare type Handler<RequestParamKeyType extends string = string, E = Env> = (c: Context<RequestParamKeyType, E>, next: Next) => Response | Promise<Response> | Promise<void> | Promise<Response | undefined>;
6
- export declare type NotFoundHandler<E = Env> = (c: Context<string, E>) => Response;
6
+ export declare type NotFoundHandler<E = Env> = (c: Context<string, E>) => Response | Promise<Response>;
7
7
  export declare type ErrorHandler<E = Env> = (err: Error, c: Context<string, E>) => Response;
8
8
  export declare type Next = () => Promise<void>;
9
9
  declare type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}` ? Name : NameWithPattern;
@@ -0,0 +1,17 @@
1
+ import type { Context } from '../../context';
2
+ import type { Next } from '../../hono';
3
+ declare global {
4
+ namespace h.JSX {
5
+ interface IntrinsicElements {
6
+ [tagName: string]: Record<string, any>;
7
+ }
8
+ }
9
+ }
10
+ export declare const jsx: () => (c: Context, next: Next) => Promise<void>;
11
+ declare type EscapedString = string & {
12
+ isEscaped: true;
13
+ };
14
+ export declare const h: (tag: string | Function, props: Record<string, any>, ...children: (string | EscapedString)[]) => EscapedString;
15
+ declare type FC<T = Record<string, any>> = (props: T) => EscapedString;
16
+ export declare const memo: <T>(component: FC<T>, propsAreEqual?: (prevProps: Readonly<T>, nextProps: Readonly<T>) => boolean) => FC<T>;
17
+ export {};
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.memo = exports.h = exports.jsx = void 0;
4
+ const html_1 = require("../../utils/html");
5
+ const jsx = () => {
6
+ return async (c, next) => {
7
+ c.render = (content) => {
8
+ const output = `<!doctype html>${content.toString()}`;
9
+ return c.html(output);
10
+ };
11
+ await next();
12
+ };
13
+ };
14
+ exports.jsx = jsx;
15
+ const h = (tag, props, ...children) => {
16
+ if (typeof tag === 'function') {
17
+ return tag.call(null, { ...props, children: children.length <= 1 ? children[0] : children });
18
+ }
19
+ let result = `<${tag}`;
20
+ const propsKeys = Object.keys(props || {});
21
+ for (let i = 0, len = propsKeys.length; i < len; i++) {
22
+ const v = props[propsKeys[i]];
23
+ if (propsKeys[i] === 'dangerouslySetInnerHTML') {
24
+ if (children.length > 0) {
25
+ throw 'Can only set one of `children` or `props.dangerouslySetInnerHTML`.';
26
+ }
27
+ const escapedString = new String(v.__html);
28
+ escapedString.isEscaped = true;
29
+ children = [escapedString];
30
+ continue;
31
+ }
32
+ else if (v === null || v === undefined) {
33
+ continue;
34
+ }
35
+ result += ` ${propsKeys[i]}="${(0, html_1.escape)(v.toString())}"`;
36
+ }
37
+ result += '>';
38
+ const flattenChildren = children.flat(Infinity);
39
+ for (let i = 0, len = flattenChildren.length; i < len; i++) {
40
+ const child = flattenChildren[i];
41
+ if (typeof child === 'boolean' || child === null || child === undefined) {
42
+ continue;
43
+ }
44
+ else if (typeof child === 'object' && child.isEscaped) {
45
+ result += child;
46
+ }
47
+ else {
48
+ result += (0, html_1.escape)(child.toString());
49
+ }
50
+ }
51
+ result += `</${tag}>`;
52
+ const escapedString = new String(result);
53
+ escapedString.isEscaped = true;
54
+ return escapedString;
55
+ };
56
+ exports.h = h;
57
+ const shallowEqual = (a, b) => {
58
+ if (a === b) {
59
+ return true;
60
+ }
61
+ const aKeys = Object.keys(a);
62
+ const bKeys = Object.keys(b);
63
+ if (aKeys.length !== bKeys.length) {
64
+ return false;
65
+ }
66
+ for (let i = 0, len = aKeys.length; i < len; i++) {
67
+ if (a[aKeys[i]] !== b[aKeys[i]]) {
68
+ return false;
69
+ }
70
+ }
71
+ return true;
72
+ };
73
+ const memo = (component, propsAreEqual = shallowEqual) => {
74
+ let computed = undefined;
75
+ let prevProps = undefined;
76
+ return ((props) => {
77
+ if (prevProps && !propsAreEqual(prevProps, props)) {
78
+ computed = undefined;
79
+ }
80
+ prevProps = props;
81
+ return (computed || (computed = component(props)));
82
+ });
83
+ };
84
+ exports.memo = memo;
@@ -0,0 +1,3 @@
1
+ export interface Context {
2
+ render(template: string, params?: object, options?: object): Promise<Response>;
3
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,3 @@
1
1
  import type { MustacheOptions } from './mustache';
2
- declare const module: (options?: MustacheOptions) => import("../../hono").Handler<string, import("../../context").Env>;
2
+ declare const module: (options?: MustacheOptions) => (c: import("../../context").Context<string, import("../../context").Env>, next: import("../../hono").Next) => Promise<void>;
3
3
  export { module as mustache };
@@ -1,8 +1,9 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
- import type { Handler } from '../../hono';
2
+ import type { Context } from '../../context';
3
+ import type { Next } from '../../hono';
3
4
  export declare type MustacheOptions = {
4
5
  root: string;
5
6
  manifest?: object | string;
6
7
  namespace?: KVNamespace;
7
8
  };
8
- export declare const mustache: (init?: MustacheOptions) => Handler;
9
+ export declare const mustache: (init?: MustacheOptions) => (c: Context, next: Next) => Promise<void>;
@@ -5,5 +5,6 @@ declare type Algorithm = {
5
5
  declare type Data = string | object | boolean;
6
6
  export declare const sha256: (data: Data) => Promise<string | null>;
7
7
  export declare const sha1: (data: Data) => Promise<string | null>;
8
+ export declare const md5: (data: Data) => Promise<string | null>;
8
9
  export declare const createHash: (data: Data, algorithm: Algorithm) => Promise<string | null>;
9
10
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createHash = exports.sha1 = exports.sha256 = void 0;
3
+ exports.createHash = exports.md5 = exports.sha1 = exports.sha256 = void 0;
4
4
  const sha256 = async (data) => {
5
5
  const algorithm = { name: 'SHA-256', alias: 'sha256' };
6
6
  const hash = await (0, exports.createHash)(data, algorithm);
@@ -13,6 +13,12 @@ const sha1 = async (data) => {
13
13
  return hash;
14
14
  };
15
15
  exports.sha1 = sha1;
16
+ const md5 = async (data) => {
17
+ const algorithm = { name: 'MD5', alias: 'md5' };
18
+ const hash = await (0, exports.createHash)(data, algorithm);
19
+ return hash;
20
+ };
21
+ exports.md5 = md5;
16
22
  const createHash = async (data, algorithm) => {
17
23
  if (crypto && crypto.subtle) {
18
24
  const buffer = await crypto.subtle.digest({
@@ -0,0 +1 @@
1
+ export declare const escape: (str: string) => string;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.escape = void 0;
4
+ const entityMap = {
5
+ '&': '&amp;',
6
+ '<': '&lt;',
7
+ '>': '&gt;',
8
+ '"': '&quot;',
9
+ };
10
+ const escapeRe = new RegExp(`[${Object.keys(entityMap).join('')}]`, 'g');
11
+ const replaceFn = (m) => entityMap[m];
12
+ const escape = (str) => {
13
+ return str.replace(escapeRe, replaceFn);
14
+ };
15
+ exports.escape = escape;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "1.4.5",
3
+ "version": "1.4.6",
4
4
  "description": "Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -24,6 +24,7 @@
24
24
  "./cors": "./dist/middleware/cors/index.js",
25
25
  "./etag": "./dist/middleware/etag/index.js",
26
26
  "./graphql-server": "./dist/middleware/graphql-server/index.js",
27
+ "./jsx": "./dist/middleware/jsx/index.js",
27
28
  "./jwt": "./dist/middleware/jwt/index.js",
28
29
  "./logger": "./dist/middleware/logger/index.js",
29
30
  "./mustache": "./dist/middleware/mustache/index.js",
@@ -60,6 +61,9 @@
60
61
  "graphql-server": [
61
62
  "./dist/middleware/graphql-server"
62
63
  ],
64
+ "jsx": [
65
+ "./dist/middleware/jsx"
66
+ ],
63
67
  "jwt": [
64
68
  "./dist/middleware/jwt"
65
69
  ],