hono 1.6.3 → 2.0.1
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 +20 -765
- package/dist/compose.d.ts +1 -1
- package/dist/compose.js +5 -5
- package/dist/context.d.ts +31 -6
- package/dist/context.js +25 -9
- package/dist/hono.d.ts +3 -3
- package/dist/hono.js +3 -3
- package/dist/index.d.ts +1 -2
- package/dist/index.js +2 -3
- package/dist/middleware/basic-auth/index.js +0 -9
- package/dist/middleware/compress/index.d.ts +8 -0
- package/dist/middleware/compress/index.js +19 -0
- package/dist/middleware/cors/index.d.ts +1 -1
- package/dist/middleware/jsx/index.js +21 -1
- package/dist/middleware/jwt/index.js +3 -0
- package/dist/middleware/logger/index.d.ts +3 -5
- package/dist/middleware/logger/index.js +16 -17
- package/dist/middleware/serve-static/bun.d.ts +7 -0
- package/dist/middleware/serve-static/bun.js +38 -0
- package/dist/middleware/serve-static/module.d.mts +3 -1
- package/dist/middleware/serve-static/serve-static.d.ts +1 -2
- package/dist/request.d.ts +9 -0
- package/dist/request.js +19 -0
- package/dist/utils/buffer.d.ts +1 -1
- package/dist/utils/cloudflare.d.ts +1 -1
- package/dist/utils/cookie.d.ts +13 -0
- package/dist/{middleware/cookie/index.js → utils/cookie.js} +3 -22
- package/dist/utils/jwt/jwt.js +4 -1
- package/dist/utils/jwt/types.d.ts +6 -1
- package/dist/utils/jwt/types.js +9 -4
- package/package.json +13 -23
- package/dist/middleware/body-parse/index.d.ts +0 -8
- package/dist/middleware/body-parse/index.js +0 -11
- package/dist/middleware/cookie/index.d.ts +0 -27
- package/dist/middleware/graphql-server/index.d.ts +0 -28
- package/dist/middleware/graphql-server/index.js +0 -174
- package/dist/middleware/graphql-server/parse-body.d.ts +0 -1
- package/dist/middleware/graphql-server/parse-body.js +0 -31
- package/dist/middleware/mustache/index.d.ts +0 -1
- package/dist/middleware/mustache/index.js +0 -5
- package/dist/middleware/mustache/module.d.mts +0 -3
- package/dist/middleware/mustache/module.mjs +0 -12
- package/dist/middleware/mustache/mustache.d.ts +0 -14
- package/dist/middleware/mustache/mustache.js +0 -53
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
|
|
2
|
+
export declare const compose: <C>(middleware: Function[], onError?: ErrorHandler, onNotFound?: NotFoundHandler) => (context: C, next?: Function) => Promise<C>;
|
package/dist/compose.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.compose = void 0;
|
|
|
4
4
|
const context_1 = require("./context");
|
|
5
5
|
// Based on the code in the MIT licensed `koa-compose` package.
|
|
6
6
|
const compose = (middleware, onError, onNotFound) => {
|
|
7
|
-
return
|
|
7
|
+
return (context, next) => {
|
|
8
8
|
let index = -1;
|
|
9
9
|
return dispatch(0);
|
|
10
10
|
async function dispatch(i) {
|
|
@@ -16,21 +16,21 @@ const compose = (middleware, onError, onNotFound) => {
|
|
|
16
16
|
if (i === middleware.length && next)
|
|
17
17
|
handler = next;
|
|
18
18
|
if (!handler) {
|
|
19
|
-
if (context instanceof context_1.
|
|
19
|
+
if (context instanceof context_1.HonoContext && context.finalized === false && onNotFound) {
|
|
20
20
|
context.res = await onNotFound(context);
|
|
21
21
|
}
|
|
22
22
|
return Promise.resolve(context);
|
|
23
23
|
}
|
|
24
24
|
return Promise.resolve(handler(context, () => dispatch(i + 1)))
|
|
25
|
-
.then(
|
|
25
|
+
.then((res) => {
|
|
26
26
|
// If handler return Response like `return c.text('foo')`
|
|
27
|
-
if (res && context instanceof context_1.
|
|
27
|
+
if (res && context instanceof context_1.HonoContext) {
|
|
28
28
|
context.res = res;
|
|
29
29
|
}
|
|
30
30
|
return context;
|
|
31
31
|
})
|
|
32
32
|
.catch((err) => {
|
|
33
|
-
if (context instanceof context_1.
|
|
33
|
+
if (context instanceof context_1.HonoContext && onError) {
|
|
34
34
|
if (err instanceof Error) {
|
|
35
35
|
context.res = onError(err, context);
|
|
36
36
|
}
|
package/dist/context.d.ts
CHANGED
|
@@ -1,23 +1,47 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
2
|
import type { NotFoundHandler } from './hono';
|
|
3
|
+
import type { CookieOptions } from './utils/cookie';
|
|
3
4
|
import type { StatusCode } from './utils/http-status';
|
|
4
5
|
declare type Headers = Record<string, string>;
|
|
5
6
|
export declare type Data = string | ArrayBuffer | ReadableStream;
|
|
6
|
-
|
|
7
|
-
export
|
|
7
|
+
declare type Env = Record<string, any>;
|
|
8
|
+
export interface Context<RequestParamKeyType extends string = string, E = Env> {
|
|
8
9
|
req: Request<RequestParamKeyType>;
|
|
9
10
|
env: E;
|
|
10
|
-
event: FetchEvent
|
|
11
|
-
executionCtx: ExecutionContext
|
|
11
|
+
event: FetchEvent;
|
|
12
|
+
executionCtx: ExecutionContext;
|
|
12
13
|
finalized: boolean;
|
|
13
|
-
|
|
14
|
+
get res(): Response;
|
|
15
|
+
set res(_res: Response);
|
|
16
|
+
header: (name: string, value: string) => void;
|
|
17
|
+
status: (status: StatusCode) => void;
|
|
18
|
+
set: (key: string, value: any) => void;
|
|
19
|
+
get: (key: string) => any;
|
|
20
|
+
pretty: (prettyJSON: boolean, space?: number) => void;
|
|
21
|
+
newResponse: (data: Data | null, status: StatusCode, headers: Headers) => Response;
|
|
22
|
+
body: (data: Data | null, status?: StatusCode, headers?: Headers) => Response;
|
|
23
|
+
text: (text: string, status?: StatusCode, headers?: Headers) => Response;
|
|
24
|
+
json: <T>(object: T, status?: StatusCode, headers?: Headers) => Response;
|
|
25
|
+
html: (html: string, status?: StatusCode, headers?: Headers) => Response;
|
|
26
|
+
redirect: (location: string, status?: StatusCode) => Response;
|
|
27
|
+
cookie: (name: string, value: string, options?: CookieOptions) => void;
|
|
28
|
+
notFound: () => Response | Promise<Response>;
|
|
29
|
+
}
|
|
30
|
+
export declare class HonoContext<RequestParamKeyType extends string = string, E = Env> implements Context<RequestParamKeyType, E> {
|
|
31
|
+
req: Request<RequestParamKeyType>;
|
|
32
|
+
env: E;
|
|
33
|
+
finalized: boolean;
|
|
34
|
+
_status: StatusCode;
|
|
35
|
+
private _executionCtx;
|
|
14
36
|
private _pretty;
|
|
15
37
|
private _prettySpace;
|
|
16
38
|
private _map;
|
|
17
39
|
private _headers;
|
|
18
40
|
private _res;
|
|
19
41
|
private notFoundHandler;
|
|
20
|
-
constructor(req: Request, env?: E | undefined,
|
|
42
|
+
constructor(req: Request, env?: E | undefined, executionCtx?: FetchEvent | ExecutionContext | undefined, notFoundHandler?: NotFoundHandler);
|
|
43
|
+
get event(): FetchEvent;
|
|
44
|
+
get executionCtx(): ExecutionContext;
|
|
21
45
|
get res(): Response;
|
|
22
46
|
set res(_res: Response);
|
|
23
47
|
header(name: string, value: string): void;
|
|
@@ -31,6 +55,7 @@ export declare class Context<RequestParamKeyType extends string = string, E = En
|
|
|
31
55
|
json<T>(object: T, status?: StatusCode, headers?: Headers): Response;
|
|
32
56
|
html(html: string, status?: StatusCode, headers?: Headers): Response;
|
|
33
57
|
redirect(location: string, status?: StatusCode): Response;
|
|
58
|
+
cookie(name: string, value: string, opt?: CookieOptions): void;
|
|
34
59
|
notFound(): Response | Promise<Response>;
|
|
35
60
|
}
|
|
36
61
|
export {};
|
package/dist/context.js
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.HonoContext = void 0;
|
|
4
|
+
const cookie_1 = require("./utils/cookie");
|
|
4
5
|
const url_1 = require("./utils/url");
|
|
5
|
-
class
|
|
6
|
-
constructor(req, env = undefined,
|
|
6
|
+
class HonoContext {
|
|
7
|
+
constructor(req, env = undefined, executionCtx = undefined, notFoundHandler = () => new Response()) {
|
|
7
8
|
this._status = 200;
|
|
8
9
|
this._pretty = false;
|
|
9
10
|
this._prettySpace = 2;
|
|
11
|
+
this._executionCtx = executionCtx;
|
|
10
12
|
this.req = req;
|
|
11
13
|
this.env = env ? env : {};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
this.notFoundHandler = notFoundHandler;
|
|
15
|
+
this.finalized = false;
|
|
16
|
+
}
|
|
17
|
+
get event() {
|
|
18
|
+
if (this._executionCtx instanceof FetchEvent) {
|
|
19
|
+
return this._executionCtx;
|
|
14
20
|
}
|
|
15
21
|
else {
|
|
16
|
-
|
|
22
|
+
throw Error('This context has no FetchEvent');
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
get executionCtx() {
|
|
26
|
+
if (this._executionCtx) {
|
|
27
|
+
return this._executionCtx;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
throw Error('This context has no ExecutionContext');
|
|
17
31
|
}
|
|
18
|
-
this.notFoundHandler = notFoundHandler;
|
|
19
|
-
this.finalized = false;
|
|
20
32
|
}
|
|
21
33
|
get res() {
|
|
22
34
|
return (this._res || (this._res = new Response()));
|
|
@@ -89,8 +101,12 @@ class Context {
|
|
|
89
101
|
Location: location,
|
|
90
102
|
});
|
|
91
103
|
}
|
|
104
|
+
cookie(name, value, opt) {
|
|
105
|
+
const cookie = (0, cookie_1.serialize)(name, value, opt);
|
|
106
|
+
this.header('Set-Cookie', cookie);
|
|
107
|
+
}
|
|
92
108
|
notFound() {
|
|
93
109
|
return this.notFoundHandler(this);
|
|
94
110
|
}
|
|
95
111
|
}
|
|
96
|
-
exports.
|
|
112
|
+
exports.HonoContext = HonoContext;
|
package/dist/hono.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import { Context } from './context';
|
|
3
|
-
import type { Env } from './context';
|
|
2
|
+
import type { Context } from './context';
|
|
4
3
|
import type { Router } from './router';
|
|
4
|
+
declare type Env = Record<string, any>;
|
|
5
5
|
export declare type Handler<RequestParamKeyType extends string = string, E = Env> = (c: Context<RequestParamKeyType, E>, next: Next) => Response | Promise<Response> | Promise<void> | Promise<Response | undefined>;
|
|
6
6
|
export declare type NotFoundHandler<E = Env> = (c: Context<string, E>) => Response | Promise<Response>;
|
|
7
7
|
export declare type ErrorHandler<E = Env> = (err: Error, c: Context<string, E>) => Response;
|
|
@@ -48,7 +48,7 @@ export declare class Hono<E extends Env = Env, P extends string = '/'> extends H
|
|
|
48
48
|
private matchRoute;
|
|
49
49
|
private dispatch;
|
|
50
50
|
handleEvent(event: FetchEvent): Promise<Response>;
|
|
51
|
-
fetch: (request: Request, env?: E
|
|
51
|
+
fetch: (request: Request, env?: E, executionCtx?: ExecutionContext) => Promise<Response>;
|
|
52
52
|
request(input: RequestInfo, requestInit?: RequestInit): Promise<Response>;
|
|
53
53
|
}
|
|
54
54
|
export {};
|
package/dist/hono.js
CHANGED
|
@@ -29,7 +29,7 @@ class Hono extends defineDynamicClass() {
|
|
|
29
29
|
const message = 'Internal Server Error';
|
|
30
30
|
return c.text(message, 500);
|
|
31
31
|
};
|
|
32
|
-
this.fetch =
|
|
32
|
+
this.fetch = (request, env, executionCtx) => {
|
|
33
33
|
return this.dispatch(request, executionCtx, env);
|
|
34
34
|
};
|
|
35
35
|
(0, request_1.extendRequestPrototype)();
|
|
@@ -100,7 +100,7 @@ class Hono extends defineDynamicClass() {
|
|
|
100
100
|
const result = this.matchRoute(method, path);
|
|
101
101
|
request.paramData = result?.params;
|
|
102
102
|
const handlers = result ? result.handlers : [this.notFoundHandler];
|
|
103
|
-
const c = new context_1.
|
|
103
|
+
const c = new context_1.HonoContext(request, env, eventOrExecutionCtx, this.notFoundHandler);
|
|
104
104
|
const composed = (0, compose_1.compose)(handlers, this.errorHandler, this.notFoundHandler);
|
|
105
105
|
let context;
|
|
106
106
|
try {
|
|
@@ -117,7 +117,7 @@ class Hono extends defineDynamicClass() {
|
|
|
117
117
|
}
|
|
118
118
|
return context.res;
|
|
119
119
|
}
|
|
120
|
-
|
|
120
|
+
handleEvent(event) {
|
|
121
121
|
return this.dispatch(event.request, event);
|
|
122
122
|
}
|
|
123
123
|
request(input, requestInit) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/// <reference path="request.d.ts" />
|
|
2
2
|
import { Hono } from './hono';
|
|
3
3
|
export type { Handler, Next } from './hono';
|
|
4
|
-
export { Context } from './context';
|
|
5
|
-
export type { Env } from './context';
|
|
4
|
+
export type { Context } from './context';
|
|
6
5
|
declare module './hono' {
|
|
7
6
|
interface Hono {
|
|
8
7
|
fire(): void;
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// @denoify-ignore
|
|
2
3
|
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
|
|
3
4
|
/// <reference path="./request.ts" /> Import "declare global" for the Request interface.
|
|
4
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.Hono =
|
|
6
|
+
exports.Hono = void 0;
|
|
6
7
|
const hono_1 = require("./hono");
|
|
7
8
|
Object.defineProperty(exports, "Hono", { enumerable: true, get: function () { return hono_1.Hono; } });
|
|
8
|
-
var context_1 = require("./context");
|
|
9
|
-
Object.defineProperty(exports, "Context", { enumerable: true, get: function () { return context_1.Context; } });
|
|
10
9
|
hono_1.Hono.prototype.fire = function () {
|
|
11
10
|
addEventListener('fetch', (event) => {
|
|
12
11
|
void event.respondWith(this.handleEvent(event));
|
|
@@ -6,15 +6,6 @@ const encode_1 = require("../../utils/encode");
|
|
|
6
6
|
const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/;
|
|
7
7
|
const USER_PASS_REGEXP = /^([^:]*):(.*)$/;
|
|
8
8
|
const auth = (req) => {
|
|
9
|
-
if (!req) {
|
|
10
|
-
throw new TypeError('argument req is required');
|
|
11
|
-
}
|
|
12
|
-
if (typeof req !== 'object') {
|
|
13
|
-
throw new TypeError('argument req is required to be an object');
|
|
14
|
-
}
|
|
15
|
-
if (!req.headers || typeof req.headers !== 'object') {
|
|
16
|
-
throw new TypeError('argument req is required to have headers property');
|
|
17
|
-
}
|
|
18
9
|
const match = CREDENTIALS_REGEXP.exec(req.headers.get('Authorization') || '');
|
|
19
10
|
if (!match) {
|
|
20
11
|
return undefined;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Context } from '../../context';
|
|
2
|
+
import type { Next } from '../../hono';
|
|
3
|
+
declare type EncodingType = 'gzip' | 'deflate';
|
|
4
|
+
interface CompressionOptions {
|
|
5
|
+
encoding?: EncodingType;
|
|
6
|
+
}
|
|
7
|
+
export declare const compress: (options?: CompressionOptions) => (ctx: Context, next: Next) => Promise<void>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compress = void 0;
|
|
4
|
+
const compress = (options) => {
|
|
5
|
+
return async (ctx, next) => {
|
|
6
|
+
await next();
|
|
7
|
+
const accepted = ctx.req.headers.get('Accept-Encoding');
|
|
8
|
+
const pattern = options?.encoding ?? /gzip|deflate/;
|
|
9
|
+
const match = accepted?.match(pattern);
|
|
10
|
+
if (!accepted || !match || !ctx.res.body) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const encoding = match[0];
|
|
14
|
+
const stream = new CompressionStream(encoding);
|
|
15
|
+
ctx.res = new Response(ctx.res.body.pipeThrough(stream), ctx.res.clone());
|
|
16
|
+
ctx.res.headers.set('Content-Encoding', encoding);
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
exports.compress = compress;
|
|
@@ -8,5 +8,5 @@ declare type CORSOptions = {
|
|
|
8
8
|
credentials?: boolean;
|
|
9
9
|
exposeHeaders?: string[];
|
|
10
10
|
};
|
|
11
|
-
export declare const cors: (options?: CORSOptions
|
|
11
|
+
export declare const cors: (options?: CORSOptions) => (c: Context, next: Next) => Promise<void>;
|
|
12
12
|
export {};
|
|
@@ -2,6 +2,23 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Fragment = exports.memo = exports.jsx = void 0;
|
|
4
4
|
const html_1 = require("../../utils/html");
|
|
5
|
+
const emptyTags = [
|
|
6
|
+
'area',
|
|
7
|
+
'base',
|
|
8
|
+
'br',
|
|
9
|
+
'col',
|
|
10
|
+
'embed',
|
|
11
|
+
'hr',
|
|
12
|
+
'img',
|
|
13
|
+
'input',
|
|
14
|
+
'keygen',
|
|
15
|
+
'link',
|
|
16
|
+
'meta',
|
|
17
|
+
'param',
|
|
18
|
+
'source',
|
|
19
|
+
'track',
|
|
20
|
+
'wbr',
|
|
21
|
+
];
|
|
5
22
|
const jsx = (tag, props, ...children) => {
|
|
6
23
|
if (typeof tag === 'function') {
|
|
7
24
|
return tag.call(null, { ...props, children: children.length <= 1 ? children[0] : children });
|
|
@@ -25,6 +42,9 @@ const jsx = (tag, props, ...children) => {
|
|
|
25
42
|
result += ` ${propsKeys[i]}="${(0, html_1.escape)(v.toString())}"`;
|
|
26
43
|
}
|
|
27
44
|
if (tag !== '') {
|
|
45
|
+
if (emptyTags.includes(tag)) {
|
|
46
|
+
result += '/';
|
|
47
|
+
}
|
|
28
48
|
result += '>';
|
|
29
49
|
}
|
|
30
50
|
const flattenChildren = children.flat(Infinity);
|
|
@@ -40,7 +60,7 @@ const jsx = (tag, props, ...children) => {
|
|
|
40
60
|
result += (0, html_1.escape)(child.toString());
|
|
41
61
|
}
|
|
42
62
|
}
|
|
43
|
-
if (tag !== '') {
|
|
63
|
+
if (tag !== '' && !emptyTags.includes(tag)) {
|
|
44
64
|
result += `</${tag}>`;
|
|
45
65
|
}
|
|
46
66
|
const escapedString = new String(result);
|
|
@@ -6,6 +6,9 @@ const jwt = (options) => {
|
|
|
6
6
|
if (!options) {
|
|
7
7
|
throw new Error('JWT auth middleware requires options for "secret');
|
|
8
8
|
}
|
|
9
|
+
if (!crypto.subtle || !crypto.subtle.importKey) {
|
|
10
|
+
throw new Error('`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.');
|
|
11
|
+
}
|
|
9
12
|
return async (ctx, next) => {
|
|
10
13
|
const credentials = ctx.req.headers.get('Authorization');
|
|
11
14
|
if (!credentials) {
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import type { Context } from '../../context';
|
|
2
2
|
import type { Next } from '../../hono';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
(message?: any, ...optionalParams: any[]): void;
|
|
7
|
-
}) => (c: Context, next: Next) => Promise<void>;
|
|
3
|
+
declare type PrintFunc = (str: string, ...rest: string[]) => void;
|
|
4
|
+
export declare const logger: (fn: PrintFunc) => (c: Context, next: Next) => Promise<void>;
|
|
5
|
+
export {};
|
|
@@ -2,24 +2,22 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.logger = void 0;
|
|
4
4
|
const url_1 = require("../../utils/url");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
var LogPrefix;
|
|
6
|
+
(function (LogPrefix) {
|
|
7
|
+
LogPrefix["Outgoing"] = "-->";
|
|
8
|
+
LogPrefix["Incoming"] = "<--";
|
|
9
|
+
LogPrefix["Error"] = "xxx";
|
|
10
|
+
})(LogPrefix || (LogPrefix = {}));
|
|
11
|
+
const humanize = (times) => {
|
|
12
|
+
const [delimiter, separator] = [',', '.'];
|
|
13
|
+
const orderTimes = times.map((v) => v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + delimiter));
|
|
14
|
+
return orderTimes.join(separator);
|
|
12
15
|
};
|
|
13
16
|
const time = (start) => {
|
|
14
17
|
const delta = Date.now() - start;
|
|
15
|
-
return humanize([delta <
|
|
18
|
+
return humanize([delta < 1000 ? delta + 'ms' : Math.round(delta / 1000) + 's']);
|
|
16
19
|
};
|
|
17
|
-
const
|
|
18
|
-
Outgoing: '-->',
|
|
19
|
-
Incoming: '<--',
|
|
20
|
-
Error: 'xxx',
|
|
21
|
-
};
|
|
22
|
-
const colorStatus = (status = 0) => {
|
|
20
|
+
const colorStatus = (status) => {
|
|
23
21
|
const out = {
|
|
24
22
|
7: `\x1b[35m${status}\x1b[0m`,
|
|
25
23
|
5: `\x1b[31m${status}\x1b[0m`,
|
|
@@ -29,15 +27,16 @@ const colorStatus = (status = 0) => {
|
|
|
29
27
|
1: `\x1b[32m${status}\x1b[0m`,
|
|
30
28
|
0: `\x1b[33m${status}\x1b[0m`,
|
|
31
29
|
};
|
|
32
|
-
|
|
30
|
+
const calculateStatus = (status / 100) | 0;
|
|
31
|
+
return out[calculateStatus];
|
|
33
32
|
};
|
|
34
|
-
function log(fn, prefix, method, path, status, elapsed) {
|
|
33
|
+
function log(fn, prefix, method, path, status = 0, elapsed) {
|
|
35
34
|
const out = prefix === LogPrefix.Incoming
|
|
36
35
|
? ` ${prefix} ${method} ${path}`
|
|
37
36
|
: ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed}`;
|
|
38
37
|
fn(out);
|
|
39
38
|
}
|
|
40
|
-
const logger = (fn
|
|
39
|
+
const logger = (fn) => {
|
|
41
40
|
return async (c, next) => {
|
|
42
41
|
const { method } = c.req;
|
|
43
42
|
const path = (0, url_1.getPathFromURL)(c.req.url);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Context } from '../../context';
|
|
2
|
+
import type { Next } from '../../hono';
|
|
3
|
+
export declare type ServeStaticOptions = {
|
|
4
|
+
root?: string;
|
|
5
|
+
path?: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const serveStatic: (options?: ServeStaticOptions) => (c: Context, next: Next) => Promise<Response | undefined>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serveStatic = void 0;
|
|
4
|
+
const filepath_1 = require("../../utils/filepath");
|
|
5
|
+
const mime_1 = require("../../utils/mime");
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
const { file } = Bun;
|
|
8
|
+
const DEFAULT_DOCUMENT = 'index.html';
|
|
9
|
+
const serveStatic = (options = { root: '' }) => {
|
|
10
|
+
return async (c, next) => {
|
|
11
|
+
// Do nothing if Response is already set
|
|
12
|
+
if (c.res && c.finalized) {
|
|
13
|
+
await next();
|
|
14
|
+
}
|
|
15
|
+
const url = new URL(c.req.url);
|
|
16
|
+
let path = (0, filepath_1.getFilePath)({
|
|
17
|
+
filename: options.path ?? url.pathname,
|
|
18
|
+
root: options.root,
|
|
19
|
+
defaultDocument: DEFAULT_DOCUMENT,
|
|
20
|
+
});
|
|
21
|
+
path = `./${path}`;
|
|
22
|
+
const content = file(path);
|
|
23
|
+
if (content) {
|
|
24
|
+
const mimeType = (0, mime_1.getMimeType)(path);
|
|
25
|
+
if (mimeType) {
|
|
26
|
+
c.header('Content-Type', mimeType);
|
|
27
|
+
}
|
|
28
|
+
// Return Response object
|
|
29
|
+
return c.body(content);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
console.warn(`Static file: ${path} is not found`);
|
|
33
|
+
await next();
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
exports.serveStatic = serveStatic;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import type { ServeStaticOptions } from './serve-static';
|
|
2
|
-
declare const module: (options?: ServeStaticOptions) => import("../../hono").Handler<string,
|
|
2
|
+
declare const module: (options?: ServeStaticOptions) => import("../../hono").Handler<string, {
|
|
3
|
+
[x: string]: any;
|
|
4
|
+
}>;
|
|
3
5
|
export { module as serveStatic };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import type { Env } from '../../context';
|
|
3
2
|
import type { Handler } from '../../hono';
|
|
4
3
|
export declare type ServeStaticOptions = {
|
|
5
4
|
root?: string;
|
|
@@ -7,4 +6,4 @@ export declare type ServeStaticOptions = {
|
|
|
7
6
|
manifest?: object | string;
|
|
8
7
|
namespace?: KVNamespace;
|
|
9
8
|
};
|
|
10
|
-
export declare const serveStatic: (options?: ServeStaticOptions) => Handler
|
|
9
|
+
export declare const serveStatic: (options?: ServeStaticOptions) => Handler;
|
package/dist/request.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Cookie } from './utils/cookie';
|
|
1
2
|
declare global {
|
|
2
3
|
interface Request<ParamKeyType extends string = string> {
|
|
3
4
|
param: {
|
|
@@ -17,6 +18,14 @@ declare global {
|
|
|
17
18
|
(name: string): string;
|
|
18
19
|
(): Record<string, string>;
|
|
19
20
|
};
|
|
21
|
+
cookie: {
|
|
22
|
+
(name: string): string;
|
|
23
|
+
(): Cookie;
|
|
24
|
+
};
|
|
25
|
+
parsedBody?: Promise<any>;
|
|
26
|
+
parseBody: {
|
|
27
|
+
(): Promise<any>;
|
|
28
|
+
};
|
|
20
29
|
}
|
|
21
30
|
}
|
|
22
31
|
export declare function extendRequestPrototype(): void;
|
package/dist/request.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.extendRequestPrototype = void 0;
|
|
4
|
+
const body_1 = require("./utils/body");
|
|
5
|
+
const cookie_1 = require("./utils/cookie");
|
|
4
6
|
function extendRequestPrototype() {
|
|
5
7
|
if (!!Request.prototype.param) {
|
|
6
8
|
// already extended
|
|
@@ -55,5 +57,22 @@ function extendRequestPrototype() {
|
|
|
55
57
|
return result;
|
|
56
58
|
}
|
|
57
59
|
};
|
|
60
|
+
Request.prototype.cookie = function (key) {
|
|
61
|
+
const cookie = this.headers.get('Cookie') || '';
|
|
62
|
+
const obj = (0, cookie_1.parse)(cookie);
|
|
63
|
+
if (key) {
|
|
64
|
+
const value = obj[key];
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return obj;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
Request.prototype.parseBody = function () {
|
|
72
|
+
if (!this.parsedBody) {
|
|
73
|
+
this.parsedBody = (0, body_1.parseBody)(this);
|
|
74
|
+
}
|
|
75
|
+
return this.parsedBody;
|
|
76
|
+
};
|
|
58
77
|
}
|
|
59
78
|
exports.extendRequestPrototype = extendRequestPrototype;
|
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
|
|
2
|
+
export declare const timingSafeEqual: (a: string | object | boolean, b: string | object | boolean, hashFunction?: Function) => Promise<boolean>;
|
|
3
3
|
export declare const bufferToString: (buffer: ArrayBuffer) => string;
|
|
@@ -3,4 +3,4 @@ export declare type KVAssetOptions = {
|
|
|
3
3
|
manifest?: object | string;
|
|
4
4
|
namespace?: KVNamespace;
|
|
5
5
|
};
|
|
6
|
-
export declare const getContentFromKVAsset: (path: string, options?: KVAssetOptions
|
|
6
|
+
export declare const getContentFromKVAsset: (path: string, options?: KVAssetOptions) => Promise<ArrayBuffer | null>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare type Cookie = Record<string, string>;
|
|
2
|
+
export declare type CookieOptions = {
|
|
3
|
+
domain?: string;
|
|
4
|
+
expires?: Date;
|
|
5
|
+
httpOnly?: boolean;
|
|
6
|
+
maxAge?: number;
|
|
7
|
+
path?: string;
|
|
8
|
+
secure?: boolean;
|
|
9
|
+
signed?: boolean;
|
|
10
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
11
|
+
};
|
|
12
|
+
export declare const parse: (cookie: string) => Cookie;
|
|
13
|
+
export declare const serialize: (name: string, value: string, opt?: CookieOptions) => string;
|
|
@@ -1,27 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const cookie = () => {
|
|
5
|
-
return async (c, next) => {
|
|
6
|
-
c.req.cookie = ((name) => {
|
|
7
|
-
const cookie = c.req.headers.get('Cookie') || '';
|
|
8
|
-
const obj = parse(cookie);
|
|
9
|
-
if (name) {
|
|
10
|
-
const value = obj[name];
|
|
11
|
-
return value;
|
|
12
|
-
}
|
|
13
|
-
else {
|
|
14
|
-
return obj;
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
c.cookie = (name, value, opt) => {
|
|
18
|
-
const cookie = serialize(name, value, opt);
|
|
19
|
-
c.header('Set-Cookie', cookie);
|
|
20
|
-
};
|
|
21
|
-
await next();
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
exports.cookie = cookie;
|
|
3
|
+
exports.serialize = exports.parse = void 0;
|
|
25
4
|
const parse = (cookie) => {
|
|
26
5
|
const pairs = cookie.split(/;\s*/g);
|
|
27
6
|
const parsedCookie = {};
|
|
@@ -31,6 +10,7 @@ const parse = (cookie) => {
|
|
|
31
10
|
}
|
|
32
11
|
return parsedCookie;
|
|
33
12
|
};
|
|
13
|
+
exports.parse = parse;
|
|
34
14
|
const serialize = (name, value, opt = {}) => {
|
|
35
15
|
value = encodeURIComponent(value);
|
|
36
16
|
let cookie = `${name}=${value}`;
|
|
@@ -57,3 +37,4 @@ const serialize = (name, value, opt = {}) => {
|
|
|
57
37
|
}
|
|
58
38
|
return cookie;
|
|
59
39
|
};
|
|
40
|
+
exports.serialize = serialize;
|
package/dist/utils/jwt/jwt.js
CHANGED
|
@@ -46,10 +46,13 @@ const param = (name) => {
|
|
|
46
46
|
},
|
|
47
47
|
};
|
|
48
48
|
default:
|
|
49
|
-
throw new types_2.
|
|
49
|
+
throw new types_2.JwtAlgorithmNotImplemented(name);
|
|
50
50
|
}
|
|
51
51
|
};
|
|
52
52
|
const signing = async (data, secret, alg = types_1.AlgorithmTypes.HS256) => {
|
|
53
|
+
if (!crypto.subtle || !crypto.subtle.importKey) {
|
|
54
|
+
throw new Error('`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.');
|
|
55
|
+
}
|
|
53
56
|
const cryptoKey = await crypto.subtle.importKey(CryptoKeyFormat.RAW, (0, encode_1.utf8ToUint8Array)(secret), param(alg), false, [CryptoKeyUsage.Sign]);
|
|
54
57
|
return await crypto.subtle.sign(param(alg), cryptoKey, (0, encode_1.utf8ToUint8Array)(data));
|
|
55
58
|
};
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
export declare class
|
|
1
|
+
export declare class JwtAlgorithmNotImplemented extends Error {
|
|
2
2
|
constructor(token: string);
|
|
3
3
|
}
|
|
4
|
+
/**
|
|
5
|
+
* Export for backward compatibility
|
|
6
|
+
* @deprecated Use JwtAlgorithmNotImplemented instead
|
|
7
|
+
**/
|
|
8
|
+
export declare const JwtAlorithmNotImplemented: typeof JwtAlgorithmNotImplemented;
|
|
4
9
|
export declare class JwtTokenInvalid extends Error {
|
|
5
10
|
constructor(token: string);
|
|
6
11
|
}
|