@onekeyfe/cross-inpage-provider-core 2.2.65 → 2.2.67

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,14 +3,16 @@
3
3
  // @ts-ignore
4
4
  import EventEmitterBase from '@onekeyfe/cross-inpage-provider-events';
5
5
  function safeApply(handler, context, args) {
6
+ var _a;
6
7
  try {
7
8
  Reflect.apply(handler, context, args);
8
9
  }
9
10
  catch (err) {
10
11
  // Throw error after timeout so as not to interrupt the stack
11
- setTimeout(() => {
12
+ const timeout = setTimeout(() => {
12
13
  throw err;
13
14
  });
15
+ (_a = timeout === null || timeout === void 0 ? void 0 : timeout.unref) === null || _a === void 0 ? void 0 : _a.call(timeout);
14
16
  }
15
17
  }
16
18
  function arrayClone(arr) {
@@ -2,13 +2,20 @@ import { CrossEventEmitter } from './CrossEventEmitter';
2
2
  import { IDebugLogger, IInjectedProviderNamesStrings, IJsBridgeConfig, IJsBridgeMessagePayload, IJsonRpcResponse } from '@onekeyfe/cross-inpage-provider-types';
3
3
  declare function isLegacyExtMessage(payload: unknown): boolean;
4
4
  declare abstract class JsBridgeBase extends CrossEventEmitter {
5
- constructor(config?: IJsBridgeConfig);
5
+ private callbacks;
6
+ private callbacksCount;
7
+ private callbacksExpireTimer?;
8
+ private callbackId;
6
9
  private _requestPayloadCache;
7
10
  protected isExtUi: boolean;
8
11
  protected isInjected: boolean;
9
12
  protected sendAsString: boolean;
10
13
  globalOnMessageEnabled: boolean;
11
14
  providersHub: Record<string, any[]>;
15
+ private config;
16
+ protected callbacksExpireTimeout: number;
17
+ debugLogger: IDebugLogger;
18
+ constructor(config?: IJsBridgeConfig);
12
19
  attachProviderInstance(provider: {
13
20
  providerName: string;
14
21
  }): void;
@@ -18,11 +25,6 @@ declare abstract class JsBridgeBase extends CrossEventEmitter {
18
25
  origin?: string;
19
26
  remoteId?: string | number | null;
20
27
  };
21
- private config;
22
- protected callbacksExpireTimeout: number;
23
- debugLogger: IDebugLogger;
24
- private callbacks;
25
- private callbackId;
26
28
  private createCallbackId;
27
29
  private createPayload;
28
30
  private send;
@@ -34,7 +36,9 @@ declare abstract class JsBridgeBase extends CrossEventEmitter {
34
36
  data?: unknown;
35
37
  error?: unknown;
36
38
  }): void;
37
- rejectExpiredCallbacks(): void;
39
+ private rejectExpiredCallbacks;
40
+ private stopCallbackExpireTimerLoop;
41
+ private startCallbackExpireTimerLoop;
38
42
  clearCallbackCache(id: number | string): void;
39
43
  receive(payloadReceived?: string | IJsBridgeMessagePayload, sender?: {
40
44
  origin?: string;
@@ -47,6 +47,9 @@ class JsBridgeBase extends CrossEventEmitter {
47
47
  constructor(config = {}) {
48
48
  var _a, _b;
49
49
  super();
50
+ this.callbacks = [];
51
+ this.callbacksCount = 0;
52
+ this.callbackId = 1;
50
53
  this._requestPayloadCache = {};
51
54
  this.isExtUi = false;
52
55
  this.isInjected = false;
@@ -55,6 +58,10 @@ class JsBridgeBase extends CrossEventEmitter {
55
58
  this.providersHub = {
56
59
  // name: []
57
60
  };
61
+ // TODO increase timeout as hardware sign transaction may take a long time
62
+ // can set timeout for each callback
63
+ this.callbacksExpireTimeout = 10 * 60 * 1000;
64
+ this.debugLogger = appDebugLogger;
58
65
  // Only handle type=REQUEST messages, type=RESPONSE message will be ignored
59
66
  this.globalOnMessage = (message) => __awaiter(this, void 0, void 0, function* () {
60
67
  try {
@@ -92,12 +99,6 @@ class JsBridgeBase extends CrossEventEmitter {
92
99
  origin: '',
93
100
  remoteId: '',
94
101
  };
95
- // TODO increase timeout as hardware sign transaction may take a long time
96
- // can set timeout for each callback
97
- this.callbacksExpireTimeout = 10 * 60 * 1000;
98
- this.debugLogger = appDebugLogger;
99
- this.callbacks = [];
100
- this.callbackId = 1;
101
102
  this.config = config;
102
103
  this.callbacksExpireTimeout = (_a = config.timeout) !== null && _a !== void 0 ? _a : this.callbacksExpireTimeout;
103
104
  this.debugLogger = config.debugLogger || appDebugLogger;
@@ -111,7 +112,6 @@ class JsBridgeBase extends CrossEventEmitter {
111
112
  code: error === null || error === void 0 ? void 0 : error.code,
112
113
  });
113
114
  });
114
- this.rejectExpiredCallbacks();
115
115
  }
116
116
  attachProviderInstance(provider) {
117
117
  var _a;
@@ -133,6 +133,8 @@ class JsBridgeBase extends CrossEventEmitter {
133
133
  throw new Error(`JsBridge ERROR: callback exists, id=${id}`);
134
134
  }
135
135
  this.callbacks[id] = { id, resolve, reject, created: Date.now() };
136
+ this.callbacksCount += 1;
137
+ this.startCallbackExpireTimerLoop();
136
138
  }
137
139
  // convert to plain error object which can be stringify
138
140
  if (payload.error) {
@@ -232,7 +234,7 @@ class JsBridgeBase extends CrossEventEmitter {
232
234
  }
233
235
  }
234
236
  rejectExpiredCallbacks() {
235
- if (!this.callbacksExpireTimeout) {
237
+ if (!this.callbacksExpireTimeout || this.callbacksCount === 0) {
236
238
  return;
237
239
  }
238
240
  const now = Date.now();
@@ -246,12 +248,35 @@ class JsBridgeBase extends CrossEventEmitter {
246
248
  }
247
249
  }
248
250
  }
249
- setTimeout(() => {
251
+ }
252
+ stopCallbackExpireTimerLoop() {
253
+ if (this.callbacksExpireTimer) {
254
+ clearTimeout(this.callbacksExpireTimer);
255
+ this.callbacksExpireTimer = undefined;
256
+ }
257
+ }
258
+ startCallbackExpireTimerLoop() {
259
+ var _a, _b;
260
+ if (!this.callbacksExpireTimeout || this.callbacksCount === 0 || this.callbacksExpireTimer) {
261
+ return;
262
+ }
263
+ this.callbacksExpireTimer = setTimeout(() => {
264
+ this.callbacksExpireTimer = undefined;
250
265
  this.rejectExpiredCallbacks();
266
+ this.startCallbackExpireTimerLoop();
251
267
  }, this.callbacksExpireTimeout);
268
+ (_b = (_a = this.callbacksExpireTimer) === null || _a === void 0 ? void 0 : _a.unref) === null || _b === void 0 ? void 0 : _b.call(_a);
252
269
  }
253
270
  clearCallbackCache(id) {
271
+ const callbackInfo = this.callbacks[id];
272
+ if (callbackInfo) {
273
+ this.callbacksCount -= 1;
274
+ }
254
275
  delete this.callbacks[id];
276
+ if (this.callbacksCount <= 0) {
277
+ this.callbacksCount = 0;
278
+ this.stopCallbackExpireTimerLoop();
279
+ }
255
280
  }
256
281
  receive(payloadReceived = '', sender) {
257
282
  // orignalConsoleLog('JsBridgeBase.receive', payloadReceived, sender);
@@ -20,7 +20,7 @@ const METHODS = {
20
20
  };
21
21
  class ProviderBase extends CrossEventEmitter {
22
22
  constructor(config) {
23
- var _a, _b, _c;
23
+ var _a, _b, _c, _d;
24
24
  super();
25
25
  this.version = versionInfo.version;
26
26
  this.isOneKey = true;
@@ -35,10 +35,11 @@ class ProviderBase extends CrossEventEmitter {
35
35
  // TODO init this.debugLogger first, and enable debug config after extension connect
36
36
  this.debugLogger = ((_a = this.bridge) === null || _a === void 0 ? void 0 : _a.debugLogger) || fakeDebugLogger;
37
37
  (_c = (_b = this.bridge) === null || _b === void 0 ? void 0 : _b.debugLogger) === null || _c === void 0 ? void 0 : _c._attachExternalLogger(this.logger);
38
- setTimeout(() => {
38
+ const timeout = setTimeout(() => {
39
39
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
40
40
  this.bridge.attachProviderInstance(this);
41
41
  }, 0);
42
+ (_d = timeout === null || timeout === void 0 ? void 0 : timeout.unref) === null || _d === void 0 ? void 0 : _d.call(timeout);
42
43
  // call sendSiteMetadataDomReady/getConnectWalletInfo in ProviderPrivate, dont need here
43
44
  // void this.sendSiteMetadataDomReady();
44
45
  // void this.getConnectWalletInfo();
@@ -68,10 +69,12 @@ class ProviderBase extends CrossEventEmitter {
68
69
  return __awaiter(this, arguments, void 0, function* ({ timeout = 3000 } = {}) {
69
70
  // eslint-disable-next-line no-async-promise-executor,@typescript-eslint/no-misused-promises
70
71
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
72
+ var _a;
71
73
  const timer = setTimeout(() => {
72
74
  console.error(`getConnectWalletInfo timeout: ${timeout}`);
73
75
  resolve(null);
74
76
  }, timeout);
77
+ (_a = timer === null || timer === void 0 ? void 0 : timer.unref) === null || _a === void 0 ? void 0 : _a.call(timer);
75
78
  try {
76
79
  const result = (yield this.bridgeRequest({
77
80
  method: METHODS.wallet_getConnectWalletInfo,
@@ -9,14 +9,16 @@ exports.CrossEventEmitter = void 0;
9
9
  // @ts-ignore
10
10
  const cross_inpage_provider_events_1 = __importDefault(require("@onekeyfe/cross-inpage-provider-events"));
11
11
  function safeApply(handler, context, args) {
12
+ var _a;
12
13
  try {
13
14
  Reflect.apply(handler, context, args);
14
15
  }
15
16
  catch (err) {
16
17
  // Throw error after timeout so as not to interrupt the stack
17
- setTimeout(() => {
18
+ const timeout = setTimeout(() => {
18
19
  throw err;
19
20
  });
21
+ (_a = timeout === null || timeout === void 0 ? void 0 : timeout.unref) === null || _a === void 0 ? void 0 : _a.call(timeout);
20
22
  }
21
23
  }
22
24
  function arrayClone(arr) {
@@ -54,6 +54,9 @@ class JsBridgeBase extends CrossEventEmitter_1.CrossEventEmitter {
54
54
  constructor(config = {}) {
55
55
  var _a, _b;
56
56
  super();
57
+ this.callbacks = [];
58
+ this.callbacksCount = 0;
59
+ this.callbackId = 1;
57
60
  this._requestPayloadCache = {};
58
61
  this.isExtUi = false;
59
62
  this.isInjected = false;
@@ -62,6 +65,10 @@ class JsBridgeBase extends CrossEventEmitter_1.CrossEventEmitter {
62
65
  this.providersHub = {
63
66
  // name: []
64
67
  };
68
+ // TODO increase timeout as hardware sign transaction may take a long time
69
+ // can set timeout for each callback
70
+ this.callbacksExpireTimeout = 10 * 60 * 1000;
71
+ this.debugLogger = loggers_1.appDebugLogger;
65
72
  // Only handle type=REQUEST messages, type=RESPONSE message will be ignored
66
73
  this.globalOnMessage = (message) => __awaiter(this, void 0, void 0, function* () {
67
74
  try {
@@ -99,12 +106,6 @@ class JsBridgeBase extends CrossEventEmitter_1.CrossEventEmitter {
99
106
  origin: '',
100
107
  remoteId: '',
101
108
  };
102
- // TODO increase timeout as hardware sign transaction may take a long time
103
- // can set timeout for each callback
104
- this.callbacksExpireTimeout = 10 * 60 * 1000;
105
- this.debugLogger = loggers_1.appDebugLogger;
106
- this.callbacks = [];
107
- this.callbackId = 1;
108
109
  this.config = config;
109
110
  this.callbacksExpireTimeout = (_a = config.timeout) !== null && _a !== void 0 ? _a : this.callbacksExpireTimeout;
110
111
  this.debugLogger = config.debugLogger || loggers_1.appDebugLogger;
@@ -118,7 +119,6 @@ class JsBridgeBase extends CrossEventEmitter_1.CrossEventEmitter {
118
119
  code: error === null || error === void 0 ? void 0 : error.code,
119
120
  });
120
121
  });
121
- this.rejectExpiredCallbacks();
122
122
  }
123
123
  attachProviderInstance(provider) {
124
124
  var _a;
@@ -140,6 +140,8 @@ class JsBridgeBase extends CrossEventEmitter_1.CrossEventEmitter {
140
140
  throw new Error(`JsBridge ERROR: callback exists, id=${id}`);
141
141
  }
142
142
  this.callbacks[id] = { id, resolve, reject, created: Date.now() };
143
+ this.callbacksCount += 1;
144
+ this.startCallbackExpireTimerLoop();
143
145
  }
144
146
  // convert to plain error object which can be stringify
145
147
  if (payload.error) {
@@ -239,7 +241,7 @@ class JsBridgeBase extends CrossEventEmitter_1.CrossEventEmitter {
239
241
  }
240
242
  }
241
243
  rejectExpiredCallbacks() {
242
- if (!this.callbacksExpireTimeout) {
244
+ if (!this.callbacksExpireTimeout || this.callbacksCount === 0) {
243
245
  return;
244
246
  }
245
247
  const now = Date.now();
@@ -253,12 +255,35 @@ class JsBridgeBase extends CrossEventEmitter_1.CrossEventEmitter {
253
255
  }
254
256
  }
255
257
  }
256
- setTimeout(() => {
258
+ }
259
+ stopCallbackExpireTimerLoop() {
260
+ if (this.callbacksExpireTimer) {
261
+ clearTimeout(this.callbacksExpireTimer);
262
+ this.callbacksExpireTimer = undefined;
263
+ }
264
+ }
265
+ startCallbackExpireTimerLoop() {
266
+ var _a, _b;
267
+ if (!this.callbacksExpireTimeout || this.callbacksCount === 0 || this.callbacksExpireTimer) {
268
+ return;
269
+ }
270
+ this.callbacksExpireTimer = setTimeout(() => {
271
+ this.callbacksExpireTimer = undefined;
257
272
  this.rejectExpiredCallbacks();
273
+ this.startCallbackExpireTimerLoop();
258
274
  }, this.callbacksExpireTimeout);
275
+ (_b = (_a = this.callbacksExpireTimer) === null || _a === void 0 ? void 0 : _a.unref) === null || _b === void 0 ? void 0 : _b.call(_a);
259
276
  }
260
277
  clearCallbackCache(id) {
278
+ const callbackInfo = this.callbacks[id];
279
+ if (callbackInfo) {
280
+ this.callbacksCount -= 1;
281
+ }
261
282
  delete this.callbacks[id];
283
+ if (this.callbacksCount <= 0) {
284
+ this.callbacksCount = 0;
285
+ this.stopCallbackExpireTimerLoop();
286
+ }
262
287
  }
263
288
  receive(payloadReceived = '', sender) {
264
289
  // orignalConsoleLog('JsBridgeBase.receive', payloadReceived, sender);
@@ -26,7 +26,7 @@ const METHODS = {
26
26
  };
27
27
  class ProviderBase extends CrossEventEmitter_1.CrossEventEmitter {
28
28
  constructor(config) {
29
- var _a, _b, _c;
29
+ var _a, _b, _c, _d;
30
30
  super();
31
31
  this.version = versionInfo_1.default.version;
32
32
  this.isOneKey = true;
@@ -41,10 +41,11 @@ class ProviderBase extends CrossEventEmitter_1.CrossEventEmitter {
41
41
  // TODO init this.debugLogger first, and enable debug config after extension connect
42
42
  this.debugLogger = ((_a = this.bridge) === null || _a === void 0 ? void 0 : _a.debugLogger) || loggers_1.fakeDebugLogger;
43
43
  (_c = (_b = this.bridge) === null || _b === void 0 ? void 0 : _b.debugLogger) === null || _c === void 0 ? void 0 : _c._attachExternalLogger(this.logger);
44
- setTimeout(() => {
44
+ const timeout = setTimeout(() => {
45
45
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
46
46
  this.bridge.attachProviderInstance(this);
47
47
  }, 0);
48
+ (_d = timeout === null || timeout === void 0 ? void 0 : timeout.unref) === null || _d === void 0 ? void 0 : _d.call(timeout);
48
49
  // call sendSiteMetadataDomReady/getConnectWalletInfo in ProviderPrivate, dont need here
49
50
  // void this.sendSiteMetadataDomReady();
50
51
  // void this.getConnectWalletInfo();
@@ -74,10 +75,12 @@ class ProviderBase extends CrossEventEmitter_1.CrossEventEmitter {
74
75
  return __awaiter(this, arguments, void 0, function* ({ timeout = 3000 } = {}) {
75
76
  // eslint-disable-next-line no-async-promise-executor,@typescript-eslint/no-misused-promises
76
77
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
78
+ var _a;
77
79
  const timer = setTimeout(() => {
78
80
  console.error(`getConnectWalletInfo timeout: ${timeout}`);
79
81
  resolve(null);
80
82
  }, timeout);
83
+ (_a = timer === null || timer === void 0 ? void 0 : timer.unref) === null || _a === void 0 ? void 0 : _a.call(timer);
81
84
  try {
82
85
  const result = (yield this.bridgeRequest({
83
86
  method: METHODS.wallet_getConnectWalletInfo,
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const version = '2.2.65';
3
+ const version = '2.2.67';
4
4
  const versionBuild = '2020-0101-1';
5
5
  exports.default = {
6
6
  version,
@@ -1,4 +1,4 @@
1
- const version = '2.2.65';
1
+ const version = '2.2.67';
2
2
  const versionBuild = '2020-0101-1';
3
3
  export default {
4
4
  version,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/cross-inpage-provider-core",
3
- "version": "2.2.65",
3
+ "version": "2.2.67",
4
4
  "keywords": [
5
5
  "cross-inpage-provider"
6
6
  ],
@@ -29,9 +29,9 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@noble/hashes": "^1.7.1",
32
- "@onekeyfe/cross-inpage-provider-errors": "2.2.65",
33
- "@onekeyfe/cross-inpage-provider-events": "2.2.65",
34
- "@onekeyfe/cross-inpage-provider-types": "2.2.65",
32
+ "@onekeyfe/cross-inpage-provider-errors": "2.2.67",
33
+ "@onekeyfe/cross-inpage-provider-events": "2.2.67",
34
+ "@onekeyfe/cross-inpage-provider-types": "2.2.67",
35
35
  "events": "^3.3.0",
36
36
  "lodash-es": "^4.17.21",
37
37
  "ms": "^2.1.3"
@@ -39,5 +39,5 @@
39
39
  "devDependencies": {
40
40
  "@types/lodash-es": "^4.17.12"
41
41
  },
42
- "gitHead": "d9f3f452a446809c8cf3254c28fe92effc16bcaa"
42
+ "gitHead": "6ba68c676a73919aff91b4270cbed4800a498dbe"
43
43
  }