@tuya-sat/micro-dev-proxy 3.0.29 → 3.1.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.
@@ -1,5 +1,6 @@
1
1
  import Token from './token';
2
2
  import { Options } from 'http-proxy-middleware';
3
+ import Spinnies from 'spinnies';
3
4
  export interface AppProxyConfig {
4
5
  target: string;
5
6
  username: string;
@@ -31,6 +32,7 @@ export default class AppProxy {
31
32
  csrfCookie: Token;
32
33
  csrfValue: Token;
33
34
  isThirdLogin?: boolean;
35
+ spinnies: Spinnies;
34
36
  constructor({ target, username, password, additionHeaders, isMainApp, isThirdLogin, loginApi, csrf, logSign, }: AppProxyConfig);
35
37
  init(): Promise<void>;
36
38
  getCsrf(): Promise<void>;
package/dist/appProxy.js CHANGED
@@ -39,13 +39,14 @@ exports.GSdf = void 0;
39
39
  const globalAxios_1 = __importDefault(require("./globalAxios"));
40
40
  const js_sha256_1 = require("js-sha256");
41
41
  const csrf_1 = __importStar(require("./csrf"));
42
- const initLogin_1 = __importDefault(require("./initLogin"));
42
+ const initLogin_1 = __importStar(require("./initLogin"));
43
43
  const token_1 = __importDefault(require("./token"));
44
44
  const util_1 = require("./util");
45
45
  const url_1 = require("url");
46
46
  const fs = __importStar(require("fs"));
47
47
  const path = __importStar(require("path"));
48
48
  const undici_1 = require("undici");
49
+ const spinnies_1 = __importDefault(require("spinnies"));
49
50
  const mfPath = path.join(process.cwd(), 'manifest.json');
50
51
  const mf = fs.existsSync(mfPath) && JSON.parse(fs.readFileSync(mfPath, 'utf8'));
51
52
  let SDF;
@@ -58,6 +59,10 @@ function GSdf() {
58
59
  return SDF;
59
60
  }
60
61
  exports.GSdf = GSdf;
62
+ const SPIN_LABEL_NAME = {
63
+ csrf: '获取 csrf ',
64
+ login: '登录 SaaS '
65
+ };
61
66
  class AppProxy {
62
67
  constructor({ target, username, password, additionHeaders, isMainApp, isThirdLogin, loginApi, csrf = true, logSign, }) {
63
68
  this.loginCookie = new token_1.default();
@@ -74,14 +79,26 @@ class AppProxy {
74
79
  this.log = (0, util_1.logFactory)(logSign);
75
80
  this.csrf = csrf;
76
81
  this.isThirdLogin = isThirdLogin;
82
+ this.spinnies = new spinnies_1.default();
77
83
  Object.assign(globalAxios_1.default.defaults.headers.common, this.additionHeaders);
78
84
  }
79
85
  init() {
80
86
  return __awaiter(this, void 0, void 0, function* () {
87
+ this.spinnies.add('\n');
88
+ this.spinnies.add('csrf', { text: SPIN_LABEL_NAME.csrf });
89
+ this.spinnies.add('login', { text: SPIN_LABEL_NAME.login });
81
90
  this.csrf && (yield this.getCsrf());
82
- if (this.isMainApp)
83
- return;
84
- yield this.getLoginCookie();
91
+ if (!this.isMainApp) {
92
+ try {
93
+ yield this.getLoginCookie();
94
+ }
95
+ catch (e) {
96
+ throw e;
97
+ }
98
+ finally {
99
+ this.spinnies.stopAll();
100
+ }
101
+ }
85
102
  });
86
103
  }
87
104
  //获取csrf
@@ -91,9 +108,12 @@ class AppProxy {
91
108
  .then(([cookie, value]) => {
92
109
  this.csrfCookie.setValue(cookie);
93
110
  this.csrfValue.setValue(value);
111
+ this.spinnies.succeed('csrf');
94
112
  })
95
113
  .catch((err) => {
96
- this.log('----get csrf error----' + err);
114
+ this.spinnies.fail('csrf', {
115
+ text: `${SPIN_LABEL_NAME.csrf}\n\r ${err.message}`
116
+ });
97
117
  return Promise.reject(err);
98
118
  });
99
119
  });
@@ -101,6 +121,9 @@ class AppProxy {
101
121
  //模拟登录
102
122
  getLoginCookie() {
103
123
  return __awaiter(this, void 0, void 0, function* () {
124
+ if (!this.spinnies.pick('login')) {
125
+ this.spinnies.add('login', { text: SPIN_LABEL_NAME.login });
126
+ }
104
127
  const { href: loginUrl } = new url_1.URL(this.loginApi, this.target);
105
128
  yield (0, initLogin_1.default)(loginUrl, { username: this.username, password: this.password }, {
106
129
  headers: this.combinedHeader({ cookie: this.csrfCookie.value }),
@@ -114,10 +137,15 @@ class AppProxy {
114
137
  SDF = JSON.parse(parse_SDF(data));
115
138
  });
116
139
  });
140
+ this.spinnies.succeed('login');
117
141
  })
118
142
  .catch((err) => {
119
- this.log('----inital login error----' + err);
120
- return Promise.reject(err);
143
+ var _a, _b;
144
+ const errMsg = ((_a = err.response) === null || _a === void 0 ? void 0 : _a.data) ? JSON.stringify((_b = err.response) === null || _b === void 0 ? void 0 : _b.data) : err.message;
145
+ this.spinnies.fail('login', {
146
+ text: `${SPIN_LABEL_NAME.login}\n\r ${errMsg}`
147
+ });
148
+ return Promise.reject(errMsg);
121
149
  });
122
150
  });
123
151
  }
@@ -131,7 +159,7 @@ class AppProxy {
131
159
  if (csrf_1.COOKIE_CSRF === key) {
132
160
  return false;
133
161
  }
134
- if (('connect.sid' === key || 'sid' === key) && !this.isMainApp) {
162
+ if (initLogin_1.LOGIN_COOKIE_KEYS.includes(key) && !this.isMainApp) {
135
163
  return false;
136
164
  }
137
165
  return true;
@@ -156,14 +184,14 @@ class AppProxy {
156
184
  return proccessedHeader;
157
185
  }
158
186
  processRes(responseBuffer, proxyRes, req, res) {
159
- var _a;
187
+ var _a, _b;
160
188
  return __awaiter(this, void 0, void 0, function* () {
161
189
  let response;
162
190
  try {
163
191
  response = JSON.parse(responseBuffer.toString('utf8'));
164
192
  }
165
193
  catch (err) {
166
- this.log('----json parse responseBuffer string error-----', err);
194
+ this.log('----json parse responseBuffer string error-----', ((_a = err.response) === null || _a === void 0 ? void 0 : _a.data) || err.message);
167
195
  return responseBuffer;
168
196
  }
169
197
  if (!this.isMainApp) {
@@ -171,7 +199,7 @@ class AppProxy {
171
199
  const apis = mf.apis || [];
172
200
  if (!(req === null || req === void 0 ? void 0 : req.headers['micro-app-id'].includes('main-app'))) {
173
201
  const method = (req === null || req === void 0 ? void 0 : req.method) || 'GET';
174
- 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)/, '')) || '';
202
+ const urlPath = ((_b = req === null || req === void 0 ? void 0 : req.path) === null || _b === void 0 ? void 0 : _b.replace(/^\s*\/(custom-api|open-api)/, '')) || '';
175
203
  let isExists = false;
176
204
  apis.forEach((api) => {
177
205
  const isLegal = new RegExp(`^${api.path.replace(apiPathMatchReg, '/[^/]+')}/?$`).test(urlPath);
@@ -1,2 +1,2 @@
1
- export declare const LOGIN_COOKIE_KEY = "connect.sid";
1
+ export declare const LOGIN_COOKIE_KEYS: string[];
2
2
  export default function getLoginCookie(url: any, loginData: any, options: any): Promise<string>;
package/dist/initLogin.js CHANGED
@@ -3,10 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.LOGIN_COOKIE_KEY = void 0;
6
+ exports.LOGIN_COOKIE_KEYS = void 0;
7
7
  const globalAxios_1 = __importDefault(require("./globalAxios"));
8
8
  const util_1 = require("./util");
9
- exports.LOGIN_COOKIE_KEY = "connect.sid";
9
+ exports.LOGIN_COOKIE_KEYS = [
10
+ 'sid',
11
+ 'connect.sid'
12
+ ];
10
13
  function getLoginCookie(url, loginData, options) {
11
14
  return globalAxios_1.default
12
15
  .post(url, loginData, options)
@@ -14,7 +17,7 @@ function getLoginCookie(url, loginData, options) {
14
17
  if (!res.data.success) {
15
18
  return Promise.reject(res);
16
19
  }
17
- return (0, util_1.parseSetCookie)(res.headers["set-cookie"], exports.LOGIN_COOKIE_KEY);
20
+ return (0, util_1.parseSetCookie)(res.headers["set-cookie"]);
18
21
  });
19
22
  }
20
23
  exports.default = getLoginCookie;
package/dist/util.d.ts CHANGED
@@ -4,4 +4,4 @@ export declare function getLoginData(username: string, password: string): {
4
4
  password: string;
5
5
  };
6
6
  export declare function logFactory(isLog: boolean): (message?: any, ...optionalParams: any[]) => void;
7
- export declare function parseSetCookie(setCookie: string[], key: string): string;
7
+ export declare function parseSetCookie(setCookie: string[]): string;
package/dist/util.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseSetCookie = exports.logFactory = exports.getLoginData = exports.exit = void 0;
4
4
  const js_sha256_1 = require("js-sha256");
5
+ const initLogin_1 = require("./initLogin");
5
6
  function exit(info) {
6
7
  console.log(info);
7
8
  process.exit(1);
@@ -18,10 +19,10 @@ function logFactory(isLog) {
18
19
  return isLog ? console.log : (...info) => { };
19
20
  }
20
21
  exports.logFactory = logFactory;
21
- function parseSetCookie(setCookie, key) {
22
+ function parseSetCookie(setCookie) {
22
23
  const targetCookie = setCookie
23
24
  .map((cookieInfo) => cookieInfo.split(';')[0])
24
- .find((cookie) => cookie.split('=')[0] === 'connect.sid' || cookie.split('=')[0] === 'sid');
25
+ .find((cookie) => initLogin_1.LOGIN_COOKIE_KEYS.includes(cookie.split('=')[0]));
25
26
  return targetCookie || '';
26
27
  }
27
28
  exports.parseSetCookie = parseSetCookie;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tuya-sat/micro-dev-proxy",
3
- "version": "3.0.29",
3
+ "version": "3.1.0",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -15,6 +15,7 @@
15
15
  "fs-extra": "10.0.1",
16
16
  "http-proxy-middleware": "2.0.4",
17
17
  "js-sha256": "0.9.0",
18
+ "spinnies": "^0.5.1",
18
19
  "undici": "^5.20.0"
19
20
  },
20
21
  "devDependencies": {