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

@edjopato/datastore

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@edjopato/datastore - npm Package Compare versions

Comparing version 0.3.5 to 0.4.0

5

dist/cache.d.ts

@@ -22,7 +22,4 @@ import { MaybePromise } from './types';

export declare class Cache<T> {
#private;
readonly query: QueryArgument<T>;
private readonly _store;
private readonly _ttl;
private readonly _singleQuery;
private readonly _bulkQuery;
constructor(query: QueryArgument<T>, options?: Options<T>);

@@ -29,0 +26,0 @@ get(key: string, forceQuery?: boolean): Promise<T>;

71

dist/cache.js
"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Cache_store, _Cache_ttl, _Cache_singleQuery, _Cache_bulkQuery;
Object.defineProperty(exports, "__esModule", { value: true });

@@ -15,38 +27,15 @@ exports.Cache = void 0;

});
Object.defineProperty(this, "_store", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_ttl", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_singleQuery", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_bulkQuery", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this._store = (_a = options.store) !== null && _a !== void 0 ? _a : new key_value_1.KeyValueInMemory();
this._ttl = options.ttl;
this._singleQuery = (_b = query.singleQuery) !== null && _b !== void 0 ? _b : (async (key) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
_Cache_store.set(this, void 0);
_Cache_ttl.set(this, void 0);
_Cache_singleQuery.set(this, void 0);
_Cache_bulkQuery.set(this, void 0);
__classPrivateFieldSet(this, _Cache_store, (_a = options.store) !== null && _a !== void 0 ? _a : new key_value_1.KeyValueInMemory(), "f");
__classPrivateFieldSet(this, _Cache_ttl, options.ttl, "f");
__classPrivateFieldSet(this, _Cache_singleQuery, (_b = query.singleQuery) !== null && _b !== void 0 ? _b : (async (key) => {
const result = await query.bulkQuery([key]);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return result[key];
});
this._bulkQuery = (_c = query.bulkQuery) !== null && _c !== void 0 ? _c : (async (keys) => {
}), "f");
__classPrivateFieldSet(this, _Cache_bulkQuery, (_c = query.bulkQuery) !== null && _c !== void 0 ? _c : (async (keys) => {
const entries = await Promise.all(keys
.map(async (key) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const value = await query.singleQuery(key);

@@ -60,7 +49,7 @@ return { key, value };

return result;
});
}), "f");
}
async get(key, forceQuery = false) {
if (!forceQuery) {
const value = await this._store.get(key);
const value = await __classPrivateFieldGet(this, _Cache_store, "f").get(key);
if (value) {

@@ -70,4 +59,4 @@ return value;

}
const queried = await this._singleQuery(key);
await this._store.set(key, queried, this._ttl);
const queried = await __classPrivateFieldGet(this, _Cache_singleQuery, "f").call(this, key);
await __classPrivateFieldGet(this, _Cache_store, "f").set(key, queried, __classPrivateFieldGet(this, _Cache_ttl, "f"));
return queried;

@@ -83,3 +72,3 @@ }

.map(async (key) => {
const missing = (await this._store.get(key)) === undefined;
const missing = (await __classPrivateFieldGet(this, _Cache_store, "f").get(key)) === undefined;
return missing ? key : undefined;

@@ -91,11 +80,10 @@ }));

if (keysToBeLoaded.length > 0) {
const queryResults = await this._bulkQuery(keysToBeLoaded);
const queryResults = await __classPrivateFieldGet(this, _Cache_bulkQuery, "f").call(this, keysToBeLoaded);
await Promise.all(Object.entries(queryResults)
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
.map(async ([key, value]) => this._store.set(key, value, this._ttl)));
.map(async ([key, value]) => __classPrivateFieldGet(this, _Cache_store, "f").set(key, value, __classPrivateFieldGet(this, _Cache_ttl, "f"))));
}
const resultEntries = await Promise.all(keys
.map(async (key) => {
const value = await this._store.get(key);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const value = await __classPrivateFieldGet(this, _Cache_store, "f").get(key);
return { key, value: value };

@@ -107,2 +95,3 @@ }));

exports.Cache = Cache;
_Cache_store = new WeakMap(), _Cache_ttl = new WeakMap(), _Cache_singleQuery = new WeakMap(), _Cache_bulkQuery = new WeakMap();
//# sourceMappingURL=cache.js.map
import { ExtendedStore } from './type';
export declare class KeyValueInMemoryFile<T> implements ExtendedStore<T> {
private readonly _filepath;
#private;
private readonly filepath;
get ttlSupport(): boolean;
private readonly _inMemoryStorage;
constructor(_filepath: string);
constructor(filepath: string);
keys(): readonly string[];

@@ -12,3 +12,2 @@ get(key: string): T | undefined;

clear(): void;
private _createFileContent;
}
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _KeyValueInMemoryFile_instances, _KeyValueInMemoryFile_inMemoryStorage, _KeyValueInMemoryFile_createFileContent;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeyValueInMemoryFile = void 0;
const fs_1 = require("fs");
const writeJsonFile = require("write-json-file");
const write_json_file_1 = require("write-json-file");
class KeyValueInMemoryFile {
constructor(_filepath) {
Object.defineProperty(this, "_filepath", {
constructor(filepath) {
_KeyValueInMemoryFile_instances.add(this);
Object.defineProperty(this, "filepath", {
enumerable: true,
configurable: true,
writable: true,
value: _filepath
value: filepath
});
Object.defineProperty(this, "_inMemoryStorage", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
if (fs_1.existsSync(this._filepath)) {
const raw = fs_1.readFileSync(this._filepath, 'utf8');
_KeyValueInMemoryFile_inMemoryStorage.set(this, new Map());
if (fs_1.existsSync(this.filepath)) {
const raw = fs_1.readFileSync(this.filepath, 'utf8');
const json = JSON.parse(raw);
for (const [key, value] of Object.entries(json)) {
this._inMemoryStorage.set(key, value);
__classPrivateFieldGet(this, _KeyValueInMemoryFile_inMemoryStorage, "f").set(key, value);
}

@@ -32,18 +34,18 @@ }

keys() {
return [...this._inMemoryStorage.keys()];
return [...__classPrivateFieldGet(this, _KeyValueInMemoryFile_inMemoryStorage, "f").keys()];
}
get(key) {
return this._inMemoryStorage.get(key);
return __classPrivateFieldGet(this, _KeyValueInMemoryFile_inMemoryStorage, "f").get(key);
}
async set(key, value) {
this._inMemoryStorage.set(key, value);
await writeJsonFile(this._filepath, this._createFileContent(), { sortKeys: true });
__classPrivateFieldGet(this, _KeyValueInMemoryFile_inMemoryStorage, "f").set(key, value);
await write_json_file_1.default(this.filepath, __classPrivateFieldGet(this, _KeyValueInMemoryFile_instances, "m", _KeyValueInMemoryFile_createFileContent).call(this), { sortKeys: true });
}
async delete(key) {
const result = this._inMemoryStorage.delete(key);
if (this._inMemoryStorage.size > 0) {
await writeJsonFile(this._filepath, this._createFileContent(), { sortKeys: true });
const result = __classPrivateFieldGet(this, _KeyValueInMemoryFile_inMemoryStorage, "f").delete(key);
if (__classPrivateFieldGet(this, _KeyValueInMemoryFile_inMemoryStorage, "f").size > 0) {
await write_json_file_1.default(this.filepath, __classPrivateFieldGet(this, _KeyValueInMemoryFile_instances, "m", _KeyValueInMemoryFile_createFileContent).call(this), { sortKeys: true });
}
else if (fs_1.existsSync(this._filepath)) {
fs_1.unlinkSync(this._filepath);
else if (fs_1.existsSync(this.filepath)) {
fs_1.unlinkSync(this.filepath);
}

@@ -53,16 +55,16 @@ return result;

clear() {
this._inMemoryStorage.clear();
if (fs_1.existsSync(this._filepath)) {
fs_1.unlinkSync(this._filepath);
__classPrivateFieldGet(this, _KeyValueInMemoryFile_inMemoryStorage, "f").clear();
if (fs_1.existsSync(this.filepath)) {
fs_1.unlinkSync(this.filepath);
}
}
_createFileContent() {
const json = {};
for (const [key, value] of this._inMemoryStorage.entries()) {
json[key] = value;
}
return json;
}
}
exports.KeyValueInMemoryFile = KeyValueInMemoryFile;
_KeyValueInMemoryFile_inMemoryStorage = new WeakMap(), _KeyValueInMemoryFile_instances = new WeakSet(), _KeyValueInMemoryFile_createFileContent = function _KeyValueInMemoryFile_createFileContent() {
const json = {};
for (const [key, value] of __classPrivateFieldGet(this, _KeyValueInMemoryFile_inMemoryStorage, "f").entries()) {
json[key] = value;
}
return json;
};
//# sourceMappingURL=in-memory-file.js.map
import { ExtendedStore } from './type';
export declare class KeyValueInMemoryFiles<T> implements ExtendedStore<T> {
private readonly _directory;
#private;
private readonly directory;
get ttlSupport(): boolean;
private readonly _inMemoryStorage;
constructor(_directory: string);
constructor(directory: string);
keys(): readonly string[];

@@ -12,5 +12,2 @@ get(key: string): T | undefined;

clear(): void;
private _pathOfKey;
private _listFromFS;
private _getFromFS;
}
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _KeyValueInMemoryFiles_instances, _KeyValueInMemoryFiles_inMemoryStorage, _KeyValueInMemoryFiles_pathOfKey, _KeyValueInMemoryFiles_listFromFS, _KeyValueInMemoryFiles_getFromFS;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeyValueInMemoryFiles = void 0;
const fs_1 = require("fs");
const writeJsonFile = require("write-json-file");
const write_json_file_1 = require("write-json-file");
class KeyValueInMemoryFiles {
constructor(_directory) {
Object.defineProperty(this, "_directory", {
constructor(directory) {
_KeyValueInMemoryFiles_instances.add(this);
Object.defineProperty(this, "directory", {
enumerable: true,
configurable: true,
writable: true,
value: _directory
value: directory
});
Object.defineProperty(this, "_inMemoryStorage", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
fs_1.mkdirSync(_directory, { recursive: true });
const entries = this._listFromFS();
_KeyValueInMemoryFiles_inMemoryStorage.set(this, new Map());
fs_1.mkdirSync(directory, { recursive: true });
const entries = __classPrivateFieldGet(this, _KeyValueInMemoryFiles_instances, "m", _KeyValueInMemoryFiles_listFromFS).call(this);
for (const entry of entries) {
this._inMemoryStorage.set(entry, this._getFromFS(entry));
__classPrivateFieldGet(this, _KeyValueInMemoryFiles_inMemoryStorage, "f").set(entry, __classPrivateFieldGet(this, _KeyValueInMemoryFiles_instances, "m", _KeyValueInMemoryFiles_getFromFS).call(this, entry));
}

@@ -30,15 +32,15 @@ }

keys() {
return [...this._inMemoryStorage.keys()];
return [...__classPrivateFieldGet(this, _KeyValueInMemoryFiles_inMemoryStorage, "f").keys()];
}
get(key) {
return this._inMemoryStorage.get(key);
return __classPrivateFieldGet(this, _KeyValueInMemoryFiles_inMemoryStorage, "f").get(key);
}
async set(key, value) {
this._inMemoryStorage.set(key, value);
await writeJsonFile(this._pathOfKey(key), value, { sortKeys: true });
__classPrivateFieldGet(this, _KeyValueInMemoryFiles_inMemoryStorage, "f").set(key, value);
await write_json_file_1.default(__classPrivateFieldGet(this, _KeyValueInMemoryFiles_instances, "m", _KeyValueInMemoryFiles_pathOfKey).call(this, key), value, { sortKeys: true });
}
delete(key) {
const result = this._inMemoryStorage.delete(key);
if (fs_1.existsSync(this._pathOfKey(key))) {
fs_1.unlinkSync(this._pathOfKey(key));
const result = __classPrivateFieldGet(this, _KeyValueInMemoryFiles_inMemoryStorage, "f").delete(key);
if (fs_1.existsSync(__classPrivateFieldGet(this, _KeyValueInMemoryFiles_instances, "m", _KeyValueInMemoryFiles_pathOfKey).call(this, key))) {
fs_1.unlinkSync(__classPrivateFieldGet(this, _KeyValueInMemoryFiles_instances, "m", _KeyValueInMemoryFiles_pathOfKey).call(this, key));
}

@@ -52,16 +54,14 @@ return result;

}
_pathOfKey(key) {
return `${this._directory}/${key}.json`;
}
_listFromFS() {
return fs_1.readdirSync(this._directory)
.map(o => o.replace('.json', ''));
}
_getFromFS(key) {
const content = fs_1.readFileSync(this._pathOfKey(key), 'utf8');
const json = JSON.parse(content);
return json;
}
}
exports.KeyValueInMemoryFiles = KeyValueInMemoryFiles;
_KeyValueInMemoryFiles_inMemoryStorage = new WeakMap(), _KeyValueInMemoryFiles_instances = new WeakSet(), _KeyValueInMemoryFiles_pathOfKey = function _KeyValueInMemoryFiles_pathOfKey(key) {
return `${this.directory}/${key}.json`;
}, _KeyValueInMemoryFiles_listFromFS = function _KeyValueInMemoryFiles_listFromFS() {
return fs_1.readdirSync(this.directory)
.map(o => o.replace('.json', ''));
}, _KeyValueInMemoryFiles_getFromFS = function _KeyValueInMemoryFiles_getFromFS(key) {
const content = fs_1.readFileSync(__classPrivateFieldGet(this, _KeyValueInMemoryFiles_instances, "m", _KeyValueInMemoryFiles_pathOfKey).call(this, key), 'utf8');
const json = JSON.parse(content);
return json;
};
//# sourceMappingURL=in-memory-files.js.map
import { ExtendedStore } from './type';
export declare class KeyValueInMemory<T> implements ExtendedStore<T> {
#private;
get ttlSupport(): boolean;
private readonly _inMemoryStorage;
keys(): readonly string[];

@@ -6,0 +6,0 @@ get(key: string): T | undefined;

"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _KeyValueInMemory_inMemoryStorage;
Object.defineProperty(exports, "__esModule", { value: true });

@@ -6,8 +12,3 @@ exports.KeyValueInMemory = void 0;

constructor() {
Object.defineProperty(this, "_inMemoryStorage", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
_KeyValueInMemory_inMemoryStorage.set(this, new Map());
}

@@ -18,18 +19,19 @@ get ttlSupport() {

keys() {
return [...this._inMemoryStorage.keys()];
return [...__classPrivateFieldGet(this, _KeyValueInMemory_inMemoryStorage, "f").keys()];
}
get(key) {
return this._inMemoryStorage.get(key);
return __classPrivateFieldGet(this, _KeyValueInMemory_inMemoryStorage, "f").get(key);
}
set(key, value) {
this._inMemoryStorage.set(key, value);
__classPrivateFieldGet(this, _KeyValueInMemory_inMemoryStorage, "f").set(key, value);
}
delete(key) {
return this._inMemoryStorage.delete(key);
return __classPrivateFieldGet(this, _KeyValueInMemory_inMemoryStorage, "f").delete(key);
}
clear() {
this._inMemoryStorage.clear();
__classPrivateFieldGet(this, _KeyValueInMemory_inMemoryStorage, "f").clear();
}
}
exports.KeyValueInMemory = KeyValueInMemory;
_KeyValueInMemory_inMemoryStorage = new WeakMap();
//# sourceMappingURL=in-memory.js.map
import { ExtendedStore } from './type';
export declare class TtlKeyValueInMemoryFile<T> implements ExtendedStore<T> {
private readonly _filepath;
#private;
private readonly filepath;
get ttlSupport(): boolean;
private readonly _inMemoryStorage;
constructor(_filepath: string, cleanupIntervalMilliseconds?: number);
constructor(filepath: string, cleanupIntervalMilliseconds?: number);
keys(): readonly string[];

@@ -12,4 +12,2 @@ get(key: string): T | undefined;

clear(): void;
private _cleanupOld;
private _createFileContent;
}
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _TtlKeyValueInMemoryFile_instances, _TtlKeyValueInMemoryFile_inMemoryStorage, _TtlKeyValueInMemoryFile_cleanupOld, _TtlKeyValueInMemoryFile_createFileContent;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TtlKeyValueInMemoryFile = void 0;
const fs_1 = require("fs");
const write_json_file_1 = require("write-json-file");
const time_to_live_1 = require("./time-to-live");
const writeJsonFile = require("write-json-file");
class TtlKeyValueInMemoryFile {
constructor(_filepath, cleanupIntervalMilliseconds = 5 * 60 * 1000) {
Object.defineProperty(this, "_filepath", {
constructor(filepath, cleanupIntervalMilliseconds = 5 * 60 * 1000) {
_TtlKeyValueInMemoryFile_instances.add(this);
Object.defineProperty(this, "filepath", {
enumerable: true,
configurable: true,
writable: true,
value: _filepath
value: filepath
});
Object.defineProperty(this, "_inMemoryStorage", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
if (fs_1.existsSync(this._filepath)) {
const raw = fs_1.readFileSync(this._filepath, 'utf8');
_TtlKeyValueInMemoryFile_inMemoryStorage.set(this, new Map());
if (fs_1.existsSync(this.filepath)) {
const raw = fs_1.readFileSync(this.filepath, 'utf8');
const json = JSON.parse(raw);
for (const [key, value] of Object.entries(json)) {
this._inMemoryStorage.set(key, value);
__classPrivateFieldGet(this, _TtlKeyValueInMemoryFile_inMemoryStorage, "f").set(key, value);
}
}
if (cleanupIntervalMilliseconds && Number.isFinite(cleanupIntervalMilliseconds) && cleanupIntervalMilliseconds > 0) {
setInterval(async () => this._cleanupOld(), cleanupIntervalMilliseconds);
setInterval(async () => __classPrivateFieldGet(this, _TtlKeyValueInMemoryFile_instances, "m", _TtlKeyValueInMemoryFile_cleanupOld).call(this), cleanupIntervalMilliseconds);
}

@@ -36,7 +38,7 @@ }

keys() {
return [...this._inMemoryStorage.keys()];
return [...__classPrivateFieldGet(this, _TtlKeyValueInMemoryFile_inMemoryStorage, "f").keys()];
}
get(key) {
const now = Date.now();
const entry = this._inMemoryStorage.get(key);
const entry = __classPrivateFieldGet(this, _TtlKeyValueInMemoryFile_inMemoryStorage, "f").get(key);
if ((entry === null || entry === void 0 ? void 0 : entry.until) && entry.until > now) {

@@ -48,12 +50,12 @@ return entry.value;

async set(key, value, ttl) {
this._inMemoryStorage.set(key, time_to_live_1.createEntry(value, ttl));
await writeJsonFile(this._filepath, this._createFileContent(), { sortKeys: true });
__classPrivateFieldGet(this, _TtlKeyValueInMemoryFile_inMemoryStorage, "f").set(key, time_to_live_1.createEntry(value, ttl));
await write_json_file_1.default(this.filepath, __classPrivateFieldGet(this, _TtlKeyValueInMemoryFile_instances, "m", _TtlKeyValueInMemoryFile_createFileContent).call(this), { sortKeys: true });
}
async delete(key) {
const result = this._inMemoryStorage.delete(key);
if (this._inMemoryStorage.size > 0) {
await writeJsonFile(this._filepath, this._createFileContent(), { sortKeys: true });
const result = __classPrivateFieldGet(this, _TtlKeyValueInMemoryFile_inMemoryStorage, "f").delete(key);
if (__classPrivateFieldGet(this, _TtlKeyValueInMemoryFile_inMemoryStorage, "f").size > 0) {
await write_json_file_1.default(this.filepath, __classPrivateFieldGet(this, _TtlKeyValueInMemoryFile_instances, "m", _TtlKeyValueInMemoryFile_createFileContent).call(this), { sortKeys: true });
}
else if (fs_1.existsSync(this._filepath)) {
fs_1.unlinkSync(this._filepath);
else if (fs_1.existsSync(this.filepath)) {
fs_1.unlinkSync(this.filepath);
}

@@ -63,19 +65,18 @@ return result;

clear() {
this._inMemoryStorage.clear();
if (fs_1.existsSync(this._filepath)) {
fs_1.unlinkSync(this._filepath);
__classPrivateFieldGet(this, _TtlKeyValueInMemoryFile_inMemoryStorage, "f").clear();
if (fs_1.existsSync(this.filepath)) {
fs_1.unlinkSync(this.filepath);
}
}
async _cleanupOld() {
await time_to_live_1.cleanupOld(this._inMemoryStorage, async (key) => this.delete(key));
}
_createFileContent() {
const json = {};
for (const [key, value] of this._inMemoryStorage.entries()) {
json[key] = value;
}
return json;
}
}
exports.TtlKeyValueInMemoryFile = TtlKeyValueInMemoryFile;
_TtlKeyValueInMemoryFile_inMemoryStorage = new WeakMap(), _TtlKeyValueInMemoryFile_instances = new WeakSet(), _TtlKeyValueInMemoryFile_cleanupOld = async function _TtlKeyValueInMemoryFile_cleanupOld() {
await time_to_live_1.cleanupOld(__classPrivateFieldGet(this, _TtlKeyValueInMemoryFile_inMemoryStorage, "f"), async (key) => this.delete(key));
}, _TtlKeyValueInMemoryFile_createFileContent = function _TtlKeyValueInMemoryFile_createFileContent() {
const json = {};
for (const [key, value] of __classPrivateFieldGet(this, _TtlKeyValueInMemoryFile_inMemoryStorage, "f").entries()) {
json[key] = value;
}
return json;
};
//# sourceMappingURL=ttl-in-memory-file.js.map
import { ExtendedStore } from './type';
export declare class TtlKeyValueInMemoryFiles<T> implements ExtendedStore<T> {
private readonly _directory;
#private;
private readonly directory;
get ttlSupport(): boolean;
private readonly _inMemoryStorage;
constructor(_directory: string, cleanupIntervalMilliseconds?: number);
constructor(directory: string, cleanupIntervalMilliseconds?: number);
keys(): readonly string[];

@@ -12,6 +12,2 @@ get(key: string): T | undefined;

clear(): void;
private _pathOfKey;
private _listFromFS;
private _getFromFS;
private _cleanupOld;
}
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _TtlKeyValueInMemoryFiles_instances, _TtlKeyValueInMemoryFiles_inMemoryStorage, _TtlKeyValueInMemoryFiles_pathOfKey, _TtlKeyValueInMemoryFiles_listFromFS, _TtlKeyValueInMemoryFiles_getFromFS, _TtlKeyValueInMemoryFiles_cleanupOld;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TtlKeyValueInMemoryFiles = void 0;
const fs_1 = require("fs");
const write_json_file_1 = require("write-json-file");
const time_to_live_1 = require("./time-to-live");
const writeJsonFile = require("write-json-file");
class TtlKeyValueInMemoryFiles {
constructor(_directory, cleanupIntervalMilliseconds = 5 * 60 * 1000) {
Object.defineProperty(this, "_directory", {
constructor(directory, cleanupIntervalMilliseconds = 5 * 60 * 1000) {
_TtlKeyValueInMemoryFiles_instances.add(this);
Object.defineProperty(this, "directory", {
enumerable: true,
configurable: true,
writable: true,
value: _directory
value: directory
});
Object.defineProperty(this, "_inMemoryStorage", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
fs_1.mkdirSync(_directory, { recursive: true });
const entries = this._listFromFS();
_TtlKeyValueInMemoryFiles_inMemoryStorage.set(this, new Map());
fs_1.mkdirSync(directory, { recursive: true });
const entries = __classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_instances, "m", _TtlKeyValueInMemoryFiles_listFromFS).call(this);
for (const entry of entries) {
this._inMemoryStorage.set(entry, this._getFromFS(entry));
__classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_inMemoryStorage, "f").set(entry, __classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_instances, "m", _TtlKeyValueInMemoryFiles_getFromFS).call(this, entry));
}
if (cleanupIntervalMilliseconds && Number.isFinite(cleanupIntervalMilliseconds) && cleanupIntervalMilliseconds > 0) {
setInterval(async () => this._cleanupOld(), cleanupIntervalMilliseconds);
setInterval(async () => __classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_instances, "m", _TtlKeyValueInMemoryFiles_cleanupOld).call(this), cleanupIntervalMilliseconds);
}

@@ -34,7 +36,7 @@ }

keys() {
return [...this._inMemoryStorage.keys()];
return [...__classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_inMemoryStorage, "f").keys()];
}
get(key) {
const now = Date.now();
const entry = this._inMemoryStorage.get(key);
const entry = __classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_inMemoryStorage, "f").get(key);
if ((entry === null || entry === void 0 ? void 0 : entry.until) && entry.until > now) {

@@ -47,9 +49,9 @@ return entry.value;

const entry = time_to_live_1.createEntry(value, ttl);
this._inMemoryStorage.set(key, entry);
await writeJsonFile(this._pathOfKey(key), entry, { sortKeys: true });
__classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_inMemoryStorage, "f").set(key, entry);
await write_json_file_1.default(__classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_instances, "m", _TtlKeyValueInMemoryFiles_pathOfKey).call(this, key), entry, { sortKeys: true });
}
delete(key) {
const result = this._inMemoryStorage.delete(key);
if (fs_1.existsSync(this._pathOfKey(key))) {
fs_1.unlinkSync(this._pathOfKey(key));
const result = __classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_inMemoryStorage, "f").delete(key);
if (fs_1.existsSync(__classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_instances, "m", _TtlKeyValueInMemoryFiles_pathOfKey).call(this, key))) {
fs_1.unlinkSync(__classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_instances, "m", _TtlKeyValueInMemoryFiles_pathOfKey).call(this, key));
}

@@ -63,19 +65,16 @@ return result;

}
_pathOfKey(key) {
return `${this._directory}/${key}.json`;
}
_listFromFS() {
return fs_1.readdirSync(this._directory)
.map(o => o.replace('.json', ''));
}
_getFromFS(key) {
const content = fs_1.readFileSync(this._pathOfKey(key), 'utf8');
const json = JSON.parse(content);
return json;
}
async _cleanupOld() {
await time_to_live_1.cleanupOld(this._inMemoryStorage, key => this.delete(key));
}
}
exports.TtlKeyValueInMemoryFiles = TtlKeyValueInMemoryFiles;
_TtlKeyValueInMemoryFiles_inMemoryStorage = new WeakMap(), _TtlKeyValueInMemoryFiles_instances = new WeakSet(), _TtlKeyValueInMemoryFiles_pathOfKey = function _TtlKeyValueInMemoryFiles_pathOfKey(key) {
return `${this.directory}/${key}.json`;
}, _TtlKeyValueInMemoryFiles_listFromFS = function _TtlKeyValueInMemoryFiles_listFromFS() {
return fs_1.readdirSync(this.directory)
.map(o => o.replace('.json', ''));
}, _TtlKeyValueInMemoryFiles_getFromFS = function _TtlKeyValueInMemoryFiles_getFromFS(key) {
const content = fs_1.readFileSync(__classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_instances, "m", _TtlKeyValueInMemoryFiles_pathOfKey).call(this, key), 'utf8');
const json = JSON.parse(content);
return json;
}, _TtlKeyValueInMemoryFiles_cleanupOld = async function _TtlKeyValueInMemoryFiles_cleanupOld() {
await time_to_live_1.cleanupOld(__classPrivateFieldGet(this, _TtlKeyValueInMemoryFiles_inMemoryStorage, "f"), key => this.delete(key));
};
//# sourceMappingURL=ttl-in-memory-files.js.map
import { ExtendedStore } from './type';
export declare class TtlKeyValueInMemory<T> implements ExtendedStore<T> {
#private;
get ttlSupport(): boolean;
private readonly _inMemoryStorage;
constructor(cleanupIntervalMilliseconds?: number);

@@ -11,3 +11,2 @@ keys(): readonly string[];

clear(): void;
private _cleanupOld;
}
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _TtlKeyValueInMemory_instances, _TtlKeyValueInMemory_inMemoryStorage, _TtlKeyValueInMemory_cleanupOld;
Object.defineProperty(exports, "__esModule", { value: true });

@@ -7,10 +13,6 @@ exports.TtlKeyValueInMemory = void 0;

constructor(cleanupIntervalMilliseconds = 5 * 60 * 1000) {
Object.defineProperty(this, "_inMemoryStorage", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
_TtlKeyValueInMemory_instances.add(this);
_TtlKeyValueInMemory_inMemoryStorage.set(this, new Map());
if (cleanupIntervalMilliseconds && Number.isFinite(cleanupIntervalMilliseconds) && cleanupIntervalMilliseconds > 0) {
setInterval(async () => this._cleanupOld(), cleanupIntervalMilliseconds);
setInterval(async () => __classPrivateFieldGet(this, _TtlKeyValueInMemory_instances, "m", _TtlKeyValueInMemory_cleanupOld).call(this), cleanupIntervalMilliseconds);
}

@@ -22,7 +24,7 @@ }

keys() {
return [...this._inMemoryStorage.keys()];
return [...__classPrivateFieldGet(this, _TtlKeyValueInMemory_inMemoryStorage, "f").keys()];
}
get(key) {
const now = Date.now();
const entry = this._inMemoryStorage.get(key);
const entry = __classPrivateFieldGet(this, _TtlKeyValueInMemory_inMemoryStorage, "f").get(key);
if ((entry === null || entry === void 0 ? void 0 : entry.until) && entry.until > now) {

@@ -34,15 +36,15 @@ return entry.value;

set(key, value, ttl) {
this._inMemoryStorage.set(key, time_to_live_1.createEntry(value, ttl));
__classPrivateFieldGet(this, _TtlKeyValueInMemory_inMemoryStorage, "f").set(key, time_to_live_1.createEntry(value, ttl));
}
delete(key) {
return this._inMemoryStorage.delete(key);
return __classPrivateFieldGet(this, _TtlKeyValueInMemory_inMemoryStorage, "f").delete(key);
}
clear() {
this._inMemoryStorage.clear();
__classPrivateFieldGet(this, _TtlKeyValueInMemory_inMemoryStorage, "f").clear();
}
async _cleanupOld() {
await time_to_live_1.cleanupOld(this._inMemoryStorage, key => this.delete(key));
}
}
exports.TtlKeyValueInMemory = TtlKeyValueInMemory;
_TtlKeyValueInMemory_inMemoryStorage = new WeakMap(), _TtlKeyValueInMemory_instances = new WeakSet(), _TtlKeyValueInMemory_cleanupOld = async function _TtlKeyValueInMemory_cleanupOld() {
await time_to_live_1.cleanupOld(__classPrivateFieldGet(this, _TtlKeyValueInMemory_inMemoryStorage, "f"), key => this.delete(key));
};
//# sourceMappingURL=ttl-in-memory.js.map
import { RawObjectStorage } from './type';
export declare class RawObjectInMemoryFile<T> implements RawObjectStorage<T> {
private readonly _filepath;
private _content;
constructor(_filepath: string);
#private;
private readonly filepath;
constructor(filepath: string);
get(): T | undefined;

@@ -7,0 +7,0 @@ set(value: T): Promise<void>;

"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _RawObjectInMemoryFile_content;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RawObjectInMemoryFile = void 0;
const fs_1 = require("fs");
const writeJsonFile = require("write-json-file");
const write_json_file_1 = require("write-json-file");
class RawObjectInMemoryFile {
constructor(_filepath) {
Object.defineProperty(this, "_filepath", {
constructor(filepath) {
Object.defineProperty(this, "filepath", {
enumerable: true,
configurable: true,
writable: true,
value: _filepath
value: filepath
});
Object.defineProperty(this, "_content", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
if (fs_1.existsSync(this._filepath)) {
const raw = fs_1.readFileSync(this._filepath, 'utf8');
_RawObjectInMemoryFile_content.set(this, void 0);
if (fs_1.existsSync(this.filepath)) {
const raw = fs_1.readFileSync(this.filepath, 'utf8');
const json = JSON.parse(raw);
this._content = json;
__classPrivateFieldSet(this, _RawObjectInMemoryFile_content, json, "f");
}
}
get() {
return this._content;
return __classPrivateFieldGet(this, _RawObjectInMemoryFile_content, "f");
}
async set(value) {
this._content = value;
await writeJsonFile(this._filepath, value, { sortKeys: true });
__classPrivateFieldSet(this, _RawObjectInMemoryFile_content, value, "f");
await write_json_file_1.default(this.filepath, value, { sortKeys: true });
}
delete() {
this._content = undefined;
if (fs_1.existsSync(this._filepath)) {
fs_1.unlinkSync(this._filepath);
__classPrivateFieldSet(this, _RawObjectInMemoryFile_content, undefined, "f");
if (fs_1.existsSync(this.filepath)) {
fs_1.unlinkSync(this.filepath);
}

@@ -41,2 +48,3 @@ }

exports.RawObjectInMemoryFile = RawObjectInMemoryFile;
_RawObjectInMemoryFile_content = new WeakMap();
//# sourceMappingURL=in-memory-file.js.map
{
"name": "@edjopato/datastore",
"version": "0.3.5",
"version": "0.4.0",
"description": "Handles different ways to store data within NodeJS",

@@ -19,3 +19,3 @@ "license": "MIT",

"engines": {
"node": ">=10"
"node": ">=12"
},

@@ -27,6 +27,6 @@ "dependencies": {

"@sindresorhus/tsconfig": "^1.0.2",
"@types/node": "^15.0.1",
"del-cli": "^3.0.0",
"@types/node": "^16.0.0",
"del-cli": "^4.0.0",
"typescript": "^4.2.3",
"xo": "^0.39.1"
"xo": "^0.41.0"
},

@@ -45,2 +45,3 @@ "files": [

"rules": {
"unicorn/prefer-node-protocol": "off",
"@typescript-eslint/prefer-readonly-parameter-types": "error"

@@ -47,0 +48,0 @@ }

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

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