hono 0.3.4 → 0.3.5
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/context.js +3 -3
- package/dist/middleware/mustache/mustache.d.ts +5 -1
- package/dist/middleware/mustache/mustache.js +13 -5
- package/dist/middleware/serve-static/serve-static.js +1 -19
- package/dist/utils/cloudflare.d.ts +7 -0
- package/dist/utils/cloudflare.js +23 -1
- package/package.json +1 -1
package/dist/context.js
CHANGED
|
@@ -52,7 +52,7 @@ class Context {
|
|
|
52
52
|
if (typeof text !== 'string') {
|
|
53
53
|
throw new TypeError('text method arg must be a string!');
|
|
54
54
|
}
|
|
55
|
-
headers['Content-Type'] = 'text/plain';
|
|
55
|
+
headers['Content-Type'] || (headers['Content-Type'] = 'text/plain; charset=UTF-8');
|
|
56
56
|
return this.body(text, status, headers);
|
|
57
57
|
}
|
|
58
58
|
json(object, status = this._status, headers = {}) {
|
|
@@ -60,14 +60,14 @@ class Context {
|
|
|
60
60
|
throw new TypeError('json method arg must be a object!');
|
|
61
61
|
}
|
|
62
62
|
const body = JSON.stringify(object);
|
|
63
|
-
headers['Content-Type'] = 'application/json; charset=UTF-8';
|
|
63
|
+
headers['Content-Type'] || (headers['Content-Type'] = 'application/json; charset=UTF-8');
|
|
64
64
|
return this.body(body, status, headers);
|
|
65
65
|
}
|
|
66
66
|
html(html, status = this._status, headers = {}) {
|
|
67
67
|
if (typeof html !== 'string') {
|
|
68
68
|
throw new TypeError('html method arg must be a string!');
|
|
69
69
|
}
|
|
70
|
-
headers['Content-Type'] = 'text/html; charset=UTF-8';
|
|
70
|
+
headers['Content-Type'] || (headers['Content-Type'] = 'text/html; charset=UTF-8');
|
|
71
71
|
return this.body(html, status, headers);
|
|
72
72
|
}
|
|
73
73
|
redirect(location, status = 302) {
|
|
@@ -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 {
|
|
@@ -13,18 +15,24 @@ const mustache = () => {
|
|
|
13
15
|
throw new Error('If you want to use Mustache Middleware, install "mustache" package first.');
|
|
14
16
|
}
|
|
15
17
|
c.render = async (filename, view = {}, options) => {
|
|
16
|
-
const
|
|
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
|
}
|
|
@@ -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/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;
|