@gudhub/core 1.1.91 → 1.2.1

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
+ }
@@ -8,7 +8,8 @@ export class AppProcessor {
8
8
  websocket,
9
9
  chunksManager,
10
10
  util,
11
- activateSW
11
+ activateSW,
12
+ dataService,
12
13
  ) {
13
14
  this.storage = storage;
14
15
  this.pipeService = pipeService;
@@ -19,6 +20,7 @@ export class AppProcessor {
19
20
  this.chunksManager = chunksManager;
20
21
  this.util = util;
21
22
  this.appListeners();
23
+ this.dataService = dataService;
22
24
  }
23
25
 
24
26
  async createNewAppApi(app) {
@@ -77,18 +79,7 @@ export class AppProcessor {
77
79
  }
78
80
 
79
81
  async getAppApi(app_id) {
80
- try {
81
- const response = await this.req.get({
82
- url: "/api/app/get",
83
- params: {
84
- app_id: app_id,
85
- },
86
- });
87
- return response;
88
- } catch (err) {
89
- console.log(err);
90
- return null;
91
- }
82
+ return this.dataService.getApp(app_id);
92
83
  }
93
84
 
94
85
  getAppListFromStorage() {
@@ -266,42 +257,29 @@ export class AppProcessor {
266
257
  if (!app_id) return null;
267
258
 
268
259
  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
- }
260
+ if (app) return app;
261
+
262
+ let receivedApp = await this.getAppApi(app_id);
263
+ if (!receivedApp) return;
264
+
265
+ //!!! temporary check !!!! if app has chanks property then we start getting it
266
+ if (
267
+ receivedApp.chunks &&
268
+ receivedApp.chunks.length
269
+ ) {
270
+ let chunks = await this.chunksManager.getChunks(app_id, receivedApp.chunks);
271
+ receivedApp.items_list = this.util.mergeChunks([
272
+ ...chunks,
273
+ receivedApp,
274
+ ]);
300
275
  }
301
-
276
+
277
+ this.saveAppInStorage(receivedApp);
278
+ this.ws.addSubscription(app_id);
279
+
302
280
  // This code for trash must be changed
303
281
  // return trash ? app : {...app, items_list: app.items_list.filter(item => !item.trash)};
304
- return app;
282
+ return receivedApp;
305
283
  }
306
284
 
307
285
  async updateApp(app) {
@@ -10,12 +10,6 @@ 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
-
19
13
  async logout(token) {
20
14
  const response = await this.logoutApi(token);
21
15
  return response;
@@ -64,18 +58,6 @@ export class Auth {
64
58
  }
65
59
  }
66
60
 
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
-
79
61
  async updateTokenApi(auth_key) {
80
62
  try {
81
63
  const user = await this.req.axiosRequest({
@@ -1,4 +1,4 @@
1
-
1
+ import should from "should";
2
2
  import {GudHub} from './../gudhub.js';
3
3
 
4
4
  describe("AUTHORIZATION", async function() {
@@ -22,16 +22,6 @@ 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
-
35
25
  it("Signing up user", async function () {
36
26
  const gudhub = new GudHub();
37
27
  let credentials = {
@@ -1,60 +1,48 @@
1
+ import { ChunkAPI } from "../api/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,5 @@
1
+ import { DataService } from "./DataService.js";
2
+
3
+ export class AppDataService extends DataService {
4
+ //interface for indexeddb and http services
5
+ }
@@ -0,0 +1 @@
1
+ //TODO later if Cache API will be used
@@ -0,0 +1,5 @@
1
+ import { DataService } from "./DataService.js";
2
+
3
+ export class ChunkDataService extends DataService {
4
+ //interface for indexeddb and http services
5
+ }
@@ -0,0 +1 @@
1
+ export class DataService {};
@@ -0,0 +1,98 @@
1
+ import { AppDataService } from "../AppDataService.js";
2
+ import { ChunkCacheDataService, ChunkDataService } from "../ChunkDataService.js";
3
+ import { AppHttpService } from "../httpService/AppHttpService.js";
4
+ import { objectAssignWithOverride } from "../utils.js";
5
+ import { IndexedDBService } from "./IndexedDBService.js";
6
+
7
+
8
+ //this should be global in project
9
+ // class IndexedDBFacade extends CacheService {
10
+
11
+ // }
12
+
13
+ export class IndexedDBAppService extends AppDataService {
14
+ constructor(req, conf) {
15
+ super(req, conf);
16
+
17
+ this.dataService = new AppHttpService(req);
18
+
19
+ let indexDBService = new IndexedDBService(conf);
20
+
21
+ objectAssignWithOverride(this, indexDBService);
22
+ }
23
+
24
+
25
+ static [Symbol.hasInstance](instance) {
26
+ if (instance instanceof IndexedDBService) return true;
27
+ if (instance.chunkDataService instanceof IndexedDBAppService) return true;
28
+ return false;
29
+ }
30
+
31
+ //TODO use IndexedDBFacade here
32
+ // indexDBAccess = new IndexedDBFacade;
33
+
34
+
35
+ async getApp(id) {
36
+ let dataServiceRequest = this.dataService.getApp(id);
37
+
38
+ dataServiceRequest.then(data => this.putApp(id, data));
39
+
40
+ try {
41
+ const db = await this.openDatabase();
42
+ const transaction = db.transaction(this.store, "readonly");
43
+ const store = transaction.objectStore(this.store);
44
+
45
+ const storeRequest = store.get(id);
46
+
47
+ return await new Promise((resolve, reject) => {
48
+ storeRequest.onsuccess = (e) => {
49
+
50
+ let cachedData = e.target.result;
51
+
52
+ if (
53
+ !cachedData
54
+ ) {
55
+ reject();
56
+ }
57
+
58
+ if (
59
+ cachedData
60
+ ) {
61
+
62
+ resolve(cachedData);
63
+ }
64
+ };
65
+
66
+ storeRequest.onerror = reject
67
+ });
68
+ } catch(e) {
69
+ return dataServiceRequest;
70
+ }
71
+ }
72
+
73
+ async putApp(id, data) {
74
+ try {
75
+ const db = await this.openDatabase();
76
+
77
+ const transaction = db.transaction(this.store, "readwrite");
78
+ const store = transaction.objectStore(this.store);
79
+
80
+ store.put(data, id);
81
+ } catch (error) {
82
+
83
+ }
84
+ }
85
+
86
+ async openDatabase() {
87
+ return new Promise((resolve, reject) => {
88
+ const request = indexedDB.open(this.dbName, this.dbVersion);
89
+
90
+ request.onsuccess = event => {
91
+ resolve(event.target.result);
92
+ };
93
+ request.onerror = event => {
94
+ reject(event.target.error);
95
+ };
96
+ });
97
+ }
98
+ }
@@ -0,0 +1,110 @@
1
+ import { ChunkCacheDataService, ChunkDataService } from "../ChunkDataService.js";
2
+ import { ChunkHttpService } from "../httpService/ChunkHttpService.js";
3
+ import { objectAssignWithOverride } from "../utils.js";
4
+ import { IndexedDBService } from "./IndexedDBService.js";
5
+
6
+
7
+ //this should be global in project
8
+ // class IndexedDBFacade extends CacheService {
9
+
10
+ // }
11
+
12
+
13
+
14
+ // class ChunkCachedDataService extends ChunkDataService {
15
+ // dataService;
16
+ // }
17
+
18
+
19
+ export class IndexedDBChunkService extends ChunkDataService {
20
+ constructor(req, conf) {
21
+ super(req, conf);
22
+
23
+ this.dataService = new ChunkHttpService(req);
24
+
25
+
26
+ let indexDBService = new IndexedDBService(conf);
27
+
28
+ objectAssignWithOverride(this, indexDBService);
29
+ }
30
+
31
+ //TODO use IndexedDBFacade here
32
+ // indexDBAccess = new IndexedDBFacade;
33
+
34
+
35
+
36
+ static [Symbol.hasInstance](instance) {
37
+ if (instance instanceof IndexedDBService) return true;
38
+ if (instance.chunkDataService instanceof IndexedDBChunkService) return true;
39
+ return false;
40
+ }
41
+
42
+
43
+
44
+
45
+ async putChunk(id, data) {
46
+ try {
47
+ const db = await this.openDatabase();
48
+
49
+ const transaction = db.transaction(this.store, "readwrite");
50
+ const store = transaction.objectStore(this.store);
51
+
52
+ store.put(data, id);
53
+ } catch (error) {
54
+
55
+ }
56
+ }
57
+
58
+
59
+ async getChunk(app_id, id) {
60
+ try {
61
+ const db = await this.openDatabase();
62
+ const transaction = db.transaction(this.store, "readonly");
63
+ const store = transaction.objectStore(this.store);
64
+
65
+ const storeRequest = store.get(id);
66
+
67
+ return await new Promise((resolve, reject) => {
68
+ storeRequest.onsuccess = (e) => {
69
+
70
+ let cachedData = e.target.result;
71
+
72
+ if (
73
+ !cachedData
74
+ ) {
75
+ reject();
76
+ }
77
+
78
+ if (
79
+ cachedData
80
+ ) {
81
+
82
+ resolve(cachedData);
83
+ }
84
+ };
85
+
86
+ storeRequest.onerror = reject
87
+ });
88
+ } catch(e) {
89
+ let res = await this.dataService.getChunk(app_id, id);
90
+
91
+ await this.putChunk(id, res);
92
+
93
+
94
+ return res;
95
+ }
96
+ }
97
+
98
+ async openDatabase() {
99
+ return new Promise((resolve, reject) => {
100
+ const request = indexedDB.open(this.dbName, this.dbVersion);
101
+
102
+ request.onsuccess = event => {
103
+ resolve(event.target.result);
104
+ };
105
+ request.onerror = event => {
106
+ reject(event.target.error);
107
+ };
108
+ });
109
+ }
110
+ }
@@ -0,0 +1,7 @@
1
+ export class IndexedDBService {
2
+ constructor(conf) {
3
+ this.store = conf.store;
4
+ this.dbName = conf.dbName;
5
+ this.dbVersion = conf.dbVersion;
6
+ }
7
+ }
@@ -0,0 +1,7 @@
1
+ import { dbName, dbVersion } from "./consts.js";
2
+
3
+ export const appsConf = {
4
+ dbName,
5
+ dbVersion,
6
+ store: 'apps',
7
+ };
@@ -0,0 +1,7 @@
1
+ import { dbName, dbVersion } from "./consts.js";
2
+
3
+ export const chunksConf = {
4
+ dbName,
5
+ dbVersion,
6
+ store: 'chunks',
7
+ };
@@ -0,0 +1,2 @@
1
+ export const dbName = "gudhub";
2
+ export const dbVersion = 7;
@@ -0,0 +1,21 @@
1
+ import { IS_WEB } from "../../consts.js";
2
+ import { appsConf } from "./appDataConf.js";
3
+ import { chunksConf } from "./chunkDataConf.js";
4
+ import { dbName, dbVersion } from "./consts.js";
5
+
6
+
7
+ if (
8
+ IS_WEB
9
+ ) {
10
+ const request = indexedDB.open(dbName, dbVersion);
11
+
12
+ request.onupgradeneeded = event => {
13
+ const db = event.target.result;
14
+
15
+ for (let store of [appsConf.store, chunksConf.store]) {
16
+ if (!db.objectStoreNames.contains(store)) {
17
+ db.createObjectStore(store);
18
+ }
19
+ }
20
+ };
21
+ }
@@ -0,0 +1,48 @@
1
+ import { cache_app_requests, cache_chunk_requests } from "../config.js";
2
+ import { IS_WEB } from "../consts.js";
3
+ import { IndexedDBAppService } from "./IndexedDB/IndexedDBAppService.js";
4
+ import { IndexedDBChunkService } from "./IndexedDB/IndexedDBChunkService.js";
5
+ import { appsConf } from "./IndexedDB/appDataConf.js";
6
+ import { chunksConf } from "./IndexedDB/chunkDataConf.js";
7
+ import { AppHttpService } from "./httpService/AppHttpService.js";
8
+ import { ChunkHttpService } from "./httpService/ChunkHttpService.js";
9
+
10
+
11
+ import "./IndexedDB/init.js";
12
+
13
+ let AppDataService;
14
+ let ChunkDataService;
15
+
16
+ let appDataServiceConf;
17
+ let chunkDataServiceConf;
18
+
19
+
20
+ if (
21
+ IS_WEB &&
22
+ cache_chunk_requests
23
+ ) {
24
+ ChunkDataService = IndexedDBChunkService;
25
+ chunkDataServiceConf = chunksConf;
26
+
27
+ } else {
28
+ ChunkDataService = ChunkHttpService;
29
+ }
30
+
31
+
32
+ if (
33
+ IS_WEB &&
34
+ cache_app_requests
35
+ ) {
36
+ AppDataService = IndexedDBAppService;
37
+ appDataServiceConf = appsConf;
38
+ } else {
39
+ AppDataService = AppHttpService;
40
+ }
41
+
42
+
43
+ export {
44
+ AppDataService,
45
+ ChunkDataService,
46
+ appDataServiceConf,
47
+ chunkDataServiceConf,
48
+ };
@@ -0,0 +1,31 @@
1
+ import { AppAPI } from "../../api/AppApi.js";
2
+ import { AppDataService } from "../AppDataService.js";
3
+ import { objectAssignWithOverride } from "../utils.js";
4
+ import { HttpService } from "./HttpService.js";
5
+
6
+ export class AppHttpService extends AppDataService {
7
+ constructor(req, conf) {
8
+ super();
9
+
10
+ this.appApi = new AppAPI(req);
11
+
12
+ let indexDBService = new HttpService(conf);
13
+
14
+ objectAssignWithOverride(this, indexDBService);
15
+ }
16
+
17
+
18
+ async getApp(id) {
19
+ return this.appApi.getApp(id);
20
+ }
21
+
22
+ async putApp(id, app) {
23
+
24
+ }
25
+
26
+ static [Symbol.hasInstance](instance) {
27
+ if (instance instanceof AppHttpService) return true;
28
+ if (instance.chunkDataService instanceof HttpService) return true;
29
+ return false;
30
+ }
31
+ }
@@ -0,0 +1,32 @@
1
+ import { ChunkAPI } from "../../api/ChunkApi.js";
2
+ import { ChunkDataService } from "../ChunkDataService.js";
3
+ import { objectAssignWithOverride } from "../utils.js";
4
+ import { HttpService } from "./HttpService.js";
5
+
6
+ export class ChunkHttpService extends ChunkDataService {
7
+ constructor(req, conf) {
8
+ super();
9
+
10
+ this.chunkApi = new ChunkAPI(req);
11
+
12
+ let indexDBService = new HttpService(conf);
13
+
14
+ objectAssignWithOverride(this, indexDBService);
15
+ }
16
+
17
+ async getChunk(app_id, id) {
18
+ return this.chunkApi.getChunk(app_id, id);
19
+ }
20
+
21
+
22
+ putChunk() {
23
+ //do nothing
24
+ }
25
+
26
+
27
+ static [Symbol.hasInstance](instance) {
28
+ if (instance instanceof ChunkHttpService) return true;
29
+ if (instance.chunkDataService instanceof HttpService) return true;
30
+ return false;
31
+ }
32
+ }
@@ -0,0 +1 @@
1
+ export class HttpService {}