@jayfong/x-server 2.12.7 → 2.12.15
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/lib/_cjs/cli/api_generator.js +20 -54
- package/lib/_cjs/cli/build_util.js +16 -45
- package/lib/_cjs/cli/cli.js +6 -27
- package/lib/_cjs/cli/deploy_util.js +0 -12
- package/lib/_cjs/cli/env_util.js +16 -49
- package/lib/_cjs/cli/template_util.js +26 -29
- package/lib/_cjs/core/define_bus.js +9 -6
- package/lib/_cjs/core/define_cron.js +0 -2
- package/lib/_cjs/core/define_handler.js +10 -12
- package/lib/_cjs/core/define_hook.js +0 -2
- package/lib/_cjs/core/define_server.js +0 -2
- package/lib/_cjs/core/define_task.js +7 -13
- package/lib/_cjs/core/get_handler_url.js +1 -2
- package/lib/_cjs/core/handler.js +8 -29
- package/lib/_cjs/core/http_error.js +0 -3
- package/lib/_cjs/core/http_header.js +8 -12
- package/lib/_cjs/core/server.js +8 -41
- package/lib/_cjs/core/types.js +0 -7
- package/lib/_cjs/index.js +0 -72
- package/lib/_cjs/plugins/cors.js +0 -7
- package/lib/_cjs/plugins/file_parser.js +2 -9
- package/lib/_cjs/plugins/form_body_parser.js +0 -6
- package/lib/_cjs/plugins/ws_parser.js +0 -6
- package/lib/_cjs/plugins/xml_parser.js +0 -12
- package/lib/_cjs/services/cache.js +18 -58
- package/lib/_cjs/services/captcha.js +0 -14
- package/lib/_cjs/services/dingtalk.js +0 -14
- package/lib/_cjs/services/dispose.js +0 -9
- package/lib/_cjs/services/emoji.js +2 -5
- package/lib/_cjs/services/jwt.js +0 -21
- package/lib/_cjs/services/log.js +0 -17
- package/lib/_cjs/services/mail.js +0 -9
- package/lib/_cjs/services/pay.js +9 -40
- package/lib/_cjs/services/rate_limit.js +0 -12
- package/lib/_cjs/services/redis.js +0 -4
- package/lib/_cjs/services/request.js +14 -299
- package/lib/_cjs/services/sensitive_words.js +13 -19
- package/lib/_cjs/x.js +0 -11
- package/lib/cli/api_generator.js +20 -42
- package/lib/cli/build_util.js +16 -19
- package/lib/cli/cli.js +6 -12
- package/lib/cli/deploy_util.js +0 -4
- package/lib/cli/env_util.js +16 -37
- package/lib/cli/template_util.d.ts +1 -0
- package/lib/cli/template_util.js +26 -14
- package/lib/core/define_bus.js +7 -5
- package/lib/core/define_handler.js +10 -9
- package/lib/core/define_task.js +7 -6
- package/lib/core/get_handler_url.js +2 -1
- package/lib/core/handler.js +9 -22
- package/lib/core/http_header.js +6 -9
- package/lib/core/server.js +8 -31
- package/lib/core/types.js +0 -7
- package/lib/index.js +4 -3
- package/lib/plugins/cors.js +0 -3
- package/lib/plugins/file_parser.js +2 -5
- package/lib/plugins/form_body_parser.js +0 -3
- package/lib/plugins/ws_parser.js +0 -3
- package/lib/plugins/xml_parser.js +0 -10
- package/lib/services/cache.js +18 -52
- package/lib/services/captcha.js +0 -7
- package/lib/services/dingtalk.js +0 -7
- package/lib/services/dispose.js +0 -5
- package/lib/services/emoji.js +2 -3
- package/lib/services/jwt.js +0 -13
- package/lib/services/log.js +0 -8
- package/lib/services/mail.js +0 -3
- package/lib/services/pay.js +9 -30
- package/lib/services/rate_limit.js +0 -8
- package/lib/services/redis.js +0 -1
- package/lib/services/request.d.ts +3 -130
- package/lib/services/request.js +5 -274
- package/lib/services/sensitive_words.js +13 -15
- package/package.json +4 -28
- package/README.md +0 -51
- package/lib/_cjs/cli/register.js +0 -8
- package/lib/_cjs/client_helper.js +0 -26
- package/lib/_cjs/types/_formstream.js +0 -1
- package/lib/cli/register.d.ts +0 -1
- package/lib/cli/register.js +0 -5
- package/lib/client_helper.d.ts +0 -1
- package/lib/client_helper.js +0 -16
- package/lib/types/_formstream.d.ts +0 -49
- package/lib/types/_formstream.js +0 -0
package/lib/_cjs/services/pay.js
CHANGED
|
@@ -1,27 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
exports.__esModule = true;
|
|
6
5
|
exports.PayService = void 0;
|
|
7
|
-
|
|
8
6
|
var _form = _interopRequireDefault(require("alipay-sdk/lib/form"));
|
|
9
|
-
|
|
10
7
|
var _alipaySdk = _interopRequireDefault(require("alipay-sdk"));
|
|
11
|
-
|
|
12
8
|
var _crypto = _interopRequireDefault(require("crypto"));
|
|
13
|
-
|
|
14
9
|
var _got = _interopRequireDefault(require("got"));
|
|
15
|
-
|
|
16
10
|
var _http_error = require("../core/http_error");
|
|
17
|
-
|
|
18
11
|
class PayService {
|
|
19
12
|
constructor(options) {
|
|
20
13
|
this.options = options;
|
|
21
14
|
this.serviceName = 'pay';
|
|
22
15
|
this.alipaySdk = void 0;
|
|
23
16
|
}
|
|
24
|
-
|
|
25
17
|
async prepareAlipay(options) {
|
|
26
18
|
if (!this.alipaySdk) {
|
|
27
19
|
this.alipaySdk = new _alipaySdk.default({
|
|
@@ -30,15 +22,12 @@ class PayService {
|
|
|
30
22
|
alipayPublicKey: this.options.alipay.publicKey
|
|
31
23
|
});
|
|
32
24
|
}
|
|
33
|
-
|
|
34
25
|
const formData = new _form.default();
|
|
35
26
|
formData.setMethod('get');
|
|
36
27
|
formData.addField('notifyUrl', options.notifyUrl);
|
|
37
|
-
|
|
38
28
|
if (options.returnUrl) {
|
|
39
29
|
formData.addField('returnUrl', options.returnUrl);
|
|
40
30
|
}
|
|
41
|
-
|
|
42
31
|
formData.addField('bizContent', {
|
|
43
32
|
outTradeNo: options.tradeNumber,
|
|
44
33
|
productCode: 'FAST_INSTANT_TRADE_PAY',
|
|
@@ -54,7 +43,6 @@ class PayService {
|
|
|
54
43
|
payUrl: payUrl
|
|
55
44
|
};
|
|
56
45
|
}
|
|
57
|
-
|
|
58
46
|
async prepareWepay(options) {
|
|
59
47
|
const appId = options.appId || this.options.wepay.appId;
|
|
60
48
|
const res = await this.wepayRequest('https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi', {
|
|
@@ -72,12 +60,10 @@ class PayService {
|
|
|
72
60
|
openid: options.openid
|
|
73
61
|
}
|
|
74
62
|
});
|
|
75
|
-
|
|
76
63
|
if (!res || !res.prepay_id) {
|
|
77
64
|
const error = res && res.message || '支付错误';
|
|
78
65
|
throw new Error(error);
|
|
79
66
|
}
|
|
80
|
-
|
|
81
67
|
const params = {
|
|
82
68
|
appId: appId,
|
|
83
69
|
timestamp: String(Math.round(Date.now() / 1000)),
|
|
@@ -91,57 +77,47 @@ class PayService {
|
|
|
91
77
|
payParams: params
|
|
92
78
|
};
|
|
93
79
|
}
|
|
94
|
-
|
|
95
80
|
verifyNotifyData(data) {
|
|
96
81
|
try {
|
|
97
82
|
if (!data || typeof data !== 'object') {
|
|
98
83
|
return false;
|
|
99
|
-
}
|
|
100
|
-
// https://opendocs.alipay.com/open/270/105902
|
|
101
|
-
|
|
84
|
+
}
|
|
102
85
|
|
|
86
|
+
// 支付宝
|
|
87
|
+
// https://opendocs.alipay.com/open/270/105902
|
|
103
88
|
if (data.passback_params === 'alipay') {
|
|
104
89
|
var _this$alipaySdk;
|
|
105
|
-
|
|
106
90
|
if (!((_this$alipaySdk = this.alipaySdk) != null && _this$alipaySdk.checkNotifySign(data))) {
|
|
107
91
|
return false;
|
|
108
92
|
}
|
|
109
|
-
|
|
110
93
|
return {
|
|
111
94
|
channelOrderNo: data.trade_no
|
|
112
95
|
};
|
|
113
|
-
}
|
|
114
|
-
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtml
|
|
115
|
-
|
|
96
|
+
}
|
|
116
97
|
|
|
98
|
+
// 微信支付
|
|
99
|
+
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtml
|
|
117
100
|
if (data.resource) {
|
|
118
101
|
const params = JSON.parse(this.wepayDecrypt(data.resource));
|
|
119
|
-
|
|
120
102
|
if (params.attach === 'wepay') {
|
|
121
103
|
return {
|
|
122
104
|
channelOrderNo: params.transaction_id
|
|
123
105
|
};
|
|
124
106
|
}
|
|
125
|
-
|
|
126
107
|
return false;
|
|
127
108
|
}
|
|
128
|
-
|
|
129
109
|
return false;
|
|
130
110
|
} catch {
|
|
131
111
|
return false;
|
|
132
112
|
}
|
|
133
113
|
}
|
|
134
|
-
|
|
135
114
|
verifyNotifyDataOrFail(data, message) {
|
|
136
115
|
const res = this.verifyNotifyData(data);
|
|
137
|
-
|
|
138
116
|
if (!res) {
|
|
139
117
|
throw new _http_error.HttpError.BadRequest(message);
|
|
140
118
|
}
|
|
141
|
-
|
|
142
119
|
return res;
|
|
143
120
|
}
|
|
144
|
-
|
|
145
121
|
async getOrderInfo(payload) {
|
|
146
122
|
// 微信支付
|
|
147
123
|
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_2.shtml
|
|
@@ -152,10 +128,10 @@ class PayService {
|
|
|
152
128
|
orderNo: res.out_trade_no,
|
|
153
129
|
status: res.trade_state === 'SUCCESS' ? 'success' : 'fail'
|
|
154
130
|
};
|
|
155
|
-
}
|
|
156
|
-
// https://opendocs.alipay.com/open/028woa?scene=common&pathHash=dbf247eb
|
|
157
|
-
|
|
131
|
+
}
|
|
158
132
|
|
|
133
|
+
// 支付宝
|
|
134
|
+
// https://opendocs.alipay.com/open/028woa?scene=common&pathHash=dbf247eb
|
|
159
135
|
const res = await this.alipaySdk.exec('alipay.trade.query', {
|
|
160
136
|
biz_content: payload.orderNo ? {
|
|
161
137
|
out_trade_no: payload.orderNo
|
|
@@ -169,25 +145,20 @@ class PayService {
|
|
|
169
145
|
status: res.trade_status === 'TRADE_SUCCESS' || res.trade_status === 'TRADE_FINISHED' ? 'success' : 'fail'
|
|
170
146
|
};
|
|
171
147
|
}
|
|
172
|
-
|
|
173
148
|
wepaySign(data) {
|
|
174
149
|
return _crypto.default.createSign('RSA-SHA256').update(data.map(v => `${v}\n`).join('')).sign(this.options.wepay.privateKey, 'base64');
|
|
175
150
|
}
|
|
176
|
-
|
|
177
151
|
wepayDecrypt(payload) {
|
|
178
152
|
const ciphertextBuffer = Buffer.from(payload.ciphertext, 'base64');
|
|
179
153
|
const authTag = ciphertextBuffer.slice(ciphertextBuffer.length - 16);
|
|
180
154
|
const data = ciphertextBuffer.slice(0, ciphertextBuffer.length - 16);
|
|
181
|
-
|
|
182
155
|
const decipherIv = _crypto.default.createDecipheriv('aes-256-gcm', this.options.wepay.secretKey, payload.nonce);
|
|
183
|
-
|
|
184
156
|
decipherIv.setAuthTag(Buffer.from(authTag));
|
|
185
157
|
decipherIv.setAAD(Buffer.from(payload.associated_data));
|
|
186
158
|
const decryptStr = decipherIv.update(data, undefined, 'utf8');
|
|
187
159
|
decipherIv.final();
|
|
188
160
|
return decryptStr;
|
|
189
161
|
}
|
|
190
|
-
|
|
191
162
|
async wepayRequest(url, data) {
|
|
192
163
|
const path = url.replace(/^https?:\/\/[^/]+/, '');
|
|
193
164
|
const method = data == null ? 'GET' : 'POST';
|
|
@@ -218,7 +189,5 @@ class PayService {
|
|
|
218
189
|
});
|
|
219
190
|
return res;
|
|
220
191
|
}
|
|
221
|
-
|
|
222
192
|
}
|
|
223
|
-
|
|
224
193
|
exports.PayService = PayService;
|
|
@@ -2,43 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.RateLimitService = void 0;
|
|
5
|
-
|
|
6
5
|
var _http_error = require("../core/http_error");
|
|
7
|
-
|
|
8
6
|
var _x = require("../x");
|
|
9
|
-
|
|
10
7
|
class RateLimitService {
|
|
11
8
|
constructor() {
|
|
12
9
|
this.serviceName = 'rateLimit';
|
|
13
10
|
}
|
|
14
|
-
|
|
15
11
|
async limitByCount(options) {
|
|
16
12
|
const cacheKey = `rateLimit_${options.key}`;
|
|
17
13
|
const remainingCount = await _x.x.cache.get(cacheKey);
|
|
18
|
-
|
|
19
14
|
if (remainingCount == null) {
|
|
20
15
|
await _x.x.cache.set(cacheKey, options.count, options.ttl);
|
|
21
16
|
return options.count;
|
|
22
17
|
}
|
|
23
|
-
|
|
24
18
|
if (remainingCount === 0) {
|
|
25
19
|
return 0;
|
|
26
20
|
}
|
|
27
|
-
|
|
28
21
|
await _x.x.cache.decrease(cacheKey);
|
|
29
22
|
return remainingCount - 1;
|
|
30
23
|
}
|
|
31
|
-
|
|
32
24
|
async limitByCountOrFail(options) {
|
|
33
25
|
const count = await this.limitByCount(options);
|
|
34
|
-
|
|
35
26
|
if (count === 0) {
|
|
36
27
|
throw new _http_error.HttpError.Forbidden(options.message);
|
|
37
28
|
}
|
|
38
|
-
|
|
39
29
|
return count;
|
|
40
30
|
}
|
|
41
|
-
|
|
42
31
|
}
|
|
43
|
-
|
|
44
32
|
exports.RateLimitService = RateLimitService;
|
|
@@ -2,15 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.RedisService = void 0;
|
|
5
|
-
|
|
6
5
|
var _ioredis = require("ioredis");
|
|
7
|
-
|
|
8
6
|
class RedisService extends _ioredis.Redis {
|
|
9
7
|
constructor(...args) {
|
|
10
8
|
super(...args);
|
|
11
9
|
this.serviceName = 'redis';
|
|
12
10
|
}
|
|
13
|
-
|
|
14
11
|
}
|
|
15
|
-
|
|
16
12
|
exports.RedisService = RedisService;
|
|
@@ -1,306 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
3
|
exports.__esModule = true;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
var _proxyAgent = require("proxy-agent");
|
|
21
|
-
|
|
22
|
-
var _stream = require("stream");
|
|
23
|
-
|
|
24
|
-
class RequestFormUrlencoded extends URLSearchParams {}
|
|
25
|
-
|
|
26
|
-
exports.RequestFormUrlencoded = RequestFormUrlencoded;
|
|
27
|
-
|
|
28
|
-
class RequestFormData extends _formData.default {
|
|
29
|
-
constructor(cbOrObject) {
|
|
30
|
-
super();
|
|
31
|
-
|
|
32
|
-
if (cbOrObject) {
|
|
33
|
-
if (typeof cbOrObject === 'function') {
|
|
34
|
-
cbOrObject(this);
|
|
35
|
-
} else {
|
|
36
|
-
Object.keys(cbOrObject).forEach(key => {
|
|
37
|
-
const value = cbOrObject[key];
|
|
38
|
-
|
|
39
|
-
if (value instanceof RequestFormFile) {
|
|
40
|
-
var _value$options, _value$options2;
|
|
41
|
-
|
|
42
|
-
this.append(key, typeof value.file === 'string' ? _fs.default.createReadStream(value.file) : value.file, {
|
|
43
|
-
filename: (_value$options = value.options) == null ? void 0 : _value$options.name,
|
|
44
|
-
contentType: (_value$options2 = value.options) == null ? void 0 : _value$options2.type
|
|
45
|
-
});
|
|
46
|
-
} else {
|
|
47
|
-
this.append(key, value);
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
exports.RequestFormData = RequestFormData;
|
|
57
|
-
|
|
58
|
-
class RequestFormStream extends _formstream.default {
|
|
59
|
-
constructor(cbOrObject) {
|
|
60
|
-
super();
|
|
61
|
-
|
|
62
|
-
if (cbOrObject) {
|
|
63
|
-
if (typeof cbOrObject === 'function') {
|
|
64
|
-
cbOrObject(this);
|
|
65
|
-
} else {
|
|
66
|
-
Object.keys(cbOrObject).forEach(key => {
|
|
67
|
-
const value = cbOrObject[key];
|
|
68
|
-
|
|
69
|
-
if (value instanceof RequestFormFile) {
|
|
70
|
-
if (typeof value.file === 'string') {
|
|
71
|
-
var _value$options3;
|
|
72
|
-
|
|
73
|
-
this.file(key, value.file, (_value$options3 = value.options) == null ? void 0 : _value$options3.name);
|
|
74
|
-
} else if (Buffer.isBuffer(value.file)) {
|
|
75
|
-
this.buffer(key, value.file, value.options.name, value.options.type);
|
|
76
|
-
} else if (value.file instanceof _stream.Readable) {
|
|
77
|
-
this.stream(key, value.file, value.options.name, value.options.type);
|
|
78
|
-
}
|
|
79
|
-
} else {
|
|
80
|
-
this.field(key, value);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
exports.RequestFormStream = RequestFormStream;
|
|
90
|
-
|
|
91
|
-
class RequestFormFile {
|
|
92
|
-
constructor(file, options) {
|
|
93
|
-
this.file = file;
|
|
94
|
-
this.options = options;
|
|
95
|
-
|
|
96
|
-
if (Buffer.isBuffer(file) || file instanceof _stream.Readable) {
|
|
97
|
-
(0, _assert.default)(options == null ? void 0 : options.name, 'options.name is required');
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
exports.RequestFormFile = RequestFormFile;
|
|
104
|
-
|
|
105
|
-
class RequestService {
|
|
106
|
-
static getGotOptions(options, responseType) {
|
|
107
|
-
var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
|
|
108
|
-
|
|
109
|
-
const gotOptions = {
|
|
110
|
-
url: options.url,
|
|
111
|
-
responseType: responseType === 'stream' ? undefined : responseType,
|
|
112
|
-
https: {
|
|
113
|
-
rejectUnauthorized: false
|
|
114
|
-
},
|
|
115
|
-
isStream: responseType === 'stream'
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
if (options.headers) {
|
|
119
|
-
gotOptions.headers = options.headers;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (options.followRedirect != null) {
|
|
123
|
-
gotOptions.followRedirect = options.followRedirect;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (options.timeoutMs) {
|
|
127
|
-
gotOptions.timeout = options.timeoutMs;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (options.proxyUrl) {
|
|
131
|
-
const proxyAgent = new _proxyAgent.ProxyAgent({
|
|
132
|
-
getProxyForUrl: () => options.proxyUrl
|
|
133
|
-
});
|
|
134
|
-
gotOptions.agent = {
|
|
135
|
-
http: proxyAgent,
|
|
136
|
-
https: proxyAgent,
|
|
137
|
-
http2: proxyAgent
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (options.cookieJar) {
|
|
142
|
-
gotOptions.cookieJar = options.cookieJar;
|
|
143
|
-
} // 头处理
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
gotOptions.headers = (0, _vtils.mapKeys)(gotOptions.headers || {}, (v, k) => k.toLowerCase());
|
|
147
|
-
(_gotOptions$headers$_ = (_gotOptions$headers = gotOptions.headers)[_userAgent = 'user-agent']) != null ? _gotOptions$headers$_ : _gotOptions$headers[_userAgent] = (_options$userAgent = options.userAgent) != null ? _options$userAgent : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36';
|
|
148
|
-
|
|
149
|
-
if (options.autoReferer && gotOptions.headers['referer'] == null) {
|
|
150
|
-
gotOptions.headers['referer'] = options.url;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return gotOptions;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
static getRaw(options, responseType) {
|
|
157
|
-
let url = options.url;
|
|
158
|
-
|
|
159
|
-
if (options.data) {
|
|
160
|
-
const _url = new URL(url);
|
|
161
|
-
|
|
162
|
-
Object.keys(options.data).forEach(key => {
|
|
163
|
-
_url.searchParams.set(key, options.data[key]);
|
|
164
|
-
});
|
|
165
|
-
url = _url.toString();
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
options.url = url;
|
|
169
|
-
const res = (0, _got.default)({
|
|
170
|
-
method: 'GET',
|
|
171
|
-
...this.getGotOptions(options, responseType || 'buffer')
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
if (responseType === 'stream') {
|
|
175
|
-
return res;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return res.then(res => ({
|
|
179
|
-
status: res.statusCode,
|
|
180
|
-
headers: res.headers,
|
|
181
|
-
data: res.body
|
|
182
|
-
}));
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
static postRaw(options, responseType) {
|
|
186
|
-
const gotOptions = this.getGotOptions(options, responseType || 'buffer');
|
|
187
|
-
|
|
188
|
-
if (responseType === 'stream' && options.data instanceof _formstream.default) {
|
|
189
|
-
gotOptions.headers = options.data.headers(gotOptions.headers);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const res = (0, _got.default)({
|
|
193
|
-
method: 'POST',
|
|
194
|
-
...(options.data == null ? {
|
|
195
|
-
body: ''
|
|
196
|
-
} : options.data instanceof _formData.default ? {
|
|
197
|
-
body: options.data
|
|
198
|
-
} : options.data instanceof URLSearchParams ? {
|
|
199
|
-
form: options.data
|
|
200
|
-
} : options.data instanceof _formstream.default ? {} : {
|
|
201
|
-
json: options.data
|
|
202
|
-
}),
|
|
203
|
-
...gotOptions
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
if (responseType === 'stream' && options.data instanceof _formstream.default) {
|
|
207
|
-
options.data.pipe(res);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
if (responseType === 'stream') {
|
|
211
|
-
return res;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return res.then(res => ({
|
|
215
|
-
status: res.statusCode,
|
|
216
|
-
headers: res.headers,
|
|
217
|
-
data: res.body
|
|
218
|
-
}));
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
static async getText(options) {
|
|
222
|
-
return this.getRaw(options, 'text');
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
static async postText(options) {
|
|
226
|
-
return this.postRaw(options, 'text');
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
static async get(options) {
|
|
230
|
-
return this.getRaw(options, 'json');
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
static async post(options) {
|
|
234
|
-
return this.postRaw(options, 'json');
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
static getStream(options) {
|
|
238
|
-
return this.getRaw(options, 'stream');
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
static postStream(options) {
|
|
242
|
-
return this.postRaw(options, 'stream');
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
static isTimeoutError(err) {
|
|
246
|
-
return err instanceof _got.default.TimeoutError;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
constructor(options) {
|
|
250
|
-
this.options = options;
|
|
4
|
+
var _exportNames = {
|
|
5
|
+
RequestService: true
|
|
6
|
+
};
|
|
7
|
+
exports.RequestService = void 0;
|
|
8
|
+
var _xRequest = require("@jayfong/x-request");
|
|
9
|
+
Object.keys(_xRequest).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
12
|
+
if (key in exports && exports[key] === _xRequest[key]) return;
|
|
13
|
+
exports[key] = _xRequest[key];
|
|
14
|
+
});
|
|
15
|
+
class RequestService extends _xRequest.XRequest {
|
|
16
|
+
constructor(...args) {
|
|
17
|
+
super(...args);
|
|
251
18
|
this.serviceName = 'request';
|
|
252
19
|
}
|
|
253
|
-
|
|
254
|
-
getRaw(options, responseType) {
|
|
255
|
-
var _this$options;
|
|
256
|
-
|
|
257
|
-
return RequestService.getRaw({ ...this.options,
|
|
258
|
-
...options,
|
|
259
|
-
headers: { ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
|
|
260
|
-
...options.headers
|
|
261
|
-
}
|
|
262
|
-
}, responseType);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
postRaw(options, responseType) {
|
|
266
|
-
var _this$options2;
|
|
267
|
-
|
|
268
|
-
return RequestService.postRaw({ ...this.options,
|
|
269
|
-
...options,
|
|
270
|
-
headers: { ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
|
|
271
|
-
...options.headers
|
|
272
|
-
}
|
|
273
|
-
}, responseType);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
async getText(options) {
|
|
277
|
-
return this.getRaw(options, 'text');
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
async postText(options) {
|
|
281
|
-
return this.postRaw(options, 'text');
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
async get(options) {
|
|
285
|
-
return this.getRaw(options, 'json');
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
async post(options) {
|
|
289
|
-
return this.postRaw(options, 'json');
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
getStream(options) {
|
|
293
|
-
return this.getRaw(options, 'stream');
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
postStream(options) {
|
|
297
|
-
return this.postRaw(options, 'stream');
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
isTimeoutError(err) {
|
|
301
|
-
return RequestService.isTimeoutError(err);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
20
|
}
|
|
305
|
-
|
|
306
21
|
exports.RequestService = RequestService;
|
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
exports.__esModule = true;
|
|
6
5
|
exports.SensitiveWordsService = void 0;
|
|
7
|
-
|
|
8
6
|
var _mintFilter = _interopRequireDefault(require("mint-filter"));
|
|
9
|
-
|
|
10
7
|
var _vtils = require("vtils");
|
|
11
|
-
|
|
12
8
|
/**
|
|
13
9
|
* 敏感词服务。
|
|
14
10
|
*/
|
|
@@ -18,30 +14,32 @@ class SensitiveWordsService {
|
|
|
18
14
|
this.serviceName = 'sensitiveWords';
|
|
19
15
|
this.mint = void 0;
|
|
20
16
|
}
|
|
17
|
+
|
|
21
18
|
/**
|
|
22
19
|
* 文本转换。
|
|
23
20
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
26
21
|
transform(text) {
|
|
27
|
-
return (
|
|
28
|
-
|
|
29
|
-
(0, _vtils.
|
|
30
|
-
|
|
31
|
-
.
|
|
22
|
+
return (
|
|
23
|
+
// 移除空白字符
|
|
24
|
+
(0, _vtils.removeBlankChars)(
|
|
25
|
+
// 移除非单词字符
|
|
26
|
+
(0, _vtils.removeNonWordChars)(
|
|
27
|
+
// 全角转半角
|
|
28
|
+
(0, _vtils.toHalfWidthString)(text)))
|
|
29
|
+
// 转小写
|
|
30
|
+
.toLowerCase()
|
|
31
|
+
// 叠词归一
|
|
32
32
|
.replace(/(.)\1+/g, '$1')
|
|
33
33
|
);
|
|
34
34
|
}
|
|
35
|
+
|
|
35
36
|
/**
|
|
36
37
|
* 处理文本,敏感词将被替换为 * 号。
|
|
37
38
|
*/
|
|
38
|
-
|
|
39
|
-
|
|
40
39
|
async process(text) {
|
|
41
40
|
if (!this.mint) {
|
|
42
41
|
this.mint = new _mintFilter.default(this.options.words);
|
|
43
42
|
}
|
|
44
|
-
|
|
45
43
|
const res = await this.mint.filter(this.transform(text), {
|
|
46
44
|
every: true,
|
|
47
45
|
replace: true,
|
|
@@ -49,16 +47,14 @@ class SensitiveWordsService {
|
|
|
49
47
|
});
|
|
50
48
|
return res.text;
|
|
51
49
|
}
|
|
50
|
+
|
|
52
51
|
/**
|
|
53
52
|
* 验证文本是否包含敏感词。
|
|
54
53
|
*/
|
|
55
|
-
|
|
56
|
-
|
|
57
54
|
async validate(text, every) {
|
|
58
55
|
if (!this.mint) {
|
|
59
56
|
this.mint = new _mintFilter.default(this.options.words);
|
|
60
57
|
}
|
|
61
|
-
|
|
62
58
|
const res = await this.mint.filter(this.transform(text), {
|
|
63
59
|
replace: false,
|
|
64
60
|
every: !!every,
|
|
@@ -69,7 +65,5 @@ class SensitiveWordsService {
|
|
|
69
65
|
words: res.words
|
|
70
66
|
};
|
|
71
67
|
}
|
|
72
|
-
|
|
73
68
|
}
|
|
74
|
-
|
|
75
69
|
exports.SensitiveWordsService = SensitiveWordsService;
|
package/lib/_cjs/x.js
CHANGED
|
@@ -1,24 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
exports.__esModule = true;
|
|
6
5
|
exports.x = void 0;
|
|
7
|
-
|
|
8
6
|
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
|
9
|
-
|
|
10
7
|
var _os = _interopRequireDefault(require("os"));
|
|
11
|
-
|
|
12
8
|
var _path = _interopRequireDefault(require("path"));
|
|
13
|
-
|
|
14
9
|
var _dispose = require("./services/dispose");
|
|
15
|
-
|
|
16
10
|
var _emoji = require("./services/emoji");
|
|
17
|
-
|
|
18
11
|
var _log = require("./services/log");
|
|
19
|
-
|
|
20
12
|
var _request = require("./services/request");
|
|
21
|
-
|
|
22
13
|
const env = JSON.parse(process.env.X_SERVER_ENVS || '{}');
|
|
23
14
|
const x = {
|
|
24
15
|
appId: env.APP_ID,
|
|
@@ -32,9 +23,7 @@ const x = {
|
|
|
32
23
|
}
|
|
33
24
|
};
|
|
34
25
|
exports.x = x;
|
|
35
|
-
|
|
36
26
|
_fsExtra.default.ensureDirSync(x.dataDir);
|
|
37
|
-
|
|
38
27
|
x.register(new _dispose.DisposeService({
|
|
39
28
|
disposeOnExit: true
|
|
40
29
|
}), new _log.LogService(), new _emoji.EmojiService(), new _request.RequestService());
|