Работа над RemoteLib
This commit is contained in:
56
server/core/RemoteLib.js
Normal file
56
server/core/RemoteLib.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const fs = require('fs-extra');
|
||||
const utils = require('./utils');
|
||||
|
||||
const WebSocketConnection = require('./WebSocketConnection');
|
||||
|
||||
//singleton
|
||||
let instance = null;
|
||||
|
||||
class RemoteLib {
|
||||
constructor(config) {
|
||||
if (!instance) {
|
||||
this.config = config;
|
||||
|
||||
this.wsc = new WebSocketConnection(config.remoteLib.url, 10, 30, {rejectUnauthorized: false});
|
||||
if (config.remoteLib.accessPassword)
|
||||
this.accessToken = utils.getBufHash(config.remoteLib.accessPassword, 'sha256', 'hex');
|
||||
|
||||
this.inpxFile = `${config.tempDir}/${utils.randomHexString(20)}`;
|
||||
this.lastUpdateTime = 0;
|
||||
|
||||
instance = this;
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
async wsRequest(query) {
|
||||
if (this.accessToken)
|
||||
query.accessToken = this.accessToken;
|
||||
|
||||
const response = await this.wsc.message(
|
||||
await this.wsc.send(query, 60),
|
||||
60
|
||||
);
|
||||
|
||||
if (response.error)
|
||||
throw new Error(response.error);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
async getInpxFile(getPeriod = 0) {
|
||||
if (getPeriod && Date.now() - this.lastUpdateTime < getPeriod)
|
||||
return this.inpxFile;
|
||||
|
||||
const response = await this.wsRequest({action: 'get-inpx-file'});
|
||||
|
||||
await fs.writeFile(this.inpxFile, response.data, 'base64');
|
||||
|
||||
this.lastUpdateTime = Date.now();
|
||||
|
||||
return this.inpxFile;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RemoteLib;
|
||||
@@ -5,11 +5,12 @@ const zlib = require('zlib');
|
||||
const _ = require('lodash');
|
||||
|
||||
const ZipReader = require('./ZipReader');
|
||||
const WorkerState = require('./WorkerState');
|
||||
const WorkerState = require('./WorkerState');//singleton
|
||||
const { JembaDbThread } = require('jembadb');
|
||||
const DbCreator = require('./DbCreator');
|
||||
const DbSearcher = require('./DbSearcher');
|
||||
const InpxHashCreator = require('./InpxHashCreator');
|
||||
const RemoteLib = require('./RemoteLib');//singleton
|
||||
|
||||
const ayncExit = new (require('./AsyncExit'))();
|
||||
const log = new (require('./AppLogger'))().log;//singleton
|
||||
@@ -505,6 +506,11 @@ class WebWorker {
|
||||
while (this.myState != ssNormal)
|
||||
await utils.sleep(1000);
|
||||
|
||||
if (this.config.remoteLib) {
|
||||
const remoteLib = new RemoteLib(this.config);
|
||||
await remoteLib.getInpxFile(60*1000);
|
||||
}
|
||||
|
||||
const newInpxHash = await inpxHashCreator.getHash();
|
||||
|
||||
const dbConfig = await this.dbConfig();
|
||||
|
||||
Reference in New Issue
Block a user