oak-backend-base 4.1.9 → 4.1.10

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.
@@ -37,6 +37,27 @@ function verify(publicKey, body, ts, nonce, signature) {
37
37
  verify2.end();
38
38
  return verify2.verify(publicKey, signature, 'hex');
39
39
  }
40
+ async function fetchWithTimeout(url, options, timeout = 5000) {
41
+ const controller = new AbortController();
42
+ const signal = controller.signal;
43
+ // 设置超时
44
+ const timeoutId = setTimeout(() => {
45
+ controller.abort();
46
+ }, timeout);
47
+ // 发起 fetch 请求并传递 signal
48
+ return fetch(url, Object.assign({}, options, { signal }))
49
+ .then(response => {
50
+ clearTimeout(timeoutId); // 如果请求成功,清除超时
51
+ return response;
52
+ })
53
+ .catch(error => {
54
+ clearTimeout(timeoutId); // 如果请求失败,清除超时
55
+ if (error.name === 'AbortError') {
56
+ throw new types_1.OakRequestTimeoutException();
57
+ }
58
+ throw error; // 其他错误
59
+ });
60
+ }
40
61
  class Synchronizer {
41
62
  config;
42
63
  schema;
@@ -45,7 +66,7 @@ class Synchronizer {
45
66
  contextBuilder;
46
67
  pushAccessMap = {};
47
68
  async startChannel2(context, channel) {
48
- const { queue, api, selfEncryptInfo, entity, entityId, onFailed } = channel;
69
+ const { queue, api, selfEncryptInfo, entity, entityId, onFailed, timeout } = channel;
49
70
  // todo 加密
50
71
  const opers = queue.map(ele => ele.oper);
51
72
  if (process.env.NODE_ENV === 'development') {
@@ -59,7 +80,7 @@ class Synchronizer {
59
80
  try {
60
81
  const body = JSON.stringify(opers);
61
82
  const { ts, nonce, signature } = await sign(selfEncryptInfo.privateKey, body);
62
- const res = await fetch(finalApi, {
83
+ const res = await fetchWithTimeout(finalApi, {
63
84
  method: 'post',
64
85
  headers: {
65
86
  'Content-Type': 'application/json',
@@ -70,7 +91,7 @@ class Synchronizer {
70
91
  [OAK_SYNC_HEADER_SIGN]: signature,
71
92
  },
72
93
  body,
73
- });
94
+ }, timeout || 5000);
74
95
  if (res.status !== 200) {
75
96
  throw new Error(`sync数据时,访问api「${finalApi}」的结果不是200。「${res.status}」`);
76
97
  }
@@ -164,7 +185,7 @@ class Synchronizer {
164
185
  }
165
186
  }));
166
187
  }
167
- pushOperToChannel(oper, userId, url, endpoint, remoteEntity, remoteEntityId, selfEncryptInfo, onSynchronized, onFailed) {
188
+ pushOperToChannel(oper, userId, url, endpoint, remoteEntity, remoteEntityId, selfEncryptInfo, onSynchronized, onFailed, timeout) {
168
189
  if (!this.channelDict[userId]) {
169
190
  // channel上缓存这些信息,暂不支持动态更新
170
191
  this.channelDict[userId] = {
@@ -174,6 +195,7 @@ class Synchronizer {
174
195
  entityId: remoteEntityId,
175
196
  selfEncryptInfo,
176
197
  onFailed,
198
+ timeout,
177
199
  };
178
200
  }
179
201
  else {
@@ -220,7 +242,7 @@ class Synchronizer {
220
242
  if (pushEntityNodes && pushEntityNodes.length > 0) {
221
243
  // 每个pushEntityNode代表配置的一个remoteEntity
222
244
  await Promise.all(pushEntityNodes.map(async (node) => {
223
- const { projection, groupByUsers, getRemotePushInfo: getRemoteAccessInfo, groupBySelfEntity, endpoint, actions, onSynchronized, onFailed } = node;
245
+ const { projection, groupByUsers, getRemotePushInfo: getRemoteAccessInfo, groupBySelfEntity, endpoint, actions, onSynchronized, onFailed, timeout } = node;
224
246
  // 定义中应该不可能没有actions
225
247
  if (!actions || actions.includes(action)) {
226
248
  const rows = await context.select(targetEntity, {
@@ -254,7 +276,7 @@ class Synchronizer {
254
276
  userId,
255
277
  remoteEntityId: entityId,
256
278
  });
257
- this.pushOperToChannel(oper2, userId, url, endpoint, entity, entityId, selfEncryptInfo, onSynchronized, onFailed);
279
+ this.pushOperToChannel(oper2, userId, url, endpoint, entity, entityId, selfEncryptInfo, onSynchronized, onFailed, timeout);
258
280
  };
259
281
  for (const userId in userSendDict) {
260
282
  if (userId !== operatorId || !oper.bornAt) {
@@ -365,7 +387,7 @@ class Synchronizer {
365
387
  const { remotes, self } = config;
366
388
  // 根据remotes定义,建立从entity到需要同步的远端结点信息的Map
367
389
  remotes.forEach((remote) => {
368
- const { getPushInfo, pushEntities: pushEntityDefs, endpoint, pathToUser, relationName: rnRemote, onFailed } = remote;
390
+ const { getPushInfo, pushEntities: pushEntityDefs, endpoint, pathToUser, relationName: rnRemote, onFailed, timeout } = remote;
369
391
  if (pushEntityDefs) {
370
392
  const pushEntities = [];
371
393
  const endpoint2 = (0, path_1.join)(endpoint || 'sync', self.entity);
@@ -433,6 +455,7 @@ class Synchronizer {
433
455
  actions,
434
456
  onSynchronized,
435
457
  onFailed,
458
+ timeout,
436
459
  }];
437
460
  }
438
461
  else {
@@ -446,6 +469,7 @@ class Synchronizer {
446
469
  actions,
447
470
  onSynchronized,
448
471
  onFailed,
472
+ timeout,
449
473
  });
450
474
  }
451
475
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-backend-base",
3
- "version": "4.1.9",
3
+ "version": "4.1.10",
4
4
  "description": "oak-backend-base",
5
5
  "main": "lib/index",
6
6
  "author": {
@@ -20,10 +20,10 @@
20
20
  "mysql": "^2.18.1",
21
21
  "mysql2": "^2.3.3",
22
22
  "node-schedule": "^2.1.0",
23
- "oak-common-aspect": "^3.0.2",
23
+ "oak-common-aspect": "^3.0.4",
24
24
  "oak-db": "^3.3.2",
25
- "oak-domain": "^5.1.9",
26
- "oak-frontend-base": "^5.3.18",
25
+ "oak-domain": "^5.1.10",
26
+ "oak-frontend-base": "^5.3.19",
27
27
  "socket.io": "^4.7.2",
28
28
  "socket.io-client": "^4.7.2",
29
29
  "uuid": "^8.3.2"