@tuya-sat/micro-dev-proxy 3.2.23 → 3.3.0
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 +2 -0
- package/dist/appProxy.js +17 -4
- package/dist/index.js +3 -1
- package/package.json +1 -1
package/dist/appProxy.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export interface AppProxyConfig {
|
|
|
17
17
|
export declare function GSdf(): any;
|
|
18
18
|
export default class AppProxy {
|
|
19
19
|
target: string;
|
|
20
|
+
targetOrigin: string;
|
|
20
21
|
username: string;
|
|
21
22
|
password: string;
|
|
22
23
|
csrf: boolean;
|
|
@@ -37,6 +38,7 @@ export default class AppProxy {
|
|
|
37
38
|
init(): Promise<void>;
|
|
38
39
|
needManualLogin(): boolean;
|
|
39
40
|
isLoginApi(url: string): boolean;
|
|
41
|
+
isApiBelongMainApp(url: string): boolean;
|
|
40
42
|
getCsrf(): Promise<void>;
|
|
41
43
|
getLoginCookie(): Promise<void>;
|
|
42
44
|
getCookie(prevCookie?: string): string;
|
package/dist/appProxy.js
CHANGED
|
@@ -68,9 +68,12 @@ class AppProxy {
|
|
|
68
68
|
this.csrfCookie = new token_1.default();
|
|
69
69
|
this.csrfValue = new token_1.default();
|
|
70
70
|
this.target = target;
|
|
71
|
+
this.targetOrigin = new url_1.URL(this.target).origin;
|
|
71
72
|
if (!isMainApp) {
|
|
72
73
|
this.username = username;
|
|
73
|
-
|
|
74
|
+
if (password) {
|
|
75
|
+
this.password = (0, js_sha256_1.sha256)(password);
|
|
76
|
+
}
|
|
74
77
|
}
|
|
75
78
|
this.isMainApp = isMainApp;
|
|
76
79
|
this.loginApi = loginApi;
|
|
@@ -107,7 +110,17 @@ class AppProxy {
|
|
|
107
110
|
this.loginApi,
|
|
108
111
|
'/api/login',
|
|
109
112
|
'/api/login/qrcode'
|
|
110
|
-
].includes(url.replace(
|
|
113
|
+
].includes(url.replace(/\/+$/, ''));
|
|
114
|
+
}
|
|
115
|
+
isApiBelongMainApp(url) {
|
|
116
|
+
return [
|
|
117
|
+
this.loginApi,
|
|
118
|
+
'/api/login',
|
|
119
|
+
'/api/login/qrcode',
|
|
120
|
+
'/open-api/v1.0/current/user',
|
|
121
|
+
'/open-api/v1.0/m/life/web/user/info',
|
|
122
|
+
'/open-api/v1.0/m/life/web/mqtt/connection/config',
|
|
123
|
+
].includes(url.replace(/\/+$/, ''));
|
|
111
124
|
}
|
|
112
125
|
//获取csrf
|
|
113
126
|
getCsrf() {
|
|
@@ -189,6 +202,7 @@ class AppProxy {
|
|
|
189
202
|
Object.entries(combineHeaders).forEach(([headerKey, value]) => proxyReq.setHeader(headerKey, value));
|
|
190
203
|
const prevCookie = proxyReq.getHeader('cookie') || '';
|
|
191
204
|
proxyReq.setHeader('cookie', this.getCookie(prevCookie));
|
|
205
|
+
proxyReq.setHeader('origin', this.targetOrigin);
|
|
192
206
|
}
|
|
193
207
|
combinedHeader(prevHeadar) {
|
|
194
208
|
let proccessedHeader = Object.assign({}, prevHeadar);
|
|
@@ -212,9 +226,8 @@ class AppProxy {
|
|
|
212
226
|
this.log('----json parse responseBuffer string error-----', ((_a = err.response) === null || _a === void 0 ? void 0 : _a.data) || err.message);
|
|
213
227
|
return responseBuffer;
|
|
214
228
|
}
|
|
215
|
-
const isApiBelongMainApp = [this.loginApi, '/open-api/v1.0/current/user'];
|
|
216
229
|
const isMicroAppPage = (_b = req.referer) === null || _b === void 0 ? void 0 : _b.includes('/apps');
|
|
217
|
-
if (!this.isMainApp && isMicroAppPage && !isApiBelongMainApp
|
|
230
|
+
if (!this.isMainApp && isMicroAppPage && !this.isApiBelongMainApp(req.url)) {
|
|
218
231
|
const apiPathMatchReg = /\/(:[^/]+|\$?{[^/]+})/g;
|
|
219
232
|
const apis = mf.apis || [];
|
|
220
233
|
const microAppId = req.headers['micro-app-id'];
|
package/dist/index.js
CHANGED
|
@@ -35,7 +35,7 @@ function awaiting() {
|
|
|
35
35
|
exports.awaiting = awaiting;
|
|
36
36
|
function createProxy(_a) {
|
|
37
37
|
var { additionHeaders = {} } = _a, restPrevConfig = __rest(_a, ["additionHeaders"]);
|
|
38
|
-
const config = Object.assign({ additionHeaders: Object.assign({ terminalId: os_1.default.hostname() + Date.now() }, additionHeaders), isMainApp: restPrevConfig.isMainApp, loginApi: '/api/login', needProxyApi: ['/api', '/open-api', '/custom-api'], logSign: false, csrf: true }, restPrevConfig);
|
|
38
|
+
const config = Object.assign({ additionHeaders: Object.assign({ terminalId: os_1.default.hostname() + Date.now() }, additionHeaders), isMainApp: restPrevConfig.isMainApp, loginApi: '/api/login', needProxyApi: ['/api', '/open-api', '/custom-api', '/ws'], logSign: false, csrf: true }, restPrevConfig);
|
|
39
39
|
const { needProxyApi } = config, appProxyConfig = __rest(config, ["needProxyApi"]);
|
|
40
40
|
const appProxy = new appProxy_1.default(appProxyConfig);
|
|
41
41
|
return function middleware(app) {
|
|
@@ -47,9 +47,11 @@ function createProxy(_a) {
|
|
|
47
47
|
});
|
|
48
48
|
app.use(needProxyApi, (0, http_proxy_middleware_1.createProxyMiddleware)({
|
|
49
49
|
target: appProxyConfig.target,
|
|
50
|
+
ws: true,
|
|
50
51
|
secure: false,
|
|
51
52
|
changeOrigin: true,
|
|
52
53
|
selfHandleResponse: true,
|
|
54
|
+
onProxyReqWs: appProxy.processReq.bind(appProxy),
|
|
53
55
|
onProxyReq: appProxy.processReq.bind(appProxy),
|
|
54
56
|
onProxyRes: (0, http_proxy_middleware_1.responseInterceptor)(appProxy.processRes.bind(appProxy)),
|
|
55
57
|
}));
|