@tuya-sat/micro-dev-proxy 3.0.7-beta.1 → 3.0.7-beta.2
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 +22 -24
- package/package.json +1 -1
package/dist/appProxy.d.ts
CHANGED
|
@@ -36,5 +36,5 @@ export default class AppProxy {
|
|
|
36
36
|
getLoginCookie(): Promise<void>;
|
|
37
37
|
processReq(proxyReq: Parameters<Options['onProxyReq']>[0], req: any, res: any): void;
|
|
38
38
|
combinedHeader(prevHeadar: any): any;
|
|
39
|
-
processRes(responseBuffer: any): Promise<any>;
|
|
39
|
+
processRes(responseBuffer: any, proxyRes: any, req: any, res: any): Promise<any>;
|
|
40
40
|
}
|
package/dist/appProxy.js
CHANGED
|
@@ -45,7 +45,7 @@ const url_1 = require("url");
|
|
|
45
45
|
const fs = __importStar(require("fs"));
|
|
46
46
|
const path = __importStar(require("path"));
|
|
47
47
|
const mfPath = path.join(process.cwd(), 'manifest.json');
|
|
48
|
-
const mf = fs.existsSync(mfPath) && JSON.
|
|
48
|
+
const mf = fs.existsSync(mfPath) && JSON.parse(fs.readFileSync(mfPath, 'utf8'));
|
|
49
49
|
class AppProxy {
|
|
50
50
|
constructor({ target, username, password, additionHeaders, isMainApp, isThirdLogin, loginApi, csrf = true, logSign, }) {
|
|
51
51
|
this.loginCookie = new token_1.default();
|
|
@@ -104,7 +104,6 @@ class AppProxy {
|
|
|
104
104
|
}
|
|
105
105
|
processReq(proxyReq, req, res) {
|
|
106
106
|
//整合需要添加的header头
|
|
107
|
-
var _a;
|
|
108
107
|
const combineHeaders = this.combinedHeader(this.additionHeaders);
|
|
109
108
|
Object.entries(combineHeaders).forEach(([headerKey, value]) => proxyReq.setHeader(headerKey, value));
|
|
110
109
|
const prevCookie = proxyReq.getHeader('cookie') || '';
|
|
@@ -130,34 +129,14 @@ class AppProxy {
|
|
|
130
129
|
.join(';');
|
|
131
130
|
proxyReq.setHeader('cookie', cookie);
|
|
132
131
|
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
|
-
}
|
|
154
132
|
}
|
|
155
133
|
combinedHeader(prevHeadar) {
|
|
156
134
|
let proccessedHeader = Object.assign({}, prevHeadar);
|
|
157
135
|
this.csrf && (proccessedHeader[csrf_1.HEADERS_CSRF] = this.csrfValue.value);
|
|
158
136
|
return proccessedHeader;
|
|
159
137
|
}
|
|
160
|
-
processRes(responseBuffer) {
|
|
138
|
+
processRes(responseBuffer, proxyRes, req, res) {
|
|
139
|
+
var _a;
|
|
161
140
|
return __awaiter(this, void 0, void 0, function* () {
|
|
162
141
|
let response;
|
|
163
142
|
try {
|
|
@@ -167,6 +146,25 @@ class AppProxy {
|
|
|
167
146
|
this.log('----json parse responseBuffer string error-----', err);
|
|
168
147
|
return responseBuffer;
|
|
169
148
|
}
|
|
149
|
+
const apiPathMatchReg = /\/(:[^/]+|\$?{[^/]+})/g;
|
|
150
|
+
const apis = mf.apis || [];
|
|
151
|
+
if (!(req === null || req === void 0 ? void 0 : req.headers['micro-app-id'].includes('main-app'))) {
|
|
152
|
+
const method = (req === null || req === void 0 ? void 0 : req.method) || 'GET';
|
|
153
|
+
const urlPath = ((_a = req === null || req === void 0 ? void 0 : req.path) === null || _a === void 0 ? void 0 : _a.replace(/^\s*\/(custom-api|open-api)/, '')) || '';
|
|
154
|
+
let isExists = false;
|
|
155
|
+
apis.forEach((api) => {
|
|
156
|
+
const isLegal = new RegExp(`^${api.path.replace(apiPathMatchReg, '/[^/]+')}/?$`).test(urlPath);
|
|
157
|
+
if (isLegal && method.toLowerCase() === api.method.toLowerCase()) {
|
|
158
|
+
isExists = true;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
if (!isExists) {
|
|
162
|
+
return JSON.stringify({
|
|
163
|
+
msg: `api:${urlPath} 未在manifest.json中注册`,
|
|
164
|
+
code: '888888',
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
170
168
|
if (response.code === 1010 && !this.isMainApp) {
|
|
171
169
|
let modifyText = '';
|
|
172
170
|
yield this.getLoginCookie()
|