abler-api 0.1.21 → 0.1.24

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 +260 -66
  2. package/package.json +3 -2
@@ -1,29 +1,29 @@
1
1
  'use strict';
2
2
 
3
- var require$$0 = require('crypto');
3
+ var require$$0$1 = require('crypto');
4
4
  var require$$1 = require('abler-util');
5
5
  var require$$2 = require('abler-db');
6
6
  var require$$3 = require('abler-net');
7
- var require$$0$1 = require('fs');
7
+ var require$$0$2 = require('fs');
8
8
  var require$$1$1 = require('path');
9
9
 
10
10
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
11
 
12
- var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);
12
+ var require$$0__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$0$1);
13
13
  var require$$1__default = /*#__PURE__*/_interopDefaultLegacy(require$$1);
14
14
  var require$$2__default = /*#__PURE__*/_interopDefaultLegacy(require$$2);
15
15
  var require$$3__default = /*#__PURE__*/_interopDefaultLegacy(require$$3);
16
- var require$$0__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$0$1);
16
+ var require$$0__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$0$2);
17
17
  var require$$1__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$1$1);
18
18
 
19
- const crypto = require$$0__default["default"];
20
- const ppUtil$2 = require$$1__default["default"].ppUtil;
19
+ const crypto = require$$0__default$1["default"];
20
+ const ppUtil$4 = require$$1__default["default"].ppUtil;
21
21
  const {
22
22
  dbUtil,
23
23
  kvStorage
24
24
  } = require$$2__default["default"];
25
- const netUtil = require$$3__default["default"];
26
- let conf$1, appSetting, err$1, dbSql;
25
+ const netUtil$1 = require$$3__default["default"];
26
+ let conf$2, appSetting$1, err$1, dbSql;
27
27
  const pnToken = "access_token",
28
28
  hnToken = pnToken,
29
29
  pnApiKey = "apiKey",
@@ -31,22 +31,22 @@ const pnToken = "access_token",
31
31
  // 我们接收到的 headers 中的字段名总是全小写的
32
32
  pnApiSecret = "apiSecret",
33
33
  hnApiSecret = pnApiSecret.toLowerCase();
34
- const MD5 = ppUtil$2.MD5,
35
- moveProperty = ppUtil$2.moveProperty;
34
+ const MD5 = ppUtil$4.MD5,
35
+ moveProperty = ppUtil$4.moveProperty;
36
36
 
37
37
  class apiUtil$1 {
38
- static debugFlag = ppUtil$2.newGuid(); //应用必须设置,否则谁也不知道是啥
38
+ static debugFlag = ppUtil$4.newGuid(); //应用必须设置,否则谁也不知道是啥
39
39
 
40
- static testFlag = ppUtil$2.newGuid();
40
+ static testFlag = ppUtil$4.newGuid();
41
41
  static envId_dev = "?"; // static apiCallRecSaver;
42
42
 
43
43
  static config(appConfig, appErrCfg, appDbSql) {
44
- ppUtil$2.config(appConfig, appErrCfg);
45
- conf$1 = appConfig;
46
- appSetting = conf$1?.appSetting;
44
+ ppUtil$4.config(appConfig, appErrCfg);
45
+ conf$2 = appConfig;
46
+ appSetting$1 = conf$2?.appSetting;
47
47
  err$1 = appErrCfg, dbSql = appDbSql;
48
- apiUtil$1.debugFlag = appSetting?.debugFlag || apiUtil$1.debugFlag;
49
- apiUtil$1.testFlag = appSetting?.testFlag || apiUtil$1.testFlag; // apiUtil.apiCallRecSaver = apiCallRecSaver;
48
+ apiUtil$1.debugFlag = appSetting$1?.debugFlag || apiUtil$1.debugFlag;
49
+ apiUtil$1.testFlag = appSetting$1?.testFlag || apiUtil$1.testFlag; // apiUtil.apiCallRecSaver = apiCallRecSaver;
50
50
  } //#region ===== 需要应用系统重写的方法
51
51
 
52
52
  /**
@@ -57,7 +57,7 @@ class apiUtil$1 {
57
57
 
58
58
 
59
59
  static _getApiSecret(companyId) {
60
- return ppUtil$2.newGuid();
60
+ return ppUtil$4.newGuid();
61
61
  }
62
62
  /**
63
63
  * 保存 API 调用记录,应用系统必须重写此方法
@@ -160,7 +160,7 @@ class apiUtil$1 {
160
160
 
161
161
 
162
162
  static setParamsFunctions(params) {
163
- ppUtil$2.defUnenumProp(params, "asNumber", function (name, minValue, maxValue, defaultValue) {
163
+ ppUtil$4.defUnenumProp(params, "asNumber", function (name, minValue, maxValue, defaultValue) {
164
164
  // if (!this.hasOwnProperty(name)) // {} 定义的对象没有 hasOwnProperty 函数
165
165
  if (this[name] === undefined) return defaultValue;
166
166
  let result = +this[name];
@@ -171,7 +171,7 @@ class apiUtil$1 {
171
171
 
172
172
  return result;
173
173
  });
174
- ppUtil$2.defUnenumProp(params, "asBool", function (name, defaultValue) {
174
+ ppUtil$4.defUnenumProp(params, "asBool", function (name, defaultValue) {
175
175
  if (this[name] === undefined) return defaultValue;
176
176
  let result = JSON.parse(this[name]);
177
177
  return !!result;
@@ -236,7 +236,7 @@ class apiUtil$1 {
236
236
 
237
237
  static apiFail(error, req) {
238
238
  configNeeded();
239
- let response = err$1.ERROR(error, err$1.errorLangParamFlag + ppUtil$2.getMsgLang(req));
239
+ let response = err$1.ERROR(error, err$1.errorLangParamFlag + ppUtil$4.getMsgLang(req));
240
240
  response.datetime = new Date();
241
241
 
242
242
  if (req && req.headers) {
@@ -484,7 +484,7 @@ class apiUtil$1 {
484
484
  const params = apiUtil$1.extractParams(req);
485
485
  req.accessToken = req.accessToken || apiUtil$1.extractToken(req);
486
486
 
487
- if (req.accessToken === MD5(ppUtil$2.idNumDisturbing)) {
487
+ if (req.accessToken === MD5(ppUtil$4.idNumDisturbing)) {
488
488
  //todo: 检查IP
489
489
  params.__internal__ = true;
490
490
  req.ignoreToken = true; // if (params.idNum || params.personId) {
@@ -541,7 +541,7 @@ class apiUtil$1 {
541
541
 
542
542
  if (dbgToken) {
543
543
  let envId = process.env.ECS_DEPLOY_ID;
544
- req.__debug__ = dbgToken && dbgToken === MD5(req.headers.timestamp, ppUtil$2.commonHashDisturbing) && (envId === "florist_longdan" || envId === "shuzi");
544
+ req.__debug__ = dbgToken && dbgToken === MD5(req.headers.timestamp, ppUtil$4.commonHashDisturbing) && (envId === "florist_longdan" || envId === "shuzi");
545
545
  } else {
546
546
  req.__debug__ = false;
547
547
  } // console.log("__debug__:", req.__debug__, "dbgToken:", dbgToken);
@@ -580,7 +580,7 @@ class apiUtil$1 {
580
580
 
581
581
 
582
582
  static tokenExpireTime(req) {
583
- return apiUtil$1.isDebugMode(req) ? 30 * 24 * 60 * 60 : apiUtil$1.isTestMode(req) ? 4 * 60 * 60 : appSetting.tokenExpireTime;
583
+ return apiUtil$1.isDebugMode(req) ? 30 * 24 * 60 * 60 : apiUtil$1.isTestMode(req) ? 4 * 60 * 60 : appSetting$1.tokenExpireTime;
584
584
  }
585
585
  /**
586
586
  * 获取用户令牌保存键
@@ -721,7 +721,7 @@ class apiUtil$1 {
721
721
  };
722
722
 
723
723
  if (rec.secret !== "") {
724
- rec.secret = ppUtil$2.getEncAse192(rec.secret);
724
+ rec.secret = ppUtil$4.getEncAse192(rec.secret);
725
725
  }
726
726
 
727
727
  req.apiCallRec = rec;
@@ -1021,7 +1021,7 @@ class apiUtil$1 {
1021
1021
  paramExists = typeof params[paramName] != 'undefined' || typeof params[paramName + paramSufix] != 'undefined';
1022
1022
  }
1023
1023
 
1024
- if (!paramExists) return ppUtil$2.errorPormise(err$1.ERROR(err$1.PARAMETER_NEEDED, paramName));
1024
+ if (!paramExists) return ppUtil$4.errorPormise(err$1.ERROR(err$1.PARAMETER_NEEDED, paramName));
1025
1025
  }
1026
1026
 
1027
1027
  return Promise.resolve(paramSufix);
@@ -1056,8 +1056,8 @@ class apiUtil$1 {
1056
1056
  static getEnvHosts(params) {
1057
1057
  configNeeded();
1058
1058
  return {
1059
- hosts: appSetting.serverHosts || ["localhost"],
1060
- port: appSetting.listenPort || +params.port || 8443
1059
+ hosts: appSetting$1.serverHosts || ["localhost"],
1060
+ port: appSetting$1.listenPort || +params.port || 8443
1061
1061
  };
1062
1062
  }
1063
1063
  /**
@@ -1070,7 +1070,7 @@ class apiUtil$1 {
1070
1070
 
1071
1071
  static async hostIsMySelf(host, req) {
1072
1072
  let clientIp = apiUtil$1.getClientIp(req);
1073
- return ppUtil$2.getMyIp({
1073
+ return ppUtil$4.getMyIp({
1074
1074
  hosts: [host, clientIp]
1075
1075
  });
1076
1076
  }
@@ -1085,9 +1085,9 @@ class apiUtil$1 {
1085
1085
  configNeeded();
1086
1086
  const params = options.replacements;
1087
1087
  const myEnvHosts = apiUtil$1.getEnvHosts(params);
1088
- const myIp = ppUtil$2.getMyIp();
1088
+ const myIp = ppUtil$4.getMyIp();
1089
1089
  const host = params.host || myIp;
1090
- if (!myEnvHosts.hosts.contains(host)) throw [err$1.ACCESS_REFUSED, `环境${conf$1.envId}貌似无此主机:${params.host}`];
1090
+ if (!myEnvHosts.hosts.contains(host)) throw [err$1.ACCESS_REFUSED, `环境${conf$2.envId}貌似无此主机:${params.host}`];
1091
1091
 
1092
1092
  if (!apiUtil$1.hostIsMySelf(host, options._res.req)) {
1093
1093
  let result = await apiUtil$1.forwardsTo(host, myEnvHosts.port, params, options._res.req, myIp);
@@ -1122,7 +1122,7 @@ class apiUtil$1 {
1122
1122
  url: url,
1123
1123
  data: params
1124
1124
  };
1125
- let result = await netUtil.apiRequest(reqOptions);
1125
+ let result = await netUtil$1.apiRequest(reqOptions);
1126
1126
 
1127
1127
  if (result.status !== 200) {
1128
1128
  console.log(`访问 ${reqOptions.url} 出错`, result.status, result.res.statusMessage);
@@ -1142,7 +1142,7 @@ class apiUtil$1 {
1142
1142
  let keys = Array.isArray(keyPath) ? keyPath : keyPath.split(".");
1143
1143
  let parentKey = "";
1144
1144
  let key = "config";
1145
- let value = conf$1;
1145
+ let value = conf$2;
1146
1146
  let parent = null;
1147
1147
 
1148
1148
  for (let i = 0; i < keys.length; i++) {
@@ -1387,7 +1387,7 @@ class apiUtil$1 {
1387
1387
  if (await apiUtil$1.checkForwardsHost(options)) return true;
1388
1388
  let c = apiUtil$1.parseCfgPath(item);
1389
1389
  delete c.parent;
1390
- c.host = ppUtil$2.getMyIp();
1390
+ c.host = ppUtil$4.getMyIp();
1391
1391
  return c;
1392
1392
  }
1393
1393
  /**
@@ -1415,7 +1415,7 @@ class apiUtil$1 {
1415
1415
  c.newValue = params.value;
1416
1416
  c.parent[c.key] = c.newValue;
1417
1417
  delete c.parent;
1418
- c.host = ppUtil$2.getMyIp();
1418
+ c.host = ppUtil$4.getMyIp();
1419
1419
  return c; // 应用层自行处理多机同步
1420
1420
  } //#endregion
1421
1421
 
@@ -1423,25 +1423,21 @@ class apiUtil$1 {
1423
1423
  }
1424
1424
 
1425
1425
  function configNeeded() {
1426
- if (!conf$1 || !err$1) {
1427
- ppUtil$2.configNeeded();
1428
- conf$1 = ppUtil$2.appConfig;
1429
- appSetting = conf$1.appSetting;
1430
- err$1 = ppUtil$2.appErrCfg;
1426
+ if (!conf$2 || !err$1) {
1427
+ ppUtil$4.configNeeded();
1428
+ conf$2 = ppUtil$4.appConfig;
1429
+ appSetting$1 = conf$2.appSetting;
1430
+ err$1 = ppUtil$4.appErrCfg;
1431
1431
  }
1432
1432
  }
1433
1433
 
1434
1434
  var ppUtilApi = apiUtil$1;
1435
1435
 
1436
- function commonjsRequire(path) {
1437
- throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
1438
- }
1439
-
1440
- const fs = require$$0__default$1["default"];
1436
+ const fs = require$$0__default$2["default"];
1441
1437
  const path = require$$1__default$1["default"];
1442
- const ppUtil$1 = require$$1__default["default"].ppUtil;
1438
+ const ppUtil$3 = require$$1__default["default"].ppUtil;
1443
1439
  const redisUtil = require$$2__default["default"].redisUtil;
1444
- let conf, err; // const dbCheck = require('../dbupdate/dd-version');
1440
+ let conf$1, err; // const dbCheck = require('../dbupdate/dd-version');
1445
1441
 
1446
1442
  /**
1447
1443
  * API服务启动时前置条件检查
@@ -1460,10 +1456,10 @@ class preconditions$1 {
1460
1456
  static checkPromises = [];
1461
1457
 
1462
1458
  static setAppName(aName) {
1463
- ppUtil$1.configNeeded();
1464
- conf = ppUtil$1.appConfig;
1465
- err = ppUtil$1.appErrCfg;
1466
- preconditions$1.appName = aName || conf.thisApp;
1459
+ ppUtil$3.configNeeded();
1460
+ conf$1 = ppUtil$3.appConfig;
1461
+ err = ppUtil$3.appErrCfg;
1462
+ preconditions$1.appName = aName || conf$1.thisApp;
1467
1463
  return preconditions$1;
1468
1464
  }
1469
1465
 
@@ -1520,8 +1516,8 @@ class preconditions$1 {
1520
1516
  */
1521
1517
 
1522
1518
 
1523
- static async checkExEnvSetting() {
1524
- return await checkExEnvSetting();
1519
+ static async checkExEnvSetting(loadjs) {
1520
+ return await checkExEnvSetting(loadjs);
1525
1521
  }
1526
1522
  /**
1527
1523
  * redis 可用性检查
@@ -1542,30 +1538,30 @@ class preconditions$1 {
1542
1538
 
1543
1539
  async function createEnvSettingFile(fileName) {
1544
1540
  try {
1545
- const cfgText = await ppUtil$1.doCreateEnvSettingFile(fileName);
1541
+ const cfgText = await ppUtil$3.doCreateEnvSettingFile(fileName);
1546
1542
 
1547
- if (cfgText.indexOf(`cfgVersion: "${conf._envSetting.cfgVersion}"`) > 0) {
1543
+ if (cfgText.indexOf(`cfgVersion: "${conf$1._envSetting.cfgVersion}"`) > 0) {
1548
1544
  console.log(`\n*** 已創建新的本機環境配置文件\n${fileName}\n请檢查各項內容是否正确(此项更新服务可正常运行)\n\n`);
1549
1545
  return "";
1550
1546
  }
1551
1547
 
1552
- return `已創建新的本機環境配置文件\n${fileName}\n请檢查並正確設置各項內容,然後將配置版本號修改為${conf._envSettings.default.cfgVersion}`;
1548
+ return `已創建新的本機環境配置文件\n${fileName}\n请檢查並正確設置各項內容,然後將配置版本號修改為${conf$1._envSettings.default.cfgVersion}`;
1553
1549
  } catch (e) {
1554
1550
  return `創建本機環境配置文件失敗\n《${fileName}》\n${err.ERROR(e).message}`;
1555
1551
  }
1556
1552
  }
1557
1553
 
1558
- async function checkExEnvSetting() {
1559
- if (!conf._envSetting.localSettingImported) {
1560
- let fileName = conf.getEnvSettingFileName();
1554
+ async function checkExEnvSetting(loadjs) {
1555
+ if (!conf$1._envSetting.localSettingImported) {
1556
+ let fileName = conf$1.getEnvSettingFileName();
1561
1557
 
1562
1558
  if (!fs.existsSync(fileName)) {
1563
1559
  return await createEnvSettingFile(fileName);
1564
1560
  }
1565
1561
 
1566
- let envSettingEx = commonjsRequire(fileName);
1562
+ let envSettingEx = loadjs(fileName);
1567
1563
 
1568
- if (envSettingEx.cfgVersion !== conf._envSettings.default.cfgVersion) {
1564
+ if (envSettingEx.cfgVersion !== conf$1._envSettings.default.cfgVersion) {
1569
1565
  if (envSettingEx.cfgVersion !== "0.0") {
1570
1566
  let fp = path.parse(fileName);
1571
1567
  let newFileName = path.join(fp.dir, fp.name + envSettingEx.cfgVersion + fp.ext);
@@ -1584,10 +1580,10 @@ async function checkExEnvSetting() {
1584
1580
 
1585
1581
 
1586
1582
  async function checkRedis() {
1587
- if (!conf.redis.enabled) return "";
1583
+ if (!conf$1.redis.enabled) return "";
1588
1584
  const redis = redisUtil.newClient("io"); // console.log(`checkRedis begin:`);
1589
1585
 
1590
- await ppUtil$1.wait(15 * 1000, param => {
1586
+ await ppUtil$3.wait(15 * 1000, param => {
1591
1587
  if (redis.ready && redis.connected || redis.error) {
1592
1588
  return true;
1593
1589
  }
@@ -1605,11 +1601,209 @@ async function checkRedis() {
1605
1601
 
1606
1602
  var ppPrecond = preconditions$1;
1607
1603
 
1604
+ var require$$0 = require$$2__default["default"];
1605
+
1606
+ function _interopDefaultLegacy$1(e) {
1607
+ return e && typeof e === 'object' && 'default' in e ? e : {
1608
+ 'default': e
1609
+ };
1610
+ }
1611
+
1612
+ var require$$0__default = /*#__PURE__*/_interopDefaultLegacy$1(require$$0); // const logger = require("log4js").getLogger();
1613
+
1614
+
1615
+ const redis = require$$0__default["default"].redisUtil;
1616
+
1617
+ class Messenger {
1618
+ constructor(appConfig, appErrCfg) {
1619
+ if (appConfig) {
1620
+ redis.config(appConfig, appErrCfg);
1621
+ }
1622
+
1623
+ this.channel = "ecs_messenger"; //default channel
1624
+
1625
+ this.publisher = redis.newClient("publisher");
1626
+ this.subscriber = redis.newClient("subscriber");
1627
+ this.subscriber.on("subscribe", function (channel, count) {
1628
+ console.log("chanel subscribed :", channel, count);
1629
+ });
1630
+ this.subscriber.on("message", this.handleMessage);
1631
+ }
1632
+
1633
+ setMessageHandle(handle, channel) {
1634
+ channel = channel || this.channel;
1635
+ Messenger.onmessage = Messenger.onmessage || {};
1636
+ Messenger.onmessage[channel] = Messenger.onmessage[channel] || [];
1637
+ const appMsgHandles = Messenger.onmessage[channel];
1638
+
1639
+ for (let i = 0; i < appMsgHandles.length; i++) {
1640
+ if (appMsgHandles[i] === handle) return this;
1641
+ }
1642
+
1643
+ appMsgHandles.push(handle);
1644
+ return this;
1645
+ }
1646
+
1647
+ subscribe(channel) {
1648
+ channel = channel || this.channel;
1649
+ this.subscriber.subscribe(channel);
1650
+ return this;
1651
+ }
1652
+
1653
+ async handleMessage(channel, message) {
1654
+ console.log(`通道[${channel}]收到订阅消息:`, message);
1655
+ let appMsgHandles = Messenger.onmessage[channel] || [];
1656
+
1657
+ for (let i = 0; i < appMsgHandles.length; i++) {
1658
+ const appMsgHandle = appMsgHandles[i];
1659
+
1660
+ try {
1661
+ let msgObj = JSON.parse(message); // await appMsgHandle(msgObj);
1662
+
1663
+ appMsgHandle(msgObj);
1664
+ } catch (e) {
1665
+ console.log(e);
1666
+ }
1667
+ }
1668
+ }
1669
+
1670
+ async publish(message, channel) {
1671
+ channel = channel || this.channel;
1672
+ let msgStr = typeof message === "object" ? JSON.stringify(message) : message.toString();
1673
+ console.log(`通道[${channel}]发布消息:`, message);
1674
+ await this.publisher.publish(channel, msgStr);
1675
+ return this;
1676
+ }
1677
+
1678
+ }
1679
+
1680
+ var ppMessenger$1 = Messenger;
1681
+ const ppMessenger = ppMessenger$1;
1682
+ var ppUtil$2 = ppMessenger;
1683
+ var ppUtil_1 = ppUtil$2;
1684
+
1685
+ // 若 redis 可用,则使用基于 redis 的消息订阅发布系统
1686
+ // 若 redis 不可用,则将消息转发到其它主机
1687
+
1688
+ const RedisMessenger = ppUtil_1;
1689
+ const {
1690
+ netUtil
1691
+ } = require$$3__default["default"];
1692
+ let conf;
1693
+
1694
+ class noneRedisMessenger {
1695
+ static onMessage = [];
1696
+
1697
+ static async publishMessage(message) {
1698
+ //没有消息处理器,则将消息转发到目标地址(不支持pm2多进程)
1699
+ if (message.destAddr) {
1700
+ await forwardMessage(message);
1701
+ } else {
1702
+ for (let i = 0; i < conf.appSetting.serverHosts.length; i++) {
1703
+ message.destAddr = conf.appSetting.serverHosts[i];
1704
+
1705
+ if (message.destAddr !== conf.myMsgAddr) {
1706
+ await forwardMessage(message);
1707
+ }
1708
+ }
1709
+ }
1710
+ }
1711
+
1712
+ static setMessageHandle(messageHandle) {
1713
+ if (noneRedisMessenger.onMessage.indexOf(messageHandle) < 0) {
1714
+ noneRedisMessenger.onMessage.push(messageHandle);
1715
+ }
1716
+
1717
+ return noneRedisMessenger;
1718
+ }
1719
+
1720
+ static async handleInternalMessage(options) {
1721
+ const message = options.replacements;
1722
+
1723
+ for (let i = 0; i < noneRedisMessenger.onMessage.length; i++) {
1724
+ const appMsgHandle = noneRedisMessenger.onMessage[i];
1725
+
1726
+ try {
1727
+ // await appMsgHandle(message);
1728
+ appMsgHandle(message);
1729
+ } catch (e) {
1730
+ console.log(e);
1731
+ }
1732
+ }
1733
+ }
1734
+
1735
+ }
1736
+
1737
+ class commonMessenger$1 {
1738
+ static myMsgAddr = "";
1739
+ static redisMessenger = null;
1740
+
1741
+ static initMessenger(appConfig, appErrCfg, msgChannel) {
1742
+ conf = appConfig;
1743
+ this.redisMessenger = conf.redis.enabled ? new RedisMessenger(appConfig, appErrCfg, msgChannel).subscribe(msgChannel) : null;
1744
+ this.myMsgAddr = commonMessenger$1.getMyMsgAddr();
1745
+ }
1746
+
1747
+ static getMyMsgAddr() {
1748
+ if (!conf.myMsgAddr) {
1749
+ conf.pm_id = process.env.pm_id || "0";
1750
+ let myIp = ppUtil.getMyIp();
1751
+
1752
+ if (this.redisMessenger) {
1753
+ myIp = `${conf.pm_id}@${myIp}`;
1754
+ }
1755
+
1756
+ conf.myMsgAddr = myIp;
1757
+ console.log(`myMsgAddr: `, conf.myMsgAddr);
1758
+ }
1759
+
1760
+ return conf.myMsgAddr;
1761
+ }
1762
+
1763
+ static setMessageHandle(messageHandle) {
1764
+ if (this.redisMessenger) {
1765
+ return this.redisMessenger.setMessageHandle(messageHandle, msgChannel);
1766
+ }
1767
+
1768
+ return noneRedisMessenger.setMessageHandle(messageHandle);
1769
+ }
1770
+
1771
+ static async publishMessage(message) {
1772
+ if (this.redisMessenger) {
1773
+ return this.redisMessenger.publish(message, msgChannel);
1774
+ } else {
1775
+ return noneRedisMessenger.publishMessage(message);
1776
+ }
1777
+ }
1778
+
1779
+ static async handleInternalMessage(options) {
1780
+ return await noneRedisMessenger.handleInternalMessage(options);
1781
+ }
1782
+
1783
+ }
1784
+
1785
+ async function forwardMessage(message) {
1786
+ let reqOptions = {
1787
+ url: `https://${message.destAddr}:${appSetting.listenPort}/api/message`,
1788
+ method: "POST",
1789
+ data: message
1790
+ };
1791
+ let res = await netUtil.apiRequest(reqOptions).catch(e => {
1792
+ console.log(`转发消息失败, url: ${reqOptions.url}`, e);
1793
+ throw e;
1794
+ });
1795
+ return res.data;
1796
+ }
1797
+
1798
+ var ppMessengerEx = commonMessenger$1;
1799
+
1608
1800
  const apiUtil = ppUtilApi;
1609
1801
  const preconditions = ppPrecond;
1610
- var ppUtil = {
1802
+ const commonMessenger = ppMessengerEx;
1803
+ var ppUtil$1 = {
1611
1804
  apiUtil,
1612
- preconditions
1805
+ preconditions,
1806
+ commonMessenger
1613
1807
  };
1614
1808
 
1615
- module.exports = ppUtil;
1809
+ module.exports = ppUtil$1;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "abler-api",
3
- "version": "0.1.21",
3
+ "version": "0.1.24",
4
4
  "description": "API服务相关工具",
5
5
  "main": "./dist/cjs/pp-util.js",
6
- "-module": "./dist/es/pp-util.js",
6
+ "module": "./dist/es/pp-util.js",
7
7
  "scripts": {
8
8
  "build": "npx rollup -c rollup.config.js",
9
9
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -17,6 +17,7 @@
17
17
  "q": "^1.5.1",
18
18
  "abler-util": ">=0.1.1",
19
19
  "abler-db": ">=0.1.1",
20
+ "abler-messenger": ">=0.1.1",
20
21
  "abler-net": ">=0.1.1"
21
22
  },
22
23
  "devDependencies": {