/
index.js
133 lines (111 loc) · 3.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
const EventEmitter = require('events');
const JSONB = require('json-buffer');
class Keyv extends EventEmitter {
constructor(options) {
super();
this.options = Object.assign(
{
namespace: 'keyv',
serialize: JSONB.stringify,
deserialize: JSONB.parse
},
options
);
this.store = this.options.store || new Map();
this.store.namespace = this.options.namespace;
if (typeof this.store.on === 'function') {
this.store.on('error', error => this.emit('error', error));
}
const generateIterator = iterator => async function * () {
for await (const [key, raw] of (typeof iterator === 'function' ? iterator() : iterator)) {
const data = (typeof raw === 'string') ? this.options.deserialize(raw) : raw;
if (!key.includes(this.options.namespace) || typeof data !== 'object') {
continue;
}
if (typeof data.expires === 'number' && Date.now() > data.expires) {
this.delete(key);
continue;
}
yield [this._getKeyUnprefix(key), data.value];
}
};
// Attach iterators
if (typeof this.store[Symbol.iterator] === 'function' && this.store instanceof Map) {
this.iterator = generateIterator(this.store);
} else if (typeof this.store.iterator === 'function') {
this.iterator = generateIterator(this.store.iterator.bind(this.store));
} else {
this.iteratorSupport = false;
}
}
_getKeyPrefix(key) {
return this.options.namespace ? `${this.options.namespace}:${key}` : key;
}
_getKeyUnprefix(key) {
return this.options.namespace ? key.split(':').splice(1).join(':') : key;
}
get(key, options) {
const keyPrefixed = this._getKeyPrefix(key);
const store = this.store;
return Promise.resolve()
.then(() => store.get(keyPrefixed))
.then(data => {
return (typeof data === 'string') ? this.options.deserialize(data) : data;
})
.then(data => {
if (data === undefined) {
return undefined;
}
if (typeof data.expires === 'number' && Date.now() > data.expires) {
this.delete(key);
return undefined;
}
return (options && options.raw) ? data : data.value;
});
}
has(key) {
const keyPrefixed = this._getKeyPrefix(key);
const store = this.store;
if (typeof store.has === 'function') {
return Promise.resolve()
.then(() => store.has(keyPrefixed));
}
return Promise.resolve()
.then(() => store.get(keyPrefixed))
.then(data => data !== undefined);
}
set(key, value, ttl) {
const keyPrefixed = this._getKeyPrefix(key);
if (typeof ttl === 'undefined') {
ttl = this.options.ttl;
}
if (ttl === 0) {
ttl = undefined;
}
const store = this.store;
return Promise.resolve()
.then(() => {
const expires = (typeof ttl === 'number') ? (Date.now() + ttl) : null;
value = { value, expires };
return this.options.serialize(value);
})
.then(value => store.set(keyPrefixed, value, ttl))
.then(() => true);
}
delete(key) {
const keyPrefixed = this._getKeyPrefix(key);
const store = this.store;
return Promise.resolve()
.then(() => store.delete(keyPrefixed));
}
clear() {
if (!this.options.namespace) {
return Promise.resolve().then(() => undefined);
}
const store = this.store;
return Promise.resolve()
.then(() => store.clear());
}
}
module.exports = Keyv;