abler-api 0.1.60 → 0.1.63
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/dist/cjs/pp-util.js +98 -27
- package/package.json +1 -1
package/dist/cjs/pp-util.js
CHANGED
|
@@ -26,8 +26,8 @@ const crypto = require$$0__default["default"];
|
|
|
26
26
|
const getBasicAuthorization = require$$1__default["default"];
|
|
27
27
|
const ppUtil$4 = require$$2__default["default"].ppUtil;
|
|
28
28
|
const {
|
|
29
|
-
dbUtil,
|
|
30
|
-
kvStorage
|
|
29
|
+
dbUtil: dbUtil$1,
|
|
30
|
+
kvStorage: kvStorage$1
|
|
31
31
|
} = require$$3__default["default"];
|
|
32
32
|
const {
|
|
33
33
|
netUtil: netUtil$1
|
|
@@ -465,8 +465,14 @@ class apiUtil$2 {
|
|
|
465
465
|
configNeeded();
|
|
466
466
|
|
|
467
467
|
if (!req.accessToken) {
|
|
468
|
-
|
|
469
|
-
|
|
468
|
+
let token = req.header('Authorization');
|
|
469
|
+
|
|
470
|
+
if (token && token.startsWith('Bearer ')) {
|
|
471
|
+
token = token.substring(7);
|
|
472
|
+
} else {
|
|
473
|
+
//检查post的信息或者url查询参数或者头信息
|
|
474
|
+
token = req.headers[hnToken] || req.headers['x-access-token']; //向后兼容
|
|
475
|
+
}
|
|
470
476
|
|
|
471
477
|
if (!token) {
|
|
472
478
|
let params = apiUtil$2.extractParams(req);
|
|
@@ -669,17 +675,17 @@ class apiUtil$2 {
|
|
|
669
675
|
tokenData.certPublicKey = req.certPublicKey;
|
|
670
676
|
}
|
|
671
677
|
|
|
672
|
-
await kvStorage.storeObj(token, tokenData, timeout);
|
|
678
|
+
await kvStorage$1.storeObj(token, tokenData, timeout);
|
|
673
679
|
|
|
674
680
|
if (dataId) {
|
|
675
|
-
await kvStorage.restoreValue("token_" + dataId).then(async oldToken => {
|
|
681
|
+
await kvStorage$1.restoreValue("token_" + dataId).then(async oldToken => {
|
|
676
682
|
if (oldToken !== token) {
|
|
677
|
-
await kvStorage.unstoreValue(oldToken);
|
|
683
|
+
await kvStorage$1.unstoreValue(oldToken);
|
|
678
684
|
} else {
|
|
679
685
|
dataId = "";
|
|
680
686
|
}
|
|
681
687
|
}, function (e) {}).then(async () => {
|
|
682
|
-
if (dataId !== "") await kvStorage.storeValue("token_" + dataId, token, timeout);
|
|
688
|
+
if (dataId !== "") await kvStorage$1.storeValue("token_" + dataId, token, timeout);
|
|
683
689
|
});
|
|
684
690
|
}
|
|
685
691
|
}
|
|
@@ -692,7 +698,7 @@ class apiUtil$2 {
|
|
|
692
698
|
|
|
693
699
|
|
|
694
700
|
static async restoreTokenData(accessToken, expireTime) {
|
|
695
|
-
return await kvStorage.restoreObj(accessToken, expireTime);
|
|
701
|
+
return await kvStorage$1.restoreObj(accessToken, expireTime);
|
|
696
702
|
}
|
|
697
703
|
/**
|
|
698
704
|
* 创建 API 调用记录
|
|
@@ -705,7 +711,7 @@ class apiUtil$2 {
|
|
|
705
711
|
static createApiCallRec(tokenData, req) {
|
|
706
712
|
if (!req.apiCallRec) {
|
|
707
713
|
if (req.apiCallRecId) {
|
|
708
|
-
return dbUtil.dbQueryOne(dbSql.loadApiUsage, {
|
|
714
|
+
return dbUtil$1.dbQueryOne(dbSql.loadApiUsage, {
|
|
709
715
|
replacements: {
|
|
710
716
|
id: req.apiCallRecId
|
|
711
717
|
}
|
|
@@ -780,7 +786,7 @@ class apiUtil$2 {
|
|
|
780
786
|
|
|
781
787
|
|
|
782
788
|
static async queryCompanyInfo(companyId, nullable) {
|
|
783
|
-
return await dbUtil.dbQueryOne(dbSql.queryCompanyInfo, {
|
|
789
|
+
return await dbUtil$1.dbQueryOne(dbSql.queryCompanyInfo, {
|
|
784
790
|
companyId
|
|
785
791
|
}, nullable);
|
|
786
792
|
}
|
|
@@ -799,7 +805,7 @@ class apiUtil$2 {
|
|
|
799
805
|
certSpare: null,
|
|
800
806
|
certSpareId: ''
|
|
801
807
|
};
|
|
802
|
-
return await dbUtil.dbQueryOne(dbSql.queryCertDataByOwner, {
|
|
808
|
+
return await dbUtil$1.dbQueryOne(dbSql.queryCertDataByOwner, {
|
|
803
809
|
ownerId: companyId,
|
|
804
810
|
applyId: "company"
|
|
805
811
|
}, false).then(certRec => {
|
|
@@ -810,7 +816,7 @@ class apiUtil$2 {
|
|
|
810
816
|
|
|
811
817
|
certInfo.certLiving = pubKeyData;
|
|
812
818
|
certInfo.certLivingId = certRec.id;
|
|
813
|
-
return dbUtil.dbQueryOne(dbSql.queryCertDataByOwner, {
|
|
819
|
+
return dbUtil$1.dbQueryOne(dbSql.queryCertDataByOwner, {
|
|
814
820
|
ownerId: companyId,
|
|
815
821
|
applyId: "company_spare"
|
|
816
822
|
}, false).then(certRec1 => {
|
|
@@ -930,14 +936,14 @@ class apiUtil$2 {
|
|
|
930
936
|
if (verifyOK) {
|
|
931
937
|
// 备用证书验证成功了,则将备用证书替换为正式证书
|
|
932
938
|
console.log(`公司(${tokenData.apiKey})备用证书验签成功,正式启用...`);
|
|
933
|
-
return dbUtil.dbTrans(trans => {
|
|
934
|
-
let p1 = dbUtil.dbExec(dbSql.deleteCertRecById, {
|
|
939
|
+
return dbUtil$1.dbTrans(trans => {
|
|
940
|
+
let p1 = dbUtil$1.dbExec(dbSql.deleteCertRecById, {
|
|
935
941
|
replacements: {
|
|
936
942
|
id: tokenData.certLivingId
|
|
937
943
|
},
|
|
938
944
|
transaction: trans
|
|
939
945
|
});
|
|
940
|
-
let p2 = dbUtil.dbExec(dbSql.spareCertToLiving, {
|
|
946
|
+
let p2 = dbUtil$1.dbExec(dbSql.spareCertToLiving, {
|
|
941
947
|
replacements: {
|
|
942
948
|
id: tokenData.certSpareId
|
|
943
949
|
},
|
|
@@ -1021,7 +1027,7 @@ class apiUtil$2 {
|
|
|
1021
1027
|
|
|
1022
1028
|
|
|
1023
1029
|
static tokenDataExist(token) {
|
|
1024
|
-
return kvStorage.stored(token);
|
|
1030
|
+
return kvStorage$1.stored(token);
|
|
1025
1031
|
}
|
|
1026
1032
|
|
|
1027
1033
|
static parametersOK(params) {
|
|
@@ -1572,7 +1578,12 @@ var ppUtilApi = apiUtil$2;
|
|
|
1572
1578
|
const fs = require$$5__default["default"];
|
|
1573
1579
|
const path = require$$1__default$1["default"];
|
|
1574
1580
|
const ppUtil$3 = require$$2__default["default"].ppUtil;
|
|
1575
|
-
const
|
|
1581
|
+
const {
|
|
1582
|
+
dbUtil,
|
|
1583
|
+
ppRedis,
|
|
1584
|
+
kvStorage,
|
|
1585
|
+
redisSimulator
|
|
1586
|
+
} = require$$3__default["default"];
|
|
1576
1587
|
let conf$2, err$1; // const dbCheck = require('../dbupdate/dd-version');
|
|
1577
1588
|
|
|
1578
1589
|
/**
|
|
@@ -1604,10 +1615,11 @@ class preconditions$1 {
|
|
|
1604
1615
|
return preconditions$1;
|
|
1605
1616
|
}
|
|
1606
1617
|
|
|
1607
|
-
static addChecker(checkFunc, args) {
|
|
1618
|
+
static addChecker(checkFunc, args, blocked) {
|
|
1608
1619
|
preconditions$1.checkFunctions.push({
|
|
1609
1620
|
check: checkFunc,
|
|
1610
|
-
args: args
|
|
1621
|
+
args: args,
|
|
1622
|
+
blocked: blocked
|
|
1611
1623
|
});
|
|
1612
1624
|
return preconditions$1;
|
|
1613
1625
|
}
|
|
@@ -1620,17 +1632,30 @@ class preconditions$1 {
|
|
|
1620
1632
|
|
|
1621
1633
|
preconditions$1.messages.push(`${preconditions$1.messages.length + 1}. ${msg}`);
|
|
1622
1634
|
}
|
|
1635
|
+
|
|
1636
|
+
return msg;
|
|
1623
1637
|
}
|
|
1624
1638
|
|
|
1625
1639
|
static async checkAll() {
|
|
1626
|
-
for (let i = 0; i < preconditions
|
|
1627
|
-
|
|
1640
|
+
// for (let i = 0; i < preconditions.checkFunctions.length; i++) {
|
|
1641
|
+
// const f = preconditions.checkFunctions[i];
|
|
1642
|
+
// // preconditions.addCheckPromise(f.check(f.args));
|
|
1643
|
+
// preconditions.pushMessage(await f.check(f.args));
|
|
1644
|
+
// }
|
|
1645
|
+
while (preconditions$1.checkFunctions.length > 0) {
|
|
1646
|
+
const f = preconditions$1.checkFunctions.shift();
|
|
1628
1647
|
|
|
1629
|
-
preconditions$1.pushMessage(await f.check(f.args))
|
|
1630
|
-
|
|
1648
|
+
if (preconditions$1.pushMessage(await f.check(f.args)) && f.blocked) {
|
|
1649
|
+
break;
|
|
1650
|
+
}
|
|
1651
|
+
} // for (let i = 0; i < preconditions.checkPromises.length; i++) {
|
|
1652
|
+
// preconditions.pushMessage(await preconditions.checkPromises[i]);
|
|
1653
|
+
// }
|
|
1631
1654
|
|
|
1632
|
-
|
|
1633
|
-
|
|
1655
|
+
|
|
1656
|
+
while (preconditions$1.checkPromises.length > 0) {
|
|
1657
|
+
const p = preconditions$1.checkPromises.shift();
|
|
1658
|
+
preconditions$1.pushMessage(await p);
|
|
1634
1659
|
}
|
|
1635
1660
|
|
|
1636
1661
|
let text = "";
|
|
@@ -1665,6 +1690,22 @@ class preconditions$1 {
|
|
|
1665
1690
|
|
|
1666
1691
|
static async checkRedis() {
|
|
1667
1692
|
return await checkRedis();
|
|
1693
|
+
}
|
|
1694
|
+
/**
|
|
1695
|
+
* 初始化数据库连接
|
|
1696
|
+
*/
|
|
1697
|
+
|
|
1698
|
+
|
|
1699
|
+
static async initDb() {
|
|
1700
|
+
return await initDb();
|
|
1701
|
+
}
|
|
1702
|
+
/**
|
|
1703
|
+
* 初始化键值对存储器
|
|
1704
|
+
*/
|
|
1705
|
+
|
|
1706
|
+
|
|
1707
|
+
static async initKvStorage(dbSql) {
|
|
1708
|
+
return await initKvStorage(dbSql);
|
|
1668
1709
|
} // /**
|
|
1669
1710
|
// * 数据库可用性及业务数据版本版本检查
|
|
1670
1711
|
// */
|
|
@@ -1712,7 +1753,7 @@ async function checkExEnvSetting(loadjs) {
|
|
|
1712
1753
|
|
|
1713
1754
|
let msg = await createEnvSettingFile(fileName, envSettingEx);
|
|
1714
1755
|
|
|
1715
|
-
if (
|
|
1756
|
+
if (msg) {
|
|
1716
1757
|
return msg;
|
|
1717
1758
|
}
|
|
1718
1759
|
|
|
@@ -1746,6 +1787,36 @@ async function checkRedis() {
|
|
|
1746
1787
|
|
|
1747
1788
|
}
|
|
1748
1789
|
|
|
1790
|
+
async function initDb() {
|
|
1791
|
+
try {
|
|
1792
|
+
dbUtil.config(conf$2, err$1);
|
|
1793
|
+
} catch (e) {
|
|
1794
|
+
if (e.reason === 'bad decrypt') {
|
|
1795
|
+
e = new Error(`解密数据库密码(${conf$2.dbconn.dbpwd})失败`);
|
|
1796
|
+
} // if (e.reason === 'bad decrypt') {
|
|
1797
|
+
// e = new Error(`解密Redis密码(${conf.redis.password})失败`);
|
|
1798
|
+
// }
|
|
1799
|
+
|
|
1800
|
+
|
|
1801
|
+
return `初始化数据库连接失败(${e.message})`;
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1805
|
+
async function initKvStorage(dbSql) {
|
|
1806
|
+
try {
|
|
1807
|
+
kvStorage.config(conf$2, err$1);
|
|
1808
|
+
|
|
1809
|
+
if (!conf$2.redis.enabled) {
|
|
1810
|
+
kvStorage.setRedis(new redisSimulator(dbSql));
|
|
1811
|
+
}
|
|
1812
|
+
} catch (e) {
|
|
1813
|
+
// if (e.reason === 'bad decrypt') {
|
|
1814
|
+
// e = new Error(`解密Redis密码(${conf.redis.password})失败`);
|
|
1815
|
+
// }
|
|
1816
|
+
return `初始化键值对存储器失败(${e.message})`;
|
|
1817
|
+
}
|
|
1818
|
+
}
|
|
1819
|
+
|
|
1749
1820
|
var ppPrecond = preconditions$1;
|
|
1750
1821
|
|
|
1751
1822
|
// 若 redis 可用,则使用基于 redis 的消息订阅发布系统
|