@shadow-corp/nearconnect 1.0.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.
- package/README.md +546 -0
- package/build/InjectedWallet.d.ts +22 -0
- package/build/InjectedWallet.js +58 -0
- package/build/InjectedWallet.js.map +1 -0
- package/build/NearConnector.d.ts +151 -0
- package/build/NearConnector.js +536 -0
- package/build/NearConnector.js.map +1 -0
- package/build/ParentFrameWallet.d.ts +22 -0
- package/build/ParentFrameWallet.js +66 -0
- package/build/ParentFrameWallet.js.map +1 -0
- package/build/SandboxedWallet/code.d.ts +7 -0
- package/build/SandboxedWallet/code.js +324 -0
- package/build/SandboxedWallet/code.js.map +1 -0
- package/build/SandboxedWallet/executor.d.ts +23 -0
- package/build/SandboxedWallet/executor.js +338 -0
- package/build/SandboxedWallet/executor.js.map +1 -0
- package/build/SandboxedWallet/iframe.d.ts +18 -0
- package/build/SandboxedWallet/iframe.js +78 -0
- package/build/SandboxedWallet/iframe.js.map +1 -0
- package/build/SandboxedWallet/index.d.ts +24 -0
- package/build/SandboxedWallet/index.js +54 -0
- package/build/SandboxedWallet/index.js.map +1 -0
- package/build/actions/index.d.ts +3 -0
- package/build/actions/index.js +105 -0
- package/build/actions/index.js.map +1 -0
- package/build/actions/types.d.ts +76 -0
- package/build/actions/types.js +3 -0
- package/build/actions/types.js.map +1 -0
- package/build/connection/health.d.ts +213 -0
- package/build/connection/health.js +391 -0
- package/build/connection/health.js.map +1 -0
- package/build/connection/index.d.ts +4 -0
- package/build/connection/index.js +48 -0
- package/build/connection/index.js.map +1 -0
- package/build/connection/reconnect.d.ts +261 -0
- package/build/connection/reconnect.js +454 -0
- package/build/connection/reconnect.js.map +1 -0
- package/build/connection/retry.d.ts +187 -0
- package/build/connection/retry.js +427 -0
- package/build/connection/retry.js.map +1 -0
- package/build/connection/state.d.ts +222 -0
- package/build/connection/state.js +431 -0
- package/build/connection/state.js.map +1 -0
- package/build/errors.d.ts +177 -0
- package/build/errors.js +546 -0
- package/build/errors.js.map +1 -0
- package/build/hardware/errors.d.ts +36 -0
- package/build/hardware/errors.js +127 -0
- package/build/hardware/errors.js.map +1 -0
- package/build/hardware/index.d.ts +7 -0
- package/build/hardware/index.js +39 -0
- package/build/hardware/index.js.map +1 -0
- package/build/hardware/near-app.d.ts +95 -0
- package/build/hardware/near-app.js +291 -0
- package/build/hardware/near-app.js.map +1 -0
- package/build/hardware/transport.d.ts +94 -0
- package/build/hardware/transport.js +267 -0
- package/build/hardware/transport.js.map +1 -0
- package/build/hardware/types.d.ts +98 -0
- package/build/hardware/types.js +72 -0
- package/build/hardware/types.js.map +1 -0
- package/build/helpers/analytics.d.ts +191 -0
- package/build/helpers/analytics.js +304 -0
- package/build/helpers/analytics.js.map +1 -0
- package/build/helpers/base58.d.ts +6 -0
- package/build/helpers/base58.js +47 -0
- package/build/helpers/base58.js.map +1 -0
- package/build/helpers/events.d.ts +42 -0
- package/build/helpers/events.js +68 -0
- package/build/helpers/events.js.map +1 -0
- package/build/helpers/html.d.ts +8 -0
- package/build/helpers/html.js +30 -0
- package/build/helpers/html.js.map +1 -0
- package/build/helpers/indexdb.d.ts +14 -0
- package/build/helpers/indexdb.js +166 -0
- package/build/helpers/indexdb.js.map +1 -0
- package/build/helpers/manifest.d.ts +147 -0
- package/build/helpers/manifest.js +329 -0
- package/build/helpers/manifest.js.map +1 -0
- package/build/helpers/queue.d.ts +11 -0
- package/build/helpers/queue.js +48 -0
- package/build/helpers/queue.js.map +1 -0
- package/build/helpers/session.d.ts +119 -0
- package/build/helpers/session.js +289 -0
- package/build/helpers/session.js.map +1 -0
- package/build/helpers/simulation.d.ts +128 -0
- package/build/helpers/simulation.js +441 -0
- package/build/helpers/simulation.js.map +1 -0
- package/build/helpers/storage.d.ts +58 -0
- package/build/helpers/storage.js +190 -0
- package/build/helpers/storage.js.map +1 -0
- package/build/helpers/trust.d.ts +157 -0
- package/build/helpers/trust.js +340 -0
- package/build/helpers/trust.js.map +1 -0
- package/build/helpers/url.d.ts +1 -0
- package/build/helpers/url.js +13 -0
- package/build/helpers/url.js.map +1 -0
- package/build/helpers/uuid.d.ts +1 -0
- package/build/helpers/uuid.js +14 -0
- package/build/helpers/uuid.js.map +1 -0
- package/build/index.d.ts +21 -0
- package/build/index.js +167 -0
- package/build/index.js.map +1 -0
- package/build/popups/IframeWalletPopup.d.ts +16 -0
- package/build/popups/IframeWalletPopup.js +38 -0
- package/build/popups/IframeWalletPopup.js.map +1 -0
- package/build/popups/NearWalletsPopup.d.ts +25 -0
- package/build/popups/NearWalletsPopup.js +153 -0
- package/build/popups/NearWalletsPopup.js.map +1 -0
- package/build/popups/Popup.d.ts +22 -0
- package/build/popups/Popup.js +94 -0
- package/build/popups/Popup.js.map +1 -0
- package/build/popups/styles.d.ts +1 -0
- package/build/popups/styles.js +257 -0
- package/build/popups/styles.js.map +1 -0
- package/build/security/audit-log.d.ts +123 -0
- package/build/security/audit-log.js +268 -0
- package/build/security/audit-log.js.map +1 -0
- package/build/security/csp.d.ts +68 -0
- package/build/security/csp.js +328 -0
- package/build/security/csp.js.map +1 -0
- package/build/security/index.d.ts +10 -0
- package/build/security/index.js +42 -0
- package/build/security/index.js.map +1 -0
- package/build/security/origin-guard.d.ts +90 -0
- package/build/security/origin-guard.js +244 -0
- package/build/security/origin-guard.js.map +1 -0
- package/build/security/rate-limiter.d.ts +84 -0
- package/build/security/rate-limiter.js +212 -0
- package/build/security/rate-limiter.js.map +1 -0
- package/build/security/secure-storage.d.ts +77 -0
- package/build/security/secure-storage.js +242 -0
- package/build/security/secure-storage.js.map +1 -0
- package/build/security/transaction-guard.d.ts +71 -0
- package/build/security/transaction-guard.js +239 -0
- package/build/security/transaction-guard.js.map +1 -0
- package/build/types.d.ts +508 -0
- package/build/types.js +3 -0
- package/build/types.js.map +1 -0
- package/build/ui/AccountSwitcherModal.d.ts +53 -0
- package/build/ui/AccountSwitcherModal.js +239 -0
- package/build/ui/AccountSwitcherModal.js.map +1 -0
- package/build/ui/Modal.d.ts +84 -0
- package/build/ui/Modal.js +278 -0
- package/build/ui/Modal.js.map +1 -0
- package/build/ui/TransactionModal.d.ts +84 -0
- package/build/ui/TransactionModal.js +406 -0
- package/build/ui/TransactionModal.js.map +1 -0
- package/build/ui/WalletSelectorModal.d.ts +97 -0
- package/build/ui/WalletSelectorModal.js +481 -0
- package/build/ui/WalletSelectorModal.js.map +1 -0
- package/build/ui/icons.d.ts +19 -0
- package/build/ui/icons.js +65 -0
- package/build/ui/icons.js.map +1 -0
- package/build/ui/index.d.ts +10 -0
- package/build/ui/index.js +31 -0
- package/build/ui/index.js.map +1 -0
- package/build/ui/styles.d.ts +5 -0
- package/build/ui/styles.js +973 -0
- package/build/ui/styles.js.map +1 -0
- package/build/ui/theme.d.ts +133 -0
- package/build/ui/theme.js +204 -0
- package/build/ui/theme.js.map +1 -0
- package/build/wallets/external/index.d.ts +4 -0
- package/build/wallets/external/index.js +9 -0
- package/build/wallets/external/index.js.map +1 -0
- package/build/wallets/external/manager.d.ts +152 -0
- package/build/wallets/external/manager.js +586 -0
- package/build/wallets/external/manager.js.map +1 -0
- package/build/wallets/privileged/index.d.ts +5 -0
- package/build/wallets/privileged/index.js +12 -0
- package/build/wallets/privileged/index.js.map +1 -0
- package/build/wallets/privileged/ledger.d.ts +132 -0
- package/build/wallets/privileged/ledger.js +563 -0
- package/build/wallets/privileged/ledger.js.map +1 -0
- package/build/wallets/privileged/manager.d.ts +54 -0
- package/build/wallets/privileged/manager.js +174 -0
- package/build/wallets/privileged/manager.js.map +1 -0
- package/package.json +33 -0
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReconnectionManager = exports.DEFAULT_RECONNECT_CONFIG = exports.DEFAULT_RECONNECT_TRIGGERS = void 0;
|
|
4
|
+
exports.createReconnectionManager = createReconnectionManager;
|
|
5
|
+
const errors_1 = require("../errors");
|
|
6
|
+
const retry_1 = require("./retry");
|
|
7
|
+
/**
|
|
8
|
+
* Default reconnection triggers
|
|
9
|
+
*/
|
|
10
|
+
exports.DEFAULT_RECONNECT_TRIGGERS = {
|
|
11
|
+
visibilityChange: true,
|
|
12
|
+
networkChange: true,
|
|
13
|
+
wakeFromSleep: true,
|
|
14
|
+
heartbeatFailure: true,
|
|
15
|
+
userActivity: false,
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Default reconnection configuration
|
|
19
|
+
*/
|
|
20
|
+
exports.DEFAULT_RECONNECT_CONFIG = {
|
|
21
|
+
autoReconnect: true,
|
|
22
|
+
reconnectOn: exports.DEFAULT_RECONNECT_TRIGGERS,
|
|
23
|
+
maxReconnectAttempts: 5,
|
|
24
|
+
backoff: {
|
|
25
|
+
maxAttempts: 5,
|
|
26
|
+
baseDelay: 1000,
|
|
27
|
+
maxDelay: 30000,
|
|
28
|
+
backoffMultiplier: 2,
|
|
29
|
+
jitter: true,
|
|
30
|
+
},
|
|
31
|
+
preserveQueue: true,
|
|
32
|
+
silentReconnect: true,
|
|
33
|
+
minReconnectInterval: 1000,
|
|
34
|
+
visibilityDebounce: 500,
|
|
35
|
+
sleepThreshold: 30000,
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Reconnection Manager
|
|
39
|
+
*
|
|
40
|
+
* Handles automatic reconnection with various triggers:
|
|
41
|
+
* - Page visibility changes (tab focus)
|
|
42
|
+
* - Network status changes
|
|
43
|
+
* - System wake from sleep
|
|
44
|
+
* - Heartbeat failures
|
|
45
|
+
*/
|
|
46
|
+
class ReconnectionManager {
|
|
47
|
+
config;
|
|
48
|
+
state;
|
|
49
|
+
reconnectFn;
|
|
50
|
+
listeners = new Map();
|
|
51
|
+
// Browser event listeners
|
|
52
|
+
visibilityHandler = null;
|
|
53
|
+
networkHandler = null;
|
|
54
|
+
activityHandler = null;
|
|
55
|
+
// Timers and tracking
|
|
56
|
+
lastActivityTime = Date.now();
|
|
57
|
+
lastCheckTime = Date.now();
|
|
58
|
+
sleepCheckInterval = null;
|
|
59
|
+
visibilityDebounceTimer = null;
|
|
60
|
+
reconnectTimer = null;
|
|
61
|
+
operationIdCounter = 0;
|
|
62
|
+
constructor(reconnectFn, config = {}) {
|
|
63
|
+
this.reconnectFn = reconnectFn;
|
|
64
|
+
this.config = {
|
|
65
|
+
...exports.DEFAULT_RECONNECT_CONFIG,
|
|
66
|
+
...config,
|
|
67
|
+
reconnectOn: { ...exports.DEFAULT_RECONNECT_TRIGGERS, ...config.reconnectOn },
|
|
68
|
+
backoff: { ...exports.DEFAULT_RECONNECT_CONFIG.backoff, ...config.backoff },
|
|
69
|
+
};
|
|
70
|
+
this.state = this.createInitialState();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Create initial state
|
|
74
|
+
*/
|
|
75
|
+
createInitialState() {
|
|
76
|
+
return {
|
|
77
|
+
attempting: false,
|
|
78
|
+
attempt: 0,
|
|
79
|
+
lastAttempt: 0,
|
|
80
|
+
queuedOperations: [],
|
|
81
|
+
lastTrigger: null,
|
|
82
|
+
disabled: false,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Start listening for reconnection triggers
|
|
87
|
+
*/
|
|
88
|
+
start() {
|
|
89
|
+
if (typeof window === "undefined")
|
|
90
|
+
return;
|
|
91
|
+
// Visibility change
|
|
92
|
+
if (this.config.reconnectOn.visibilityChange) {
|
|
93
|
+
this.visibilityHandler = this.handleVisibilityChange.bind(this);
|
|
94
|
+
document.addEventListener("visibilitychange", this.visibilityHandler);
|
|
95
|
+
}
|
|
96
|
+
// Network change
|
|
97
|
+
if (this.config.reconnectOn.networkChange) {
|
|
98
|
+
this.networkHandler = this.handleNetworkChange.bind(this);
|
|
99
|
+
window.addEventListener("online", this.networkHandler);
|
|
100
|
+
window.addEventListener("offline", this.networkHandler);
|
|
101
|
+
}
|
|
102
|
+
// User activity
|
|
103
|
+
if (this.config.reconnectOn.userActivity) {
|
|
104
|
+
this.activityHandler = this.handleUserActivity.bind(this);
|
|
105
|
+
window.addEventListener("mousemove", this.activityHandler, { passive: true });
|
|
106
|
+
window.addEventListener("keydown", this.activityHandler, { passive: true });
|
|
107
|
+
window.addEventListener("touchstart", this.activityHandler, { passive: true });
|
|
108
|
+
}
|
|
109
|
+
// Wake from sleep detection
|
|
110
|
+
if (this.config.reconnectOn.wakeFromSleep) {
|
|
111
|
+
this.startSleepDetection();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Stop listening for reconnection triggers
|
|
116
|
+
*/
|
|
117
|
+
stop() {
|
|
118
|
+
if (typeof window === "undefined")
|
|
119
|
+
return;
|
|
120
|
+
if (this.visibilityHandler) {
|
|
121
|
+
document.removeEventListener("visibilitychange", this.visibilityHandler);
|
|
122
|
+
this.visibilityHandler = null;
|
|
123
|
+
}
|
|
124
|
+
if (this.networkHandler) {
|
|
125
|
+
window.removeEventListener("online", this.networkHandler);
|
|
126
|
+
window.removeEventListener("offline", this.networkHandler);
|
|
127
|
+
this.networkHandler = null;
|
|
128
|
+
}
|
|
129
|
+
if (this.activityHandler) {
|
|
130
|
+
window.removeEventListener("mousemove", this.activityHandler);
|
|
131
|
+
window.removeEventListener("keydown", this.activityHandler);
|
|
132
|
+
window.removeEventListener("touchstart", this.activityHandler);
|
|
133
|
+
this.activityHandler = null;
|
|
134
|
+
}
|
|
135
|
+
this.stopSleepDetection();
|
|
136
|
+
this.cancelReconnect();
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Handle visibility change
|
|
140
|
+
*/
|
|
141
|
+
handleVisibilityChange() {
|
|
142
|
+
const visible = document.visibilityState === "visible";
|
|
143
|
+
this.emit("trigger:visibility", { visible });
|
|
144
|
+
if (visible) {
|
|
145
|
+
// Debounce to avoid rapid reconnects
|
|
146
|
+
if (this.visibilityDebounceTimer) {
|
|
147
|
+
clearTimeout(this.visibilityDebounceTimer);
|
|
148
|
+
}
|
|
149
|
+
this.visibilityDebounceTimer = setTimeout(() => {
|
|
150
|
+
this.trigger("visibilityChange");
|
|
151
|
+
}, this.config.visibilityDebounce);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Handle network change
|
|
156
|
+
*/
|
|
157
|
+
handleNetworkChange() {
|
|
158
|
+
const online = navigator.onLine;
|
|
159
|
+
this.emit("trigger:network", { online });
|
|
160
|
+
if (online) {
|
|
161
|
+
this.trigger("networkChange");
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Handle user activity
|
|
166
|
+
*/
|
|
167
|
+
handleUserActivity() {
|
|
168
|
+
this.lastActivityTime = Date.now();
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Start sleep detection
|
|
172
|
+
*/
|
|
173
|
+
startSleepDetection() {
|
|
174
|
+
this.lastCheckTime = Date.now();
|
|
175
|
+
this.sleepCheckInterval = setInterval(() => {
|
|
176
|
+
const now = Date.now();
|
|
177
|
+
const elapsed = now - this.lastCheckTime;
|
|
178
|
+
// If elapsed time is much greater than interval, system was asleep
|
|
179
|
+
if (elapsed > this.config.sleepThreshold) {
|
|
180
|
+
this.emit("trigger:wake", { sleepDuration: elapsed });
|
|
181
|
+
this.trigger("wakeFromSleep");
|
|
182
|
+
}
|
|
183
|
+
this.lastCheckTime = now;
|
|
184
|
+
}, 10000); // Check every 10 seconds
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Stop sleep detection
|
|
188
|
+
*/
|
|
189
|
+
stopSleepDetection() {
|
|
190
|
+
if (this.sleepCheckInterval) {
|
|
191
|
+
clearInterval(this.sleepCheckInterval);
|
|
192
|
+
this.sleepCheckInterval = null;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Trigger reconnection
|
|
197
|
+
*/
|
|
198
|
+
async trigger(reason) {
|
|
199
|
+
if (!this.config.autoReconnect || this.state.disabled) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
// Don't interrupt ongoing reconnect
|
|
203
|
+
if (this.state.attempting) {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
// Enforce minimum interval
|
|
207
|
+
if (Date.now() - this.state.lastAttempt < this.config.minReconnectInterval) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
this.state.lastTrigger = reason;
|
|
211
|
+
return this.attemptReconnect();
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Attempt reconnection with retries
|
|
215
|
+
*/
|
|
216
|
+
async attemptReconnect() {
|
|
217
|
+
this.state.attempting = true;
|
|
218
|
+
this.state.attempt = 0;
|
|
219
|
+
const maxAttempts = this.config.maxReconnectAttempts;
|
|
220
|
+
const backoff = { ...retry_1.DEFAULT_RETRY_CONFIG, ...this.config.backoff };
|
|
221
|
+
while (this.state.attempt < maxAttempts) {
|
|
222
|
+
this.state.attempt++;
|
|
223
|
+
this.state.lastAttempt = Date.now();
|
|
224
|
+
this.emit("reconnect:started", {
|
|
225
|
+
attempt: this.state.attempt,
|
|
226
|
+
trigger: this.state.lastTrigger || "unknown",
|
|
227
|
+
});
|
|
228
|
+
try {
|
|
229
|
+
const success = await this.reconnectFn();
|
|
230
|
+
if (success) {
|
|
231
|
+
this.emit("reconnect:success", {
|
|
232
|
+
attempt: this.state.attempt,
|
|
233
|
+
trigger: this.state.lastTrigger || "unknown",
|
|
234
|
+
});
|
|
235
|
+
this.state.attempting = false;
|
|
236
|
+
// Replay queued operations
|
|
237
|
+
if (this.config.preserveQueue && this.state.queuedOperations.length > 0) {
|
|
238
|
+
await this.replayQueue();
|
|
239
|
+
}
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
const willRetry = this.state.attempt < maxAttempts;
|
|
245
|
+
this.emit("reconnect:failed", {
|
|
246
|
+
attempt: this.state.attempt,
|
|
247
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
248
|
+
willRetry,
|
|
249
|
+
});
|
|
250
|
+
if (willRetry) {
|
|
251
|
+
// Wait before next attempt
|
|
252
|
+
const delay = (0, retry_1.calculateBackoffDelay)(this.state.attempt - 1, backoff);
|
|
253
|
+
await this.wait(delay);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
// All attempts exhausted
|
|
258
|
+
this.emit("reconnect:exhausted", {
|
|
259
|
+
attempts: this.state.attempt,
|
|
260
|
+
trigger: this.state.lastTrigger || "unknown",
|
|
261
|
+
});
|
|
262
|
+
this.state.attempting = false;
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Cancel ongoing reconnection
|
|
267
|
+
*/
|
|
268
|
+
cancelReconnect() {
|
|
269
|
+
if (this.state.attempting) {
|
|
270
|
+
this.state.attempting = false;
|
|
271
|
+
this.emit("reconnect:cancelled", { reason: "manual" });
|
|
272
|
+
}
|
|
273
|
+
if (this.reconnectTimer) {
|
|
274
|
+
clearTimeout(this.reconnectTimer);
|
|
275
|
+
this.reconnectTimer = null;
|
|
276
|
+
}
|
|
277
|
+
if (this.visibilityDebounceTimer) {
|
|
278
|
+
clearTimeout(this.visibilityDebounceTimer);
|
|
279
|
+
this.visibilityDebounceTimer = null;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Queue an operation for replay after reconnect
|
|
284
|
+
*/
|
|
285
|
+
queueOperation(type, payload, maxRetries = 3) {
|
|
286
|
+
return new Promise((resolve, reject) => {
|
|
287
|
+
const operation = {
|
|
288
|
+
id: `op_${++this.operationIdCounter}_${Date.now()}`,
|
|
289
|
+
type,
|
|
290
|
+
payload,
|
|
291
|
+
timestamp: Date.now(),
|
|
292
|
+
retries: 0,
|
|
293
|
+
maxRetries,
|
|
294
|
+
resolve: resolve,
|
|
295
|
+
reject,
|
|
296
|
+
};
|
|
297
|
+
this.state.queuedOperations.push(operation);
|
|
298
|
+
this.emit("queue:added", { operation });
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Remove an operation from the queue
|
|
303
|
+
*/
|
|
304
|
+
removeOperation(operationId) {
|
|
305
|
+
const index = this.state.queuedOperations.findIndex((op) => op.id === operationId);
|
|
306
|
+
if (index !== -1) {
|
|
307
|
+
const [removed] = this.state.queuedOperations.splice(index, 1);
|
|
308
|
+
removed.reject(new errors_1.WalletError({
|
|
309
|
+
code: errors_1.ErrorCode.USER_REJECTED,
|
|
310
|
+
message: "Operation cancelled",
|
|
311
|
+
}));
|
|
312
|
+
this.emit("queue:removed", { operationId });
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Clear all queued operations
|
|
319
|
+
*/
|
|
320
|
+
clearQueue() {
|
|
321
|
+
for (const op of this.state.queuedOperations) {
|
|
322
|
+
op.reject(new errors_1.WalletError({
|
|
323
|
+
code: errors_1.ErrorCode.USER_REJECTED,
|
|
324
|
+
message: "Queue cleared",
|
|
325
|
+
}));
|
|
326
|
+
}
|
|
327
|
+
this.state.queuedOperations = [];
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Replay queued operations
|
|
331
|
+
*/
|
|
332
|
+
async replayQueue() {
|
|
333
|
+
const operations = [...this.state.queuedOperations];
|
|
334
|
+
this.state.queuedOperations = [];
|
|
335
|
+
this.emit("queue:replaying", { operations });
|
|
336
|
+
let successful = 0;
|
|
337
|
+
let failed = 0;
|
|
338
|
+
for (const op of operations) {
|
|
339
|
+
try {
|
|
340
|
+
// The actual replay logic would be implemented by the caller
|
|
341
|
+
// through a custom replay handler
|
|
342
|
+
// For now, we just resolve with null and let the caller handle it
|
|
343
|
+
op.resolve(null);
|
|
344
|
+
successful++;
|
|
345
|
+
}
|
|
346
|
+
catch (error) {
|
|
347
|
+
op.reject(error);
|
|
348
|
+
failed++;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
this.emit("queue:replayed", { successful, failed });
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Get reconnection state
|
|
355
|
+
*/
|
|
356
|
+
getState() {
|
|
357
|
+
return { ...this.state };
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Get queued operations count
|
|
361
|
+
*/
|
|
362
|
+
getQueueSize() {
|
|
363
|
+
return this.state.queuedOperations.length;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Disable reconnection temporarily
|
|
367
|
+
*/
|
|
368
|
+
disable() {
|
|
369
|
+
this.state.disabled = true;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Enable reconnection
|
|
373
|
+
*/
|
|
374
|
+
enable() {
|
|
375
|
+
this.state.disabled = false;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Check if reconnection is enabled
|
|
379
|
+
*/
|
|
380
|
+
isEnabled() {
|
|
381
|
+
return this.config.autoReconnect && !this.state.disabled;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Event listener
|
|
385
|
+
*/
|
|
386
|
+
on(event, callback) {
|
|
387
|
+
if (!this.listeners.has(event)) {
|
|
388
|
+
this.listeners.set(event, new Set());
|
|
389
|
+
}
|
|
390
|
+
this.listeners.get(event).add(callback);
|
|
391
|
+
return () => {
|
|
392
|
+
this.listeners.get(event)?.delete(callback);
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Emit event
|
|
397
|
+
*/
|
|
398
|
+
emit(event, payload) {
|
|
399
|
+
const listeners = this.listeners.get(event);
|
|
400
|
+
if (listeners) {
|
|
401
|
+
for (const listener of listeners) {
|
|
402
|
+
try {
|
|
403
|
+
listener(payload);
|
|
404
|
+
}
|
|
405
|
+
catch (e) {
|
|
406
|
+
console.error(`Error in reconnect event listener for ${event}:`, e);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Wait helper
|
|
413
|
+
*/
|
|
414
|
+
wait(ms) {
|
|
415
|
+
return new Promise((resolve) => {
|
|
416
|
+
this.reconnectTimer = setTimeout(resolve, ms);
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Update configuration
|
|
421
|
+
*/
|
|
422
|
+
updateConfig(config) {
|
|
423
|
+
this.config = {
|
|
424
|
+
...this.config,
|
|
425
|
+
...config,
|
|
426
|
+
reconnectOn: { ...this.config.reconnectOn, ...config.reconnectOn },
|
|
427
|
+
backoff: { ...this.config.backoff, ...config.backoff },
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Reset state
|
|
432
|
+
*/
|
|
433
|
+
reset() {
|
|
434
|
+
this.cancelReconnect();
|
|
435
|
+
this.clearQueue();
|
|
436
|
+
this.state = this.createInitialState();
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Cleanup
|
|
440
|
+
*/
|
|
441
|
+
destroy() {
|
|
442
|
+
this.stop();
|
|
443
|
+
this.clearQueue();
|
|
444
|
+
this.listeners.clear();
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
exports.ReconnectionManager = ReconnectionManager;
|
|
448
|
+
/**
|
|
449
|
+
* Create a reconnection manager
|
|
450
|
+
*/
|
|
451
|
+
function createReconnectionManager(reconnectFn, config) {
|
|
452
|
+
return new ReconnectionManager(reconnectFn, config);
|
|
453
|
+
}
|
|
454
|
+
//# sourceMappingURL=reconnect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reconnect.js","sourceRoot":"","sources":["../../src/connection/reconnect.ts"],"names":[],"mappings":";;;AAumBA,8DAKC;AA5mBD,sCAAmD;AACnD,mCAAmF;AA4FnF;;GAEG;AACU,QAAA,0BAA0B,GAAsB;IAC3D,gBAAgB,EAAE,IAAI;IACtB,aAAa,EAAE,IAAI;IACnB,aAAa,EAAE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AAC;gBACtE,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED;;OAEG;IACK,IAAI,CAAC,EAAU;QACrB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC7B,IAAI,CAAC,cAAc,GAAG,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;QAChD,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,YAAY,CAAC,MAAgC;QAC3C,IAAI,CAAC,MAAM,GAAG;YACZ,GAAG,IAAI,CAAC,MAAM;YACd,GAAG,MAAM;YACT,WAAW,EAAE,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,GAAG,MAAM,CAAC,WAAW,EAAE;YAClE,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,MAAM,CAAC,OAAO,EAAE;SACvD,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,eAAe,EAAE,CAAC;QACvB,IAAI,CAAC,UAAU,EAAE,CAAC;QAClB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAC;IACzC,CAAC;IAED;;OAEG;IACH,OAAO;QACL,IAAI,CAAC,IAAI,EAAE,CAAC;QACZ,IAAI,CAAC,UAAU,EAAE,CAAC;QAClB,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;CACF;AAvdD,kDAudC;AAED;;GAEG;AACH,SAAgB,yBAAyB,CACvC,WAAwB,EACxB,MAAiC;IAEjC,OAAO,IAAI,mBAAmB,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;AACtD,CAAC"}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { ErrorCode } from "../errors";
|
|
2
|
+
/**
|
|
3
|
+
* Retry configuration
|
|
4
|
+
*/
|
|
5
|
+
export interface RetryConfig {
|
|
6
|
+
/** Maximum number of retry attempts (default: 3) */
|
|
7
|
+
maxAttempts: number;
|
|
8
|
+
/** Base delay between retries in ms (default: 1000) */
|
|
9
|
+
baseDelay: number;
|
|
10
|
+
/** Maximum delay between retries in ms (default: 30000) */
|
|
11
|
+
maxDelay: number;
|
|
12
|
+
/** Backoff multiplier (default: 2) */
|
|
13
|
+
backoffMultiplier: number;
|
|
14
|
+
/** Whether to add jitter to prevent thundering herd (default: true) */
|
|
15
|
+
jitter: boolean;
|
|
16
|
+
/** Error codes that should trigger a retry */
|
|
17
|
+
retryableErrors: ErrorCode[];
|
|
18
|
+
/** Custom retry condition function */
|
|
19
|
+
shouldRetry?: (error: unknown, attempt: number) => boolean;
|
|
20
|
+
/** Callback for each retry attempt */
|
|
21
|
+
onRetry?: (attempt: number, delay: number, error: unknown) => void;
|
|
22
|
+
/** Callback when all retries exhausted */
|
|
23
|
+
onExhausted?: (error: unknown, attempts: number) => void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Timeout configuration
|
|
27
|
+
*/
|
|
28
|
+
export interface TimeoutConfig {
|
|
29
|
+
/** Connection timeout in ms (default: 30000) */
|
|
30
|
+
connection: number;
|
|
31
|
+
/** Sign transaction timeout in ms (default: 120000) */
|
|
32
|
+
signTransaction: number;
|
|
33
|
+
/** Sign message timeout in ms (default: 60000) */
|
|
34
|
+
signMessage: number;
|
|
35
|
+
/** Broadcast timeout in ms (default: 60000) */
|
|
36
|
+
broadcast: number;
|
|
37
|
+
/** Show warning at this percentage of timeout (default: 0.8) */
|
|
38
|
+
warningThreshold: number;
|
|
39
|
+
/** Callback when warning threshold reached */
|
|
40
|
+
onWarning?: (operation: string, elapsed: number, timeout: number) => void;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Circuit breaker configuration
|
|
44
|
+
*/
|
|
45
|
+
export interface CircuitBreakerConfig {
|
|
46
|
+
/** Number of failures before opening circuit (default: 5) */
|
|
47
|
+
failureThreshold: number;
|
|
48
|
+
/** Time in ms before attempting to close circuit (default: 60000) */
|
|
49
|
+
cooldownPeriod: number;
|
|
50
|
+
/** Interval for health checks in ms (default: 10000) */
|
|
51
|
+
healthCheckInterval: number;
|
|
52
|
+
/** Callback when circuit opens */
|
|
53
|
+
onOpen?: (walletId: string, failures: number) => void;
|
|
54
|
+
/** Callback when circuit closes */
|
|
55
|
+
onClose?: (walletId: string) => void;
|
|
56
|
+
/** Callback when circuit is half-open (testing) */
|
|
57
|
+
onHalfOpen?: (walletId: string) => void;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Circuit breaker state
|
|
61
|
+
*/
|
|
62
|
+
export type CircuitState = "closed" | "open" | "half-open";
|
|
63
|
+
/**
|
|
64
|
+
* Default retry configuration
|
|
65
|
+
*/
|
|
66
|
+
export declare const DEFAULT_RETRY_CONFIG: RetryConfig;
|
|
67
|
+
/**
|
|
68
|
+
* Default timeout configuration
|
|
69
|
+
*/
|
|
70
|
+
export declare const DEFAULT_TIMEOUT_CONFIG: TimeoutConfig;
|
|
71
|
+
/**
|
|
72
|
+
* Default circuit breaker configuration
|
|
73
|
+
*/
|
|
74
|
+
export declare const DEFAULT_CIRCUIT_BREAKER_CONFIG: CircuitBreakerConfig;
|
|
75
|
+
/**
|
|
76
|
+
* Calculate delay for a given attempt with exponential backoff
|
|
77
|
+
*/
|
|
78
|
+
export declare function calculateBackoffDelay(attempt: number, config: Pick<RetryConfig, "baseDelay" | "maxDelay" | "backoffMultiplier" | "jitter">): number;
|
|
79
|
+
/**
|
|
80
|
+
* Check if an error is retryable based on config
|
|
81
|
+
*/
|
|
82
|
+
export declare function isRetryableError(error: unknown, config: RetryConfig): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Execute an operation with retry logic
|
|
85
|
+
*/
|
|
86
|
+
export declare function withRetry<T>(operation: () => Promise<T>, config?: Partial<RetryConfig>): Promise<T>;
|
|
87
|
+
/**
|
|
88
|
+
* Execute an operation with timeout
|
|
89
|
+
*/
|
|
90
|
+
export declare function withTimeout<T>(operation: () => Promise<T>, timeoutMs: number, operationName?: string, onWarning?: (elapsed: number, timeout: number) => void, warningThreshold?: number): Promise<T>;
|
|
91
|
+
/**
|
|
92
|
+
* Combine retry and timeout
|
|
93
|
+
*/
|
|
94
|
+
export declare function withRetryAndTimeout<T>(operation: () => Promise<T>, timeoutMs: number, retryConfig?: Partial<RetryConfig>, operationName?: string): Promise<T>;
|
|
95
|
+
/**
|
|
96
|
+
* Circuit Breaker
|
|
97
|
+
*
|
|
98
|
+
* Prevents hammering a failing wallet by temporarily disabling connections
|
|
99
|
+
*/
|
|
100
|
+
export declare class CircuitBreaker {
|
|
101
|
+
private circuits;
|
|
102
|
+
private config;
|
|
103
|
+
private healthCheckTimers;
|
|
104
|
+
constructor(config?: Partial<CircuitBreakerConfig>);
|
|
105
|
+
/**
|
|
106
|
+
* Get or create circuit entry for a wallet
|
|
107
|
+
*/
|
|
108
|
+
private getCircuit;
|
|
109
|
+
/**
|
|
110
|
+
* Check if a wallet's circuit allows requests
|
|
111
|
+
*/
|
|
112
|
+
isAllowed(walletId: string): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Record a successful operation
|
|
115
|
+
*/
|
|
116
|
+
recordSuccess(walletId: string): void;
|
|
117
|
+
/**
|
|
118
|
+
* Record a failed operation
|
|
119
|
+
*/
|
|
120
|
+
recordFailure(walletId: string): void;
|
|
121
|
+
/**
|
|
122
|
+
* Transition circuit to a new state
|
|
123
|
+
*/
|
|
124
|
+
private transitionTo;
|
|
125
|
+
/**
|
|
126
|
+
* Schedule health check for an open circuit
|
|
127
|
+
*/
|
|
128
|
+
private scheduleHealthCheck;
|
|
129
|
+
/**
|
|
130
|
+
* Cancel health check timer
|
|
131
|
+
*/
|
|
132
|
+
private cancelHealthCheck;
|
|
133
|
+
/**
|
|
134
|
+
* Manually reset a circuit
|
|
135
|
+
*/
|
|
136
|
+
reset(walletId: string): void;
|
|
137
|
+
/**
|
|
138
|
+
* Manually reset all circuits
|
|
139
|
+
*/
|
|
140
|
+
resetAll(): void;
|
|
141
|
+
/**
|
|
142
|
+
* Get circuit state for a wallet
|
|
143
|
+
*/
|
|
144
|
+
getState(walletId: string): CircuitState;
|
|
145
|
+
/**
|
|
146
|
+
* Get circuit info for a wallet
|
|
147
|
+
*/
|
|
148
|
+
getInfo(walletId: string): {
|
|
149
|
+
state: CircuitState;
|
|
150
|
+
failures: number;
|
|
151
|
+
lastFailure: number;
|
|
152
|
+
lastSuccess: number;
|
|
153
|
+
remainingCooldown: number | null;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Get all circuit states
|
|
157
|
+
*/
|
|
158
|
+
getAllStates(): Record<string, CircuitState>;
|
|
159
|
+
/**
|
|
160
|
+
* Execute an operation through the circuit breaker
|
|
161
|
+
*/
|
|
162
|
+
execute<T>(walletId: string, operation: () => Promise<T>): Promise<T>;
|
|
163
|
+
/**
|
|
164
|
+
* Cleanup all timers
|
|
165
|
+
*/
|
|
166
|
+
destroy(): void;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Create a retry wrapper with default configuration
|
|
170
|
+
*/
|
|
171
|
+
export declare function createRetryWrapper(config?: Partial<RetryConfig>): <T>(operation: () => Promise<T>) => Promise<T>;
|
|
172
|
+
/**
|
|
173
|
+
* Create a timeout wrapper with default configuration
|
|
174
|
+
*/
|
|
175
|
+
export declare function createTimeoutWrapper(timeoutMs: number, operationName?: string, onWarning?: (elapsed: number, timeout: number) => void): <T>(operation: () => Promise<T>) => Promise<T>;
|
|
176
|
+
/**
|
|
177
|
+
* Cancellable operation wrapper
|
|
178
|
+
*/
|
|
179
|
+
export interface CancellableOperation<T> {
|
|
180
|
+
promise: Promise<T>;
|
|
181
|
+
cancel: () => void;
|
|
182
|
+
isCancelled: () => boolean;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Create a cancellable operation
|
|
186
|
+
*/
|
|
187
|
+
export declare function makeCancellable<T>(operation: () => Promise<T>): CancellableOperation<T>;
|