hono 1.3.3 → 1.3.6
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 +25 -7
- package/dist/compose.js +5 -2
- package/dist/context.d.ts +1 -2
- package/dist/context.js +20 -8
- package/dist/hono.d.ts +8 -0
- package/dist/hono.js +7 -1
- package/dist/middleware/mustache/index.d.ts +1 -7
- package/dist/middleware/mustache/index.js +2 -48
- package/dist/middleware/mustache/module.d.mts +3 -0
- package/dist/middleware/mustache/module.mjs +12 -0
- package/dist/middleware/mustache/mustache.d.ts +8 -0
- package/dist/middleware/mustache/mustache.js +52 -0
- package/dist/middleware/serve-static/index.d.ts +1 -7
- package/dist/middleware/serve-static/index.js +2 -32
- package/dist/middleware/serve-static/module.d.mts +3 -0
- package/dist/middleware/serve-static/module.mjs +12 -0
- package/dist/middleware/serve-static/serve-static.d.ts +8 -0
- package/dist/middleware/serve-static/serve-static.js +38 -0
- package/dist/utils/cloudflare.d.ts +8 -3
- package/dist/utils/cloudflare.js +26 -10
- package/dist/utils/crypto.js +0 -9
- package/dist/utils/encode.js +0 -26
- package/package.json +11 -3
package/README.md
CHANGED
|
@@ -137,15 +137,15 @@ npm install hono
|
|
|
137
137
|
|
|
138
138
|
An instance of `Hono` has these methods.
|
|
139
139
|
|
|
140
|
-
- app.**HTTP_METHOD**(\[
|
|
141
|
-
- app.**all**(\[
|
|
142
|
-
- app.**route**(
|
|
143
|
-
- app.**use**(\[
|
|
140
|
+
- app.**HTTP_METHOD**(\[パス,\]handler|middleware...)
|
|
141
|
+
- app.**all**(\[パス,\]handler|middleware...)
|
|
142
|
+
- app.**route**(パス, \[app\])
|
|
143
|
+
- app.**use**(\[パス,\]middleware)
|
|
144
144
|
- app.**notFound**(handler)
|
|
145
145
|
- app.**onError**(err, handler)
|
|
146
146
|
- app.**fire**()
|
|
147
147
|
- app.**fetch**(request, env, event)
|
|
148
|
-
- app.**request**(
|
|
148
|
+
- app.**request**(パス, options)
|
|
149
149
|
|
|
150
150
|
## Routing
|
|
151
151
|
|
|
@@ -354,6 +354,14 @@ app.get('/search', (c) => {
|
|
|
354
354
|
...
|
|
355
355
|
})
|
|
356
356
|
|
|
357
|
+
// Multiple query values
|
|
358
|
+
app.get('/search', (c) => {
|
|
359
|
+
const queries = c.req.queries('q')
|
|
360
|
+
// ---> GET search?q=foo&q=bar
|
|
361
|
+
// queries[0] => foo, queries[1] => bar
|
|
362
|
+
...
|
|
363
|
+
})
|
|
364
|
+
|
|
357
365
|
// Captured params
|
|
358
366
|
app.get('/entry/:id', (c) => {
|
|
359
367
|
const id = c.req.param('id')
|
|
@@ -510,6 +518,16 @@ test('GET /hello is ok', async () => {
|
|
|
510
518
|
})
|
|
511
519
|
```
|
|
512
520
|
|
|
521
|
+
## routerClass
|
|
522
|
+
|
|
523
|
+
The `routerClass` option specify which router is used inside. The default router is `TrieRouter`. If you want to use `RexExpRouter`, write like this:
|
|
524
|
+
|
|
525
|
+
```ts
|
|
526
|
+
import { RegExpRouter } from 'hono/router/reg-exp-router'
|
|
527
|
+
|
|
528
|
+
const app = new Hono({ routerClass: RegExpRouter })
|
|
529
|
+
```
|
|
530
|
+
|
|
513
531
|
## Cloudflare Workers with Hono
|
|
514
532
|
|
|
515
533
|
Using [Wrangler](https://developers.cloudflare.com/workers/cli-wrangler/), you can develop the application locally and publish it with few commands.
|
|
@@ -584,7 +602,7 @@ import { Hono } from 'hono'
|
|
|
584
602
|
import { cors } from 'hono/cors'
|
|
585
603
|
import { basicAuth } from 'hono/basic-auth'
|
|
586
604
|
import { prettyJSON } from 'hono/pretty-json'
|
|
587
|
-
import { getPosts,
|
|
605
|
+
import { getPosts, getPost, createPost, Post } from './model'
|
|
588
606
|
|
|
589
607
|
const app = new Hono()
|
|
590
608
|
app.get('/', (c) => c.text('Pretty Blog API'))
|
|
@@ -617,7 +635,7 @@ api.post(
|
|
|
617
635
|
await auth(c, next)
|
|
618
636
|
},
|
|
619
637
|
async (c) => {
|
|
620
|
-
const post = await c.req.json<
|
|
638
|
+
const post = await c.req.json<Post>()
|
|
621
639
|
const ok = createPost({ post })
|
|
622
640
|
return c.json({ ok })
|
|
623
641
|
}
|
package/dist/compose.js
CHANGED
|
@@ -16,8 +16,9 @@ const compose = (middleware, onError, onNotFound) => {
|
|
|
16
16
|
if (i === middleware.length)
|
|
17
17
|
handler = next;
|
|
18
18
|
if (handler === undefined) {
|
|
19
|
-
if (context instanceof context_1.Context && context.res ===
|
|
19
|
+
if (context instanceof context_1.Context && context.res.finalized === false) {
|
|
20
20
|
context.res = onNotFound(context);
|
|
21
|
+
context.res.finalized = true;
|
|
21
22
|
}
|
|
22
23
|
return Promise.resolve(context);
|
|
23
24
|
}
|
|
@@ -26,7 +27,8 @@ const compose = (middleware, onError, onNotFound) => {
|
|
|
26
27
|
// If handler return Response like `return c.text('foo')`
|
|
27
28
|
if (res && context instanceof context_1.Context) {
|
|
28
29
|
context.res = res;
|
|
29
|
-
|
|
30
|
+
context.res.finalized = true;
|
|
31
|
+
await dispatch(i + 1); // <--- Call next()
|
|
30
32
|
}
|
|
31
33
|
return context;
|
|
32
34
|
})
|
|
@@ -34,6 +36,7 @@ const compose = (middleware, onError, onNotFound) => {
|
|
|
34
36
|
if (onError && context instanceof context_1.Context) {
|
|
35
37
|
if (err instanceof Error) {
|
|
36
38
|
context.res = onError(err, context);
|
|
39
|
+
context.res.finalized = true;
|
|
37
40
|
}
|
|
38
41
|
return context;
|
|
39
42
|
}
|
package/dist/context.d.ts
CHANGED
|
@@ -10,16 +10,15 @@ export declare class Context<RequestParamKeyType extends string = string, E = En
|
|
|
10
10
|
event: FetchEvent;
|
|
11
11
|
private _headers;
|
|
12
12
|
private _status;
|
|
13
|
-
private _statusText;
|
|
14
13
|
private _pretty;
|
|
15
14
|
private _prettySpace;
|
|
16
15
|
private _map;
|
|
17
16
|
render: (template: string, params?: object, options?: object) => Promise<Response>;
|
|
18
17
|
notFound: () => Response | Promise<Response>;
|
|
19
18
|
constructor(req: Request<RequestParamKeyType>, opts?: {
|
|
20
|
-
res: Response;
|
|
21
19
|
env: E;
|
|
22
20
|
event: FetchEvent;
|
|
21
|
+
res?: Response;
|
|
23
22
|
});
|
|
24
23
|
private initRequest;
|
|
25
24
|
header(name: string, value: string): void;
|
package/dist/context.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Context = void 0;
|
|
4
|
-
const http_status_1 = require("./utils/http-status");
|
|
5
4
|
const url_1 = require("./utils/url");
|
|
6
5
|
class Context {
|
|
7
6
|
constructor(req, opts) {
|
|
8
|
-
this.res = undefined;
|
|
9
7
|
this._headers = {};
|
|
10
8
|
this._status = 200;
|
|
11
|
-
this._statusText = '';
|
|
12
9
|
this._pretty = false;
|
|
13
10
|
this._prettySpace = 2;
|
|
14
11
|
this.req = this.initRequest(req);
|
|
15
12
|
this._map = {};
|
|
16
13
|
Object.assign(this, opts);
|
|
14
|
+
if (!this.res) {
|
|
15
|
+
const res = new Response(null, { status: 404 });
|
|
16
|
+
res.finalized = false;
|
|
17
|
+
this.res = res;
|
|
18
|
+
}
|
|
17
19
|
}
|
|
18
20
|
initRequest(req) {
|
|
19
21
|
req.header = ((name) => {
|
|
@@ -35,8 +37,21 @@ class Context {
|
|
|
35
37
|
}
|
|
36
38
|
else {
|
|
37
39
|
const result = {};
|
|
38
|
-
for (const
|
|
39
|
-
result[key] =
|
|
40
|
+
for (const key of url.searchParams.keys()) {
|
|
41
|
+
result[key] = url.searchParams.get(key);
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
req.queries = ((key) => {
|
|
47
|
+
const url = new URL(req.url);
|
|
48
|
+
if (key) {
|
|
49
|
+
return url.searchParams.getAll(key);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
const result = {};
|
|
53
|
+
for (const key of url.searchParams.keys()) {
|
|
54
|
+
result[key] = url.searchParams.getAll(key);
|
|
40
55
|
}
|
|
41
56
|
return result;
|
|
42
57
|
}
|
|
@@ -51,7 +66,6 @@ class Context {
|
|
|
51
66
|
}
|
|
52
67
|
status(status) {
|
|
53
68
|
this._status = status;
|
|
54
|
-
this._statusText = (0, http_status_1.getStatusText)(status);
|
|
55
69
|
}
|
|
56
70
|
set(key, value) {
|
|
57
71
|
this._map[key] = value;
|
|
@@ -65,8 +79,6 @@ class Context {
|
|
|
65
79
|
}
|
|
66
80
|
newResponse(data, init = {}) {
|
|
67
81
|
init.status = init.status || this._status || 200;
|
|
68
|
-
init.statusText =
|
|
69
|
-
init.statusText || this._statusText || (0, http_status_1.getStatusText)(init.status);
|
|
70
82
|
init.headers = Object.assign(Object.assign({}, this._headers), init.headers);
|
|
71
83
|
return new Response(data, init);
|
|
72
84
|
}
|
package/dist/hono.d.ts
CHANGED
|
@@ -12,11 +12,18 @@ declare global {
|
|
|
12
12
|
(key: string): string;
|
|
13
13
|
(): Record<string, string>;
|
|
14
14
|
};
|
|
15
|
+
queries: {
|
|
16
|
+
(key: string): string[];
|
|
17
|
+
(): Record<string, string[]>;
|
|
18
|
+
};
|
|
15
19
|
header: {
|
|
16
20
|
(name: string): string;
|
|
17
21
|
(): Record<string, string>;
|
|
18
22
|
};
|
|
19
23
|
}
|
|
24
|
+
interface Response {
|
|
25
|
+
finalized: boolean;
|
|
26
|
+
}
|
|
20
27
|
}
|
|
21
28
|
export declare type Handler<RequestParamKeyType extends string = string, E = Env> = (c: Context<RequestParamKeyType, E>, next: Next) => Response | Promise<Response> | void | Promise<void>;
|
|
22
29
|
export declare type NotFoundHandler<E = Env> = (c: Context<string, E>) => Response;
|
|
@@ -54,6 +61,7 @@ export declare class Hono<E = Env, P extends string = '/'> extends Hono_base<E,
|
|
|
54
61
|
private _router;
|
|
55
62
|
private _tempPath;
|
|
56
63
|
private path;
|
|
64
|
+
private _cacheResponse;
|
|
57
65
|
routes: Route<E>[];
|
|
58
66
|
constructor(init?: Partial<Pick<Hono, 'routerClass' | 'strict'>>);
|
|
59
67
|
private notFoundHandler;
|
package/dist/hono.js
CHANGED
|
@@ -48,6 +48,8 @@ class Hono extends defineDynamicClass() {
|
|
|
48
48
|
Object.assign(this, init);
|
|
49
49
|
this._router = new this.routerClass();
|
|
50
50
|
this._tempPath = null;
|
|
51
|
+
this._cacheResponse = new Response(null, { status: 404 });
|
|
52
|
+
this._cacheResponse.finalized = false;
|
|
51
53
|
}
|
|
52
54
|
route(path, app) {
|
|
53
55
|
this._tempPath = path;
|
|
@@ -106,7 +108,11 @@ class Hono extends defineDynamicClass() {
|
|
|
106
108
|
}
|
|
107
109
|
});
|
|
108
110
|
const handlers = result ? result.handlers : [this.notFoundHandler];
|
|
109
|
-
const c = new context_1.Context(request, {
|
|
111
|
+
const c = new context_1.Context(request, {
|
|
112
|
+
env: env,
|
|
113
|
+
event: event,
|
|
114
|
+
res: this._cacheResponse,
|
|
115
|
+
});
|
|
110
116
|
c.notFound = () => this.notFoundHandler(c);
|
|
111
117
|
const composed = (0, compose_1.compose)(handlers, this.errorHandler, this.notFoundHandler);
|
|
112
118
|
let context;
|
|
@@ -1,7 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import type { Next } from '../../hono';
|
|
3
|
-
declare type Init = {
|
|
4
|
-
root: string;
|
|
5
|
-
};
|
|
6
|
-
export declare const mustache: (init?: Init) => (c: Context, next: Next) => Promise<void>;
|
|
7
|
-
export {};
|
|
1
|
+
export { mustache } from './mustache';
|
|
@@ -1,51 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.mustache = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const EXTENSION = '.mustache';
|
|
7
|
-
const DEFAULT_DOCUMENT = 'index.mustache';
|
|
8
|
-
const mustache = (init = { root: '' }) => {
|
|
9
|
-
const { root } = init;
|
|
10
|
-
return async (c, next) => {
|
|
11
|
-
let Mustache;
|
|
12
|
-
try {
|
|
13
|
-
Mustache = require('mustache');
|
|
14
|
-
}
|
|
15
|
-
catch (_a) {
|
|
16
|
-
throw new Error('If you want to use Mustache Middleware, install "mustache" package first.');
|
|
17
|
-
}
|
|
18
|
-
c.render = async (filename, params = {}, options) => {
|
|
19
|
-
const path = (0, cloudflare_1.getKVFilePath)({
|
|
20
|
-
filename: `${filename}${EXTENSION}`,
|
|
21
|
-
root: root,
|
|
22
|
-
defaultDocument: DEFAULT_DOCUMENT,
|
|
23
|
-
});
|
|
24
|
-
const buffer = await (0, cloudflare_1.getContentFromKVAsset)(path);
|
|
25
|
-
if (!buffer) {
|
|
26
|
-
throw new Error(`Template "${path}" is not found or blank.`);
|
|
27
|
-
}
|
|
28
|
-
const content = (0, buffer_1.bufferToString)(buffer);
|
|
29
|
-
const partialArgs = {};
|
|
30
|
-
if (options) {
|
|
31
|
-
const partials = options;
|
|
32
|
-
for (const key of Object.keys(partials)) {
|
|
33
|
-
const partialPath = (0, cloudflare_1.getKVFilePath)({
|
|
34
|
-
filename: `${partials[key]}${EXTENSION}`,
|
|
35
|
-
root: root,
|
|
36
|
-
defaultDocument: DEFAULT_DOCUMENT,
|
|
37
|
-
});
|
|
38
|
-
const partialBuffer = await (0, cloudflare_1.getContentFromKVAsset)(partialPath);
|
|
39
|
-
if (!partialBuffer) {
|
|
40
|
-
throw new Error(`Partial Template "${partialPath}" is not found or blank.`);
|
|
41
|
-
}
|
|
42
|
-
partialArgs[key] = (0, buffer_1.bufferToString)(partialBuffer);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
const output = Mustache.render(content, params, partialArgs);
|
|
46
|
-
return c.html(output);
|
|
47
|
-
};
|
|
48
|
-
await next();
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
exports.mustache = mustache;
|
|
4
|
+
var mustache_1 = require("./mustache");
|
|
5
|
+
Object.defineProperty(exports, "mustache", { enumerable: true, get: function () { return mustache_1.mustache; } });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
// For ES module mode
|
|
4
|
+
import manifest from '__STATIC_CONTENT_MANIFEST';
|
|
5
|
+
import { mustache } from './mustache';
|
|
6
|
+
const module = (options = { root: '' }) => {
|
|
7
|
+
return mustache({
|
|
8
|
+
root: options.root,
|
|
9
|
+
manifest: options.manifest ? options.manifest : manifest,
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
export { module as mustache };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import type { Handler } from '../../hono';
|
|
3
|
+
export declare type MustacheOptions = {
|
|
4
|
+
root: string;
|
|
5
|
+
manifest?: object | string;
|
|
6
|
+
namespace?: KVNamespace;
|
|
7
|
+
};
|
|
8
|
+
export declare const mustache: (init?: MustacheOptions) => Handler;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.mustache = void 0;
|
|
7
|
+
const mustache_1 = __importDefault(require("mustache"));
|
|
8
|
+
const buffer_1 = require("../../utils/buffer");
|
|
9
|
+
const cloudflare_1 = require("../../utils/cloudflare");
|
|
10
|
+
const EXTENSION = '.mustache';
|
|
11
|
+
const DEFAULT_DOCUMENT = 'index.mustache';
|
|
12
|
+
const mustache = (init = { root: '' }) => {
|
|
13
|
+
const { root } = init;
|
|
14
|
+
return async (c, next) => {
|
|
15
|
+
c.render = async (filename, params = {}, options) => {
|
|
16
|
+
const path = (0, cloudflare_1.getKVFilePath)({
|
|
17
|
+
filename: `${filename}${EXTENSION}`,
|
|
18
|
+
root: root,
|
|
19
|
+
defaultDocument: DEFAULT_DOCUMENT,
|
|
20
|
+
});
|
|
21
|
+
const kvAssetOptions = {
|
|
22
|
+
manifest: init.manifest,
|
|
23
|
+
namespace: init.namespace ? init.namespace : c.env ? c.env.__STATIC_CONTENT : undefined,
|
|
24
|
+
};
|
|
25
|
+
const buffer = await (0, cloudflare_1.getContentFromKVAsset)(path, kvAssetOptions);
|
|
26
|
+
if (!buffer) {
|
|
27
|
+
throw new Error(`Template "${path}" is not found or blank.`);
|
|
28
|
+
}
|
|
29
|
+
const content = (0, buffer_1.bufferToString)(buffer);
|
|
30
|
+
const partialArgs = {};
|
|
31
|
+
if (options) {
|
|
32
|
+
const partials = options;
|
|
33
|
+
for (const key of Object.keys(partials)) {
|
|
34
|
+
const partialPath = (0, cloudflare_1.getKVFilePath)({
|
|
35
|
+
filename: `${partials[key]}${EXTENSION}`,
|
|
36
|
+
root: root,
|
|
37
|
+
defaultDocument: DEFAULT_DOCUMENT,
|
|
38
|
+
});
|
|
39
|
+
const partialBuffer = await (0, cloudflare_1.getContentFromKVAsset)(partialPath, kvAssetOptions);
|
|
40
|
+
if (!partialBuffer) {
|
|
41
|
+
throw new Error(`Partial Template "${partialPath}" is not found or blank.`);
|
|
42
|
+
}
|
|
43
|
+
partialArgs[key] = (0, buffer_1.bufferToString)(partialBuffer);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const output = mustache_1.default.render(content, params, partialArgs);
|
|
47
|
+
return c.html(output);
|
|
48
|
+
};
|
|
49
|
+
await next();
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
exports.mustache = mustache;
|
|
@@ -1,7 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import type { Next } from '../../hono';
|
|
3
|
-
declare type Options = {
|
|
4
|
-
root: string;
|
|
5
|
-
};
|
|
6
|
-
export declare const serveStatic: (opt?: Options) => (c: Context, next: Next) => Promise<Response>;
|
|
7
|
-
export {};
|
|
1
|
+
export { serveStatic } from './serve-static';
|
|
@@ -1,35 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.serveStatic = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const DEFAULT_DOCUMENT = 'index.html';
|
|
7
|
-
// This middleware is available only on Cloudflare Workers.
|
|
8
|
-
const serveStatic = (opt = { root: '' }) => {
|
|
9
|
-
return async (c, next) => {
|
|
10
|
-
// Do nothing if Response is already set
|
|
11
|
-
if (c.res) {
|
|
12
|
-
await next();
|
|
13
|
-
}
|
|
14
|
-
const url = new URL(c.req.url);
|
|
15
|
-
const path = (0, cloudflare_1.getKVFilePath)({
|
|
16
|
-
filename: url.pathname,
|
|
17
|
-
root: opt.root,
|
|
18
|
-
defaultDocument: DEFAULT_DOCUMENT,
|
|
19
|
-
});
|
|
20
|
-
const content = await (0, cloudflare_1.getContentFromKVAsset)(path);
|
|
21
|
-
if (content) {
|
|
22
|
-
const mimeType = (0, mime_1.getMimeType)(path);
|
|
23
|
-
if (mimeType) {
|
|
24
|
-
c.header('Content-Type', mimeType);
|
|
25
|
-
}
|
|
26
|
-
// Return Response object
|
|
27
|
-
return c.body(content);
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
console.warn(`Static file: ${path} is not found`);
|
|
31
|
-
await next();
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
exports.serveStatic = serveStatic;
|
|
4
|
+
var serve_static_1 = require("./serve-static");
|
|
5
|
+
Object.defineProperty(exports, "serveStatic", { enumerable: true, get: function () { return serve_static_1.serveStatic; } });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
// For ES module mode
|
|
4
|
+
import manifest from '__STATIC_CONTENT_MANIFEST';
|
|
5
|
+
import { serveStatic } from './serve-static';
|
|
6
|
+
const module = (options = { root: '' }) => {
|
|
7
|
+
return serveStatic({
|
|
8
|
+
root: options.root,
|
|
9
|
+
manifest: options.manifest ? options.manifest : manifest,
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
export { module as serveStatic };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import type { Handler } from '../../hono';
|
|
3
|
+
export declare type ServeStaticOptions = {
|
|
4
|
+
root: string;
|
|
5
|
+
manifest?: object | string;
|
|
6
|
+
namespace?: KVNamespace;
|
|
7
|
+
};
|
|
8
|
+
export declare const serveStatic: (options?: ServeStaticOptions) => Handler;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serveStatic = void 0;
|
|
4
|
+
const cloudflare_1 = require("../../utils/cloudflare");
|
|
5
|
+
const mime_1 = require("../../utils/mime");
|
|
6
|
+
const DEFAULT_DOCUMENT = 'index.html';
|
|
7
|
+
// This middleware is available only on Cloudflare Workers.
|
|
8
|
+
const serveStatic = (options = { root: '' }) => {
|
|
9
|
+
return async (c, next) => {
|
|
10
|
+
// Do nothing if Response is already set
|
|
11
|
+
if (c.res && c.res.finalized) {
|
|
12
|
+
await next();
|
|
13
|
+
}
|
|
14
|
+
const url = new URL(c.req.url);
|
|
15
|
+
const path = (0, cloudflare_1.getKVFilePath)({
|
|
16
|
+
filename: url.pathname,
|
|
17
|
+
root: options.root,
|
|
18
|
+
defaultDocument: DEFAULT_DOCUMENT,
|
|
19
|
+
});
|
|
20
|
+
const content = await (0, cloudflare_1.getContentFromKVAsset)(path, {
|
|
21
|
+
manifest: options.manifest,
|
|
22
|
+
namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : undefined,
|
|
23
|
+
});
|
|
24
|
+
if (content) {
|
|
25
|
+
const mimeType = (0, mime_1.getMimeType)(path);
|
|
26
|
+
if (mimeType) {
|
|
27
|
+
c.header('Content-Type', mimeType);
|
|
28
|
+
}
|
|
29
|
+
// Return Response object
|
|
30
|
+
return c.body(content);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
console.warn(`Static file: ${path} is not found`);
|
|
34
|
+
await next();
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
exports.serveStatic = serveStatic;
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
declare type
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
export declare type KVAssetOptions = {
|
|
3
|
+
manifest?: object | string;
|
|
4
|
+
namespace?: KVNamespace;
|
|
5
|
+
};
|
|
6
|
+
export declare const getContentFromKVAsset: (path: string, options?: KVAssetOptions) => Promise<ArrayBuffer>;
|
|
7
|
+
declare type FilePathOptions = {
|
|
3
8
|
filename: string;
|
|
4
9
|
root?: string;
|
|
5
10
|
defaultDocument?: string;
|
|
6
11
|
};
|
|
7
|
-
export declare const getKVFilePath: (
|
|
12
|
+
export declare const getKVFilePath: (options: FilePathOptions) => string;
|
|
8
13
|
export {};
|
package/dist/utils/cloudflare.js
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getKVFilePath = exports.getContentFromKVAsset = void 0;
|
|
4
|
-
const getContentFromKVAsset = async (path) => {
|
|
5
|
-
let ASSET_MANIFEST;
|
|
6
|
-
if (
|
|
7
|
-
|
|
4
|
+
const getContentFromKVAsset = async (path, options) => {
|
|
5
|
+
let ASSET_MANIFEST = {};
|
|
6
|
+
if (options && options.manifest) {
|
|
7
|
+
if (typeof options.manifest === 'string') {
|
|
8
|
+
ASSET_MANIFEST = JSON.parse(options.manifest);
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
ASSET_MANIFEST = options.manifest;
|
|
12
|
+
}
|
|
8
13
|
}
|
|
9
14
|
else {
|
|
10
|
-
|
|
15
|
+
if (typeof __STATIC_CONTENT_MANIFEST === 'string') {
|
|
16
|
+
ASSET_MANIFEST = JSON.parse(__STATIC_CONTENT_MANIFEST);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
ASSET_MANIFEST = __STATIC_CONTENT_MANIFEST;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
let ASSET_NAMESPACE;
|
|
23
|
+
if (options && options.namespace) {
|
|
24
|
+
ASSET_NAMESPACE = options.namespace;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
ASSET_NAMESPACE = __STATIC_CONTENT;
|
|
11
28
|
}
|
|
12
|
-
const ASSET_NAMESPACE = __STATIC_CONTENT;
|
|
13
29
|
const key = ASSET_MANIFEST[path] || path;
|
|
14
30
|
if (!key) {
|
|
15
31
|
return;
|
|
@@ -21,10 +37,10 @@ const getContentFromKVAsset = async (path) => {
|
|
|
21
37
|
return content;
|
|
22
38
|
};
|
|
23
39
|
exports.getContentFromKVAsset = getContentFromKVAsset;
|
|
24
|
-
const getKVFilePath = (
|
|
25
|
-
let filename =
|
|
26
|
-
let root =
|
|
27
|
-
const defaultDocument =
|
|
40
|
+
const getKVFilePath = (options) => {
|
|
41
|
+
let filename = options.filename;
|
|
42
|
+
let root = options.root || '';
|
|
43
|
+
const defaultDocument = options.defaultDocument || 'index.html';
|
|
28
44
|
if (filename.endsWith('/')) {
|
|
29
45
|
// /top/ => /top/index.html
|
|
30
46
|
filename = filename.concat(defaultDocument);
|
package/dist/utils/crypto.js
CHANGED
|
@@ -23,14 +23,5 @@ const createHash = async (data, algorithm) => {
|
|
|
23
23
|
.join('');
|
|
24
24
|
return hash;
|
|
25
25
|
}
|
|
26
|
-
try {
|
|
27
|
-
const crypto = require('crypto');
|
|
28
|
-
const hash = crypto.createHash(algorithm.alias).update(data).digest('hex');
|
|
29
|
-
return hash;
|
|
30
|
-
}
|
|
31
|
-
catch (e) {
|
|
32
|
-
console.error(`If you want to create hash ${algorithm.name}, polyfill "crypto" module.`);
|
|
33
|
-
throw e;
|
|
34
|
-
}
|
|
35
26
|
};
|
|
36
27
|
exports.createHash = createHash;
|
package/dist/utils/encode.js
CHANGED
|
@@ -1,27 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
3
|
exports.arrayBufferToBase64URL = exports.arrayBufferToBase64 = exports.utf8ToUint8Array = exports.decodeBase64URL = exports.encodeBase64URL = exports.decodeBase64 = exports.encodeBase64 = void 0;
|
|
27
4
|
const encodeBase64 = (str) => {
|
|
@@ -35,7 +12,6 @@ const encodeBase64 = (str) => {
|
|
|
35
12
|
}
|
|
36
13
|
catch (_a) { }
|
|
37
14
|
try {
|
|
38
|
-
const { Buffer } = require('buffer');
|
|
39
15
|
return Buffer.from(str).toString('base64');
|
|
40
16
|
}
|
|
41
17
|
catch (e) {
|
|
@@ -56,7 +32,6 @@ const decodeBase64 = (str) => {
|
|
|
56
32
|
}
|
|
57
33
|
catch (_a) { }
|
|
58
34
|
try {
|
|
59
|
-
const { Buffer } = require('buffer');
|
|
60
35
|
return Buffer.from(str, 'base64').toString();
|
|
61
36
|
}
|
|
62
37
|
catch (e) {
|
|
@@ -93,7 +68,6 @@ const arrayBufferToBase64 = async (buf) => {
|
|
|
93
68
|
return btoa(String.fromCharCode(...new Uint8Array(buf)));
|
|
94
69
|
}
|
|
95
70
|
try {
|
|
96
|
-
const { Buffer } = await Promise.resolve().then(() => __importStar(require('buffer')));
|
|
97
71
|
return Buffer.from(String.fromCharCode(...new Uint8Array(buf))).toString('base64');
|
|
98
72
|
}
|
|
99
73
|
catch (e) { }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.6",
|
|
4
4
|
"description": "Ultrafast web framework for Cloudflare Workers.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"test": "jest",
|
|
12
12
|
"lint": "eslint --ext js,ts src .eslintrc.js",
|
|
13
13
|
"lint:fix": "eslint --ext js,ts src .eslintrc.js --fix",
|
|
14
|
-
"build": "rimraf dist && tsc --project tsconfig.build.json",
|
|
14
|
+
"build": "rimraf dist && tsc --project tsconfig.build.esm.json && tsc --project tsconfig.build.json",
|
|
15
15
|
"watch": "tsc --project tsconfig.build.json -w",
|
|
16
16
|
"prepublishOnly": "yarn build"
|
|
17
17
|
},
|
|
@@ -26,9 +26,11 @@
|
|
|
26
26
|
"./jwt": "./dist/middleware/jwt/index.js",
|
|
27
27
|
"./logger": "./dist/middleware/logger/index.js",
|
|
28
28
|
"./mustache": "./dist/middleware/mustache/index.js",
|
|
29
|
+
"./mustache.module": "./dist/middleware/mustache/module.mjs",
|
|
29
30
|
"./powered-by": "./dist/middleware/powered-by/index.js",
|
|
30
31
|
"./pretty-json": "./dist/middleware/pretty-json/index.js",
|
|
31
32
|
"./serve-static": "./dist/middleware/serve-static/index.js",
|
|
33
|
+
"./serve-static.module": "./dist/middleware/serve-static/module.mjs",
|
|
32
34
|
"./router/trie-router": "./dist/router/trie-router/index.js",
|
|
33
35
|
"./router/reg-exp-router": "./dist/router/reg-exp-router/index.js",
|
|
34
36
|
"./utils/jwt": "./dist/utils/jwt/index.js",
|
|
@@ -63,6 +65,9 @@
|
|
|
63
65
|
"mustache": [
|
|
64
66
|
"./dist/middleware/mustache"
|
|
65
67
|
],
|
|
68
|
+
"mustache.module": [
|
|
69
|
+
"./dist/middleware/mustache/module.d.mts"
|
|
70
|
+
],
|
|
66
71
|
"powered-by": [
|
|
67
72
|
"./dist/middleware/powered-by"
|
|
68
73
|
],
|
|
@@ -70,7 +75,10 @@
|
|
|
70
75
|
"./dist/middleware/pretty-json"
|
|
71
76
|
],
|
|
72
77
|
"serve-static": [
|
|
73
|
-
"./dist/middleware/serve-static"
|
|
78
|
+
"./dist/middleware/serve-static/index.d.ts"
|
|
79
|
+
],
|
|
80
|
+
"serve-static.module": [
|
|
81
|
+
"./dist/middleware/serve-static/module.d.mts"
|
|
74
82
|
],
|
|
75
83
|
"router/trie-router": [
|
|
76
84
|
"./dist/router/trie-router/router.d.ts"
|