Beta 7 with some repo names changed.

This commit is contained in:
Luis Fernando Molina committed 2020-11-15 13:05:19 +01:00
1 parent 0f7b40cca7
commit 963c19493a
3408 files changed
+462217 -15516

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
{
"name": "keyv",
"version": "3.1.0",
"description": "Simple key-value storage with support for multiple backends",
"main": "src/index.js",
"scripts": {
"test": "xo && nyc ava test/keyv.js",
"test:full": "xo && nyc ava --serial",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"xo": {
"extends": "xo-lukechilds"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lukechilds/keyv.git"
},
"keywords": [
"key",
"value",
"store",
"cache",
"ttl"
],
"author": "Luke Childs <lukechilds123@gmail.com> (http://lukechilds.co.uk)",
"license": "MIT",
"bugs": {
"url": "https://github.com/lukechilds/keyv/issues"
},
"homepage": "https://github.com/lukechilds/keyv",
"dependencies": {
"json-buffer": "3.0.0"
},
"devDependencies": {
"ava": "^0.25.0",
"coveralls": "^3.0.0",
"eslint-config-xo-lukechilds": "^1.0.0",
"@keyv/mongo": "*",
"@keyv/mysql": "*",
"@keyv/postgres": "*",
"@keyv/redis": "*",
"@keyv/sqlite": "*",
"@keyv/test-suite": "*",
"nyc": "^11.0.3",
"this": "^1.0.2",
"timekeeper": "^2.0.0",
"xo": "^0.20.1"
}
,"_resolved": "https://registry.npmjs.org/keyv/-/keyv-3.1.0.tgz"
,"_integrity": "sha512-9ykJ/46SN/9KPM/sichzQ7OvXyGDYKGTaDlKMGCAlg2UK8KRy4jb0d8sFc+0Tt0YYnThq8X2RZgCg74RPxgcVA=="
,"_from": "keyv@^3.0.0"
}
+103
View File
@@ -0,0 +1,103 @@
'use strict';
const EventEmitter = require('events');
const JSONB = require('json-buffer');
const loadStore = opts => {
const adapters = {
redis: '@keyv/redis',
mongodb: '@keyv/mongo',
mongo: '@keyv/mongo',
sqlite: '@keyv/sqlite',
postgresql: '@keyv/postgres',
postgres: '@keyv/postgres',
mysql: '@keyv/mysql'
};
if (opts.adapter || opts.uri) {
const adapter = opts.adapter || /^[^:]*/.exec(opts.uri)[0];
return new (require(adapters[adapter]))(opts);
}
return new Map();
};
class Keyv extends EventEmitter {
constructor(uri, opts) {
super();
this.opts = Object.assign(
{
namespace: 'keyv',
serialize: JSONB.stringify,
deserialize: JSONB.parse
},
(typeof uri === 'string') ? { uri } : uri,
opts
);
if (!this.opts.store) {
const adapterOpts = Object.assign({}, this.opts);
this.opts.store = loadStore(adapterOpts);
}
if (typeof this.opts.store.on === 'function') {
this.opts.store.on('error', err => this.emit('error', err));
}
this.opts.store.namespace = this.opts.namespace;
}
_getKeyPrefix(key) {
return `${this.opts.namespace}:${key}`;
}
get(key) {
key = this._getKeyPrefix(key);
const store = this.opts.store;
return Promise.resolve()
.then(() => store.get(key))
.then(data => {
data = (typeof data === 'string') ? this.opts.deserialize(data) : data;
if (data === undefined) {
return undefined;
}
if (typeof data.expires === 'number' && Date.now() > data.expires) {
this.delete(key);
return undefined;
}
return data.value;
});
}
set(key, value, ttl) {
key = this._getKeyPrefix(key);
if (typeof ttl === 'undefined') {
ttl = this.opts.ttl;
}
if (ttl === 0) {
ttl = undefined;
}
const store = this.opts.store;
return Promise.resolve()
.then(() => {
const expires = (typeof ttl === 'number') ? (Date.now() + ttl) : null;
value = { value, expires };
return store.set(key, this.opts.serialize(value), ttl);
})
.then(() => true);
}
delete(key) {
key = this._getKeyPrefix(key);
const store = this.opts.store;
return Promise.resolve()
.then(() => store.delete(key));
}
clear() {
const store = this.opts.store;
return Promise.resolve()
.then(() => store.clear());
}
}
module.exports = Keyv;