New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@iobroker/db-base

Package Overview
Dependencies
Maintainers
6
Versions
429
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@iobroker/db-base - npm Package Compare versions

Comparing version 4.0.0-alpha.5-20210903-ac21ada4 to 4.0.0-alpha.50-20220123-bedb0512

lib/constants.js

92

lib/inMemFileDB.js

@@ -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) {

@@ -62,11 +61,11 @@ 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
this.settings.backup = this.settings.connection.backup || {
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 });
}

@@ -240,3 +250,3 @@ }

deleteOldBackupFiles() {
deleteOldBackupFiles(baseFilename) {
// delete files only if settings.backupNumber is not 0

@@ -247,3 +257,3 @@ let files = fs.readdirSync(this.backupDir);

files = files.filter(f => f.endsWith(this.settings.fileDB.fileName + '.gz'));
files = files.filter(f => f.endsWith(baseFilename + '.gz'));

@@ -261,3 +271,5 @@ while (files.length > this.settings.backup.files) {

} 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}`
);
}

@@ -336,3 +348,3 @@ }

try {
await fs.move(this.datasetName, `${this.datasetName}.bak`, {overwrite: true});
await fs.move(this.datasetName, `${this.datasetName}.bak`, { overwrite: true });
} catch (e) {

@@ -351,5 +363,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 {

@@ -361,6 +375,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 {

@@ -388,3 +402,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'
);

@@ -407,3 +424,3 @@ try {

// analyse older files
this.deleteOldBackupFiles();
this.deleteOldBackupFiles(this.settings.fileDB.fileName);
}

@@ -417,3 +434,3 @@ } catch (e) {

getStatus() {
return {type: 'file', server: true};
return { type: 'file', server: true };
}

@@ -442,7 +459,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;

@@ -449,0 +469,0 @@ }

const Resp = require('respjs');
const { EventEmitter } = require('events');
const { QUEUED_STR_BUF, OK_STR_BUF } = require('./constants');

@@ -43,3 +44,3 @@ /**

if (this.initialized) {
this.sendError(null,new Error('PARSER ERROR ' + err)); // TODO
this.sendError(null, new Error(`PARSER ERROR ${err}`)); // TODO
} else {

@@ -54,3 +55,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, '')
}`
);
}

@@ -92,15 +105,45 @@ this.resp.write(data);

}
const t = process.hrtime();
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)
}`
);
}
if (command === 'multi') {
this._handleMulti();
return;
}
// multi active and exec not called yet
if (this.multiActive && !this.execCalled && command !== 'exec') {
// store all response ids so we know which need to be in the multi call
this.multiResponseIds.push(responseId);
// add it for the correct order will be overwritten with correct response
this.multiResponseMap.set(responseId, null);
} else {
// multi response ids should not be pushed - we will answer combined
this.writeQueue.push({ id: responseId, data: false });
}
if (command === 'exec') {
this._handleExec(responseId);
return;
}
if (command === 'info') {
this.initialized = true;
}
const t = process.hrtime();
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.writeQueue.push({id: responseId, data: false});
if (this.listenerCount(command) !== 0) {
setImmediate(() => this.emit(command, data, responseId));
} else {
this.sendError(responseId, new Error(command + ' NOT SUPPORTED'));
this.sendError(responseId, new Error(`${command} NOT SUPPORTED`));
}

@@ -118,2 +161,3 @@ }

let idx = 0;
while (this.writeQueue.length && idx < this.writeQueue.length) {

@@ -134,4 +178,11 @@ // we found the queue entry that matches with the responseId, so store the data so be sent out

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, '')
}`
);
}
this._write(response.data);

@@ -183,4 +234,5 @@ // We sended out first queue entry but no further response is ready

this.log.warn(`${this.socketId} Not able to write ${JSON.stringify(data)}`);
data = Resp.encodeError(new Error('INVALID RESPONSE: ' + JSON.stringify(data)));
data = Resp.encodeError(new Error(`INVALID RESPONSE: ${JSON.stringify(data)}`));
}
setImmediate(() => this._sendQueued(responseId, data));

@@ -211,2 +263,7 @@ }

sendNull(responseId) {
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
this._handleMultiResponse(responseId, Resp.encodeNull());
return;
}
this.sendResponse(responseId, Resp.encodeNull());

@@ -220,2 +277,7 @@ }

sendNullArray(responseId) {
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
this._handleMultiResponse(responseId, Resp.encodeNullArray());
return;
}
this.sendResponse(responseId, Resp.encodeNullArray());

@@ -230,2 +292,7 @@ }

sendString(responseId, str) {
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
this._handleMultiResponse(responseId, Resp.encodeString(str));
return;
}
this.sendResponse(responseId, Resp.encodeString(str));

@@ -241,2 +308,8 @@ }

this.log.warn(`${this.socketId} Error from InMemDB: ${error}`);
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
this._handleMultiResponse(responseId, Resp.encodeError(error));
return;
}
this.sendResponse(responseId, Resp.encodeError(error));

@@ -251,2 +324,7 @@ }

sendInteger(responseId, num) {
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
this._handleMultiResponse(responseId, Resp.encodeInteger(num));
return;
}
this.sendResponse(responseId, Resp.encodeInteger(num));

@@ -261,2 +339,7 @@ }

sendBulk(responseId, str) {
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
this._handleMultiResponse(responseId, Resp.encodeBulk(str));
return;
}
this.sendResponse(responseId, Resp.encodeBulk(str));

@@ -271,2 +354,7 @@ }

sendBufBulk(responseId, buf) {
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
this._handleMultiResponse(responseId, Resp.encodeBufBulk(buf));
return;
}
this.sendResponse(responseId, Resp.encodeBufBulk(buf));

@@ -299,10 +387,79 @@ }

* Encode a array values to buffers and send out
* @param responseId ID of the response
* @param arr Array to send out
* @param {string} responseId ID of the response
* @param {any[]} arr Array to send out
*/
sendArray(responseId, arr) {
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
this._handleMultiResponse(responseId, Resp.encodeArray(this.encodeRespArray(arr)));
return;
}
this.sendResponse(responseId, Resp.encodeArray(this.encodeRespArray(arr)));
}
/**
* Handles a 'multi' command
*
* @private
*/
_handleMulti() {
if (this.multiActive) {
this.log.warn(`${this.socketId} Conflicting multi call`);
}
this.multiActive = true;
this.execCalled = false;
this.multiResponseIds = [];
this.multiResponseCount = 0;
this.multiResponseMap = new Map();
}
/**
* Handles an 'exec' command
*
* @param {number} responseId ID of the response
* @private
*/
_handleExec(responseId) {
this.execCalled = true;
this.execId = responseId;
// maybe we have all fullfilled yet
if (this.multiResponseCount === this.multiResponseIds.length) {
this._sendExecReponse();
}
}
/**
* Builds up the exec response and sends it
*
* @private
*/
_sendExecReponse() {
this.multiActive = false;
// collect all 'QUEUED' answers
const queuedStrArr = new Array(this.multiResponseCount).fill(QUEUED_STR_BUF);
this._sendQueued(
this.execId,
Buffer.concat([OK_STR_BUF, ...queuedStrArr, Resp.encodeArray(Array.from(this.multiResponseMap.values()))])
);
}
/**
* Handles a multi response
*
* @param {number} responseId ID of the response
* @param {Buffer} buf buffer to include in response
* @private
*/
_handleMultiResponse(responseId, buf) {
this.multiResponseMap.set(responseId, buf);
this.multiResponseCount++;
if (this.execCalled && this.multiResponseCount === this.multiResponseIds.length) {
this._sendExecReponse();
}
}
}
module.exports = RedisHandler;

@@ -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.5-20210903-ac21ada4",
"version": "4.0.0-alpha.50-20220123-bedb0512",
"engines": {

@@ -8,6 +8,5 @@ "node": ">=12.0.0"

"dependencies": {
"@iobroker/js-controller-common": "4.0.0-alpha.5-20210903-ac21ada4",
"@iobroker/js-controller-common": "4.0.0-alpha.50-20220123-bedb0512",
"deep-clone": "^3.0.3",
"fs-extra": "^10.0.0",
"node.extend": "^2.0.2",
"respjs": "^4.2.0"

@@ -36,3 +35,7 @@ },

},
"gitHead": "747dba053c5838df01bb90d04101665f449cec8a"
"files": [
"lib/",
"index.js"
],
"gitHead": "6c23f1520df68de8eb5450e81ee94ec286063720"
}

@@ -7,3 +7,3 @@ # Base DB classes for ioBroker

Copyright (c) 2014-2020 bluefox <dogafox@gmail.com>,
Copyright (c) 2014-2021 bluefox <dogafox@gmail.com>,
Copyright (c) 2014 hobbyquaker

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc