@gudhub/core 1.1.90 → 1.2.0

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.
@@ -0,0 +1,31 @@
1
+ {
2
+ "version": "0.2.0",
3
+ "configurations": [
4
+ {
5
+ "name": "Launch Program",
6
+ "program": "${workspaceFolder}/app.js",
7
+ "request": "launch",
8
+ "skipFiles": [
9
+ "<node_internals>/**"
10
+ ],
11
+ "type": "node"
12
+ },
13
+ {
14
+ "type": "node",
15
+ "request": "launch",
16
+ "runtimeVersion": "16",
17
+ "name": "Launch Program",
18
+ "skipFiles": [
19
+ "<node_internals>/**"
20
+ ],
21
+ "program": "${workspaceFolder}/index.js"
22
+ },
23
+ {
24
+ "type": "node-terminal",
25
+ "name": "Run Script: test",
26
+ "request": "launch",
27
+ "command": "npm run test",
28
+ "cwd": "${workspaceFolder}"
29
+ }
30
+ ]
31
+ }
@@ -266,42 +266,29 @@ export class AppProcessor {
266
266
  if (!app_id) return null;
267
267
 
268
268
  let app = this.getAppFromStorage(app_id);
269
- if (!app) {
270
- let receivedApp = await this.getAppApi(app_id);
271
- if (!receivedApp) return null;
272
-
273
- //!!! temporary check !!!! if app has chanks property then we start getting it
274
- if (receivedApp.chunks && receivedApp.chunks.length) {
275
- this.chunksManager
276
- .getChunks(app_id, receivedApp.chunks)
277
- .then((chunks) => {
278
- receivedApp.items_list = this.util.mergeChunks([
279
- ...chunks,
280
- receivedApp,
281
- ]);
282
- this.saveAppInStorage(receivedApp);
283
- this.pipeService.emit(
284
- "gh_items_update",
285
- { app_id },
286
- receivedApp.items_list
287
- );
288
- });
289
- app = receivedApp
290
- this.saveAppInStorage(receivedApp);
291
- } else {
292
- app = this.getAppFromStorage(app_id);
293
- // for oprimization purpose: we check if app was already received in case when there was severals calls at same moment of the getApp one call will overwrite the App from another call with the same App,
294
- if (!app) {
295
- app = receivedApp;
296
- this.saveAppInStorage(receivedApp); //!!! will be saved over several times for each app request
297
- this.ws.addSubscription(app_id);
298
- }
299
- }
269
+ if (app) return app;
270
+
271
+ let receivedApp = await this.getAppApi(app_id);
272
+ if (!receivedApp) return;
273
+
274
+ //!!! temporary check !!!! if app has chanks property then we start getting it
275
+ if (
276
+ receivedApp.chunks &&
277
+ receivedApp.chunks.length
278
+ ) {
279
+ let chunks = await this.chunksManager.getChunks(app_id, receivedApp.chunks);
280
+ receivedApp.items_list = this.util.mergeChunks([
281
+ ...chunks,
282
+ receivedApp,
283
+ ]);
300
284
  }
301
-
285
+
286
+ this.saveAppInStorage(receivedApp);
287
+ this.ws.addSubscription(app_id);
288
+
302
289
  // This code for trash must be changed
303
290
  // return trash ? app : {...app, items_list: app.items_list.filter(item => !item.trash)};
304
- return app;
291
+ return receivedApp;
305
292
  }
306
293
 
307
294
  async updateApp(app) {
@@ -10,6 +10,12 @@ export class Auth {
10
10
  return user;
11
11
  }
12
12
 
13
+ async loginWithToken(token) {
14
+ const user = await this.loginWithTokenApi(token);
15
+ this.storage.updateUser(user);
16
+ return user;
17
+ }
18
+
13
19
  async logout(token) {
14
20
  const response = await this.logoutApi(token);
15
21
  return response;
@@ -58,6 +64,18 @@ export class Auth {
58
64
  }
59
65
  }
60
66
 
67
+ async loginWithTokenApi(token) {
68
+ try {
69
+ const user = await this.req.axiosRequest({
70
+ method: 'POST',
71
+ url: `${this.req.root}/auth/login?accesstoken=${token}`
72
+ });
73
+ return user;
74
+ } catch (error) {
75
+ console.log(error);
76
+ }
77
+ }
78
+
61
79
  async updateTokenApi(auth_key) {
62
80
  try {
63
81
  const user = await this.req.axiosRequest({
@@ -1,4 +1,4 @@
1
- import should from "should";
1
+
2
2
  import {GudHub} from './../gudhub.js';
3
3
 
4
4
  describe("AUTHORIZATION", async function() {
@@ -22,6 +22,16 @@ describe("AUTHORIZATION", async function() {
22
22
  user.fullname.should.equal('Vasya Pupkin');
23
23
  })
24
24
 
25
+ it("Authentication with access token", async function () {
26
+ const gudhub = new GudHub();
27
+
28
+ const token = 'accesstoken';
29
+
30
+ const user = await gudhub.loginWithToken(token);
31
+
32
+ user.hasOwnProperty('fullname').should.equal(true);
33
+ })
34
+
25
35
  it("Signing up user", async function () {
26
36
  const gudhub = new GudHub();
27
37
  let credentials = {
@@ -0,0 +1,19 @@
1
+ export class ChunkAPI {
2
+ constructor(req) {
3
+ this.req = req;
4
+ }
5
+
6
+ async getChunk(app_id, chunk_id) {
7
+ return this.req.get({
8
+ url: `${this.req.root}/api/get-items-chunk/${app_id}/${chunk_id}`,
9
+ method: 'GET'
10
+ });
11
+ }
12
+
13
+ async getLastChunk(app_id) {
14
+ return this.req.axiosRequest({
15
+ url: `${this.req.root}/api/get-last-items-chunk/${app_id}`,
16
+ method: 'GET'
17
+ });
18
+ }
19
+ }
@@ -1,60 +1,48 @@
1
+ import { ChunkAPI } from "./ChunkApi.js";
1
2
 
2
3
 
3
4
  export class ChunksManager {
4
- constructor(storage, pipeService, req, util) {
5
+ constructor(
6
+ storage,
7
+ pipeService,
8
+ req,
9
+ util,
10
+ dataService,
11
+ ) {
5
12
  this.storage = storage;
6
13
  this.pipeService = pipeService;
7
14
  this.req = req;
8
15
  this.util = util;
16
+ this.dataService = dataService;
17
+ this.chunkApi = new ChunkAPI(req);
9
18
  this.itemListeners();
10
19
  }
11
20
 
12
- async getChunkApi(app_id, chunk_id) {
21
+ async getChunk(app_id, chunk_id) {
13
22
  try {
14
- const response = await this.req.axiosRequest({
15
- url: `${this.req.root}/api/get-items-chunk/${app_id}/${chunk_id}`,
16
- method: 'GET'
17
- });
18
- return response;
23
+ return this.dataService.getChunk(app_id, chunk_id);
19
24
  } catch (err) {
20
25
  console.log(err);
21
26
  return null;
22
27
  }
23
28
  }
24
29
 
25
- async getLastChunkApi(app_id) {
30
+ async getLastChunk(app_id) {
26
31
  try {
27
- const response = await this.req.axiosRequest({
28
- url: `${this.req.root}/api/get-last-items-chunk/${app_id}`,
29
- method: 'GET'
30
- });
31
- return response;
32
+ return this.chunkApi.getLastChunk(app_id);
32
33
  } catch (err) {
33
34
  console.log(err);
34
35
  return null;
35
36
  }
36
37
  }
37
38
 
38
-
39
- getChunk(app_id, chunk_id) {
40
- return this.getChunkApi(app_id, chunk_id);
41
- }
42
-
43
-
44
39
  async getChunks(app_id, chunk_ids) {
45
40
  let chunks = [];
46
41
  if(chunk_ids){
47
- chunks = await Promise.all(chunk_ids.map((chunk_id) => this.getChunkApi(app_id, chunk_id)));
42
+ chunks = await Promise.all(chunk_ids.map((chunk_id) => this.getChunk(app_id, chunk_id)));
48
43
  }
49
44
  return chunks;
50
45
  }
51
-
52
-
53
- async getLastChunk(app_id) {
54
- let chunk = await this.getLastChunkApi(app_id);
55
- return chunk;
56
- }
57
-
58
46
 
59
47
  itemListeners() {
60
48
  }
@@ -76,7 +64,5 @@ export class ChunksManager {
76
64
  }
77
65
  }
78
66
  return app;
79
- }
80
-
81
-
82
- }
67
+ }
68
+ }
@@ -0,0 +1,101 @@
1
+ import { ChunkDataService, HttpDataService } from "./httpDataService.js";
2
+
3
+
4
+ //this should be global in project
5
+ // class IndexedDBFacade extends CacheService {
6
+
7
+ // }
8
+
9
+
10
+
11
+ class ChunkCachedDataService extends ChunkDataService {
12
+ dataService;
13
+ }
14
+
15
+
16
+ export class IndexedDBChunkCacheService extends ChunkCachedDataService {
17
+ constructor(req) {
18
+ super();
19
+
20
+ this.dataService = new HttpDataService(req);
21
+ }
22
+
23
+
24
+ OBJECT_STORE = "v2";
25
+ DatabaseName = "my-cache-db";
26
+ DatabaseVersion = 1;
27
+
28
+
29
+ //TODO use IndexedDBFacade here
30
+ // indexDBAccess = new IndexedDBFacade;
31
+
32
+
33
+ async putChunk(id, data) {
34
+ try {
35
+ const db = await this.openDatabase();
36
+
37
+ const transaction = db.transaction(this.OBJECT_STORE, "readwrite");
38
+ const store = transaction.objectStore(this.OBJECT_STORE);
39
+
40
+ store.put(data, id);
41
+ } catch (error) {
42
+
43
+ }
44
+ }
45
+
46
+
47
+ async getChunk(app_id, id) {
48
+ try {
49
+ const db = await this.openDatabase();
50
+ const transaction = db.transaction(this.OBJECT_STORE, "readonly");
51
+ const store = transaction.objectStore(this.OBJECT_STORE);
52
+
53
+ const storeRequest = store.get(id);
54
+
55
+ return await new Promise((resolve, reject) => {
56
+ storeRequest.onsuccess = (e) => {
57
+
58
+ let cachedData = e.target.result;
59
+
60
+ if (
61
+ !cachedData
62
+ ) {
63
+ reject();
64
+ }
65
+
66
+ if (
67
+ cachedData
68
+ ) {
69
+
70
+ resolve(cachedData);
71
+ }
72
+ };
73
+
74
+ storeRequest.onerror = reject
75
+ });
76
+ } catch(e) {
77
+ let res = await this.dataService.getChunk(app_id, id);
78
+
79
+ await this.putChunk(id, res);
80
+
81
+
82
+ return res;
83
+ }
84
+ }
85
+
86
+ async openDatabase() {
87
+ return new Promise((resolve, reject) => {
88
+ const request = indexedDB.open(this.DatabaseName, this.DatabaseVersion);
89
+ request.onupgradeneeded = event => {
90
+ const db = event.target.result;
91
+ db.createObjectStore(this.OBJECT_STORE);
92
+ };
93
+ request.onsuccess = event => {
94
+ resolve(event.target.result);
95
+ };
96
+ request.onerror = event => {
97
+ reject(event.target.error);
98
+ };
99
+ });
100
+ }
101
+ }
@@ -0,0 +1,13 @@
1
+ import { IS_WEB } from "../../consts.js";
2
+ import { IndexedDBChunkCacheService } from "./IndexedDBCacheService.js";
3
+ import { HttpDataService } from "./httpDataService.js";
4
+
5
+ let exportClass;
6
+
7
+ if (IS_WEB) {
8
+ exportClass = IndexedDBChunkCacheService
9
+ } else {
10
+ exportClass = HttpDataService
11
+ }
12
+
13
+ export { exportClass as DataService };
@@ -0,0 +1,21 @@
1
+ import { ChunkAPI } from "../ChunkApi.js";
2
+
3
+ export class ChunkDataService {}
4
+
5
+
6
+ export class HttpDataService extends ChunkDataService {
7
+ constructor(req) {
8
+ super();
9
+
10
+ this.chunkApi = new ChunkAPI(req);
11
+ }
12
+
13
+ async getChunk(app_id, id) {
14
+ return this.chunkApi.getChunk(app_id, id);
15
+ }
16
+
17
+
18
+ putChunk() {
19
+ //do nothing
20
+ }
21
+ }
package/GUDHUB/gudhub.js CHANGED
@@ -18,6 +18,7 @@ import { WebsocketHandler } from './WebSocket/WebsocketHandler.js';
18
18
  import { GroupSharing } from './Utils/sharing/GroupSharing.js';
19
19
  import { Sharing } from './Utils/sharing/Sharing.js';
20
20
  import { WebSocketEmitter } from './WebSocket/WebSocketEmitter.js';
21
+ import { DataService } from "./ChunksManager/DataService/export.js";
21
22
 
22
23
  export class GudHub {
23
24
  constructor(
@@ -31,7 +32,9 @@ export class GudHub {
31
32
  swLink: "",
32
33
  async_modules_path,
33
34
  file_server_url,
34
- automation_modules_path
35
+ automation_modules_path,
36
+ accesstoken: this.accesstoken,
37
+ expirydate: this.expirydate
35
38
  }
36
39
  ) {
37
40
  this.config = options;
@@ -44,7 +47,15 @@ export class GudHub {
44
47
  this.auth = new Auth(this.req, this.storage);
45
48
  this.sharing = new Sharing(this, this.req);
46
49
  this.groupSharing = new GroupSharing(this, this.req, this.pipeService);
47
- if (authKey) this.storage.setUser({ auth_key: authKey });
50
+
51
+ // Login with access token - pass to setUser access token and after it user can make requests with access token
52
+ // This method created for optimize GudHub initialization without auth key
53
+ if (authKey) {
54
+ this.storage.setUser({ auth_key: authKey })
55
+ } else if(options.accesstoken && options.expirydate) {
56
+ this.storage.setUser({accesstoken: options.accesstoken, expirydate: options.expirydate})
57
+ }
58
+
48
59
  this.req.init(this.auth.getToken.bind(this.auth));
49
60
 
50
61
  this.ws = new WebSocketApi(options.wss_url, this.auth);
@@ -52,7 +63,8 @@ export class GudHub {
52
63
  this.storage,
53
64
  this.pipeService,
54
65
  this.req,
55
- this.util
66
+ this.util,
67
+ new DataService(this.req),
56
68
  );
57
69
  this.appProcessor = new AppProcessor(
58
70
  this.storage,
@@ -442,6 +454,10 @@ export class GudHub {
442
454
  return this.auth.login(data);
443
455
  }
444
456
 
457
+ loginWithToken(token) {
458
+ return this.auth.loginWithToken(token);
459
+ }
460
+
445
461
  logout(token) {
446
462
  this.appProcessor.clearAppProcessor();
447
463
  return this.auth.logout(token);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gudhub/core",
3
- "version": "1.1.90",
3
+ "version": "1.2.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {