mosquito-transport-js 0.2.7 → 0.2.8

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/TODO CHANGED
@@ -1,4 +1,6 @@
1
1
  - fix local cache query on sqlite and fs
2
2
  - fix and add all mongodb query and update operator
3
3
  - reauthenticate
4
- - change `Object` in d.ts to [key: string]: any
4
+ - change `Object` in d.ts to [key: string]: any
5
+ - add `getServerTimeOffset` method
6
+ - change null to undefined in `value`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mosquito-transport-js",
3
- "version": "0.2.7",
3
+ "version": "0.2.8",
4
4
  "description": "Javascript web sdk for mosquito-transport (https://github.com/deflexable/mosquito-transport)",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -33,7 +33,7 @@
33
33
  "buffer": "^6.0.3",
34
34
  "crypto-js": "^4.2.0",
35
35
  "fast-json-stable-stringify": "^2.1.0",
36
- "guard-object": "^1.0.7",
36
+ "guard-object": "^1.0.9",
37
37
  "json-buffer": "^3.0.1",
38
38
  "lodash.get": "^4.4.2",
39
39
  "lodash.isequal": "^4.5.0",
@@ -57,4 +57,4 @@
57
57
  "engines": {
58
58
  "node": ">=14"
59
59
  }
60
- }
60
+ }
package/src/index.js CHANGED
@@ -5,7 +5,7 @@ import { MTAuth } from "./products/auth";
5
5
  import { MTCollection, batchWrite } from "./products/database";
6
6
  import { MTStorage } from "./products/storage";
7
7
  import { ServerReachableListener, TokenRefreshListener } from "./helpers/listeners";
8
- import { initTokenRefresher, listenToken, listenTokenReady, parseToken, triggerAuthToken } from "./products/auth/accessor";
8
+ import { initTokenRefresher, listenToken, listenTokenReady, triggerAuthToken } from "./products/auth/accessor";
9
9
  import { TIMESTAMP, DOCUMENT_EXTRACTION, FIND_GEO_JSON, GEO_JSON } from "./products/database/types";
10
10
  import { mfetch } from "./products/http_callable";
11
11
  import { io } from "socket.io-client";
@@ -47,41 +47,43 @@ export class MosquitoTransport {
47
47
  triggerAuthToken(projectUrl);
48
48
  initTokenRefresher({ ...this.config }, true);
49
49
 
50
- const disconnectionGlich = {};
51
- let socket, lastUid, lastSocketProcess = 0, hasInited;
50
+ let isConnected, lastSentToken, queuedToken;
52
51
 
53
- listenToken((token, thisInited) => {
54
- const user = token && parseToken(token);
52
+ const socket = io(`${this.config.wsPrefix}://${projectUrl.split('://')[1]}`, {
53
+ transports: ['websocket', 'polling', 'flashsocket'],
54
+ auth: {
55
+ _m_internal: true,
56
+ _from_base: true
57
+ }
58
+ });
55
59
 
56
- const thisUid = (user?.uid || null);
57
- if (lastUid === thisUid && (hasInited || !thisInited)) return;
58
- if (!hasInited) hasInited = thisInited;
59
- lastUid = thisUid;
60
+ socket.on('_signal_signout', () => {
61
+ this.auth().signOut();
62
+ });
60
63
 
61
- if (lastSocketProcess) disconnectionGlich[lastSocketProcess] = true;
62
- if (socket) socket.close();
64
+ socket.on('connect', () => {
65
+ isConnected = true;
66
+ if (queuedToken) updateMountedToken(queuedToken.token);
67
+ ServerReachableListener.dispatch(projectUrl, true);
68
+ });
63
69
 
64
- const thisProcess = ++lastSocketProcess;
65
- socket = io(`${this.config.wsPrefix}://${projectUrl.split('://')[1]}`, {
66
- transports: ['websocket', 'polling', 'flashsocket'],
67
- auth: {
68
- _m_internal: true,
69
- _from_base: true,
70
- atoken: token
71
- }
72
- });
70
+ socket.on('disconnect', () => {
71
+ isConnected = false;
72
+ ServerReachableListener.dispatch(projectUrl, false);
73
+ });
73
74
 
74
- socket.on('_signal_signout', () => {
75
- this.auth().signOut();
76
- });
75
+ const updateMountedToken = (token) => {
76
+ if ((lastSentToken || null) !== (token || null)) {
77
+ socket.emit('_update_mounted_user', token || null);
78
+ lastSentToken = token;
79
+ }
80
+ queuedToken = undefined;
81
+ }
77
82
 
78
- socket.on('connect', () => {
79
- ServerReachableListener.dispatch(projectUrl, true);
80
- });
81
- socket.on('disconnect', () => {
82
- if (!disconnectionGlich[thisProcess])
83
- ServerReachableListener.dispatch(projectUrl, false);
84
- });
83
+ listenToken(token => {
84
+ if (isConnected) {
85
+ updateMountedToken(token);
86
+ } else queuedToken = { token };
85
87
  }, projectUrl);
86
88
 
87
89
  listenReachableServer(c => {
@@ -69,8 +69,8 @@ export class MTCollection {
69
69
  updateMany: (find, value) => initOnDisconnectionTask({ ...this.builder, command: { find } }, value, 'updateMany'),
70
70
  mergeOne: (find, value) => initOnDisconnectionTask({ ...this.builder, command: { find } }, value, 'mergeOne'),
71
71
  mergeMany: (find, value) => initOnDisconnectionTask({ ...this.builder, command: { find } }, value, 'mergeMany'),
72
- deleteOne: (find) => initOnDisconnectionTask({ ...this.builder, command: { find } }, null, 'deleteOne'),
73
- deleteMany: (find) => initOnDisconnectionTask({ ...this.builder, command: { find } }, null, 'deleteMany'),
72
+ deleteOne: (find) => initOnDisconnectionTask({ ...this.builder, command: { find } }, undefined, 'deleteOne'),
73
+ deleteMany: (find) => initOnDisconnectionTask({ ...this.builder, command: { find } }, undefined, 'deleteMany'),
74
74
  replaceOne: (find, value) => initOnDisconnectionTask({ ...this.builder, command: { find } }, value, 'replaceOne'),
75
75
  putOne: (find, value) => initOnDisconnectionTask({ ...this.builder, command: { find } }, value, 'putOne')
76
76
  })
@@ -91,9 +91,9 @@ export class MTCollection {
91
91
 
92
92
  putOne = (find, value, config) => commitData({ ...this.builder, find }, value, 'putOne', config);
93
93
 
94
- deleteOne = (find, config) => commitData({ ...this.builder, find }, null, 'deleteOne', config);
94
+ deleteOne = (find, config) => commitData({ ...this.builder, find }, undefined, 'deleteOne', config);
95
95
 
96
- deleteMany = (find, config) => commitData({ ...this.builder, find }, null, 'deleteMany', config);
96
+ deleteMany = (find, config) => commitData({ ...this.builder, find }, undefined, 'deleteMany', config);
97
97
  }
98
98
 
99
99
  export const batchWrite = (builder, map, config) => commitData({ ...builder }, map, 'batchWrite', config);