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

@iobroker/db-states-jsonl

Package Overview
Dependencies
Maintainers
6
Versions
417
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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

Comparing version 4.0.0-alpha.45-20220114-88aaebdb to 4.0.0-alpha.46-20220119-9e487e55

84

lib/states/statesInMemJsonlDB.js

@@ -80,3 +80,5 @@ /**

async open() {
await this._db.open();
if (!(await this._maybeMigrateFileDB())) {
await this._db.open();
}

@@ -126,2 +128,78 @@ // Create an object-like wrapper around the internal Map

/**
* Checks if an existing file DB should be migrated to JSONL
* @returns {Promise<boolean>} true if the file DB was migrated. false if not.
* If this returns true, the jsonl DB was opened and doesn't need to be opened again.
*/
async _maybeMigrateFileDB() {
const jsonlFileName = path.join(this.dataDir, this.settings.jsonlDB.fileName);
const jsonFileName = path.join(this.dataDir, this.settings.fileDB.fileName);
const bakFileName = path.join(this.dataDir, this.settings.fileDB.fileName + '.bak');
// Check the timestamps of each file, defaulting to 0 if they don't exist
let jsonlTimeStamp = 0;
let jsonTimeStamp = 0;
let bakTimeStamp = 0;
try {
const stat = fs.statSync(jsonlFileName);
if (stat.isFile()) {
jsonlTimeStamp = stat.mtime;
}
} catch {
// ignore
}
try {
const stat = fs.statSync(jsonFileName);
if (stat.isFile()) {
jsonTimeStamp = stat.mtime;
}
} catch {
// ignore
}
try {
const stat = fs.statSync(bakFileName);
if (stat.isFile()) {
bakTimeStamp = stat.mtime;
}
} catch {
// ignore
}
// Figure out which file needs to be imported
/** @type {string} */
let importFilename;
if (jsonTimeStamp > 0 && jsonTimeStamp >= bakTimeStamp && jsonTimeStamp >= jsonlTimeStamp) {
importFilename = jsonFileName;
} else if (bakTimeStamp > 0 && bakTimeStamp >= jsonTimeStamp && bakTimeStamp >= jsonlTimeStamp) {
importFilename = bakFileName;
} else {
// None of the File DB files are newer than the JSONL file
// There is nothing to restore, we're done
return false;
}
await this._db.open();
this._db.clear();
await this._db.importJson(importFilename);
// And rename the existing files to avoid redoing the work next time
if (fs.existsSync(jsonFileName)) {
try {
fs.renameSync(jsonFileName, `${jsonFileName}.migrated`);
} catch {
// ignore
}
}
if (fs.existsSync(bakFileName)) {
try {
fs.renameSync(bakFileName, `${bakFileName}.migrated`);
} catch {
// ignore
}
}
// Signal to the caller that the DB is already open
return true;
}
async saveState() {

@@ -134,6 +212,6 @@ // Nothing to do, the DB saves behind the scenes

const now = Date.now();
const tmpBackupFileName = path.join(os.tmpdir(), this.getTimeStr(now) + '_' + this.settings.jsonlDB.fileName);
const tmpBackupFileName = path.join(os.tmpdir(), `${this.getTimeStr(now)}_${this.settings.jsonlDB.fileName}`);
const backupFileName = path.join(
this.backupDir,
this.getTimeStr(now) + '_' + this.settings.jsonlDB.fileName + '.gz'
`${this.getTimeStr(now)}_${this.settings.jsonlDB.fileName}.gz`
);

@@ -140,0 +218,0 @@ try {

10

package.json
{
"name": "@iobroker/db-states-jsonl",
"version": "4.0.0-alpha.45-20220114-88aaebdb",
"version": "4.0.0-alpha.46-20220119-9e487e55",
"engines": {

@@ -9,5 +9,5 @@ "node": ">=12.0.0"

"@alcalzone/jsonl-db": "~2.4.1",
"@iobroker/db-base": "4.0.0-alpha.45-20220114-88aaebdb",
"@iobroker/db-states-file": "4.0.0-alpha.45-20220114-88aaebdb",
"@iobroker/db-states-redis": "4.0.0-alpha.45-20220114-88aaebdb"
"@iobroker/db-base": "4.0.0-alpha.46-20220119-9e487e55",
"@iobroker/db-states-file": "4.0.0-alpha.46-20220119-9e487e55",
"@iobroker/db-states-redis": "4.0.0-alpha.46-20220119-9e487e55"
},

@@ -39,3 +39,3 @@ "keywords": [

],
"gitHead": "e2fd3e7af91eb83c30f8ae45f71d6e178edc7b28"
"gitHead": "e081e38bf6d24e767e16a0bb5a8d8202f65a9302"
}
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