@kerebron/extension-server-hono 0.4.8 → 0.4.10
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/esm/HonoYjsMemAdapter.d.ts +0 -1
- package/esm/HonoYjsMemAdapter.d.ts.map +1 -1
- package/esm/HonoYjsMemAdapter.js +31 -79
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/context.js +309 -412
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/helper/websocket/index.js +9 -42
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/hono-base.js +192 -299
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/http-exception.js +2 -12
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/request.js +82 -125
- package/esm/mod.d.ts +0 -1
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +1 -1
- package/package.json +1 -1
- package/esm/_dnt.polyfills.d.ts +0 -7
- package/esm/_dnt.polyfills.d.ts.map +0 -1
- package/esm/_dnt.polyfills.js +0 -1
|
@@ -2,64 +2,31 @@
|
|
|
2
2
|
* @module
|
|
3
3
|
* WebSocket Helper for Hono.
|
|
4
4
|
*/
|
|
5
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
6
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
7
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
8
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
9
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
10
|
-
};
|
|
11
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
12
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
13
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
14
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
15
|
-
};
|
|
16
|
-
var _WSContext_init;
|
|
17
5
|
/**
|
|
18
6
|
* A context for controlling WebSockets
|
|
19
7
|
*/
|
|
20
8
|
export class WSContext {
|
|
9
|
+
#init;
|
|
21
10
|
constructor(init) {
|
|
22
|
-
|
|
23
|
-
Object.defineProperty(this, "raw", {
|
|
24
|
-
enumerable: true,
|
|
25
|
-
configurable: true,
|
|
26
|
-
writable: true,
|
|
27
|
-
value: void 0
|
|
28
|
-
});
|
|
29
|
-
Object.defineProperty(this, "binaryType", {
|
|
30
|
-
enumerable: true,
|
|
31
|
-
configurable: true,
|
|
32
|
-
writable: true,
|
|
33
|
-
value: 'arraybuffer'
|
|
34
|
-
});
|
|
35
|
-
Object.defineProperty(this, "url", {
|
|
36
|
-
enumerable: true,
|
|
37
|
-
configurable: true,
|
|
38
|
-
writable: true,
|
|
39
|
-
value: void 0
|
|
40
|
-
});
|
|
41
|
-
Object.defineProperty(this, "protocol", {
|
|
42
|
-
enumerable: true,
|
|
43
|
-
configurable: true,
|
|
44
|
-
writable: true,
|
|
45
|
-
value: void 0
|
|
46
|
-
});
|
|
47
|
-
__classPrivateFieldSet(this, _WSContext_init, init, "f");
|
|
11
|
+
this.#init = init;
|
|
48
12
|
this.raw = init.raw;
|
|
49
13
|
this.url = init.url ? new URL(init.url) : null;
|
|
50
14
|
this.protocol = init.protocol ?? null;
|
|
51
15
|
}
|
|
52
16
|
send(source, options) {
|
|
53
|
-
|
|
17
|
+
this.#init.send(source, options ?? {});
|
|
54
18
|
}
|
|
19
|
+
raw;
|
|
20
|
+
binaryType = 'arraybuffer';
|
|
55
21
|
get readyState() {
|
|
56
|
-
return
|
|
22
|
+
return this.#init.readyState;
|
|
57
23
|
}
|
|
24
|
+
url;
|
|
25
|
+
protocol;
|
|
58
26
|
close(code, reason) {
|
|
59
|
-
|
|
27
|
+
this.#init.close(code, reason);
|
|
60
28
|
}
|
|
61
29
|
}
|
|
62
|
-
_WSContext_init = new WeakMap();
|
|
63
30
|
export const createWSMessageEvent = (source) => {
|
|
64
31
|
return new MessageEvent('message', {
|
|
65
32
|
data: source,
|
|
@@ -2,18 +2,6 @@
|
|
|
2
2
|
* @module
|
|
3
3
|
* This module is the base module for the Hono object.
|
|
4
4
|
*/
|
|
5
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
6
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
7
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
8
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
9
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
10
|
-
};
|
|
11
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
12
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
13
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
14
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
15
|
-
};
|
|
16
|
-
var _Hono_instances, _a, _Hono_path, _Hono_clone, _Hono_notFoundHandler, _Hono_addRoute, _Hono_handleError, _Hono_dispatch;
|
|
17
5
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
18
6
|
import { compose } from './compose.js';
|
|
19
7
|
import { Context } from './context.js';
|
|
@@ -32,235 +20,38 @@ const errorHandler = (err, c) => {
|
|
|
32
20
|
return c.text('Internal Server Error', 500);
|
|
33
21
|
};
|
|
34
22
|
class Hono {
|
|
23
|
+
get;
|
|
24
|
+
post;
|
|
25
|
+
put;
|
|
26
|
+
delete;
|
|
27
|
+
options;
|
|
28
|
+
patch;
|
|
29
|
+
all;
|
|
30
|
+
on;
|
|
31
|
+
use;
|
|
32
|
+
/*
|
|
33
|
+
This class is like an abstract class and does not have a router.
|
|
34
|
+
To use it, inherit the class and implement router in the constructor.
|
|
35
|
+
*/
|
|
36
|
+
router;
|
|
37
|
+
getPath;
|
|
38
|
+
// Cannot use `#` because it requires visibility at JavaScript runtime.
|
|
39
|
+
_basePath = '/';
|
|
40
|
+
#path = '/';
|
|
41
|
+
routes = [];
|
|
35
42
|
constructor(options = {}) {
|
|
36
|
-
_Hono_instances.add(this);
|
|
37
|
-
Object.defineProperty(this, "get", {
|
|
38
|
-
enumerable: true,
|
|
39
|
-
configurable: true,
|
|
40
|
-
writable: true,
|
|
41
|
-
value: void 0
|
|
42
|
-
});
|
|
43
|
-
Object.defineProperty(this, "post", {
|
|
44
|
-
enumerable: true,
|
|
45
|
-
configurable: true,
|
|
46
|
-
writable: true,
|
|
47
|
-
value: void 0
|
|
48
|
-
});
|
|
49
|
-
Object.defineProperty(this, "put", {
|
|
50
|
-
enumerable: true,
|
|
51
|
-
configurable: true,
|
|
52
|
-
writable: true,
|
|
53
|
-
value: void 0
|
|
54
|
-
});
|
|
55
|
-
Object.defineProperty(this, "delete", {
|
|
56
|
-
enumerable: true,
|
|
57
|
-
configurable: true,
|
|
58
|
-
writable: true,
|
|
59
|
-
value: void 0
|
|
60
|
-
});
|
|
61
|
-
Object.defineProperty(this, "options", {
|
|
62
|
-
enumerable: true,
|
|
63
|
-
configurable: true,
|
|
64
|
-
writable: true,
|
|
65
|
-
value: void 0
|
|
66
|
-
});
|
|
67
|
-
Object.defineProperty(this, "patch", {
|
|
68
|
-
enumerable: true,
|
|
69
|
-
configurable: true,
|
|
70
|
-
writable: true,
|
|
71
|
-
value: void 0
|
|
72
|
-
});
|
|
73
|
-
Object.defineProperty(this, "all", {
|
|
74
|
-
enumerable: true,
|
|
75
|
-
configurable: true,
|
|
76
|
-
writable: true,
|
|
77
|
-
value: void 0
|
|
78
|
-
});
|
|
79
|
-
Object.defineProperty(this, "on", {
|
|
80
|
-
enumerable: true,
|
|
81
|
-
configurable: true,
|
|
82
|
-
writable: true,
|
|
83
|
-
value: void 0
|
|
84
|
-
});
|
|
85
|
-
Object.defineProperty(this, "use", {
|
|
86
|
-
enumerable: true,
|
|
87
|
-
configurable: true,
|
|
88
|
-
writable: true,
|
|
89
|
-
value: void 0
|
|
90
|
-
});
|
|
91
|
-
/*
|
|
92
|
-
This class is like an abstract class and does not have a router.
|
|
93
|
-
To use it, inherit the class and implement router in the constructor.
|
|
94
|
-
*/
|
|
95
|
-
Object.defineProperty(this, "router", {
|
|
96
|
-
enumerable: true,
|
|
97
|
-
configurable: true,
|
|
98
|
-
writable: true,
|
|
99
|
-
value: void 0
|
|
100
|
-
});
|
|
101
|
-
Object.defineProperty(this, "getPath", {
|
|
102
|
-
enumerable: true,
|
|
103
|
-
configurable: true,
|
|
104
|
-
writable: true,
|
|
105
|
-
value: void 0
|
|
106
|
-
});
|
|
107
|
-
// Cannot use `#` because it requires visibility at JavaScript runtime.
|
|
108
|
-
Object.defineProperty(this, "_basePath", {
|
|
109
|
-
enumerable: true,
|
|
110
|
-
configurable: true,
|
|
111
|
-
writable: true,
|
|
112
|
-
value: '/'
|
|
113
|
-
});
|
|
114
|
-
_Hono_path.set(this, '/');
|
|
115
|
-
Object.defineProperty(this, "routes", {
|
|
116
|
-
enumerable: true,
|
|
117
|
-
configurable: true,
|
|
118
|
-
writable: true,
|
|
119
|
-
value: []
|
|
120
|
-
});
|
|
121
|
-
_Hono_notFoundHandler.set(this, notFoundHandler
|
|
122
|
-
// Cannot use `#` because it requires visibility at JavaScript runtime.
|
|
123
|
-
);
|
|
124
|
-
// Cannot use `#` because it requires visibility at JavaScript runtime.
|
|
125
|
-
Object.defineProperty(this, "errorHandler", {
|
|
126
|
-
enumerable: true,
|
|
127
|
-
configurable: true,
|
|
128
|
-
writable: true,
|
|
129
|
-
value: errorHandler
|
|
130
|
-
});
|
|
131
|
-
/**
|
|
132
|
-
* `.onError()` handles an error and returns a customized Response.
|
|
133
|
-
*
|
|
134
|
-
* @see {@link https://hono.dev/docs/api/hono#error-handling}
|
|
135
|
-
*
|
|
136
|
-
* @param {ErrorHandler} handler - request Handler for error
|
|
137
|
-
* @returns {Hono} changed Hono instance
|
|
138
|
-
*
|
|
139
|
-
* @example
|
|
140
|
-
* ```ts
|
|
141
|
-
* app.onError((err, c) => {
|
|
142
|
-
* console.error(`${err}`)
|
|
143
|
-
* return c.text('Custom Error Message', 500)
|
|
144
|
-
* })
|
|
145
|
-
* ```
|
|
146
|
-
*/
|
|
147
|
-
Object.defineProperty(this, "onError", {
|
|
148
|
-
enumerable: true,
|
|
149
|
-
configurable: true,
|
|
150
|
-
writable: true,
|
|
151
|
-
value: (handler) => {
|
|
152
|
-
this.errorHandler = handler;
|
|
153
|
-
return this;
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
/**
|
|
157
|
-
* `.notFound()` allows you to customize a Not Found Response.
|
|
158
|
-
*
|
|
159
|
-
* @see {@link https://hono.dev/docs/api/hono#not-found}
|
|
160
|
-
*
|
|
161
|
-
* @param {NotFoundHandler} handler - request handler for not-found
|
|
162
|
-
* @returns {Hono} changed Hono instance
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* ```ts
|
|
166
|
-
* app.notFound((c) => {
|
|
167
|
-
* return c.text('Custom 404 Message', 404)
|
|
168
|
-
* })
|
|
169
|
-
* ```
|
|
170
|
-
*/
|
|
171
|
-
Object.defineProperty(this, "notFound", {
|
|
172
|
-
enumerable: true,
|
|
173
|
-
configurable: true,
|
|
174
|
-
writable: true,
|
|
175
|
-
value: (handler) => {
|
|
176
|
-
__classPrivateFieldSet(this, _Hono_notFoundHandler, handler, "f");
|
|
177
|
-
return this;
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
/**
|
|
181
|
-
* `.fetch()` will be entry point of your app.
|
|
182
|
-
*
|
|
183
|
-
* @see {@link https://hono.dev/docs/api/hono#fetch}
|
|
184
|
-
*
|
|
185
|
-
* @param {Request} request - request Object of request
|
|
186
|
-
* @param {Env} Env - env Object
|
|
187
|
-
* @param {ExecutionContext} - context of execution
|
|
188
|
-
* @returns {Response | Promise<Response>} response of request
|
|
189
|
-
*
|
|
190
|
-
*/
|
|
191
|
-
Object.defineProperty(this, "fetch", {
|
|
192
|
-
enumerable: true,
|
|
193
|
-
configurable: true,
|
|
194
|
-
writable: true,
|
|
195
|
-
value: (request, ...rest) => {
|
|
196
|
-
return __classPrivateFieldGet(this, _Hono_instances, "m", _Hono_dispatch).call(this, request, rest[1], rest[0], request.method);
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
/**
|
|
200
|
-
* `.request()` is a useful method for testing.
|
|
201
|
-
* You can pass a URL or pathname to send a GET request.
|
|
202
|
-
* app will return a Response object.
|
|
203
|
-
* ```ts
|
|
204
|
-
* test('GET /hello is ok', async () => {
|
|
205
|
-
* const res = await app.request('/hello')
|
|
206
|
-
* expect(res.status).toBe(200)
|
|
207
|
-
* })
|
|
208
|
-
* ```
|
|
209
|
-
* @see https://hono.dev/docs/api/hono#request
|
|
210
|
-
*/
|
|
211
|
-
Object.defineProperty(this, "request", {
|
|
212
|
-
enumerable: true,
|
|
213
|
-
configurable: true,
|
|
214
|
-
writable: true,
|
|
215
|
-
value: (input, requestInit, Env, executionCtx) => {
|
|
216
|
-
if (input instanceof Request) {
|
|
217
|
-
return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
|
|
218
|
-
}
|
|
219
|
-
input = input.toString();
|
|
220
|
-
return this.fetch(new Request(/^https?:\/\//.test(input) ? input : `http://localhost${mergePath('/', input)}`, requestInit), Env, executionCtx);
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
/**
|
|
224
|
-
* `.fire()` automatically adds a global fetch event listener.
|
|
225
|
-
* This can be useful for environments that adhere to the Service Worker API, such as non-ES module Cloudflare Workers.
|
|
226
|
-
* @deprecated
|
|
227
|
-
* Use `fire` from `hono/service-worker` instead.
|
|
228
|
-
* ```ts
|
|
229
|
-
* import { Hono } from 'hono'
|
|
230
|
-
* import { fire } from 'hono/service-worker'
|
|
231
|
-
*
|
|
232
|
-
* const app = new Hono()
|
|
233
|
-
* // ...
|
|
234
|
-
* fire(app)
|
|
235
|
-
* ```
|
|
236
|
-
* @see https://hono.dev/docs/api/hono#fire
|
|
237
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API
|
|
238
|
-
* @see https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/
|
|
239
|
-
*/
|
|
240
|
-
Object.defineProperty(this, "fire", {
|
|
241
|
-
enumerable: true,
|
|
242
|
-
configurable: true,
|
|
243
|
-
writable: true,
|
|
244
|
-
value: () => {
|
|
245
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
246
|
-
// @ts-ignore
|
|
247
|
-
addEventListener('fetch', (event) => {
|
|
248
|
-
event.respondWith(__classPrivateFieldGet(this, _Hono_instances, "m", _Hono_dispatch).call(this, event.request, event, undefined, event.request.method));
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
43
|
// Implementation of app.get(...handlers[]) or app.get(path, ...handlers[])
|
|
253
44
|
const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE];
|
|
254
45
|
allMethods.forEach((method) => {
|
|
255
46
|
this[method] = (args1, ...args) => {
|
|
256
47
|
if (typeof args1 === 'string') {
|
|
257
|
-
|
|
48
|
+
this.#path = args1;
|
|
258
49
|
}
|
|
259
50
|
else {
|
|
260
|
-
|
|
51
|
+
this.#addRoute(method, this.#path, args1);
|
|
261
52
|
}
|
|
262
53
|
args.forEach((handler) => {
|
|
263
|
-
|
|
54
|
+
this.#addRoute(method, this.#path, handler);
|
|
264
55
|
});
|
|
265
56
|
return this;
|
|
266
57
|
};
|
|
@@ -268,10 +59,10 @@ class Hono {
|
|
|
268
59
|
// Implementation of app.on(method, path, ...handlers[])
|
|
269
60
|
this.on = (method, path, ...handlers) => {
|
|
270
61
|
for (const p of [path].flat()) {
|
|
271
|
-
|
|
62
|
+
this.#path = p;
|
|
272
63
|
for (const m of [method].flat()) {
|
|
273
64
|
handlers.map((handler) => {
|
|
274
|
-
|
|
65
|
+
this.#addRoute(m.toUpperCase(), this.#path, handler);
|
|
275
66
|
});
|
|
276
67
|
}
|
|
277
68
|
}
|
|
@@ -280,14 +71,14 @@ class Hono {
|
|
|
280
71
|
// Implementation of app.use(...handlers[]) or app.use(path, ...handlers[])
|
|
281
72
|
this.use = (arg1, ...handlers) => {
|
|
282
73
|
if (typeof arg1 === 'string') {
|
|
283
|
-
|
|
74
|
+
this.#path = arg1;
|
|
284
75
|
}
|
|
285
76
|
else {
|
|
286
|
-
|
|
77
|
+
this.#path = '*';
|
|
287
78
|
handlers.unshift(arg1);
|
|
288
79
|
}
|
|
289
80
|
handlers.forEach((handler) => {
|
|
290
|
-
|
|
81
|
+
this.#addRoute(METHOD_NAME_ALL, this.#path, handler);
|
|
291
82
|
});
|
|
292
83
|
return this;
|
|
293
84
|
};
|
|
@@ -295,6 +86,19 @@ class Hono {
|
|
|
295
86
|
Object.assign(this, optionsWithoutStrict);
|
|
296
87
|
this.getPath = strict ?? true ? options.getPath ?? getPath : getPathNoStrict;
|
|
297
88
|
}
|
|
89
|
+
#clone() {
|
|
90
|
+
const clone = new Hono({
|
|
91
|
+
router: this.router,
|
|
92
|
+
getPath: this.getPath,
|
|
93
|
+
});
|
|
94
|
+
clone.errorHandler = this.errorHandler;
|
|
95
|
+
clone.#notFoundHandler = this.#notFoundHandler;
|
|
96
|
+
clone.routes = this.routes;
|
|
97
|
+
return clone;
|
|
98
|
+
}
|
|
99
|
+
#notFoundHandler = notFoundHandler;
|
|
100
|
+
// Cannot use `#` because it requires visibility at JavaScript runtime.
|
|
101
|
+
errorHandler = errorHandler;
|
|
298
102
|
/**
|
|
299
103
|
* `.route()` allows grouping other Hono instance in routes.
|
|
300
104
|
*
|
|
@@ -324,7 +128,7 @@ class Hono {
|
|
|
324
128
|
handler = async (c, next) => (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res;
|
|
325
129
|
handler[COMPOSED_HANDLER] = r.handler;
|
|
326
130
|
}
|
|
327
|
-
|
|
131
|
+
subApp.#addRoute(r.method, r.path, handler);
|
|
328
132
|
});
|
|
329
133
|
return this;
|
|
330
134
|
}
|
|
@@ -342,10 +146,49 @@ class Hono {
|
|
|
342
146
|
* ```
|
|
343
147
|
*/
|
|
344
148
|
basePath(path) {
|
|
345
|
-
const subApp =
|
|
149
|
+
const subApp = this.#clone();
|
|
346
150
|
subApp._basePath = mergePath(this._basePath, path);
|
|
347
151
|
return subApp;
|
|
348
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* `.onError()` handles an error and returns a customized Response.
|
|
155
|
+
*
|
|
156
|
+
* @see {@link https://hono.dev/docs/api/hono#error-handling}
|
|
157
|
+
*
|
|
158
|
+
* @param {ErrorHandler} handler - request Handler for error
|
|
159
|
+
* @returns {Hono} changed Hono instance
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* app.onError((err, c) => {
|
|
164
|
+
* console.error(`${err}`)
|
|
165
|
+
* return c.text('Custom Error Message', 500)
|
|
166
|
+
* })
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
onError = (handler) => {
|
|
170
|
+
this.errorHandler = handler;
|
|
171
|
+
return this;
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* `.notFound()` allows you to customize a Not Found Response.
|
|
175
|
+
*
|
|
176
|
+
* @see {@link https://hono.dev/docs/api/hono#not-found}
|
|
177
|
+
*
|
|
178
|
+
* @param {NotFoundHandler} handler - request handler for not-found
|
|
179
|
+
* @returns {Hono} changed Hono instance
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* app.notFound((c) => {
|
|
184
|
+
* return c.text('Custom 404 Message', 404)
|
|
185
|
+
* })
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
notFound = (handler) => {
|
|
189
|
+
this.#notFoundHandler = handler;
|
|
190
|
+
return this;
|
|
191
|
+
};
|
|
349
192
|
/**
|
|
350
193
|
* `.mount()` allows you to mount applications built with other frameworks into your Hono application.
|
|
351
194
|
*
|
|
@@ -426,73 +269,123 @@ class Hono {
|
|
|
426
269
|
}
|
|
427
270
|
await next();
|
|
428
271
|
};
|
|
429
|
-
|
|
272
|
+
this.#addRoute(METHOD_NAME_ALL, mergePath(path, '*'), handler);
|
|
430
273
|
return this;
|
|
431
274
|
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
clone.errorHandler = this.errorHandler;
|
|
439
|
-
__classPrivateFieldSet(clone, _Hono_notFoundHandler, __classPrivateFieldGet(this, _Hono_notFoundHandler, "f"), "f");
|
|
440
|
-
clone.routes = this.routes;
|
|
441
|
-
return clone;
|
|
442
|
-
}, _Hono_addRoute = function _Hono_addRoute(method, path, handler) {
|
|
443
|
-
method = method.toUpperCase();
|
|
444
|
-
path = mergePath(this._basePath, path);
|
|
445
|
-
const r = { basePath: this._basePath, path, method, handler };
|
|
446
|
-
this.router.add(method, path, [handler, r]);
|
|
447
|
-
this.routes.push(r);
|
|
448
|
-
}, _Hono_handleError = function _Hono_handleError(err, c) {
|
|
449
|
-
if (err instanceof Error) {
|
|
450
|
-
return this.errorHandler(err, c);
|
|
451
|
-
}
|
|
452
|
-
throw err;
|
|
453
|
-
}, _Hono_dispatch = function _Hono_dispatch(request, executionCtx, env, method) {
|
|
454
|
-
// Handle HEAD method
|
|
455
|
-
if (method === 'HEAD') {
|
|
456
|
-
return (async () => new Response(null, await __classPrivateFieldGet(this, _Hono_instances, "m", _Hono_dispatch).call(this, request, executionCtx, env, 'GET')))();
|
|
275
|
+
#addRoute(method, path, handler) {
|
|
276
|
+
method = method.toUpperCase();
|
|
277
|
+
path = mergePath(this._basePath, path);
|
|
278
|
+
const r = { basePath: this._basePath, path, method, handler };
|
|
279
|
+
this.router.add(method, path, [handler, r]);
|
|
280
|
+
this.routes.push(r);
|
|
457
281
|
}
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
path,
|
|
462
|
-
matchResult,
|
|
463
|
-
env,
|
|
464
|
-
executionCtx,
|
|
465
|
-
notFoundHandler: __classPrivateFieldGet(this, _Hono_notFoundHandler, "f"),
|
|
466
|
-
});
|
|
467
|
-
// Do not `compose` if it has only one handler
|
|
468
|
-
if (matchResult[0].length === 1) {
|
|
469
|
-
let res;
|
|
470
|
-
try {
|
|
471
|
-
res = matchResult[0][0][0][0](c, async () => {
|
|
472
|
-
c.res = await __classPrivateFieldGet(this, _Hono_notFoundHandler, "f").call(this, c);
|
|
473
|
-
});
|
|
474
|
-
}
|
|
475
|
-
catch (err) {
|
|
476
|
-
return __classPrivateFieldGet(this, _Hono_instances, "m", _Hono_handleError).call(this, err, c);
|
|
282
|
+
#handleError(err, c) {
|
|
283
|
+
if (err instanceof Error) {
|
|
284
|
+
return this.errorHandler(err, c);
|
|
477
285
|
}
|
|
478
|
-
|
|
479
|
-
? res
|
|
480
|
-
.then((resolved) => resolved || (c.finalized ? c.res : __classPrivateFieldGet(this, _Hono_notFoundHandler, "f").call(this, c)))
|
|
481
|
-
.catch((err) => __classPrivateFieldGet(this, _Hono_instances, "m", _Hono_handleError).call(this, err, c))
|
|
482
|
-
: res ?? __classPrivateFieldGet(this, _Hono_notFoundHandler, "f").call(this, c);
|
|
286
|
+
throw err;
|
|
483
287
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
288
|
+
#dispatch(request, executionCtx, env, method) {
|
|
289
|
+
// Handle HEAD method
|
|
290
|
+
if (method === 'HEAD') {
|
|
291
|
+
return (async () => new Response(null, await this.#dispatch(request, executionCtx, env, 'GET')))();
|
|
292
|
+
}
|
|
293
|
+
const path = this.getPath(request, { env });
|
|
294
|
+
const matchResult = this.router.match(method, path);
|
|
295
|
+
const c = new Context(request, {
|
|
296
|
+
path,
|
|
297
|
+
matchResult,
|
|
298
|
+
env,
|
|
299
|
+
executionCtx,
|
|
300
|
+
notFoundHandler: this.#notFoundHandler,
|
|
301
|
+
});
|
|
302
|
+
// Do not `compose` if it has only one handler
|
|
303
|
+
if (matchResult[0].length === 1) {
|
|
304
|
+
let res;
|
|
305
|
+
try {
|
|
306
|
+
res = matchResult[0][0][0][0](c, async () => {
|
|
307
|
+
c.res = await this.#notFoundHandler(c);
|
|
308
|
+
});
|
|
490
309
|
}
|
|
491
|
-
|
|
310
|
+
catch (err) {
|
|
311
|
+
return this.#handleError(err, c);
|
|
312
|
+
}
|
|
313
|
+
return res instanceof Promise
|
|
314
|
+
? res
|
|
315
|
+
.then((resolved) => resolved || (c.finalized ? c.res : this.#notFoundHandler(c)))
|
|
316
|
+
.catch((err) => this.#handleError(err, c))
|
|
317
|
+
: res ?? this.#notFoundHandler(c);
|
|
492
318
|
}
|
|
493
|
-
|
|
494
|
-
|
|
319
|
+
const composed = compose(matchResult[0], this.errorHandler, this.#notFoundHandler);
|
|
320
|
+
return (async () => {
|
|
321
|
+
try {
|
|
322
|
+
const context = await composed(c);
|
|
323
|
+
if (!context.finalized) {
|
|
324
|
+
throw new Error('Context is not finalized. Did you forget to return a Response object or `await next()`?');
|
|
325
|
+
}
|
|
326
|
+
return context.res;
|
|
327
|
+
}
|
|
328
|
+
catch (err) {
|
|
329
|
+
return this.#handleError(err, c);
|
|
330
|
+
}
|
|
331
|
+
})();
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* `.fetch()` will be entry point of your app.
|
|
335
|
+
*
|
|
336
|
+
* @see {@link https://hono.dev/docs/api/hono#fetch}
|
|
337
|
+
*
|
|
338
|
+
* @param {Request} request - request Object of request
|
|
339
|
+
* @param {Env} Env - env Object
|
|
340
|
+
* @param {ExecutionContext} - context of execution
|
|
341
|
+
* @returns {Response | Promise<Response>} response of request
|
|
342
|
+
*
|
|
343
|
+
*/
|
|
344
|
+
fetch = (request, ...rest) => {
|
|
345
|
+
return this.#dispatch(request, rest[1], rest[0], request.method);
|
|
346
|
+
};
|
|
347
|
+
/**
|
|
348
|
+
* `.request()` is a useful method for testing.
|
|
349
|
+
* You can pass a URL or pathname to send a GET request.
|
|
350
|
+
* app will return a Response object.
|
|
351
|
+
* ```ts
|
|
352
|
+
* test('GET /hello is ok', async () => {
|
|
353
|
+
* const res = await app.request('/hello')
|
|
354
|
+
* expect(res.status).toBe(200)
|
|
355
|
+
* })
|
|
356
|
+
* ```
|
|
357
|
+
* @see https://hono.dev/docs/api/hono#request
|
|
358
|
+
*/
|
|
359
|
+
request = (input, requestInit, Env, executionCtx) => {
|
|
360
|
+
if (input instanceof Request) {
|
|
361
|
+
return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
|
|
495
362
|
}
|
|
496
|
-
|
|
497
|
-
};
|
|
363
|
+
input = input.toString();
|
|
364
|
+
return this.fetch(new Request(/^https?:\/\//.test(input) ? input : `http://localhost${mergePath('/', input)}`, requestInit), Env, executionCtx);
|
|
365
|
+
};
|
|
366
|
+
/**
|
|
367
|
+
* `.fire()` automatically adds a global fetch event listener.
|
|
368
|
+
* This can be useful for environments that adhere to the Service Worker API, such as non-ES module Cloudflare Workers.
|
|
369
|
+
* @deprecated
|
|
370
|
+
* Use `fire` from `hono/service-worker` instead.
|
|
371
|
+
* ```ts
|
|
372
|
+
* import { Hono } from 'hono'
|
|
373
|
+
* import { fire } from 'hono/service-worker'
|
|
374
|
+
*
|
|
375
|
+
* const app = new Hono()
|
|
376
|
+
* // ...
|
|
377
|
+
* fire(app)
|
|
378
|
+
* ```
|
|
379
|
+
* @see https://hono.dev/docs/api/hono#fire
|
|
380
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API
|
|
381
|
+
* @see https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/
|
|
382
|
+
*/
|
|
383
|
+
fire = () => {
|
|
384
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
385
|
+
// @ts-ignore
|
|
386
|
+
addEventListener('fetch', (event) => {
|
|
387
|
+
event.respondWith(this.#dispatch(event.request, event, undefined, event.request.method));
|
|
388
|
+
});
|
|
389
|
+
};
|
|
390
|
+
}
|
|
498
391
|
export { Hono as HonoBase };
|
|
@@ -29,6 +29,8 @@
|
|
|
29
29
|
* ```
|
|
30
30
|
*/
|
|
31
31
|
export class HTTPException extends Error {
|
|
32
|
+
res;
|
|
33
|
+
status;
|
|
32
34
|
/**
|
|
33
35
|
* Creates an instance of `HTTPException`.
|
|
34
36
|
* @param status - HTTP status code for the exception. Defaults to 500.
|
|
@@ -36,18 +38,6 @@ export class HTTPException extends Error {
|
|
|
36
38
|
*/
|
|
37
39
|
constructor(status = 500, options) {
|
|
38
40
|
super(options?.message, { cause: options?.cause });
|
|
39
|
-
Object.defineProperty(this, "res", {
|
|
40
|
-
enumerable: true,
|
|
41
|
-
configurable: true,
|
|
42
|
-
writable: true,
|
|
43
|
-
value: void 0
|
|
44
|
-
});
|
|
45
|
-
Object.defineProperty(this, "status", {
|
|
46
|
-
enumerable: true,
|
|
47
|
-
configurable: true,
|
|
48
|
-
writable: true,
|
|
49
|
-
value: void 0
|
|
50
|
-
});
|
|
51
41
|
this.res = options?.res;
|
|
52
42
|
this.status = status;
|
|
53
43
|
}
|