@iobroker/db-states-jsonl
Advanced tools
Comparing version 4.0.0-alpha.25-20211211-1232a951 to 4.0.0-alpha.26-20211214-19e8d05e
@@ -18,4 +18,4 @@ /** | ||
const StatesInMemoryFileDB = require('@iobroker/db-states-file').StatesInMemoryFileDB; | ||
const { JsonlDB } = require('@alcalzone/jsonl-db'); | ||
const path = require('path'); | ||
const { JsonlDB } = require('@alcalzone/jsonl-db'); | ||
const path = require('path'); | ||
@@ -48,3 +48,2 @@ // settings = { | ||
class StatesInMemoryJsonlDB extends StatesInMemoryFileDB { | ||
constructor(settings) { | ||
@@ -76,6 +75,3 @@ settings = settings || {}; | ||
/** @type {JsonlDB<any>} */ | ||
this._db = new JsonlDB( | ||
path.join(this.dataDir, settings.jsonlDB.fileName), | ||
jsonlOptions | ||
); | ||
this._db = new JsonlDB(path.join(this.dataDir, settings.jsonlDB.fileName), jsonlOptions); | ||
} | ||
@@ -82,0 +78,0 @@ |
@@ -18,6 +18,5 @@ /** | ||
const StatesInRedisClient = require('@iobroker/db-states-redis').Client; | ||
const StatesInMemServer = require('./statesInMemServerRedis'); | ||
const StatesInMemServer = require('./statesInMemServerRedis'); | ||
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 |
@@ -16,6 +16,6 @@ /** | ||
'use strict'; | ||
const net = require('net'); | ||
const net = require('net'); | ||
const { inspect } = require('util'); | ||
const RedisHandler = require('@iobroker/db-base').redisHandler; | ||
const RedisHandler = require('@iobroker/db-base').redisHandler; | ||
const StatesInMemoryJsonlDB = require('./statesInMemJsonlDB'); | ||
@@ -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,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); | ||
@@ -184,4 +195,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 +210,3 @@ } | ||
} | ||
const {id, namespace} = this._normalizeId(data); | ||
const { id, namespace } = this._normalizeId(data); | ||
@@ -204,3 +216,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 +223,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 +233,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 +254,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 +264,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 +271,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 +283,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 +293,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 +300,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 +307,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 +323,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 +335,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 +345,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 +354,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 +367,3 @@ }); | ||
} | ||
const {id, namespace} = this._normalizeId(data[0]); | ||
const { id, namespace } = this._normalizeId(data[0]); | ||
if (namespace === this.namespaceStates) { | ||
@@ -344,3 +379,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 +389,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 +401,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 +411,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 +423,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 +469,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 +493,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 +516,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 +552,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 +560,0 @@ |
{ | ||
"name": "@iobroker/db-states-jsonl", | ||
"version": "4.0.0-alpha.25-20211211-1232a951", | ||
"version": "4.0.0-alpha.26-20211214-19e8d05e", | ||
"engines": { | ||
@@ -9,5 +9,5 @@ "node": ">=12.0.0" | ||
"@alcalzone/jsonl-db": "^2.2.0", | ||
"@iobroker/db-base": "4.0.0-alpha.25-20211211-1232a951", | ||
"@iobroker/db-states-file": "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-file": "4.0.0-alpha.26-20211214-19e8d05e", | ||
"@iobroker/db-states-redis": "4.0.0-alpha.26-20211214-19e8d05e" | ||
}, | ||
@@ -39,3 +39,3 @@ "keywords": [ | ||
], | ||
"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
29215
663
+ Added@iobroker/db-base@4.0.0-alpha.26-20211214-19e8d05e(transitive)
+ Added@iobroker/db-states-file@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)
- Removed@iobroker/db-base@4.0.0-alpha.25-20211211-1232a951(transitive)
- Removed@iobroker/db-states-file@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)
Updated@iobroker/db-states-file@4.0.0-alpha.26-20211214-19e8d05e
Updated@iobroker/db-states-redis@4.0.0-alpha.26-20211214-19e8d05e