edge-core-js 2.48.0 → 2.48.1
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/CHANGELOG.md +4 -0
- package/android/src/main/assets/edge-core-js/edge-core.js +1 -1
- package/lib/util/nym.js +137 -43
- package/package.json +1 -1
package/lib/util/nym.js
CHANGED
|
@@ -32,54 +32,148 @@ export const mixFetchOptions = {
|
|
|
32
32
|
*/
|
|
33
33
|
const SETUP_TIMEOUT_MS = 60000
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* How long to refuse new setups after one fails, and the ceiling that wait
|
|
37
|
+
* doubles up to.
|
|
38
|
+
*
|
|
39
|
+
* Every `createMixFetch` spawns a web worker holding megabytes of WASM before
|
|
40
|
+
* it ever contacts the gateway, and the library exposes no way to terminate
|
|
41
|
+
* that worker, so a failed setup leaves one behind. Retrying on each request
|
|
42
|
+
* therefore costs memory per attempt: with a dead gateway and a poll loop
|
|
43
|
+
* driving requests every few seconds, the workers accumulate until the host
|
|
44
|
+
* kills the whole JS context. On iOS that reads to the user as being logged
|
|
45
|
+
* out, since the core's WebView is what gets killed and reloaded.
|
|
46
|
+
*
|
|
47
|
+
* A cooldown bounds the cost to one worker per window, and the doubling keeps
|
|
48
|
+
* a long outage from spending any meaningful memory at all.
|
|
49
|
+
*/
|
|
50
|
+
const RETRY_BASE_MS = 30000
|
|
51
|
+
const RETRY_MAX_MS = 300000
|
|
37
52
|
|
|
38
53
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
54
|
+
* Builds the mixFetch setup routine over its own cooldown state.
|
|
55
|
+
*
|
|
56
|
+
* The returned function initializes the NYM mixFetch client, and must be
|
|
57
|
+
* called before using mixFetch. It is safe to call multiple times: subsequent
|
|
58
|
+
* calls return the same promise.
|
|
59
|
+
*
|
|
60
|
+
* A failed setup starts a cooldown: calls made before it expires fail
|
|
61
|
+
* immediately with the error that started it, instead of building another
|
|
62
|
+
* client.
|
|
63
|
+
*
|
|
64
|
+
* Tests build their own instance over a clock they control, which also gives
|
|
65
|
+
* them fresh state per case.
|
|
66
|
+
*
|
|
67
|
+
* The cooldown state is per instance, but the client, its worker and
|
|
68
|
+
* `window.__mixFetchGlobal` are process-global, so two live instances would
|
|
69
|
+
* each hold a cooldown of their own while spawning workers into the same
|
|
70
|
+
* process. The single instance exported below is the only supported
|
|
71
|
+
* arrangement.
|
|
41
72
|
*/
|
|
42
|
-
export
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
73
|
+
export function makeMixFetchSetup(
|
|
74
|
+
now = () => Date.now()
|
|
75
|
+
) {
|
|
76
|
+
let mixFetchInitPromise = null
|
|
77
|
+
|
|
78
|
+
/** When a new setup may be attempted, and the wait that produced it. */
|
|
79
|
+
let retryAfter = 0
|
|
80
|
+
let retryDelay = RETRY_BASE_MS
|
|
81
|
+
|
|
82
|
+
/** The failure to re-throw for callers that arrive during the cooldown. */
|
|
83
|
+
let lastError
|
|
84
|
+
|
|
85
|
+
return async function initMixFetch(log) {
|
|
86
|
+
if (mixFetchInitPromise == null) {
|
|
87
|
+
if (now() < retryAfter) {
|
|
88
|
+
// Re-throwing `lastError` itself would hand the caller a stack and a
|
|
89
|
+
// message from a setup that ended minutes ago, so a cooldown
|
|
90
|
+
// rejection reads in a crash report as a fresh 60-second timeout. It
|
|
91
|
+
// is also not necessarily an `Error`: the library rejects with a raw
|
|
92
|
+
// `MessageEvent` on a worker error, so `.message` can be undefined
|
|
93
|
+
// downstream.
|
|
94
|
+
//
|
|
95
|
+
// Nothing is logged here. This runs once per refused caller, and the
|
|
96
|
+
// breadcrumb that makes the quiet window visible is emitted once per
|
|
97
|
+
// window where the cooldown is armed.
|
|
98
|
+
const remainingMs = retryAfter - now()
|
|
99
|
+
const error = new Error(
|
|
100
|
+
`mixFetch setup is cooling down for another ${Math.round(
|
|
101
|
+
remainingMs / 1000
|
|
102
|
+
)}s`
|
|
58
103
|
)
|
|
59
|
-
|
|
60
|
-
})
|
|
61
|
-
mixFetchInitPromise = Promise.race([pending, timeout])
|
|
62
|
-
.then(mixFetchModule => {
|
|
63
|
-
log('mixFetch initialized successfully')
|
|
64
|
-
return mixFetchModule
|
|
65
|
-
})
|
|
66
|
-
.catch(async error => {
|
|
67
|
-
// Clean up stale global state left by the failed init so the
|
|
68
|
-
// next createMixFetch call starts fresh instead of reusing a
|
|
69
|
-
// broken singleton.
|
|
70
|
-
try {
|
|
71
|
-
await disconnectMixFetch()
|
|
72
|
-
} catch (e) {}
|
|
73
|
-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
74
|
-
delete (window ).__mixFetchGlobal
|
|
75
|
-
mixFetchInitPromise = null
|
|
76
|
-
log.error('mixFetch initialization failed:', error)
|
|
104
|
+
error.cause = lastError
|
|
77
105
|
throw error
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
log('Initializing mixFetch...')
|
|
109
|
+
const pending = createMixFetch(mixFetchOptions)
|
|
110
|
+
// The timeout below can abandon this setup while it is still in flight.
|
|
111
|
+
// `createMixFetch` publishes `window.__mixFetchGlobal` as soon as the
|
|
112
|
+
// worker exists and only then awaits the gateway handshake
|
|
113
|
+
// (`@nymproject/mix-fetch/index.js:446-449`), so the failure path
|
|
114
|
+
// usually deletes a global whose owner is already past that assignment:
|
|
115
|
+
// the abandoned worker stays alive and unreachable, and the next setup
|
|
116
|
+
// builds a fresh one. A delete that lands before the assignment lets the
|
|
117
|
+
// late completion re-publish instead, and the line right after it
|
|
118
|
+
// configures that client, so the next setup finds a working global
|
|
119
|
+
// rather than an unconfigured one. Either way the cost is one worker per
|
|
120
|
+
// cooldown window, against a ceiling of RETRY_MAX_MS. Swallow the late
|
|
121
|
+
// rejection so it is not unhandled.
|
|
122
|
+
pending.catch(() => {})
|
|
123
|
+
let timer
|
|
124
|
+
const timeout = new Promise((resolve, reject) => {
|
|
125
|
+
timer = setTimeout(() => {
|
|
126
|
+
reject(
|
|
127
|
+
new Error(`mixFetch setup timed out after ${SETUP_TIMEOUT_MS}ms`)
|
|
128
|
+
)
|
|
129
|
+
}, SETUP_TIMEOUT_MS)
|
|
78
130
|
})
|
|
79
|
-
.
|
|
80
|
-
|
|
81
|
-
|
|
131
|
+
mixFetchInitPromise = Promise.race([pending, timeout])
|
|
132
|
+
.then(mixFetchModule => {
|
|
133
|
+
log('mixFetch initialized successfully')
|
|
134
|
+
return mixFetchModule
|
|
135
|
+
})
|
|
136
|
+
.catch(error => {
|
|
137
|
+
// Arm the cooldown before any cleanup. The timeout fires while the
|
|
138
|
+
// setup is still running, so the disconnect below is an RPC into a
|
|
139
|
+
// worker that is still busy and has no bounded completion; awaiting
|
|
140
|
+
// it would leave `mixFetchInitPromise` pending forever, and every
|
|
141
|
+
// later caller would await that instead of failing fast.
|
|
142
|
+
mixFetchInitPromise = null
|
|
143
|
+
lastError = error
|
|
144
|
+
retryAfter = now() + retryDelay
|
|
145
|
+
log.error(
|
|
146
|
+
`mixFetch initialization failed (no retry for ${Math.round(
|
|
147
|
+
retryDelay / 1000
|
|
148
|
+
)}s):`,
|
|
149
|
+
error
|
|
150
|
+
)
|
|
151
|
+
// One breadcrumb per cooldown window, not one per refused caller.
|
|
152
|
+
// The app caps Sentry at 25 breadcrumbs, so a poll loop retrying
|
|
153
|
+
// every few seconds would evict the whole history in about a
|
|
154
|
+
// minute, blinding the crash report this failure most needs to
|
|
155
|
+
// appear in. Emitted before the doubling below, so it names the
|
|
156
|
+
// window actually armed.
|
|
157
|
+
log.breadcrumb('mixFetch setup failed, cooling down', {
|
|
158
|
+
cooldownMs: retryDelay
|
|
159
|
+
})
|
|
160
|
+
retryDelay = Math.min(retryDelay * 2, RETRY_MAX_MS)
|
|
161
|
+
|
|
162
|
+
// Best-effort: clear the library's singleton so the next setup starts
|
|
163
|
+
// fresh instead of reusing a broken one. Not awaited, per above.
|
|
164
|
+
disconnectMixFetch().catch(() => {})
|
|
165
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
166
|
+
delete (window ).__mixFetchGlobal
|
|
167
|
+
|
|
168
|
+
throw error
|
|
169
|
+
})
|
|
170
|
+
.finally(() => {
|
|
171
|
+
clearTimeout(timer)
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
const mixFetchModule = await mixFetchInitPromise
|
|
175
|
+
return mixFetchModule.mixFetch
|
|
82
176
|
}
|
|
83
|
-
const mixFetchModule = await mixFetchInitPromise
|
|
84
|
-
return mixFetchModule.mixFetch
|
|
85
177
|
}
|
|
178
|
+
|
|
179
|
+
export const initMixFetch = makeMixFetchSetup()
|