mosquito-transport-js 0.2.4 → 0.2.6

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,3 +1,4 @@
1
1
  - fix local cache query on sqlite and fs
2
2
  - fix and add all mongodb query and update operator
3
- - reauthenticate
3
+ - reauthenticate
4
+ - change `Object` in d.ts to [key: string]: any
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mosquito-transport-js",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
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,8 +33,13 @@
33
33
  "@types/lodash": "^4.14.194",
34
34
  "buffer": "^6.0.3",
35
35
  "crypto-js": "^4.2.0",
36
+ "fast-json-stable-stringify": "^2.1.0",
37
+ "guard-object": "^1.0.6",
36
38
  "json-buffer": "^3.0.1",
37
- "lodash": "^4.17.21",
39
+ "lodash.get": "^4.4.2",
40
+ "lodash.isequal": "^4.5.0",
41
+ "lodash.set": "^4.3.2",
42
+ "lodash.unset": "^4.5.2",
38
43
  "set-large-timeout": "^1.0.1",
39
44
  "socket.io-client": "^4.6.2",
40
45
  "subscription-listener": "^1.1.2",
@@ -98,9 +98,12 @@ export const cloneInstance = (v) => {
98
98
  return v;
99
99
  }
100
100
 
101
- export const listenReachableServer = (callback, projectUrl) => ServerReachableListener.listenTo(projectUrl, t => {
102
- if (typeof t === 'boolean') callback?.(t);
103
- }, true);
101
+ export const listenReachableServer = (callback, projectUrl) => {
102
+ let lastValue;
103
+ return ServerReachableListener.listenTo(projectUrl, t => {
104
+ if (typeof t === 'boolean' && t !== lastValue) callback?.(t);
105
+ }, true);
106
+ }
104
107
 
105
108
  export const prefixStoragePath = (path, prefix = 'file:///') => {
106
109
  if (!path) return path;
@@ -20,7 +20,7 @@ export const updateCacheStore = () => {
20
20
  );
21
21
 
22
22
  if (io) {
23
- io.ouput(txt);
23
+ io.output(txt);
24
24
  } else if (cacheProtocol === CACHE_PROTOCOL.LOCAL_STORAGE) {
25
25
  window.localStorage.setItem(CACHE_STORAGE_PATH, txt);
26
26
  }
@@ -63,4 +63,13 @@ export const READ_OPS_LIST = Object.values(READ_OPS);
63
63
 
64
64
  export const Regexs = {
65
65
  LINK: () => /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig
66
- }
66
+ };
67
+
68
+ export const AUTH_PROVIDER_ID = {
69
+ GOOGLE: 'google.com',
70
+ FACEBOOK: 'facebook.com',
71
+ PASSWORD: 'password',
72
+ TWITTER: 'x.com',
73
+ GITHUB: 'github.com',
74
+ APPLE: 'apple.com'
75
+ };
package/src/index.d.ts CHANGED
@@ -25,6 +25,43 @@ interface mtimestamp { $timestamp: 'now' }
25
25
  export const TIMESTAMP: mtimestamp;
26
26
  export function DOCUMENT_EXTRACTION(path: string): { $dynamicValue: number };
27
27
 
28
+ type longitude = number;
29
+ type latitude = number;
30
+
31
+ export function GEO_JSON(latitude: latitude, longitude: longitude): {
32
+ type: "Point",
33
+ coordinates: [longitude, latitude],
34
+ };
35
+
36
+ export function FIND_GEO_JSON(coordinates: [latitude, longitude], offSetMeters: number, centerMeters?: number): {
37
+ $near: {
38
+ $geometry: {
39
+ type: "Point",
40
+ coordinates: [longitude, latitude]
41
+ },
42
+ $minDistance: number | 0,
43
+ $maxDistance: number
44
+ }
45
+ };
46
+
47
+ export const AUTH_PROVIDER_ID: auth_provider_id;
48
+
49
+ interface auth_provider_id {
50
+ GOOGLE: 'google.com';
51
+ FACEBOOK: 'facebook.com';
52
+ PASSWORD: 'password';
53
+ TWITTER: 'x.com';
54
+ GITHUB: 'github.com';
55
+ APPLE: 'apple.com';
56
+ }
57
+
58
+ type auth_provider_id_values = auth_provider_id['GOOGLE'] |
59
+ auth_provider_id['FACEBOOK'] |
60
+ auth_provider_id['PASSWORD'] |
61
+ auth_provider_id['GITHUB'] |
62
+ auth_provider_id['TWITTER'] |
63
+ auth_provider_id['APPLE'];
64
+
28
65
  interface ReleaseCacheOption_IO {
29
66
  /**
30
67
  * This password will be used to encrypt data stored locally
@@ -342,8 +379,8 @@ interface SignupResult extends SigninResult {
342
379
  interface AuthData {
343
380
  email?: string;
344
381
  metadata: Object;
345
- signupMethod: 'google' | 'apple' | 'custom' | 'github' | 'twitter' | 'facebook' | string;
346
- currentAuthMethod: 'google' | 'apple' | 'custom' | 'github' | 'twitter' | 'facebook' | string;
382
+ signupMethod: auth_provider_id_values;
383
+ currentAuthMethod: auth_provider_id_values;
347
384
  joinedOn: number;
348
385
  uid: string;
349
386
  claims: Object;
package/src/index.js CHANGED
@@ -5,12 +5,12 @@ 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, listenTokenReady, triggerAuthToken } from "./products/auth/accessor";
8
+ import { initTokenRefresher, listenToken, listenTokenReady, parseToken, 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";
12
12
  import { validateCollectionPath } from "./products/database/validator";
13
- import { CACHE_PROTOCOL, Regexs } from "./helpers/values";
13
+ import { AUTH_PROVIDER_ID, CACHE_PROTOCOL, Regexs } from "./helpers/values";
14
14
  import { trySendPendingWrite } from "./products/database/accessor";
15
15
  import EngineApi from './helpers/EngineApi';
16
16
  import { parse, stringify } from 'json-buffer';
@@ -47,21 +47,42 @@ export class MosquitoTransport {
47
47
  triggerAuthToken(projectUrl);
48
48
  initTokenRefresher({ ...this.config }, true);
49
49
 
50
- const socket = io(`${this.config.wsPrefix}://${projectUrl.split('://')[1]}`, {
51
- transports: ['websocket', 'polling', 'flashsocket'],
52
- auth: { _m_internal: true, _from_base: true }
53
- });
50
+ const disconnectionGlich = {};
51
+ let socket, lastUid, lastSocketProcess = 0, hasInited;
54
52
 
55
- socket.on('_signal_signout', () => {
56
- this.auth().signOut();
57
- });
53
+ listenToken((token, thisInited) => {
54
+ const user = token && parseToken(token);
58
55
 
59
- socket.on('connect', () => {
60
- ServerReachableListener.dispatch(projectUrl, true);
61
- });
62
- socket.on('disconnect', () => {
63
- ServerReachableListener.dispatch(projectUrl, false);
64
- });
56
+ const thisUid = (user?.uid || null);
57
+ if (lastUid === thisUid && (hasInited || !thisInited)) return;
58
+ if (!hasInited) hasInited = thisInited;
59
+ lastUid = thisUid;
60
+
61
+ if (lastSocketProcess) disconnectionGlich[lastSocketProcess] = true;
62
+ if (socket) socket.close();
63
+
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
+ });
73
+
74
+ socket.on('_signal_signout', () => {
75
+ this.auth().signOut();
76
+ });
77
+
78
+ socket.on('connect', () => {
79
+ ServerReachableListener.dispatch(projectUrl, true);
80
+ });
81
+ socket.on('disconnect', () => {
82
+ if (!disconnectionGlich[thisProcess])
83
+ ServerReachableListener.dispatch(projectUrl, false);
84
+ });
85
+ }, projectUrl);
65
86
 
66
87
  listenReachableServer(c => {
67
88
  Scoped.IS_CONNECTED[projectUrl] = c;
@@ -379,5 +400,6 @@ export {
379
400
  TIMESTAMP,
380
401
  DOCUMENT_EXTRACTION,
381
402
  FIND_GEO_JSON,
382
- GEO_JSON
403
+ GEO_JSON,
404
+ AUTH_PROVIDER_ID
383
405
  };
@@ -119,6 +119,9 @@ export class MTAuth {
119
119
  }, this.builder.projectUrl);
120
120
  });
121
121
 
122
+ /**
123
+ * @type {import('../../index').RNMTAuth['listenAuth']}
124
+ */
122
125
  listenAuth = (callback) => {
123
126
  let lastTrig;
124
127
 
@@ -2,10 +2,10 @@ import { IS_RAW_OBJECT, objToUniqueString, queryEntries, shuffleArray, sortArray
2
2
  import { awaitStore, updateCacheStore } from "../../helpers/utils";
3
3
  import { CacheStore } from "../../helpers/variables";
4
4
  import { confirmFilterDoc } from "./validator";
5
- import getLodash from 'lodash/get';
6
- import setLodash from 'lodash/set';
7
- import unsetLodash from 'lodash/unset';
8
- import isEqual from 'lodash/isEqual';
5
+ import getLodash from 'lodash.get';
6
+ import setLodash from 'lodash.set';
7
+ import unsetLodash from 'lodash.unset';
8
+ import isEqual from 'lodash.isequal';
9
9
  import { DEFAULT_DB_NAME, DEFAULT_DB_URL, DELIVERY, RETRIEVAL, WRITE_OPS, WRITE_OPS_LIST } from "../../helpers/values";
10
10
  import { DatabaseRecordsListener } from "../../helpers/listeners";
11
11
 
@@ -8,7 +8,7 @@ import { addPendingWrites, generateRecordID, getRecord, insertRecord, listenQuer
8
8
  import { validateCollectionPath, validateFilter, validateReadConfig, validateWriteValue } from "./validator";
9
9
  import { awaitRefreshToken, listenToken } from "../auth/accessor";
10
10
  import { DEFAULT_DB_NAME, DEFAULT_DB_URL, DELIVERY, RETRIEVAL } from "../../helpers/values";
11
- import setLodash from 'lodash/set';
11
+ import setLodash from 'lodash.set';
12
12
 
13
13
  export class MTCollection {
14
14
  constructor(config) {
@@ -1,7 +1,7 @@
1
1
  import { IS_DECIMAL_NUMBER, IS_RAW_OBJECT, IS_WHOLE_NUMBER, queryEntries } from "../../helpers/peripherals";
2
2
  import { READ_OPS, READ_OPS_LIST, RETRIEVAL } from "../../helpers/values";
3
- import getLodash from 'lodash/get';
4
- import isEqual from 'lodash/isEqual';
3
+ import getLodash from 'lodash.get';
4
+ import isEqual from 'lodash.isequal';
5
5
 
6
6
  const dirn = ['desc', 'asc', 'ascending', 'descending'];
7
7