@sphereon/ssi-sdk.kv-store-temp 0.33.1-next.3 → 0.33.1-next.68
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +709 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +324 -0
- package/dist/index.d.ts +321 -8
- package/dist/index.js +676 -27
- package/dist/index.js.map +1 -1
- package/package.json +25 -12
- package/src/__tests__/keyv.test.ts +3 -3
- package/src/__tests__/kvstore.test.ts +1 -0
- package/src/store-adapters/typeorm/entities/keyValueStoreEntity.ts +3 -5
- package/src/store-adapters/typeorm/index.ts +1 -3
- package/dist/index.d.ts.map +0 -1
- package/dist/key-value-store.d.ts +0 -27
- package/dist/key-value-store.d.ts.map +0 -1
- package/dist/key-value-store.js +0 -123
- package/dist/key-value-store.js.map +0 -1
- package/dist/key-value-types.d.ts +0 -118
- package/dist/key-value-types.d.ts.map +0 -1
- package/dist/key-value-types.js +0 -3
- package/dist/key-value-types.js.map +0 -1
- package/dist/keyv/keyv-types.d.ts +0 -56
- package/dist/keyv/keyv-types.d.ts.map +0 -1
- package/dist/keyv/keyv-types.js +0 -3
- package/dist/keyv/keyv-types.js.map +0 -1
- package/dist/keyv/keyv.d.ts +0 -48
- package/dist/keyv/keyv.d.ts.map +0 -1
- package/dist/keyv/keyv.js +0 -302
- package/dist/keyv/keyv.js.map +0 -1
- package/dist/store-adapters/index.d.ts +0 -3
- package/dist/store-adapters/index.d.ts.map +0 -1
- package/dist/store-adapters/index.js +0 -19
- package/dist/store-adapters/index.js.map +0 -1
- package/dist/store-adapters/tiered/index.d.ts +0 -29
- package/dist/store-adapters/tiered/index.d.ts.map +0 -1
- package/dist/store-adapters/tiered/index.js +0 -198
- package/dist/store-adapters/tiered/index.js.map +0 -1
- package/dist/store-adapters/tiered/types.d.ts +0 -14
- package/dist/store-adapters/tiered/types.d.ts.map +0 -1
- package/dist/store-adapters/tiered/types.js +0 -3
- package/dist/store-adapters/tiered/types.js.map +0 -1
- package/dist/store-adapters/typeorm/entities/keyValueStoreEntity.d.ts +0 -11
- package/dist/store-adapters/typeorm/entities/keyValueStoreEntity.d.ts.map +0 -1
- package/dist/store-adapters/typeorm/entities/keyValueStoreEntity.js +0 -38
- package/dist/store-adapters/typeorm/entities/keyValueStoreEntity.js.map +0 -1
- package/dist/store-adapters/typeorm/index.d.ts +0 -39
- package/dist/store-adapters/typeorm/index.d.ts.map +0 -1
- package/dist/store-adapters/typeorm/index.js +0 -141
- package/dist/store-adapters/typeorm/index.js.map +0 -1
- package/dist/store-adapters/typeorm/migrations/1.createKVDatabase.d.ts +0 -14
- package/dist/store-adapters/typeorm/migrations/1.createKVDatabase.d.ts.map +0 -1
- package/dist/store-adapters/typeorm/migrations/1.createKVDatabase.js +0 -59
- package/dist/store-adapters/typeorm/migrations/1.createKVDatabase.js.map +0 -1
- package/dist/store-adapters/typeorm/migrations/index.d.ts +0 -10
- package/dist/store-adapters/typeorm/migrations/index.d.ts.map +0 -1
- package/dist/store-adapters/typeorm/migrations/index.js +0 -13
- package/dist/store-adapters/typeorm/migrations/index.js.map +0 -1
- package/dist/store-adapters/typeorm/types.d.ts +0 -22
- package/dist/store-adapters/typeorm/types.d.ts.map +0 -1
- package/dist/store-adapters/typeorm/types.js +0 -3
- package/dist/store-adapters/typeorm/types.js.map +0 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,709 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var index_exports = {};
|
|
33
|
+
__export(index_exports, {
|
|
34
|
+
KeyValueStore: () => KeyValueStore,
|
|
35
|
+
KeyValueStoreEntity: () => KeyValueStoreEntity,
|
|
36
|
+
KeyValueTieredStoreAdapter: () => KeyValueTieredStoreAdapter,
|
|
37
|
+
KeyValueTypeORMStoreAdapter: () => KeyValueTypeORMStoreAdapter,
|
|
38
|
+
_getConnectedDb: () => _getConnectedDb,
|
|
39
|
+
kvStoreMigrations: () => kvStoreMigrations
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(index_exports);
|
|
42
|
+
|
|
43
|
+
// src/keyv/keyv.ts
|
|
44
|
+
var import_events = require("events");
|
|
45
|
+
var import_json_buffer = __toESM(require("json-buffer"), 1);
|
|
46
|
+
var Keyv = class extends import_events.EventEmitter {
|
|
47
|
+
static {
|
|
48
|
+
__name(this, "Keyv");
|
|
49
|
+
}
|
|
50
|
+
opts;
|
|
51
|
+
namespace;
|
|
52
|
+
iterator;
|
|
53
|
+
constructor(uri, options) {
|
|
54
|
+
super();
|
|
55
|
+
const emitErrors = options?.emitErrors === void 0 ? true : options.emitErrors;
|
|
56
|
+
uri = uri ?? options?.uri;
|
|
57
|
+
this.opts = {
|
|
58
|
+
namespace: "keyv",
|
|
59
|
+
serialize: import_json_buffer.default.stringify,
|
|
60
|
+
deserialize: import_json_buffer.default.parse,
|
|
61
|
+
...typeof uri === "string" ? {
|
|
62
|
+
uri
|
|
63
|
+
} : uri,
|
|
64
|
+
...options
|
|
65
|
+
};
|
|
66
|
+
if (!this.opts.store) {
|
|
67
|
+
if (typeof uri !== "string") {
|
|
68
|
+
this.opts.store = uri;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (!this.opts.store) {
|
|
72
|
+
throw Error("No store");
|
|
73
|
+
}
|
|
74
|
+
if (this.opts.compression) {
|
|
75
|
+
const compression = this.opts.compression;
|
|
76
|
+
this.opts.serialize = compression.serialize.bind(compression);
|
|
77
|
+
this.opts.deserialize = compression.deserialize.bind(compression);
|
|
78
|
+
}
|
|
79
|
+
if (typeof this.opts.store.on === "function" && emitErrors) {
|
|
80
|
+
this.opts.store.on("error", (error) => this.emit("error", error));
|
|
81
|
+
}
|
|
82
|
+
this.opts.store.namespace = this.opts.namespace || "keyv";
|
|
83
|
+
this.namespace = this.opts.store.namespace;
|
|
84
|
+
const generateIterator = /* @__PURE__ */ __name((iterator, keyv) => async function* () {
|
|
85
|
+
for await (const [key, raw] of typeof iterator === "function" ? iterator(keyv.store.namespace) : iterator) {
|
|
86
|
+
const data = await keyv.deserialize(raw);
|
|
87
|
+
if (keyv.store.namespace && !key.includes(keyv.store.namespace)) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (data && typeof data.expires === "number" && Date.now() > data.expires) {
|
|
91
|
+
keyv.delete(key);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
yield [
|
|
95
|
+
keyv._getKeyUnprefix(key),
|
|
96
|
+
data?.value
|
|
97
|
+
];
|
|
98
|
+
}
|
|
99
|
+
}, "generateIterator");
|
|
100
|
+
if (typeof this.store[Symbol.iterator] === "function" && this.store instanceof Map) {
|
|
101
|
+
this.iterator = generateIterator(this.store, this);
|
|
102
|
+
} else if (typeof this.store.iterator === "function" && this.store.opts && this._checkIterableAdapter()) {
|
|
103
|
+
this.iterator = generateIterator(this.store.iterator.bind(this.store), this);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
get store() {
|
|
107
|
+
return this.opts.store;
|
|
108
|
+
}
|
|
109
|
+
get deserialize() {
|
|
110
|
+
return this.opts.deserialize;
|
|
111
|
+
}
|
|
112
|
+
get serialize() {
|
|
113
|
+
return this.opts.serialize;
|
|
114
|
+
}
|
|
115
|
+
_checkIterableAdapter() {
|
|
116
|
+
return this.store.opts.dialect && iterableAdapters.includes(this.store.opts.dialect) || this.store.opts.url && iterableAdapters.findIndex((element) => this.store.opts.url.includes(element)) >= 0;
|
|
117
|
+
}
|
|
118
|
+
_getKeyPrefix(key) {
|
|
119
|
+
return `${this.opts.namespace}:${key}`;
|
|
120
|
+
}
|
|
121
|
+
_getKeyPrefixArray(keys) {
|
|
122
|
+
return keys.map((key) => this._getKeyPrefix(key));
|
|
123
|
+
}
|
|
124
|
+
_getKeyUnprefix(key) {
|
|
125
|
+
return key.split(":").splice(1).join(":");
|
|
126
|
+
}
|
|
127
|
+
async getMany(keys, options) {
|
|
128
|
+
const keyPrefixed = this._getKeyPrefixArray(keys);
|
|
129
|
+
let promise;
|
|
130
|
+
if (this.store.getMany !== void 0) {
|
|
131
|
+
promise = this.store.getMany(keyPrefixed, options);
|
|
132
|
+
} else if (this.isMapWithEntries(this.store)) {
|
|
133
|
+
promise = this.getFromMapWithPrefix(keyPrefixed);
|
|
134
|
+
} else {
|
|
135
|
+
promise = Promise.all(keyPrefixed.map((k) => this.store.get(k, options)));
|
|
136
|
+
}
|
|
137
|
+
const allValues = Promise.resolve(promise);
|
|
138
|
+
const results = [];
|
|
139
|
+
return Promise.resolve(allValues).then((all) => {
|
|
140
|
+
const entries = Array.isArray(all) ? all : [
|
|
141
|
+
all
|
|
142
|
+
];
|
|
143
|
+
entries.forEach((data, index) => {
|
|
144
|
+
let result = typeof data === "string" ? this.deserialize(data) : !!data && this.opts.compression ? this.deserialize(data) : data;
|
|
145
|
+
if (result && typeof result === "object" && "expires" in result && typeof result.expires === "number" && Date.now() > result.expires) {
|
|
146
|
+
this.delete(keys[index]);
|
|
147
|
+
result = void 0;
|
|
148
|
+
}
|
|
149
|
+
const final = options && options.raw ? result : result && typeof result === "object" && "value" in result ? result.value : result;
|
|
150
|
+
results.push(final);
|
|
151
|
+
});
|
|
152
|
+
}).then(() => Promise.all(results));
|
|
153
|
+
}
|
|
154
|
+
isMapWithEntries(store) {
|
|
155
|
+
return store instanceof Map && typeof store.entries === "function";
|
|
156
|
+
}
|
|
157
|
+
async getFromMapWithPrefix(prefixes) {
|
|
158
|
+
if (!this.isMapWithEntries(this.store)) {
|
|
159
|
+
return [];
|
|
160
|
+
}
|
|
161
|
+
const map = this.store;
|
|
162
|
+
return prefixes.flatMap((prefix) => {
|
|
163
|
+
const matchedValues = Array.from(map.entries()).filter(([key]) => key.startsWith(prefix)).map(([, value]) => value);
|
|
164
|
+
return matchedValues.length > 0 ? matchedValues : [
|
|
165
|
+
void 0
|
|
166
|
+
];
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
async get(key, options) {
|
|
170
|
+
const isArray = Array.isArray(key);
|
|
171
|
+
return Promise.resolve().then(() => isArray ? this.getMany(this._getKeyPrefixArray(key), options) : this.store.get(this._getKeyPrefix(key))).then((data) => typeof data === "string" ? this.deserialize(data) : this.opts.compression ? this.deserialize(data) : data).then((data) => {
|
|
172
|
+
if (data === void 0 || data === null) {
|
|
173
|
+
return void 0;
|
|
174
|
+
}
|
|
175
|
+
const rows = Array.isArray(data) ? data : [
|
|
176
|
+
data
|
|
177
|
+
];
|
|
178
|
+
if (isArray) {
|
|
179
|
+
const result = [];
|
|
180
|
+
for (let row of rows) {
|
|
181
|
+
if (row === void 0 || row === null) {
|
|
182
|
+
result.push(void 0);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (this.isExpired(row)) {
|
|
186
|
+
this.delete(key).then(() => void 0);
|
|
187
|
+
result.push(void 0);
|
|
188
|
+
} else {
|
|
189
|
+
result.push(options && options.raw ? row : toValue(row));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
} else if (!Array.isArray(data)) {
|
|
194
|
+
if (this.isExpired(data)) {
|
|
195
|
+
return this.delete(key).then(() => void 0);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return options && options.raw ? data : Array.isArray(data) ? data.map((d) => toValue(d)) : toValue(data);
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
isExpired(data) {
|
|
202
|
+
return typeof data !== "string" && data && typeof data === "object" && "expires" in data && typeof data.expires === "number" && Date.now() > data.expires;
|
|
203
|
+
}
|
|
204
|
+
set(key, value, ttl) {
|
|
205
|
+
const keyPrefixed = this._getKeyPrefix(key);
|
|
206
|
+
if (typeof ttl === "undefined") {
|
|
207
|
+
ttl = this.opts.ttl;
|
|
208
|
+
}
|
|
209
|
+
if (ttl === 0) {
|
|
210
|
+
ttl = void 0;
|
|
211
|
+
}
|
|
212
|
+
return Promise.resolve().then(() => {
|
|
213
|
+
const expires = typeof ttl === "number" ? Date.now() + ttl : void 0;
|
|
214
|
+
if (typeof value === "symbol") {
|
|
215
|
+
this.emit("error", "symbol cannot be serialized");
|
|
216
|
+
}
|
|
217
|
+
const input = {
|
|
218
|
+
value,
|
|
219
|
+
expires
|
|
220
|
+
};
|
|
221
|
+
return this.serialize(input);
|
|
222
|
+
}).then((value2) => this.store.set(keyPrefixed, value2, ttl)).then(() => true);
|
|
223
|
+
}
|
|
224
|
+
delete(key) {
|
|
225
|
+
if (!Array.isArray(key)) {
|
|
226
|
+
const keyPrefixed2 = this._getKeyPrefix(key);
|
|
227
|
+
return Promise.resolve().then(() => this.store.delete(keyPrefixed2));
|
|
228
|
+
}
|
|
229
|
+
const keyPrefixed = this._getKeyPrefixArray(key);
|
|
230
|
+
if (this.store.deleteMany !== void 0) {
|
|
231
|
+
return Promise.resolve().then(() => this.store.deleteMany(keyPrefixed));
|
|
232
|
+
}
|
|
233
|
+
const promises = [];
|
|
234
|
+
for (const key2 of keyPrefixed) {
|
|
235
|
+
promises.push(this.store.delete(key2));
|
|
236
|
+
}
|
|
237
|
+
return Promise.allSettled(promises).then((values) => values.every((x) => x.valueOf() === true));
|
|
238
|
+
}
|
|
239
|
+
async clear() {
|
|
240
|
+
return Promise.resolve().then(() => this.store.clear());
|
|
241
|
+
}
|
|
242
|
+
has(key) {
|
|
243
|
+
const keyPrefixed = this._getKeyPrefix(key);
|
|
244
|
+
return Promise.resolve().then(async () => {
|
|
245
|
+
if (typeof this.store.has === "function") {
|
|
246
|
+
return this.store.has(keyPrefixed);
|
|
247
|
+
}
|
|
248
|
+
const value = await this.store.get(keyPrefixed);
|
|
249
|
+
return value !== void 0;
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
disconnect() {
|
|
253
|
+
if (typeof this.store.disconnect === "function") {
|
|
254
|
+
return this.store.disconnect();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
var iterableAdapters = [
|
|
259
|
+
"sqlite",
|
|
260
|
+
"postgres",
|
|
261
|
+
"mysql",
|
|
262
|
+
"mongo",
|
|
263
|
+
"redis",
|
|
264
|
+
"tiered"
|
|
265
|
+
];
|
|
266
|
+
function toValue(input) {
|
|
267
|
+
return input !== null && typeof input === "object" && "value" in input ? input.value : input;
|
|
268
|
+
}
|
|
269
|
+
__name(toValue, "toValue");
|
|
270
|
+
|
|
271
|
+
// src/key-value-store.ts
|
|
272
|
+
var KeyValueStore = class {
|
|
273
|
+
static {
|
|
274
|
+
__name(this, "KeyValueStore");
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* The main keyv typescript port which delegates to the storage adapters and takes care of some common functionality
|
|
278
|
+
*
|
|
279
|
+
* @internal
|
|
280
|
+
*/
|
|
281
|
+
keyv;
|
|
282
|
+
constructor(options) {
|
|
283
|
+
this.keyv = new Keyv(options.uri, options);
|
|
284
|
+
}
|
|
285
|
+
async get(key) {
|
|
286
|
+
const result = await this.keyv.get(key, {
|
|
287
|
+
raw: false
|
|
288
|
+
});
|
|
289
|
+
if (result === null || result === void 0) {
|
|
290
|
+
return void 0;
|
|
291
|
+
}
|
|
292
|
+
return result;
|
|
293
|
+
}
|
|
294
|
+
async getAsValueData(key) {
|
|
295
|
+
const result = await this.keyv.get(key, {
|
|
296
|
+
raw: true
|
|
297
|
+
});
|
|
298
|
+
if (result === null || result === void 0) {
|
|
299
|
+
return {
|
|
300
|
+
value: void 0,
|
|
301
|
+
expires: void 0
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
return this.toDeserializedValueData(result);
|
|
305
|
+
}
|
|
306
|
+
async getMany(keys) {
|
|
307
|
+
if (!keys || keys.length === 0) {
|
|
308
|
+
return [];
|
|
309
|
+
}
|
|
310
|
+
let result = await this.keyv.getMany(keys, {
|
|
311
|
+
raw: false
|
|
312
|
+
});
|
|
313
|
+
if (result === null || result === void 0 || result.length === 0) {
|
|
314
|
+
result = new Array();
|
|
315
|
+
keys.forEach(() => result.push(void 0));
|
|
316
|
+
}
|
|
317
|
+
return result.map((v) => !!v ? v : void 0);
|
|
318
|
+
}
|
|
319
|
+
async getManyAsValueData(keys) {
|
|
320
|
+
if (!keys || keys.length === 0) {
|
|
321
|
+
return [];
|
|
322
|
+
}
|
|
323
|
+
let result = await this.keyv.getMany(keys, {
|
|
324
|
+
raw: true
|
|
325
|
+
});
|
|
326
|
+
if (result === null || result === void 0 || result.length === 0) {
|
|
327
|
+
result = new Array();
|
|
328
|
+
keys.forEach(() => result.push({
|
|
329
|
+
value: void 0,
|
|
330
|
+
expires: void 0
|
|
331
|
+
}));
|
|
332
|
+
}
|
|
333
|
+
return result.map((v) => !!v ? this.toDeserializedValueData(v) : {
|
|
334
|
+
value: void 0,
|
|
335
|
+
expires: void 0
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
async set(key, value, ttl) {
|
|
339
|
+
return this.keyv.set(key, value, ttl).then(() => this.getAsValueData(key));
|
|
340
|
+
}
|
|
341
|
+
async has(key) {
|
|
342
|
+
return this.keyv.has(key);
|
|
343
|
+
}
|
|
344
|
+
async delete(key) {
|
|
345
|
+
return this.keyv.delete(key);
|
|
346
|
+
}
|
|
347
|
+
async deleteMany(keys) {
|
|
348
|
+
return Promise.all(keys.map((key) => this.keyv.delete(key)));
|
|
349
|
+
}
|
|
350
|
+
async clear() {
|
|
351
|
+
return this.keyv.clear().then(() => this);
|
|
352
|
+
}
|
|
353
|
+
async disconnect() {
|
|
354
|
+
return this.keyv.disconnect();
|
|
355
|
+
}
|
|
356
|
+
// Public so parties using the kv store directly can add listeners if they want
|
|
357
|
+
async kvStoreOn(args) {
|
|
358
|
+
this.keyv.on(args.eventName, args.listener);
|
|
359
|
+
return this;
|
|
360
|
+
}
|
|
361
|
+
toDeserializedValueData(result) {
|
|
362
|
+
if (result === null || result === void 0) {
|
|
363
|
+
throw Error(`Result cannot be undefined or null at this this point`);
|
|
364
|
+
} else if (typeof result !== "object") {
|
|
365
|
+
return {
|
|
366
|
+
value: result,
|
|
367
|
+
expires: void 0
|
|
368
|
+
};
|
|
369
|
+
} else if (!("value" in result)) {
|
|
370
|
+
return {
|
|
371
|
+
value: result,
|
|
372
|
+
expires: void 0
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
if (!("expires" in result)) {
|
|
376
|
+
result.expires = void 0;
|
|
377
|
+
}
|
|
378
|
+
return result;
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
// src/store-adapters/tiered/index.ts
|
|
383
|
+
var import_events2 = require("events");
|
|
384
|
+
var KeyValueTieredStoreAdapter = class extends import_events2.EventEmitter {
|
|
385
|
+
static {
|
|
386
|
+
__name(this, "KeyValueTieredStoreAdapter");
|
|
387
|
+
}
|
|
388
|
+
opts;
|
|
389
|
+
remote;
|
|
390
|
+
local;
|
|
391
|
+
iterationLimit;
|
|
392
|
+
namespace;
|
|
393
|
+
constructor({ remote, local, ...options }) {
|
|
394
|
+
super();
|
|
395
|
+
this.opts = {
|
|
396
|
+
validator: /* @__PURE__ */ __name(() => true, "validator"),
|
|
397
|
+
dialect: "tiered",
|
|
398
|
+
...options
|
|
399
|
+
};
|
|
400
|
+
this.local = isMap(local) ? new Keyv(local) : local;
|
|
401
|
+
this.remote = isMap(remote) ? new Keyv(remote) : remote;
|
|
402
|
+
this.namespace = this.local.namespace;
|
|
403
|
+
}
|
|
404
|
+
async get(key, options) {
|
|
405
|
+
if (Array.isArray(key)) {
|
|
406
|
+
return await this.getMany(key, options);
|
|
407
|
+
}
|
|
408
|
+
const localResult = await this.local.get(key, options);
|
|
409
|
+
if (localResult === void 0 || !this.opts.validator(localResult, key)) {
|
|
410
|
+
const remoteResult = await this.remote.get(key, options);
|
|
411
|
+
if (remoteResult !== localResult) {
|
|
412
|
+
const value = !!remoteResult ? typeof remoteResult === "object" && "value" in remoteResult ? remoteResult.value : remoteResult : void 0;
|
|
413
|
+
const ttl = !!remoteResult && typeof remoteResult === "object" && "expires" in remoteResult && remoteResult.expires ? remoteResult.expires - Date.now() : void 0;
|
|
414
|
+
if (!ttl || ttl > 0) {
|
|
415
|
+
await this.local.set(key, value, ttl);
|
|
416
|
+
} else {
|
|
417
|
+
this.local.delete(key);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return remoteResult;
|
|
421
|
+
}
|
|
422
|
+
return localResult;
|
|
423
|
+
}
|
|
424
|
+
async getMany(keys, options) {
|
|
425
|
+
const promises = [];
|
|
426
|
+
for (const key of keys) {
|
|
427
|
+
promises.push(this.get(key, options));
|
|
428
|
+
}
|
|
429
|
+
const values = await Promise.all(promises);
|
|
430
|
+
const data = [];
|
|
431
|
+
for (const value of values) {
|
|
432
|
+
data.push(value);
|
|
433
|
+
}
|
|
434
|
+
return data;
|
|
435
|
+
}
|
|
436
|
+
async set(key, value, ttl) {
|
|
437
|
+
const toSet = [
|
|
438
|
+
"local",
|
|
439
|
+
"remote"
|
|
440
|
+
];
|
|
441
|
+
return Promise.all(toSet.map(async (store) => this[store].set(key, value, ttl)));
|
|
442
|
+
}
|
|
443
|
+
async clear() {
|
|
444
|
+
const toClear = [
|
|
445
|
+
"local"
|
|
446
|
+
];
|
|
447
|
+
if (!this.opts.localOnly) {
|
|
448
|
+
toClear.push("remote");
|
|
449
|
+
}
|
|
450
|
+
await Promise.all(toClear.map(async (store) => this[store].clear()));
|
|
451
|
+
return void 0;
|
|
452
|
+
}
|
|
453
|
+
async delete(key) {
|
|
454
|
+
const toDelete = [
|
|
455
|
+
"local"
|
|
456
|
+
];
|
|
457
|
+
if (!this.opts.localOnly) {
|
|
458
|
+
toDelete.push("remote");
|
|
459
|
+
}
|
|
460
|
+
const deleted = await Promise.all(toDelete.map(async (store) => this[store].delete(key)));
|
|
461
|
+
return deleted.every(Boolean);
|
|
462
|
+
}
|
|
463
|
+
async deleteMany(keys) {
|
|
464
|
+
const promises = [];
|
|
465
|
+
for (const key of keys) {
|
|
466
|
+
promises.push(this.delete(key));
|
|
467
|
+
}
|
|
468
|
+
const values = await Promise.all(promises);
|
|
469
|
+
return values.every(Boolean);
|
|
470
|
+
}
|
|
471
|
+
async has(key) {
|
|
472
|
+
let response;
|
|
473
|
+
if (typeof this.local.has === "function") {
|
|
474
|
+
response = this.local.has(key);
|
|
475
|
+
} else {
|
|
476
|
+
const value = await this.local.get(key);
|
|
477
|
+
response = value !== void 0;
|
|
478
|
+
}
|
|
479
|
+
if (!response || !this.opts.validator(response, key)) {
|
|
480
|
+
if (typeof this.remote.has === "function") {
|
|
481
|
+
response = this.remote.has(key);
|
|
482
|
+
} else {
|
|
483
|
+
const value = await this.remote.get(key);
|
|
484
|
+
response = value !== void 0;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
return response;
|
|
488
|
+
}
|
|
489
|
+
async *iterator(namespace) {
|
|
490
|
+
const limit = Number.parseInt(this.iterationLimit, 10) || 10;
|
|
491
|
+
this.remote.opts.iterationLimit = limit;
|
|
492
|
+
if (this.remote && typeof this.remote.iterator === "function") {
|
|
493
|
+
for await (const entries of this.remote.iterator(namespace)) {
|
|
494
|
+
yield entries;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
function isMap(map) {
|
|
500
|
+
if (map instanceof Map) {
|
|
501
|
+
return true;
|
|
502
|
+
} else if (map && typeof map.clear === "function" && typeof map.delete === "function" && typeof map.get === "function" && typeof map.has === "function" && typeof map.set === "function") {
|
|
503
|
+
return true;
|
|
504
|
+
}
|
|
505
|
+
return false;
|
|
506
|
+
}
|
|
507
|
+
__name(isMap, "isMap");
|
|
508
|
+
|
|
509
|
+
// src/store-adapters/typeorm/index.ts
|
|
510
|
+
var import_events3 = require("events");
|
|
511
|
+
var import_typeorm3 = require("typeorm");
|
|
512
|
+
|
|
513
|
+
// src/store-adapters/typeorm/entities/keyValueStoreEntity.ts
|
|
514
|
+
var import_typeorm = require("typeorm");
|
|
515
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
516
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
517
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
518
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
519
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
520
|
+
}
|
|
521
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
522
|
+
function _ts_metadata(k, v) {
|
|
523
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
524
|
+
}
|
|
525
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
526
|
+
var KeyValueStoreEntity = class extends import_typeorm.BaseEntity {
|
|
527
|
+
static {
|
|
528
|
+
__name(this, "KeyValueStoreEntity");
|
|
529
|
+
}
|
|
530
|
+
key;
|
|
531
|
+
data;
|
|
532
|
+
expires;
|
|
533
|
+
};
|
|
534
|
+
_ts_decorate([
|
|
535
|
+
(0, import_typeorm.PrimaryColumn)("text"),
|
|
536
|
+
_ts_metadata("design:type", String)
|
|
537
|
+
], KeyValueStoreEntity.prototype, "key", void 0);
|
|
538
|
+
_ts_decorate([
|
|
539
|
+
(0, import_typeorm.Column)({
|
|
540
|
+
type: "text"
|
|
541
|
+
}),
|
|
542
|
+
_ts_metadata("design:type", String)
|
|
543
|
+
], KeyValueStoreEntity.prototype, "data", void 0);
|
|
544
|
+
KeyValueStoreEntity = _ts_decorate([
|
|
545
|
+
(0, import_typeorm.Entity)("keyvaluestore")
|
|
546
|
+
], KeyValueStoreEntity);
|
|
547
|
+
|
|
548
|
+
// src/store-adapters/typeorm/index.ts
|
|
549
|
+
var import_json_buffer2 = __toESM(require("json-buffer"), 1);
|
|
550
|
+
|
|
551
|
+
// src/store-adapters/typeorm/migrations/1.createKVDatabase.ts
|
|
552
|
+
var import_typeorm2 = require("typeorm");
|
|
553
|
+
var import_debug = __toESM(require("debug"), 1);
|
|
554
|
+
var debug = (0, import_debug.default)("veramo:kv-store:initial-migration");
|
|
555
|
+
var CreateKVDatabaseMigration = class {
|
|
556
|
+
static {
|
|
557
|
+
__name(this, "CreateKVDatabaseMigration");
|
|
558
|
+
}
|
|
559
|
+
_tableName;
|
|
560
|
+
name;
|
|
561
|
+
constructor(tableName) {
|
|
562
|
+
this._tableName = tableName || "keyvaluestore";
|
|
563
|
+
this.name = `CreateKVDatabase${tableName}1680297189001`;
|
|
564
|
+
}
|
|
565
|
+
async up(queryRunner) {
|
|
566
|
+
function getTableName(givenName) {
|
|
567
|
+
return queryRunner.connection.entityMetadatas.find((meta) => meta.givenTableName === givenName)?.tableName || givenName;
|
|
568
|
+
}
|
|
569
|
+
__name(getTableName, "getTableName");
|
|
570
|
+
debug(`creating ${this._tableName} table`);
|
|
571
|
+
await queryRunner.createTable(new import_typeorm2.Table({
|
|
572
|
+
name: getTableName(this._tableName),
|
|
573
|
+
columns: [
|
|
574
|
+
{
|
|
575
|
+
name: "key",
|
|
576
|
+
type: "varchar",
|
|
577
|
+
isPrimary: true
|
|
578
|
+
},
|
|
579
|
+
{
|
|
580
|
+
name: "data",
|
|
581
|
+
type: "text",
|
|
582
|
+
isNullable: false
|
|
583
|
+
}
|
|
584
|
+
],
|
|
585
|
+
indices: [
|
|
586
|
+
{
|
|
587
|
+
columnNames: [
|
|
588
|
+
"key"
|
|
589
|
+
],
|
|
590
|
+
isUnique: true
|
|
591
|
+
}
|
|
592
|
+
]
|
|
593
|
+
}), true);
|
|
594
|
+
}
|
|
595
|
+
async down(queryRunner) {
|
|
596
|
+
throw new Error("illegal_operation: cannot roll back initial migration");
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
// src/store-adapters/typeorm/migrations/index.ts
|
|
601
|
+
var kvStoreMigrations = [
|
|
602
|
+
CreateKVDatabaseMigration
|
|
603
|
+
];
|
|
604
|
+
|
|
605
|
+
// src/store-adapters/typeorm/index.ts
|
|
606
|
+
var KeyValueTypeORMStoreAdapter = class extends import_events3.EventEmitter {
|
|
607
|
+
static {
|
|
608
|
+
__name(this, "KeyValueTypeORMStoreAdapter");
|
|
609
|
+
}
|
|
610
|
+
dbConnection;
|
|
611
|
+
namespace;
|
|
612
|
+
opts;
|
|
613
|
+
constructor(options) {
|
|
614
|
+
super();
|
|
615
|
+
this.dbConnection = options.dbConnection;
|
|
616
|
+
this.namespace = options.namespace || "keyv";
|
|
617
|
+
this.opts = {
|
|
618
|
+
validator: /* @__PURE__ */ __name(() => true, "validator"),
|
|
619
|
+
dialect: "typeorm",
|
|
620
|
+
serialize: import_json_buffer2.default.stringify,
|
|
621
|
+
deserialize: import_json_buffer2.default.parse,
|
|
622
|
+
...options
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
async get(key, options) {
|
|
626
|
+
if (Array.isArray(key)) {
|
|
627
|
+
return this.getMany(key, options);
|
|
628
|
+
}
|
|
629
|
+
const connection = await _getConnectedDb(this.dbConnection);
|
|
630
|
+
const result = await connection.getRepository(KeyValueStoreEntity).findOneBy({
|
|
631
|
+
key
|
|
632
|
+
});
|
|
633
|
+
return options?.raw !== true || !result ? result?.data : {
|
|
634
|
+
value: result?.data,
|
|
635
|
+
expires: result?.expires
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
async getMany(keys, options) {
|
|
639
|
+
const connection = await _getConnectedDb(this.dbConnection);
|
|
640
|
+
const results = await connection.getRepository(KeyValueStoreEntity).findBy({
|
|
641
|
+
key: (0, import_typeorm3.In)(keys)
|
|
642
|
+
});
|
|
643
|
+
const values = keys.map(async (key) => {
|
|
644
|
+
const result = results.find((result2) => result2.key === key);
|
|
645
|
+
return options?.raw !== true || !result ? result?.data : {
|
|
646
|
+
value: result?.data ? (await this.opts.deserialize(result.data))?.value : void 0,
|
|
647
|
+
expires: result?.expires
|
|
648
|
+
};
|
|
649
|
+
});
|
|
650
|
+
return Promise.all(values);
|
|
651
|
+
}
|
|
652
|
+
async set(key, value, ttl) {
|
|
653
|
+
const connection = await _getConnectedDb(this.dbConnection);
|
|
654
|
+
const entity = new KeyValueStoreEntity();
|
|
655
|
+
entity.key = key;
|
|
656
|
+
entity.data = value;
|
|
657
|
+
entity.expires = ttl;
|
|
658
|
+
await connection.getRepository(KeyValueStoreEntity).save(entity);
|
|
659
|
+
return {
|
|
660
|
+
value,
|
|
661
|
+
expires: ttl
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
async delete(key) {
|
|
665
|
+
if (Array.isArray(key)) {
|
|
666
|
+
return this.deleteMany(key);
|
|
667
|
+
}
|
|
668
|
+
const connection = await _getConnectedDb(this.dbConnection);
|
|
669
|
+
const result = await connection.getRepository(KeyValueStoreEntity).delete({
|
|
670
|
+
key
|
|
671
|
+
});
|
|
672
|
+
return result.affected === 1;
|
|
673
|
+
}
|
|
674
|
+
async deleteMany(keys) {
|
|
675
|
+
const connection = await _getConnectedDb(this.dbConnection);
|
|
676
|
+
const results = await connection.getRepository(KeyValueStoreEntity).delete({
|
|
677
|
+
key: (0, import_typeorm3.In)(keys)
|
|
678
|
+
});
|
|
679
|
+
return !!results.affected && results.affected >= 1;
|
|
680
|
+
}
|
|
681
|
+
async clear() {
|
|
682
|
+
const connection = await _getConnectedDb(this.dbConnection);
|
|
683
|
+
await connection.getRepository(KeyValueStoreEntity).delete({
|
|
684
|
+
key: (0, import_typeorm3.Like)(`${this.namespace}:%`)
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
async has(key) {
|
|
688
|
+
const connection = await _getConnectedDb(this.dbConnection);
|
|
689
|
+
const result = await connection.getRepository(KeyValueStoreEntity).countBy({
|
|
690
|
+
key
|
|
691
|
+
});
|
|
692
|
+
return result === 1;
|
|
693
|
+
}
|
|
694
|
+
async disconnect() {
|
|
695
|
+
const connection = await _getConnectedDb(this.dbConnection);
|
|
696
|
+
await connection.destroy();
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
async function _getConnectedDb(dbConnection) {
|
|
700
|
+
if (dbConnection instanceof Promise) {
|
|
701
|
+
return await dbConnection;
|
|
702
|
+
} else if (!dbConnection.isInitialized) {
|
|
703
|
+
return await dbConnection.initialize();
|
|
704
|
+
} else {
|
|
705
|
+
return dbConnection;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
__name(_getConnectedDb, "_getConnectedDb");
|
|
709
|
+
//# sourceMappingURL=index.cjs.map
|