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,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExtensibleI18n = exports.I18n = exports.BoundI18n = void 0;
|
|
4
|
+
const tag_1 = require("./tag");
|
|
5
|
+
/**
|
|
6
|
+
* 绑定语言的 i18n 类型,不可切换语言.
|
|
7
|
+
*/
|
|
8
|
+
class BoundI18n {
|
|
9
|
+
lang;
|
|
10
|
+
msgs;
|
|
11
|
+
constructor(lang, msgs) {
|
|
12
|
+
this.lang = lang;
|
|
13
|
+
this.msgs = msgs;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 获取当前语言
|
|
17
|
+
*/
|
|
18
|
+
getLang() {
|
|
19
|
+
return this.lang;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 构建国际化消息.
|
|
23
|
+
* @param key 消息模板的 key
|
|
24
|
+
* @param args 参数,如果 key 对应的消息模板有占位符号({})可以使用参数来填充
|
|
25
|
+
*/
|
|
26
|
+
buildMsg(key, ...args) {
|
|
27
|
+
const template = this.msgs[key];
|
|
28
|
+
if (!args || !args.length) {
|
|
29
|
+
return template;
|
|
30
|
+
}
|
|
31
|
+
let idx = 0;
|
|
32
|
+
return template.replace(/\{\}/g, () => `${args[idx++]}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.BoundI18n = BoundI18n;
|
|
36
|
+
/**
|
|
37
|
+
* 国际化,提供不同语言消息构建的功能,默认英文.
|
|
38
|
+
*/
|
|
39
|
+
class I18n {
|
|
40
|
+
enMsgs;
|
|
41
|
+
/**
|
|
42
|
+
* 当前语言
|
|
43
|
+
*/
|
|
44
|
+
lang;
|
|
45
|
+
/**
|
|
46
|
+
* 消息模板表,与当前语言是对应的
|
|
47
|
+
*/
|
|
48
|
+
msgs;
|
|
49
|
+
/**
|
|
50
|
+
* 消息表,key 是语言,value 是一个 key 为地区,value 为 消息模板的 map,两层 map 为的是能够快速匹配.
|
|
51
|
+
* 地区为空的情况下,缺省值为 - 。语言和地区的 key 全转成小写存储。
|
|
52
|
+
*/
|
|
53
|
+
msgsMap;
|
|
54
|
+
/**
|
|
55
|
+
* 构造 i18n 对象,必须指定英文的消息模板作为默认。
|
|
56
|
+
* @param enMsgs 英文的消息模板
|
|
57
|
+
*/
|
|
58
|
+
constructor(enMsgs) {
|
|
59
|
+
this.enMsgs = enMsgs;
|
|
60
|
+
const regionMap = new Map();
|
|
61
|
+
regionMap.set('-', enMsgs);
|
|
62
|
+
this.msgsMap = new Map();
|
|
63
|
+
this.msgsMap.set('en', regionMap);
|
|
64
|
+
this.lang = 'en';
|
|
65
|
+
this.msgs = enMsgs;
|
|
66
|
+
}
|
|
67
|
+
findMsgsByLang(langTag) {
|
|
68
|
+
// 检查表中是否支持指定语言,如果不支持则使用默认语言
|
|
69
|
+
const tag = (0, tag_1.parseLangTag)(langTag);
|
|
70
|
+
const regionMap = this.msgsMap.get(tag.lang);
|
|
71
|
+
if (!regionMap) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
// 查找顺序:
|
|
75
|
+
// 1. 如果有地区信息,查找地区标签对应的消息
|
|
76
|
+
// 2. 上一步没有找到,则查找默认地区标签对应的消息
|
|
77
|
+
// 3. 前面都没有找到,则取语言下的第一个地区的消息
|
|
78
|
+
let msgs = undefined;
|
|
79
|
+
if (tag.region) {
|
|
80
|
+
msgs = regionMap.get(tag.region);
|
|
81
|
+
}
|
|
82
|
+
if (!msgs) {
|
|
83
|
+
msgs = regionMap.get('-');
|
|
84
|
+
}
|
|
85
|
+
if (!msgs) {
|
|
86
|
+
msgs = regionMap.values().next().value;
|
|
87
|
+
}
|
|
88
|
+
return msgs;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 设置一种语言对应的消息模板,支持异步,可以做动态加载
|
|
92
|
+
* @param langTag
|
|
93
|
+
* @param msgs
|
|
94
|
+
*/
|
|
95
|
+
setMsgs(langTag, msgs) {
|
|
96
|
+
const tag = (0, tag_1.parseLangTag)(langTag);
|
|
97
|
+
let regionMap = this.msgsMap.get(tag.lang);
|
|
98
|
+
if (!regionMap) {
|
|
99
|
+
regionMap = new Map();
|
|
100
|
+
this.msgsMap.set(tag.lang, regionMap);
|
|
101
|
+
}
|
|
102
|
+
regionMap.set(tag.region || '-', msgs);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* 给定一个语言标签列表,获得可以被支持的标签列表
|
|
106
|
+
* @param tags
|
|
107
|
+
*/
|
|
108
|
+
getSupportedLanguageTags(...tags) {
|
|
109
|
+
return tags.filter(tag => {
|
|
110
|
+
const langTag = (0, tag_1.parseLangTag)(tag);
|
|
111
|
+
return this.msgsMap.get(langTag.lang);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* 设置当前语言,如果指定的语言不被支持,则修改无效
|
|
116
|
+
* @param lang 语言本标签,如 en-US
|
|
117
|
+
* @returns 是否成功,如果语言不支持则返回 false,不会改变现有语言
|
|
118
|
+
*/
|
|
119
|
+
setLang(lang) {
|
|
120
|
+
const msgs = this.findMsgsByLang(lang);
|
|
121
|
+
if (!msgs) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
this.lang = lang;
|
|
125
|
+
this.msgs = msgs;
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 获取当前语言
|
|
130
|
+
*/
|
|
131
|
+
getLang() {
|
|
132
|
+
return this.lang;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 构建国际化消息.
|
|
136
|
+
* @param key 消息模板的 key
|
|
137
|
+
* @param args 参数,如果 key 对应的消息模板有占位符号({})可以使用参数来填充
|
|
138
|
+
*/
|
|
139
|
+
buildMsg(key, ...args) {
|
|
140
|
+
const template = this.msgs[key];
|
|
141
|
+
if (!args || !args.length) {
|
|
142
|
+
return template;
|
|
143
|
+
}
|
|
144
|
+
let idx = 0;
|
|
145
|
+
return template.replace(/\{\}/g, () => `${args[idx++]}`);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* 绑定一种语言,生成新的 i18n 对象,该对象仅能使用某一种语言.
|
|
149
|
+
* @param langTag 语言标签,无值则表示绑定当前语言
|
|
150
|
+
* @returns 返回一个语言受限的 i18n 对象.
|
|
151
|
+
*/
|
|
152
|
+
bindLang(langTag) {
|
|
153
|
+
if (!langTag) {
|
|
154
|
+
return new BoundI18n(this.lang, this.msgs);
|
|
155
|
+
}
|
|
156
|
+
const msgs = this.findMsgsByLang(langTag);
|
|
157
|
+
if (!msgs) {
|
|
158
|
+
throw new Error(`Unsupported language : ${langTag}`);
|
|
159
|
+
}
|
|
160
|
+
return new BoundI18n(langTag, msgs);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* 根据请求进行语言切换,解析消息头 'accept-language' ,如果 'accept-language' 中没有包含能够支持的语言则使用默认的英文.
|
|
164
|
+
* @param headers 消息头
|
|
165
|
+
*/
|
|
166
|
+
switchByRequest(headers) {
|
|
167
|
+
const langs = this.parseAcceptLanguage(headers);
|
|
168
|
+
const tags = this.getSupportedLanguageTags(...langs);
|
|
169
|
+
if (tags.length) {
|
|
170
|
+
this.setLang(tags[0]);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
this.setLang('en');
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
parseAcceptLanguage(headers) {
|
|
177
|
+
const acceptLang = headers['accept-language'];
|
|
178
|
+
if (!acceptLang) {
|
|
179
|
+
return [];
|
|
180
|
+
}
|
|
181
|
+
// zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
|
|
182
|
+
// 为了性能考虑,暂不支持 q 参数设定的优先级,否则还要排序
|
|
183
|
+
const tags = acceptLang.split(',');
|
|
184
|
+
const langs = [];
|
|
185
|
+
for (const tag of tags) {
|
|
186
|
+
const [t, q] = tag.split(';');
|
|
187
|
+
if ('*' !== t) {
|
|
188
|
+
langs.push(t);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return langs;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* 根据请求进行语言绑定,解析消息头 'accept-language',绑定受支持的第一个语言,如果没有可以被支持的语言,则使用默认的英文.
|
|
195
|
+
* @param headers 消息头
|
|
196
|
+
*/
|
|
197
|
+
bindByRequest(headers) {
|
|
198
|
+
const langs = this.parseAcceptLanguage(headers);
|
|
199
|
+
const tags = this.getSupportedLanguageTags(...langs);
|
|
200
|
+
if (tags.length) {
|
|
201
|
+
return this.bindLang(tags[0]);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
return this.bindLang('en');
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
exports.I18n = I18n;
|
|
209
|
+
/**
|
|
210
|
+
* 可扩展的 i18n ,增加扩展新 i18n 对象的功能,记录扩展的新对象,同步 setLang 操作
|
|
211
|
+
*/
|
|
212
|
+
class ExtensibleI18n extends I18n {
|
|
213
|
+
extendedI18ns = [];
|
|
214
|
+
setLang(lang) {
|
|
215
|
+
for (const ex of this.extendedI18ns) {
|
|
216
|
+
ex.setLang(lang);
|
|
217
|
+
}
|
|
218
|
+
return super.setLang(lang);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* 扩展国际化内容,生成一个新的 i18n 对象,与当前的 i18n 对象语言保持一致,可在新对象中设置扩展部分的语言。
|
|
222
|
+
* <K> 新的消息模板类型
|
|
223
|
+
* @param enMsgs
|
|
224
|
+
*/
|
|
225
|
+
extend(enMsgs) {
|
|
226
|
+
const extendedI18n = new I18n(enMsgs);
|
|
227
|
+
this.extendedI18ns.push(extendedI18n);
|
|
228
|
+
return extendedI18n;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
exports.ExtensibleI18n = ExtensibleI18n;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getI18n = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const ar_1 = require("./ar");
|
|
6
|
+
const de_1 = require("./de");
|
|
7
|
+
const en_us_1 = require("./en-us");
|
|
8
|
+
const es_1 = require("./es");
|
|
9
|
+
const fr_1 = require("./fr");
|
|
10
|
+
const i18n_1 = require("./i18n");
|
|
11
|
+
const ja_1 = require("./ja");
|
|
12
|
+
const ko_1 = require("./ko");
|
|
13
|
+
const pt_1 = require("./pt");
|
|
14
|
+
const ru_1 = require("./ru");
|
|
15
|
+
const zh_HK_1 = require("./zh-HK");
|
|
16
|
+
const zh_TW_1 = require("./zh-TW");
|
|
17
|
+
const zh_cn_1 = require("./zh-cn");
|
|
18
|
+
let I18N;
|
|
19
|
+
/**
|
|
20
|
+
* 获取 i18n 对象
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
function getI18n() {
|
|
24
|
+
if (I18N) {
|
|
25
|
+
return I18N;
|
|
26
|
+
}
|
|
27
|
+
// 如果 i18n 对象不存在,则创建,并进行初始化
|
|
28
|
+
I18N = new i18n_1.ExtensibleI18n(en_us_1.enUS);
|
|
29
|
+
I18N.setMsgs('zh-CN', zh_cn_1.zhCN);
|
|
30
|
+
I18N.setMsgs('zh-TW', zh_TW_1.zhTW);
|
|
31
|
+
I18N.setMsgs('zh-HK', zh_HK_1.zhHK);
|
|
32
|
+
I18N.setMsgs('ja', ja_1.ja);
|
|
33
|
+
I18N.setMsgs('ko', ko_1.ko);
|
|
34
|
+
I18N.setMsgs('es', es_1.es);
|
|
35
|
+
I18N.setMsgs('de', de_1.de);
|
|
36
|
+
I18N.setMsgs('fr', fr_1.fr);
|
|
37
|
+
I18N.setMsgs('ar', ar_1.ar);
|
|
38
|
+
I18N.setMsgs('pt', pt_1.pt);
|
|
39
|
+
I18N.setMsgs('ru', ru_1.ru);
|
|
40
|
+
// 当前语言
|
|
41
|
+
if (process.env.LANG) {
|
|
42
|
+
I18N.setLang(process.env.LANG);
|
|
43
|
+
}
|
|
44
|
+
else if (process.env.LC_CTYPE) {
|
|
45
|
+
const [tag] = process.env.LC_CTYPE.split('.');
|
|
46
|
+
I18N.setLang(tag);
|
|
47
|
+
}
|
|
48
|
+
return I18N;
|
|
49
|
+
}
|
|
50
|
+
exports.getI18n = getI18n;
|
|
51
|
+
tslib_1.__exportStar(require("./i18n"), exports);
|
|
52
|
+
tslib_1.__exportStar(require("./msg"), exports);
|
package/dist/i18n/ja.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ja = void 0;
|
|
4
|
+
exports.ja = {
|
|
5
|
+
'validate-err-array': '値が配列ではありません',
|
|
6
|
+
'validate-err-enum': '値は {} の中のいずれかでなければなりません',
|
|
7
|
+
'validate-err-numer': '値が数字ではありません',
|
|
8
|
+
'validate-err-max': '{} 以下である必要があります',
|
|
9
|
+
'validate-err-min': '{} 以上である必要があります',
|
|
10
|
+
'validate-err-empty': '空であってはいけません',
|
|
11
|
+
'validate-err-string': '値が文字列のタイプではありません',
|
|
12
|
+
'validate-err-incorrect-format': '形式が正しくありません',
|
|
13
|
+
'validate-err-no-length': 'length属性が見当たりません',
|
|
14
|
+
'validate-err-length-not-number': 'lengthプロパティは数値ではありません',
|
|
15
|
+
'validate-err-min-length': '長さは {} 以上でなければりません',
|
|
16
|
+
'validate-err-max-length': '長さは {} 以下でなければなりません'
|
|
17
|
+
};
|
package/dist/i18n/ko.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ko = void 0;
|
|
4
|
+
exports.ko = {
|
|
5
|
+
'validate-err-array': '값은 배열이 아닙니다',
|
|
6
|
+
'validate-err-enum': '값은 {} 중 하나여야 합니다',
|
|
7
|
+
'validate-err-numer': '값은 숫자가 아닙니다',
|
|
8
|
+
'validate-err-max': '{}보 크면 안 됩니다',
|
|
9
|
+
'validate-err-min': '{}보다 작으면 안 됩니다',
|
|
10
|
+
'validate-err-empty': '비워둘 수 없습니다',
|
|
11
|
+
'validate-err-string': '값이 문자열 유형이 아닙니다',
|
|
12
|
+
'validate-err-incorrect-format': '형식이 올바르지 않습니다',
|
|
13
|
+
'validate-err-no-length': 'length 속성을 찾을 수 없습니다',
|
|
14
|
+
'validate-err-length-not-number': 'length 속성이 숫자가 아닙니다',
|
|
15
|
+
'validate-err-min-length': '길이는 {}보다 작을 수 없습니다',
|
|
16
|
+
'validate-err-max-length': '길이는 {}보다 클 수 없습니다'
|
|
17
|
+
};
|
package/dist/i18n/msg.js
ADDED
package/dist/i18n/pt.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pt = void 0;
|
|
4
|
+
exports.pt = {
|
|
5
|
+
'validate-err-array': 'O valor não é um array',
|
|
6
|
+
'validate-err-enum': 'O valor deve ser um dos {}',
|
|
7
|
+
'validate-err-numer': 'O valor não é um número',
|
|
8
|
+
'validate-err-max': 'Não pode ser maior que {}',
|
|
9
|
+
'validate-err-min': 'Não pode ser menor que {}',
|
|
10
|
+
'validate-err-empty': 'Não pode ser vazio',
|
|
11
|
+
'validate-err-string': 'O valor não é do tipo string',
|
|
12
|
+
'validate-err-incorrect-format': 'Formato incorreto',
|
|
13
|
+
'validate-err-no-length': 'Não foi possível encontrar a propriedade "length"',
|
|
14
|
+
'validate-err-length-not-number': 'A propriedade "length" não é um número',
|
|
15
|
+
'validate-err-min-length': 'O comprimento não pode ser menor que {}',
|
|
16
|
+
'validate-err-max-length': 'O comprimento não pode ser maior que {}'
|
|
17
|
+
};
|
package/dist/i18n/ru.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ru = void 0;
|
|
4
|
+
exports.ru = {
|
|
5
|
+
'validate-err-array': 'Значение не является массивом',
|
|
6
|
+
'validate-err-enum': 'Значение должно быть одним из {}',
|
|
7
|
+
'validate-err-numer': 'Значение не является числом',
|
|
8
|
+
'validate-err-max': 'Не должно быть больше {}',
|
|
9
|
+
'validate-err-min': 'Не должно быть меньше {}',
|
|
10
|
+
'validate-err-empty': 'не может быть пустым',
|
|
11
|
+
'validate-err-string': 'Значение не является строковым типом',
|
|
12
|
+
'validate-err-incorrect-format': 'Неправильный формат',
|
|
13
|
+
'validate-err-no-length': 'Никаких признаков length',
|
|
14
|
+
'validate-err-length-not-number': 'Атрибут длины не является числом',
|
|
15
|
+
'validate-err-min-length': 'Длина не должна быть меньше {}',
|
|
16
|
+
'validate-err-max-length': 'Длина не должна быть больше {}'
|
|
17
|
+
};
|
package/dist/i18n/tag.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseLangTag = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* 解析语言标签,语言标签格式非常多,这里仅支持语言加地区的形式,如 zh-CN。
|
|
6
|
+
* 更多信息可以查看 https://www.rfc-editor.org/rfc/bcp/bcp47.txt 。
|
|
7
|
+
* @param tag
|
|
8
|
+
* @returns 语言和地区信息,为了方便匹配,全转小写
|
|
9
|
+
*/
|
|
10
|
+
function parseLangTag(tag) {
|
|
11
|
+
const [lang, region] = tag.split('-');
|
|
12
|
+
// 解析失败,抛出错误
|
|
13
|
+
if (!lang) {
|
|
14
|
+
throw new Error(`Unable to parse lang tag:${tag}`);
|
|
15
|
+
}
|
|
16
|
+
return { lang: lang.toLowerCase(), region: region ? region.toLowerCase() : undefined };
|
|
17
|
+
}
|
|
18
|
+
exports.parseLangTag = parseLangTag;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.zhHK = void 0;
|
|
4
|
+
exports.zhHK = {
|
|
5
|
+
'validate-err-array': '值不是陣列',
|
|
6
|
+
'validate-err-enum': '值必須是 {} 其中一個',
|
|
7
|
+
'validate-err-numer': '值不是數字',
|
|
8
|
+
'validate-err-max': '不得大於 {}',
|
|
9
|
+
'validate-err-min': '不得小於 {}',
|
|
10
|
+
'validate-err-empty': '不能為空',
|
|
11
|
+
'validate-err-string': '值不是字串型別',
|
|
12
|
+
'validate-err-incorrect-format': '格式不正確',
|
|
13
|
+
'validate-err-no-length': '找不到 length 屬性',
|
|
14
|
+
'validate-err-length-not-number': 'length 屬性不是數字',
|
|
15
|
+
'validate-err-min-length': '長度不得小於 {}',
|
|
16
|
+
'validate-err-max-length': '長度不得大於 {}'
|
|
17
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.zhTW = void 0;
|
|
4
|
+
exports.zhTW = {
|
|
5
|
+
'validate-err-array': '值不是陣列',
|
|
6
|
+
'validate-err-enum': '值必須是 {} 其中一個',
|
|
7
|
+
'validate-err-numer': '值不是數字',
|
|
8
|
+
'validate-err-max': '不得大於 {}',
|
|
9
|
+
'validate-err-min': '不得小於 {}',
|
|
10
|
+
'validate-err-empty': '不能為空',
|
|
11
|
+
'validate-err-string': '值不是字串型別',
|
|
12
|
+
'validate-err-incorrect-format': '格式不正確',
|
|
13
|
+
'validate-err-no-length': '找不到 length 屬性',
|
|
14
|
+
'validate-err-length-not-number': 'length 屬性不是數字',
|
|
15
|
+
'validate-err-min-length': '長度不得小於 {}',
|
|
16
|
+
'validate-err-max-length': '長度不得大於 {}'
|
|
17
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.zhCN = void 0;
|
|
4
|
+
exports.zhCN = {
|
|
5
|
+
'validate-err-array': '值不是数组',
|
|
6
|
+
'validate-err-enum': '值必须是 {} 其中一个',
|
|
7
|
+
'validate-err-numer': '值不是数字',
|
|
8
|
+
'validate-err-max': '不得大于 {}',
|
|
9
|
+
'validate-err-min': '不得小于 {}',
|
|
10
|
+
'validate-err-empty': '不能为空',
|
|
11
|
+
'validate-err-string': '值不是字符串类型',
|
|
12
|
+
'validate-err-incorrect-format': '格式不正确',
|
|
13
|
+
'validate-err-no-length': '找不到 length 属性',
|
|
14
|
+
'validate-err-length-not-number': 'length 属性不是数字',
|
|
15
|
+
'validate-err-min-length': '长度不得小于 {}',
|
|
16
|
+
'validate-err-max-length': '长度不得大于 {}'
|
|
17
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./log"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./i18n"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./validation"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./config"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./cache"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./task"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./mvc"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./mysql"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./http-client"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./mongodb"), exports);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.config = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const level_1 = require("./level");
|
|
6
|
+
const dotenv_1 = require("dotenv");
|
|
7
|
+
(0, dotenv_1.config)();
|
|
8
|
+
/**
|
|
9
|
+
* 默认配置信息.
|
|
10
|
+
*/
|
|
11
|
+
const config = {
|
|
12
|
+
file: process.env.LOG_FILE === 'true',
|
|
13
|
+
fileDir: process.env.LOG_FILE_DIR || 'logs',
|
|
14
|
+
fileMaxDays: 30,
|
|
15
|
+
level: (0, level_1.parseLogLevel)(process.env.LOG_LEVEL || 'info')
|
|
16
|
+
};
|
|
17
|
+
exports.config = config;
|
|
18
|
+
// 目录处理
|
|
19
|
+
if (!(0, path_1.isAbsolute)(config.fileDir)) {
|
|
20
|
+
config.fileDir = (0, path_1.resolve)(process.cwd(), config.fileDir);
|
|
21
|
+
}
|
|
22
|
+
// 天数
|
|
23
|
+
if (process.env.LOG_FILE_MAX_DAYS) {
|
|
24
|
+
const days = parseInt(process.env.LOG_FILE_MAX_DAYS);
|
|
25
|
+
if (!isNaN(days)) {
|
|
26
|
+
config.fileMaxDays = days;
|
|
27
|
+
}
|
|
28
|
+
}
|
package/dist/log/date.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatDateTime = exports.currentDateTimeStr = void 0;
|
|
4
|
+
function currentDateTimeStr() {
|
|
5
|
+
return formatDateTime(new Date());
|
|
6
|
+
}
|
|
7
|
+
exports.currentDateTimeStr = currentDateTimeStr;
|
|
8
|
+
function formatDateTime(date) {
|
|
9
|
+
return (date.toLocaleString('zh-CN', {
|
|
10
|
+
timeZone: 'Asia/Shanghai',
|
|
11
|
+
year: 'numeric',
|
|
12
|
+
month: '2-digit',
|
|
13
|
+
day: '2-digit',
|
|
14
|
+
hour: '2-digit',
|
|
15
|
+
minute: '2-digit',
|
|
16
|
+
second: '2-digit',
|
|
17
|
+
hour12: false,
|
|
18
|
+
hourCycle: 'h23'
|
|
19
|
+
}) + `.${date.getMilliseconds()}`);
|
|
20
|
+
}
|
|
21
|
+
exports.formatDateTime = formatDateTime;
|
package/dist/log/file.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fileStore = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const promises_1 = require("fs/promises");
|
|
6
|
+
const os_1 = require("os");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const task_1 = require("../task");
|
|
9
|
+
const config_1 = require("./config");
|
|
10
|
+
let QUEUE = [];
|
|
11
|
+
/**
|
|
12
|
+
* 文件存储.
|
|
13
|
+
* @param log
|
|
14
|
+
*/
|
|
15
|
+
function fileStore(log) {
|
|
16
|
+
QUEUE.push(log);
|
|
17
|
+
setTimeout(() => {
|
|
18
|
+
try {
|
|
19
|
+
write();
|
|
20
|
+
}
|
|
21
|
+
catch (e) {
|
|
22
|
+
console.error('Writing log file failed', e);
|
|
23
|
+
}
|
|
24
|
+
}, 0);
|
|
25
|
+
}
|
|
26
|
+
exports.fileStore = fileStore;
|
|
27
|
+
function buildFilePath() {
|
|
28
|
+
const date = new Date();
|
|
29
|
+
let fileName = `${date.getFullYear()}-`;
|
|
30
|
+
const month = date.getMonth() + 1;
|
|
31
|
+
fileName += month.toFixed(0).padStart(2, '0');
|
|
32
|
+
fileName += '-';
|
|
33
|
+
const day = date.getDate();
|
|
34
|
+
fileName += day.toFixed(0).padStart(2, '0');
|
|
35
|
+
fileName += '.log';
|
|
36
|
+
return (0, path_1.resolve)(config_1.config.fileDir, fileName);
|
|
37
|
+
}
|
|
38
|
+
function write() {
|
|
39
|
+
if (!QUEUE.length) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const path = buildFilePath();
|
|
43
|
+
const dir = (0, path_1.dirname)(path);
|
|
44
|
+
if (!(0, fs_1.existsSync)(dir)) {
|
|
45
|
+
(0, fs_1.mkdirSync)(dir);
|
|
46
|
+
}
|
|
47
|
+
const lines = QUEUE.join(os_1.EOL);
|
|
48
|
+
(0, fs_1.appendFileSync)(path, lines);
|
|
49
|
+
QUEUE = [];
|
|
50
|
+
}
|
|
51
|
+
if (config_1.config.file) {
|
|
52
|
+
/**
|
|
53
|
+
* 清理任务.
|
|
54
|
+
*/
|
|
55
|
+
(0, task_1.scheduleDailyTask)(3, 0, {
|
|
56
|
+
name: '日志文件清理',
|
|
57
|
+
async run() {
|
|
58
|
+
const files = await (0, promises_1.readdir)(config_1.config.fileDir);
|
|
59
|
+
const now = new Date().getTime();
|
|
60
|
+
for (const file of files) {
|
|
61
|
+
const dotIdx = file.indexOf('.');
|
|
62
|
+
if (dotIdx === -1) {
|
|
63
|
+
console.warn(`无法处理日志文件 ${file}`);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const dateStr = file.substring(0, dotIdx);
|
|
67
|
+
const timestamp = Date.parse(dateStr);
|
|
68
|
+
if (isNaN(timestamp)) {
|
|
69
|
+
console.warn(`无法处理日志文件 ${file}`);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (timestamp + config_1.config.fileMaxDays * 24 * 3600 * 1000 < now) {
|
|
73
|
+
console.warn(`清理掉日志文件: ${file}`);
|
|
74
|
+
await (0, promises_1.rm)((0, path_1.resolve)(config_1.config.fileDir, file));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|