hono 1.4.1 → 1.4.2
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/README.md +7 -7
- package/dist/compose.d.ts +1 -1
- package/dist/compose.js +7 -7
- package/dist/context.d.ts +7 -6
- package/dist/context.js +10 -6
- package/dist/hono.d.ts +4 -5
- package/dist/hono.js +5 -7
- package/dist/middleware/basic-auth/index.js +3 -2
- package/dist/middleware/cookie/index.js +1 -1
- package/dist/middleware/cors/index.d.ts +1 -1
- package/dist/middleware/cors/index.js +5 -4
- package/dist/middleware/etag/index.js +3 -2
- package/dist/middleware/graphql-server/index.d.ts +1 -1
- package/dist/middleware/graphql-server/index.js +7 -2
- package/dist/middleware/serve-static/serve-static.d.ts +2 -1
- package/dist/middleware/serve-static/serve-static.js +2 -1
- package/dist/response.d.ts +25 -0
- package/dist/response.js +35 -0
- package/dist/router/reg-exp-router/router.js +3 -0
- package/dist/router/reg-exp-router/trie.js +2 -0
- package/dist/router/trie-router/node.d.ts +1 -1
- package/dist/router/trie-router/router.d.ts +1 -1
- package/dist/utils/body.js +1 -0
- package/dist/utils/buffer.d.ts +1 -1
- package/dist/utils/cloudflare.d.ts +1 -1
- package/dist/utils/cloudflare.js +1 -1
- package/dist/utils/crypto.d.ts +3 -3
- package/dist/utils/crypto.js +1 -0
- package/dist/utils/encode.js +1 -0
- package/dist/utils/mime.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,13 +38,13 @@ app.fire()
|
|
|
38
38
|
**Hono is fastest**, compared to other routers for Cloudflare Workers.
|
|
39
39
|
|
|
40
40
|
```plain
|
|
41
|
-
hono - trie-router(default) x
|
|
42
|
-
hono - regexp-router x
|
|
43
|
-
itty-router x
|
|
44
|
-
sunder x
|
|
45
|
-
worktop x
|
|
46
|
-
Fastest is hono - regexp-router
|
|
47
|
-
✨ Done in
|
|
41
|
+
hono - trie-router(default) x 724,143 ops/sec ±3.63% (80 runs sampled)
|
|
42
|
+
hono - regexp-router x 1,236,810 ops/sec ±6.77% (72 runs sampled)
|
|
43
|
+
itty-router x 161,786 ops/sec ±2.28% (97 runs sampled)
|
|
44
|
+
sunder x 312,262 ops/sec ±2.59% (85 runs sampled)
|
|
45
|
+
worktop x 224,979 ops/sec ±1.13% (96 runs sampled)
|
|
46
|
+
Fastest is hono - regexp-router
|
|
47
|
+
✨ Done in 95.05s.
|
|
48
48
|
```
|
|
49
49
|
|
|
50
50
|
## Why so fast?
|
package/dist/compose.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { ErrorHandler, NotFoundHandler } from './hono';
|
|
2
|
-
export declare const compose: <C>(middleware: Function[], onError?: ErrorHandler, onNotFound?: NotFoundHandler) => (context: C, next?: Function) => Promise<C>;
|
|
2
|
+
export declare const compose: <C>(middleware: Function[], onError?: ErrorHandler<import("./context").Env> | undefined, onNotFound?: NotFoundHandler<import("./context").Env> | undefined) => (context: C, next?: Function | undefined) => Promise<C>;
|
package/dist/compose.js
CHANGED
|
@@ -13,12 +13,12 @@ const compose = (middleware, onError, onNotFound) => {
|
|
|
13
13
|
}
|
|
14
14
|
let handler = middleware[i];
|
|
15
15
|
index = i;
|
|
16
|
-
if (i === middleware.length)
|
|
16
|
+
if (i === middleware.length && next)
|
|
17
17
|
handler = next;
|
|
18
|
-
if (handler
|
|
19
|
-
if (context instanceof context_1.Context && context.res.
|
|
18
|
+
if (!handler) {
|
|
19
|
+
if (context instanceof context_1.Context && context.res._finalized === false && onNotFound) {
|
|
20
20
|
context.res = onNotFound(context);
|
|
21
|
-
context.res.
|
|
21
|
+
context.res._finalized = true;
|
|
22
22
|
}
|
|
23
23
|
return Promise.resolve(context);
|
|
24
24
|
}
|
|
@@ -27,15 +27,15 @@ const compose = (middleware, onError, onNotFound) => {
|
|
|
27
27
|
// If handler return Response like `return c.text('foo')`
|
|
28
28
|
if (res && context instanceof context_1.Context) {
|
|
29
29
|
context.res = res;
|
|
30
|
-
context.res.
|
|
30
|
+
context.res._finalized = true;
|
|
31
31
|
}
|
|
32
32
|
return context;
|
|
33
33
|
})
|
|
34
34
|
.catch((err) => {
|
|
35
|
-
if (
|
|
35
|
+
if (context instanceof context_1.Context && onError) {
|
|
36
36
|
if (err instanceof Error) {
|
|
37
37
|
context.res = onError(err, context);
|
|
38
|
-
context.res.
|
|
38
|
+
context.res._finalized = true;
|
|
39
39
|
}
|
|
40
40
|
return context;
|
|
41
41
|
}
|
package/dist/context.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import { HonoResponse } from './response';
|
|
2
3
|
import type { StatusCode } from './utils/http-status';
|
|
3
4
|
declare type Headers = Record<string, string>;
|
|
4
|
-
declare type Data = string | ArrayBuffer | ReadableStream;
|
|
5
|
+
export declare type Data = string | ArrayBuffer | ReadableStream;
|
|
5
6
|
export declare type Env = Record<string, any>;
|
|
6
7
|
export declare class Context<RequestParamKeyType extends string = string, E = Env> {
|
|
7
8
|
req: Request<RequestParamKeyType>;
|
|
8
9
|
res: Response;
|
|
9
10
|
env: E;
|
|
10
|
-
event: FetchEvent;
|
|
11
|
+
event: FetchEvent | undefined;
|
|
11
12
|
private _status;
|
|
12
13
|
private _pretty;
|
|
13
14
|
private _prettySpace;
|
|
@@ -15,9 +16,9 @@ export declare class Context<RequestParamKeyType extends string = string, E = En
|
|
|
15
16
|
render: (template: string, params?: object, options?: object) => Promise<Response>;
|
|
16
17
|
notFound: () => Response | Promise<Response>;
|
|
17
18
|
constructor(req: Request<RequestParamKeyType>, opts?: {
|
|
18
|
-
env
|
|
19
|
-
event
|
|
20
|
-
res?: Response;
|
|
19
|
+
env?: Env;
|
|
20
|
+
event?: FetchEvent;
|
|
21
|
+
res?: Response | HonoResponse;
|
|
21
22
|
});
|
|
22
23
|
private initRequest;
|
|
23
24
|
header(name: string, value: string): void;
|
|
@@ -25,7 +26,7 @@ export declare class Context<RequestParamKeyType extends string = string, E = En
|
|
|
25
26
|
set(key: string, value: any): void;
|
|
26
27
|
get(key: string): any;
|
|
27
28
|
pretty(prettyJSON: boolean, space?: number): void;
|
|
28
|
-
newResponse(data: Data, init?: ResponseInit): Response;
|
|
29
|
+
newResponse(data: Data | null, init?: ResponseInit): Response;
|
|
29
30
|
body(data: Data | null, status?: StatusCode, headers?: Headers): Response;
|
|
30
31
|
text(text: string, status?: StatusCode, headers?: Headers): Response;
|
|
31
32
|
json(object: object, status?: StatusCode, headers?: Headers): Response;
|
package/dist/context.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Context = void 0;
|
|
4
|
+
const response_1 = require("./response");
|
|
4
5
|
const url_1 = require("./utils/url");
|
|
5
6
|
class Context {
|
|
6
|
-
constructor(req, opts
|
|
7
|
+
constructor(req, opts = {
|
|
8
|
+
env: {},
|
|
9
|
+
event: undefined,
|
|
10
|
+
res: undefined,
|
|
11
|
+
}) {
|
|
7
12
|
this._status = 200;
|
|
8
13
|
this._pretty = false;
|
|
9
14
|
this._prettySpace = 2;
|
|
@@ -11,8 +16,8 @@ class Context {
|
|
|
11
16
|
this._map = {};
|
|
12
17
|
Object.assign(this, opts);
|
|
13
18
|
if (!this.res) {
|
|
14
|
-
const res = new
|
|
15
|
-
res.
|
|
19
|
+
const res = new response_1.HonoResponse(null, { status: 404 });
|
|
20
|
+
res._finalized = false;
|
|
16
21
|
this.res = res;
|
|
17
22
|
}
|
|
18
23
|
}
|
|
@@ -37,7 +42,7 @@ class Context {
|
|
|
37
42
|
else {
|
|
38
43
|
const result = {};
|
|
39
44
|
for (const key of url.searchParams.keys()) {
|
|
40
|
-
result[key] = url.searchParams.get(key);
|
|
45
|
+
result[key] = url.searchParams.get(key) || '';
|
|
41
46
|
}
|
|
42
47
|
return result;
|
|
43
48
|
}
|
|
@@ -75,12 +80,11 @@ class Context {
|
|
|
75
80
|
}
|
|
76
81
|
newResponse(data, init = {}) {
|
|
77
82
|
init.status = init.status || this._status || 200;
|
|
78
|
-
|
|
83
|
+
const headers = {};
|
|
79
84
|
this.res.headers.forEach((v, k) => {
|
|
80
85
|
headers[k] = v;
|
|
81
86
|
});
|
|
82
87
|
init.headers = Object.assign(headers, init.headers);
|
|
83
|
-
headers = {};
|
|
84
88
|
return new Response(data, init);
|
|
85
89
|
}
|
|
86
90
|
body(data, status = this._status, headers = {}) {
|
package/dist/hono.d.ts
CHANGED
|
@@ -22,10 +22,10 @@ declare global {
|
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
24
|
interface Response {
|
|
25
|
-
|
|
25
|
+
_finalized: boolean;
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
-
export declare type Handler<RequestParamKeyType extends string = string, E = Env> = (c: Context<RequestParamKeyType, E>, next: Next) => Response | Promise<Response> | void | Promise<
|
|
28
|
+
export declare type Handler<RequestParamKeyType extends string = string, E = Env> = (c: Context<RequestParamKeyType, E>, next: Next) => Response | Promise<Response> | Promise<void> | Promise<Response | undefined>;
|
|
29
29
|
export declare type NotFoundHandler<E = Env> = (c: Context<string, E>) => Response;
|
|
30
30
|
export declare type ErrorHandler<E = Env> = (err: Error, c: Context<string, E>) => Response;
|
|
31
31
|
export declare type Next = () => Promise<void>;
|
|
@@ -44,11 +44,11 @@ interface Route<E extends Env> {
|
|
|
44
44
|
handler: Handler<string, E>;
|
|
45
45
|
}
|
|
46
46
|
declare const Hono_base: new <E_1 extends Env, T extends string, U>() => {
|
|
47
|
-
delete: HandlerInterface<T, E_1, U>;
|
|
48
|
-
get: HandlerInterface<T, E_1, U>;
|
|
49
47
|
all: HandlerInterface<T, E_1, U>;
|
|
48
|
+
get: HandlerInterface<T, E_1, U>;
|
|
50
49
|
post: HandlerInterface<T, E_1, U>;
|
|
51
50
|
put: HandlerInterface<T, E_1, U>;
|
|
51
|
+
delete: HandlerInterface<T, E_1, U>;
|
|
52
52
|
head: HandlerInterface<T, E_1, U>;
|
|
53
53
|
options: HandlerInterface<T, E_1, U>;
|
|
54
54
|
patch: HandlerInterface<T, E_1, U>;
|
|
@@ -58,7 +58,6 @@ export declare class Hono<E = Env, P extends string = '/'> extends Hono_base<E,
|
|
|
58
58
|
readonly strict: boolean;
|
|
59
59
|
private _tempPath;
|
|
60
60
|
private path;
|
|
61
|
-
private _cachedResponse;
|
|
62
61
|
routes: Route<E>[];
|
|
63
62
|
constructor(init?: Partial<Pick<Hono, 'router' | 'strict'>>);
|
|
64
63
|
private notFoundHandler;
|
package/dist/hono.js
CHANGED
|
@@ -4,7 +4,6 @@ exports.Hono = void 0;
|
|
|
4
4
|
const compose_1 = require("./compose");
|
|
5
5
|
const context_1 = require("./context");
|
|
6
6
|
const router_1 = require("./router");
|
|
7
|
-
const router_2 = require("./router");
|
|
8
7
|
const trie_router_1 = require("./router/trie-router"); // Default Router
|
|
9
8
|
const url_1 = require("./utils/url");
|
|
10
9
|
const methods = ['get', 'post', 'put', 'delete', 'head', 'options', 'patch'];
|
|
@@ -17,6 +16,7 @@ class Hono extends defineDynamicClass() {
|
|
|
17
16
|
super();
|
|
18
17
|
this.router = new trie_router_1.TrieRouter();
|
|
19
18
|
this.strict = true; // strict routing - default is true
|
|
19
|
+
this._tempPath = '';
|
|
20
20
|
this.path = '/';
|
|
21
21
|
this.routes = [];
|
|
22
22
|
this.notFoundHandler = (c) => {
|
|
@@ -28,7 +28,7 @@ class Hono extends defineDynamicClass() {
|
|
|
28
28
|
const message = 'Internal Server Error';
|
|
29
29
|
return c.text(message, 500);
|
|
30
30
|
};
|
|
31
|
-
const allMethods = [...methods,
|
|
31
|
+
const allMethods = [...methods, router_1.METHOD_NAME_ALL_LOWERCASE];
|
|
32
32
|
allMethods.map((method) => {
|
|
33
33
|
this[method] = (args1, ...args) => {
|
|
34
34
|
if (typeof args1 === 'string') {
|
|
@@ -46,9 +46,6 @@ class Hono extends defineDynamicClass() {
|
|
|
46
46
|
};
|
|
47
47
|
});
|
|
48
48
|
Object.assign(this, init);
|
|
49
|
-
this._tempPath = null;
|
|
50
|
-
this._cachedResponse = new Response(null, { status: 404 });
|
|
51
|
-
this._cachedResponse.finalized = false;
|
|
52
49
|
}
|
|
53
50
|
route(path, app) {
|
|
54
51
|
this._tempPath = path;
|
|
@@ -56,7 +53,7 @@ class Hono extends defineDynamicClass() {
|
|
|
56
53
|
app.routes.map((r) => {
|
|
57
54
|
this.addRoute(r.method, r.path, r.handler);
|
|
58
55
|
});
|
|
59
|
-
this._tempPath =
|
|
56
|
+
this._tempPath = '';
|
|
60
57
|
}
|
|
61
58
|
return this;
|
|
62
59
|
}
|
|
@@ -105,12 +102,12 @@ class Hono extends defineDynamicClass() {
|
|
|
105
102
|
return result.params;
|
|
106
103
|
}
|
|
107
104
|
}
|
|
105
|
+
return null;
|
|
108
106
|
});
|
|
109
107
|
const handlers = result ? result.handlers : [this.notFoundHandler];
|
|
110
108
|
const c = new context_1.Context(request, {
|
|
111
109
|
env: env,
|
|
112
110
|
event: event,
|
|
113
|
-
res: this._cachedResponse,
|
|
114
111
|
});
|
|
115
112
|
c.notFound = () => this.notFoundHandler(c);
|
|
116
113
|
const composed = (0, compose_1.compose)(handlers, this.errorHandler, this.notFoundHandler);
|
|
@@ -122,6 +119,7 @@ class Hono extends defineDynamicClass() {
|
|
|
122
119
|
if (err instanceof Error) {
|
|
123
120
|
return this.errorHandler(err, c);
|
|
124
121
|
}
|
|
122
|
+
throw err;
|
|
125
123
|
}
|
|
126
124
|
if (!context.res)
|
|
127
125
|
return context.notFound();
|
|
@@ -15,7 +15,7 @@ const auth = (req) => {
|
|
|
15
15
|
if (!req.headers || typeof req.headers !== 'object') {
|
|
16
16
|
throw new TypeError('argument req is required to have headers property');
|
|
17
17
|
}
|
|
18
|
-
const match = CREDENTIALS_REGEXP.exec(req.headers.get('Authorization'));
|
|
18
|
+
const match = CREDENTIALS_REGEXP.exec(req.headers.get('Authorization') || '');
|
|
19
19
|
if (!match) {
|
|
20
20
|
return undefined;
|
|
21
21
|
}
|
|
@@ -34,6 +34,7 @@ const basicAuth = (options, ...users) => {
|
|
|
34
34
|
}
|
|
35
35
|
users.unshift({ username: options.username, password: options.password });
|
|
36
36
|
return async (ctx, next) => {
|
|
37
|
+
var _a;
|
|
37
38
|
const requestUser = auth(ctx.req);
|
|
38
39
|
if (requestUser) {
|
|
39
40
|
for (const user of users) {
|
|
@@ -49,7 +50,7 @@ const basicAuth = (options, ...users) => {
|
|
|
49
50
|
ctx.res = new Response('Unauthorized', {
|
|
50
51
|
status: 401,
|
|
51
52
|
headers: {
|
|
52
|
-
'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"',
|
|
53
|
+
'WWW-Authenticate': 'Basic realm="' + ((_a = options.realm) === null || _a === void 0 ? void 0 : _a.replace(/"/g, '\\"')) + '"',
|
|
53
54
|
},
|
|
54
55
|
});
|
|
55
56
|
};
|
|
@@ -4,7 +4,7 @@ exports.cookie = void 0;
|
|
|
4
4
|
const cookie = () => {
|
|
5
5
|
return async (c, next) => {
|
|
6
6
|
c.req.cookie = ((name) => {
|
|
7
|
-
const cookie = c.req.headers.get('Cookie');
|
|
7
|
+
const cookie = c.req.headers.get('Cookie') || '';
|
|
8
8
|
const obj = parse(cookie);
|
|
9
9
|
if (name) {
|
|
10
10
|
const value = obj[name];
|
|
@@ -8,5 +8,5 @@ declare type CORSOptions = {
|
|
|
8
8
|
credentials?: boolean;
|
|
9
9
|
exposeHeaders?: string[];
|
|
10
10
|
};
|
|
11
|
-
export declare const cors: (options?: CORSOptions) => (c: Context, next: Next) => Promise<void>;
|
|
11
|
+
export declare const cors: (options?: CORSOptions | undefined) => (c: Context, next: Next) => Promise<void>;
|
|
12
12
|
export {};
|
|
@@ -10,6 +10,7 @@ const cors = (options) => {
|
|
|
10
10
|
};
|
|
11
11
|
const opts = Object.assign(Object.assign({}, defaults), options);
|
|
12
12
|
return async (c, next) => {
|
|
13
|
+
var _a, _b;
|
|
13
14
|
await next();
|
|
14
15
|
function set(key, value) {
|
|
15
16
|
c.res.headers.append(key, value);
|
|
@@ -23,7 +24,7 @@ const cors = (options) => {
|
|
|
23
24
|
if (opts.credentials) {
|
|
24
25
|
set('Access-Control-Allow-Credentials', 'true');
|
|
25
26
|
}
|
|
26
|
-
if (opts.exposeHeaders.length) {
|
|
27
|
+
if ((_a = opts.exposeHeaders) === null || _a === void 0 ? void 0 : _a.length) {
|
|
27
28
|
set('Access-Control-Expose-Headers', opts.exposeHeaders.join(','));
|
|
28
29
|
}
|
|
29
30
|
if (c.req.method === 'OPTIONS') {
|
|
@@ -31,17 +32,17 @@ const cors = (options) => {
|
|
|
31
32
|
if (opts.maxAge != null) {
|
|
32
33
|
set('Access-Control-Max-Age', opts.maxAge.toString());
|
|
33
34
|
}
|
|
34
|
-
if (opts.allowMethods.length) {
|
|
35
|
+
if ((_b = opts.allowMethods) === null || _b === void 0 ? void 0 : _b.length) {
|
|
35
36
|
set('Access-Control-Allow-Methods', opts.allowMethods.join(','));
|
|
36
37
|
}
|
|
37
38
|
let headers = opts.allowHeaders;
|
|
38
|
-
if (!headers.length) {
|
|
39
|
+
if (!(headers === null || headers === void 0 ? void 0 : headers.length)) {
|
|
39
40
|
const requestHeaders = c.req.headers.get('Access-Control-Request-Headers');
|
|
40
41
|
if (requestHeaders) {
|
|
41
42
|
headers = requestHeaders.split(/\s*,\s*/);
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
|
-
if (headers.length) {
|
|
45
|
+
if (headers === null || headers === void 0 ? void 0 : headers.length) {
|
|
45
46
|
set('Access-Control-Allow-Headers', headers.join(','));
|
|
46
47
|
set('Vary', 'Access-Control-Request-Headers');
|
|
47
48
|
}
|
|
@@ -7,8 +7,9 @@ const etag = (options = { weak: false }) => {
|
|
|
7
7
|
return async (c, next) => {
|
|
8
8
|
const ifNoneMatch = c.req.header('If-None-Match') || c.req.header('if-none-match');
|
|
9
9
|
await next();
|
|
10
|
-
const
|
|
11
|
-
const
|
|
10
|
+
const res = c.res;
|
|
11
|
+
const clone = res.clone();
|
|
12
|
+
const body = await (0, body_1.parseBody)(res);
|
|
12
13
|
const hash = await (0, crypto_1.sha1)(body);
|
|
13
14
|
const etag = options.weak ? `W/"${hash}"` : `"${hash}"`;
|
|
14
15
|
if (ifNoneMatch && ifNoneMatch === etag) {
|
|
@@ -18,7 +18,7 @@ export interface GraphQLParams {
|
|
|
18
18
|
raw: boolean;
|
|
19
19
|
}
|
|
20
20
|
export declare const getGraphQLParams: (request: Request) => Promise<GraphQLParams>;
|
|
21
|
-
export declare const errorMessages: (messages: string[], graphqlErrors?: readonly GraphQLError[] | readonly GraphQLFormattedError[]) => {
|
|
21
|
+
export declare const errorMessages: (messages: string[], graphqlErrors?: readonly GraphQLError[] | readonly GraphQLFormattedError[] | undefined) => {
|
|
22
22
|
errors: readonly GraphQLError[] | readonly GraphQLFormattedError[];
|
|
23
23
|
} | {
|
|
24
24
|
errors: {
|
|
@@ -28,6 +28,7 @@ const graphqlServer = (options) => {
|
|
|
28
28
|
console.error(`${e.stack || e.message}`);
|
|
29
29
|
return c.json((0, exports.errorMessages)([e.message], [e]), 400);
|
|
30
30
|
}
|
|
31
|
+
throw e;
|
|
31
32
|
}
|
|
32
33
|
const { query, variables, operationName } = params;
|
|
33
34
|
if (query == null) {
|
|
@@ -51,6 +52,7 @@ const graphqlServer = (options) => {
|
|
|
51
52
|
});
|
|
52
53
|
return c.json((0, exports.errorMessages)(['GraphQL syntax error.'], [e]), 400);
|
|
53
54
|
}
|
|
55
|
+
throw syntaxError;
|
|
54
56
|
}
|
|
55
57
|
// Validate AST, reporting any errors.
|
|
56
58
|
const validationErrors = (0, graphql_1.validate)(schema, documentAST, [...graphql_1.specifiedRules, ...validationRules]);
|
|
@@ -94,9 +96,12 @@ const graphqlServer = (options) => {
|
|
|
94
96
|
// Return 400: Bad Request if any execution context errors exist.
|
|
95
97
|
return c.json((0, exports.errorMessages)(['GraphQL execution context error.'], [e]), 400);
|
|
96
98
|
}
|
|
99
|
+
throw contextError;
|
|
97
100
|
}
|
|
98
|
-
if (result.data
|
|
99
|
-
|
|
101
|
+
if (!result.data) {
|
|
102
|
+
if (result.errors) {
|
|
103
|
+
return c.json((0, exports.errorMessages)([result.errors.toString()], result.errors), 500);
|
|
104
|
+
}
|
|
100
105
|
}
|
|
101
106
|
/*
|
|
102
107
|
Now, does not support GraphiQL
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import type { Env } from '../../context';
|
|
2
3
|
import type { Handler } from '../../hono';
|
|
3
4
|
export declare type ServeStaticOptions = {
|
|
4
5
|
root: string;
|
|
5
6
|
manifest?: object | string;
|
|
6
7
|
namespace?: KVNamespace;
|
|
7
8
|
};
|
|
8
|
-
export declare const serveStatic: (options?: ServeStaticOptions) => Handler
|
|
9
|
+
export declare const serveStatic: (options?: ServeStaticOptions) => Handler<string, Env>;
|
|
@@ -8,7 +8,7 @@ const DEFAULT_DOCUMENT = 'index.html';
|
|
|
8
8
|
const serveStatic = (options = { root: '' }) => {
|
|
9
9
|
return async (c, next) => {
|
|
10
10
|
// Do nothing if Response is already set
|
|
11
|
-
if (c.res && c.res.
|
|
11
|
+
if (c.res && c.res._finalized) {
|
|
12
12
|
await next();
|
|
13
13
|
}
|
|
14
14
|
const url = new URL(c.req.url);
|
|
@@ -33,6 +33,7 @@ const serveStatic = (options = { root: '' }) => {
|
|
|
33
33
|
console.warn(`Static file: ${path} is not found`);
|
|
34
34
|
await next();
|
|
35
35
|
}
|
|
36
|
+
return;
|
|
36
37
|
};
|
|
37
38
|
};
|
|
38
39
|
exports.serveStatic = serveStatic;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Data } from './context';
|
|
2
|
+
export declare class HonoResponse implements Response {
|
|
3
|
+
headers: Headers;
|
|
4
|
+
ok: boolean;
|
|
5
|
+
redirected: boolean;
|
|
6
|
+
status: number;
|
|
7
|
+
statusText: string;
|
|
8
|
+
type: ResponseType;
|
|
9
|
+
url: string;
|
|
10
|
+
_finalized: boolean;
|
|
11
|
+
constructor(_data: Data | null, init: ResponseInit);
|
|
12
|
+
clone(): Response;
|
|
13
|
+
body: ReadableStream<Uint8Array> | null;
|
|
14
|
+
bodyUsed: boolean;
|
|
15
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
16
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
17
|
+
blob(): Promise<Blob>;
|
|
18
|
+
blob(): Promise<Blob>;
|
|
19
|
+
formData(): Promise<FormData>;
|
|
20
|
+
formData(): Promise<FormData>;
|
|
21
|
+
json(): Promise<any>;
|
|
22
|
+
json<T>(): Promise<T>;
|
|
23
|
+
text(): Promise<string>;
|
|
24
|
+
text(): Promise<string>;
|
|
25
|
+
}
|
package/dist/response.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
This Response object is for better performance.
|
|
4
|
+
This object is used in a Context before the Handler dispatches.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.HonoResponse = void 0;
|
|
8
|
+
const errorMessage = 'Response is not finalized';
|
|
9
|
+
class HonoResponse {
|
|
10
|
+
constructor(_data, init) {
|
|
11
|
+
var _a;
|
|
12
|
+
this.headers = new Headers(init.headers);
|
|
13
|
+
this.status = (_a = init.status) !== null && _a !== void 0 ? _a : 404;
|
|
14
|
+
this._finalized = false;
|
|
15
|
+
}
|
|
16
|
+
clone() {
|
|
17
|
+
throw new Error(errorMessage);
|
|
18
|
+
}
|
|
19
|
+
arrayBuffer() {
|
|
20
|
+
throw new Error(errorMessage);
|
|
21
|
+
}
|
|
22
|
+
blob() {
|
|
23
|
+
throw new Error(errorMessage);
|
|
24
|
+
}
|
|
25
|
+
formData() {
|
|
26
|
+
throw new Error(errorMessage);
|
|
27
|
+
}
|
|
28
|
+
json() {
|
|
29
|
+
throw new Error(errorMessage);
|
|
30
|
+
}
|
|
31
|
+
text() {
|
|
32
|
+
throw new Error(errorMessage);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.HonoResponse = HonoResponse;
|
|
@@ -240,6 +240,7 @@ class RegExpRouter {
|
|
|
240
240
|
return this.match(method, path);
|
|
241
241
|
}
|
|
242
242
|
buildAllMatchers() {
|
|
243
|
+
// @ts-ignore
|
|
243
244
|
this.routeData.routes.sort(({ hint: a }, { hint: b }) => {
|
|
244
245
|
if (a.componentsLength !== b.componentsLength) {
|
|
245
246
|
return a.componentsLength - b.componentsLength;
|
|
@@ -265,6 +266,7 @@ class RegExpRouter {
|
|
|
265
266
|
const primaryMatchers = {};
|
|
266
267
|
const secondaryMatchers = {};
|
|
267
268
|
let hasAmbiguous = false;
|
|
269
|
+
// @ts-ignore
|
|
268
270
|
this.routeData.methods.forEach((method) => {
|
|
269
271
|
let _hasAmbiguous;
|
|
270
272
|
[primaryMatchers[method], secondaryMatchers[method], _hasAmbiguous] =
|
|
@@ -280,6 +282,7 @@ class RegExpRouter {
|
|
|
280
282
|
var _a, _b;
|
|
281
283
|
let hasAmbiguous = false;
|
|
282
284
|
const targetMethods = new Set([method, router_1.METHOD_NAME_ALL]);
|
|
285
|
+
// @ts-ignore
|
|
283
286
|
const routes = this.routeData.routes.filter(({ method }) => targetMethods.has(method));
|
|
284
287
|
// Reset temporary data per method
|
|
285
288
|
for (let i = 0, len = routes.length; i < len; i++) {
|
|
@@ -15,6 +15,8 @@ class Trie {
|
|
|
15
15
|
* - character
|
|
16
16
|
*/
|
|
17
17
|
const tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g);
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
19
|
+
// @ts-ignore
|
|
18
20
|
this.root.insert(tokens, index, paramMap, this.context);
|
|
19
21
|
return paramMap;
|
|
20
22
|
}
|
|
@@ -15,6 +15,6 @@ export declare class Node<T> {
|
|
|
15
15
|
insert(method: string, path: string, handler: T): Node<T>;
|
|
16
16
|
private getHandlerSets;
|
|
17
17
|
private next;
|
|
18
|
-
search(method: string, path: string): Result<T
|
|
18
|
+
search(method: string, path: string): Result<T> | null;
|
|
19
19
|
}
|
|
20
20
|
export {};
|
package/dist/utils/body.js
CHANGED
package/dist/utils/buffer.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export declare const equal: (a: ArrayBuffer, b: ArrayBuffer) => boolean;
|
|
2
|
-
export declare const timingSafeEqual: (a: string | object | boolean, b: string | object | boolean, hashFunction?: Function) => Promise<boolean>;
|
|
2
|
+
export declare const timingSafeEqual: (a: string | object | boolean, b: string | object | boolean, hashFunction?: Function | undefined) => Promise<boolean>;
|
|
3
3
|
export declare const bufferToString: (buffer: ArrayBuffer) => string;
|
|
@@ -3,7 +3,7 @@ export declare type KVAssetOptions = {
|
|
|
3
3
|
manifest?: object | string;
|
|
4
4
|
namespace?: KVNamespace;
|
|
5
5
|
};
|
|
6
|
-
export declare const getContentFromKVAsset: (path: string, options?: KVAssetOptions) => Promise<ArrayBuffer>;
|
|
6
|
+
export declare const getContentFromKVAsset: (path: string, options?: KVAssetOptions | undefined) => Promise<ArrayBuffer | null>;
|
|
7
7
|
declare type FilePathOptions = {
|
|
8
8
|
filename: string;
|
|
9
9
|
root?: string;
|
package/dist/utils/cloudflare.js
CHANGED
package/dist/utils/crypto.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ declare type Algorithm = {
|
|
|
3
3
|
alias: string;
|
|
4
4
|
};
|
|
5
5
|
declare type Data = string | object | boolean;
|
|
6
|
-
export declare const sha256: (data: Data) => Promise<string>;
|
|
7
|
-
export declare const sha1: (data: Data) => Promise<string>;
|
|
8
|
-
export declare const createHash: (data: Data, algorithm: Algorithm) => Promise<string>;
|
|
6
|
+
export declare const sha256: (data: Data) => Promise<string | null>;
|
|
7
|
+
export declare const sha1: (data: Data) => Promise<string | null>;
|
|
8
|
+
export declare const createHash: (data: Data, algorithm: Algorithm) => Promise<string | null>;
|
|
9
9
|
export {};
|
package/dist/utils/crypto.js
CHANGED
package/dist/utils/encode.js
CHANGED
|
@@ -71,6 +71,7 @@ const arrayBufferToBase64 = async (buf) => {
|
|
|
71
71
|
return Buffer.from(String.fromCharCode(...new Uint8Array(buf))).toString('base64');
|
|
72
72
|
}
|
|
73
73
|
catch (e) { }
|
|
74
|
+
return '';
|
|
74
75
|
};
|
|
75
76
|
exports.arrayBufferToBase64 = arrayBufferToBase64;
|
|
76
77
|
const arrayBufferToBase64URL = async (buf) => {
|
package/dist/utils/mime.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const getMimeType: (filename: string) => string;
|
|
1
|
+
export declare const getMimeType: (filename: string) => string | undefined;
|