/
index.ts
396 lines (329 loc) · 12 KB
/
index.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import {defaultSerialize, defaultDeserialize} from '@keyv/serialize';
import HooksManager from './hooks-manager';
import EventManager from './event-manager';
import StatsManager from './stats-manager';
export type DeserializedData<Value> = {
value?: Value;
expires?: number;
};
export interface CompressionAdapter {
compress(value: any, options?: any): Promise<any>;
decompress(value: any, options?: any): Promise<any>;
serialize<Value>(data: DeserializedData<Value>): Promise<string> | string;
deserialize<Value>(data: string): Promise<DeserializedData<Value> | undefined> | DeserializedData<Value> | undefined;
}
export enum KeyvHooks {
PRE_SET = 'preSet',
POST_SET = 'postSet',
PRE_GET = 'preGet',
POST_GET = 'postGet',
PRE_GET_MANY = 'preGetMany',
POST_GET_MANY = 'postGetMany',
PRE_DELETE = 'preDelete',
POST_DELETE = 'postDelete',
}
export type StoredDataNoRaw<Value> = Value | undefined;
export type StoredDataRaw<Value> = DeserializedData<Value> | undefined;
export type StoredData<Value> = StoredDataNoRaw<Value> | StoredDataRaw<Value>;
export interface IEventEmitter {
on(event: string, listener: (...args: any[]) => void): this;
}
export interface KeyvStoreAdapter extends IEventEmitter {
opts: any;
namespace?: string;
get<Value>(key: string): Promise<StoredData<Value> | undefined>;
set(key: string, value: any, ttl?: number): any;
delete(key: string): Promise<boolean>;
clear(): Promise<void>;
has?(key: string): Promise<boolean>;
getMany?<Value>(
keys: string[]
): Promise<Array<StoredData<Value | undefined>>>;
disconnect?(): Promise<void>;
deleteMany?(key: string[]): Promise<boolean>;
iterator?<Value>(namespace?: string): AsyncGenerator<Array<string | Awaited<Value> | undefined>, void>;
}
export interface Options {
[key: string]: any;
/** Emit errors */
emitErrors?: boolean;
/** Namespace for the current instance. */
namespace?: string;
/** A custom serialization function. */
serialize?: CompressionAdapter['serialize'];
/** A custom deserialization function. */
deserialize?: CompressionAdapter['deserialize'];
/** The connection string URI. */
uri?: string;
/** The storage adapter instance to be used by Keyv. */
store: KeyvStoreAdapter;
/** Default TTL. Can be overridden by specififying a TTL on `.set()`. */
ttl?: number;
/** Enable compression option **/
compression?: CompressionAdapter;
/** Specify an adapter to use. e.g `'redis'` or `'mongodb'`. */
adapter?: 'redis' | 'mongodb' | 'mongo' | 'sqlite' | 'postgresql' | 'postgres' | 'mysql';
/** Enable or disable statistics (default is false) */
stats?: boolean;
}
type IteratorFunction = (arg: any) => AsyncGenerator<any, void>;
const loadStore = (options: Options) => {
const adapters = {
redis: '@keyv/redis',
rediss: '@keyv/redis',
mongodb: '@keyv/mongo',
mongo: '@keyv/mongo',
sqlite: '@keyv/sqlite',
postgresql: '@keyv/postgres',
postgres: '@keyv/postgres',
mysql: '@keyv/mysql',
etcd: '@keyv/etcd',
offline: '@keyv/offline',
tiered: '@keyv/tiered',
};
if (options.adapter ?? options.uri) {
const adapter = (options.adapter ?? /^[^:+]*/.exec(options.uri!)![0]) as Options['adapter'];
if (!adapter || !adapters[adapter]) {
throw new Error(`Adapter ${adapter} is not allowed`);
}
// eslint-disable-next-line @typescript-eslint/no-require-imports
return new (require(adapters[adapter]))(options);
}
return new Map();
};
const iterableAdapters = [
'sqlite',
'postgres',
'mysql',
'mongo',
'redis',
'tiered',
];
class Keyv extends EventManager {
opts: Options;
iterator?: IteratorFunction;
hooks = new HooksManager();
stats = new StatsManager(false);
constructor(uri?: string | Omit<Options, 'store'>, options_?: Omit<Options, 'store'>) {
super();
options_ = options_ ?? {};
const options = {
...((typeof uri === 'string') ? {uri} : uri),
...options_,
};
// @ts-expect-error - store is being added in the next step
this.opts = {
namespace: 'keyv',
serialize: defaultSerialize,
deserialize: defaultDeserialize,
emitErrors: true,
...options,
};
if (!this.opts.store) {
const adapterOptions = {...this.opts};
this.opts.store = loadStore(adapterOptions);
}
if (this.opts.compression) {
const compression = this.opts.compression;
this.opts.serialize = compression.serialize.bind(compression);
this.opts.deserialize = compression.deserialize.bind(compression);
}
if (this.opts.store) {
if (typeof this.opts.store.on === 'function' && this.opts.emitErrors) {
this.opts.store.on('error', (error: any) => this.emit('error', error));
}
this.opts.store.namespace = this.opts.namespace;
// Attach iterators
// @ts-expect-error
if (typeof this.opts.store[Symbol.iterator] === 'function' && this.opts.store instanceof Map) {
this.iterator = this.generateIterator((this.opts.store as unknown as IteratorFunction));
} else if (this.opts.store.iterator && this.opts.store.opts && this._checkIterableAdapter()) {
this.iterator = this.generateIterator(this.opts.store.iterator.bind(this.opts.store));
}
}
if (this.opts.stats) {
this.stats.enabled = this.opts.stats;
}
}
generateIterator(iterator: IteratorFunction): IteratorFunction {
const func: IteratorFunction = async function * (this: any) {
for await (const [key, raw] of (typeof iterator === 'function'
? iterator(this.opts.store.namespace)
: iterator)) {
const data = await this.opts.deserialize(raw);
if (this.opts.store.namespace && !key.includes(this.opts.store.namespace)) {
continue;
}
if (typeof data.expires === 'number' && Date.now() > data.expires) {
this.delete(key);
continue;
}
yield [this._getKeyUnprefix(key), data.value];
}
};
return func.bind(this);
}
_checkIterableAdapter(): boolean {
return iterableAdapters.includes((this.opts.store.opts.dialect as string))
|| iterableAdapters.findIndex(element => (this.opts.store.opts.url as string).includes(element)) >= 0;
}
_getKeyPrefix(key: string): string {
return `${this.opts.namespace}:${key}`;
}
_getKeyPrefixArray(keys: string[]): string[] {
return keys.map(key => `${this.opts.namespace}:${key}`);
}
_getKeyUnprefix(key: string): string {
return key
.split(':')
.splice(1)
.join(':');
}
async get<Value>(key: string, options?: {raw: false}): Promise<StoredDataNoRaw<Value>>;
async get<Value>(key: string, options?: {raw: true}): Promise<StoredDataRaw<Value>>;
async get<Value>(key: string[], options?: {raw: false}): Promise<Array<StoredDataNoRaw<Value>>>;
async get<Value>(key: string[], options?: {raw: true}): Promise<Array<StoredDataRaw<Value>>>;
async get<Value>(key: string | string[], options?: {raw: boolean}): Promise<StoredDataNoRaw<Value> | Array<StoredDataNoRaw<Value>> | StoredDataRaw<Value> | Array<StoredDataRaw<Value>>> {
const {store} = this.opts;
const isArray = Array.isArray(key);
const keyPrefixed = isArray ? this._getKeyPrefixArray(key) : this._getKeyPrefix(key);
const isDataExpired = (data: DeserializedData<Value>): boolean => typeof data.expires === 'number' && Date.now() > data.expires;
if (isArray) {
this.hooks.trigger(KeyvHooks.PRE_GET_MANY, {keys: keyPrefixed});
if (store.getMany === undefined) {
const promises = (keyPrefixed as string[]).map(async key => {
const rawData = await store.get<Value>(key);
const deserializedRow = (typeof rawData === 'string' || this.opts.compression) ? await this.opts.deserialize!<Value>(rawData as string) : rawData;
if (deserializedRow === undefined || deserializedRow === null) {
return undefined;
}
if (isDataExpired(deserializedRow as DeserializedData<Value>)) {
await this.delete(key);
return undefined;
}
return (options && options.raw) ? deserializedRow as StoredDataRaw<Value> : (deserializedRow as DeserializedData<Value>).value as StoredDataNoRaw<Value>;
});
const deserializedRows = await Promise.allSettled(promises);
const result = deserializedRows.map(row => (row as PromiseFulfilledResult<any>).value);
this.hooks.trigger(KeyvHooks.POST_GET_MANY, result);
if (result.length > 0) {
this.stats.hit();
}
return result;
}
const rawData = await store.getMany<Value>(keyPrefixed as string[]);
const result = [];
for (const index in rawData) {
let row = rawData[index];
if ((typeof row === 'string')) {
row = await this.opts.deserialize!<Value>(row);
}
if (row === undefined || row === null) {
result.push(undefined);
continue;
}
if (isDataExpired(row as DeserializedData<Value>)) {
await this.delete(key[index]);
result.push(undefined);
continue;
}
const value = (options && options.raw) ? row as StoredDataRaw<Value> : (row as DeserializedData<Value>).value as StoredDataNoRaw<Value>;
result.push(value);
}
this.hooks.trigger(KeyvHooks.POST_GET_MANY, result);
if (result.length > 0) {
this.stats.hit();
}
return result as (Array<StoredDataNoRaw<Value>> | Array<StoredDataRaw<Value>>);
}
this.hooks.trigger(KeyvHooks.PRE_GET, {key: keyPrefixed});
const rawData = await store.get<Value>(keyPrefixed as string);
const deserializedData = (typeof rawData === 'string' || this.opts.compression) ? await this.opts.deserialize!<Value>(rawData as string) : rawData;
if (deserializedData === undefined || deserializedData === null) {
this.stats.miss();
return undefined;
}
if (isDataExpired(deserializedData as DeserializedData<Value>)) {
await this.delete(key);
this.stats.miss();
return undefined;
}
this.hooks.trigger(KeyvHooks.POST_GET, {key: keyPrefixed, value: deserializedData});
this.stats.hit();
return (options && options.raw) ? deserializedData : (deserializedData as DeserializedData<Value>).value;
}
async set(key: string, value: any, ttl?: number): Promise<boolean> {
this.hooks.trigger(KeyvHooks.PRE_SET, {key, value, ttl});
const keyPrefixed = this._getKeyPrefix(key);
if (typeof ttl === 'undefined') {
ttl = this.opts.ttl;
}
if (ttl === 0) {
ttl = undefined;
}
const {store} = this.opts;
const expires = (typeof ttl === 'number') ? (Date.now() + ttl) : null;
if (typeof value === 'symbol') {
this.emit('error', 'symbol cannot be serialized');
}
value = {value, expires};
value = await this.opts.serialize!(value);
await store.set(keyPrefixed, value, ttl);
this.hooks.trigger(KeyvHooks.POST_SET, {key: keyPrefixed, value, ttl});
this.stats.set();
return true;
}
async delete(key: string | string[]): Promise<boolean> {
const {store} = this.opts;
if (Array.isArray(key)) {
const keyPrefixed = this._getKeyPrefixArray(key);
this.hooks.trigger(KeyvHooks.PRE_DELETE, {key: keyPrefixed});
if (store.deleteMany !== undefined) {
return store.deleteMany(keyPrefixed);
}
const promises = keyPrefixed.map(async key => store.delete(key));
const results = await Promise.allSettled(promises);
const returnResult = results.every(x => (x as PromiseFulfilledResult<any>).value === true);
this.hooks.trigger(KeyvHooks.POST_DELETE, returnResult);
return returnResult;
}
const keyPrefixed = this._getKeyPrefix(key);
const result = store.delete(keyPrefixed);
this.hooks.trigger(KeyvHooks.POST_DELETE, result);
this.stats.delete();
return result;
}
async clear(): Promise<void> {
this.emit('clear');
const {store} = this.opts;
await store.clear();
}
async has(key: string): Promise<boolean> {
const keyPrefixed = this._getKeyPrefix(key);
const {store} = this.opts;
if (store.has !== undefined && !(store instanceof Map)) {
return store.has(keyPrefixed);
}
const rawData = await store.get(keyPrefixed) as any;
if (rawData) {
const data = this.opts.deserialize!(rawData) as any;
if (data) {
if (data.expires === undefined || data.expires === null) {
return true;
}
return data.expires > Date.now();
}
}
return false;
}
async disconnect(): Promise<void> {
const {store} = this.opts;
this.emit('disconnect');
if (typeof store.disconnect === 'function') {
return store.disconnect();
}
}
}
export default Keyv;
module.exports = Keyv;
module.exports.KeyvHooks = KeyvHooks;