Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@iobroker/db-states-file

Package Overview
Dependencies
Maintainers
6
Versions
433
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@iobroker/db-states-file - npm Package Compare versions

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

61

lib/states/statesInMemFileDB.js

@@ -17,4 +17,4 @@ /**

const InMemoryFileDB = require('@iobroker/db-base').inMemoryFileDB;
const tools = require('@iobroker/db-base').tools;
const InMemoryFileDB = require('@iobroker/db-base').inMemoryFileDB;
const tools = require('@iobroker/db-base').tools;

@@ -47,3 +47,2 @@ // settings = {

class StatesInMemoryFileDB extends InMemoryFileDB {
constructor(settings) {

@@ -64,5 +63,7 @@ settings = settings || {};

this.sessionExpires = {};
this.ONE_DAY_IN_SECS = 24*60*60*1000;
this.writeFileInterval = this.settings.connection && typeof this.settings.connection.writeFileInterval === 'number' ?
parseInt(this.settings.connection.writeFileInterval) : 30000;
this.ONE_DAY_IN_SECS = 24 * 60 * 60 * 1000;
this.writeFileInterval =
this.settings.connection && typeof this.settings.connection.writeFileInterval === 'number'
? parseInt(this.settings.connection.writeFileInterval)
: 30000;
this.log.silly(`${this.namespace} States DB uses file write interval of ${this.writeFileInterval} ms`);

@@ -78,3 +79,3 @@

_expireAll() {
Object.keys(this.stateExpires).forEach( id => {
Object.keys(this.stateExpires).forEach(id => {
clearTimeout(this.stateExpires[id]);

@@ -144,3 +145,3 @@ this._expireState(id);

}
return keys.map(el => this.dataset[el] !== undefined ? this.dataset[el] : null);
return keys.map(el => (this.dataset[el] !== undefined ? this.dataset[el] : null));
}

@@ -153,2 +154,40 @@

/**
* Get value of given meta id
*
* @param {string} id
* @returns {*}
*/
getMeta(id) {
if (!this.dataset['**META**']) {
this.dataset['**META**'] = {};
}
return this.dataset['**META**'][id];
}
/**
* Sets given value to id in metaNamespace
*
* @param {string} id
* @param {string} value
*/
setMeta(id, value) {
if (!this.dataset['**META**']) {
this.dataset['**META**'] = {};
}
this.dataset['**META**'][id] = value;
setImmediate(() => {
// publish event in states
this.log.silly(`${this.namespace} memory publish meta ${id} ${value}`);
this.publishAll('meta', id, value);
});
if (!this.stateTimer) {
this.stateTimer = setTimeout(() => this.saveState(), this.writeFileInterval);
}
}
// needed by Server

@@ -171,3 +210,3 @@ _setStateDirect(id, obj, expire) {

setImmediate(() => {
// publish event in states
// publish event in states
this.log.silly(`${this.namespace} memory publish ${id} ${JSON.stringify(obj)}`);

@@ -213,2 +252,6 @@ this.publishAll('state', id, obj);

_subscribeMeta(client, pattern) {
this.handleSubscribe(client, 'meta', pattern);
}
// needed by Server

@@ -215,0 +258,0 @@ _unsubscribeForClient(client, pattern) {

30

lib/states/statesInMemServerClass.js

@@ -21,27 +21,11 @@ /**

class StatesInMemoryServerClass extends StatesInRedisClient {
constructor(settings) {
settings.autoConnect = false; // delay Client connection to when we need it
// hack around testing problem where subscribe was called before connect
// Should be removed for a later release
const origConnected = settings.connected;
settings.connected = () => {
this.clientConnected = true;
if (Array.isArray(this.storedSubscribes) && this.storedSubscribes.length) {
this.log.warn(`${this.namespace} Replay ${this.storedSubscribes.length} subscription calls for States Server that were done before the client was connected initially`);
this.storedSubscribes.forEach((s => this.subscribe(s.pattern, s.options, s.callback)));
this.storedSubscribes = [];
}
origConnected();
};
super(settings);
this.clientConnected = false;
this.storedSubscribes = [];
const serverSettings = {
namespace: settings.namespace ? `${settings.namespace}-Server` : 'Server',
namespace: settings.namespace ? `${settings.namespace}-Server` : 'Server',
connection: settings.connection,
logger: settings.logger,
hostname: settings.hostname,
logger: settings.logger,
hostname: settings.hostname,
connected: () => {

@@ -62,12 +46,4 @@ this.connectDb(); // now that server is connected also connect client

}
async subscribe(pattern, options, callback) {
if (!this.clientConnected) {
this.storedSubscribes.push({pattern, options, callback}); // we ignore the promise return because not used for this testing issue we work around here
} else {
await super.subscribe(pattern, options, callback);
}
}
}
module.exports = StatesInMemoryServerClass;

@@ -19,3 +19,3 @@ /**

const RedisHandler = require('@iobroker/db-base').redisHandler;
const RedisHandler = require('@iobroker/db-base').redisHandler;
const StatesInMemoryFileDB = require('./statesInMemFileDB');

@@ -57,23 +57,34 @@

this.serverConnections = {};
this.namespaceStates = (this.settings.redisNamespace || 'io') + '.';
this.namespaceMsg = (this.settings.namespaceMsg || 'messagebox') + '.';
this.namespaceLog = (this.settings.namespaceLog || 'log') + '.';
this.namespaceSession = (this.settings.namespaceSession || 'session') + '.';
this.namespaceStates = (this.settings.redisNamespace || 'io') + '.';
this.namespaceMsg = (this.settings.namespaceMsg || 'messagebox') + '.';
this.namespaceLog = (this.settings.namespaceLog || 'log') + '.';
this.namespaceSession = (this.settings.namespaceSession || 'session') + '.';
//this.namespaceStatesLen = this.namespaceStates.length;
this.namespaceMsgLen = this.namespaceMsg.length;
this.namespaceLogLen = this.namespaceLog.length;
this.namespaceMsgLen = this.namespaceMsg.length;
this.namespaceLogLen = this.namespaceLog.length;
//this.namespaceSessionlen = this.namespaceSession.length;
this.metaNamespace = (this.settings.metaNamespace || 'meta') + '.';
this.metaNamespaceLen = this.metaNamespace.length;
this.open().then(() => {
return this._initRedisServer(this.settings.connection);
}).then(() => {
this.log.debug(`${this.namespace} ${settings.secure ? 'Secure ' : ''} Redis inMem-states listening on port ${this.settings.port || 9000}`);
this.open()
.then(() => {
return this._initRedisServer(this.settings.connection);
})
.then(() => {
this.log.debug(
`${this.namespace} ${settings.secure ? 'Secure ' : ''} Redis inMem-states listening on port ${
this.settings.port || 9000
}`
);
if (typeof this.settings.connected === 'function') {
setImmediate(() => this.settings.connected());
}
}).catch(e => {
this.log.error(`${this.namespace} Cannot start inMem-states on port ${this.settings.port || 9000}: ${e.message}`);
process.exit(24); // todo: replace it with exitcode
});
if (typeof this.settings.connected === 'function') {
setImmediate(() => this.settings.connected());
}
})
.catch(e => {
this.log.error(
`${this.namespace} Cannot start inMem-states on port ${this.settings.port || 9000}: ${e.message}`
);
process.exit(24); // todo: replace it with exitcode
});
}

@@ -94,3 +105,3 @@

idWithNamespace.forEach(el => {
const {id, namespace} = this._normalizeId(el);
const { id, namespace } = this._normalizeId(el);
ids.push(id);

@@ -105,3 +116,3 @@ ns = namespace; // we ignore the pot. case from arrays with different namespaces

ns = idWithNamespace.substr(0, pointIdx + 1);
if (ns === this.namespaceStates) {
if (ns === this.namespaceStates || ns === this.metaNamespace) {
id = idWithNamespace.substr(pointIdx + 1);

@@ -111,3 +122,3 @@ }

}
return {id: id, namespace: ns};
return { id: id, namespace: ns };
}

@@ -132,15 +143,22 @@

if (found) {
let objString;
try {
objString = JSON.stringify(obj);
} catch (e) {
// mainly catch circular structures - thus log object with inspect
this.log.error(`${this.namespace} Error on publishing state: ${id}=${inspect(obj)}: ${e.message}`);
return 0;
if (type === 'meta') {
this.log.silly(`${this.namespace} Redis Publish Meta ${id}=${obj}`);
const sendPattern = this.metaNamespace + found.pattern;
const sendId = this.metaNamespace + id;
client.sendArray(null, ['pmessage', sendPattern, sendId, obj]);
} else {
let objString;
try {
objString = JSON.stringify(obj);
} catch (e) {
// mainly catch circular structures - thus log object with inspect
this.log.error(`${this.namespace} Error on publishing state: ${id}=${inspect(obj)}: ${e.message}`);
return 0;
}
this.log.silly(`${this.namespace} Redis Publish State ${id}=${objString}`);
const sendPattern = (type === 'state' ? '' : this.namespaceStates) + found.pattern;
const sendId = (type === 'state' ? '' : this.namespaceStates) + id;
client.sendArray(null, ['pmessage', sendPattern, sendId, objString]);
}
this.log.silly(`${this.namespace} Redis Publish State ${id}=${objString}`);
const sendPattern = (type === 'state' ? '' : this.namespaceStates) + found.pattern;
const sendId = (type === 'state' ? '' : this.namespaceStates) + id;
client.sendArray(null, ['pmessage', sendPattern, sendId, objString]);
return 1;

@@ -172,3 +190,5 @@ }

infoString += '# Keyspace\r\n';
infoString += 'db0:keys=' + Object.keys(this.dataset).length + ',expires=' + (Object.keys(this.stateExpires).length + Object.keys(this.sessionExpires).length) + ',avg_ttl=98633637897';
infoString += `db0:keys=${Object.keys(this.dataset).length},expires=${
Object.keys(this.stateExpires).length + Object.keys(this.sessionExpires).length
},avg_ttl=98633637897`;
handler.sendBulk(responseId, infoString);

@@ -186,4 +206,5 @@ });

handler.on('publish', (data, responseId) => {
const {id, namespace} = this._normalizeId(data[0]);
if (namespace === this.namespaceStates) { // a "set" always comes afterwards, so do not publish
const { id, namespace } = this._normalizeId(data[0]);
if (namespace === this.namespaceStates || namespace === this.metaNamespace) {
// a "set" always comes afterwards, so do not publish
return void handler.sendInteger(responseId, 0); // do not publish for now

@@ -200,3 +221,3 @@ }

}
const {id, namespace} = this._normalizeId(data);
const { id, namespace } = this._normalizeId(data);

@@ -206,3 +227,3 @@ if (namespace === this.namespaceStates) {

const states = this._getStates(id);
const result = states.map(el => el ? JSON.stringify(el) : null);
const result = states.map(el => (el ? JSON.stringify(el) : null));
handler.sendArray(responseId, result);

@@ -213,3 +234,6 @@ } catch (err) {

} else {
handler.sendError(responseId, new Error(`MGET-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`));
handler.sendError(
responseId,
new Error(`MGET-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`)
);
}

@@ -220,3 +244,3 @@ });

handler.on('get', (data, responseId) => {
const {id, namespace} = this._normalizeId(data[0]);
const { id, namespace } = this._normalizeId(data[0]);
if (namespace === this.namespaceStates) {

@@ -240,4 +264,14 @@ const result = this._getState(id);

}
} else if (namespace === this.metaNamespace) {
const result = this.getMeta(id);
if (result === undefined || result === null) {
handler.sendNull(responseId);
} else {
handler.sendBulk(responseId, result);
}
} else {
handler.sendError(responseId, new Error(`GET-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`));
handler.sendError(
responseId,
new Error(`GET-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`)
);
}

@@ -248,3 +282,3 @@ });

handler.on('set', (data, responseId) => {
const {id, namespace} = this._normalizeId(data[0]);
const { id, namespace } = this._normalizeId(data[0]);
if (namespace === this.namespaceStates) {

@@ -255,3 +289,4 @@ try {

state = JSON.parse(data[1].toString('utf-8'));
} catch { // No JSON, so handle as binary data and set as Buffer
} catch {
// No JSON, so handle as binary data and set as Buffer
this._setBinaryState(id, data[1]);

@@ -265,4 +300,10 @@ return void handler.sendString(responseId, 'OK');

}
} else if (namespace === this.metaNamespace) {
this.setMeta(id, data[1].toString('utf-8'));
handler.sendString(responseId, 'OK');
} else {
handler.sendError(responseId, new Error(`SET-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`));
handler.sendError(
responseId,
new Error(`SET-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`)
);
}

@@ -273,3 +314,3 @@ });

handler.on('setex', (data, responseId) => {
const {id, namespace} = this._normalizeId(data[0]);
const { id, namespace } = this._normalizeId(data[0]);
if (namespace === this.namespaceStates) {

@@ -280,3 +321,4 @@ try {

state = JSON.parse(data[2].toString('utf-8'));
} catch { // No JSON, so handle as binary data and set as Buffer
} catch {
// No JSON, so handle as binary data and set as Buffer
state = data[2];

@@ -286,3 +328,6 @@ }

if (isNaN(expire)) {
return void handler.sendError(responseId, new Error(`ERROR parsing expire value ${data[1].toString('utf-8')}`));
return void handler.sendError(
responseId,
new Error(`ERROR parsing expire value ${data[1].toString('utf-8')}`)
);
}

@@ -299,3 +344,6 @@ this._setStateDirect(id, state, expire);

if (isNaN(expire)) {
return void handler.sendError(responseId, new Error(`ERROR parsing expire value ${data[1].toString('utf-8')}`));
return void handler.sendError(
responseId,
new Error(`ERROR parsing expire value ${data[1].toString('utf-8')}`)
);
}

@@ -308,3 +356,6 @@ this._setSession(id, expire, state);

} else {
handler.sendError(responseId, new Error(`SETEX-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`));
handler.sendError(
responseId,
new Error(`SETEX-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`)
);
}

@@ -315,3 +366,3 @@ });

handler.on('del', (data, responseId) => {
const {id, namespace} = this._normalizeId(data[0]);
const { id, namespace } = this._normalizeId(data[0]);
if (namespace === this.namespaceStates) {

@@ -324,3 +375,6 @@ this._delState(id);

} else {
handler.sendError(responseId, new Error(`DEL-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`));
handler.sendError(
responseId,
new Error(`DEL-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`)
);
}

@@ -334,3 +388,3 @@ });

}
const {id, namespace} = this._normalizeId(data[0]);
const { id, namespace } = this._normalizeId(data[0]);
if (namespace === this.namespaceStates) {

@@ -346,3 +400,6 @@ // special case because of simulation of redis

} else {
handler.sendError(responseId, new Error(`KEYS-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`));
handler.sendError(
responseId,
new Error(`KEYS-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`)
);
}

@@ -353,3 +410,3 @@ });

handler.on('psubscribe', (data, responseId) => {
const {id, namespace} = this._normalizeId(data[0]);
const { id, namespace } = this._normalizeId(data[0]);
if (namespace === this.namespaceMsg) {

@@ -364,4 +421,10 @@ this._subscribeMessageForClient(handler, id.substr(this.namespaceMsgLen));

handler.sendArray(responseId, ['psubscribe', data[0], 1]);
} else if (namespace === this.metaNamespace) {
this._subscribeMeta(handler, id);
handler.sendArray(responseId, ['psubscribe', data[0], 1]);
} else {
handler.sendError(responseId, new Error(`PSUBSCRIBE-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`));
handler.sendError(
responseId,
new Error(`PSUBSCRIBE-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`)
);
}

@@ -372,3 +435,3 @@ });

handler.on('punsubscribe', (data, responseId) => {
const {id, namespace} = this._normalizeId(data[0]);
const { id, namespace } = this._normalizeId(data[0]);
if (namespace === this.namespaceMsg) {

@@ -384,3 +447,6 @@ this._unsubscribeMessageForClient(handler, id.substr(this.namespaceMsgLen));

} else {
handler.sendError(responseId, new Error(`PUNSUBSCRIBE-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`));
handler.sendError(
responseId,
new Error(`PUNSUBSCRIBE-UNSUPPORTED for namespace ${namespace}: Data=${JSON.stringify(data)}`)
);
}

@@ -427,4 +493,3 @@ });

handler.on('error', err =>
this.log.warn(`${namespaceLog} Redis states: ${err}`));
handler.on('error', err => this.log.warn(`${namespaceLog} Redis states: ${err}`));
}

@@ -452,13 +517,15 @@

return /** @type {Promise<void>} */ (new Promise(resolve => {
if (!this.server) {
return void resolve();
}
try {
this.server.close(() => resolve());
} catch (e) {
console.log(e.message);
resolve();
}
}));
return /** @type {Promise<void>} */ (
new Promise(resolve => {
if (!this.server) {
return void resolve();
}
try {
this.server.close(() => resolve());
} catch (e) {
console.log(e.message);
resolve();
}
})
);
}

@@ -473,3 +540,4 @@ }

_initSocket(socket) {
this.settings.connection.enhancedLogging && this.log.silly(`${this.namespace} Handling new Redis States connection`);
this.settings.connection.enhancedLogging &&
this.log.silly(`${this.namespace} Handling new Redis States connection`);

@@ -508,3 +576,8 @@ const options = {

this.server.on('error', err =>
this.log.info(`${this.namespace} ${settings.secure ? 'Secure ' : ''} Error inMem-objects listening on port ${settings.port || 9001}: ${err}`));
this.log.info(
`${this.namespace} ${settings.secure ? 'Secure ' : ''} Error inMem-objects listening on port ${
settings.port || 9001
}: ${err}`
)
);
this.server.on('connection', socket => this._initSocket(socket));

@@ -511,0 +584,0 @@

{
"name": "@iobroker/db-states-file",
"version": "4.0.0-alpha.5-20210903-ac21ada4",
"version": "4.0.0-alpha.50-20220123-bedb0512",
"engines": {

@@ -8,4 +8,4 @@ "node": ">=12.0.0"

"dependencies": {
"@iobroker/db-base": "4.0.0-alpha.5-20210903-ac21ada4",
"@iobroker/db-states-redis": "4.0.0-alpha.5-20210903-ac21ada4"
"@iobroker/db-base": "4.0.0-alpha.50-20220123-bedb0512",
"@iobroker/db-states-redis": "4.0.0-alpha.50-20220123-bedb0512"
},

@@ -33,3 +33,7 @@ "keywords": [

},
"gitHead": "747dba053c5838df01bb90d04101665f449cec8a"
"files": [
"lib/",
"index.js"
],
"gitHead": "6c23f1520df68de8eb5450e81ee94ec286063720"
}
# File DB states classes for ioBroker
The Library contains the Database classes for File based states database client and server.
## Redis simulator
The states db client is always a redis client, but if the database type is file, it will communicate with a built-in redis simulator instead of a real redis db.
In the js-controller we use [ioredis](https://github.com/luin/ioredis), the library supports all redis commands by simply calling them on the client instance, like `redis.set("foo", "bar")`.
For an explanation of the commands in native redis, we refer to the [redis documentation](https://redis.io/commands).
Currently, the following commands are supported by the simulator for states db:
### Namespaces
The simulator supports five different namespaces:
- states (default: `io.`)
- messages (default: `messagebox.`)
- log (default: `log.`)
- session (default: `session.`)
- meta (default: `meta.`)
### Overview
| Command | State of integration | namespace |
| ----------- | ----------- | ----------- |
| info | partial | independent |
| quit | full | independent |
| publish | full | all |
| mget | full | states |
| get | full | states, session, meta |
| set | full | states, meta |
| setex | full | states, session |
| del | full | states, session |
| keys | full | states |
| psubscribe | full | messages, log, states, meta |
| punsubscribe | full | messages, log, states |
| subscribe | dummy | independent |
| config | dummy | independent |
| client | partial | independent |
### info
Returns infomration about the simulator.
### quit
This will close the connection.
### publish
On publish the server will publish to all clients who have subscribed to the states, just like redis does.
### mget
`mget` is used to receive multiple states from the server.
### get
`get` is used to receive a single state from the server.
### set
`set` is used to set a state to the database.
### setex
`setex` is used to set a state to the database which automatically expires after a given time.
### del
`del` deletes a given state from the db.
### keys
It returns all matching keys.
### psubscribe
Subscribes for a pattern to receive state changes.
### punsubscribe
Unsubscribes a pattern to no longer receive state changes.
### subscribe
Just a dummy needed to acknowledge expiring keys.
### config
Mainly a dummy, just sends a positive response if `notify-keyspace-events` request received.
### client
Is used to handle `setname` and `getname` requests. `setname` is used to change the logging namespace. On `getname` the server will respond with the current connection name, which has been set via `getname`.
## License
The MIT License (MIT)
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