@gudhub/core 1.2.0 → 1.2.2

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.
@@ -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() {
@@ -1,4 +1,4 @@
1
- import { ChunkAPI } from "./ChunkApi.js";
1
+ import { ChunkAPI } from "../api/ChunkApi.js";
2
2
 
3
3
 
4
4
  export class ChunksManager {
@@ -21,8 +21,8 @@ export class IndexedDBChunkCacheService extends ChunkCachedDataService {
21
21
  }
22
22
 
23
23
 
24
- OBJECT_STORE = "v2";
25
- DatabaseName = "my-cache-db";
24
+ OBJECT_STORE = "chunks";
25
+ DatabaseName = "gudhubapi-data";
26
26
  DatabaseVersion = 1;
27
27
 
28
28
 
@@ -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 { ChunkDataService } from "../ChunkDataService.js"; // removed "ChunkCacheDataService" from imported because it was causing an error "{ ChunkCacheDataService } not found in exported names" in "gudhub-node-server"
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 { ChunkDataService } from "../ChunkDataService.js"; // removed "ChunkCacheDataService" from imported because it was causing an error "{ ChunkCacheDataService } not found in exported names" in "gudhub-node-server"
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 {}
@@ -0,0 +1,34 @@
1
+
2
+ export let objectAssignWithoutOverride = (target, ...sourceList) => {
3
+ for (let source of sourceList) {
4
+ if (!source) continue;
5
+ for(let key of Object.keys(source)) {//this doesnt iterate over class like source properties
6
+ if (!(key in target)) {//checks own and inherited keys, works fine also for class prototypes and instances
7
+ const desc = Object.getOwnPropertyDescriptor(source, key);
8
+ if (desc.enumerable) Object.defineProperty(target, key, desc);
9
+ }
10
+ }
11
+ }
12
+ };
13
+
14
+ export let objectAssignWithOverride = (target, ...sourceList) => {
15
+ for (let source of sourceList) {
16
+ if (!source) continue;
17
+ for(let key of Object.keys(source)) {//this doesnt iterate over class like source properties
18
+ const desc = Object.getOwnPropertyDescriptor(source, key);
19
+ if (desc.enumerable) Object.defineProperty(target, key, desc);
20
+ }
21
+ }
22
+ };
23
+
24
+
25
+
26
+ export let checkInstanceModifier = (target, ...sourceList) => {
27
+ for (let source of sourceList) {
28
+ if (!source) continue;
29
+ for(let key of Object.keys(source)) {//this doesnt iterate over class like source properties
30
+ const desc = Object.getOwnPropertyDescriptor(source, key);
31
+ if (desc.enumerable) Object.defineProperty(target, key, desc);
32
+ }
33
+ }
34
+ };
@@ -0,0 +1,14 @@
1
+ export class AppAPI {
2
+ constructor(req) {
3
+ this.req = req;
4
+ }
5
+
6
+ async getApp(app_id) {
7
+ return this.req.get({
8
+ url: `${this.req.root}/api/app/get`,
9
+ params: {
10
+ app_id: app_id,
11
+ },
12
+ });
13
+ }
14
+ }
@@ -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
+ }
package/GUDHUB/config.js CHANGED
@@ -8,5 +8,8 @@ export const async_modules_path = 'build/latest/async_modules_node/';
8
8
  export const automation_modules_path = 'build/latest/automation_modules/'
9
9
  export const file_server_url = 'https://gudhub.com'
10
10
 
11
+ export const cache_chunk_requests = true;
12
+ export const cache_app_requests = true;
13
+
11
14
  // FOR TESTS
12
15
  export const port = 9000;
package/GUDHUB/gudhub.js CHANGED
@@ -18,7 +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
+ import { AppDataService, ChunkDataService, appDataServiceConf, chunkDataServiceConf } from "./DataService/export.js";
22
22
 
23
23
  export class GudHub {
24
24
  constructor(
@@ -64,7 +64,7 @@ export class GudHub {
64
64
  this.pipeService,
65
65
  this.req,
66
66
  this.util,
67
- new DataService(this.req),
67
+ new ChunkDataService(this.req, chunkDataServiceConf),
68
68
  );
69
69
  this.appProcessor = new AppProcessor(
70
70
  this.storage,
@@ -73,7 +73,8 @@ export class GudHub {
73
73
  this.ws,
74
74
  this.chunksManager,
75
75
  this.util,
76
- options.activateSW
76
+ options.activateSW,
77
+ new AppDataService(this.req, appDataServiceConf),
77
78
  );
78
79
  this.itemProcessor = new ItemProcessor(
79
80
  this.storage,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gudhub/core",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {