abler-api 0.2.0 → 0.2.4

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.
Files changed (2) hide show
  1. package/dist/cjs/pp-util.js +41 -35
  2. package/package.json +46 -45
@@ -27,7 +27,7 @@ var require$$0__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$0$1);
27
27
  const fs$1 = require$$0__default["default"];
28
28
  const path = require$$1__default["default"];
29
29
  const {
30
- t_f: t_f$3,
30
+ t_f: t_f$4,
31
31
  t: t$1
32
32
  } = require$$2__default["default"];
33
33
  const ppUtil$4 = require$$3__default["default"].ppUtil;
@@ -108,7 +108,7 @@ class preconditions$2 {
108
108
  }
109
109
  let text = "";
110
110
  if (preconditions$2.messages) {
111
- text = t_f$3("*** 由于以下原因,%s 尚不能正常提供服务:\n\n", preconditions$2.appName) + preconditions$2.messages.join("\n\n") + t$1("\n\n*** 请检查以上各项条件,修复后重新启动服务");
111
+ text = t_f$4("*** 由于以下原因,%s 尚不能正常提供服务:\n\n", preconditions$2.appName) + preconditions$2.messages.join("\n\n") + t$1("\n\n*** 请检查以上各项条件,修复后重新启动服务");
112
112
  console.log("\n" + text);
113
113
  // for (let i = 0; i < preconditions.messages.length; i++) {
114
114
  // console.log(preconditions.messages[i]);
@@ -162,17 +162,20 @@ async function createEnvSettingFile(fileName, lastEnvSettingEx) {
162
162
  try {
163
163
  const cfgText = await ppUtil$4.doCreateEnvSettingFile(fileName, lastEnvSettingEx);
164
164
  if (cfgText.indexOf(`cfgVersion: "${conf$3._envSetting.cfgVersion}"`) > 0) {
165
- console.log(t_f$3("\n*** 已創建新的本機環境配置文件\n%s\n请檢查各項內容是否正确(此项更新服务可正常运行)\n\n", fileName));
165
+ console.log(t_f$4("\n*** 已創建新的本機環境配置文件\n%s\n请檢查各項內容是否正确(此项更新服务可正常运行)\n\n", fileName));
166
166
  return "";
167
167
  }
168
- return t_f$3("已創建新的本機環境配置文件\n%s\n请檢查並正確設置各項內容,然後將配置版本號修改為%s", fileName, conf$3._envSettings.default.cfgVersion);
168
+ return t_f$4("已創建新的本機環境配置文件\n%s\n请檢查並正確設置各項內容,然後將配置版本號修改為%s", fileName, conf$3._envSettings.default.cfgVersion);
169
169
  } catch (e) {
170
- return t_f$3("創建本機環境配置文件失敗\n《%s》\n%s", fileName, err$1.ERROR(e).message);
170
+ return t_f$4("創建本機環境配置文件失敗\n《%s》\n%s", fileName, err$1.ERROR(e).message);
171
171
  }
172
172
  }
173
173
  async function checkExEnvSetting(loadjs) {
174
174
  if (conf$3._envSetting.localSettingImported) return "";
175
175
  try {
176
+ if (typeof conf$3.getEnvSettingFileName !== "function") {
177
+ throw new Error("必须在 config 中定义 getEnvSettingFileName 方法获取本地配置文件名");
178
+ }
176
179
  let fileName = conf$3.getEnvSettingFileName();
177
180
  if (!fs$1.existsSync(fileName)) {
178
181
  return await createEnvSettingFile(fileName);
@@ -194,7 +197,7 @@ async function checkExEnvSetting(loadjs) {
194
197
  }
195
198
  ppUtil$4.importExEnvSetting(conf$3._envSetting, fileName, loadjs);
196
199
  } catch (e) {
197
- return t_f$3("导入本机环境配置失败\n%s", e.stack);
200
+ return t_f$4("导入本机环境配置失败\n%s", e.stack);
198
201
  }
199
202
  }
200
203
 
@@ -214,8 +217,8 @@ async function checkRedis() {
214
217
  redis.error = e;
215
218
  });
216
219
  if (redis.error) {
217
- console.log(t_f$3("未能成功連接 redis: %s,請檢查相關設置", redis.error));
218
- return t_f$3("未能成功連接 redis: %s,請檢查相關設置", redis.error);
220
+ console.log(t_f$4("未能成功連接 redis: %s,請檢查相關設置", redis.error));
221
+ return t_f$4("未能成功連接 redis: %s,請檢查相關設置", redis.error);
219
222
  }
220
223
  // console.log(`checkRedis done:`);
221
224
  // console.log(redis);
@@ -225,12 +228,12 @@ async function initDb(dbSql) {
225
228
  dbUtil$1.config(conf$3, err$1, dbSql);
226
229
  } catch (e) {
227
230
  if (e.reason === 'bad decrypt') {
228
- e = new Error(t_f$3("解密数据库密码(%s)失败", conf$3.dbconn.dbpwd));
231
+ e = new Error(t_f$4("解密数据库密码(%s)失败", conf$3.dbconn.dbpwd));
229
232
  }
230
233
  // if (e.reason === 'bad decrypt') {
231
234
  // e = new Error(`解密Redis密码(${conf.redis.password})失败`);
232
235
  // }
233
- return t_f$3("初始化数据库连接失败(%s)", e.message);
236
+ return t_f$4("初始化数据库连接失败(%s)", e.message);
234
237
  }
235
238
  }
236
239
  async function initKvStorage(dbSql) {
@@ -245,7 +248,7 @@ async function initKvStorage(dbSql) {
245
248
  // if (e.reason === 'bad decrypt') {
246
249
  // e = new Error(`解密Redis密码(${conf.redis.password})失败`);
247
250
  // }
248
- return t_f$3("初始化键值对存储器失败(%s)", e.message);
251
+ return t_f$4("初始化键值对存储器失败(%s)", e.message);
249
252
  }
250
253
  }
251
254
  var ppPrecond = preconditions$2;
@@ -254,7 +257,7 @@ const fs = require$$0__default["default"];
254
257
  const crypto = require$$1__default$1["default"];
255
258
  const getBasicAuthorization = require$$2__default$1["default"];
256
259
  const {
257
- t_f: t_f$2,
260
+ t_f: t_f$3,
258
261
  t
259
262
  } = require$$2__default["default"];
260
263
  const {
@@ -407,7 +410,7 @@ class apiUtil$2 {
407
410
  if (this[name] === undefined) return defaultValue;
408
411
  let result = +this[name];
409
412
  if (typeof result !== "number" || result < minValue || result > maxValue) {
410
- throw [errCfg.INVALID_PARAM, t_f$2("参数 %s 必须是 %s ~ %s 之间的数字", name, minValue, maxValue)];
413
+ throw [errCfg.INVALID_PARAM, t_f$3("参数 %s 必须是 %s ~ %s 之间的数字", name, minValue, maxValue)];
411
414
  }
412
415
  return result;
413
416
  });
@@ -519,7 +522,7 @@ class apiUtil$2 {
519
522
  try {
520
523
  response.send(data);
521
524
  } catch (e) {
522
- console.log(t_f$2("response.send 出错了,req.url: %s, response:", response.req.url), response);
525
+ console.log(t_f$3("response.send 出错了,req.url: %s, response:", response.req.url), response);
523
526
  console.log("异常信息", e);
524
527
  }
525
528
  }
@@ -996,7 +999,7 @@ class apiUtil$2 {
996
999
  return certInfo;
997
1000
  }).catch(e => {
998
1001
  console.log('获取企业证书失败:\n', e);
999
- throw [errCfg.ACCESS_REFUSED, t_f$2("获取企业证书失败:%s", errCfg.ERROR(e).message)];
1002
+ throw [errCfg.ACCESS_REFUSED, t_f$3("获取企业证书失败:%s", errCfg.ERROR(e).message)];
1000
1003
  });
1001
1004
  }
1002
1005
 
@@ -1009,17 +1012,17 @@ class apiUtil$2 {
1009
1012
  static async verifyApiTokenSignature(tokenData, req) {
1010
1013
  try {
1011
1014
  if (!req.headers.timestamp) {
1012
- throw [errCfg.ACCESS_REFUSED, t_f$2("必须在请求头中设置时戳")];
1015
+ throw [errCfg.ACCESS_REFUSED, t_f$3("必须在请求头中设置时戳")];
1013
1016
  }
1014
1017
  let timestamp = parseInt(req.headers.timestamp);
1015
1018
  let currentTime = new Date().valueOf();
1016
1019
  let maxTimeDiff = apiUtil$2.isDebugMode(req) ? 7 * 24 * 3600 : apiUtil$2.isTestMode(req) ? 4 * 3600 : 100; // 100
1017
1020
  if (timestamp.toString() == "NaN" || currentTime < timestamp - 5000 || currentTime - timestamp > maxTimeDiff * 1000) {
1018
- throw [errCfg.ACCESS_REFUSED, t_f$2("时戳(%s)无效,当前时戳:%s,时差 %s秒", timestamp, currentTime, (currentTime - timestamp) / 1000)];
1021
+ throw [errCfg.ACCESS_REFUSED, t_f$3("时戳(%s)无效,当前时戳:%s,时差 %s秒", timestamp, currentTime, (currentTime - timestamp) / 1000)];
1019
1022
  }
1020
1023
  let signature = req.headers.signature;
1021
1024
  if (!signature) {
1022
- throw [errCfg.ACCESS_REFUSED, t_f$2("必须在请求头中提供签名")];
1025
+ throw [errCfg.ACCESS_REFUSED, t_f$3("必须在请求头中提供签名")];
1023
1026
  }
1024
1027
  let signData = apiUtil$2.extractToken(req, null, true) || tokenData.apiKey;
1025
1028
  signData += timestamp;
@@ -1080,7 +1083,7 @@ class apiUtil$2 {
1080
1083
  verifyOK = verifySpare.verify(tokenData.certPublicKeySpare, signature, 'base64');
1081
1084
  if (verifyOK) {
1082
1085
  // 备用证书验证成功了,则将备用证书替换为正式证书
1083
- console.log(t_f$2("公司(%s)备用证书验签成功,正式启用...", tokenData.apiKey));
1086
+ console.log(t_f$3("公司(%s)备用证书验签成功,正式启用...", tokenData.apiKey));
1084
1087
  return dbUtil.dbTrans(trans => {
1085
1088
  let p1 = dbUtil.dbExec(dbSql.deleteCertRecById, {
1086
1089
  replacements: {
@@ -1104,7 +1107,7 @@ class apiUtil$2 {
1104
1107
  if (token) {
1105
1108
  apiUtil$2.storeToken(apiUtil$2.apiTokenStoreKey(token), tokenData, tokenData.apiKey, req);
1106
1109
  }
1107
- console.log(t_f$2("公司(%s)备用证书成功转换为正式证书", tokenData.apiKey));
1110
+ console.log(t_f$3("公司(%s)备用证书成功转换为正式证书", tokenData.apiKey));
1108
1111
  });
1109
1112
  }
1110
1113
  } catch (e) {
@@ -1161,10 +1164,10 @@ class apiUtil$2 {
1161
1164
  * @param token
1162
1165
  * @returns {*}
1163
1166
  */
1164
- static tokenDataExist(token) {
1165
- return kvStorage.stored(token);
1167
+ static async tokenDataExist(token) {
1168
+ return await kvStorage.stored(token);
1166
1169
  }
1167
- static parametersOK(params) {
1170
+ static checkParametersOK(params) {
1168
1171
  configNeeded();
1169
1172
  let paramSufix = params._param_sufix || "";
1170
1173
  for (let i = 1; i < arguments.length; i++) {
@@ -1196,9 +1199,12 @@ class apiUtil$2 {
1196
1199
  } else {
1197
1200
  paramExists = typeof params[paramName] != 'undefined' || typeof params[paramName + paramSufix] != 'undefined';
1198
1201
  }
1199
- if (!paramExists) return ppUtil$3.errorPormise(errCfg.ERROR(errCfg.PARAMETER_NEEDED, paramName));
1202
+ if (!paramExists) throw errCfg$1.ERROR(errCfg$1.PARAMETER_NEEDED, paramName);
1200
1203
  }
1201
- return Promise.resolve(paramSufix);
1204
+ return paramSufix;
1205
+ }
1206
+ static async parametersOK(params) {
1207
+ return await apiUtil$2.checkParametersOK(params);
1202
1208
  }
1203
1209
 
1204
1210
  /**
@@ -1262,7 +1268,7 @@ class apiUtil$2 {
1262
1268
  const myEnvHosts = apiUtil$2.getEnvHosts(params);
1263
1269
  const myIp = ppUtil$3.getMyIp();
1264
1270
  const host = params.host || myIp;
1265
- if (!myEnvHosts.hosts.contains(host)) throw [errCfg.ACCESS_REFUSED, t_f$2("环境%s貌似无此主机:%s", conf$2.envId, params.host)];
1271
+ if (!myEnvHosts.hosts.contains(host)) throw [errCfg.ACCESS_REFUSED, t_f$3("环境%s貌似无此主机:%s", conf$2.envId, params.host)];
1266
1272
  if (!apiUtil$2.hostIsMySelf(host, options._res.req)) {
1267
1273
  let result = await apiUtil$2.forwardsTo(host, myEnvHosts.port, params, options._res.req, myIp);
1268
1274
  options._res.send(result);
@@ -1292,7 +1298,7 @@ class apiUtil$2 {
1292
1298
  };
1293
1299
  let result = await netUtil$1.apiRequest(reqOptions);
1294
1300
  if (result.status !== 200) {
1295
- console.log(t_f$2("访问 %s 出错", reqOptions.url), result.status, result.res.statusMessage);
1301
+ console.log(t_f$3("访问 %s 出错", reqOptions.url), result.status, result.res.statusMessage);
1296
1302
  throw [errCfg.EXCEPTION, `[${result.status}] - ${result.res.statusMessage}`];
1297
1303
  }
1298
1304
  return result.data;
@@ -1315,7 +1321,7 @@ class apiUtil$2 {
1315
1321
  key = keys[i];
1316
1322
  value = parent[key];
1317
1323
  if (value === undefined) {
1318
- throw [errCfg.INVALID_PARAM, t_f$2("无此配置项: %s", key)];
1324
+ throw [errCfg.INVALID_PARAM, t_f$3("无此配置项: %s", key)];
1319
1325
  }
1320
1326
  }
1321
1327
  return {
@@ -1410,7 +1416,7 @@ class apiUtil$2 {
1410
1416
  };
1411
1417
  if (appInfo) {
1412
1418
  // appInfo.cek = cek;
1413
- await kvStorage.storeObj(cekStoreKey(appInfo.apiKey, req), cek, appSetting.cekExpireTime);
1419
+ await kvStorage.storeObj(this.cekStoreKey(appInfo.apiKey, req), cek, appSetting.cekExpireTime);
1414
1420
  }
1415
1421
  return cek;
1416
1422
  }
@@ -1471,7 +1477,7 @@ class apiUtil$2 {
1471
1477
  };
1472
1478
  const appInfo = await this.restoreAppInfo(params.apiKey, options._res.req);
1473
1479
  if (!appInfo) {
1474
- throw [errCfg.ACCESS_REFUSED, t_f$2("apiKey (%s) 无效", params.apiKey)];
1480
+ throw [errCfg.ACCESS_REFUSED, t_f$3("apiKey (%s) 无效", params.apiKey)];
1475
1481
  }
1476
1482
  let content = '';
1477
1483
  if (appSetting.e2eEncryptionNeeded) {
@@ -1524,7 +1530,7 @@ class apiUtil$2 {
1524
1530
  }
1525
1531
  req.appInfo = await this.restoreAppInfo(apiKey, req);
1526
1532
  if (!req.appInfo) {
1527
- throw [errCfg.ACCESS_REFUSED, t_f$2("api-key (%s) 无效", apiKey)];
1533
+ throw [errCfg.ACCESS_REFUSED, t_f$3("api-key (%s) 无效", apiKey)];
1528
1534
  }
1529
1535
  }
1530
1536
  }
@@ -1535,17 +1541,17 @@ class apiUtil$2 {
1535
1541
  await this.reqAppInfoNeeded(req);
1536
1542
  if (!appSetting.reqSignatureNeeded) return true;
1537
1543
  if (!req.headers.timestamp) {
1538
- throw [errCfg.ACCESS_REFUSED, t_f$2("必须在请求头中设置时戳")];
1544
+ throw [errCfg.ACCESS_REFUSED, t_f$3("必须在请求头中设置时戳")];
1539
1545
  }
1540
1546
  let timestamp = parseInt(req.headers.timestamp);
1541
1547
  let currentTime = new Date().valueOf();
1542
1548
  let maxTimeDiff = apiUtil$2.isDebugMode(req) ? 7 * 24 * 3600 : apiUtil$2.isTestMode(req) ? 4 * 3600 : 300; // 100
1543
1549
  if (timestamp.toString() === "NaN" || currentTime < timestamp - 5000 || currentTime - timestamp > maxTimeDiff * 1000) {
1544
- throw [errCfg.ACCESS_REFUSED, t_f$2("时戳(%s)无效,当前时戳:%s,时差 %s秒", timestamp, currentTime, (currentTime - timestamp) / 1000)];
1550
+ throw [errCfg.ACCESS_REFUSED, t_f$3("时戳(%s)无效,当前时戳:%s,时差 %s秒", timestamp, currentTime, (currentTime - timestamp) / 1000)];
1545
1551
  }
1546
1552
  let signature = req.headers.signature;
1547
1553
  if (!signature) {
1548
- throw [errCfg.ACCESS_REFUSED, t_f$2("必须在请求头中提供签名")];
1554
+ throw [errCfg.ACCESS_REFUSED, t_f$3("必须在请求头中提供签名")];
1549
1555
  }
1550
1556
  const params = apiUtil$2.extractParams(req);
1551
1557
  const content = appSetting.e2eEncryptionNeeded ? params?.encryptedContent || '' : JSON.stringify(params || null);
@@ -1852,7 +1858,7 @@ class apiUtil$2 {
1852
1858
  await apiUtil$2.parametersOK(params, "value");
1853
1859
  let c = apiUtil$2.parseCfgPath(cfgItem);
1854
1860
  if (typeof c.value === "object") {
1855
- throw [errCfg.ACCESS_REFUSED, t_f$2("暂不支持修改复杂配置")];
1861
+ throw [errCfg.ACCESS_REFUSED, t_f$3("暂不支持修改复杂配置")];
1856
1862
  }
1857
1863
  // c.oldValue = c.value;
1858
1864
  c.newValue = params.value;
package/package.json CHANGED
@@ -1,45 +1,46 @@
1
- {
2
- "name": "abler-api",
3
- "version": "0.2.0",
4
- "description": "API服务相关工具",
5
- "main": "./dist/cjs/pp-util.js",
6
- "-module": "./dist/es/pp-util.js",
7
- "scripts": {
8
- "build": "npx rollup -c rollup.config.js",
9
- "postbuild": "node ..\\postbuild.js",
10
- "test": "echo \"Error: no test specified\" && exit 1"
11
- },
12
- "files": [
13
- "dist"
14
- ],
15
- "author": "peng_peng",
16
- "license": "ISC",
17
- "dependencies": {
18
- "q": "^1.5.1",
19
- "abler-util": ">=0.3.0",
20
- "abler-db": ">=0.1.1",
21
- "abler-i18n": "^0.1.20",
22
- "abler-messenger": ">=0.1.1",
23
- "abler-net": ">=0.1.1",
24
- "basic-auth": "^2.0.1",
25
- "node-cron": "^3.0.1"
26
- },
27
- "devDependencies": {
28
- "@babel/core": "^7.18.5",
29
- "@babel/eslint-parser": "^7.18.2",
30
- "@babel/plugin-transform-runtime": "^7.18.0",
31
- "@babel/preset-env": "^7.18.2",
32
- "@babel/runtime": "^7.18.0",
33
- "@rollup/plugin-babel": "^5.3.1",
34
- "@rollup/plugin-commonjs": "^22.0.0",
35
- "@rollup/plugin-eslint": "^8.0.2",
36
- "@rollup/plugin-node-resolve": "^13.3.0",
37
- "@typescript-eslint/eslint-plugin": "^5.25.0",
38
- "@typescript-eslint/parser": "^5.25.0",
39
- "eslint": "^8.16.0",
40
- "eslint-plugin-react": "^7.30.0",
41
- "rollup-plugin-filesize": "^9.1.2",
42
- "rollup-plugin-json": "^4.0.0",
43
- "rollup-plugin-terser": "^7.0.2"
44
- }
45
- }
1
+ {
2
+ "name": "abler-api",
3
+ "version": "0.2.4",
4
+ "description": "API服务相关工具",
5
+ "main": "./dist/cjs/pp-util.js",
6
+ "-module": "./dist/es/pp-util.js",
7
+ "scripts": {
8
+ "build": "npx rollup -c rollup.config.js",
9
+ "postbuild": "node ..\\postbuild.js",
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "author": "peng_peng",
16
+ "license": "ISC",
17
+ "dependencies": {
18
+ "abler-db": "^0.1.57",
19
+ "abler-i18n": "^0.1.25",
20
+ "abler-messenger": "^0.1.16",
21
+ "abler-net": "^0.1.13",
22
+ "abler-util": "^0.3.4",
23
+ "basic-auth": "^2.0.1",
24
+ "node-cron": "^3.0.1",
25
+ "q": "^1.5.1"
26
+ },
27
+ "devDependencies": {
28
+ "@babel/core": "^7.18.5",
29
+ "@babel/eslint-parser": "^7.18.2",
30
+ "@babel/plugin-transform-runtime": "^7.18.0",
31
+ "@babel/preset-env": "^7.18.2",
32
+ "@babel/runtime": "^7.18.0",
33
+ "@rollup/plugin-babel": "^5.3.1",
34
+ "@rollup/plugin-commonjs": "^22.0.0",
35
+ "@rollup/plugin-eslint": "^8.0.2",
36
+ "@rollup/plugin-node-resolve": "^13.3.0",
37
+ "@typescript-eslint/eslint-plugin": "^5.25.0",
38
+ "@typescript-eslint/parser": "^5.25.0",
39
+ "eslint": "^8.16.0",
40
+ "eslint-plugin-react": "^7.30.0",
41
+ "rollup-plugin-filesize": "^9.1.2",
42
+ "rollup-plugin-json": "^4.0.0",
43
+ "rollup-plugin-terser": "^7.0.2"
44
+ },
45
+ "gitHead": "b371a71a41ed24e889f0c35e51deb13916351ff6"
46
+ }