hono 3.1.4 → 3.1.5

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.
@@ -23,6 +23,7 @@ __export(jwt_exports, {
23
23
  module.exports = __toCommonJS(jwt_exports);
24
24
  var import_http_exception = require("../../http-exception");
25
25
  var import_jwt = require("../../utils/jwt");
26
+ var import_context = require("../../context");
26
27
  const jwt = (options) => {
27
28
  if (!options) {
28
29
  throw new Error('JWT auth middleware requires options for "secret');
@@ -58,14 +59,14 @@ const jwt = (options) => {
58
59
  });
59
60
  throw new import_http_exception.HTTPException(401, { res });
60
61
  }
61
- let authorized = false;
62
+ let payload;
62
63
  let msg = "";
63
64
  try {
64
- authorized = await import_jwt.Jwt.verify(token, options.secret, options.alg);
65
+ payload = await import_jwt.Jwt.verify(token, options.secret, options.alg);
65
66
  } catch (e) {
66
67
  msg = `${e}`;
67
68
  }
68
- if (!authorized) {
69
+ if (!payload) {
69
70
  const res = new Response("Unauthorized", {
70
71
  status: 401,
71
72
  statusText: msg,
@@ -75,6 +76,7 @@ const jwt = (options) => {
75
76
  });
76
77
  throw new import_http_exception.HTTPException(401, { res });
77
78
  }
79
+ ctx.set("jwtPayload", payload);
78
80
  await next();
79
81
  };
80
82
  };
@@ -120,7 +120,7 @@ const verify = async (token, secret, alg = import_types.AlgorithmTypes.HS256) =>
120
120
  if (encodedSignature !== tokenParts[2]) {
121
121
  throw new import_types2.JwtTokenSignatureMismatched(token);
122
122
  }
123
- return true;
123
+ return payload;
124
124
  };
125
125
  const decode = (token) => {
126
126
  try {
@@ -142,8 +142,7 @@ const _getQueryParam = (url, key, multiple) => {
142
142
  if (trailingKeyCode === 61) {
143
143
  const valueIndex = keyIndex2 + key.length + 2;
144
144
  const endIndex = url.indexOf("&", valueIndex);
145
- const value = url.slice(valueIndex, endIndex === -1 ? void 0 : endIndex);
146
- return _decodeURI(value);
145
+ return _decodeURI(url.slice(valueIndex, endIndex === -1 ? void 0 : endIndex));
147
146
  } else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {
148
147
  return "";
149
148
  }
@@ -158,18 +157,27 @@ const _getQueryParam = (url, key, multiple) => {
158
157
  encoded ?? (encoded = /[%+]/.test(url));
159
158
  let keyIndex = url.indexOf("?", 8);
160
159
  while (keyIndex !== -1) {
161
- const valueIndex = url.indexOf("=", keyIndex);
162
- let name = url.slice(keyIndex + 1, valueIndex === -1 ? void 0 : valueIndex);
160
+ const nextKeyIndex = url.indexOf("&", keyIndex + 1);
161
+ let valueIndex = url.indexOf("=", keyIndex);
162
+ if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) {
163
+ valueIndex = -1;
164
+ }
165
+ let name = url.slice(
166
+ keyIndex + 1,
167
+ valueIndex === -1 ? nextKeyIndex === -1 ? void 0 : nextKeyIndex : valueIndex
168
+ );
163
169
  if (encoded) {
164
170
  name = _decodeURI(name);
165
171
  }
172
+ keyIndex = nextKeyIndex;
173
+ if (name === "") {
174
+ continue;
175
+ }
166
176
  let value;
167
177
  if (valueIndex === -1) {
168
- keyIndex = -1;
169
178
  value = "";
170
179
  } else {
171
- keyIndex = url.indexOf("&", valueIndex);
172
- value = url.slice(valueIndex + 1, keyIndex === -1 ? void 0 : keyIndex);
180
+ value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? void 0 : nextKeyIndex);
173
181
  if (encoded) {
174
182
  value = _decodeURI(value);
175
183
  }
@@ -1,6 +1,7 @@
1
1
  // src/middleware/jwt/index.ts
2
2
  import { HTTPException } from "../../http-exception.js";
3
3
  import { Jwt } from "../../utils/jwt/index.js";
4
+ import "../../context.js";
4
5
  var jwt = (options) => {
5
6
  if (!options) {
6
7
  throw new Error('JWT auth middleware requires options for "secret');
@@ -36,14 +37,14 @@ var jwt = (options) => {
36
37
  });
37
38
  throw new HTTPException(401, { res });
38
39
  }
39
- let authorized = false;
40
+ let payload;
40
41
  let msg = "";
41
42
  try {
42
- authorized = await Jwt.verify(token, options.secret, options.alg);
43
+ payload = await Jwt.verify(token, options.secret, options.alg);
43
44
  } catch (e) {
44
45
  msg = `${e}`;
45
46
  }
46
- if (!authorized) {
47
+ if (!payload) {
47
48
  const res = new Response("Unauthorized", {
48
49
  status: 401,
49
50
  statusText: msg,
@@ -53,6 +54,7 @@ var jwt = (options) => {
53
54
  });
54
55
  throw new HTTPException(401, { res });
55
56
  }
57
+ ctx.set("jwtPayload", payload);
56
58
  await next();
57
59
  };
58
60
  };
@@ -1,2 +1,2 @@
1
1
  import type { Context } from './context';
2
- export declare const env: <T = Record<string, string>>(c: Context) => T;
2
+ export declare const env: <T extends Record<string, string>, C extends Context<any, any, {}> = Context<{}, any, {}>>(c: C) => T & C["env"];
@@ -70,8 +70,8 @@ export declare class Context<E extends Env = any, P extends string = any, I exte
70
70
  append?: boolean;
71
71
  }) => void;
72
72
  status: (status: StatusCode) => void;
73
- set: <Key extends keyof E["Variables"]>(key: Key, value: GetVariable<Key, E>) => void;
74
- get: <Key extends keyof E["Variables"]>(key: Key) => GetVariable<Key, E>;
73
+ set: <Key extends keyof E["Variables"] | "jwtPayload">(key: Key, value: GetVariable<Key, E>) => void;
74
+ get: <Key extends keyof E["Variables"] | "jwtPayload">(key: Key) => GetVariable<Key, E>;
75
75
  pretty: (prettyJSON: boolean, space?: number) => void;
76
76
  newResponse: NewResponse;
77
77
  body: BodyRespond;
@@ -1,5 +1,5 @@
1
1
  import { Hono } from './hono';
2
- export type { Env, ErrorHandler, Handler, MiddlewareHandler, Next, NotFoundHandler, ValidationTargets, } from './types';
2
+ export type { Env, ErrorHandler, Handler, MiddlewareHandler, Next, NotFoundHandler, ValidationTargets, Input, } from './types';
3
3
  export type { Context, ContextVariableMap } from './context';
4
4
  export type { HonoRequest } from './request';
5
5
  declare module './hono' {
@@ -1,4 +1,10 @@
1
1
  import type { MiddlewareHandler } from '../../types';
2
+ import '../../context';
3
+ declare module '../../context' {
4
+ interface ContextVariableMap {
5
+ jwtPayload: any;
6
+ }
7
+ }
2
8
  export declare const jwt: (options: {
3
9
  secret: string;
4
10
  cookie?: string;
@@ -1,6 +1,6 @@
1
1
  import { AlgorithmTypes } from './types';
2
2
  export declare const sign: (payload: unknown, secret: string, alg?: AlgorithmTypes) => Promise<string>;
3
- export declare const verify: (token: string, secret: string, alg?: AlgorithmTypes) => Promise<boolean>;
3
+ export declare const verify: (token: string, secret: string, alg?: AlgorithmTypes) => Promise<any>;
4
4
  export declare const decode: (token: string) => {
5
5
  header: any;
6
6
  payload: any;
@@ -84,7 +84,7 @@ var verify = async (token, secret, alg = AlgorithmTypes.HS256) => {
84
84
  if (encodedSignature !== tokenParts[2]) {
85
85
  throw new JwtTokenSignatureMismatched(token);
86
86
  }
87
- return true;
87
+ return payload;
88
88
  };
89
89
  var decode = (token) => {
90
90
  try {
package/dist/utils/url.js CHANGED
@@ -113,8 +113,7 @@ var _getQueryParam = (url, key, multiple) => {
113
113
  if (trailingKeyCode === 61) {
114
114
  const valueIndex = keyIndex2 + key.length + 2;
115
115
  const endIndex = url.indexOf("&", valueIndex);
116
- const value = url.slice(valueIndex, endIndex === -1 ? void 0 : endIndex);
117
- return _decodeURI(value);
116
+ return _decodeURI(url.slice(valueIndex, endIndex === -1 ? void 0 : endIndex));
118
117
  } else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {
119
118
  return "";
120
119
  }
@@ -129,18 +128,27 @@ var _getQueryParam = (url, key, multiple) => {
129
128
  encoded ?? (encoded = /[%+]/.test(url));
130
129
  let keyIndex = url.indexOf("?", 8);
131
130
  while (keyIndex !== -1) {
132
- const valueIndex = url.indexOf("=", keyIndex);
133
- let name = url.slice(keyIndex + 1, valueIndex === -1 ? void 0 : valueIndex);
131
+ const nextKeyIndex = url.indexOf("&", keyIndex + 1);
132
+ let valueIndex = url.indexOf("=", keyIndex);
133
+ if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) {
134
+ valueIndex = -1;
135
+ }
136
+ let name = url.slice(
137
+ keyIndex + 1,
138
+ valueIndex === -1 ? nextKeyIndex === -1 ? void 0 : nextKeyIndex : valueIndex
139
+ );
134
140
  if (encoded) {
135
141
  name = _decodeURI(name);
136
142
  }
143
+ keyIndex = nextKeyIndex;
144
+ if (name === "") {
145
+ continue;
146
+ }
137
147
  let value;
138
148
  if (valueIndex === -1) {
139
- keyIndex = -1;
140
149
  value = "";
141
150
  } else {
142
- keyIndex = url.indexOf("&", valueIndex);
143
- value = url.slice(valueIndex + 1, keyIndex === -1 ? void 0 : keyIndex);
151
+ value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? void 0 : nextKeyIndex);
144
152
  if (encoded) {
145
153
  value = _decodeURI(value);
146
154
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "3.1.4",
3
+ "version": "3.1.5",
4
4
  "description": "Ultrafast web framework for the Edge",
5
5
  "main": "dist/cjs/index.js",
6
6
  "type": "module",