mosquito-transport-js 0.6.2 → 0.6.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mosquito-transport-js",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "description": "Javascript web sdk for mosquito-transport (https://github.com/brainbehindx/mosquito-transport)",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -179,18 +179,13 @@ export const awaitStore = () => new Promise(resolve => {
179
179
 
180
180
  export const checkAreYouOk = (projectUrl) => {
181
181
  if (!Scoped.AreYouOkPromise[projectUrl]) {
182
- const signal = new AbortController();
183
- const timer = setTimeout(() => {
184
- signal.abort();
185
- }, 9000);
186
- const promise = fetch(engine_api._areYouOk(projectUrl), { credentials: 'omit', signal })
182
+ const promise = fetch(engine_api._areYouOk(projectUrl), { credentials: 'omit' })
187
183
  .then(async r => (await r.json()).status === 'yes')
188
184
  .catch(() => false)
189
185
  .then(async connected => {
190
186
  Scoped.IS_CONNECTED[projectUrl] = connected;
191
187
  ServerReachableListener.dispatchPersist(projectUrl, connected);
192
188
 
193
- clearTimeout(timer);
194
189
  delete Scoped.AreYouOkPromise[projectUrl];
195
190
  return connected;
196
191
  });
package/src/index.js CHANGED
@@ -73,7 +73,6 @@ export class MosquitoTransport {
73
73
  });
74
74
 
75
75
  let connectionIte = 0;
76
- let chainedPromise;
77
76
  const setConnected = c => {
78
77
  isConnected = c;
79
78
  Scoped.IS_CONNECTED[projectUrl] = isConnected;
@@ -90,11 +89,9 @@ export class MosquitoTransport {
90
89
  };
91
90
 
92
91
  const manualCheckConnection = () => {
93
- if (chainedPromise) return;
94
92
  const ref = ++connectionIte;
95
93
 
96
94
  checkAreYouOk(projectUrl).then(ok => {
97
- chainedPromise = undefined;
98
95
  if (ref !== connectionIte) return;
99
96
  if (ok) {
100
97
  onConnect();
@@ -2,7 +2,7 @@ import { doSignOut, revokeAuthIntance } from "./index.js";
2
2
  import EngineApi from "../../helpers/engine_api";
3
3
  import { AuthTokenListener, TokenRefreshListener } from "../../helpers/listeners";
4
4
  import { decodeBinary, deserializeE2E } from "../../helpers/peripherals";
5
- import { awaitReachableServer, awaitStore, buildFetchInterface, buildFetchResult, updateCacheStore } from "../../helpers/utils";
5
+ import { awaitReachableServer, awaitStore, buildFetchInterface, buildFetchResult, getReachableServer, updateCacheStore } from "../../helpers/utils";
6
6
  import { CacheStore, Scoped } from "../../helpers/variables";
7
7
  import { simplifyError } from "simplify-error";
8
8
  import { Validator } from "guard-object";
@@ -75,6 +75,19 @@ export const awaitRefreshToken = (projectUrl) =>
75
75
  }
76
76
  });
77
77
 
78
+ export const ensureActiveToken = async (projectUrl) => {
79
+ if (await getReachableServer(projectUrl)) {
80
+ await awaitRefreshToken(projectUrl);
81
+ } else {
82
+ const emulatedURL = CacheStore.EmulatedAuth[projectUrl];
83
+ const { token } = CacheStore.AuthStore[emulatedURL || projectUrl] || {};
84
+
85
+ if (token && await hasTokenExpire(emulatedURL || projectUrl)) {
86
+ throw 'unable to refreshed expired token because of unreachable internet connection';
87
+ }
88
+ }
89
+ }
90
+
78
91
  export const listenTokenReady = (callback, projectUrl) => TokenRefreshListener.listenToPersist(projectUrl, callback);
79
92
 
80
93
  export const initTokenRefresher = async ({ config, forceRefresh, justCheck }) => {
@@ -54,11 +54,13 @@ export default class MTAuth {
54
54
  const processID = ++lastInitRef;
55
55
  await awaitRefreshToken(projectUrl);
56
56
 
57
+ if (processID !== lastInitRef || hasCancelled) return;
58
+
57
59
  if (!Scoped.AuthJWTToken[projectUrl]) {
58
60
  onError?.(simplifyError('user_login_required', 'You must be signed-in to use this method').simpleError);
59
61
  return;
60
62
  }
61
- if (processID !== lastInitRef || hasCancelled) return;
63
+
62
64
  const mtoken = Scoped.AuthJWTToken[projectUrl],
63
65
  [reqBuilder, [privateKey]] = uglify ? await serializeE2E({ mtoken }, undefined, serverE2E_PublicKey) : [null, []];
64
66
 
@@ -251,6 +253,7 @@ const clearCacheForSignout = (builder, disposeEmulated) => {
251
253
  purgeCache(projectUrl, true);
252
254
  if (disposeEmulated) getEmulatedLinks(projectUrl).forEach(e => purgeCache(e));
253
255
 
256
+ clearInterval(Scoped.TokenRefreshTimer[projectUrl]);
254
257
  setTimeout(() => {
255
258
  initTokenRefresher({ config: builder });
256
259
  }, 600);
@@ -6,7 +6,7 @@ import { awaitReachableServer, awaitStore, buildFetchInterface, buildFetchResult
6
6
  import { CacheStore, Scoped } from "../../helpers/variables";
7
7
  import { addPendingWrites, generateRecordID, getCountQuery, getRecord, insertCountQuery, insertRecord, listenQueryEntry, removePendingWrite, validateWriteValue } from "./accessor";
8
8
  import { validateCollectionName, validateFilter, validateFindConfig, validateFindObject, validateListenFindConfig } from "./validator";
9
- import { awaitRefreshToken, listenTokenReady } from "../auth/accessor";
9
+ import { awaitRefreshToken, ensureActiveToken, listenTokenReady } from "../auth/accessor";
10
10
  import { DELIVERY, RETRIEVAL } from "../../helpers/values";
11
11
  import { ObjectId } from "bson";
12
12
  import { guardObject, Validator } from "guard-object";
@@ -525,8 +525,7 @@ const countCollection = async (builder, config) => {
525
525
  };
526
526
 
527
527
  try {
528
- if (!disableAuth && await getReachableServer(projectUrl))
529
- await awaitRefreshToken(projectUrl);
528
+ if (!disableAuth) await ensureActiveToken(projectUrl);
530
529
 
531
530
  const [reqBuilder, [privateKey]] = await buildFetchInterface({
532
531
  body: {
@@ -675,8 +674,7 @@ const findObject = async (builder, initConfig) => {
675
674
  }
676
675
  }
677
676
 
678
- if (!disableAuth && await getReachableServer(projectUrl))
679
- await awaitRefreshToken(projectUrl);
677
+ if (!disableAuth) await ensureActiveToken(projectUrl);
680
678
 
681
679
  const [reqBuilder, [privateKey]] = await buildFetchInterface({
682
680
  body: {
@@ -838,8 +836,7 @@ const commitData = async (builder, value, type, config) => {
838
836
  };
839
837
 
840
838
  try {
841
- if (!disableAuth && await getReachableServer(projectUrl))
842
- await awaitRefreshToken(projectUrl);
839
+ if (!disableAuth) await ensureActiveToken(projectUrl);
843
840
 
844
841
  const [reqBuilder, [privateKey]] = await buildFetchInterface({
845
842
  body: {
@@ -1,9 +1,9 @@
1
1
  import { Buffer } from "buffer";
2
2
  import { deserializeE2E, niceHash, normalizeRoute, serializeE2E } from "../../helpers/peripherals";
3
- import { awaitReachableServer, awaitStore, getReachableServer } from "../../helpers/utils";
3
+ import { awaitReachableServer, awaitStore } from "../../helpers/utils";
4
4
  import { RETRIEVAL } from "../../helpers/values";
5
5
  import { Scoped } from "../../helpers/variables";
6
- import { awaitRefreshToken, parseToken } from "../auth/accessor";
6
+ import { ensureActiveToken, parseToken } from "../auth/accessor";
7
7
  import { simplifyCaughtError } from "simplify-error";
8
8
  import { guardObject, Validator } from "guard-object";
9
9
  import { serialize } from "entity-serializer";
@@ -138,8 +138,7 @@ export const mfetch = async (input = '', init, config) => {
138
138
  }
139
139
  }
140
140
 
141
- if (!disableAuth && await getReachableServer(projectUrl))
142
- await awaitRefreshToken(projectUrl);
141
+ if (!disableAuth) await ensureActiveToken(projectUrl);
143
142
 
144
143
  const mtoken = disableAuth ? undefined : Scoped.AuthJWTToken[projectUrl];
145
144
  const initType = rawHeader['content-type'];