wok-server 0.1.0
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/LICENSE +21 -0
- package/README.md +47 -0
- package/dist/cache/cache.js +94 -0
- package/dist/cache/config.js +19 -0
- package/dist/cache/index.js +27 -0
- package/dist/cache/purge-task.js +56 -0
- package/dist/cache/stat.js +47 -0
- package/dist/config/convert.js +36 -0
- package/dist/config/exception.js +14 -0
- package/dist/config/index.js +67 -0
- package/dist/http-client/index.js +132 -0
- package/dist/i18n/ar.js +17 -0
- package/dist/i18n/de.js +17 -0
- package/dist/i18n/en-us.js +17 -0
- package/dist/i18n/es.js +17 -0
- package/dist/i18n/fr.js +17 -0
- package/dist/i18n/i18n.js +231 -0
- package/dist/i18n/index.js +52 -0
- package/dist/i18n/ja.js +17 -0
- package/dist/i18n/ko.js +17 -0
- package/dist/i18n/msg.js +2 -0
- package/dist/i18n/pt.js +17 -0
- package/dist/i18n/ru.js +17 -0
- package/dist/i18n/tag.js +18 -0
- package/dist/i18n/zh-HK.js +17 -0
- package/dist/i18n/zh-TW.js +17 -0
- package/dist/i18n/zh-cn.js +17 -0
- package/dist/index.js +13 -0
- package/dist/log/config.js +28 -0
- package/dist/log/date.js +21 -0
- package/dist/log/file.js +79 -0
- package/dist/log/index.js +109 -0
- package/dist/log/level.js +39 -0
- package/dist/log/store.js +16 -0
- package/dist/mongodb/collection.js +2 -0
- package/dist/mongodb/config.js +34 -0
- package/dist/mongodb/doc.js +2 -0
- package/dist/mongodb/exception.js +14 -0
- package/dist/mongodb/index.js +58 -0
- package/dist/mongodb/manager/base.js +563 -0
- package/dist/mongodb/manager/index.js +63 -0
- package/dist/mongodb/manager/tx-strict.js +84 -0
- package/dist/mongodb/manager/tx.js +30 -0
- package/dist/mongodb/migration.js +52 -0
- package/dist/mvc/access-log.js +31 -0
- package/dist/mvc/config.js +20 -0
- package/dist/mvc/exchange.js +113 -0
- package/dist/mvc/handler/index.js +6 -0
- package/dist/mvc/handler/json.js +33 -0
- package/dist/mvc/handler/restful.js +35 -0
- package/dist/mvc/handler/upload.js +33 -0
- package/dist/mvc/index.js +316 -0
- package/dist/mvc/interceptor.js +2 -0
- package/dist/mvc/query.js +43 -0
- package/dist/mvc/render/file.js +177 -0
- package/dist/mvc/render/html/html.js +90 -0
- package/dist/mvc/render/html/index.js +18 -0
- package/dist/mvc/render/html/style.js +2 -0
- package/dist/mvc/render/index.js +7 -0
- package/dist/mvc/render/json.js +26 -0
- package/dist/mvc/render/text.js +16 -0
- package/dist/mvc/router.js +2 -0
- package/dist/mysql/config.js +49 -0
- package/dist/mysql/exception.js +14 -0
- package/dist/mysql/index.js +85 -0
- package/dist/mysql/manager/base.js +233 -0
- package/dist/mysql/manager/index.js +107 -0
- package/dist/mysql/manager/ops/count.js +20 -0
- package/dist/mysql/manager/ops/criteria.js +326 -0
- package/dist/mysql/manager/ops/delete.js +65 -0
- package/dist/mysql/manager/ops/exist.js +26 -0
- package/dist/mysql/manager/ops/find.js +111 -0
- package/dist/mysql/manager/ops/index.js +14 -0
- package/dist/mysql/manager/ops/insert.js +101 -0
- package/dist/mysql/manager/ops/modify.js +10 -0
- package/dist/mysql/manager/ops/paginate.js +23 -0
- package/dist/mysql/manager/ops/query.js +9 -0
- package/dist/mysql/manager/ops/update.js +201 -0
- package/dist/mysql/manager/tx-strict.js +98 -0
- package/dist/mysql/manager/tx.js +30 -0
- package/dist/mysql/manager/utils.js +56 -0
- package/dist/mysql/migration.js +136 -0
- package/dist/mysql/table-info.js +8 -0
- package/dist/task/daily.js +58 -0
- package/dist/task/fixed-delay.js +33 -0
- package/dist/task/fixed-rate.js +37 -0
- package/dist/task/index.js +9 -0
- package/dist/task/task.js +39 -0
- package/dist/validation/exception.js +44 -0
- package/dist/validation/index.js +29 -0
- package/dist/validation/validator/array.js +38 -0
- package/dist/validation/validator/enum.js +28 -0
- package/dist/validation/validator/index.js +14 -0
- package/dist/validation/validator/length.js +40 -0
- package/dist/validation/validator/max-length.js +35 -0
- package/dist/validation/validator/max.js +29 -0
- package/dist/validation/validator/min-length.js +33 -0
- package/dist/validation/validator/min.js +29 -0
- package/dist/validation/validator/not-blank.js +33 -0
- package/dist/validation/validator/not-null.js +21 -0
- package/dist/validation/validator/plain-obj.js +32 -0
- package/dist/validation/validator/regexp.js +30 -0
- package/documentation/en/index.md +1 -0
- package/documentation/zh-cn/cache.md +59 -0
- package/documentation/zh-cn/config.md +68 -0
- package/documentation/zh-cn/http-client.md +33 -0
- package/documentation/zh-cn/i18n.md +154 -0
- package/documentation/zh-cn/index.md +25 -0
- package/documentation/zh-cn/log.md +40 -0
- package/documentation/zh-cn/mongodb.md +262 -0
- package/documentation/zh-cn/mvc.md +430 -0
- package/documentation/zh-cn/mysql.md +389 -0
- package/documentation/zh-cn/task.md +50 -0
- package/documentation/zh-cn/test.md +57 -0
- package/documentation/zh-cn/validate.md +125 -0
- package/package.json +46 -0
- package/types/cache/cache.d.ts +52 -0
- package/types/cache/config.d.ts +32 -0
- package/types/cache/index.d.ts +2 -0
- package/types/cache/purge-task.d.ts +11 -0
- package/types/cache/stat.d.ts +26 -0
- package/types/config/convert.d.ts +6 -0
- package/types/config/exception.d.ts +7 -0
- package/types/config/index.d.ts +15 -0
- package/types/http-client/index.d.ts +71 -0
- package/types/i18n/ar.d.ts +2 -0
- package/types/i18n/de.d.ts +2 -0
- package/types/i18n/en-us.d.ts +2 -0
- package/types/i18n/es.d.ts +2 -0
- package/types/i18n/fr.d.ts +2 -0
- package/types/i18n/i18n.d.ts +102 -0
- package/types/i18n/index.d.ts +9 -0
- package/types/i18n/ja.d.ts +2 -0
- package/types/i18n/ko.d.ts +2 -0
- package/types/i18n/msg.d.ts +50 -0
- package/types/i18n/pt.d.ts +2 -0
- package/types/i18n/ru.d.ts +2 -0
- package/types/i18n/tag.d.ts +11 -0
- package/types/i18n/zh-HK.d.ts +2 -0
- package/types/i18n/zh-TW.d.ts +2 -0
- package/types/i18n/zh-cn.d.ts +2 -0
- package/types/index.d.ts +10 -0
- package/types/log/config.d.ts +27 -0
- package/types/log/date.d.ts +2 -0
- package/types/log/file.d.ts +5 -0
- package/types/log/index.d.ts +34 -0
- package/types/log/level.d.ts +15 -0
- package/types/log/store.d.ts +12 -0
- package/types/mongodb/collection.d.ts +25 -0
- package/types/mongodb/config.d.ts +45 -0
- package/types/mongodb/doc.d.ts +11 -0
- package/types/mongodb/exception.d.ts +7 -0
- package/types/mongodb/index.d.ts +29 -0
- package/types/mongodb/manager/base.d.ts +188 -0
- package/types/mongodb/manager/index.d.ts +38 -0
- package/types/mongodb/manager/tx-strict.d.ts +41 -0
- package/types/mongodb/manager/tx.d.ts +21 -0
- package/types/mongodb/migration.d.ts +12 -0
- package/types/mvc/access-log.d.ts +7 -0
- package/types/mvc/config.d.ts +30 -0
- package/types/mvc/exchange.d.ts +72 -0
- package/types/mvc/handler/index.d.ts +3 -0
- package/types/mvc/handler/json.d.ts +23 -0
- package/types/mvc/handler/restful.d.ts +11 -0
- package/types/mvc/handler/upload.d.ts +40 -0
- package/types/mvc/index.d.ts +49 -0
- package/types/mvc/interceptor.d.ts +11 -0
- package/types/mvc/query.d.ts +13 -0
- package/types/mvc/render/file.d.ts +10 -0
- package/types/mvc/render/html/html.d.ts +98 -0
- package/types/mvc/render/html/index.d.ts +11 -0
- package/types/mvc/render/html/style.d.ts +1201 -0
- package/types/mvc/render/index.d.ts +4 -0
- package/types/mvc/render/json.d.ts +17 -0
- package/types/mvc/render/text.d.ts +10 -0
- package/types/mvc/router.d.ts +11 -0
- package/types/mysql/config.d.ts +86 -0
- package/types/mysql/exception.d.ts +7 -0
- package/types/mysql/index.d.ts +16 -0
- package/types/mysql/manager/base.d.ts +158 -0
- package/types/mysql/manager/index.d.ts +36 -0
- package/types/mysql/manager/ops/count.d.ts +13 -0
- package/types/mysql/manager/ops/criteria.d.ts +120 -0
- package/types/mysql/manager/ops/delete.d.ts +46 -0
- package/types/mysql/manager/ops/exist.d.ts +6 -0
- package/types/mysql/manager/ops/find.d.ts +66 -0
- package/types/mysql/manager/ops/index.d.ts +10 -0
- package/types/mysql/manager/ops/insert.d.ts +18 -0
- package/types/mysql/manager/ops/modify.d.ts +3 -0
- package/types/mysql/manager/ops/paginate.d.ts +36 -0
- package/types/mysql/manager/ops/query.d.ts +3 -0
- package/types/mysql/manager/ops/update.d.ts +70 -0
- package/types/mysql/manager/tx-strict.d.ts +34 -0
- package/types/mysql/manager/tx.d.ts +15 -0
- package/types/mysql/manager/utils.d.ts +17 -0
- package/types/mysql/migration.d.ts +8 -0
- package/types/mysql/table-info.d.ts +36 -0
- package/types/task/daily.d.ts +15 -0
- package/types/task/fixed-delay.d.ts +8 -0
- package/types/task/fixed-rate.d.ts +8 -0
- package/types/task/index.d.ts +4 -0
- package/types/task/task.d.ts +33 -0
- package/types/validation/exception.d.ts +43 -0
- package/types/validation/index.d.ts +32 -0
- package/types/validation/validator/array.d.ts +5 -0
- package/types/validation/validator/enum.d.ts +8 -0
- package/types/validation/validator/index.d.ts +11 -0
- package/types/validation/validator/length.d.ts +10 -0
- package/types/validation/validator/max-length.d.ts +8 -0
- package/types/validation/validator/max.d.ts +7 -0
- package/types/validation/validator/min-length.d.ts +6 -0
- package/types/validation/validator/min.d.ts +7 -0
- package/types/validation/validator/not-blank.d.ts +7 -0
- package/types/validation/validator/not-null.d.ts +6 -0
- package/types/validation/validator/plain-obj.d.ts +7 -0
- package/types/validation/validator/regexp.d.ts +8 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueryString = void 0;
|
|
4
|
+
const querystring_1 = require("querystring");
|
|
5
|
+
class QueryString {
|
|
6
|
+
qs;
|
|
7
|
+
constructor(url) {
|
|
8
|
+
const idx = url.indexOf('?');
|
|
9
|
+
this.qs = idx !== -1 ? (0, querystring_1.parse)(url.substring(idx + 1)) : {};
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 获取单个字符串值
|
|
13
|
+
* @param name
|
|
14
|
+
*/
|
|
15
|
+
getStr(name) {
|
|
16
|
+
const res = this.qs[name];
|
|
17
|
+
if (!res) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
else if (typeof res === 'string') {
|
|
21
|
+
return res;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return res[0];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 获取参数的所有值
|
|
29
|
+
*/
|
|
30
|
+
getStrVals(name) {
|
|
31
|
+
const res = this.qs[name];
|
|
32
|
+
if (!res) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
else if (typeof res === 'string') {
|
|
36
|
+
return [res];
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return res;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.QueryString = QueryString;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.renderFile = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const json_1 = require("./json");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
/**
|
|
8
|
+
* 常用的 content-type 对照表
|
|
9
|
+
*/
|
|
10
|
+
const frequentlyUsedContentTypeTable = {
|
|
11
|
+
aac: 'audio/aac',
|
|
12
|
+
abw: 'application/x-abiword',
|
|
13
|
+
arc: 'application/x-freearc',
|
|
14
|
+
avi: 'video/x-msvideo',
|
|
15
|
+
azw: 'application/vnd.amazon.ebook',
|
|
16
|
+
bin: 'application/octet-stream',
|
|
17
|
+
bmp: 'image/bmp',
|
|
18
|
+
bz: 'application/x-bzip',
|
|
19
|
+
bz2: 'application/x-bzip2',
|
|
20
|
+
csh: 'application/x-csh',
|
|
21
|
+
css: 'text/css',
|
|
22
|
+
csv: 'text/csv',
|
|
23
|
+
doc: 'application/msword',
|
|
24
|
+
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
25
|
+
eot: 'application/vnd.ms-fontobject',
|
|
26
|
+
epub: 'application/epub+zip',
|
|
27
|
+
gif: 'image/gif',
|
|
28
|
+
htm: 'text/html',
|
|
29
|
+
html: 'text/html',
|
|
30
|
+
ico: 'image/vnd.microsoft.icon',
|
|
31
|
+
ics: 'text/calendar',
|
|
32
|
+
jar: 'application/java-archive',
|
|
33
|
+
jpeg: 'image/jpeg',
|
|
34
|
+
jpg: 'image/jpeg',
|
|
35
|
+
js: 'text/javascript',
|
|
36
|
+
json: 'application/json',
|
|
37
|
+
jsonld: 'application/ld+json',
|
|
38
|
+
mid: 'audio/midi',
|
|
39
|
+
midi: 'audio/midi',
|
|
40
|
+
mjs: 'text/javascript',
|
|
41
|
+
mp3: 'audio/mpeg',
|
|
42
|
+
mpeg: 'video/mpeg',
|
|
43
|
+
mpkg: 'application/vnd.apple.installer+xml',
|
|
44
|
+
odp: 'application/vnd.oasis.opendocument.presentation',
|
|
45
|
+
ods: 'application/vnd.oasis.opendocument.spreadsheet',
|
|
46
|
+
odt: 'application/vnd.oasis.opendocument.text',
|
|
47
|
+
oga: 'audio/ogg',
|
|
48
|
+
ogv: 'video/ogg',
|
|
49
|
+
ogx: 'application/ogg',
|
|
50
|
+
otf: 'font/otf',
|
|
51
|
+
png: 'image/png',
|
|
52
|
+
pdf: 'application/pdf',
|
|
53
|
+
ppt: 'application/vnd.ms-powerpoint',
|
|
54
|
+
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
55
|
+
rar: 'application/x-rar-compressed',
|
|
56
|
+
rtf: 'application/rtf',
|
|
57
|
+
sh: 'application/x-sh',
|
|
58
|
+
svg: 'image/svg+xml',
|
|
59
|
+
swf: 'application/x-shockwave-flash',
|
|
60
|
+
tar: 'application/x-tar',
|
|
61
|
+
tif: 'image/tiff',
|
|
62
|
+
tiff: 'image/tiff',
|
|
63
|
+
ttf: 'font/ttf',
|
|
64
|
+
txt: 'text/plain',
|
|
65
|
+
vsd: 'application/vnd.visio',
|
|
66
|
+
wav: 'audio/wav',
|
|
67
|
+
weba: 'audio/webm',
|
|
68
|
+
webm: 'video/webm',
|
|
69
|
+
webp: 'image/webp',
|
|
70
|
+
woff: 'font/woff',
|
|
71
|
+
woff2: 'font/woff2',
|
|
72
|
+
xhtml: 'application/xhtml+xml',
|
|
73
|
+
xls: 'application/vnd.ms-excel',
|
|
74
|
+
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
75
|
+
xml: 'application/xml',
|
|
76
|
+
xul: 'application/vnd.mozilla.xul+xml',
|
|
77
|
+
zip: 'application/zip',
|
|
78
|
+
'3gp': 'video/3gpp',
|
|
79
|
+
'3g2': 'video/3gpp2',
|
|
80
|
+
'7z': 'application/x-7z-compressed'
|
|
81
|
+
};
|
|
82
|
+
function decideContentType(fileName) {
|
|
83
|
+
const idx = fileName.lastIndexOf('.');
|
|
84
|
+
if (idx === -1) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
const suffix = fileName.substring(idx + 1);
|
|
88
|
+
return frequentlyUsedContentTypeTable[suffix];
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 响应一个文件.
|
|
92
|
+
* @param request 请求信息
|
|
93
|
+
* @param response 响应信息
|
|
94
|
+
* @param filePath 文件路径,如果文件不存在,则会响应 404
|
|
95
|
+
* @param download 是否下载
|
|
96
|
+
*/
|
|
97
|
+
async function renderFile(request, response, filePath, download = false) {
|
|
98
|
+
if (!(0, fs_1.existsSync)(filePath)) {
|
|
99
|
+
(0, json_1.renderError)(response, '文件不存在', 404);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const fileName = (0, path_1.basename)(filePath);
|
|
103
|
+
let isDownload = false;
|
|
104
|
+
let contentType;
|
|
105
|
+
if (download) {
|
|
106
|
+
isDownload = true;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
contentType = decideContentType(fileName);
|
|
110
|
+
if (!contentType) {
|
|
111
|
+
isDownload = true;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (isDownload) {
|
|
115
|
+
response.setHeader('Content-Type', 'application/octet-stream');
|
|
116
|
+
response.setHeader('Content-Disposition', `attachment; filename="${encodeURIComponent(fileName)}"`);
|
|
117
|
+
}
|
|
118
|
+
else if (contentType) {
|
|
119
|
+
response.setHeader('Content-Type', contentType);
|
|
120
|
+
}
|
|
121
|
+
// 支持 Range
|
|
122
|
+
// https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Range
|
|
123
|
+
const rangeHeader = request.headers['range'];
|
|
124
|
+
if (!rangeHeader) {
|
|
125
|
+
return streamFile(filePath, response);
|
|
126
|
+
}
|
|
127
|
+
// 解析,range 示例:bytes=200-1000, 2000-6576, 19000-
|
|
128
|
+
// 多段的情况,暂时不做支持,非常麻烦,段数多还可能会有效率问题
|
|
129
|
+
// 如果不是字节范围不是以字节为单位,暂时也不做支持
|
|
130
|
+
const ranges = rangeHeader.split(',');
|
|
131
|
+
let range = ranges.length ? ranges[0] : undefined;
|
|
132
|
+
if (!range) {
|
|
133
|
+
return streamFile(filePath, response);
|
|
134
|
+
}
|
|
135
|
+
range = range.trim();
|
|
136
|
+
if (!range.startsWith('bytes=')) {
|
|
137
|
+
return streamFile(filePath, response);
|
|
138
|
+
}
|
|
139
|
+
range = range.substring(6);
|
|
140
|
+
const strs = range.split('-');
|
|
141
|
+
let start = strs[0] ? parseInt(strs[0], 10) : NaN;
|
|
142
|
+
let end = strs[1] ? parseInt(strs[1], 10) : NaN;
|
|
143
|
+
// 解析文件
|
|
144
|
+
const stat = (0, fs_1.statSync)(filePath);
|
|
145
|
+
if (isNaN(start) || start < 0) {
|
|
146
|
+
// 范围不合法,返回 416
|
|
147
|
+
(0, json_1.renderError)(response, `Range not satisfiable,start is ${start}`, 416);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (isNaN(end)) {
|
|
151
|
+
end = stat.size - 1;
|
|
152
|
+
}
|
|
153
|
+
else if (end > stat.size - 1) {
|
|
154
|
+
// 范围不合法,返回 416
|
|
155
|
+
(0, json_1.renderError)(response, `Range not satisfiable,end must not be greater than ${stat.size - 1}`, 416);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
// 注:Range 和 Content-Range 还有 createReadStream 中的字节范围,都是前后全包含的
|
|
159
|
+
// Content-Range: bytes 42-1233/1234
|
|
160
|
+
response.setHeader('Content-Range', `bytes ${start}-${end}/${stat.size}`);
|
|
161
|
+
return streamFile(filePath, response, { start, end });
|
|
162
|
+
}
|
|
163
|
+
exports.renderFile = renderFile;
|
|
164
|
+
function streamFile(filePath, response, opts) {
|
|
165
|
+
return new Promise((res, rej) => {
|
|
166
|
+
if (opts && typeof opts.start === 'number') {
|
|
167
|
+
// 部分返回 206
|
|
168
|
+
response.statusCode = 206;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
// 全部返回 200
|
|
172
|
+
response.statusCode = 200;
|
|
173
|
+
}
|
|
174
|
+
(0, fs_1.createReadStream)(filePath, opts).pipe(response);
|
|
175
|
+
response.once('finish', res).once('error', rej);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateHtmlCode = void 0;
|
|
4
|
+
function encodeAttrs(attrs) {
|
|
5
|
+
return Object.entries(attrs)
|
|
6
|
+
.map(entry => {
|
|
7
|
+
const [name, value] = entry;
|
|
8
|
+
return { name, value };
|
|
9
|
+
})
|
|
10
|
+
.filter(attr => attr.value !== undefined)
|
|
11
|
+
.map(attr => {
|
|
12
|
+
const { name } = attr;
|
|
13
|
+
const value = attr.value;
|
|
14
|
+
if (name === 'style') {
|
|
15
|
+
if (typeof value === 'string') {
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
const style = value;
|
|
19
|
+
return Object.entries(style)
|
|
20
|
+
.map(entry => `${entry[0]}:${entry[1]}`)
|
|
21
|
+
.join(';');
|
|
22
|
+
}
|
|
23
|
+
if (typeof value === 'boolean' && value) {
|
|
24
|
+
return `${name}`;
|
|
25
|
+
}
|
|
26
|
+
if (typeof value === 'string') {
|
|
27
|
+
return `${name}="${value.replace(/"/g, '"')}"`;
|
|
28
|
+
}
|
|
29
|
+
return `${name}="${value}"`;
|
|
30
|
+
})
|
|
31
|
+
.join(' ');
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 将标签编码
|
|
35
|
+
*/
|
|
36
|
+
function encodeTag(tag) {
|
|
37
|
+
if (tag.selfClosing) {
|
|
38
|
+
if (tag.attrs) {
|
|
39
|
+
return `<${tag.tag} ${encodeAttrs(tag.attrs)}/>`;
|
|
40
|
+
}
|
|
41
|
+
return `<${tag.tag}/>`;
|
|
42
|
+
}
|
|
43
|
+
let html = '';
|
|
44
|
+
html += `<${tag.tag}`;
|
|
45
|
+
if (tag.attrs) {
|
|
46
|
+
html += ` ${encodeAttrs(tag.attrs)}>`;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
html += '>';
|
|
50
|
+
}
|
|
51
|
+
if (tag.children) {
|
|
52
|
+
if (Array.isArray(tag.children)) {
|
|
53
|
+
html += tag.children
|
|
54
|
+
.map(subTag => (typeof subTag === 'string' ? subTag : encodeTag(subTag)))
|
|
55
|
+
.join('');
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
const children = [];
|
|
59
|
+
tag.children((...child) => {
|
|
60
|
+
children.push(...child);
|
|
61
|
+
});
|
|
62
|
+
html += children
|
|
63
|
+
.map(subTag => (typeof subTag === 'string' ? subTag : encodeTag(subTag)))
|
|
64
|
+
.join('');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
html += `</${tag.tag}>`;
|
|
68
|
+
return html;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 生成 html 代码
|
|
72
|
+
* @param html
|
|
73
|
+
*/
|
|
74
|
+
function generateHtmlCode(html) {
|
|
75
|
+
return encodeTag({
|
|
76
|
+
tag: 'html',
|
|
77
|
+
attrs: { lang: html.lang },
|
|
78
|
+
children: [
|
|
79
|
+
{ tag: 'head', children: html.head },
|
|
80
|
+
{
|
|
81
|
+
tag: 'body',
|
|
82
|
+
attrs: Array.isArray(html.body) || typeof html.body === 'function' ? undefined : html.body.attrs,
|
|
83
|
+
children: Array.isArray(html.body) || typeof html.body === 'function'
|
|
84
|
+
? html.body
|
|
85
|
+
: html.body.children
|
|
86
|
+
}
|
|
87
|
+
]
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
exports.generateHtmlCode = generateHtmlCode;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.renderHtml = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const html_1 = require("./html");
|
|
6
|
+
/**
|
|
7
|
+
* 渲染 html
|
|
8
|
+
* @param response
|
|
9
|
+
* @param html
|
|
10
|
+
* @param status
|
|
11
|
+
*/
|
|
12
|
+
function renderHtml(response, html, status = 200) {
|
|
13
|
+
response.statusCode = status;
|
|
14
|
+
response.setHeader('content-type', 'text/html; charset=utf-8');
|
|
15
|
+
response.end(typeof html === 'string' ? html : (0, html_1.generateHtmlCode)(html));
|
|
16
|
+
}
|
|
17
|
+
exports.renderHtml = renderHtml;
|
|
18
|
+
tslib_1.__exportStar(require("./html"), exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./text"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./json"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./file"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./html"), exports);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.renderError = exports.renderJson = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* 渲染 json 格式数据.
|
|
6
|
+
* @param response 响应信息
|
|
7
|
+
* @param json json对象
|
|
8
|
+
* @param status 状态码,默认 200
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
function renderJson(response, json, status = 200) {
|
|
12
|
+
response.setHeader('content-type', 'application/json; charset=UTF-8');
|
|
13
|
+
response.statusCode = status;
|
|
14
|
+
response.end(JSON.stringify(json));
|
|
15
|
+
}
|
|
16
|
+
exports.renderJson = renderJson;
|
|
17
|
+
/**
|
|
18
|
+
* 渲染错误信息,json 格式
|
|
19
|
+
* @param response
|
|
20
|
+
* @param message
|
|
21
|
+
* @param status
|
|
22
|
+
*/
|
|
23
|
+
function renderError(response, message, status = 400) {
|
|
24
|
+
renderJson(response, { message }, status);
|
|
25
|
+
}
|
|
26
|
+
exports.renderError = renderError;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.renderText = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* 渲染 json 格式数据.
|
|
6
|
+
* @param response 响应信息
|
|
7
|
+
* @param text 文本
|
|
8
|
+
* @param status 状态码,默认 200
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
function renderText(response, text, status = 200) {
|
|
12
|
+
response.setHeader('content-type', 'text/plain; charset=UTF-8');
|
|
13
|
+
response.statusCode = status;
|
|
14
|
+
response.end(text);
|
|
15
|
+
}
|
|
16
|
+
exports.renderText = renderText;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.configValidation = exports.defaultConfig = void 0;
|
|
4
|
+
const validation_1 = require("../validation");
|
|
5
|
+
/**
|
|
6
|
+
* 默认配置.
|
|
7
|
+
*/
|
|
8
|
+
exports.defaultConfig = {
|
|
9
|
+
host: 'localhost',
|
|
10
|
+
port: 3306,
|
|
11
|
+
user: 'root',
|
|
12
|
+
password: '123456',
|
|
13
|
+
charset: 'utf8mb4',
|
|
14
|
+
database: 'example',
|
|
15
|
+
versionControlEnabled: false,
|
|
16
|
+
versionControlDir: 'db_migration',
|
|
17
|
+
timezone: '+08:00',
|
|
18
|
+
connectTimeout: 10000,
|
|
19
|
+
debug: false,
|
|
20
|
+
connectionLimit: 10,
|
|
21
|
+
maxIdle: 10,
|
|
22
|
+
idleTimeout: 60000,
|
|
23
|
+
slowSqlWarn: true,
|
|
24
|
+
slowSqlMs: 200,
|
|
25
|
+
transactionTimeout: 5000,
|
|
26
|
+
transactionStrict: true
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* 配置校验规则.
|
|
30
|
+
*/
|
|
31
|
+
exports.configValidation = {
|
|
32
|
+
host: [(0, validation_1.notBlank)()],
|
|
33
|
+
port: [(0, validation_1.notNull)(), (0, validation_1.min)(80), (0, validation_1.max)(65535)],
|
|
34
|
+
user: [(0, validation_1.notBlank)()],
|
|
35
|
+
password: [(0, validation_1.notBlank)()],
|
|
36
|
+
charset: [(0, validation_1.notBlank)()],
|
|
37
|
+
database: [(0, validation_1.notBlank)()],
|
|
38
|
+
versionControlEnabled: [(0, validation_1.notNull)()],
|
|
39
|
+
versionControlDir: [(0, validation_1.notNull)()],
|
|
40
|
+
timezone: [(0, validation_1.notBlank)()],
|
|
41
|
+
connectTimeout: [(0, validation_1.notNull)(), (0, validation_1.min)(1000), (0, validation_1.max)(60000)],
|
|
42
|
+
connectionLimit: [(0, validation_1.notNull)(), (0, validation_1.min)(1), (0, validation_1.max)(999)],
|
|
43
|
+
maxIdle: [(0, validation_1.notNull)(), (0, validation_1.min)(1), (0, validation_1.max)(999)],
|
|
44
|
+
idleTimeout: [(0, validation_1.notNull)(), (0, validation_1.min)(1000), (0, validation_1.max)(60000)],
|
|
45
|
+
slowSqlWarn: [(0, validation_1.notNull)()],
|
|
46
|
+
slowSqlMs: [(0, validation_1.notNull)(), (0, validation_1.min)(1), (0, validation_1.max)(3600000)],
|
|
47
|
+
transactionTimeout: [(0, validation_1.notNull)(), (0, validation_1.min)(0), (0, validation_1.max)(60000)],
|
|
48
|
+
transactionStrict: [(0, validation_1.notNull)()]
|
|
49
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MysqlException = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* mysql 异常.
|
|
6
|
+
*/
|
|
7
|
+
class MysqlException extends Error {
|
|
8
|
+
message;
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.message = message;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.MysqlException = MysqlException;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MysqlException = exports.MysqlCriteria = exports.getMysqlManager = exports.enableMysql = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const mysql2_1 = require("mysql2");
|
|
6
|
+
const config_1 = require("../config");
|
|
7
|
+
const config_2 = require("./config");
|
|
8
|
+
const exception_1 = require("./exception");
|
|
9
|
+
Object.defineProperty(exports, "MysqlException", { enumerable: true, get: function () { return exception_1.MysqlException; } });
|
|
10
|
+
const manager_1 = require("./manager");
|
|
11
|
+
const criteria_1 = require("./manager/ops/criteria");
|
|
12
|
+
Object.defineProperty(exports, "MysqlCriteria", { enumerable: true, get: function () { return criteria_1.MysqlCriteria; } });
|
|
13
|
+
const migration_1 = require("./migration");
|
|
14
|
+
const managerMap = new Map();
|
|
15
|
+
const defaultName = 'mysql';
|
|
16
|
+
/**
|
|
17
|
+
* 启用mysql
|
|
18
|
+
* @param name 配置名称,同时也是环境变量配置的前缀,英文开头,由英文数字和下划线成.
|
|
19
|
+
*/
|
|
20
|
+
async function enableMysql(name) {
|
|
21
|
+
// 校验名称
|
|
22
|
+
if (name) {
|
|
23
|
+
if (!/^[a-zA-Z][a-zA-Z0-9_]{0,31}$/.test(name)) {
|
|
24
|
+
throw new Error(`mysql 配置名称必须是英文开头,由英文数字和下划线成,并且不得超过 32 位:${name}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const finalName = name || defaultName;
|
|
28
|
+
// 连接池配置
|
|
29
|
+
const mysqlConfig = (0, config_1.registerConfig)(config_2.defaultConfig, finalName, config_2.configValidation);
|
|
30
|
+
// 数据库版本管理
|
|
31
|
+
if (mysqlConfig.versionControlEnabled) {
|
|
32
|
+
// 创建一个支持多语句的连接,专门处理迁移
|
|
33
|
+
const conn = await new Promise((resolve, reject) => {
|
|
34
|
+
const conn = (0, mysql2_1.createConnection)({
|
|
35
|
+
host: mysqlConfig.host,
|
|
36
|
+
port: mysqlConfig.port,
|
|
37
|
+
user: mysqlConfig.user,
|
|
38
|
+
password: mysqlConfig.password,
|
|
39
|
+
charset: mysqlConfig.charset,
|
|
40
|
+
database: mysqlConfig.database,
|
|
41
|
+
connectTimeout: mysqlConfig.connectTimeout,
|
|
42
|
+
multipleStatements: true
|
|
43
|
+
});
|
|
44
|
+
conn.connect(err => (err ? reject(err) : resolve(conn)));
|
|
45
|
+
});
|
|
46
|
+
try {
|
|
47
|
+
await (0, migration_1.migrate)(mysqlConfig, conn);
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
await new Promise((resolve, reject) => {
|
|
51
|
+
conn.end(err => (err ? reject(err) : resolve()));
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const pool = (0, mysql2_1.createPool)({
|
|
56
|
+
host: mysqlConfig.host,
|
|
57
|
+
port: mysqlConfig.port,
|
|
58
|
+
user: mysqlConfig.user,
|
|
59
|
+
password: mysqlConfig.password,
|
|
60
|
+
charset: mysqlConfig.charset,
|
|
61
|
+
database: mysqlConfig.database,
|
|
62
|
+
connectTimeout: mysqlConfig.connectTimeout,
|
|
63
|
+
debug: mysqlConfig.debug,
|
|
64
|
+
connectionLimit: mysqlConfig.connectionLimit,
|
|
65
|
+
maxIdle: mysqlConfig.maxIdle,
|
|
66
|
+
idleTimeout: mysqlConfig.idleTimeout
|
|
67
|
+
});
|
|
68
|
+
process.addListener('beforeExit', () => pool.end());
|
|
69
|
+
managerMap.set(finalName, new manager_1.MysqlManager(mysqlConfig, pool));
|
|
70
|
+
}
|
|
71
|
+
exports.enableMysql = enableMysql;
|
|
72
|
+
/**
|
|
73
|
+
* 获取管理器.
|
|
74
|
+
* @param name 配置名称,如果调用 enableMysql 时填写了,那么这里也要填写并保持一致
|
|
75
|
+
*/
|
|
76
|
+
function getMysqlManager(name) {
|
|
77
|
+
const manager = managerMap.get(name || defaultName);
|
|
78
|
+
if (!manager) {
|
|
79
|
+
throw new Error(`找不到想要的 mysql 配置 ${name ? ': ' + name : ''},请先调用 enableMysql 启用`);
|
|
80
|
+
}
|
|
81
|
+
return manager;
|
|
82
|
+
}
|
|
83
|
+
exports.getMysqlManager = getMysqlManager;
|
|
84
|
+
// 部分需要导出的对象
|
|
85
|
+
tslib_1.__exportStar(require("./manager"), exports);
|