@iobroker/db-base
Advanced tools
Comparing version 4.0.0-alpha.25-20211211-1232a951 to 4.0.0-alpha.26-20211214-19e8d05e
@@ -17,5 +17,5 @@ /** | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const tools = require('./tools.js'); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const tools = require('./tools.js'); | ||
@@ -48,3 +48,2 @@ // settings = { | ||
class InMemoryFileDB { | ||
constructor(settings) { | ||
@@ -63,10 +62,10 @@ this.settings = settings || {}; | ||
this.settings.backup = this.settings.backup || { | ||
disabled: false, // deactivates | ||
files: 24, // minimum number of files | ||
hours: 48, // hours | ||
period: 120, // minutes | ||
path: '' // use default path | ||
disabled: false, // deactivates | ||
files: 24, // minimum number of files | ||
hours: 48, // hours | ||
period: 120, // minutes | ||
path: '' // use default path | ||
}; | ||
this.dataDir = (this.settings.connection.dataDir || tools.getDefaultDataDir()); | ||
this.dataDir = this.settings.connection.dataDir || tools.getDefaultDataDir(); | ||
if (!path.isAbsolute(this.dataDir)) { | ||
@@ -83,3 +82,3 @@ this.dataDir = path.normalize(path.join(tools.getControllerDir(), this.dataDir)); | ||
this.backupDir = this.settings.backup.path || (path.join(this.dataDir, this.settings.fileDB.backupDirName)); | ||
this.backupDir = this.settings.backup.path || path.join(this.dataDir, this.settings.fileDB.backupDirName); | ||
@@ -134,5 +133,7 @@ if (!this.settings.backup.disabled) { | ||
try { | ||
await fs.move(datasetName, `${datasetName}.broken`, {overwrite: true}); | ||
await fs.move(datasetName, `${datasetName}.broken`, { overwrite: true }); | ||
} catch (e) { | ||
this.log.error(`${this.namespace} Cannot copy the broken file ${datasetName} to ${datasetName}.broken ${e.message}`); | ||
this.log.error( | ||
`${this.namespace} Cannot copy the broken file ${datasetName} to ${datasetName}.broken ${e.message}` | ||
); | ||
} | ||
@@ -142,3 +143,5 @@ try { | ||
} catch (e) { | ||
this.log.error(`${this.namespace} Cannot restore backup file as new main ${datasetName}: ${e.message}`); | ||
this.log.error( | ||
`${this.namespace} Cannot restore backup file as new main ${datasetName}: ${e.message}` | ||
); | ||
} | ||
@@ -150,4 +153,8 @@ } | ||
} catch (err) { | ||
this.log.error(`${this.namespace} Cannot load ${datasetName}.bak: ${err.message}. Continue with empty dataset!`); | ||
this.log.error(`${this.namespace} If this is no Migration or initial start please restore the last backup from ${this.backupDir}`); | ||
this.log.error( | ||
`${this.namespace} Cannot load ${datasetName}.bak: ${err.message}. Continue with empty dataset!` | ||
); | ||
this.log.error( | ||
`${this.namespace} If this is no Migration or initial start please restore the last backup from ${this.backupDir}` | ||
); | ||
} | ||
@@ -161,3 +168,4 @@ } | ||
// Interval in minutes => to milliseconds | ||
this.settings.backup.period = this.settings.backup.period === undefined ? 120 : parseInt(this.settings.backup.period); | ||
this.settings.backup.period = | ||
this.settings.backup.period === undefined ? 120 : parseInt(this.settings.backup.period); | ||
if (isNaN(this.settings.backup.period)) { | ||
@@ -168,3 +176,4 @@ this.settings.backup.period = 120; | ||
this.settings.backup.files = this.settings.backup.files === undefined ? 24 : parseInt(this.settings.backup.files); | ||
this.settings.backup.files = | ||
this.settings.backup.files === undefined ? 24 : parseInt(this.settings.backup.files); | ||
if (isNaN(this.settings.backup.files)) { | ||
@@ -174,3 +183,4 @@ this.settings.backup.files = 24; | ||
this.settings.backup.hours = this.settings.backup.hours === undefined ? 48 : parseInt(this.settings.backup.hours); | ||
this.settings.backup.hours = | ||
this.settings.backup.hours === undefined ? 48 : parseInt(this.settings.backup.hours); | ||
if (isNaN(this.settings.backup.hours)) { | ||
@@ -199,7 +209,7 @@ this.settings.backup.hours = 48; | ||
s.push({pattern: pattern, regex: new RegExp(tools.pattern2RegEx(pattern)), options: options}); | ||
s.push({ pattern: pattern, regex: new RegExp(tools.pattern2RegEx(pattern)), options: options }); | ||
}); | ||
} else { | ||
if (!s.find(sub => sub.pattern === pattern)) { | ||
s.push({pattern: pattern, regex: new RegExp(tools.pattern2RegEx(pattern)), options: options}); | ||
s.push({ pattern: pattern, regex: new RegExp(tools.pattern2RegEx(pattern)), options: options }); | ||
} | ||
@@ -259,3 +269,5 @@ } | ||
} catch (e) { | ||
this.log.error(`${this.namespace} Cannot delete file "${path.join(this.backupDir, file)}: ${e.message}`); | ||
this.log.error( | ||
`${this.namespace} Cannot delete file "${path.join(this.backupDir, file)}: ${e.message}` | ||
); | ||
} | ||
@@ -334,3 +346,3 @@ } | ||
try { | ||
await fs.move(this.datasetName, `${this.datasetName}.bak`, {overwrite: true}); | ||
await fs.move(this.datasetName, `${this.datasetName}.bak`, { overwrite: true }); | ||
} catch (e) { | ||
@@ -349,5 +361,7 @@ bakOk = false; | ||
try { | ||
await fs.move(`${this.datasetName}.new`, this.datasetName, {overwrite: true}); | ||
await fs.move(`${this.datasetName}.new`, this.datasetName, { overwrite: true }); | ||
} catch (e) { | ||
this.log.error(`${this.namespace} Cannot move ${this.datasetName}.new to ${this.datasetName}: ${e.message}. Try direct write as fallback`); | ||
this.log.error( | ||
`${this.namespace} Cannot move ${this.datasetName}.new to ${this.datasetName}: ${e.message}. Try direct write as fallback` | ||
); | ||
try { | ||
@@ -359,6 +373,6 @@ await fs.writeFile(this.datasetName, jsonString); | ||
} | ||
} | ||
if (!bakOk) { // it seems the bak File is not successfully there, write current content again | ||
if (!bakOk) { | ||
// it seems the bak File is not successfully there, write current content again | ||
try { | ||
@@ -386,3 +400,6 @@ await fs.writeFile(`${this.datasetName}.bak`, jsonString); | ||
this.lastSave = now; | ||
const backFileName = path.join(this.backupDir, this.getTimeStr(now) + '_' + this.settings.fileDB.fileName + '.gz'); | ||
const backFileName = path.join( | ||
this.backupDir, | ||
this.getTimeStr(now) + '_' + this.settings.fileDB.fileName + '.gz' | ||
); | ||
@@ -414,3 +431,3 @@ try { | ||
getStatus() { | ||
return {type: 'file', server: true}; | ||
return { type: 'file', server: true }; | ||
} | ||
@@ -439,7 +456,10 @@ | ||
// local subscriptions | ||
if (this.change && this.callbackSubscriptionClient._subscribe && this.callbackSubscriptionClient._subscribe[type]) { | ||
if ( | ||
this.change && | ||
this.callbackSubscriptionClient._subscribe && | ||
this.callbackSubscriptionClient._subscribe[type] | ||
) { | ||
for (let j = 0; j < this.callbackSubscriptionClient._subscribe[type].length; j++) { | ||
if (this.callbackSubscriptionClient._subscribe[type][j].regex.test(id)) { | ||
setImmediate(() => | ||
this.change(id, obj)); | ||
setImmediate(() => this.change(id, obj)); | ||
break; | ||
@@ -446,0 +466,0 @@ } |
@@ -43,3 +43,3 @@ const Resp = require('respjs'); | ||
if (this.initialized) { | ||
this.sendError(null,new Error('PARSER ERROR ' + err)); // TODO | ||
this.sendError(null, new Error('PARSER ERROR ' + err)); // TODO | ||
} else { | ||
@@ -54,3 +54,15 @@ this.close(); | ||
if (this.options.enhancedLogging) { | ||
this.log.silly(`${this.socketId} New Redis request: ${(data.length > 1024) ? data.toString().replace(/[\r\n]+/g, '').substring(0, 100) + ' -- ' + data.length + ' bytes' : data.toString().replace(/[\r\n]+/g, '')}`); | ||
this.log.silly( | ||
`${this.socketId} New Redis request: ${ | ||
data.length > 1024 | ||
? data | ||
.toString() | ||
.replace(/[\r\n]+/g, '') | ||
.substring(0, 100) + | ||
' -- ' + | ||
data.length + | ||
' bytes' | ||
: data.toString().replace(/[\r\n]+/g, '') | ||
}` | ||
); | ||
} | ||
@@ -96,7 +108,13 @@ this.resp.write(data); | ||
const t = process.hrtime(); | ||
const responseId = (t[0] * 1e3) + (t[1] / 1e6); | ||
const responseId = t[0] * 1e3 + t[1] / 1e6; | ||
if (this.options.enhancedLogging) { | ||
this.log.silly(`${this.socketId} Parser result: id=${responseId}, command=${command}, data=${(JSON.stringify(data).length > 1024) ? JSON.stringify(data).substring(0, 100) + ' -- ' + JSON.stringify(data).length + ' bytes' : JSON.stringify(data)}`); | ||
this.log.silly( | ||
`${this.socketId} Parser result: id=${responseId}, command=${command}, data=${ | ||
JSON.stringify(data).length > 1024 | ||
? JSON.stringify(data).substring(0, 100) + ' -- ' + JSON.stringify(data).length + ' bytes' | ||
: JSON.stringify(data) | ||
}` | ||
); | ||
} | ||
this.writeQueue.push({id: responseId, data: false}); | ||
this.writeQueue.push({ id: responseId, data: false }); | ||
if (this.listenerCount(command) !== 0) { | ||
@@ -133,3 +151,9 @@ setImmediate(() => this.emit(command, data, responseId)); | ||
if (this.options.enhancedLogging) { | ||
this.log.silly(`${this.socketId} Redis response (${response.id}): ${(response.data.length > 1024) ? data.length + ' bytes' : response.data.toString().replace(/[\r\n]+/g, '')}`); | ||
this.log.silly( | ||
`${this.socketId} Redis response (${response.id}): ${ | ||
response.data.length > 1024 | ||
? data.length + ' bytes' | ||
: response.data.toString().replace(/[\r\n]+/g, '') | ||
}` | ||
); | ||
} | ||
@@ -136,0 +160,0 @@ this._write(response.data); |
@@ -21,2 +21,2 @@ const { tools } = require('@iobroker/js-controller-common'); | ||
return module.exports.maybeCallbackWithError(callback, error, ...args); | ||
}; | ||
}; |
{ | ||
"name": "@iobroker/db-base", | ||
"version": "4.0.0-alpha.25-20211211-1232a951", | ||
"version": "4.0.0-alpha.26-20211214-19e8d05e", | ||
"engines": { | ||
@@ -8,3 +8,3 @@ "node": ">=12.0.0" | ||
"dependencies": { | ||
"@iobroker/js-controller-common": "4.0.0-alpha.25-20211211-1232a951", | ||
"@iobroker/js-controller-common": "4.0.0-alpha.26-20211214-19e8d05e", | ||
"deep-clone": "^3.0.3", | ||
@@ -40,3 +40,3 @@ "fs-extra": "^10.0.0", | ||
], | ||
"gitHead": "f1cdd329b8fd9a6c442ace4fd1c50a1e7f53d381" | ||
"gitHead": "b192169960992cc360b1f6c297a6ec0877e2558f" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30296
720