hono 4.5.10 → 4.6.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.
Files changed (49) hide show
  1. package/README.md +1 -1
  2. package/dist/adapter/cloudflare-pages/handler.js +1 -1
  3. package/dist/cjs/adapter/cloudflare-pages/handler.js +1 -1
  4. package/dist/cjs/context.js +25 -16
  5. package/dist/cjs/helper/streaming/sse.js +4 -2
  6. package/dist/cjs/jsx/base.js +1 -1
  7. package/dist/cjs/jsx/components.js +10 -5
  8. package/dist/cjs/jsx/intrinsic-element/components.js +15 -3
  9. package/dist/cjs/middleware/basic-auth/index.js +15 -24
  10. package/dist/cjs/middleware/bearer-auth/index.js +32 -21
  11. package/dist/cjs/middleware/context-storage/index.js +43 -0
  12. package/dist/cjs/middleware/jsx-renderer/index.js +1 -0
  13. package/dist/cjs/middleware/secure-headers/permissions-policy.js +16 -0
  14. package/dist/cjs/middleware/secure-headers/secure-headers.js +30 -1
  15. package/dist/cjs/middleware/serve-static/index.js +32 -15
  16. package/dist/cjs/utils/basic-auth.js +46 -0
  17. package/dist/cjs/utils/body.js +1 -1
  18. package/dist/cjs/utils/html.js +8 -0
  19. package/dist/context.js +25 -16
  20. package/dist/helper/streaming/sse.js +4 -2
  21. package/dist/jsx/base.js +1 -1
  22. package/dist/jsx/components.js +10 -5
  23. package/dist/jsx/intrinsic-element/components.js +15 -3
  24. package/dist/middleware/basic-auth/index.js +15 -24
  25. package/dist/middleware/bearer-auth/index.js +32 -21
  26. package/dist/middleware/context-storage/index.js +19 -0
  27. package/dist/middleware/jsx-renderer/index.js +1 -0
  28. package/dist/middleware/secure-headers/permissions-policy.js +0 -0
  29. package/dist/middleware/secure-headers/secure-headers.js +30 -1
  30. package/dist/middleware/serve-static/index.js +32 -15
  31. package/dist/types/adapter/bun/websocket.d.ts +4 -6
  32. package/dist/types/adapter/cloudflare-pages/handler.d.ts +6 -2
  33. package/dist/types/adapter/cloudflare-workers/websocket.d.ts +1 -1
  34. package/dist/types/adapter/deno/websocket.d.ts +1 -1
  35. package/dist/types/helper/streaming/sse.d.ts +1 -1
  36. package/dist/types/helper/websocket/index.d.ts +8 -8
  37. package/dist/types/middleware/basic-auth/index.d.ts +4 -0
  38. package/dist/types/middleware/bearer-auth/index.d.ts +10 -0
  39. package/dist/types/middleware/context-storage/index.d.ts +39 -0
  40. package/dist/types/middleware/jsx-renderer/index.d.ts +2 -2
  41. package/dist/types/middleware/secure-headers/permissions-policy.d.ts +14 -0
  42. package/dist/types/middleware/secure-headers/secure-headers.d.ts +5 -0
  43. package/dist/types/middleware/serve-static/index.d.ts +2 -0
  44. package/dist/types/utils/basic-auth.d.ts +5 -0
  45. package/dist/types/utils/html.d.ts +1 -1
  46. package/dist/utils/basic-auth.js +23 -0
  47. package/dist/utils/body.js +1 -1
  48. package/dist/utils/html.js +8 -0
  49. package/package.json +9 -1
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @module
3
+ * Context Storage Middleware for Hono.
4
+ */
5
+ import type { Context } from '../../context';
6
+ import type { Env, MiddlewareHandler } from '../../types';
7
+ /**
8
+ * Context Storage Middleware for Hono.
9
+ *
10
+ * @see {@link https://hono.dev/docs/middleware/builtin/context-storage}
11
+ *
12
+ * @returns {MiddlewareHandler} The middleware handler function.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * type Env = {
17
+ * Variables: {
18
+ * message: string
19
+ * }
20
+ * }
21
+ *
22
+ * const app = new Hono<Env>()
23
+ *
24
+ * app.use(contextStorage())
25
+ *
26
+ * app.use(async (c, next) => {
27
+ * c.set('message', 'Hono is cool!!)
28
+ * await next()
29
+ * })
30
+ *
31
+ * app.get('/', async (c) => { c.text(getMessage()) })
32
+ *
33
+ * const getMessage = () => {
34
+ * return getContext<Env>().var.message
35
+ * }
36
+ * ```
37
+ */
38
+ export declare const contextStorage: () => MiddlewareHandler;
39
+ export declare const getContext: <E extends Env = Env>() => Context<E, any, {}>;
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module
3
- * JSR Renderer Middleware for Hono.
3
+ * JSX Renderer Middleware for Hono.
4
4
  */
5
5
  import type { Context, PropsForRenderer } from '../../context';
6
6
  import type { FC, Context as JSXContext, PropsWithChildren } from '../../jsx';
@@ -17,7 +17,7 @@ type ComponentWithChildren = (props: PropsWithChildren<PropsForRenderer & {
17
17
  /**
18
18
  * JSX Renderer Middleware for hono.
19
19
  *
20
- * @see {@link{https://hono.dev/docs/middleware/builtin/jsx-renderer}}
20
+ * @see {@link https://hono.dev/docs/middleware/builtin/jsx-renderer}
21
21
  *
22
22
  * @param {ComponentWithChildren} [component] - The component to render, which can accept children and props.
23
23
  * @param {RendererOptions} [options] - The options for the JSX renderer middleware.
@@ -0,0 +1,14 @@
1
+ export type PermissionsPolicyDirective = StandardizedFeatures | ProposedFeatures | ExperimentalFeatures;
2
+ /**
3
+ * These features have been declared in a published version of the respective specification.
4
+ */
5
+ type StandardizedFeatures = 'accelerometer' | 'ambientLightSensor' | 'attributionReporting' | 'autoplay' | 'battery' | 'bluetooth' | 'camera' | 'chUa' | 'chUaArch' | 'chUaBitness' | 'chUaFullVersion' | 'chUaFullVersionList' | 'chUaMobile' | 'chUaModel' | 'chUaPlatform' | 'chUaPlatformVersion' | 'chUaWow64' | 'computePressure' | 'crossOriginIsolated' | 'directSockets' | 'displayCapture' | 'encryptedMedia' | 'executionWhileNotRendered' | 'executionWhileOutOfViewport' | 'fullscreen' | 'geolocation' | 'gyroscope' | 'hid' | 'identityCredentialsGet' | 'idleDetection' | 'keyboardMap' | 'magnetometer' | 'microphone' | 'midi' | 'navigationOverride' | 'payment' | 'pictureInPicture' | 'publickeyCredentialsGet' | 'screenWakeLock' | 'serial' | 'storageAccess' | 'syncXhr' | 'usb' | 'webShare' | 'windowManagement' | 'xrSpatialTracking';
6
+ /**
7
+ * These features have been proposed, but the definitions have not yet been integrated into their respective specs.
8
+ */
9
+ type ProposedFeatures = 'clipboardRead' | 'clipboardWrite' | 'gemepad' | 'sharedAutofill' | 'speakerSelection';
10
+ /**
11
+ * These features generally have an explainer only, but may be available for experimentation by web developers.
12
+ */
13
+ type ExperimentalFeatures = 'allScreensCapture' | 'browsingTopics' | 'capturedSurfaceControl' | 'conversionMeasurement' | 'digitalCredentialsGet' | 'focusWithoutUserActivation' | 'joinAdInterestGroup' | 'localFonts' | 'runAdAuction' | 'smartCard' | 'syncScript' | 'trustTokenRedemption' | 'unload' | 'verticalScroll';
14
+ export {};
@@ -4,6 +4,7 @@
4
4
  */
5
5
  import type { Context } from '../../context';
6
6
  import type { MiddlewareHandler } from '../../types';
7
+ import type { PermissionsPolicyDirective } from './permissions-policy';
7
8
  export type SecureHeadersVariables = {
8
9
  secureHeadersNonce?: string;
9
10
  };
@@ -45,6 +46,8 @@ interface ReportingEndpointOptions {
45
46
  name: string;
46
47
  url: string;
47
48
  }
49
+ type PermissionsPolicyValue = '*' | 'self' | 'src' | 'none' | string;
50
+ type PermissionsPolicyOptions = Partial<Record<PermissionsPolicyDirective, PermissionsPolicyValue[] | boolean>>;
48
51
  type overridableHeader = boolean | string;
49
52
  interface SecureHeadersOptions {
50
53
  contentSecurityPolicy?: ContentSecurityPolicyOptions;
@@ -63,6 +66,7 @@ interface SecureHeadersOptions {
63
66
  xPermittedCrossDomainPolicies?: overridableHeader;
64
67
  xXssProtection?: overridableHeader;
65
68
  removePoweredBy?: boolean;
69
+ permissionsPolicy?: PermissionsPolicyOptions;
66
70
  }
67
71
  export declare const NONCE: ContentSecurityPolicyOptionHandler;
68
72
  /**
@@ -87,6 +91,7 @@ export declare const NONCE: ContentSecurityPolicyOptionHandler;
87
91
  * @param {overridableHeader} [customOptions.xPermittedCrossDomainPolicies=true] - Settings for the X-Permitted-Cross-Domain-Policies header.
88
92
  * @param {overridableHeader} [customOptions.xXssProtection=true] - Settings for the X-XSS-Protection header.
89
93
  * @param {boolean} [customOptions.removePoweredBy=true] - Settings for remove X-Powered-By header.
94
+ * @param {PermissionsPolicyOptions} [customOptions.permissionsPolicy] - Settings for the Permissions-Policy header.
90
95
  * @returns {MiddlewareHandler} The middleware handler function.
91
96
  *
92
97
  * @example
@@ -7,8 +7,10 @@ import type { Env, MiddlewareHandler } from '../../types';
7
7
  export type ServeStaticOptions<E extends Env = Env> = {
8
8
  root?: string;
9
9
  path?: string;
10
+ precompressed?: boolean;
10
11
  mimes?: Record<string, string>;
11
12
  rewriteRequestPath?: (path: string) => string;
13
+ onFound?: (path: string, c: Context<E>) => void | Promise<void>;
12
14
  onNotFound?: (path: string, c: Context<E>) => void | Promise<void>;
13
15
  };
14
16
  /**
@@ -0,0 +1,5 @@
1
+ export type Auth = (req: Request) => {
2
+ username: string;
3
+ password: string;
4
+ } | undefined;
5
+ export declare const auth: Auth;
@@ -40,5 +40,5 @@ export declare const raw: (value: unknown, callbacks?: HtmlEscapedCallback[]) =>
40
40
  export declare const stringBufferToString: (buffer: StringBuffer, callbacks: HtmlEscapedCallback[] | undefined) => Promise<HtmlEscapedString>;
41
41
  export declare const escapeToBuffer: (str: string, buffer: StringBuffer) => void;
42
42
  export declare const resolveCallbackSync: (str: string | HtmlEscapedString) => string;
43
- export declare const resolveCallback: (str: string | HtmlEscapedString, phase: (typeof HtmlEscapedCallbackPhase)[keyof typeof HtmlEscapedCallbackPhase], preserveCallbacks: boolean, context: object, buffer?: [string]) => Promise<string>;
43
+ export declare const resolveCallback: (str: string | HtmlEscapedString | Promise<string>, phase: (typeof HtmlEscapedCallbackPhase)[keyof typeof HtmlEscapedCallbackPhase], preserveCallbacks: boolean, context: object, buffer?: [string]) => Promise<string>;
44
44
  export {};
@@ -0,0 +1,23 @@
1
+ // src/utils/basic-auth.ts
2
+ import { decodeBase64 } from "./encode.js";
3
+ var CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/;
4
+ var USER_PASS_REGEXP = /^([^:]*):(.*)$/;
5
+ var utf8Decoder = new TextDecoder();
6
+ var auth = (req) => {
7
+ const match = CREDENTIALS_REGEXP.exec(req.headers.get("Authorization") || "");
8
+ if (!match) {
9
+ return void 0;
10
+ }
11
+ let userPass = void 0;
12
+ try {
13
+ userPass = USER_PASS_REGEXP.exec(utf8Decoder.decode(decodeBase64(match[1])));
14
+ } catch {
15
+ }
16
+ if (!userPass) {
17
+ return void 0;
18
+ }
19
+ return { username: userPass[1], password: userPass[2] };
20
+ };
21
+ export {
22
+ auth
23
+ };
@@ -4,7 +4,7 @@ var parseBody = async (request, options = /* @__PURE__ */ Object.create(null)) =
4
4
  const { all = false, dot = false } = options;
5
5
  const headers = request instanceof HonoRequest ? request.raw.headers : request.headers;
6
6
  const contentType = headers.get("Content-Type");
7
- if (contentType !== null && contentType.startsWith("multipart/form-data") || contentType !== null && contentType.startsWith("application/x-www-form-urlencoded")) {
7
+ if (contentType?.startsWith("multipart/form-data") || contentType?.startsWith("application/x-www-form-urlencoded")) {
8
8
  return parseFormData(request, { all, dot });
9
9
  }
10
10
  return {};
@@ -85,6 +85,14 @@ var resolveCallbackSync = (str) => {
85
85
  return buffer[0];
86
86
  };
87
87
  var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) => {
88
+ if (typeof str === "object" && !(str instanceof String)) {
89
+ if (!(str instanceof Promise)) {
90
+ str = str.toString();
91
+ }
92
+ if (str instanceof Promise) {
93
+ str = await str;
94
+ }
95
+ }
88
96
  const callbacks = str.callbacks;
89
97
  if (!callbacks?.length) {
90
98
  return Promise.resolve(str);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "4.5.10",
3
+ "version": "4.6.0",
4
4
  "description": "Web framework built on Web Standards",
5
5
  "main": "dist/cjs/index.js",
6
6
  "type": "module",
@@ -104,6 +104,11 @@
104
104
  "import": "./dist/middleware/compress/index.js",
105
105
  "require": "./dist/cjs/middleware/compress/index.js"
106
106
  },
107
+ "./context-storage": {
108
+ "types": "./dist/types/middleware/context-storage/index.d.ts",
109
+ "import": "./dist/middleware/context-storage/index.js",
110
+ "require": "./dist/cjs/middleware/context-storage/index.js"
111
+ },
107
112
  "./cors": {
108
113
  "types": "./dist/types/middleware/cors/index.d.ts",
109
114
  "import": "./dist/middleware/cors/index.js",
@@ -421,6 +426,9 @@
421
426
  "compress": [
422
427
  "./dist/types/middleware/compress"
423
428
  ],
429
+ "context-storage": [
430
+ "./dist/types/middleware/context-storage"
431
+ ],
424
432
  "cors": [
425
433
  "./dist/types/middleware/cors"
426
434
  ],