@iobroker/db-states-file
Advanced tools
Comparing version 4.0.0-alpha.25-20211211-1232a951 to 4.0.0-alpha.26-20211214-19e8d05e
@@ -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)); | ||
} | ||
@@ -170,3 +171,3 @@ | ||
setImmediate(() => { | ||
// publish event in states | ||
// publish event in states | ||
this.log.silly(`${this.namespace} memory publish ${id} ${JSON.stringify(obj)}`); | ||
@@ -173,0 +174,0 @@ this.publishAll('state', id, obj); |
@@ -21,3 +21,2 @@ /** | ||
class StatesInMemoryServerClass extends StatesInRedisClient { | ||
constructor(settings) { | ||
@@ -28,6 +27,6 @@ settings.autoConnect = false; // delay Client connection to when we need it | ||
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: () => { | ||
@@ -34,0 +33,0 @@ this.connectDb(); // now that server is connected also connect client |
@@ -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,32 @@ | ||
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.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 +103,3 @@ | ||
idWithNamespace.forEach(el => { | ||
const {id, namespace} = this._normalizeId(el); | ||
const { id, namespace } = this._normalizeId(el); | ||
ids.push(id); | ||
@@ -110,3 +119,3 @@ ns = namespace; // we ignore the pot. case from arrays with different namespaces | ||
} | ||
return {id: id, namespace: ns}; | ||
return { id: id, namespace: ns }; | ||
} | ||
@@ -170,3 +179,8 @@ | ||
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); | ||
@@ -184,4 +198,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) { | ||
// a "set" always comes afterwards, so do not publish | ||
return void handler.sendInteger(responseId, 0); // do not publish for now | ||
@@ -198,3 +213,3 @@ } | ||
} | ||
const {id, namespace} = this._normalizeId(data); | ||
const { id, namespace } = this._normalizeId(data); | ||
@@ -204,3 +219,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); | ||
@@ -211,3 +226,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)}`) | ||
); | ||
} | ||
@@ -218,3 +236,3 @@ }); | ||
handler.on('get', (data, responseId) => { | ||
const {id, namespace} = this._normalizeId(data[0]); | ||
const { id, namespace } = this._normalizeId(data[0]); | ||
if (namespace === this.namespaceStates) { | ||
@@ -239,3 +257,6 @@ const result = this._getState(id); | ||
} 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)}`) | ||
); | ||
} | ||
@@ -246,3 +267,3 @@ }); | ||
handler.on('set', (data, responseId) => { | ||
const {id, namespace} = this._normalizeId(data[0]); | ||
const { id, namespace } = this._normalizeId(data[0]); | ||
if (namespace === this.namespaceStates) { | ||
@@ -253,3 +274,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]); | ||
@@ -264,3 +286,6 @@ return void 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)}`) | ||
); | ||
} | ||
@@ -271,3 +296,3 @@ }); | ||
handler.on('setex', (data, responseId) => { | ||
const {id, namespace} = this._normalizeId(data[0]); | ||
const { id, namespace } = this._normalizeId(data[0]); | ||
if (namespace === this.namespaceStates) { | ||
@@ -278,3 +303,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]; | ||
@@ -284,3 +310,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')}`) | ||
); | ||
} | ||
@@ -297,3 +326,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')}`) | ||
); | ||
} | ||
@@ -306,3 +338,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)}`) | ||
); | ||
} | ||
@@ -313,3 +348,3 @@ }); | ||
handler.on('del', (data, responseId) => { | ||
const {id, namespace} = this._normalizeId(data[0]); | ||
const { id, namespace } = this._normalizeId(data[0]); | ||
if (namespace === this.namespaceStates) { | ||
@@ -322,3 +357,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)}`) | ||
); | ||
} | ||
@@ -332,3 +370,3 @@ }); | ||
} | ||
const {id, namespace} = this._normalizeId(data[0]); | ||
const { id, namespace } = this._normalizeId(data[0]); | ||
if (namespace === this.namespaceStates) { | ||
@@ -344,3 +382,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)}`) | ||
); | ||
} | ||
@@ -351,3 +392,3 @@ }); | ||
handler.on('psubscribe', (data, responseId) => { | ||
const {id, namespace} = this._normalizeId(data[0]); | ||
const { id, namespace } = this._normalizeId(data[0]); | ||
if (namespace === this.namespaceMsg) { | ||
@@ -363,3 +404,6 @@ this._subscribeMessageForClient(handler, id.substr(this.namespaceMsgLen)); | ||
} 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)}`) | ||
); | ||
} | ||
@@ -370,3 +414,3 @@ }); | ||
handler.on('punsubscribe', (data, responseId) => { | ||
const {id, namespace} = this._normalizeId(data[0]); | ||
const { id, namespace } = this._normalizeId(data[0]); | ||
if (namespace === this.namespaceMsg) { | ||
@@ -382,3 +426,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)}`) | ||
); | ||
} | ||
@@ -425,4 +472,3 @@ }); | ||
handler.on('error', err => | ||
this.log.warn(`${namespaceLog} Redis states: ${err}`)); | ||
handler.on('error', err => this.log.warn(`${namespaceLog} Redis states: ${err}`)); | ||
} | ||
@@ -450,13 +496,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(); | ||
} | ||
}) | ||
); | ||
} | ||
@@ -471,3 +519,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`); | ||
@@ -506,3 +555,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)); | ||
@@ -509,0 +563,0 @@ |
{ | ||
"name": "@iobroker/db-states-file", | ||
"version": "4.0.0-alpha.25-20211211-1232a951", | ||
"version": "4.0.0-alpha.26-20211214-19e8d05e", | ||
"engines": { | ||
@@ -8,4 +8,4 @@ "node": ">=12.0.0" | ||
"dependencies": { | ||
"@iobroker/db-base": "4.0.0-alpha.25-20211211-1232a951", | ||
"@iobroker/db-states-redis": "4.0.0-alpha.25-20211211-1232a951" | ||
"@iobroker/db-base": "4.0.0-alpha.26-20211214-19e8d05e", | ||
"@iobroker/db-states-redis": "4.0.0-alpha.26-20211214-19e8d05e" | ||
}, | ||
@@ -37,3 +37,3 @@ "keywords": [ | ||
], | ||
"gitHead": "f1cdd329b8fd9a6c442ace4fd1c50a1e7f53d381" | ||
"gitHead": "b192169960992cc360b1f6c297a6ec0877e2558f" | ||
} |
34500
803
+ Added@iobroker/db-base@4.0.0-alpha.26-20211214-19e8d05e(transitive)
+ Added@iobroker/db-states-redis@4.0.0-alpha.26-20211214-19e8d05e(transitive)
+ Added@iobroker/js-controller-common@4.0.0-alpha.26-20211214-19e8d05e(transitive)
+ Addedsemver@7.7.1(transitive)
- Removed@iobroker/db-base@4.0.0-alpha.25-20211211-1232a951(transitive)
- Removed@iobroker/db-states-redis@4.0.0-alpha.25-20211211-1232a951(transitive)
- Removed@iobroker/js-controller-common@4.0.0-alpha.25-20211211-1232a951(transitive)
- Removedsemver@7.7.0(transitive)
Updated@iobroker/db-states-redis@4.0.0-alpha.26-20211214-19e8d05e