hono 2.0.0 → 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/dist/compose.d.ts +1 -5
- package/dist/compose.js +2 -2
- package/dist/context.d.ts +6 -5
- package/dist/context.js +17 -6
- package/dist/hono.d.ts +1 -1
- package/dist/hono.js +2 -2
- package/dist/middleware/compress/index.d.ts +3 -2
- package/dist/middleware/cors/index.d.ts +1 -1
- package/dist/middleware/logger/index.d.ts +3 -5
- package/dist/middleware/logger/index.js +16 -17
- package/dist/utils/buffer.d.ts +1 -1
- package/dist/utils/cloudflare.d.ts +1 -1
- package/dist/utils/jwt/jwt.js +1 -1
- package/dist/utils/jwt/types.d.ts +6 -1
- package/dist/utils/jwt/types.js +9 -4
- package/package.json +2 -2
package/dist/compose.d.ts
CHANGED
|
@@ -1,6 +1,2 @@
|
|
|
1
1
|
import type { ErrorHandler, NotFoundHandler } from './hono';
|
|
2
|
-
export declare const compose: <C>(middleware: Function[], onError?: ErrorHandler<
|
|
3
|
-
[x: string]: any;
|
|
4
|
-
}> | undefined, onNotFound?: NotFoundHandler<{
|
|
5
|
-
[x: string]: any;
|
|
6
|
-
}> | undefined) => (context: C, next?: Function | undefined) => Promise<C>;
|
|
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) {
|
|
@@ -22,7 +22,7 @@ const compose = (middleware, onError, onNotFound) => {
|
|
|
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
27
|
if (res && context instanceof context_1.HonoContext) {
|
|
28
28
|
context.res = res;
|
package/dist/context.d.ts
CHANGED
|
@@ -8,8 +8,8 @@ declare type Env = Record<string, any>;
|
|
|
8
8
|
export interface Context<RequestParamKeyType extends string = string, E = Env> {
|
|
9
9
|
req: Request<RequestParamKeyType>;
|
|
10
10
|
env: E;
|
|
11
|
-
event: FetchEvent
|
|
12
|
-
executionCtx: ExecutionContext
|
|
11
|
+
event: FetchEvent;
|
|
12
|
+
executionCtx: ExecutionContext;
|
|
13
13
|
finalized: boolean;
|
|
14
14
|
get res(): Response;
|
|
15
15
|
set res(_res: Response);
|
|
@@ -30,17 +30,18 @@ export interface Context<RequestParamKeyType extends string = string, E = Env> {
|
|
|
30
30
|
export declare class HonoContext<RequestParamKeyType extends string = string, E = Env> implements Context<RequestParamKeyType, E> {
|
|
31
31
|
req: Request<RequestParamKeyType>;
|
|
32
32
|
env: E;
|
|
33
|
-
event: FetchEvent | undefined;
|
|
34
|
-
executionCtx: ExecutionContext | undefined;
|
|
35
33
|
finalized: boolean;
|
|
36
34
|
_status: StatusCode;
|
|
35
|
+
private _executionCtx;
|
|
37
36
|
private _pretty;
|
|
38
37
|
private _prettySpace;
|
|
39
38
|
private _map;
|
|
40
39
|
private _headers;
|
|
41
40
|
private _res;
|
|
42
41
|
private notFoundHandler;
|
|
43
|
-
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;
|
|
44
45
|
get res(): Response;
|
|
45
46
|
set res(_res: Response);
|
|
46
47
|
header(name: string, value: string): void;
|
package/dist/context.js
CHANGED
|
@@ -4,20 +4,31 @@ exports.HonoContext = void 0;
|
|
|
4
4
|
const cookie_1 = require("./utils/cookie");
|
|
5
5
|
const url_1 = require("./utils/url");
|
|
6
6
|
class HonoContext {
|
|
7
|
-
constructor(req, env = undefined,
|
|
7
|
+
constructor(req, env = undefined, executionCtx = undefined, notFoundHandler = () => new Response()) {
|
|
8
8
|
this._status = 200;
|
|
9
9
|
this._pretty = false;
|
|
10
10
|
this._prettySpace = 2;
|
|
11
|
+
this._executionCtx = executionCtx;
|
|
11
12
|
this.req = req;
|
|
12
13
|
this.env = env ? env : {};
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
this.notFoundHandler = notFoundHandler;
|
|
15
|
+
this.finalized = false;
|
|
16
|
+
}
|
|
17
|
+
get event() {
|
|
18
|
+
if (this._executionCtx instanceof FetchEvent) {
|
|
19
|
+
return this._executionCtx;
|
|
15
20
|
}
|
|
16
21
|
else {
|
|
17
|
-
|
|
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');
|
|
18
31
|
}
|
|
19
|
-
this.notFoundHandler = notFoundHandler;
|
|
20
|
-
this.finalized = false;
|
|
21
32
|
}
|
|
22
33
|
get res() {
|
|
23
34
|
return (this._res || (this._res = new Response()));
|
package/dist/hono.d.ts
CHANGED
|
@@ -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)();
|
|
@@ -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) {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { Context } from '../../context';
|
|
2
2
|
import type { Next } from '../../hono';
|
|
3
|
+
declare type EncodingType = 'gzip' | 'deflate';
|
|
3
4
|
interface CompressionOptions {
|
|
4
|
-
encoding?:
|
|
5
|
+
encoding?: EncodingType;
|
|
5
6
|
}
|
|
6
|
-
export declare const compress: (options?: CompressionOptions
|
|
7
|
+
export declare const compress: (options?: CompressionOptions) => (ctx: Context, next: Next) => Promise<void>;
|
|
7
8
|
export {};
|
|
@@ -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 {};
|
|
@@ -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);
|
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>;
|
package/dist/utils/jwt/jwt.js
CHANGED
|
@@ -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
|
}
|
package/dist/utils/jwt/types.js
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AlgorithmTypes = exports.JwtTokenSignatureMismatched = exports.JwtTokenExpired = exports.JwtTokenNotBefore = exports.JwtTokenInvalid = exports.JwtAlorithmNotImplemented = void 0;
|
|
4
|
-
class
|
|
3
|
+
exports.AlgorithmTypes = exports.JwtTokenSignatureMismatched = exports.JwtTokenExpired = exports.JwtTokenNotBefore = exports.JwtTokenInvalid = exports.JwtAlorithmNotImplemented = exports.JwtAlgorithmNotImplemented = void 0;
|
|
4
|
+
class JwtAlgorithmNotImplemented extends Error {
|
|
5
5
|
constructor(token) {
|
|
6
6
|
super(`invalid JWT token: ${token}`);
|
|
7
|
-
this.name = '
|
|
7
|
+
this.name = 'JwtAlgorithmNotImplemented';
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
-
exports.
|
|
10
|
+
exports.JwtAlgorithmNotImplemented = JwtAlgorithmNotImplemented;
|
|
11
|
+
/**
|
|
12
|
+
* Export for backward compatibility
|
|
13
|
+
* @deprecated Use JwtAlgorithmNotImplemented instead
|
|
14
|
+
**/
|
|
15
|
+
exports.JwtAlorithmNotImplemented = JwtAlgorithmNotImplemented;
|
|
11
16
|
class JwtTokenInvalid extends Error {
|
|
12
17
|
constructor(token) {
|
|
13
18
|
super(`invalid JWT token: ${token}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Ultrafast web framework for Cloudflare Workers.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"scripts": {
|
|
11
11
|
"test": "jest",
|
|
12
12
|
"test:deno": "deno test --allow-read deno_test",
|
|
13
|
-
"test:bun": "bun
|
|
13
|
+
"test:bun": "bun wiptest bun_test/index.test.ts",
|
|
14
14
|
"lint": "eslint --ext js,ts src .eslintrc.js",
|
|
15
15
|
"lint:fix": "eslint --ext js,ts src .eslintrc.js --fix",
|
|
16
16
|
"denoify": "rimraf deno_dist && denoify && rimraf 'deno_dist/**/*.test.ts'",
|