@tuya-sat/micro-dev-proxy 3.0.9 → 3.0.11

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.
@@ -13,6 +13,7 @@ export interface AppProxyConfig {
13
13
  logSign?: boolean;
14
14
  isThirdLogin?: boolean;
15
15
  }
16
+ export declare function GSdf(): any;
16
17
  export default class AppProxy {
17
18
  target: string;
18
19
  username: string;
@@ -34,6 +35,7 @@ export default class AppProxy {
34
35
  init(): Promise<void>;
35
36
  getCsrf(): Promise<void>;
36
37
  getLoginCookie(): Promise<void>;
38
+ getCookie(prevCookie: any): string;
37
39
  processReq(proxyReq: Parameters<Options['onProxyReq']>[0], req: any, res: any): void;
38
40
  combinedHeader(prevHeadar: any): any;
39
41
  processRes(responseBuffer: any, proxyRes: any, req: any, res: any): Promise<any>;
package/dist/appProxy.js CHANGED
@@ -35,6 +35,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.GSdf = void 0;
38
39
  const globalAxios_1 = __importDefault(require("./globalAxios"));
39
40
  const js_sha256_1 = require("js-sha256");
40
41
  const csrf_1 = __importStar(require("./csrf"));
@@ -44,8 +45,19 @@ const util_1 = require("./util");
44
45
  const url_1 = require("url");
45
46
  const fs = __importStar(require("fs"));
46
47
  const path = __importStar(require("path"));
48
+ const undici_1 = require("undici");
47
49
  const mfPath = path.join(process.cwd(), 'manifest.json');
48
50
  const mf = fs.existsSync(mfPath) && JSON.parse(fs.readFileSync(mfPath, 'utf8'));
51
+ let SDF;
52
+ function parse_SDF(html) {
53
+ var _a;
54
+ const REGEX = /(?<=window._SDF=).*?}(?=;)/;
55
+ return ((_a = REGEX.exec(html)) === null || _a === void 0 ? void 0 : _a[0]) || '';
56
+ }
57
+ function GSdf() {
58
+ return SDF;
59
+ }
60
+ exports.GSdf = GSdf;
49
61
  class AppProxy {
50
62
  constructor({ target, username, password, additionHeaders, isMainApp, isThirdLogin, loginApi, csrf = true, logSign, }) {
51
63
  this.loginCookie = new token_1.default();
@@ -95,6 +107,14 @@ class AppProxy {
95
107
  })
96
108
  .then((cookie) => {
97
109
  this.loginCookie.setValue(cookie);
110
+ (0, undici_1.request)(new url_1.URL('/', this.target).href, {
111
+ headers: this.combinedHeader({ cookie }),
112
+ }).then(({ body }) => {
113
+ body.text().then((data) => {
114
+ SDF = JSON.parse(parse_SDF(data));
115
+ console.log(SDF);
116
+ });
117
+ });
98
118
  })
99
119
  .catch((err) => {
100
120
  this.log('----inital login error----' + err);
@@ -102,12 +122,8 @@ class AppProxy {
102
122
  });
103
123
  });
104
124
  }
105
- processReq(proxyReq, req, res) {
106
- //整合需要添加的header头
107
- const combineHeaders = this.combinedHeader(this.additionHeaders);
108
- Object.entries(combineHeaders).forEach(([headerKey, value]) => proxyReq.setHeader(headerKey, value));
109
- const prevCookie = proxyReq.getHeader('cookie') || '';
110
- const cookie = [
125
+ getCookie(prevCookie) {
126
+ return [
111
127
  ...prevCookie
112
128
  .split(';')
113
129
  .map((item) => [item.split('=')[0].trim(), item])
@@ -127,7 +143,13 @@ class AppProxy {
127
143
  ]
128
144
  .filter(Boolean)
129
145
  .join(';');
130
- proxyReq.setHeader('cookie', cookie);
146
+ }
147
+ processReq(proxyReq, req, res) {
148
+ //整合需要添加的header头
149
+ const combineHeaders = this.combinedHeader(this.additionHeaders);
150
+ Object.entries(combineHeaders).forEach(([headerKey, value]) => proxyReq.setHeader(headerKey, value));
151
+ const prevCookie = proxyReq.getHeader('cookie') || '';
152
+ proxyReq.setHeader('cookie', this.getCookie(prevCookie));
131
153
  this.log('----req-header----', proxyReq.getHeaders());
132
154
  }
133
155
  combinedHeader(prevHeadar) {
@@ -146,23 +168,25 @@ class AppProxy {
146
168
  this.log('----json parse responseBuffer string error-----', err);
147
169
  return responseBuffer;
148
170
  }
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: `${method}:${urlPath} 未在manifest.json中注册`,
164
- code: '888888',
171
+ if (!this.isMainApp) {
172
+ const apiPathMatchReg = /\/(:[^/]+|\$?{[^/]+})/g;
173
+ const apis = mf.apis || [];
174
+ if (!(req === null || req === void 0 ? void 0 : req.headers['micro-app-id'].includes('main-app'))) {
175
+ const method = (req === null || req === void 0 ? void 0 : req.method) || 'GET';
176
+ 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)/, '')) || '';
177
+ let isExists = false;
178
+ apis.forEach((api) => {
179
+ const isLegal = new RegExp(`^${api.path.replace(apiPathMatchReg, '/[^/]+')}/?$`).test(urlPath);
180
+ if (isLegal && method.toLowerCase() === api.method.toLowerCase()) {
181
+ isExists = true;
182
+ }
165
183
  });
184
+ if (!isExists) {
185
+ return JSON.stringify({
186
+ msg: `${method}:${urlPath} 未在manifest.json中注册`,
187
+ code: '888888',
188
+ });
189
+ }
166
190
  }
167
191
  }
168
192
  if (response.code === 1010 && !this.isMainApp) {
package/dist/csrf.js CHANGED
@@ -6,16 +6,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.HEADERS_CSRF = exports.COOKIE_CSRF = void 0;
7
7
  const globalAxios_1 = __importDefault(require("./globalAxios"));
8
8
  const util_1 = require("./util");
9
- exports.COOKIE_CSRF = "_csrf";
10
- exports.HEADERS_CSRF = "csrf-token";
9
+ exports.COOKIE_CSRF = '_csrf';
10
+ exports.HEADERS_CSRF = 'csrf-token';
11
11
  function parseHtml(html) {
12
12
  var _a;
13
13
  const CSRF_REGEX = /.*window.csrf=('|")?([^'"]+)/;
14
- return ((_a = html.match(CSRF_REGEX)) === null || _a === void 0 ? void 0 : _a[2]) || "";
14
+ return ((_a = html.match(CSRF_REGEX)) === null || _a === void 0 ? void 0 : _a[2]) || '';
15
15
  }
16
16
  function getCsrf(url) {
17
17
  return globalAxios_1.default.get(url).then((res) => {
18
- let csrfCookie = (0, util_1.parseSetCookie)(res.headers["set-cookie"], exports.COOKIE_CSRF);
18
+ let csrfCookie = (0, util_1.parseSetCookie)(res.headers['set-cookie'], exports.COOKIE_CSRF);
19
19
  let csrfToken = parseHtml(res.data);
20
20
  return [csrfCookie, csrfToken];
21
21
  });
package/dist/index.d.ts CHANGED
@@ -4,5 +4,6 @@ export interface Config extends AppProxyConfig {
4
4
  [key: string]: any;
5
5
  };
6
6
  }
7
+ export { GSdf } from './appProxy';
7
8
  export declare function awaiting(): any[];
8
9
  export default function createProxy({ additionHeaders, ...restPrevConfig }: Config): (app: any) => void;
package/dist/index.js CHANGED
@@ -14,10 +14,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.awaiting = void 0;
17
+ exports.awaiting = exports.GSdf = void 0;
18
18
  const os_1 = __importDefault(require("os"));
19
19
  const http_proxy_middleware_1 = require("http-proxy-middleware");
20
20
  const appProxy_1 = __importDefault(require("./appProxy"));
21
+ var appProxy_2 = require("./appProxy");
22
+ Object.defineProperty(exports, "GSdf", { enumerable: true, get: function () { return appProxy_2.GSdf; } });
21
23
  function awaiting() {
22
24
  let $resolve, $reject;
23
25
  const waiting = new Promise((resolve, reject) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tuya-sat/micro-dev-proxy",
3
- "version": "3.0.9",
3
+ "version": "3.0.11",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -14,7 +14,8 @@
14
14
  "chalk": "5.0.1",
15
15
  "fs-extra": "10.0.1",
16
16
  "http-proxy-middleware": "2.0.4",
17
- "js-sha256": "0.9.0"
17
+ "js-sha256": "0.9.0",
18
+ "undici": "^5.20.0"
18
19
  },
19
20
  "devDependencies": {
20
21
  "@types/fs-extra": "9.0.13",