hono 4.2.9 → 4.3.1

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.
Files changed (72) hide show
  1. package/README.md +0 -13
  2. package/dist/adapter/deno/serve-static.js +1 -1
  3. package/dist/cjs/adapter/deno/serve-static.js +1 -1
  4. package/dist/cjs/client/client.js +2 -1
  5. package/dist/cjs/helper/cookie/index.js +2 -0
  6. package/dist/cjs/helper/factory/index.js +13 -1
  7. package/dist/cjs/jsx/base.js +26 -14
  8. package/dist/cjs/jsx/children.js +45 -0
  9. package/dist/cjs/jsx/constants.js +3 -0
  10. package/dist/cjs/jsx/dom/context.js +22 -11
  11. package/dist/cjs/jsx/dom/css.js +6 -4
  12. package/dist/cjs/jsx/dom/index.js +35 -4
  13. package/dist/cjs/jsx/dom/jsx-dev-runtime.js +20 -13
  14. package/dist/cjs/jsx/dom/render.js +110 -48
  15. package/dist/cjs/jsx/dom/utils.js +33 -0
  16. package/dist/cjs/jsx/hooks/index.js +41 -1
  17. package/dist/cjs/jsx/index.js +17 -1
  18. package/dist/cjs/jsx/jsx-dev-runtime.js +0 -1
  19. package/dist/cjs/jsx/utils.js +12 -2
  20. package/dist/cjs/middleware/bearer-auth/index.js +2 -1
  21. package/dist/cjs/middleware/secure-headers/index.js +58 -8
  22. package/dist/cjs/middleware/serve-static/index.js +5 -2
  23. package/dist/cjs/middleware/timing/index.js +3 -2
  24. package/dist/cjs/utils/mime.js +4 -2
  25. package/dist/client/client.js +2 -1
  26. package/dist/helper/cookie/index.js +2 -0
  27. package/dist/helper/factory/index.js +13 -1
  28. package/dist/jsx/base.js +27 -15
  29. package/dist/jsx/children.js +21 -0
  30. package/dist/jsx/constants.js +2 -0
  31. package/dist/jsx/dom/context.js +22 -11
  32. package/dist/jsx/dom/css.js +6 -4
  33. package/dist/jsx/dom/index.js +31 -5
  34. package/dist/jsx/dom/jsx-dev-runtime.js +20 -13
  35. package/dist/jsx/dom/render.js +109 -49
  36. package/dist/jsx/dom/utils.js +10 -0
  37. package/dist/jsx/hooks/index.js +37 -1
  38. package/dist/jsx/index.js +17 -2
  39. package/dist/jsx/jsx-dev-runtime.js +0 -1
  40. package/dist/jsx/utils.js +10 -1
  41. package/dist/middleware/bearer-auth/index.js +2 -1
  42. package/dist/middleware/secure-headers/index.js +57 -8
  43. package/dist/middleware/serve-static/index.js +5 -2
  44. package/dist/middleware/timing/index.js +3 -2
  45. package/dist/types/adapter/cloudflare-workers/serve-static.d.ts +6 -0
  46. package/dist/types/client/types.d.ts +44 -20
  47. package/dist/types/context.d.ts +6 -6
  48. package/dist/types/helper/cookie/index.d.ts +1 -1
  49. package/dist/types/helper/factory/index.d.ts +15 -1
  50. package/dist/types/helper/websocket/index.d.ts +1 -4
  51. package/dist/types/jsx/base.d.ts +10 -3
  52. package/dist/types/jsx/children.d.ts +9 -0
  53. package/dist/types/jsx/constants.d.ts +1 -0
  54. package/dist/types/jsx/dom/context.d.ts +1 -10
  55. package/dist/types/jsx/dom/index.d.ts +31 -11
  56. package/dist/types/jsx/dom/jsx-dev-runtime.d.ts +3 -13
  57. package/dist/types/jsx/dom/render.d.ts +6 -4
  58. package/dist/types/jsx/dom/utils.d.ts +1 -0
  59. package/dist/types/jsx/hooks/index.d.ts +6 -0
  60. package/dist/types/jsx/index.d.ts +18 -3
  61. package/dist/types/jsx/types.d.ts +24 -1
  62. package/dist/types/jsx/utils.d.ts +1 -0
  63. package/dist/types/middleware/bearer-auth/index.d.ts +2 -0
  64. package/dist/types/middleware/secure-headers/index.d.ts +30 -21
  65. package/dist/types/middleware/serve-static/index.d.ts +2 -2
  66. package/dist/types/middleware/timing/index.d.ts +1 -1
  67. package/dist/types/request.d.ts +3 -2
  68. package/dist/types/types.d.ts +88 -63
  69. package/dist/types/utils/mime.d.ts +2 -0
  70. package/dist/types/utils/types.d.ts +3 -0
  71. package/dist/utils/mime.js +2 -1
  72. package/package.json +9 -1
@@ -45,7 +45,7 @@ const serveStatic = (options) => {
45
45
  const getContent = options.getContent;
46
46
  const pathResolve = options.pathResolve ?? defaultPathResolve;
47
47
  path = pathResolve(path);
48
- let content = await getContent(path);
48
+ let content = await getContent(path, c);
49
49
  if (!content) {
50
50
  let pathWithOutDefaultDocument = (0, import_filepath.getFilePathWithoutDefaultDocument)({
51
51
  filename,
@@ -55,11 +55,14 @@ const serveStatic = (options) => {
55
55
  return await next();
56
56
  }
57
57
  pathWithOutDefaultDocument = pathResolve(pathWithOutDefaultDocument);
58
- content = await getContent(pathWithOutDefaultDocument);
58
+ content = await getContent(pathWithOutDefaultDocument, c);
59
59
  if (content) {
60
60
  path = pathWithOutDefaultDocument;
61
61
  }
62
62
  }
63
+ if (content instanceof Response) {
64
+ return c.newResponse(content.body, content);
65
+ }
63
66
  if (content) {
64
67
  let mimeType;
65
68
  if (options.mimes) {
@@ -60,10 +60,11 @@ const timing = (config) => {
60
60
  const enabled = typeof options.enabled === "function" ? options.enabled(c) : options.enabled;
61
61
  if (enabled) {
62
62
  c.res.headers.append("Server-Timing", headers.join(","));
63
- if (options.crossOrigin) {
63
+ const crossOrigin = typeof options.crossOrigin === "function" ? options.crossOrigin(c) : options.crossOrigin;
64
+ if (crossOrigin) {
64
65
  c.res.headers.append(
65
66
  "Timing-Allow-Origin",
66
- typeof options.crossOrigin === "string" ? options.crossOrigin : "*"
67
+ typeof crossOrigin === "string" ? crossOrigin : "*"
67
68
  );
68
69
  }
69
70
  }
@@ -19,7 +19,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var mime_exports = {};
20
20
  __export(mime_exports, {
21
21
  getExtension: () => getExtension,
22
- getMimeType: () => getMimeType
22
+ getMimeType: () => getMimeType,
23
+ mimes: () => baseMimes
23
24
  });
24
25
  module.exports = __toCommonJS(mime_exports);
25
26
  const getMimeType = (filename, mimes = baseMimes) => {
@@ -101,5 +102,6 @@ const baseMimes = {
101
102
  // Annotate the CommonJS export names for ESM import in node:
102
103
  0 && (module.exports = {
103
104
  getExtension,
104
- getMimeType
105
+ getMimeType,
106
+ mimes
105
107
  });
@@ -95,7 +95,8 @@ var ClientRequestImpl = class {
95
95
  return (opt?.fetch || fetch)(url, {
96
96
  body: setBody ? this.rBody : void 0,
97
97
  method: methodUpperCase,
98
- headers
98
+ headers,
99
+ ...opt?.init
99
100
  });
100
101
  };
101
102
  };
@@ -79,7 +79,9 @@ var setSignedCookie = async (c, name, value, secret, opt) => {
79
79
  c.header("set-cookie", cookie, { append: true });
80
80
  };
81
81
  var deleteCookie = (c, name, opt) => {
82
+ const deletedCookie = getCookie(c, name);
82
83
  setCookie(c, name, "", { ...opt, maxAge: 0 });
84
+ return deletedCookie;
83
85
  };
84
86
  export {
85
87
  deleteCookie,
@@ -1,11 +1,23 @@
1
1
  // src/helper/factory/index.ts
2
+ import { Hono } from "../../hono.js";
2
3
  var Factory = class {
4
+ initApp;
5
+ constructor(init) {
6
+ this.initApp = init?.initApp;
7
+ }
8
+ createApp = () => {
9
+ const app = new Hono();
10
+ if (this.initApp) {
11
+ this.initApp(app);
12
+ }
13
+ return app;
14
+ };
3
15
  createMiddleware = (middleware) => middleware;
4
16
  createHandlers(...handlers) {
5
17
  return handlers.filter((handler) => handler !== void 0);
6
18
  }
7
19
  };
8
- var createFactory = () => new Factory();
20
+ var createFactory = (init) => new Factory(init);
9
21
  var createMiddleware = (middleware) => createFactory().createMiddleware(middleware);
10
22
  export {
11
23
  Factory,
package/dist/jsx/base.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import { raw } from "../helper/html/index.js";
3
3
  import { escapeToBuffer, stringBufferToString } from "../utils/html.js";
4
4
  import { globalContexts } from "./context.js";
5
- import { normalizeIntrinsicElementProps } from "./utils.js";
5
+ import { normalizeIntrinsicElementProps, styleObjectForEach } from "./utils.js";
6
6
  var emptyTags = [
7
7
  "area",
8
8
  "base",
@@ -78,6 +78,12 @@ var JSXNode = class {
78
78
  this.props = props;
79
79
  this.children = children;
80
80
  }
81
+ get type() {
82
+ return this.tag;
83
+ }
84
+ get ref() {
85
+ return this.props.ref || null;
86
+ }
81
87
  toString() {
82
88
  const buffer = [""];
83
89
  this.localContexts?.forEach(([context, value]) => {
@@ -101,13 +107,16 @@ var JSXNode = class {
101
107
  for (let i = 0, len = propsKeys.length; i < len; i++) {
102
108
  const key = propsKeys[i];
103
109
  const v = props[key];
104
- if (key === "style" && typeof v === "object") {
105
- const styles = Object.keys(v).map((k) => {
106
- const property = k.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
107
- return `${property}:${v[k]}`;
108
- }).join(";");
110
+ if (key === "children") {
111
+ } else if (key === "style" && typeof v === "object") {
112
+ let styleStr = "";
113
+ styleObjectForEach(v, (property, value) => {
114
+ if (value != null) {
115
+ styleStr += `${styleStr ? ";" : ""}${property}:${value}`;
116
+ }
117
+ });
109
118
  buffer[0] += ' style="';
110
- escapeToBuffer(styles, buffer);
119
+ escapeToBuffer(styleStr, buffer);
111
120
  buffer[0] += '"';
112
121
  } else if (typeof v === "string") {
113
122
  buffer[0] += ` ${key}="`;
@@ -184,11 +193,12 @@ var JSXFragmentNode = class extends JSXNode {
184
193
  }
185
194
  };
186
195
  var jsx = (tag, props, ...children) => {
187
- let key;
188
- if (props) {
189
- key = props?.key;
190
- delete props["key"];
196
+ props ??= {};
197
+ if (children.length) {
198
+ props.children = children.length === 1 ? children[0] : children;
191
199
  }
200
+ const key = props.key;
201
+ delete props["key"];
192
202
  const node = jsxFn(tag, props, children);
193
203
  node.key = key;
194
204
  return node;
@@ -235,18 +245,20 @@ var Fragment = ({
235
245
  }) => {
236
246
  return new JSXFragmentNode(
237
247
  "",
238
- {},
248
+ {
249
+ children
250
+ },
239
251
  Array.isArray(children) ? children : children ? [children] : []
240
252
  );
241
253
  };
242
254
  var isValidElement = (element) => {
243
- return !!(element && typeof element === "object" && "tag" in element && "props" in element && "children" in element);
255
+ return !!(element && typeof element === "object" && "tag" in element && "props" in element);
244
256
  };
245
257
  var cloneElement = (element, props, ...children) => {
246
- return jsxFn(
258
+ return jsx(
247
259
  element.tag,
248
260
  { ...element.props, ...props },
249
- children.length ? children : element.children || []
261
+ ...children
250
262
  );
251
263
  };
252
264
  export {
@@ -0,0 +1,21 @@
1
+ // src/jsx/children.ts
2
+ var toArray = (children) => Array.isArray(children) ? children : [children];
3
+ var Children = {
4
+ map: (children, fn) => toArray(children).map(fn),
5
+ forEach: (children, fn) => {
6
+ toArray(children).forEach(fn);
7
+ },
8
+ count: (children) => toArray(children).length,
9
+ only: (_children) => {
10
+ const children = toArray(_children);
11
+ if (children.length !== 1) {
12
+ throw new Error("Children.only() expects only one child");
13
+ }
14
+ return children[0];
15
+ },
16
+ toArray
17
+ };
18
+ export {
19
+ Children,
20
+ toArray
21
+ };
@@ -2,8 +2,10 @@
2
2
  var DOM_RENDERER = Symbol("RENDERER");
3
3
  var DOM_ERROR_HANDLER = Symbol("ERROR_HANDLER");
4
4
  var DOM_STASH = Symbol("STASH");
5
+ var DOM_INTERNAL_TAG = Symbol("INTERNAL");
5
6
  export {
6
7
  DOM_ERROR_HANDLER,
8
+ DOM_INTERNAL_TAG,
7
9
  DOM_RENDERER,
8
10
  DOM_STASH
9
11
  };
@@ -2,28 +2,39 @@
2
2
  import { DOM_ERROR_HANDLER } from "../constants.js";
3
3
  import { globalContexts } from "../context.js";
4
4
  import { Fragment } from "./jsx-runtime.js";
5
- var createContextProviderFunction = (values) => ({ value, children }) => {
6
- const res = Fragment({
5
+ import { setInternalTagFlag } from "./utils.js";
6
+ var createContextProviderFunction = (values) => setInternalTagFlag(({ value, children }) => {
7
+ if (!children) {
8
+ return void 0;
9
+ }
10
+ const props = {
7
11
  children: [
8
12
  {
9
- tag: () => {
13
+ tag: setInternalTagFlag(() => {
10
14
  values.push(value);
11
- }
12
- },
13
- ...children,
14
- {
15
- tag: () => {
16
- values.pop();
17
- }
15
+ }),
16
+ props: {}
18
17
  }
19
18
  ]
19
+ };
20
+ if (Array.isArray(children)) {
21
+ props.children.push(...children.flat());
22
+ } else {
23
+ props.children.push(children);
24
+ }
25
+ props.children.push({
26
+ tag: setInternalTagFlag(() => {
27
+ values.pop();
28
+ }),
29
+ props: {}
20
30
  });
31
+ const res = Fragment(props);
21
32
  res[DOM_ERROR_HANDLER] = (err) => {
22
33
  values.pop();
23
34
  throw err;
24
35
  };
25
36
  return res;
26
- };
37
+ });
27
38
  var createContext = (defaultValue) => {
28
39
  const values = [defaultValue];
29
40
  const context = {
@@ -89,10 +89,12 @@ var createCssJsxDomObjects = ({ id }) => {
89
89
  };
90
90
  const Style2 = ({ children }) => ({
91
91
  tag: "style",
92
- children: (Array.isArray(children) ? children : [children]).map(
93
- (c) => c[STYLE_STRING]
94
- ),
95
- props: { id }
92
+ props: {
93
+ id,
94
+ children: children && (Array.isArray(children) ? children : [children]).map(
95
+ (c) => c[STYLE_STRING]
96
+ )
97
+ }
96
98
  });
97
99
  return [cssObject, Style2];
98
100
  };
@@ -1,5 +1,6 @@
1
1
  // src/jsx/dom/index.ts
2
2
  import { memo, isValidElement } from "../base.js";
3
+ import { Children } from "../children.js";
3
4
  import { useContext } from "../context.js";
4
5
  import {
5
6
  useState,
@@ -16,14 +17,23 @@ import {
16
17
  useLayoutEffect,
17
18
  useReducer,
18
19
  useId,
19
- useDebugValue
20
+ useDebugValue,
21
+ createRef,
22
+ forwardRef,
23
+ useImperativeHandle,
24
+ useSyncExternalStore
20
25
  } from "../hooks/index.js";
21
26
  import { Suspense, ErrorBoundary } from "./components.js";
22
27
  import { createContext } from "./context.js";
23
- import { jsx } from "./jsx-runtime.js";
28
+ import { jsx, Fragment } from "./jsx-runtime.js";
29
+ import { flushSync, createPortal } from "./render.js";
24
30
  import { render } from "./render.js";
31
+ export * from "../types.js";
25
32
  var createElement = (tag, props, ...children) => {
26
- const jsxProps = { ...props, children };
33
+ const jsxProps = props ? { ...props } : {};
34
+ if (children.length) {
35
+ jsxProps.children = children.length === 1 ? children[0] : children;
36
+ }
27
37
  let key = void 0;
28
38
  if ("key" in jsxProps) {
29
39
  key = jsxProps.key;
@@ -37,7 +47,7 @@ var cloneElement = (element, props, ...children) => {
37
47
  {
38
48
  ...element.props,
39
49
  ...props,
40
- children: children.length ? children : element.children
50
+ children: children.length ? children : element.props.children
41
51
  },
42
52
  element.key
43
53
  );
@@ -58,6 +68,10 @@ var dom_default = {
58
68
  useReducer,
59
69
  useId,
60
70
  useDebugValue,
71
+ createRef,
72
+ forwardRef,
73
+ useImperativeHandle,
74
+ useSyncExternalStore,
61
75
  Suspense,
62
76
  ErrorBoundary,
63
77
  createContext,
@@ -65,15 +79,25 @@ var dom_default = {
65
79
  memo,
66
80
  isValidElement,
67
81
  createElement,
68
- cloneElement
82
+ cloneElement,
83
+ Children,
84
+ Fragment,
85
+ flushSync,
86
+ createPortal
69
87
  };
70
88
  export {
89
+ Children,
71
90
  ErrorBoundary,
91
+ Fragment,
72
92
  Suspense,
73
93
  cloneElement,
74
94
  createContext,
75
95
  createElement,
96
+ createPortal,
97
+ createRef,
76
98
  dom_default as default,
99
+ flushSync,
100
+ forwardRef,
77
101
  isValidElement,
78
102
  createElement as jsx,
79
103
  memo,
@@ -87,11 +111,13 @@ export {
87
111
  useDeferredValue,
88
112
  useEffect,
89
113
  useId,
114
+ useImperativeHandle,
90
115
  useLayoutEffect,
91
116
  useMemo,
92
117
  useReducer,
93
118
  useRef,
94
119
  useState,
120
+ useSyncExternalStore,
95
121
  useTransition,
96
122
  useViewTransition
97
123
  };
@@ -1,22 +1,29 @@
1
1
  // src/jsx/dom/jsx-dev-runtime.ts
2
2
  import { normalizeIntrinsicElementProps } from "../utils.js";
3
+ var JSXNodeCompatPrototype = {
4
+ type: {
5
+ get() {
6
+ return this.tag;
7
+ }
8
+ },
9
+ ref: {
10
+ get() {
11
+ return this.props?.ref;
12
+ }
13
+ }
14
+ };
3
15
  var jsxDEV = (tag, props, key) => {
4
16
  if (typeof tag === "string") {
5
17
  normalizeIntrinsicElementProps(props);
6
18
  }
7
- let children;
8
- if (props && "children" in props) {
9
- children = props.children;
10
- delete props["children"];
11
- } else {
12
- children = [];
13
- }
14
- return {
15
- tag,
16
- props,
17
- key,
18
- children: Array.isArray(children) ? children : [children]
19
- };
19
+ return Object.defineProperties(
20
+ {
21
+ tag,
22
+ props,
23
+ key
24
+ },
25
+ JSXNodeCompatPrototype
26
+ );
20
27
  };
21
28
  var Fragment = (props) => jsxDEV("", props, void 0);
22
29
  export {