midway-fatcms 0.0.1-beta.40 → 0.0.1-beta.41

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.
@@ -3,6 +3,7 @@ declare class Transaction {
3
3
  private transactionMySQL;
4
4
  private transactionPostgres;
5
5
  private transactionSqlServer;
6
+ private isReleased;
6
7
  private getTxObj;
7
8
  /**
8
9
  * 获取链接对象
@@ -10,6 +10,7 @@ class Transaction {
10
10
  this.transactionMySQL = new TransactionMySQL_1.TransactionMySQL();
11
11
  this.transactionPostgres = new TransactionPostgres_1.TransactionPostgres();
12
12
  this.transactionSqlServer = new TransactionSqlServer_1.TransactionSqlServer();
13
+ this.isReleased = false;
13
14
  }
14
15
  getTxObj(pool) {
15
16
  if (pool.dbType === keys_1.SqlDbType.postgres) {
@@ -28,6 +29,11 @@ class Transaction {
28
29
  * @param pool
29
30
  */
30
31
  async getTxConnection(pool) {
32
+ if (this.isReleased) {
33
+ const msg = '[Transaction] getTxConnection error, the txObject is isReleased, please check your code . DB request must be await';
34
+ console.error(msg);
35
+ throw new Error(msg);
36
+ }
31
37
  const txObj = this.getTxObj(pool);
32
38
  return txObj.getTxConnection(pool);
33
39
  }
@@ -62,6 +68,7 @@ class Transaction {
62
68
  await this.transactionMySQL.releaseTx();
63
69
  await this.transactionPostgres.releaseTx();
64
70
  await this.transactionSqlServer.releaseTx();
71
+ this.isReleased = true;
65
72
  }
66
73
  }
67
74
  exports.Transaction = Transaction;
@@ -0,0 +1,9 @@
1
+ declare function toBase64(obj: any): string;
2
+ /**
3
+ * 将经 toBase64 函数序列化的 Base64 字符串还原为 JS 对象
4
+ * @param base64Str - 经 toBase64 处理后的 Base64 字符串
5
+ * @returns 还原后的 JS 对象
6
+ * @throws 当 Base64 格式无效或 JSON 解析失败时抛出含具体原因的错误
7
+ */
8
+ declare function fromBase64(base64Str: string): any;
9
+ export { toBase64, fromBase64 };
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fromBase64 = exports.toBase64 = void 0;
4
+ function toBase64(obj) {
5
+ const jsonString = JSON.stringify(obj);
6
+ return Buffer.from(jsonString, 'utf8').toString('base64');
7
+ }
8
+ exports.toBase64 = toBase64;
9
+ /**
10
+ * 将经 toBase64 函数序列化的 Base64 字符串还原为 JS 对象
11
+ * @param base64Str - 经 toBase64 处理后的 Base64 字符串
12
+ * @returns 还原后的 JS 对象
13
+ * @throws 当 Base64 格式无效或 JSON 解析失败时抛出含具体原因的错误
14
+ */
15
+ function fromBase64(base64Str) {
16
+ // 校验输入是否为字符串
17
+ if (typeof base64Str !== 'string') {
18
+ throw new Error('input must Base64 string');
19
+ }
20
+ try {
21
+ // 1. 反向步骤1:将 Base64 字符串解码为 UTF-8 格式的 JSON 字符串
22
+ // 对应原函数的 Buffer.from(jsonString, 'utf8') 反向操作
23
+ const jsonString = Buffer.from(base64Str, 'base64').toString('utf8');
24
+ // 2. 反向步骤2:将 JSON 字符串解析为 JS 对象
25
+ // 对应原函数的 JSON.stringify(obj) 反向操作
26
+ const restoredObj = JSON.parse(jsonString);
27
+ return restoredObj;
28
+ }
29
+ catch (error) {
30
+ // 分类处理不同错误类型,提供明确提示
31
+ if (error instanceof TypeError && error.message.includes('invalid base64')) {
32
+ throw new Error('还原失败:输入不是有效的 Base64 字符串');
33
+ }
34
+ else if (error instanceof SyntaxError && error.message.includes('JSON')) {
35
+ throw new Error('还原失败:解码后的内容不是有效的 JSON 格式');
36
+ }
37
+ else {
38
+ throw new Error(`还原失败:未知错误 - ${error.message}`);
39
+ }
40
+ }
41
+ }
42
+ exports.fromBase64 = fromBase64;
@@ -136,7 +136,7 @@ let WorkbenchService = class WorkbenchService extends BaseService_1.BaseService
136
136
  }
137
137
  // 只有一个workbench的场景。对于后台系统。不需要配置域名的场景。
138
138
  const fatcmsTargetWorkbenchCode = this.fatcmsTargetWorkbenchCode;
139
- if (typeof fatcmsTargetWorkbenchCode === "string" && fatcmsTargetWorkbenchCode.length > 3) {
139
+ if (typeof fatcmsTargetWorkbenchCode === 'string' && fatcmsTargetWorkbenchCode.length > 3) {
140
140
  return await this.getWorkbenchInfoByCode(fatcmsTargetWorkbenchCode);
141
141
  }
142
142
  //正式环境: 根据域名获取站点配置
@@ -146,7 +146,7 @@ let WorkbenchService = class WorkbenchService extends BaseService_1.BaseService
146
146
  return workbenchInfo;
147
147
  }
148
148
  // 最后兜底
149
- return await this.getWorkbenchInfoByHost("*");
149
+ return await this.getWorkbenchInfoByHost('*');
150
150
  }
151
151
  /**
152
152
  * 获取当前请求的域名
@@ -155,7 +155,7 @@ let AsyncTaskRunnerService = class AsyncTaskRunnerService extends BaseService_1.
155
155
  return Promise.resolve();
156
156
  }
157
157
  const timeoutTime = moment().subtract(24, 'hours').format("YYYY-MM-DD HH:mm:ss.SSS");
158
- this.curdProService.executeCrudByCfg({
158
+ await this.curdProService.executeCrudByCfg({
159
159
  condition: {
160
160
  task_status: AsyncTaskModel_1.SysAsyncTaskStatus.RUNNING,
161
161
  updated_at: {
@@ -247,7 +247,7 @@ let AsyncTaskRunnerService = class AsyncTaskRunnerService extends BaseService_1.
247
247
  sqlDbType: SystemDbType,
248
248
  });
249
249
  // 开始执行。
250
- exports.ASYNC_TASK_RUNNER.executeTaskList(taskList).then(() => {
250
+ await exports.ASYNC_TASK_RUNNER.executeTaskList(taskList).then(() => {
251
251
  console.log('[AsyncTaskRunnerService] ASYNC_TASK_RUNNER finished taskIds ==> ' + JSON.stringify(taskIds));
252
252
  });
253
253
  }
@@ -57,4 +57,5 @@ export declare class ProxyApiService extends ApiBaseService {
57
57
  */
58
58
  private selectTargetByBalanceStrategy;
59
59
  private setRequestHeaders;
60
+ private buildUserContextHeader;
60
61
  }
@@ -22,10 +22,7 @@ const fatcms_request_1 = require("../../libs/utils/fatcms-request");
22
22
  const ProxyApiUtils_1 = require("./ProxyApiUtils");
23
23
  const ProxyApiLoadService_1 = require("./ProxyApiLoadService");
24
24
  const ApiBaseService_1 = require("../base/ApiBaseService");
25
- function toBase64(obj) {
26
- const jsonString = JSON.stringify(obj);
27
- return Buffer.from(jsonString, 'utf8').toString('base64');
28
- }
25
+ const base64_1 = require("../../libs/utils/base64");
29
26
  /**
30
27
  * 接口代理
31
28
  */
@@ -231,20 +228,40 @@ let ProxyApiService = class ProxyApiService extends ApiBaseService_1.ApiBaseServ
231
228
  }
232
229
  setRequestHeaders(headers, proxyApiEntity) {
233
230
  const ctx = this.ctx;
231
+ // 转发IP
234
232
  if (proxyApiEntity.pass_real_ip) {
235
233
  headers['x-real-ip'] = ctx.headers['x-real-ip'];
236
234
  headers['x-forwarded-for'] = ctx.headers['x-forwarded-for'];
237
235
  }
236
+ // 转发用户上下文
237
+ const userContextHeader = this.buildUserContextHeader(proxyApiEntity);
238
+ if (userContextHeader) {
239
+ headers['x-fatcms-usercontext'] = userContextHeader;
240
+ }
241
+ else {
242
+ delete headers['x-fatcms-usercontext']; // 只有自己构建的才能被转发。前面传过来的,不要转发。
243
+ }
244
+ if (proxyApiEntity.change_host && proxyApiEntity.change_host.length > 0) {
245
+ headers.host = proxyApiEntity.change_host;
246
+ }
247
+ if (proxyApiEntity.change_origin && proxyApiEntity.change_origin.length > 0) {
248
+ headers.origin = proxyApiEntity.change_origin;
249
+ }
250
+ }
251
+ buildUserContextHeader(proxyApiEntity) {
252
+ var _a, _b;
238
253
  // 传递整个用户上下文
239
254
  if (proxyApiEntity.user_context === SystemEntities_1.ProxyUserContextEnum.SESSION_INFO) {
240
255
  const isLogin = this.ctx.userSession.isLogin();
241
256
  const sessionInfo = this.ctx.userSession.getSessionInfo();
242
- headers['x-fatcms-usercontext'] = toBase64({ ...sessionInfo, isLogin });
257
+ const workbenchCode = (_a = this.ctx.workbenchInfo) === null || _a === void 0 ? void 0 : _a.workbench_code;
258
+ return (0, base64_1.toBase64)({ ...sessionInfo, isLogin, workbenchCode });
243
259
  }
244
260
  // 只传递用户基本信息
245
261
  if (proxyApiEntity.user_context === SystemEntities_1.ProxyUserContextEnum.BASIC_INFO) {
246
262
  const isLogin = this.ctx.userSession.isLogin();
247
263
  const sessionInfo = this.ctx.userSession.getSessionInfo();
264
+ const workbenchCode = (_b = this.ctx.workbenchInfo) === null || _b === void 0 ? void 0 : _b.workbench_code;
248
265
  const userBasicInfo = { isLogin };
249
266
  if (sessionInfo && isLogin) {
250
267
  userBasicInfo.loginName = sessionInfo.loginName;
@@ -253,14 +270,10 @@ let ProxyApiService = class ProxyApiService extends ApiBaseService_1.ApiBaseServ
253
270
  userBasicInfo.workbenchCode = sessionInfo.workbenchCode;
254
271
  userBasicInfo.accountType = sessionInfo.accountType;
255
272
  }
256
- headers['x-fatcms-usercontext'] = toBase64(userBasicInfo);
257
- }
258
- if (proxyApiEntity.change_host && proxyApiEntity.change_host.length > 0) {
259
- headers.host = proxyApiEntity.change_host;
260
- }
261
- if (proxyApiEntity.change_origin && proxyApiEntity.change_origin.length > 0) {
262
- headers.origin = proxyApiEntity.change_origin;
273
+ userBasicInfo.workbenchCode = workbenchCode;
274
+ return (0, base64_1.toBase64)(userBasicInfo);
263
275
  }
276
+ return null;
264
277
  }
265
278
  };
266
279
  __decorate([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "midway-fatcms",
3
- "version": "0.0.1-beta.40",
3
+ "version": "0.0.1-beta.41",
4
4
  "description": "This is a midway component sample",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -12,6 +12,9 @@
12
12
  "lint:fix": "mwts fix"
13
13
  },
14
14
  "keywords": [],
15
+ "engines": {
16
+ "node": ">10"
17
+ },
15
18
  "author": "",
16
19
  "files": [
17
20
  "dist/**/*.txt",