hono 0.3.4 → 0.3.8
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 +6 -28
- package/dist/compose.d.ts +1 -1
- package/dist/compose.js +1 -1
- package/dist/context.d.ts +6 -6
- package/dist/context.js +19 -9
- package/dist/hono.d.ts +25 -13
- package/dist/hono.js +14 -22
- package/dist/middleware/mustache/mustache.d.ts +5 -1
- package/dist/middleware/mustache/mustache.js +15 -7
- package/dist/middleware/serve-static/serve-static.js +1 -19
- package/dist/node.d.ts +1 -1
- package/dist/utils/buffer.d.ts +2 -2
- package/dist/utils/cloudflare.d.ts +7 -0
- package/dist/utils/cloudflare.js +23 -1
- package/dist/utils/http-status.d.ts +1 -0
- package/dist/utils/http-status.js +46 -0
- package/package.json +4 -2
- package/dist/middleware/default.d.ts +0 -2
- package/dist/middleware/default.js +0 -15
- package/dist/middleware.d.ts +0 -3
- package/dist/middleware.js +0 -8
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Hono
|
|
2
2
|
|
|
3
|
-
Hono[炎] - _**means flame🔥 in Japanese**_ - is small, simple, and ultrafast web
|
|
3
|
+
Hono[炎] - _**means flame🔥 in Japanese**_ - is small, simple, and ultrafast web framework for a Service Workers API based serverless such as Cloudflare Workers and Fastly Compute@Edge.
|
|
4
4
|
|
|
5
5
|
```js
|
|
6
6
|
import { Hono } from 'hono'
|
|
@@ -13,7 +13,7 @@ app.fire()
|
|
|
13
13
|
|
|
14
14
|
## Features
|
|
15
15
|
|
|
16
|
-
- **Ultra fast** - the router is implemented with Trie-Tree structure.
|
|
16
|
+
- **Ultra fast** - the router is implemented with Trie-Tree structure. Not use loops.
|
|
17
17
|
- **Zero dependencies** - using only Web standard API.
|
|
18
18
|
- **Middleware** - builtin middleware, and you can make your own middleware.
|
|
19
19
|
- **Optimized** - for Cloudflare Workers.
|
|
@@ -174,7 +174,7 @@ app.use('*', async (c, next) => {
|
|
|
174
174
|
// Add a custom header
|
|
175
175
|
app.use('/message/*', async (c, next) => {
|
|
176
176
|
await next()
|
|
177
|
-
await c.
|
|
177
|
+
await c.header('x-message', 'This is middleware!')
|
|
178
178
|
})
|
|
179
179
|
|
|
180
180
|
app.get('/message/hello', (c) => c.text('Hello Middleware!'))
|
|
@@ -193,30 +193,9 @@ app.use('*', async (c, next) => {
|
|
|
193
193
|
})
|
|
194
194
|
```
|
|
195
195
|
|
|
196
|
-
### Complex Pattern
|
|
197
|
-
|
|
198
|
-
You can also do this:
|
|
199
|
-
|
|
200
|
-
```js
|
|
201
|
-
// Output response time
|
|
202
|
-
app.use('*', async (c, next) => {
|
|
203
|
-
await next()
|
|
204
|
-
const responseTime = await c.res.headers.get('X-Response-Time')
|
|
205
|
-
console.log(`X-Response-Time: ${responseTime}`)
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
// Add X-Response-Time header
|
|
209
|
-
app.use('*', async (c, next) => {
|
|
210
|
-
const start = Date.now()
|
|
211
|
-
await next()
|
|
212
|
-
const ms = Date.now() - start
|
|
213
|
-
await c.res.headers.append('X-Response-Time', `${ms}ms`)
|
|
214
|
-
})
|
|
215
|
-
```
|
|
216
|
-
|
|
217
196
|
## Context
|
|
218
197
|
|
|
219
|
-
To handle Request and Reponse
|
|
198
|
+
To handle Request and Reponse, you can use Context object:
|
|
220
199
|
|
|
221
200
|
### c.req
|
|
222
201
|
|
|
@@ -253,7 +232,6 @@ app.get('/welcome', (c) => {
|
|
|
253
232
|
c.header('X-Message', 'Hello!')
|
|
254
233
|
c.header('Content-Type', 'text/plain')
|
|
255
234
|
c.status(201)
|
|
256
|
-
c.statusText('201 Content Created')
|
|
257
235
|
|
|
258
236
|
return c.body('Thank you for comming')
|
|
259
237
|
|
|
@@ -261,7 +239,7 @@ app.get('/welcome', (c) => {
|
|
|
261
239
|
Same as:
|
|
262
240
|
return new Response('Thank you for comming', {
|
|
263
241
|
status: 201,
|
|
264
|
-
statusText: '
|
|
242
|
+
statusText: 'Created',
|
|
265
243
|
headers: {
|
|
266
244
|
'X-Message': 'Hello',
|
|
267
245
|
'Content-Type': 'text/plain',
|
|
@@ -431,7 +409,7 @@ Run the development server locally. Then, access like `http://127.0.0.1:8787/` i
|
|
|
431
409
|
wrangler dev
|
|
432
410
|
```
|
|
433
411
|
|
|
434
|
-
### Publish
|
|
412
|
+
### 7. Publish
|
|
435
413
|
|
|
436
414
|
Deploy to Cloudflare. That's all!
|
|
437
415
|
|
package/dist/compose.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const compose: (middleware:
|
|
1
|
+
export declare const compose: <T>(middleware: Function[]) => (context: T, next?: Function) => Promise<void | object>;
|
package/dist/compose.js
CHANGED
|
@@ -7,7 +7,7 @@ const compose = (middleware) => {
|
|
|
7
7
|
return function (context, next) {
|
|
8
8
|
let index = -1;
|
|
9
9
|
return dispatch(0);
|
|
10
|
-
function dispatch(i) {
|
|
10
|
+
async function dispatch(i) {
|
|
11
11
|
if (i <= index)
|
|
12
12
|
return Promise.reject(new Error('next() called multiple times'));
|
|
13
13
|
index = i;
|
package/dist/context.d.ts
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
declare type Headers = {
|
|
3
3
|
[key: string]: string;
|
|
4
4
|
};
|
|
5
|
+
declare type Data = string | ArrayBuffer | ReadableStream;
|
|
5
6
|
export interface Env {
|
|
6
7
|
}
|
|
7
|
-
export declare class Context {
|
|
8
|
-
req: Request
|
|
8
|
+
export declare class Context<RequestParamKeyType = string> {
|
|
9
|
+
req: Request<RequestParamKeyType>;
|
|
9
10
|
res: Response;
|
|
10
11
|
env: Env;
|
|
11
12
|
event: FetchEvent;
|
|
@@ -13,16 +14,15 @@ export declare class Context {
|
|
|
13
14
|
private _status;
|
|
14
15
|
private _statusText;
|
|
15
16
|
render: (template: string, params?: object, options?: object) => Promise<Response>;
|
|
16
|
-
constructor(req: Request
|
|
17
|
+
constructor(req: Request<RequestParamKeyType>, opts?: {
|
|
17
18
|
res: Response;
|
|
18
19
|
env: Env;
|
|
19
20
|
event: FetchEvent;
|
|
20
21
|
});
|
|
21
22
|
header(name: string, value: string): void;
|
|
22
23
|
status(number: number): void;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
body(data: any, status?: number, headers?: Headers): Response;
|
|
24
|
+
newResponse(data: Data, init?: ResponseInit): Response;
|
|
25
|
+
body(data: Data, status?: number, headers?: Headers): Response;
|
|
26
26
|
text(text: string, status?: number, headers?: Headers): Response;
|
|
27
27
|
json(object: object, status?: number, headers?: Headers): Response;
|
|
28
28
|
html(html: string, status?: number, headers?: Headers): Response;
|
package/dist/context.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Context = void 0;
|
|
4
4
|
const url_1 = require("./utils/url");
|
|
5
|
+
const http_status_1 = require("./utils/http-status");
|
|
5
6
|
class Context {
|
|
6
7
|
constructor(req, opts) {
|
|
7
8
|
this.req = req;
|
|
@@ -13,13 +14,25 @@ class Context {
|
|
|
13
14
|
this._headers = {};
|
|
14
15
|
}
|
|
15
16
|
header(name, value) {
|
|
17
|
+
/*
|
|
18
|
+
XXX:
|
|
19
|
+
app.use('*', (c, next) => {
|
|
20
|
+
next()
|
|
21
|
+
c.header('foo', 'bar') // => c.res.headers.set(...)
|
|
22
|
+
})
|
|
23
|
+
*/
|
|
24
|
+
if (this.res) {
|
|
25
|
+
this.res.headers.set(name, value);
|
|
26
|
+
}
|
|
16
27
|
this._headers[name] = value;
|
|
17
28
|
}
|
|
18
29
|
status(number) {
|
|
30
|
+
if (this.res) {
|
|
31
|
+
console.warn('c.res.status is already set.');
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
19
34
|
this._status = number;
|
|
20
|
-
|
|
21
|
-
statusText(text) {
|
|
22
|
-
this._statusText = text;
|
|
35
|
+
this._statusText = (0, http_status_1.getStatusText)(number);
|
|
23
36
|
}
|
|
24
37
|
newResponse(data, init = {}) {
|
|
25
38
|
init.status = init.status || this._status;
|
|
@@ -35,9 +48,6 @@ class Context {
|
|
|
35
48
|
const Encoder = new TextEncoder();
|
|
36
49
|
length = Encoder.encode(data).byteLength || 0;
|
|
37
50
|
}
|
|
38
|
-
else {
|
|
39
|
-
length = data.bytelength;
|
|
40
|
-
}
|
|
41
51
|
}
|
|
42
52
|
init.headers = Object.assign(Object.assign({}, init.headers), { 'Content-Length': length.toString() });
|
|
43
53
|
return new Response(data, init);
|
|
@@ -52,7 +62,7 @@ class Context {
|
|
|
52
62
|
if (typeof text !== 'string') {
|
|
53
63
|
throw new TypeError('text method arg must be a string!');
|
|
54
64
|
}
|
|
55
|
-
headers['Content-Type'] = 'text/plain';
|
|
65
|
+
headers['Content-Type'] || (headers['Content-Type'] = 'text/plain; charset=UTF-8');
|
|
56
66
|
return this.body(text, status, headers);
|
|
57
67
|
}
|
|
58
68
|
json(object, status = this._status, headers = {}) {
|
|
@@ -60,14 +70,14 @@ class Context {
|
|
|
60
70
|
throw new TypeError('json method arg must be a object!');
|
|
61
71
|
}
|
|
62
72
|
const body = JSON.stringify(object);
|
|
63
|
-
headers['Content-Type'] = 'application/json; charset=UTF-8';
|
|
73
|
+
headers['Content-Type'] || (headers['Content-Type'] = 'application/json; charset=UTF-8');
|
|
64
74
|
return this.body(body, status, headers);
|
|
65
75
|
}
|
|
66
76
|
html(html, status = this._status, headers = {}) {
|
|
67
77
|
if (typeof html !== 'string') {
|
|
68
78
|
throw new TypeError('html method arg must be a string!');
|
|
69
79
|
}
|
|
70
|
-
headers['Content-Type'] = 'text/html; charset=UTF-8';
|
|
80
|
+
headers['Content-Type'] || (headers['Content-Type'] = 'text/html; charset=UTF-8');
|
|
71
81
|
return this.body(html, status, headers);
|
|
72
82
|
}
|
|
73
83
|
redirect(location, status = 302) {
|
package/dist/hono.d.ts
CHANGED
|
@@ -4,15 +4,18 @@ import { Node } from './node';
|
|
|
4
4
|
import { Context } from './context';
|
|
5
5
|
import type { Env } from './context';
|
|
6
6
|
declare global {
|
|
7
|
-
interface Request {
|
|
8
|
-
param: (key:
|
|
9
|
-
query: (key: string) => string
|
|
7
|
+
interface Request<ParamKeyType = string> {
|
|
8
|
+
param: (key: ParamKeyType) => string;
|
|
9
|
+
query: (key: string) => string;
|
|
10
10
|
header: (name: string) => string;
|
|
11
11
|
parsedBody: any;
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
-
export declare type Handler = (c: Context
|
|
14
|
+
export declare type Handler<RequestParamKeyType = string> = (c: Context<RequestParamKeyType>, next?: Function) => Response | Promise<Response>;
|
|
15
15
|
export declare type MiddlewareHandler = (c: Context, next: Function) => Promise<void>;
|
|
16
|
+
declare type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}` ? Name : NameWithPattern;
|
|
17
|
+
declare type ParamKey<Component> = Component extends `:${infer NameWithPattern}` ? ParamKeyName<NameWithPattern> : never;
|
|
18
|
+
declare type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}` ? ParamKey<Component> | ParamKeys<Rest> : ParamKey<Path>;
|
|
16
19
|
export declare class Router<T> {
|
|
17
20
|
node: Node<T>;
|
|
18
21
|
constructor();
|
|
@@ -24,14 +27,22 @@ export declare class Hono {
|
|
|
24
27
|
middlewareRouters: Router<MiddlewareHandler>[];
|
|
25
28
|
tempPath: string;
|
|
26
29
|
constructor();
|
|
27
|
-
get(arg:
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
get<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
31
|
+
get(arg: Handler<never>, ...args: Handler<never>[]): Hono;
|
|
32
|
+
post<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
33
|
+
post(arg: Handler, ...args: Handler[]): Hono;
|
|
34
|
+
put<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
35
|
+
put(arg: Handler, ...args: Handler[]): Hono;
|
|
36
|
+
head<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
37
|
+
head(arg: Handler, ...args: Handler[]): Hono;
|
|
38
|
+
delete<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
39
|
+
delete(arg: Handler, ...args: Handler[]): Hono;
|
|
40
|
+
options<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
41
|
+
options(arg: Handler, ...args: Handler[]): Hono;
|
|
42
|
+
patch<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
43
|
+
patch(arg: Handler, ...args: Handler[]): Hono;
|
|
44
|
+
all<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono;
|
|
45
|
+
all(arg: Handler<never>, ...args: Handler<never>[]): Hono;
|
|
35
46
|
route(path: string): Hono;
|
|
36
47
|
use(path: string, middleware: MiddlewareHandler): void;
|
|
37
48
|
addRoute(method: string, arg: string | Handler, ...args: Handler[]): Hono;
|
|
@@ -40,6 +51,7 @@ export declare class Hono {
|
|
|
40
51
|
handleEvent(event: FetchEvent): Promise<Response>;
|
|
41
52
|
fetch(request: Request, env?: Env, event?: FetchEvent): Promise<Response>;
|
|
42
53
|
fire(): void;
|
|
43
|
-
onError(err:
|
|
54
|
+
onError(err: Error): Response;
|
|
44
55
|
notFound(): Response;
|
|
45
56
|
}
|
|
57
|
+
export {};
|
package/dist/hono.js
CHANGED
|
@@ -4,7 +4,6 @@ exports.Hono = exports.Router = void 0;
|
|
|
4
4
|
const node_1 = require("./node");
|
|
5
5
|
const compose_1 = require("./compose");
|
|
6
6
|
const url_1 = require("./utils/url");
|
|
7
|
-
const middleware_1 = require("./middleware");
|
|
8
7
|
const context_1 = require("./context");
|
|
9
8
|
const METHOD_NAME_OF_ALL = 'ALL';
|
|
10
9
|
class Router {
|
|
@@ -25,7 +24,6 @@ class Hono {
|
|
|
25
24
|
this.middlewareRouters = [];
|
|
26
25
|
this.tempPath = '/';
|
|
27
26
|
}
|
|
28
|
-
/* HTTP METHODS */
|
|
29
27
|
get(arg, ...args) {
|
|
30
28
|
return this.addRoute('get', arg, ...args);
|
|
31
29
|
}
|
|
@@ -47,22 +45,6 @@ class Hono {
|
|
|
47
45
|
patch(arg, ...args) {
|
|
48
46
|
return this.addRoute('patch', arg, ...args);
|
|
49
47
|
}
|
|
50
|
-
/*
|
|
51
|
-
We may implement these HTTP methods:
|
|
52
|
-
trace
|
|
53
|
-
copy
|
|
54
|
-
lock
|
|
55
|
-
purge
|
|
56
|
-
unlock
|
|
57
|
-
report
|
|
58
|
-
checkout
|
|
59
|
-
merge
|
|
60
|
-
notify
|
|
61
|
-
subscribe
|
|
62
|
-
unsubscribe
|
|
63
|
-
search
|
|
64
|
-
connect
|
|
65
|
-
*/
|
|
66
48
|
all(arg, ...args) {
|
|
67
49
|
return this.addRoute('all', arg, ...args);
|
|
68
50
|
}
|
|
@@ -97,11 +79,18 @@ class Hono {
|
|
|
97
79
|
async dispatch(request, env, event) {
|
|
98
80
|
const [method, path] = [request.method, (0, url_1.getPathFromURL)(request.url)];
|
|
99
81
|
const result = await this.matchRoute(method, path);
|
|
82
|
+
// Methods for Request object
|
|
100
83
|
request.param = (key) => {
|
|
101
84
|
if (result) {
|
|
102
85
|
return result.params[key];
|
|
103
86
|
}
|
|
104
|
-
|
|
87
|
+
};
|
|
88
|
+
request.header = (name) => {
|
|
89
|
+
return request.headers.get(name);
|
|
90
|
+
};
|
|
91
|
+
request.query = (key) => {
|
|
92
|
+
const url = new URL(c.req.url);
|
|
93
|
+
return url.searchParams.get(key);
|
|
105
94
|
};
|
|
106
95
|
const handler = result ? result.handler[0] : this.notFound; // XXX
|
|
107
96
|
const middleware = [];
|
|
@@ -112,10 +101,13 @@ class Hono {
|
|
|
112
101
|
}
|
|
113
102
|
}
|
|
114
103
|
const wrappedHandler = async (context, next) => {
|
|
115
|
-
|
|
104
|
+
const res = await handler(context);
|
|
105
|
+
if (!(res instanceof Response)) {
|
|
106
|
+
throw new TypeError('response must be a instace of Response');
|
|
107
|
+
}
|
|
108
|
+
context.res = res;
|
|
116
109
|
await next();
|
|
117
110
|
};
|
|
118
|
-
middleware.push(middleware_1.Middleware.default);
|
|
119
111
|
middleware.push(wrappedHandler);
|
|
120
112
|
const composed = (0, compose_1.compose)(middleware);
|
|
121
113
|
const c = new context_1.Context(request, { env: env, event: event, res: null });
|
|
@@ -149,7 +141,7 @@ class Hono {
|
|
|
149
141
|
}
|
|
150
142
|
notFound() {
|
|
151
143
|
const message = 'Not Found';
|
|
152
|
-
return new Response(
|
|
144
|
+
return new Response(message, {
|
|
153
145
|
status: 404,
|
|
154
146
|
headers: {
|
|
155
147
|
'Content-Length': message.length.toString(),
|
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
import type { Context } from '../../context';
|
|
2
|
-
|
|
2
|
+
declare type Options = {
|
|
3
|
+
root: string;
|
|
4
|
+
};
|
|
5
|
+
export declare const mustache: (opt?: Options) => (c: Context, next: Function) => Promise<void>;
|
|
6
|
+
export {};
|
|
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.mustache = void 0;
|
|
4
4
|
const cloudflare_1 = require("../../utils/cloudflare");
|
|
5
5
|
const EXTENSION = '.mustache';
|
|
6
|
-
const
|
|
6
|
+
const DEFAULT_DOCUMENT = 'index.mustache';
|
|
7
|
+
const mustache = (opt = { root: '' }) => {
|
|
8
|
+
const { root } = opt;
|
|
7
9
|
return async (c, next) => {
|
|
8
10
|
let Mustache;
|
|
9
11
|
try {
|
|
@@ -12,24 +14,30 @@ const mustache = () => {
|
|
|
12
14
|
catch (_a) {
|
|
13
15
|
throw new Error('If you want to use Mustache Middleware, install "mustache" package first.');
|
|
14
16
|
}
|
|
15
|
-
c.render = async (filename,
|
|
16
|
-
const
|
|
17
|
+
c.render = async (filename, params = {}, options) => {
|
|
18
|
+
const path = (0, cloudflare_1.getKVFilePath)({ filename: `${filename}${EXTENSION}`, root: root, defaultDocument: DEFAULT_DOCUMENT });
|
|
19
|
+
const buffer = await (0, cloudflare_1.getContentFromKVAsset)(path);
|
|
17
20
|
if (!buffer) {
|
|
18
|
-
throw new Error(`Template "${
|
|
21
|
+
throw new Error(`Template "${path}" is not found or blank.`);
|
|
19
22
|
}
|
|
20
23
|
const content = bufferToString(buffer);
|
|
21
24
|
const partialArgs = {};
|
|
22
25
|
if (options) {
|
|
23
26
|
const partials = options;
|
|
24
27
|
for (const key of Object.keys(partials)) {
|
|
25
|
-
const
|
|
28
|
+
const partialPath = (0, cloudflare_1.getKVFilePath)({
|
|
29
|
+
filename: `${partials[key]}${EXTENSION}`,
|
|
30
|
+
root: root,
|
|
31
|
+
defaultDocument: DEFAULT_DOCUMENT,
|
|
32
|
+
});
|
|
33
|
+
const partialBuffer = await (0, cloudflare_1.getContentFromKVAsset)(partialPath);
|
|
26
34
|
if (!partialBuffer) {
|
|
27
|
-
throw new Error(`Partial Template "${
|
|
35
|
+
throw new Error(`Partial Template "${partialPath}" is not found or blank.`);
|
|
28
36
|
}
|
|
29
37
|
partialArgs[key] = bufferToString(partialBuffer);
|
|
30
38
|
}
|
|
31
39
|
}
|
|
32
|
-
const output = Mustache.render(content,
|
|
40
|
+
const output = Mustache.render(content, params, partialArgs);
|
|
33
41
|
return c.html(output);
|
|
34
42
|
};
|
|
35
43
|
await next();
|
|
@@ -9,7 +9,7 @@ const serveStatic = (opt = { root: '' }) => {
|
|
|
9
9
|
return async (c, next) => {
|
|
10
10
|
await next();
|
|
11
11
|
const url = new URL(c.req.url);
|
|
12
|
-
const path =
|
|
12
|
+
const path = (0, cloudflare_1.getKVFilePath)({ filename: url.pathname, root: opt.root, defaultDocument: DEFAULT_DOCUMENT });
|
|
13
13
|
const content = await (0, cloudflare_1.getContentFromKVAsset)(path);
|
|
14
14
|
if (content) {
|
|
15
15
|
const mimeType = (0, mime_1.getMimeType)(path);
|
|
@@ -24,21 +24,3 @@ const serveStatic = (opt = { root: '' }) => {
|
|
|
24
24
|
};
|
|
25
25
|
};
|
|
26
26
|
exports.serveStatic = serveStatic;
|
|
27
|
-
const getKVPath = (filename, root) => {
|
|
28
|
-
if (filename.endsWith('/')) {
|
|
29
|
-
// /top/ => /top/index.html
|
|
30
|
-
filename = filename.concat(DEFAULT_DOCUMENT);
|
|
31
|
-
}
|
|
32
|
-
else if (!(0, mime_1.getMimeType)(filename)) {
|
|
33
|
-
// /top => /top/index.html
|
|
34
|
-
filename = filename.concat('/' + DEFAULT_DOCUMENT);
|
|
35
|
-
}
|
|
36
|
-
// /foo.html => foo.html
|
|
37
|
-
filename = filename.replace(/^\//, '');
|
|
38
|
-
// assets/ => assets
|
|
39
|
-
root = root.replace(/\/$/, '');
|
|
40
|
-
// ./assets/foo.html => assets/foo.html
|
|
41
|
-
let path = root + '/' + filename;
|
|
42
|
-
path = path.replace(/^\.?\//, '');
|
|
43
|
-
return path;
|
|
44
|
-
};
|
package/dist/node.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export declare class Node<T> {
|
|
|
16
16
|
[key: string]: Node<T>;
|
|
17
17
|
};
|
|
18
18
|
middlewares: [];
|
|
19
|
-
constructor(method?: string, handler?:
|
|
19
|
+
constructor(method?: string, handler?: T, children?: {
|
|
20
20
|
[key: string]: Node<T>;
|
|
21
21
|
});
|
|
22
22
|
insert(method: string, path: string, handler: T): Node<T>;
|
package/dist/utils/buffer.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export declare const equal: (a: ArrayBuffer, b: ArrayBuffer) => boolean;
|
|
2
2
|
export declare const decodeBase64: (str: string) => any;
|
|
3
|
-
export declare const sha256: (a: string) => Promise<string>;
|
|
4
|
-
export declare const timingSafeEqual: (a:
|
|
3
|
+
export declare const sha256: (a: string | object | boolean) => Promise<string>;
|
|
4
|
+
export declare const timingSafeEqual: (a: string | object | boolean, b: string | object | boolean) => Promise<boolean>;
|
package/dist/utils/cloudflare.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getContentFromKVAsset = void 0;
|
|
3
|
+
exports.getKVFilePath = exports.getContentFromKVAsset = void 0;
|
|
4
4
|
const getContentFromKVAsset = async (path) => {
|
|
5
5
|
let ASSET_MANIFEST;
|
|
6
6
|
if (typeof __STATIC_CONTENT_MANIFEST === 'string') {
|
|
@@ -21,3 +21,25 @@ const getContentFromKVAsset = async (path) => {
|
|
|
21
21
|
return content;
|
|
22
22
|
};
|
|
23
23
|
exports.getContentFromKVAsset = getContentFromKVAsset;
|
|
24
|
+
const getKVFilePath = (opt) => {
|
|
25
|
+
let filename = opt.filename;
|
|
26
|
+
let root = opt.root || '';
|
|
27
|
+
const defaultDocument = opt.defaultDocument || 'index.html';
|
|
28
|
+
if (filename.endsWith('/')) {
|
|
29
|
+
// /top/ => /top/index.html
|
|
30
|
+
filename = filename.concat(defaultDocument);
|
|
31
|
+
}
|
|
32
|
+
else if (!filename.match(/\.[a-zA-Z0-9]+$/)) {
|
|
33
|
+
// /top => /top/index.html
|
|
34
|
+
filename = filename.concat('/' + defaultDocument);
|
|
35
|
+
}
|
|
36
|
+
// /foo.html => foo.html
|
|
37
|
+
filename = filename.replace(/^\//, '');
|
|
38
|
+
// assets/ => assets
|
|
39
|
+
root = root.replace(/\/$/, '');
|
|
40
|
+
// ./assets/foo.html => assets/foo.html
|
|
41
|
+
let path = root ? root + '/' + filename : filename;
|
|
42
|
+
path = path.replace(/^\.?\//, '');
|
|
43
|
+
return path;
|
|
44
|
+
};
|
|
45
|
+
exports.getKVFilePath = getKVFilePath;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getStatusText: (statusNumber: number) => string;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStatusText = void 0;
|
|
4
|
+
const getStatusText = (statusNumber) => {
|
|
5
|
+
const text = statuses[statusNumber];
|
|
6
|
+
return text;
|
|
7
|
+
};
|
|
8
|
+
exports.getStatusText = getStatusText;
|
|
9
|
+
const statuses = {
|
|
10
|
+
200: 'OK',
|
|
11
|
+
201: 'Created',
|
|
12
|
+
202: 'Accepted',
|
|
13
|
+
204: 'No Content',
|
|
14
|
+
206: 'Partial Content',
|
|
15
|
+
301: 'Moved Permanently',
|
|
16
|
+
302: 'Moved Temporarily',
|
|
17
|
+
303: 'See Other',
|
|
18
|
+
304: 'Not Modified',
|
|
19
|
+
307: 'Temporary Redirect',
|
|
20
|
+
308: 'Permanent Redirect',
|
|
21
|
+
400: 'Bad Request',
|
|
22
|
+
401: 'Unauthorized',
|
|
23
|
+
402: 'Payment Required',
|
|
24
|
+
403: 'Forbidden',
|
|
25
|
+
404: 'Not Found',
|
|
26
|
+
405: 'Not Allowed',
|
|
27
|
+
406: 'Not Acceptable',
|
|
28
|
+
408: 'Request Time-out',
|
|
29
|
+
409: 'Conflict',
|
|
30
|
+
410: 'Gone',
|
|
31
|
+
411: 'Length Required',
|
|
32
|
+
412: 'Precondition Failed',
|
|
33
|
+
413: 'Request Entity Too Large',
|
|
34
|
+
414: 'Request-URI Too Large',
|
|
35
|
+
415: 'Unsupported Media Type',
|
|
36
|
+
416: 'Requested Range Not Satisfiable',
|
|
37
|
+
421: 'Misdirected Request',
|
|
38
|
+
429: 'Too Many Requests',
|
|
39
|
+
500: 'Internal Server Error',
|
|
40
|
+
501: 'Not Implemented',
|
|
41
|
+
502: 'Bad Gateway',
|
|
42
|
+
503: 'Service Temporarily Unavailable',
|
|
43
|
+
504: 'Gateway Time-out',
|
|
44
|
+
505: 'HTTP Version Not Supported',
|
|
45
|
+
507: 'Insufficient Storage',
|
|
46
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.8",
|
|
4
4
|
"description": "[炎] Ultrafast web framework for Cloudflare Workers.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -16,7 +16,9 @@
|
|
|
16
16
|
"./logger": "./dist/middleware/logger/logger.js",
|
|
17
17
|
"./mustache": "./dist/middleware/mustache/mustache.js",
|
|
18
18
|
"./powered-by": "./dist/middleware/powered-by/powered-by.js",
|
|
19
|
-
"./serve-static": "./dist/middleware/serve-static/serve-static.js"
|
|
19
|
+
"./serve-static": "./dist/middleware/serve-static/serve-static.js",
|
|
20
|
+
"./utils/buffer": "./dist/utils/buffer.js",
|
|
21
|
+
"./package.json": "./package.json"
|
|
20
22
|
},
|
|
21
23
|
"typesVersions": {
|
|
22
24
|
"*": {
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.defaultMiddleware = void 0;
|
|
4
|
-
const defaultMiddleware = async (c, next) => {
|
|
5
|
-
c.req.query = (key) => {
|
|
6
|
-
// eslint-disable-next-line
|
|
7
|
-
const url = new URL(c.req.url);
|
|
8
|
-
return url.searchParams.get(key);
|
|
9
|
-
};
|
|
10
|
-
c.req.header = (name) => {
|
|
11
|
-
return c.req.headers.get(name);
|
|
12
|
-
};
|
|
13
|
-
await next();
|
|
14
|
-
};
|
|
15
|
-
exports.defaultMiddleware = defaultMiddleware;
|
package/dist/middleware.d.ts
DELETED
package/dist/middleware.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Middleware = void 0;
|
|
4
|
-
const default_1 = require("./middleware/default");
|
|
5
|
-
class Middleware {
|
|
6
|
-
}
|
|
7
|
-
exports.Middleware = Middleware;
|
|
8
|
-
Middleware.default = default_1.defaultMiddleware;
|