hono 4.7.6 → 4.7.8

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.
@@ -84,30 +84,19 @@ class Context {
84
84
  set res(_res) {
85
85
  this.#isFresh = false;
86
86
  if (this.#res && _res) {
87
- try {
88
- for (const [k, v] of this.#res.headers.entries()) {
89
- if (k === "content-type") {
90
- continue;
91
- }
92
- if (k === "set-cookie") {
93
- const cookies = this.#res.headers.getSetCookie();
94
- _res.headers.delete("set-cookie");
95
- for (const cookie of cookies) {
96
- _res.headers.append("set-cookie", cookie);
97
- }
98
- } else {
99
- _res.headers.set(k, v);
100
- }
87
+ _res = new Response(_res.body, _res);
88
+ for (const [k, v] of this.#res.headers.entries()) {
89
+ if (k === "content-type") {
90
+ continue;
101
91
  }
102
- } catch (e) {
103
- if (e instanceof TypeError && e.message.includes("immutable")) {
104
- this.res = new Response(_res.body, {
105
- headers: _res.headers,
106
- status: _res.status
107
- });
108
- return;
92
+ if (k === "set-cookie") {
93
+ const cookies = this.#res.headers.getSetCookie();
94
+ _res.headers.delete("set-cookie");
95
+ for (const cookie of cookies) {
96
+ _res.headers.append("set-cookie", cookie);
97
+ }
109
98
  } else {
110
- throw e;
99
+ _res.headers.set(k, v);
111
100
  }
112
101
  }
113
102
  }
@@ -124,6 +113,9 @@ class Context {
124
113
  this.#renderer = renderer;
125
114
  };
126
115
  header = (name, value, options) => {
116
+ if (this.finalized) {
117
+ this.#res = new Response(this.#res.body, this.#res);
118
+ }
127
119
  if (value === void 0) {
128
120
  if (this.#headers) {
129
121
  this.#headers.delete(name);
@@ -62,7 +62,7 @@ const preprocessRequestInit = (requestInit) => {
62
62
  return requestInit;
63
63
  };
64
64
  const proxy = async (input, proxyInit) => {
65
- const { raw, ...requestInit } = proxyInit ?? {};
65
+ const { raw, ...requestInit } = proxyInit instanceof Request ? { raw: proxyInit } : proxyInit ?? {};
66
66
  const req = new Request(input, {
67
67
  ...buildRequestInitFromRequest(raw),
68
68
  ...preprocessRequestInit(requestInit)
@@ -51,15 +51,27 @@ const createWSMessageEvent = (source) => {
51
51
  });
52
52
  };
53
53
  const defineWebSocketHelper = (handler) => {
54
- return (createEvents, options) => {
55
- return async function UpgradeWebSocket(c, next) {
56
- const events = await createEvents(c);
57
- const result = await handler(c, events, options);
58
- if (result) {
59
- return result;
60
- }
61
- await next();
62
- };
54
+ return (...args) => {
55
+ if (typeof args[0] === "function") {
56
+ const [createEvents, options] = args;
57
+ return async function upgradeWebSocket(c, next) {
58
+ const events = await createEvents(c);
59
+ const result = await handler(c, events, options);
60
+ if (result) {
61
+ return result;
62
+ }
63
+ await next();
64
+ };
65
+ } else {
66
+ const [c, events, options] = args;
67
+ return (async () => {
68
+ const upgraded = await handler(c, events, options);
69
+ if (!upgraded) {
70
+ throw new Error("Failed to upgrade WebSocket");
71
+ }
72
+ return upgraded;
73
+ })();
74
+ }
63
75
  };
64
76
  };
65
77
  // Annotate the CommonJS export names for ESM import in node:
@@ -138,7 +138,11 @@ class Hono {
138
138
  optionHandler = options;
139
139
  } else {
140
140
  optionHandler = options.optionHandler;
141
- replaceRequest = options.replaceRequest;
141
+ if (options.replaceRequest === false) {
142
+ replaceRequest = (request) => request;
143
+ } else {
144
+ replaceRequest = options.replaceRequest;
145
+ }
142
146
  }
143
147
  }
144
148
  const getOptions = optionHandler ? (c) => {
@@ -25,7 +25,7 @@ module.exports = __toCommonJS(trailing_slash_exports);
25
25
  const trimTrailingSlash = () => {
26
26
  return async function trimTrailingSlash2(c, next) {
27
27
  await next();
28
- if (c.res.status === 404 && c.req.method === "GET" && c.req.path !== "/" && c.req.path.at(-1) === "/") {
28
+ if (c.res.status === 404 && (c.req.method === "GET" || c.req.method === "HEAD") && c.req.path !== "/" && c.req.path.at(-1) === "/") {
29
29
  const url = new URL(c.req.url);
30
30
  url.pathname = url.pathname.substring(0, url.pathname.length - 1);
31
31
  c.res = c.redirect(url.toString(), 301);
@@ -35,7 +35,7 @@ const trimTrailingSlash = () => {
35
35
  const appendTrailingSlash = () => {
36
36
  return async function appendTrailingSlash2(c, next) {
37
37
  await next();
38
- if (c.res.status === 404 && c.req.method === "GET" && c.req.path.at(-1) !== "/") {
38
+ if (c.res.status === 404 && (c.req.method === "GET" || c.req.method === "HEAD") && c.req.path.at(-1) !== "/") {
39
39
  const url = new URL(c.req.url);
40
40
  url.pathname += "/";
41
41
  c.res = c.redirect(url.toString(), 301);
package/dist/context.js CHANGED
@@ -61,30 +61,19 @@ var Context = class {
61
61
  set res(_res) {
62
62
  this.#isFresh = false;
63
63
  if (this.#res && _res) {
64
- try {
65
- for (const [k, v] of this.#res.headers.entries()) {
66
- if (k === "content-type") {
67
- continue;
68
- }
69
- if (k === "set-cookie") {
70
- const cookies = this.#res.headers.getSetCookie();
71
- _res.headers.delete("set-cookie");
72
- for (const cookie of cookies) {
73
- _res.headers.append("set-cookie", cookie);
74
- }
75
- } else {
76
- _res.headers.set(k, v);
77
- }
64
+ _res = new Response(_res.body, _res);
65
+ for (const [k, v] of this.#res.headers.entries()) {
66
+ if (k === "content-type") {
67
+ continue;
78
68
  }
79
- } catch (e) {
80
- if (e instanceof TypeError && e.message.includes("immutable")) {
81
- this.res = new Response(_res.body, {
82
- headers: _res.headers,
83
- status: _res.status
84
- });
85
- return;
69
+ if (k === "set-cookie") {
70
+ const cookies = this.#res.headers.getSetCookie();
71
+ _res.headers.delete("set-cookie");
72
+ for (const cookie of cookies) {
73
+ _res.headers.append("set-cookie", cookie);
74
+ }
86
75
  } else {
87
- throw e;
76
+ _res.headers.set(k, v);
88
77
  }
89
78
  }
90
79
  }
@@ -101,6 +90,9 @@ var Context = class {
101
90
  this.#renderer = renderer;
102
91
  };
103
92
  header = (name, value, options) => {
93
+ if (this.finalized) {
94
+ this.#res = new Response(this.#res.body, this.#res);
95
+ }
104
96
  if (value === void 0) {
105
97
  if (this.#headers) {
106
98
  this.#headers.delete(name);
@@ -40,7 +40,7 @@ var preprocessRequestInit = (requestInit) => {
40
40
  return requestInit;
41
41
  };
42
42
  var proxy = async (input, proxyInit) => {
43
- const { raw, ...requestInit } = proxyInit ?? {};
43
+ const { raw, ...requestInit } = proxyInit instanceof Request ? { raw: proxyInit } : proxyInit ?? {};
44
44
  const req = new Request(input, {
45
45
  ...buildRequestInitFromRequest(raw),
46
46
  ...preprocessRequestInit(requestInit)
@@ -27,15 +27,27 @@ var createWSMessageEvent = (source) => {
27
27
  });
28
28
  };
29
29
  var defineWebSocketHelper = (handler) => {
30
- return (createEvents, options) => {
31
- return async function UpgradeWebSocket(c, next) {
32
- const events = await createEvents(c);
33
- const result = await handler(c, events, options);
34
- if (result) {
35
- return result;
36
- }
37
- await next();
38
- };
30
+ return (...args) => {
31
+ if (typeof args[0] === "function") {
32
+ const [createEvents, options] = args;
33
+ return async function upgradeWebSocket(c, next) {
34
+ const events = await createEvents(c);
35
+ const result = await handler(c, events, options);
36
+ if (result) {
37
+ return result;
38
+ }
39
+ await next();
40
+ };
41
+ } else {
42
+ const [c, events, options] = args;
43
+ return (async () => {
44
+ const upgraded = await handler(c, events, options);
45
+ if (!upgraded) {
46
+ throw new Error("Failed to upgrade WebSocket");
47
+ }
48
+ return upgraded;
49
+ })();
50
+ }
39
51
  };
40
52
  };
41
53
  export {
package/dist/hono-base.js CHANGED
@@ -116,7 +116,11 @@ var Hono = class {
116
116
  optionHandler = options;
117
117
  } else {
118
118
  optionHandler = options.optionHandler;
119
- replaceRequest = options.replaceRequest;
119
+ if (options.replaceRequest === false) {
120
+ replaceRequest = (request) => request;
121
+ } else {
122
+ replaceRequest = options.replaceRequest;
123
+ }
120
124
  }
121
125
  }
122
126
  const getOptions = optionHandler ? (c) => {
@@ -2,7 +2,7 @@
2
2
  var trimTrailingSlash = () => {
3
3
  return async function trimTrailingSlash2(c, next) {
4
4
  await next();
5
- if (c.res.status === 404 && c.req.method === "GET" && c.req.path !== "/" && c.req.path.at(-1) === "/") {
5
+ if (c.res.status === 404 && (c.req.method === "GET" || c.req.method === "HEAD") && c.req.path !== "/" && c.req.path.at(-1) === "/") {
6
6
  const url = new URL(c.req.url);
7
7
  url.pathname = url.pathname.substring(0, url.pathname.length - 1);
8
8
  c.res = c.redirect(url.toString(), 301);
@@ -12,7 +12,7 @@ var trimTrailingSlash = () => {
12
12
  var appendTrailingSlash = () => {
13
13
  return async function appendTrailingSlash2(c, next) {
14
14
  await next();
15
- if (c.res.status === 404 && c.req.method === "GET" && c.req.path.at(-1) !== "/") {
15
+ if (c.res.status === 404 && (c.req.method === "GET" || c.req.method === "HEAD") && c.req.path.at(-1) !== "/") {
16
16
  const url = new URL(c.req.url);
17
17
  url.pathname += "/";
18
18
  c.res = c.redirect(url.toString(), 301);
@@ -3,7 +3,8 @@
3
3
  * WebSocket Helper for Hono.
4
4
  */
5
5
  import type { Context } from '../../context';
6
- import type { MiddlewareHandler } from '../../types';
6
+ import type { MiddlewareHandler, TypedResponse } from '../../types';
7
+ import type { StatusCode } from '../../utils/http-status';
7
8
  /**
8
9
  * WebSocket Event Listeners type
9
10
  */
@@ -16,9 +17,12 @@ export interface WSEvents<T = unknown> {
16
17
  /**
17
18
  * Upgrade WebSocket Type
18
19
  */
19
- export type UpgradeWebSocket<T = unknown, U = any, _WSEvents = WSEvents<T>> = (createEvents: (c: Context) => _WSEvents | Promise<_WSEvents>, options?: U) => MiddlewareHandler<any, string, {
20
- outputFormat: "ws";
21
- }>;
20
+ export interface UpgradeWebSocket<T = unknown, U = any, _WSEvents = WSEvents<T>> {
21
+ (createEvents: (c: Context) => _WSEvents | Promise<_WSEvents>, options?: U): MiddlewareHandler<any, string, {
22
+ outputFormat: "ws";
23
+ }>;
24
+ (c: Context, events: _WSEvents, options?: U): Promise<Response & TypedResponse<{}, StatusCode, "ws">>;
25
+ }
22
26
  /**
23
27
  * ReadyState for WebSocket
24
28
  */
@@ -58,7 +58,7 @@ type MountOptionHandler = (c: Context) => unknown;
58
58
  type MountReplaceRequest = (originalRequest: Request) => Request;
59
59
  type MountOptions = MountOptionHandler | {
60
60
  optionHandler?: MountOptionHandler;
61
- replaceRequest?: MountReplaceRequest;
61
+ replaceRequest?: MountReplaceRequest | false;
62
62
  };
63
63
  declare class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string = "/"> {
64
64
  get: HandlerInterface<E, "get", S, BasePath>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "4.7.6",
3
+ "version": "4.7.8",
4
4
  "description": "Web framework built on Web Standards",
5
5
  "main": "dist/cjs/index.js",
6
6
  "type": "module",
@@ -668,7 +668,7 @@
668
668
  "typescript": "^5.3.3",
669
669
  "vite-plugin-fastly-js-compute": "^0.4.2",
670
670
  "vitest": "^3.0.5",
671
- "wrangler": "3.58.0",
671
+ "wrangler": "4.12.0",
672
672
  "ws": "^8.18.0",
673
673
  "zod": "^3.23.8"
674
674
  },