@tuya-sat/micro-dev-proxy 3.0.6 → 3.0.7-beta.1
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/appProxy.d.ts +1 -1
- package/dist/appProxy.js +27 -1
- package/package.json +1 -1
package/dist/appProxy.d.ts
CHANGED
|
@@ -34,7 +34,7 @@ export default class AppProxy {
|
|
|
34
34
|
init(): Promise<void>;
|
|
35
35
|
getCsrf(): Promise<void>;
|
|
36
36
|
getLoginCookie(): Promise<void>;
|
|
37
|
-
processReq(proxyReq: Parameters<Options['onProxyReq']>[0]): void;
|
|
37
|
+
processReq(proxyReq: Parameters<Options['onProxyReq']>[0], req: any, res: any): void;
|
|
38
38
|
combinedHeader(prevHeadar: any): any;
|
|
39
39
|
processRes(responseBuffer: any): Promise<any>;
|
|
40
40
|
}
|
package/dist/appProxy.js
CHANGED
|
@@ -42,6 +42,10 @@ const initLogin_1 = __importStar(require("./initLogin"));
|
|
|
42
42
|
const token_1 = __importDefault(require("./token"));
|
|
43
43
|
const util_1 = require("./util");
|
|
44
44
|
const url_1 = require("url");
|
|
45
|
+
const fs = __importStar(require("fs"));
|
|
46
|
+
const path = __importStar(require("path"));
|
|
47
|
+
const mfPath = path.join(process.cwd(), 'manifest.json');
|
|
48
|
+
const mf = fs.existsSync(mfPath) && JSON.stringify(fs.readFileSync(mfPath, 'utf8'));
|
|
45
49
|
class AppProxy {
|
|
46
50
|
constructor({ target, username, password, additionHeaders, isMainApp, isThirdLogin, loginApi, csrf = true, logSign, }) {
|
|
47
51
|
this.loginCookie = new token_1.default();
|
|
@@ -98,8 +102,9 @@ class AppProxy {
|
|
|
98
102
|
});
|
|
99
103
|
});
|
|
100
104
|
}
|
|
101
|
-
processReq(proxyReq) {
|
|
105
|
+
processReq(proxyReq, req, res) {
|
|
102
106
|
//整合需要添加的header头
|
|
107
|
+
var _a;
|
|
103
108
|
const combineHeaders = this.combinedHeader(this.additionHeaders);
|
|
104
109
|
Object.entries(combineHeaders).forEach(([headerKey, value]) => proxyReq.setHeader(headerKey, value));
|
|
105
110
|
const prevCookie = proxyReq.getHeader('cookie') || '';
|
|
@@ -125,6 +130,27 @@ class AppProxy {
|
|
|
125
130
|
.join(';');
|
|
126
131
|
proxyReq.setHeader('cookie', cookie);
|
|
127
132
|
this.log('----req-header----', proxyReq.getHeaders());
|
|
133
|
+
//todo: 拦截请求,判断是否在微应用manifest中存在
|
|
134
|
+
const apiPathMatchReg = /\/(:[^/]+|\$?{[^/]+})/g;
|
|
135
|
+
const apis = mf.apis || [];
|
|
136
|
+
if (!(proxyReq === null || proxyReq === void 0 ? void 0 : proxyReq._headers['micro-app-id'].includes('main-app'))) {
|
|
137
|
+
const method = (proxyReq === null || proxyReq === void 0 ? void 0 : proxyReq.method) || 'GET';
|
|
138
|
+
const urlPath = ((_a = proxyReq === null || proxyReq === void 0 ? void 0 : proxyReq.path) === null || _a === void 0 ? void 0 : _a.replace(/^\s*\/(custom-api|open-api)/, '')) ||
|
|
139
|
+
'';
|
|
140
|
+
let isExists = false;
|
|
141
|
+
apis.array.forEach((api) => {
|
|
142
|
+
const isLegal = new RegExp(`^${api.path.replace(apiPathMatchReg, '/[^/]+')}/?$`).test(urlPath);
|
|
143
|
+
if (isLegal && method.toLowerCase() === api.method.toLowerCase()) {
|
|
144
|
+
isExists = true;
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
if (!isExists) {
|
|
148
|
+
res.send(JSON.stringify({
|
|
149
|
+
msg: `api:${urlPath} 未在manifest.json中注册`,
|
|
150
|
+
code: '888888',
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
128
154
|
}
|
|
129
155
|
combinedHeader(prevHeadar) {
|
|
130
156
|
let proccessedHeader = Object.assign({}, prevHeadar);
|