hono 4.0.10 → 4.1.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.
- package/dist/adapter/aws-lambda/handler.js +1 -1
- package/dist/adapter/bun/index.js +2 -0
- package/dist/adapter/bun/websocket.js +77 -0
- package/dist/adapter/cloudflare-workers/index.js +3 -1
- package/dist/adapter/cloudflare-workers/websocket.js +57 -0
- package/dist/adapter/deno/index.js +3 -1
- package/dist/adapter/deno/websocket.js +29 -0
- package/dist/adapter/lambda-edge/handler.js +1 -1
- package/dist/cjs/adapter/aws-lambda/handler.js +1 -1
- package/dist/cjs/adapter/bun/index.js +3 -0
- package/dist/cjs/adapter/bun/websocket.js +98 -0
- package/dist/cjs/adapter/cloudflare-workers/index.js +5 -2
- package/dist/cjs/adapter/cloudflare-workers/websocket.js +80 -0
- package/dist/cjs/adapter/deno/index.js +5 -2
- package/dist/cjs/adapter/deno/websocket.js +52 -0
- package/dist/cjs/adapter/lambda-edge/handler.js +1 -1
- package/dist/cjs/client/client.js +68 -61
- package/dist/cjs/context.js +164 -188
- package/dist/cjs/helper/adapter/index.js +1 -1
- package/dist/cjs/helper/cookie/index.js +48 -8
- package/dist/cjs/helper/dev/index.js +3 -4
- package/dist/cjs/helper/factory/index.js +1 -3
- package/dist/cjs/helper/ssg/index.js +9 -159
- package/dist/cjs/helper/ssg/middleware.js +57 -0
- package/dist/cjs/helper/ssg/ssg.js +212 -0
- package/dist/cjs/helper/websocket/index.js +32 -0
- package/dist/cjs/helper.js +1 -0
- package/dist/cjs/hono-base.js +46 -65
- package/dist/cjs/http-exception.js +3 -1
- package/dist/cjs/jsx/base.js +7 -2
- package/dist/cjs/jsx/components.js +1 -1
- package/dist/cjs/jsx/dom/index.js +45 -3
- package/dist/cjs/jsx/dom/render.js +6 -6
- package/dist/cjs/jsx/hooks/index.js +25 -16
- package/dist/cjs/jsx/index.js +34 -2
- package/dist/cjs/middleware/body-limit/index.js +80 -0
- package/dist/cjs/middleware/logger/index.js +9 -7
- package/dist/cjs/request.js +29 -48
- package/dist/cjs/router/linear-router/router.js +3 -5
- package/dist/cjs/router/pattern-router/router.js +2 -4
- package/dist/cjs/router/reg-exp-router/node.js +3 -3
- package/dist/cjs/router/reg-exp-router/router.js +11 -12
- package/dist/cjs/router/reg-exp-router/trie.js +2 -4
- package/dist/cjs/router/smart-router/router.js +3 -3
- package/dist/cjs/router/trie-router/node.js +6 -2
- package/dist/cjs/router/trie-router/router.js +2 -1
- package/dist/cjs/test-utils/setup-vitest.js +2 -0
- package/dist/cjs/utils/color.js +32 -0
- package/dist/cjs/utils/concurrent.js +62 -0
- package/dist/cjs/utils/cookie.js +28 -1
- package/dist/cjs/utils/stream.js +5 -1
- package/dist/cjs/utils/url.js +2 -2
- package/dist/client/client.js +68 -61
- package/dist/context.js +164 -189
- package/dist/helper/adapter/index.js +1 -1
- package/dist/helper/cookie/index.js +48 -8
- package/dist/helper/dev/index.js +3 -4
- package/dist/helper/factory/index.js +1 -3
- package/dist/helper/ssg/index.js +3 -151
- package/dist/helper/ssg/middleware.js +29 -0
- package/dist/helper/ssg/ssg.js +187 -0
- package/dist/helper/websocket/index.js +9 -0
- package/dist/helper.js +1 -0
- package/dist/hono-base.js +45 -65
- package/dist/http-exception.js +3 -1
- package/dist/jsx/base.js +7 -2
- package/dist/jsx/components.js +1 -1
- package/dist/jsx/dom/index.js +44 -4
- package/dist/jsx/dom/render.js +6 -6
- package/dist/jsx/hooks/index.js +23 -16
- package/dist/jsx/index.js +34 -3
- package/dist/middleware/body-limit/index.js +57 -0
- package/dist/middleware/logger/index.js +9 -7
- package/dist/request.js +29 -49
- package/dist/router/linear-router/router.js +3 -5
- package/dist/router/pattern-router/router.js +2 -4
- package/dist/router/reg-exp-router/node.js +3 -3
- package/dist/router/reg-exp-router/router.js +11 -12
- package/dist/router/reg-exp-router/trie.js +2 -4
- package/dist/router/smart-router/router.js +3 -3
- package/dist/router/trie-router/node.js +6 -2
- package/dist/router/trie-router/router.js +2 -1
- package/dist/test-utils/setup-vitest.js +2 -0
- package/dist/types/adapter/bun/index.d.ts +1 -0
- package/dist/types/adapter/bun/websocket.d.ts +26 -0
- package/dist/types/adapter/cloudflare-workers/index.d.ts +1 -0
- package/dist/types/adapter/cloudflare-workers/websocket.d.ts +2 -0
- package/dist/types/adapter/deno/index.d.ts +1 -0
- package/dist/types/adapter/deno/websocket.d.ts +2 -0
- package/dist/types/client/types.d.ts +9 -0
- package/dist/types/helper/cookie/index.d.ts +3 -1
- package/dist/types/helper/ssg/index.d.ts +2 -105
- package/dist/types/helper/ssg/middleware.d.ts +38 -0
- package/dist/types/helper/ssg/ssg.d.ts +70 -0
- package/dist/types/helper/websocket/index.d.ts +34 -0
- package/dist/types/http-exception.d.ts +1 -0
- package/dist/types/jsx/dom/index.d.ts +40 -7
- package/dist/types/jsx/hooks/index.d.ts +2 -0
- package/dist/types/jsx/index.d.ts +41 -5
- package/dist/types/middleware/body-limit/index.d.ts +28 -0
- package/dist/types/utils/color.d.ts +1 -0
- package/dist/types/utils/concurrent.d.ts +7 -0
- package/dist/types/utils/cookie.d.ts +2 -0
- package/dist/utils/color.js +9 -0
- package/dist/utils/concurrent.js +39 -0
- package/dist/utils/cookie.js +28 -1
- package/dist/utils/stream.js +5 -1
- package/dist/utils/url.js +2 -2
- package/package.json +18 -2
package/dist/jsx/hooks/index.js
CHANGED
|
@@ -97,7 +97,6 @@ var setShadow = (node) => {
|
|
|
97
97
|
node.s?.forEach(setShadow);
|
|
98
98
|
};
|
|
99
99
|
var useState = (initialState) => {
|
|
100
|
-
var _a;
|
|
101
100
|
const resolveInitialState = () => typeof initialState === "function" ? initialState() : initialState;
|
|
102
101
|
const buildData = buildDataStack.at(-1);
|
|
103
102
|
if (!buildData) {
|
|
@@ -105,9 +104,9 @@ var useState = (initialState) => {
|
|
|
105
104
|
}];
|
|
106
105
|
}
|
|
107
106
|
const [, node] = buildData;
|
|
108
|
-
const stateArray =
|
|
107
|
+
const stateArray = node[DOM_STASH][1][STASH_SATE] ||= [];
|
|
109
108
|
const hookIndex = node[DOM_STASH][0]++;
|
|
110
|
-
return stateArray[hookIndex]
|
|
109
|
+
return stateArray[hookIndex] ||= [
|
|
111
110
|
resolveInitialState(),
|
|
112
111
|
(newState) => {
|
|
113
112
|
const localUpdateHook = updateHook;
|
|
@@ -148,18 +147,26 @@ var useState = (initialState) => {
|
|
|
148
147
|
}
|
|
149
148
|
}
|
|
150
149
|
}
|
|
151
|
-
]
|
|
150
|
+
];
|
|
151
|
+
};
|
|
152
|
+
var useReducer = (reducer, initialArg, init) => {
|
|
153
|
+
const [state, setState] = useState(() => init ? init(initialArg) : initialArg);
|
|
154
|
+
return [
|
|
155
|
+
state,
|
|
156
|
+
(action) => {
|
|
157
|
+
setState((state2) => reducer(state2, action));
|
|
158
|
+
}
|
|
159
|
+
];
|
|
152
160
|
};
|
|
153
161
|
var useEffectCommon = (index, effect, deps) => {
|
|
154
|
-
var _a;
|
|
155
162
|
const buildData = buildDataStack.at(-1);
|
|
156
163
|
if (!buildData) {
|
|
157
164
|
return;
|
|
158
165
|
}
|
|
159
166
|
const [, node] = buildData;
|
|
160
|
-
const effectDepsArray =
|
|
167
|
+
const effectDepsArray = node[DOM_STASH][1][STASH_EFFECT] ||= [];
|
|
161
168
|
const hookIndex = node[DOM_STASH][0]++;
|
|
162
|
-
const [prevDeps, , prevCleanup] = effectDepsArray[hookIndex]
|
|
169
|
+
const [prevDeps, , prevCleanup] = effectDepsArray[hookIndex] ||= [];
|
|
163
170
|
if (isDepsChanged(prevDeps, deps)) {
|
|
164
171
|
if (prevCleanup) {
|
|
165
172
|
prevCleanup();
|
|
@@ -176,13 +183,12 @@ var useEffectCommon = (index, effect, deps) => {
|
|
|
176
183
|
var useEffect = (effect, deps) => useEffectCommon(3, effect, deps);
|
|
177
184
|
var useLayoutEffect = (effect, deps) => useEffectCommon(1, effect, deps);
|
|
178
185
|
var useCallback = (callback, deps) => {
|
|
179
|
-
var _a;
|
|
180
186
|
const buildData = buildDataStack.at(-1);
|
|
181
187
|
if (!buildData) {
|
|
182
188
|
return callback;
|
|
183
189
|
}
|
|
184
190
|
const [, node] = buildData;
|
|
185
|
-
const callbackArray =
|
|
191
|
+
const callbackArray = node[DOM_STASH][1][STASH_CALLBACK] ||= [];
|
|
186
192
|
const hookIndex = node[DOM_STASH][0]++;
|
|
187
193
|
const prevDeps = callbackArray[hookIndex];
|
|
188
194
|
if (isDepsChanged(prevDeps?.[1], deps)) {
|
|
@@ -193,18 +199,16 @@ var useCallback = (callback, deps) => {
|
|
|
193
199
|
return callback;
|
|
194
200
|
};
|
|
195
201
|
var useRef = (initialValue) => {
|
|
196
|
-
var _a;
|
|
197
202
|
const buildData = buildDataStack.at(-1);
|
|
198
203
|
if (!buildData) {
|
|
199
204
|
return { current: initialValue };
|
|
200
205
|
}
|
|
201
206
|
const [, node] = buildData;
|
|
202
|
-
const refArray =
|
|
207
|
+
const refArray = node[DOM_STASH][1][STASH_REF] ||= [];
|
|
203
208
|
const hookIndex = node[DOM_STASH][0]++;
|
|
204
|
-
return refArray[hookIndex]
|
|
209
|
+
return refArray[hookIndex] ||= { current: initialValue };
|
|
205
210
|
};
|
|
206
211
|
var use = (promise) => {
|
|
207
|
-
var _a;
|
|
208
212
|
const cachedRes = resolvedPromiseValueMap.get(promise);
|
|
209
213
|
if (cachedRes) {
|
|
210
214
|
if (cachedRes.length === 2) {
|
|
@@ -221,7 +225,7 @@ var use = (promise) => {
|
|
|
221
225
|
throw promise;
|
|
222
226
|
}
|
|
223
227
|
const [, node] = buildData;
|
|
224
|
-
const promiseArray =
|
|
228
|
+
const promiseArray = node[DOM_STASH][1][STASH_USE] ||= [];
|
|
225
229
|
const hookIndex = node[DOM_STASH][0]++;
|
|
226
230
|
promise.then(
|
|
227
231
|
(res2) => {
|
|
@@ -241,13 +245,12 @@ var use = (promise) => {
|
|
|
241
245
|
throw promise;
|
|
242
246
|
};
|
|
243
247
|
var useMemo = (factory, deps) => {
|
|
244
|
-
var _a;
|
|
245
248
|
const buildData = buildDataStack.at(-1);
|
|
246
249
|
if (!buildData) {
|
|
247
250
|
return factory();
|
|
248
251
|
}
|
|
249
252
|
const [, node] = buildData;
|
|
250
|
-
const memoArray =
|
|
253
|
+
const memoArray = node[DOM_STASH][1][STASH_MEMO] ||= [];
|
|
251
254
|
const hookIndex = node[DOM_STASH][0]++;
|
|
252
255
|
const prevDeps = memoArray[hookIndex];
|
|
253
256
|
if (isDepsChanged(prevDeps?.[1], deps)) {
|
|
@@ -255,16 +258,20 @@ var useMemo = (factory, deps) => {
|
|
|
255
258
|
}
|
|
256
259
|
return memoArray[hookIndex][0];
|
|
257
260
|
};
|
|
261
|
+
var useDebugValue = (value, formatter) => {
|
|
262
|
+
};
|
|
258
263
|
export {
|
|
259
264
|
STASH_EFFECT,
|
|
260
265
|
startTransition,
|
|
261
266
|
startViewTransition,
|
|
262
267
|
use,
|
|
263
268
|
useCallback,
|
|
269
|
+
useDebugValue,
|
|
264
270
|
useDeferredValue,
|
|
265
271
|
useEffect,
|
|
266
272
|
useLayoutEffect,
|
|
267
273
|
useMemo,
|
|
274
|
+
useReducer,
|
|
268
275
|
useRef,
|
|
269
276
|
useState,
|
|
270
277
|
useTransition,
|
package/dist/jsx/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/jsx/index.ts
|
|
2
2
|
import { jsx, memo, Fragment, isValidElement, cloneElement } from "./base.js";
|
|
3
3
|
import { ErrorBoundary } from "./components.js";
|
|
4
|
-
import {
|
|
4
|
+
import { createContext, useContext } from "./context.js";
|
|
5
5
|
import {
|
|
6
6
|
useState,
|
|
7
7
|
useEffect,
|
|
@@ -14,16 +14,45 @@ import {
|
|
|
14
14
|
startViewTransition,
|
|
15
15
|
useViewTransition,
|
|
16
16
|
useMemo,
|
|
17
|
-
useLayoutEffect
|
|
17
|
+
useLayoutEffect,
|
|
18
|
+
useReducer,
|
|
19
|
+
useDebugValue
|
|
18
20
|
} from "./hooks/index.js";
|
|
19
|
-
import {
|
|
21
|
+
import { Suspense } from "./streaming.js";
|
|
20
22
|
export * from "./types.js";
|
|
23
|
+
var jsx_default = {
|
|
24
|
+
memo,
|
|
25
|
+
Fragment,
|
|
26
|
+
isValidElement,
|
|
27
|
+
createElement: jsx,
|
|
28
|
+
cloneElement,
|
|
29
|
+
ErrorBoundary,
|
|
30
|
+
createContext,
|
|
31
|
+
useContext,
|
|
32
|
+
useState,
|
|
33
|
+
useEffect,
|
|
34
|
+
useRef,
|
|
35
|
+
useCallback,
|
|
36
|
+
useReducer,
|
|
37
|
+
useDebugValue,
|
|
38
|
+
use,
|
|
39
|
+
startTransition,
|
|
40
|
+
useTransition,
|
|
41
|
+
useDeferredValue,
|
|
42
|
+
startViewTransition,
|
|
43
|
+
useViewTransition,
|
|
44
|
+
useMemo,
|
|
45
|
+
useLayoutEffect,
|
|
46
|
+
Suspense
|
|
47
|
+
};
|
|
21
48
|
export {
|
|
22
49
|
ErrorBoundary,
|
|
23
50
|
Fragment,
|
|
24
51
|
Suspense,
|
|
25
52
|
cloneElement,
|
|
26
53
|
createContext,
|
|
54
|
+
jsx as createElement,
|
|
55
|
+
jsx_default as default,
|
|
27
56
|
isValidElement,
|
|
28
57
|
jsx,
|
|
29
58
|
memo,
|
|
@@ -32,10 +61,12 @@ export {
|
|
|
32
61
|
use,
|
|
33
62
|
useCallback,
|
|
34
63
|
useContext,
|
|
64
|
+
useDebugValue,
|
|
35
65
|
useDeferredValue,
|
|
36
66
|
useEffect,
|
|
37
67
|
useLayoutEffect,
|
|
38
68
|
useMemo,
|
|
69
|
+
useReducer,
|
|
39
70
|
useRef,
|
|
40
71
|
useState,
|
|
41
72
|
useTransition,
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// src/middleware/body-limit/index.ts
|
|
2
|
+
import { HTTPException } from "../../http-exception.js";
|
|
3
|
+
var ERROR_MESSAGE = "Payload Too Large";
|
|
4
|
+
var BodyLimitError = class extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "BodyLimitError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var bodyLimit = (options) => {
|
|
11
|
+
const onError = options.onError || (() => {
|
|
12
|
+
const res = new Response(ERROR_MESSAGE, {
|
|
13
|
+
status: 413
|
|
14
|
+
});
|
|
15
|
+
throw new HTTPException(413, { res });
|
|
16
|
+
});
|
|
17
|
+
const maxSize = options.maxSize;
|
|
18
|
+
return async function bodyLimit2(c, next) {
|
|
19
|
+
if (!c.req.raw.body) {
|
|
20
|
+
return next();
|
|
21
|
+
}
|
|
22
|
+
if (c.req.raw.headers.has("content-length")) {
|
|
23
|
+
const contentLength = parseInt(c.req.raw.headers.get("content-length") || "0", 10);
|
|
24
|
+
return contentLength > maxSize ? onError(c) : next();
|
|
25
|
+
}
|
|
26
|
+
let size = 0;
|
|
27
|
+
const rawReader = c.req.raw.body.getReader();
|
|
28
|
+
const reader = new ReadableStream({
|
|
29
|
+
async start(controller) {
|
|
30
|
+
try {
|
|
31
|
+
for (; ; ) {
|
|
32
|
+
const { done, value } = await rawReader.read();
|
|
33
|
+
if (done) {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
size += value.length;
|
|
37
|
+
if (size > maxSize) {
|
|
38
|
+
controller.error(new BodyLimitError(ERROR_MESSAGE));
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
controller.enqueue(value);
|
|
42
|
+
}
|
|
43
|
+
} finally {
|
|
44
|
+
controller.close();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
c.req.raw = new Request(c.req.raw, { body: reader });
|
|
49
|
+
await next();
|
|
50
|
+
if (c.error instanceof BodyLimitError) {
|
|
51
|
+
c.res = await onError(c);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
export {
|
|
56
|
+
bodyLimit
|
|
57
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/middleware/logger/index.ts
|
|
2
|
+
import { getColorEnabled } from "../../utils/color.js";
|
|
2
3
|
import { getPath } from "../../utils/url.js";
|
|
3
4
|
var humanize = (times) => {
|
|
4
5
|
const [delimiter, separator] = [",", "."];
|
|
@@ -10,14 +11,15 @@ var time = (start) => {
|
|
|
10
11
|
return humanize([delta < 1e3 ? delta + "ms" : Math.round(delta / 1e3) + "s"]);
|
|
11
12
|
};
|
|
12
13
|
var colorStatus = (status) => {
|
|
14
|
+
const colorEnabled = getColorEnabled();
|
|
13
15
|
const out = {
|
|
14
|
-
7: `\x1B[35m${status}\x1B[0m`,
|
|
15
|
-
5: `\x1B[31m${status}\x1B[0m`,
|
|
16
|
-
4: `\x1B[33m${status}\x1B[0m`,
|
|
17
|
-
3: `\x1B[36m${status}\x1B[0m`,
|
|
18
|
-
2: `\x1B[32m${status}\x1B[0m`,
|
|
19
|
-
1: `\x1B[32m${status}\x1B[0m`,
|
|
20
|
-
0: `\x1B[33m${status}\x1B[0m`
|
|
16
|
+
7: colorEnabled ? `\x1B[35m${status}\x1B[0m` : `${status}`,
|
|
17
|
+
5: colorEnabled ? `\x1B[31m${status}\x1B[0m` : `${status}`,
|
|
18
|
+
4: colorEnabled ? `\x1B[33m${status}\x1B[0m` : `${status}`,
|
|
19
|
+
3: colorEnabled ? `\x1B[36m${status}\x1B[0m` : `${status}`,
|
|
20
|
+
2: colorEnabled ? `\x1B[32m${status}\x1B[0m` : `${status}`,
|
|
21
|
+
1: colorEnabled ? `\x1B[32m${status}\x1B[0m` : `${status}`,
|
|
22
|
+
0: colorEnabled ? `\x1B[33m${status}\x1B[0m` : `${status}`
|
|
21
23
|
};
|
|
22
24
|
const calculateStatus = status / 100 | 0;
|
|
23
25
|
return out[calculateStatus];
|
package/dist/request.js
CHANGED
|
@@ -1,63 +1,32 @@
|
|
|
1
|
-
var __accessCheck = (obj, member, msg) => {
|
|
2
|
-
if (!member.has(obj))
|
|
3
|
-
throw TypeError("Cannot " + msg);
|
|
4
|
-
};
|
|
5
|
-
var __privateGet = (obj, member, getter) => {
|
|
6
|
-
__accessCheck(obj, member, "read from private field");
|
|
7
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
8
|
-
};
|
|
9
|
-
var __privateAdd = (obj, member, value) => {
|
|
10
|
-
if (member.has(obj))
|
|
11
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
12
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
13
|
-
};
|
|
14
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
15
|
-
__accessCheck(obj, member, "write to private field");
|
|
16
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
17
|
-
return value;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
1
|
// src/request.ts
|
|
21
2
|
import { parseBody } from "./utils/body.js";
|
|
22
3
|
import { getQueryParam, getQueryParams, decodeURIComponent_ } from "./utils/url.js";
|
|
23
|
-
var _validatedData, _matchResult;
|
|
24
4
|
var HonoRequest = class {
|
|
5
|
+
raw;
|
|
6
|
+
#validatedData;
|
|
7
|
+
#matchResult;
|
|
8
|
+
routeIndex = 0;
|
|
9
|
+
path;
|
|
10
|
+
bodyCache = {};
|
|
25
11
|
constructor(request, path = "/", matchResult = [[]]) {
|
|
26
|
-
__privateAdd(this, _validatedData, void 0);
|
|
27
|
-
__privateAdd(this, _matchResult, void 0);
|
|
28
|
-
this.routeIndex = 0;
|
|
29
|
-
this.bodyCache = {};
|
|
30
|
-
this.cachedBody = (key) => {
|
|
31
|
-
const { bodyCache, raw } = this;
|
|
32
|
-
const cachedBody = bodyCache[key];
|
|
33
|
-
if (cachedBody) {
|
|
34
|
-
return cachedBody;
|
|
35
|
-
}
|
|
36
|
-
if (bodyCache.arrayBuffer) {
|
|
37
|
-
return (async () => {
|
|
38
|
-
return await new Response(bodyCache.arrayBuffer)[key]();
|
|
39
|
-
})();
|
|
40
|
-
}
|
|
41
|
-
return bodyCache[key] = raw[key]();
|
|
42
|
-
};
|
|
43
12
|
this.raw = request;
|
|
44
13
|
this.path = path;
|
|
45
|
-
|
|
46
|
-
|
|
14
|
+
this.#matchResult = matchResult;
|
|
15
|
+
this.#validatedData = {};
|
|
47
16
|
}
|
|
48
17
|
param(key) {
|
|
49
18
|
return key ? this.getDecodedParam(key) : this.getAllDecodedParams();
|
|
50
19
|
}
|
|
51
20
|
getDecodedParam(key) {
|
|
52
|
-
const paramKey =
|
|
21
|
+
const paramKey = this.#matchResult[0][this.routeIndex][1][key];
|
|
53
22
|
const param = this.getParamValue(paramKey);
|
|
54
23
|
return param ? /\%/.test(param) ? decodeURIComponent_(param) : param : void 0;
|
|
55
24
|
}
|
|
56
25
|
getAllDecodedParams() {
|
|
57
26
|
const decoded = {};
|
|
58
|
-
const keys = Object.keys(
|
|
27
|
+
const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]);
|
|
59
28
|
for (const key of keys) {
|
|
60
|
-
const value = this.getParamValue(
|
|
29
|
+
const value = this.getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
|
|
61
30
|
if (value && typeof value === "string") {
|
|
62
31
|
decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value;
|
|
63
32
|
}
|
|
@@ -65,7 +34,7 @@ var HonoRequest = class {
|
|
|
65
34
|
return decoded;
|
|
66
35
|
}
|
|
67
36
|
getParamValue(paramKey) {
|
|
68
|
-
return
|
|
37
|
+
return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey;
|
|
69
38
|
}
|
|
70
39
|
query(key) {
|
|
71
40
|
return getQueryParam(this.url, key);
|
|
@@ -91,6 +60,19 @@ var HonoRequest = class {
|
|
|
91
60
|
this.bodyCache.parsedBody = parsedBody;
|
|
92
61
|
return parsedBody;
|
|
93
62
|
}
|
|
63
|
+
cachedBody = (key) => {
|
|
64
|
+
const { bodyCache, raw } = this;
|
|
65
|
+
const cachedBody = bodyCache[key];
|
|
66
|
+
if (cachedBody) {
|
|
67
|
+
return cachedBody;
|
|
68
|
+
}
|
|
69
|
+
if (bodyCache.arrayBuffer) {
|
|
70
|
+
return (async () => {
|
|
71
|
+
return await new Response(bodyCache.arrayBuffer)[key]();
|
|
72
|
+
})();
|
|
73
|
+
}
|
|
74
|
+
return bodyCache[key] = raw[key]();
|
|
75
|
+
};
|
|
94
76
|
json() {
|
|
95
77
|
return this.cachedBody("json");
|
|
96
78
|
}
|
|
@@ -107,10 +89,10 @@ var HonoRequest = class {
|
|
|
107
89
|
return this.cachedBody("formData");
|
|
108
90
|
}
|
|
109
91
|
addValidatedData(target, data) {
|
|
110
|
-
|
|
92
|
+
this.#validatedData[target] = data;
|
|
111
93
|
}
|
|
112
94
|
valid(target) {
|
|
113
|
-
return
|
|
95
|
+
return this.#validatedData[target];
|
|
114
96
|
}
|
|
115
97
|
get url() {
|
|
116
98
|
return this.raw.url;
|
|
@@ -119,14 +101,12 @@ var HonoRequest = class {
|
|
|
119
101
|
return this.raw.method;
|
|
120
102
|
}
|
|
121
103
|
get matchedRoutes() {
|
|
122
|
-
return
|
|
104
|
+
return this.#matchResult[0].map(([[, route]]) => route);
|
|
123
105
|
}
|
|
124
106
|
get routePath() {
|
|
125
|
-
return
|
|
107
|
+
return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path;
|
|
126
108
|
}
|
|
127
109
|
};
|
|
128
|
-
_validatedData = new WeakMap();
|
|
129
|
-
_matchResult = new WeakMap();
|
|
130
110
|
export {
|
|
131
111
|
HonoRequest
|
|
132
112
|
};
|
|
@@ -5,10 +5,8 @@ var emptyParams = {};
|
|
|
5
5
|
var splitPathRe = /\/(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/[^\/\?]+|(\?)/g;
|
|
6
6
|
var splitByStarRe = /\*/;
|
|
7
7
|
var LinearRouter = class {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
this.routes = [];
|
|
11
|
-
}
|
|
8
|
+
name = "LinearRouter";
|
|
9
|
+
routes = [];
|
|
12
10
|
add(method, path, handler) {
|
|
13
11
|
;
|
|
14
12
|
(checkOptionalParameter(path) || [path]).forEach((p) => {
|
|
@@ -91,7 +89,7 @@ var LinearRouter = class {
|
|
|
91
89
|
value = path.slice(pos + 1, endValuePos);
|
|
92
90
|
pos = endValuePos;
|
|
93
91
|
}
|
|
94
|
-
params[name]
|
|
92
|
+
params[name] ||= value;
|
|
95
93
|
} else {
|
|
96
94
|
const index = path.indexOf(part, pos);
|
|
97
95
|
if (index !== pos) {
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
// src/router/pattern-router/router.ts
|
|
2
2
|
import { METHOD_NAME_ALL, UnsupportedPathError } from "../../router.js";
|
|
3
3
|
var PatternRouter = class {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
this.routes = [];
|
|
7
|
-
}
|
|
4
|
+
name = "PatternRouter";
|
|
5
|
+
routes = [];
|
|
8
6
|
add(method, path, handler) {
|
|
9
7
|
const endsWithWildcard = path[path.length - 1] === "*";
|
|
10
8
|
if (endsWithWildcard) {
|
|
@@ -23,9 +23,9 @@ function compareKey(a, b) {
|
|
|
23
23
|
return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
|
|
24
24
|
}
|
|
25
25
|
var Node = class {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
26
|
+
index;
|
|
27
|
+
varIndex;
|
|
28
|
+
children = {};
|
|
29
29
|
insert(tokens, index, paramMap, context, pathErrorCheckOnly) {
|
|
30
30
|
if (tokens.length === 0) {
|
|
31
31
|
if (this.index !== void 0) {
|
|
@@ -11,9 +11,9 @@ var emptyParam = [];
|
|
|
11
11
|
var nullMatcher = [/^$/, [], {}];
|
|
12
12
|
var wildcardRegExpCache = {};
|
|
13
13
|
function buildWildcardRegExp(path) {
|
|
14
|
-
return wildcardRegExpCache[path]
|
|
14
|
+
return wildcardRegExpCache[path] ??= new RegExp(
|
|
15
15
|
path === "*" ? "" : `^${path.replace(/\/\*/, "(?:|/.*)")}$`
|
|
16
|
-
)
|
|
16
|
+
);
|
|
17
17
|
}
|
|
18
18
|
function clearWildcardRegExpCache() {
|
|
19
19
|
wildcardRegExpCache = {};
|
|
@@ -87,13 +87,14 @@ function findMiddleware(middleware, path) {
|
|
|
87
87
|
return void 0;
|
|
88
88
|
}
|
|
89
89
|
var RegExpRouter = class {
|
|
90
|
+
name = "RegExpRouter";
|
|
91
|
+
middleware;
|
|
92
|
+
routes;
|
|
90
93
|
constructor() {
|
|
91
|
-
this.name = "RegExpRouter";
|
|
92
94
|
this.middleware = { [METHOD_NAME_ALL]: {} };
|
|
93
95
|
this.routes = { [METHOD_NAME_ALL]: {} };
|
|
94
96
|
}
|
|
95
97
|
add(method, path, handler) {
|
|
96
|
-
var _a;
|
|
97
98
|
const { middleware, routes } = this;
|
|
98
99
|
if (!middleware || !routes) {
|
|
99
100
|
throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
@@ -115,11 +116,10 @@ var RegExpRouter = class {
|
|
|
115
116
|
const re = buildWildcardRegExp(path);
|
|
116
117
|
if (method === METHOD_NAME_ALL) {
|
|
117
118
|
Object.keys(middleware).forEach((m) => {
|
|
118
|
-
|
|
119
|
-
(_a2 = middleware[m])[path] || (_a2[path] = findMiddleware(middleware[m], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || []);
|
|
119
|
+
middleware[m][path] ||= findMiddleware(middleware[m], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
|
120
120
|
});
|
|
121
121
|
} else {
|
|
122
|
-
|
|
122
|
+
middleware[method][path] ||= findMiddleware(middleware[method], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
|
123
123
|
}
|
|
124
124
|
Object.keys(middleware).forEach((m) => {
|
|
125
125
|
if (method === METHOD_NAME_ALL || method === m) {
|
|
@@ -141,11 +141,10 @@ var RegExpRouter = class {
|
|
|
141
141
|
for (let i = 0, len = paths.length; i < len; i++) {
|
|
142
142
|
const path2 = paths[i];
|
|
143
143
|
Object.keys(routes).forEach((m) => {
|
|
144
|
-
var _a2;
|
|
145
144
|
if (method === METHOD_NAME_ALL || method === m) {
|
|
146
|
-
|
|
145
|
+
routes[m][path2] ||= [
|
|
147
146
|
...findMiddleware(middleware[m], path2) || findMiddleware(middleware[METHOD_NAME_ALL], path2) || []
|
|
148
|
-
]
|
|
147
|
+
];
|
|
149
148
|
routes[m][path2].push([handler, paramCount - len + i + 1]);
|
|
150
149
|
}
|
|
151
150
|
});
|
|
@@ -172,7 +171,7 @@ var RegExpRouter = class {
|
|
|
172
171
|
buildAllMatchers() {
|
|
173
172
|
const matchers = {};
|
|
174
173
|
[...Object.keys(this.routes), ...Object.keys(this.middleware)].forEach((method) => {
|
|
175
|
-
matchers[method]
|
|
174
|
+
matchers[method] ||= this.buildMatcher(method);
|
|
176
175
|
});
|
|
177
176
|
this.middleware = this.routes = void 0;
|
|
178
177
|
return matchers;
|
|
@@ -183,7 +182,7 @@ var RegExpRouter = class {
|
|
|
183
182
|
[this.middleware, this.routes].forEach((r) => {
|
|
184
183
|
const ownRoute = r[method] ? Object.keys(r[method]).map((path) => [path, r[method][path]]) : [];
|
|
185
184
|
if (ownRoute.length !== 0) {
|
|
186
|
-
hasOwnRoute
|
|
185
|
+
hasOwnRoute ||= true;
|
|
187
186
|
routes.push(...ownRoute);
|
|
188
187
|
} else if (method !== METHOD_NAME_ALL) {
|
|
189
188
|
routes.push(
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
// src/router/reg-exp-router/trie.ts
|
|
2
2
|
import { Node } from "./node.js";
|
|
3
3
|
var Trie = class {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
this.root = new Node();
|
|
7
|
-
}
|
|
4
|
+
context = { varIndex: 0 };
|
|
5
|
+
root = new Node();
|
|
8
6
|
insert(path, index, pathErrorCheckOnly) {
|
|
9
7
|
const paramAssoc = [];
|
|
10
8
|
const groups = [];
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// src/router/smart-router/router.ts
|
|
2
2
|
import { UnsupportedPathError, MESSAGE_MATCHER_IS_ALREADY_BUILT } from "../../router.js";
|
|
3
3
|
var SmartRouter = class {
|
|
4
|
+
name = "SmartRouter";
|
|
5
|
+
routers = [];
|
|
6
|
+
routes = [];
|
|
4
7
|
constructor(init) {
|
|
5
|
-
this.name = "SmartRouter";
|
|
6
|
-
this.routers = [];
|
|
7
|
-
this.routes = [];
|
|
8
8
|
Object.assign(this, init);
|
|
9
9
|
}
|
|
10
10
|
add(method, path, handler) {
|
|
@@ -2,9 +2,13 @@
|
|
|
2
2
|
import { METHOD_NAME_ALL } from "../../router.js";
|
|
3
3
|
import { splitPath, splitRoutingPath, getPattern } from "../../utils/url.js";
|
|
4
4
|
var Node = class {
|
|
5
|
+
methods;
|
|
6
|
+
children;
|
|
7
|
+
patterns;
|
|
8
|
+
order = 0;
|
|
9
|
+
name;
|
|
10
|
+
params = {};
|
|
5
11
|
constructor(method, handler, children) {
|
|
6
|
-
this.order = 0;
|
|
7
|
-
this.params = {};
|
|
8
12
|
this.children = children || {};
|
|
9
13
|
this.methods = [];
|
|
10
14
|
this.name = "";
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
import { checkOptionalParameter } from "../../utils/url.js";
|
|
3
3
|
import { Node } from "./node.js";
|
|
4
4
|
var TrieRouter = class {
|
|
5
|
+
name = "TrieRouter";
|
|
6
|
+
node;
|
|
5
7
|
constructor() {
|
|
6
|
-
this.name = "TrieRouter";
|
|
7
8
|
this.node = new Node();
|
|
8
9
|
}
|
|
9
10
|
add(method, path, handler) {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { type UpgradeWebSocket } from '../../helper/websocket';
|
|
3
|
+
interface BunServerWebSocket<T> {
|
|
4
|
+
send(data: string | ArrayBufferLike, compress?: boolean): void;
|
|
5
|
+
close(code?: number, reason?: string): void;
|
|
6
|
+
data: T;
|
|
7
|
+
readyState: 0 | 1 | 2 | 3;
|
|
8
|
+
}
|
|
9
|
+
interface BunWebSocketHandler<T> {
|
|
10
|
+
open(ws: BunServerWebSocket<T>): void;
|
|
11
|
+
close(ws: BunServerWebSocket<T>, code?: number, reason?: string): void;
|
|
12
|
+
message(ws: BunServerWebSocket<T>, message: string | Buffer): void;
|
|
13
|
+
}
|
|
14
|
+
interface CreateWebSocket {
|
|
15
|
+
(): {
|
|
16
|
+
upgradeWebSocket: UpgradeWebSocket;
|
|
17
|
+
websocket: BunWebSocketHandler<BunWebSocketData>;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export interface BunWebSocketData {
|
|
21
|
+
connId: number;
|
|
22
|
+
url: URL;
|
|
23
|
+
protocol: string;
|
|
24
|
+
}
|
|
25
|
+
export declare const createBunWebSocket: CreateWebSocket;
|
|
26
|
+
export {};
|