hono 0.3.1 → 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/basic-auth/basic-auth.js +1 -10
- 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/buffer.d.ts +2 -0
- package/dist/utils/buffer.js +45 -8
- 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) {
|
|
@@ -18,21 +18,12 @@ const auth = (req) => {
|
|
|
18
18
|
if (!match) {
|
|
19
19
|
return undefined;
|
|
20
20
|
}
|
|
21
|
-
const userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1]));
|
|
21
|
+
const userPass = USER_PASS_REGEXP.exec((0, buffer_1.decodeBase64)(match[1]));
|
|
22
22
|
if (!userPass) {
|
|
23
23
|
return undefined;
|
|
24
24
|
}
|
|
25
25
|
return { username: userPass[1], password: userPass[2] };
|
|
26
26
|
};
|
|
27
|
-
function decodeBase64(str) {
|
|
28
|
-
if (atob && btoa) {
|
|
29
|
-
return atob(str);
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
const { Buffer } = require('buffer');
|
|
33
|
-
return Buffer.from(str, 'base64').toString();
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
27
|
const basicAuth = (options) => {
|
|
37
28
|
if (!options.realm) {
|
|
38
29
|
options.realm = 'Secure Area';
|
|
@@ -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/buffer.d.ts
CHANGED
package/dist/utils/buffer.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.timingSafeEqual = exports.equal = void 0;
|
|
3
|
+
exports.timingSafeEqual = exports.sha256 = exports.decodeBase64 = exports.equal = void 0;
|
|
4
4
|
const equal = (a, b) => {
|
|
5
5
|
if (a === b) {
|
|
6
6
|
return true;
|
|
@@ -19,13 +19,50 @@ const equal = (a, b) => {
|
|
|
19
19
|
return true;
|
|
20
20
|
};
|
|
21
21
|
exports.equal = equal;
|
|
22
|
+
const decodeBase64 = (str) => {
|
|
23
|
+
try {
|
|
24
|
+
const text = atob(str);
|
|
25
|
+
const length = text.length;
|
|
26
|
+
const bytes = new Uint8Array(length);
|
|
27
|
+
for (let i = 0; i < length; i++) {
|
|
28
|
+
bytes[i] = text.charCodeAt(i);
|
|
29
|
+
}
|
|
30
|
+
const decoder = new TextDecoder();
|
|
31
|
+
return decoder.decode(bytes);
|
|
32
|
+
}
|
|
33
|
+
catch (_a) { }
|
|
34
|
+
try {
|
|
35
|
+
const { Buffer } = require('buffer');
|
|
36
|
+
return Buffer.from(str, 'base64').toString();
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
console.error('If you want to do "decodeBase64", polyfill "buffer" module.');
|
|
40
|
+
throw e;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
exports.decodeBase64 = decodeBase64;
|
|
44
|
+
const sha256 = async (a) => {
|
|
45
|
+
if (crypto && crypto.subtle) {
|
|
46
|
+
const buffer = await crypto.subtle.digest({
|
|
47
|
+
name: 'SHA-256',
|
|
48
|
+
}, new TextEncoder().encode(String(a)));
|
|
49
|
+
const hash = Array.prototype.map.call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2)).join('');
|
|
50
|
+
return hash;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
const crypto = require('crypto');
|
|
54
|
+
const hash = crypto.createHash('sha256').update(a).digest('hex');
|
|
55
|
+
return hash;
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
console.error('If you want to do "sha256", polyfill "crypto" module.');
|
|
59
|
+
throw e;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
exports.sha256 = sha256;
|
|
22
63
|
const timingSafeEqual = async (a, b) => {
|
|
23
|
-
const sa = await
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const sb = await crypto.subtle.digest({
|
|
27
|
-
name: 'SHA-256',
|
|
28
|
-
}, new TextEncoder().encode(String(b)));
|
|
29
|
-
return (0, exports.equal)(sa, sb) && a === b;
|
|
64
|
+
const sa = await (0, exports.sha256)(a);
|
|
65
|
+
const sb = await (0, exports.sha256)(b);
|
|
66
|
+
return sa === sb && a === b;
|
|
30
67
|
};
|
|
31
68
|
exports.timingSafeEqual = timingSafeEqual;
|
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;
|