react-native-mosquito-transport 0.0.55 → 0.0.57
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/package.json +1 -1
- package/src/helpers/utils.js +19 -7
- package/src/index.js +4 -4
- package/src/products/auth/accessor.js +40 -4
- package/src/products/auth/index.js +1 -5
package/package.json
CHANGED
package/src/helpers/utils.js
CHANGED
|
@@ -155,17 +155,27 @@ export const awaitStore = () => new Promise(resolve => {
|
|
|
155
155
|
});
|
|
156
156
|
});
|
|
157
157
|
|
|
158
|
-
export const checkAreYouOk = (projectUrl) => {
|
|
159
|
-
if (!Scoped.AreYouOkPromise[projectUrl]) {
|
|
158
|
+
export const checkAreYouOk = (projectUrl, refresh) => {
|
|
159
|
+
if (!Scoped.AreYouOkPromise[projectUrl] || refresh) {
|
|
160
160
|
Scoped.IS_CONNECTED[projectUrl] = undefined;
|
|
161
161
|
ServerReachableListener.dispatchPersist(projectUrl, undefined);
|
|
162
162
|
|
|
163
|
+
const thatState = AppState.currentState;
|
|
164
|
+
|
|
163
165
|
const promise = fetch(engine_api._areYouOk(projectUrl), { credentials: 'omit' })
|
|
164
166
|
.then(async r => (await r.json()).status === 'yes')
|
|
165
167
|
.catch(() => false)
|
|
166
168
|
.then(async connected => {
|
|
167
|
-
Scoped.
|
|
168
|
-
|
|
169
|
+
const thatPromise = Scoped.AreYouOkPromise[projectUrl];
|
|
170
|
+
if (thatPromise !== promise) {
|
|
171
|
+
if (thatPromise) return thatPromise;
|
|
172
|
+
return Scoped.IS_CONNECTED[projectUrl];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (thatState === AppState.currentState) {
|
|
176
|
+
Scoped.IS_CONNECTED[projectUrl] = connected;
|
|
177
|
+
ServerReachableListener.dispatchPersist(projectUrl, connected);
|
|
178
|
+
}
|
|
169
179
|
|
|
170
180
|
delete Scoped.AreYouOkPromise[projectUrl];
|
|
171
181
|
return connected;
|
|
@@ -180,8 +190,10 @@ export const checkAreYouOk = (projectUrl) => {
|
|
|
180
190
|
export const listenReachableServer = (callback, projectUrl) => {
|
|
181
191
|
let lastValue;
|
|
182
192
|
return ServerReachableListener.listenToPersist(projectUrl, t => {
|
|
183
|
-
if (typeof t === 'boolean' && t !== lastValue)
|
|
184
|
-
|
|
193
|
+
if (typeof t === 'boolean' && t !== lastValue) {
|
|
194
|
+
callback?.(t);
|
|
195
|
+
lastValue = t;
|
|
196
|
+
}
|
|
185
197
|
});
|
|
186
198
|
};
|
|
187
199
|
|
|
@@ -208,7 +220,7 @@ export const awaitReachableServer = (projectUrl, pauseForRetry) =>
|
|
|
208
220
|
}
|
|
209
221
|
|
|
210
222
|
if (pauseForRetry) {
|
|
211
|
-
setTimeout(check, Number.isInteger(pauseForRetry) ? pauseForRetry :
|
|
223
|
+
setTimeout(check, Number.isInteger(pauseForRetry) ? pauseForRetry : 500);
|
|
212
224
|
} else check();
|
|
213
225
|
});
|
|
214
226
|
|
package/src/index.js
CHANGED
|
@@ -89,10 +89,10 @@ class RNMT {
|
|
|
89
89
|
});
|
|
90
90
|
};
|
|
91
91
|
|
|
92
|
-
const manualCheckConnection = () => {
|
|
92
|
+
const manualCheckConnection = (refresh) => {
|
|
93
93
|
const ref = ++connectionIte;
|
|
94
94
|
|
|
95
|
-
checkAreYouOk(projectUrl).then(ok => {
|
|
95
|
+
checkAreYouOk(projectUrl, refresh).then(ok => {
|
|
96
96
|
if (ref !== connectionIte) return;
|
|
97
97
|
if (ok) {
|
|
98
98
|
onConnect();
|
|
@@ -111,11 +111,11 @@ class RNMT {
|
|
|
111
111
|
|
|
112
112
|
socket.on('connect', onConnect);
|
|
113
113
|
socket.on('disconnect', () => {
|
|
114
|
-
manualCheckConnection();
|
|
114
|
+
manualCheckConnection(true);
|
|
115
115
|
});
|
|
116
116
|
|
|
117
117
|
AppState.addEventListener('change', s => {
|
|
118
|
-
manualCheckConnection();
|
|
118
|
+
if (s === 'active') manualCheckConnection();
|
|
119
119
|
});
|
|
120
120
|
|
|
121
121
|
const updateMountedToken = () => {
|
|
@@ -89,7 +89,43 @@ export const ensureActiveToken = async (projectUrl) => {
|
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
export const listenTokenReady = (callback, projectUrl) =>
|
|
92
|
+
export const listenTokenReady = (callback, projectUrl) => {
|
|
93
|
+
let lastToken;
|
|
94
|
+
let resetDelayTimer;
|
|
95
|
+
let postCaller;
|
|
96
|
+
|
|
97
|
+
const listener = TokenRefreshListener.listenToPersist(projectUrl, (...args) => {
|
|
98
|
+
const newTruthy = !!Scoped.AuthJWTToken[projectUrl];
|
|
99
|
+
|
|
100
|
+
if (resetDelayTimer !== undefined) {
|
|
101
|
+
if (newTruthy) {
|
|
102
|
+
clearTimeout(resetDelayTimer);
|
|
103
|
+
resetDelayTimer = undefined;
|
|
104
|
+
postCaller = undefined;
|
|
105
|
+
callback?.(...args);
|
|
106
|
+
} else {
|
|
107
|
+
postCaller = () => callback?.(...args);
|
|
108
|
+
}
|
|
109
|
+
} else if (typeof lastToken === 'boolean' && !newTruthy && lastToken) {
|
|
110
|
+
callback?.(false);
|
|
111
|
+
resetDelayTimer = setTimeout(() => {
|
|
112
|
+
resetDelayTimer = undefined;
|
|
113
|
+
if (postCaller) {
|
|
114
|
+
postCaller();
|
|
115
|
+
postCaller = undefined;
|
|
116
|
+
} else callback?.(...args);
|
|
117
|
+
}, 3000);
|
|
118
|
+
} else callback?.(...args);
|
|
119
|
+
|
|
120
|
+
lastToken = newTruthy;
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return () => {
|
|
124
|
+
postCaller = undefined;
|
|
125
|
+
listener?.();
|
|
126
|
+
clearTimeout(resetDelayTimer);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
93
129
|
|
|
94
130
|
export const initTokenRefresher = async ({ config, forceRefresh, justCheck }) => {
|
|
95
131
|
const { projectUrl, maxRetries } = config;
|
|
@@ -168,7 +204,7 @@ export const getEmulatedLinks = (projectUrl) =>
|
|
|
168
204
|
.filter(([_, v]) => v === projectUrl)
|
|
169
205
|
.map(v => v[0]);
|
|
170
206
|
|
|
171
|
-
const refreshToken = (builder, remainRetries =
|
|
207
|
+
const refreshToken = (builder, remainRetries = 3) =>
|
|
172
208
|
new Promise(async (resolve, reject) => {
|
|
173
209
|
const { projectUrl, serverE2E_PublicKey, uglify, extraHeaders } = builder;
|
|
174
210
|
|
|
@@ -206,7 +242,7 @@ const refreshToken = (builder, remainRetries = 1, isForceRefresh) =>
|
|
|
206
242
|
Scoped.AuthJWTToken[v] = f.result.token;
|
|
207
243
|
|
|
208
244
|
triggerAuthToken(v, isInit);
|
|
209
|
-
if (
|
|
245
|
+
if (isInit) Scoped.InitiatedForcedToken[v] = true;
|
|
210
246
|
});
|
|
211
247
|
updateCacheStore(['AuthStore']);
|
|
212
248
|
initTokenRefresher({ config: builder });
|
|
@@ -222,7 +258,7 @@ const refreshToken = (builder, remainRetries = 1, isForceRefresh) =>
|
|
|
222
258
|
console.error(`refreshToken retry limit exceeded err:`, e);
|
|
223
259
|
} else {
|
|
224
260
|
awaitReachableServer(projectUrl, true).then(() => {
|
|
225
|
-
refreshToken(builder, remainRetries - 1
|
|
261
|
+
refreshToken(builder, remainRetries - 1).then(resolve, reject);
|
|
226
262
|
});
|
|
227
263
|
}
|
|
228
264
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { io } from "socket.io-client";
|
|
2
2
|
import EngineApi from "../../helpers/engine_api";
|
|
3
|
-
import { TokenRefreshListener } from "../../helpers/listeners";
|
|
4
3
|
import { awaitReachableServer, awaitStore, buildFetchInterface, buildFetchResult, updateCacheStore } from "../../helpers/utils";
|
|
5
4
|
import { CacheStore, Scoped } from "../../helpers/variables";
|
|
6
5
|
import { awaitRefreshToken, getEmulatedLinks, initTokenRefresher, injectEmulatedAuth, injectFreshToken, listenToken, parseToken, triggerAuthToken } from "./accessor";
|
|
@@ -243,7 +242,6 @@ const purgeCache = (url, isMain) => {
|
|
|
243
242
|
].forEach(e => {
|
|
244
243
|
if (e && CacheStore[e]?.[url]) delete CacheStore[e][url];
|
|
245
244
|
});
|
|
246
|
-
TokenRefreshListener.dispatchPersist(url);
|
|
247
245
|
triggerAuthToken(url);
|
|
248
246
|
};
|
|
249
247
|
|
|
@@ -254,9 +252,7 @@ const clearCacheForSignout = (builder, disposeEmulated) => {
|
|
|
254
252
|
if (disposeEmulated) getEmulatedLinks(projectUrl).forEach(e => purgeCache(e));
|
|
255
253
|
|
|
256
254
|
clearInterval(Scoped.TokenRefreshTimer[projectUrl]);
|
|
257
|
-
|
|
258
|
-
initTokenRefresher({ config: builder });
|
|
259
|
-
}, 600);
|
|
255
|
+
initTokenRefresher({ config: builder });
|
|
260
256
|
};
|
|
261
257
|
|
|
262
258
|
export const doSignOut = async (builder) => {
|