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

@alcalzone/jsonl-db

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@alcalzone/jsonl-db - npm Package Compare versions

Comparing version 0.3.0 to 0.5.0

2

build/index.d.ts

@@ -1,2 +0,2 @@

export { DB } from "./lib/db";
export { JsonlDB as DB } from "./lib/db";
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var db_1 = require("./lib/db");
exports.DB = db_1.DB;
exports.DB = db_1.JsonlDB;
//# sourceMappingURL=index.js.map
import * as fs from "fs-extra";
export declare class DB<V extends unknown = unknown> {
constructor(filename: string);
export interface JsonlDBOptions<V> {
/**
* Whether errors reading the db file (e.g. invalid JSON) should silently be ignored.
* **Warning:** This may result in inconsistent data!
*/
ignoreReadErrors?: boolean;
/**
* An optional reviver functionn (similar to JSON.parse) to transform parsed values before they are accessible in the database.
* If this function is defined, it must always return a value.
*/
reviver?: (key: string, value: any) => any;
}
export declare class JsonlDB<V extends unknown = unknown> {
constructor(filename: string, options?: JsonlDBOptions<V>);
readonly filename: string;
readonly dumpFilename: string;
private options;
private _db;

@@ -22,4 +35,2 @@ forEach: Map<string, V>["forEach"];

private _dumpBacklog;
/** Opens a file and allows iterating over all lines */
private readLines;
private _openPromise;

@@ -26,0 +37,0 @@ open(): Promise<void>;

@@ -15,4 +15,4 @@ "use strict";

const stream = require("stream");
class DB {
constructor(filename) {
class JsonlDB {
constructor(filename, options = {}) {
this._db = new Map();

@@ -22,2 +22,3 @@ this._isOpen = false;

this.dumpFilename = this.filename + ".dump";
this.options = options;
// Bind all map properties we can use directly

@@ -38,5 +39,6 @@ this.forEach = this._db.forEach.bind(this._db);

}
/** Opens a file and allows iterating over all lines */
readLines() {
const output = new stream.PassThrough({ objectMode: true });
// /** Opens the database file or creates it if it doesn't exist */
async open() {
// Open the file for appending and reading
this._fd = await fs.open(this.filename, "a+");
const readStream = fs.createReadStream(this.filename, {

@@ -48,31 +50,32 @@ encoding: "utf8",

const rl = readline.createInterface(readStream);
rl.on("line", (line) => {
output.write(line);
});
rl.on("close", () => {
output.end();
});
return output;
}
// /** Opens the database file or creates it if it doesn't exist */
async open() {
var e_1, _a;
// Open the file for appending and reading
this._fd = await fs.open(this.filename, "a+");
let lineNo = 0;
try {
for (var _b = __asyncValues(this.readLines()), _c; _c = await _b.next(), !_c.done;) {
const line = _c.value;
this.parseLine(line);
}
await new Promise((resolve, reject) => {
rl.on("line", (line) => {
// Count source lines for the error message
lineNo++;
// Skip empty lines
if (!line)
return;
try {
this.parseLine(line);
}
catch (e) {
if (this.options.ignoreReadErrors === true) {
return;
}
else {
reject(new Error(`Cannot open file: Invalid data in line ${lineNo}`));
}
}
});
rl.on("close", resolve);
});
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) await _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
// Close the file again to avoid EBADF
rl.close();
await fs.close(this._fd);
this._fd = undefined;
}
// Close the file again to avoid EBADF
await fs.close(this._fd);
this._fd = undefined;
// Start the write thread

@@ -89,3 +92,5 @@ this._openPromise = deferred_promise_1.createDeferredPromise();

if (v !== undefined) {
this._db.set(k, v);
this._db.set(k, typeof this.options.reviver === "function"
? this.options.reviver(k, v)
: v);
}

@@ -203,3 +208,3 @@ else {

async writeThread() {
var e_2, _a;
var e_1, _a;
var _b, _c;

@@ -225,3 +230,3 @@ // This must be called before any awaits

}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {

@@ -231,3 +236,3 @@ try {

}
finally { if (e_2) throw e_2.error; }
finally { if (e_1) throw e_1.error; }
}

@@ -294,3 +299,3 @@ // The write backlog was closed, this means that the DB is being closed

}
exports.DB = DB;
exports.JsonlDB = JsonlDB;
//# sourceMappingURL=db.js.map
{
"name": "@alcalzone/jsonl-db",
"version": "0.3.0",
"version": "0.5.0",
"description": "Simple JSONL-based key-value store",

@@ -54,3 +54,3 @@ "main": "./build/index.js",

"@types/mock-fs": "^4.10.0",
"@types/node": "^13.13.2",
"@types/node": "^13.13.4",
"@typescript-eslint/eslint-plugin": "^2.29.0",

@@ -66,3 +66,3 @@ "@typescript-eslint/parser": "^2.29.0",

"jest-extended": "^0.11.5",
"mock-fs": "^4.11.0",
"mock-fs": "^4.12.0",
"prettier": "^2.0.5",

@@ -75,3 +75,3 @@ "source-map-support": "^0.5.19",

"alcalzone-shared": "^2.2.0",
"fs-extra": "^8.1.0"
"fs-extra": "^9.0.0"
},

@@ -78,0 +78,0 @@ "scripts": {

@@ -19,14 +19,38 @@ # jsonl-db

Open or create a database file:
Open or create a database file and use it like a [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
```ts
// Open
const db = new DB("/path/to/file");
await db.open();
// db.isOpen is now true
// and use it
db.set("key", value);
db.delete("key");
db.clear();
if (db.has("key")) {
result = db.get("key");
}
// ...forEach, keys(), entries(), values(), ...
```
Now, `db.isOpen` is `true`.
If corrupt data is encountered while opening the DB, the call to `open()` will be rejected. If this is to be expected, use the options parameter on the constructor to turn on forgiving behavior:
```ts
const db = new DB("/path/to/file", { ignoreReadErrors: true });
await db.open();
```
**Warning:** This may result in inconsistent data since invalid lines are silently ignored.
Use the database like you would use a [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map).
You can optionally transform the parsed values by passing a reviver function. This allows storing non-primitive objects in the database if those can be transformed to JSON (e.g. by overwriting the `toJSON` method).
```ts
function reviver(key: string, value: any) {
// MUST return a value. If you don't want to transform `value`, return it.
}
The data is persisted asynchronously, so make sure to `close()` the DB when you no longer need it:
const db = new DB("/path/to/file", { reviver });
await db.open();
```
Data written to the DB is persisted asynchronously. Be sure to call `close()` when you no longer need the database in order to flush all pending writes and close all files:
```ts

@@ -73,2 +97,10 @@ await db.close();

### 0.5.0 (2020-04-27)
Added an optional reviver function to transform non-primitive objects while loading the DB
### 0.4.0 (2020-04-27)
* Renamed the `DB` class to `JsonlDB`
* `open()` now skips empty lines
* `open()` throws an error with the line number when it encounters an invalid line. These errors can be ignored using the new constructor options argument.
### 0.3.0 (2020-04-26)

@@ -75,0 +107,0 @@ * Added `importJson` and `exportJson` methods

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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