@tuya-sat/micro-dev-proxy 3.0.2 → 3.0.4

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.
@@ -1,5 +1,5 @@
1
- import Token from "./token";
2
- import { Options } from "http-proxy-middleware";
1
+ import Token from './token';
2
+ import { Options } from 'http-proxy-middleware';
3
3
  export interface AppProxyConfig {
4
4
  target: string;
5
5
  username: string;
@@ -11,6 +11,7 @@ export interface AppProxyConfig {
11
11
  };
12
12
  isMainApp?: boolean;
13
13
  logSign?: boolean;
14
+ isThirdLogin?: boolean;
14
15
  }
15
16
  export default class AppProxy {
16
17
  target: string;
@@ -28,11 +29,12 @@ export default class AppProxy {
28
29
  loginCookie: Token;
29
30
  csrfCookie: Token;
30
31
  csrfValue: Token;
31
- constructor({ target, username, password, additionHeaders, isMainApp, loginApi, csrf, logSign, }: AppProxyConfig);
32
+ isThirdLogin?: boolean;
33
+ constructor({ target, username, password, additionHeaders, isMainApp, isThirdLogin, loginApi, csrf, logSign, }: AppProxyConfig);
32
34
  init(): Promise<void>;
33
35
  getCsrf(): Promise<void>;
34
36
  getLoginCookie(): Promise<void>;
35
- processReq(proxyReq: Parameters<Options["onProxyReq"]>[0]): void;
37
+ processReq(proxyReq: Parameters<Options['onProxyReq']>[0]): void;
36
38
  combinedHeader(prevHeadar: any): any;
37
39
  processRes(responseBuffer: any): Promise<any>;
38
40
  }
package/dist/appProxy.js CHANGED
@@ -43,18 +43,21 @@ const token_1 = __importDefault(require("./token"));
43
43
  const util_1 = require("./util");
44
44
  const url_1 = require("url");
45
45
  class AppProxy {
46
- constructor({ target, username, password, additionHeaders, isMainApp, loginApi, csrf = true, logSign, }) {
46
+ constructor({ target, username, password, additionHeaders, isMainApp, isThirdLogin, loginApi, csrf = true, logSign, }) {
47
47
  this.loginCookie = new token_1.default();
48
48
  this.csrfCookie = new token_1.default();
49
49
  this.csrfValue = new token_1.default();
50
50
  this.target = target;
51
- this.username = username;
52
- this.password = (0, js_sha256_1.sha256)(password);
51
+ if (!isMainApp) {
52
+ this.username = username;
53
+ this.password = (0, js_sha256_1.sha256)(password);
54
+ }
53
55
  this.isMainApp = isMainApp;
54
56
  this.loginApi = loginApi;
55
57
  this.additionHeaders = additionHeaders;
56
58
  this.log = (0, util_1.logFactory)(logSign);
57
59
  this.csrf = csrf;
60
+ this.isThirdLogin = isThirdLogin;
58
61
  Object.assign(globalAxios_1.default.defaults.headers.common, this.additionHeaders);
59
62
  }
60
63
  init() {
@@ -68,13 +71,13 @@ class AppProxy {
68
71
  //获取csrf
69
72
  getCsrf() {
70
73
  return __awaiter(this, void 0, void 0, function* () {
71
- yield (0, csrf_1.default)(new url_1.URL("/login", this.target).href)
74
+ yield (0, csrf_1.default)(new url_1.URL('/login?disable_third_login=true', this.target).href)
72
75
  .then(([cookie, value]) => {
73
76
  this.csrfCookie.setValue(cookie);
74
77
  this.csrfValue.setValue(value);
75
78
  })
76
79
  .catch((err) => {
77
- this.log("----get csrf error----" + err);
80
+ this.log('----get csrf error----' + err);
78
81
  return Promise.reject(err);
79
82
  });
80
83
  });
@@ -90,7 +93,7 @@ class AppProxy {
90
93
  this.loginCookie.setValue(cookie);
91
94
  })
92
95
  .catch((err) => {
93
- this.log("----inital login error----" + err);
96
+ this.log('----inital login error----' + err);
94
97
  return Promise.reject(err);
95
98
  });
96
99
  });
@@ -99,11 +102,11 @@ class AppProxy {
99
102
  //整合需要添加的header头
100
103
  const combineHeaders = this.combinedHeader(this.additionHeaders);
101
104
  Object.entries(combineHeaders).forEach(([headerKey, value]) => proxyReq.setHeader(headerKey, value));
102
- const prevCookie = proxyReq.getHeader("cookie") || "";
105
+ const prevCookie = proxyReq.getHeader('cookie') || '';
103
106
  const cookie = [
104
107
  ...prevCookie
105
- .split(";")
106
- .map((item) => [item.split("=")[0].trim(), item])
108
+ .split(';')
109
+ .map((item) => [item.split('=')[0].trim(), item])
107
110
  //过滤字符
108
111
  .filter(([key]) => {
109
112
  if (csrf_1.COOKIE_CSRF === key) {
@@ -119,9 +122,9 @@ class AppProxy {
119
122
  this.loginCookie.value,
120
123
  ]
121
124
  .filter(Boolean)
122
- .join(";");
123
- proxyReq.setHeader("cookie", cookie);
124
- this.log("----req-header----", proxyReq.getHeaders());
125
+ .join(';');
126
+ proxyReq.setHeader('cookie', cookie);
127
+ this.log('----req-header----', proxyReq.getHeaders());
125
128
  }
126
129
  combinedHeader(prevHeadar) {
127
130
  let proccessedHeader = Object.assign({}, prevHeadar);
@@ -132,22 +135,22 @@ class AppProxy {
132
135
  return __awaiter(this, void 0, void 0, function* () {
133
136
  let response;
134
137
  try {
135
- response = JSON.parse(responseBuffer.toString("utf8"));
138
+ response = JSON.parse(responseBuffer.toString('utf8'));
136
139
  }
137
140
  catch (err) {
138
- this.log("----json parse responseBuffer string error-----", err);
141
+ this.log('----json parse responseBuffer string error-----', err);
139
142
  return responseBuffer;
140
143
  }
141
144
  if (response.code === 1010 && !this.isMainApp) {
142
- let modifyText = "";
145
+ let modifyText = '';
143
146
  yield this.getLoginCookie()
144
147
  .then((res) => {
145
- modifyText = "proxyInfo:cookie刷新成功,请刷新浏览器";
146
- this.log("----refresh success----");
148
+ modifyText = 'proxyInfo:cookie刷新成功,请刷新浏览器';
149
+ this.log('----refresh success----');
147
150
  })
148
151
  .catch((err) => {
149
- modifyText = "proxyInfo:cookie刷新失败,只能重新启动应用啦";
150
- this.log("----refresh failure----", err);
152
+ modifyText = 'proxyInfo:cookie刷新失败,只能重新启动应用啦';
153
+ this.log('----refresh failure----', err);
151
154
  });
152
155
  const data = Object.assign(Object.assign({}, response), { msg: (response === null || response === void 0 ? void 0 : response.msg) ? `${response === null || response === void 0 ? void 0 : response.msg}----${modifyText}` : modifyText });
153
156
  return JSON.stringify(data);
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AppProxyConfig } from "./appProxy";
1
+ import { AppProxyConfig } from './appProxy';
2
2
  export interface Config extends AppProxyConfig {
3
3
  needProxyApi?: {
4
4
  [key: string]: any;
package/dist/index.js CHANGED
@@ -29,7 +29,7 @@ function awaiting() {
29
29
  exports.awaiting = awaiting;
30
30
  function createProxy(_a) {
31
31
  var { additionHeaders = {} } = _a, restPrevConfig = __rest(_a, ["additionHeaders"]);
32
- const config = Object.assign({ additionHeaders: Object.assign({ terminalId: os_1.default.hostname() }, additionHeaders), isMainApp: false, loginApi: "/api/login", needProxyApi: ["/api", "/open-api", "/custom-api"], logSign: false, csrf: true }, restPrevConfig);
32
+ const config = Object.assign({ additionHeaders: Object.assign({ terminalId: os_1.default.hostname() }, additionHeaders), isMainApp: restPrevConfig.isMainApp, loginApi: '/api/login', needProxyApi: ['/api', '/open-api', '/custom-api'], logSign: false, csrf: true }, restPrevConfig);
33
33
  const { needProxyApi } = config, appProxyConfig = __rest(config, ["needProxyApi"]);
34
34
  const appProxy = new appProxy_1.default(appProxyConfig);
35
35
  return function middleware(app) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tuya-sat/micro-dev-proxy",
3
- "version": "3.0.2",
3
+ "version": "3.0.4",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {