@sphereon/ssi-sdk.presentation-exchange 0.11.1-next.65
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/.yalc/@veramo/kv-store/LICENSE +201 -0
- package/.yalc/@veramo/kv-store/README.md +132 -0
- package/.yalc/@veramo/kv-store/build/index.d.ts +11 -0
- package/.yalc/@veramo/kv-store/build/index.js +45 -0
- package/.yalc/@veramo/kv-store/build/key-value-store.d.ts +27 -0
- package/.yalc/@veramo/kv-store/build/key-value-store.js +143 -0
- package/.yalc/@veramo/kv-store/build/key-value-types.d.ts +51 -0
- package/.yalc/@veramo/kv-store/build/key-value-types.js +3 -0
- package/.yalc/@veramo/kv-store/build/keyv/keyv-types.d.ts +62 -0
- package/.yalc/@veramo/kv-store/build/keyv/keyv-types.js +3 -0
- package/.yalc/@veramo/kv-store/build/keyv/keyv.d.ts +53 -0
- package/.yalc/@veramo/kv-store/build/keyv/keyv.js +385 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/index.d.ts +3 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/index.js +30 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/tiered/index.d.ts +32 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/tiered/index.js +301 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/tiered/types.d.ts +14 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/tiered/types.js +3 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/typeorm/entities/keyValueStoreEntity.d.ts +11 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/typeorm/entities/keyValueStoreEntity.js +50 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/typeorm/index.d.ts +41 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/typeorm/index.js +186 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/typeorm/migrations/1.createKVDatabase.d.ts +14 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/typeorm/migrations/1.createKVDatabase.js +90 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/typeorm/migrations/index.d.ts +10 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/typeorm/migrations/index.js +13 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/typeorm/types.d.ts +15 -0
- package/.yalc/@veramo/kv-store/build/store-adapters/typeorm/types.js +3 -0
- package/.yalc/@veramo/kv-store/build/tsdoc-metadata.json +11 -0
- package/.yalc/@veramo/kv-store/package.json +39 -0
- package/.yalc/@veramo/kv-store/src/__tests__/keyv.test.ts +204 -0
- package/.yalc/@veramo/kv-store/src/__tests__/kvstore.test.ts +93 -0
- package/.yalc/@veramo/kv-store/src/index.ts +10 -0
- package/.yalc/@veramo/kv-store/src/key-value-store.ts +109 -0
- package/.yalc/@veramo/kv-store/src/key-value-types.ts +97 -0
- package/.yalc/@veramo/kv-store/src/keyv/keyv-types.ts +71 -0
- package/.yalc/@veramo/kv-store/src/keyv/keyv.ts +282 -0
- package/.yalc/@veramo/kv-store/src/store-adapters/index.ts +2 -0
- package/.yalc/@veramo/kv-store/src/store-adapters/tiered/index.ts +158 -0
- package/.yalc/@veramo/kv-store/src/store-adapters/tiered/types.ts +16 -0
- package/.yalc/@veramo/kv-store/src/store-adapters/typeorm/entities/keyValueStoreEntity.ts +20 -0
- package/.yalc/@veramo/kv-store/src/store-adapters/typeorm/index.ts +122 -0
- package/.yalc/@veramo/kv-store/src/store-adapters/typeorm/migrations/1.createKVDatabase.ts +47 -0
- package/.yalc/@veramo/kv-store/src/store-adapters/typeorm/migrations/index.ts +10 -0
- package/.yalc/@veramo/kv-store/src/store-adapters/typeorm/types.ts +18 -0
- package/.yalc/@veramo/kv-store/yalc.sig +1 -0
- package/LICENSE +201 -0
- package/README.md +56 -0
- package/dist/agent/PresentationExchange.d.ts +25 -0
- package/dist/agent/PresentationExchange.d.ts.map +1 -0
- package/dist/agent/PresentationExchange.js +182 -0
- package/dist/agent/PresentationExchange.js.map +1 -0
- package/dist/functions.d.ts +12 -0
- package/dist/functions.d.ts.map +1 -0
- package/dist/functions.js +41 -0
- package/dist/functions.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/types/IPresentationExchange.d.ts +81 -0
- package/dist/types/IPresentationExchange.d.ts.map +1 -0
- package/dist/types/IPresentationExchange.js +3 -0
- package/dist/types/IPresentationExchange.js.map +1 -0
- package/package.json +67 -0
- package/plugin.schema.json +329 -0
- package/src/agent/PresentationExchange.ts +204 -0
- package/src/functions.ts +56 -0
- package/src/index.ts +8 -0
- package/src/types/IPresentationExchange.ts +114 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
var __awaiter =
|
|
3
|
+
(this && this.__awaiter) ||
|
|
4
|
+
function (thisArg, _arguments, P, generator) {
|
|
5
|
+
function adopt(value) {
|
|
6
|
+
return value instanceof P
|
|
7
|
+
? value
|
|
8
|
+
: new P(function (resolve) {
|
|
9
|
+
resolve(value)
|
|
10
|
+
})
|
|
11
|
+
}
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) {
|
|
14
|
+
try {
|
|
15
|
+
step(generator.next(value))
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function rejected(value) {
|
|
21
|
+
try {
|
|
22
|
+
step(generator['throw'](value))
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function step(result) {
|
|
28
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected)
|
|
29
|
+
}
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next())
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
var __importDefault =
|
|
34
|
+
(this && this.__importDefault) ||
|
|
35
|
+
function (mod) {
|
|
36
|
+
return mod && mod.__esModule ? mod : { default: mod }
|
|
37
|
+
}
|
|
38
|
+
Object.defineProperty(exports, '__esModule', { value: true })
|
|
39
|
+
exports.CreateKVDatabaseMigration = void 0
|
|
40
|
+
const typeorm_1 = require('typeorm')
|
|
41
|
+
const debug_1 = __importDefault(require('debug'))
|
|
42
|
+
const debug = (0, debug_1.default)('veramo:kv-store:initial-migration')
|
|
43
|
+
/**
|
|
44
|
+
* Create the database layout for Veramo 3.0
|
|
45
|
+
*
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
class CreateKVDatabaseMigration {
|
|
49
|
+
constructor(tableName) {
|
|
50
|
+
this._tableName = tableName || 'keyvaluestore'
|
|
51
|
+
this.name = `CreateKVDatabase${tableName}1680297189001`
|
|
52
|
+
}
|
|
53
|
+
up(queryRunner) {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
function getTableName(givenName) {
|
|
56
|
+
var _a
|
|
57
|
+
return (
|
|
58
|
+
((_a = queryRunner.connection.entityMetadatas.find((meta) => meta.givenTableName === givenName)) === null || _a === void 0
|
|
59
|
+
? void 0
|
|
60
|
+
: _a.tableName) || givenName
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
debug(`creating ${this._tableName} table`)
|
|
64
|
+
// CREATE TABLE "keyvaluestore" ("key" varchar PRIMARY KEY NOT NULL, "data" text NOT NULL)
|
|
65
|
+
yield queryRunner.createTable(
|
|
66
|
+
new typeorm_1.Table({
|
|
67
|
+
name: getTableName(this._tableName),
|
|
68
|
+
columns: [
|
|
69
|
+
{ name: 'key', type: 'varchar', isPrimary: true },
|
|
70
|
+
{ name: 'data', type: 'text', isNullable: false },
|
|
71
|
+
],
|
|
72
|
+
indices: [
|
|
73
|
+
{
|
|
74
|
+
columnNames: ['key'],
|
|
75
|
+
isUnique: true,
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
}),
|
|
79
|
+
true
|
|
80
|
+
)
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
down(queryRunner) {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
throw new Error('illegal_operation: cannot roll back initial migration')
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.CreateKVDatabaseMigration = CreateKVDatabaseMigration
|
|
90
|
+
//# sourceMappingURL=1.createKVDatabase.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CreateKVDatabaseMigration } from './1.createKVDatabase'
|
|
2
|
+
/**
|
|
3
|
+
* The migrations array that SHOULD be used when initializing a TypeORM database connection.
|
|
4
|
+
*
|
|
5
|
+
* These ensure the correct creation of tables and the proper migrations of data when tables change between versions.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export declare const kvStoreMigrations: (typeof CreateKVDatabaseMigration)[]
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true })
|
|
3
|
+
exports.kvStoreMigrations = void 0
|
|
4
|
+
const _1_createKVDatabase_1 = require('./1.createKVDatabase')
|
|
5
|
+
/**
|
|
6
|
+
* The migrations array that SHOULD be used when initializing a TypeORM database connection.
|
|
7
|
+
*
|
|
8
|
+
* These ensure the correct creation of tables and the proper migrations of data when tables change between versions.
|
|
9
|
+
*
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
exports.kvStoreMigrations = [_1_createKVDatabase_1.CreateKVDatabaseMigration]
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { OrPromise } from '@veramo/utils'
|
|
2
|
+
import { DataSource } from 'typeorm'
|
|
3
|
+
import { KeyvDeserializedData } from '../../keyv/keyv-types'
|
|
4
|
+
export type KeyValueTypeORMOptions = {
|
|
5
|
+
dbConnection: OrPromise<DataSource>
|
|
6
|
+
namespace?: string
|
|
7
|
+
}
|
|
8
|
+
export type Options_<Value> = {
|
|
9
|
+
validator: (value: any, key: string) => boolean
|
|
10
|
+
dialect: string
|
|
11
|
+
serialize: (data: KeyvDeserializedData<Value>) => OrPromise<string | undefined>
|
|
12
|
+
/** A custom deserialization function. */
|
|
13
|
+
deserialize: (data: any) => OrPromise<KeyvDeserializedData<Value> | undefined>
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
+
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
+
{
|
|
4
|
+
"tsdocVersion": "0.12",
|
|
5
|
+
"toolPackages": [
|
|
6
|
+
{
|
|
7
|
+
"packageName": "@microsoft/api-extractor",
|
|
8
|
+
"packageVersion": "7.33.7"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@veramo/kv-store",
|
|
3
|
+
"description": "Veramo Key Value Store plugin",
|
|
4
|
+
"version": "4.3.0",
|
|
5
|
+
"main": "build/index.js",
|
|
6
|
+
"types": "build/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "jest --config=../../jest.config.mjs",
|
|
10
|
+
"test:ci": "jest --config=../../jest.config.mjs",
|
|
11
|
+
"extract-api": "node ../cli/bin/veramo.js dev extract-api"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@veramo/utils": "4.3.0",
|
|
15
|
+
"debug": "^4.3.4",
|
|
16
|
+
"events": "^3.3.0",
|
|
17
|
+
"json-buffer": "^3.0.1",
|
|
18
|
+
"typeorm": "^0.3.10",
|
|
19
|
+
"uint8arrays": "^3.1.1"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"build/**/*",
|
|
23
|
+
"src/**/*",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"repository": "git@github.com:uport-project/veramo.git",
|
|
31
|
+
"author": "Niels Klomp <nklomp@sphereon.com>",
|
|
32
|
+
"license": "Apache-2.0",
|
|
33
|
+
"keywords": [
|
|
34
|
+
"Veramo",
|
|
35
|
+
"Key Value Store",
|
|
36
|
+
"keyv"
|
|
37
|
+
],
|
|
38
|
+
"yalcSig": "bfd8bfc89f0050ba31664b2d7c3efc7a"
|
|
39
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import KeyvSqlite from '@keyv/sqlite'
|
|
2
|
+
|
|
3
|
+
import timekeeper from 'timekeeper'
|
|
4
|
+
import { Keyv } from '../keyv/keyv'
|
|
5
|
+
import { DataSource } from 'typeorm'
|
|
6
|
+
import { KeyValueStoreEntity } from '../store-adapters/typeorm/entities/keyValueStoreEntity'
|
|
7
|
+
import { KeyValueTieredStoreAdapter, KeyValueTypeORMStoreAdapter } from '../store-adapters'
|
|
8
|
+
import { KeyvOptions } from '../keyv/keyv-types'
|
|
9
|
+
import { kvStoreMigrations } from '../store-adapters/typeorm/migrations'
|
|
10
|
+
|
|
11
|
+
let dbConnection: DataSource
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
dbConnection = await new DataSource({
|
|
14
|
+
type: 'sqlite',
|
|
15
|
+
database: ':memory:',
|
|
16
|
+
logging: ['error'],
|
|
17
|
+
migrationsRun: true,
|
|
18
|
+
synchronize: false,
|
|
19
|
+
migrations: [...kvStoreMigrations],
|
|
20
|
+
entities: [KeyValueStoreEntity],
|
|
21
|
+
}).initialize()
|
|
22
|
+
})
|
|
23
|
+
afterEach(async () => {
|
|
24
|
+
try {
|
|
25
|
+
if (dbConnection?.isInitialized) {
|
|
26
|
+
await (await dbConnection).destroy()
|
|
27
|
+
}
|
|
28
|
+
} catch (error) {
|
|
29
|
+
// the disconnect test will close the DB anyway
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
describe('keyv MAP store', () => {
|
|
33
|
+
it('should respect ttl', async () => {
|
|
34
|
+
const store = new Map<string, string>()
|
|
35
|
+
const keyv = new Keyv<string>(store)
|
|
36
|
+
await keyv.set('key', 'value', 100)
|
|
37
|
+
|
|
38
|
+
expect(await keyv.get('key')).toEqual('value')
|
|
39
|
+
timekeeper.freeze(Date.now() + 150)
|
|
40
|
+
expect(await keyv.get('key')).toBeUndefined()
|
|
41
|
+
})
|
|
42
|
+
it('should work for all methods', async () => {
|
|
43
|
+
const store = new Map<string, string>()
|
|
44
|
+
await testAllKeyvMethods(store)
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
describe('keyv sqlite store', () => {
|
|
49
|
+
it('should respect ttl', async () => {
|
|
50
|
+
const keyv = new Keyv<string>(new KeyvSqlite<string>())
|
|
51
|
+
await keyv.set('key', 'value', 100)
|
|
52
|
+
|
|
53
|
+
expect(await keyv.get('key')).toEqual('value')
|
|
54
|
+
timekeeper.freeze(Date.now() + 150)
|
|
55
|
+
expect(await keyv.get('key')).toBeUndefined()
|
|
56
|
+
})
|
|
57
|
+
it('should work for all methods', async () => {
|
|
58
|
+
const store = new KeyvSqlite<string>()
|
|
59
|
+
await testAllKeyvMethods(store)
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
describe('keyv TypeORM store', () => {
|
|
64
|
+
/*let dbConnection: DataSource
|
|
65
|
+
|
|
66
|
+
beforeEach(async () => {
|
|
67
|
+
dbConnection = await new DataSource({
|
|
68
|
+
type: 'sqlite',
|
|
69
|
+
database: ':memory:',
|
|
70
|
+
logging: 'all',
|
|
71
|
+
migrationsRun: true,
|
|
72
|
+
synchronize: false,
|
|
73
|
+
migrations: [...kvStoreMigrations],
|
|
74
|
+
entities: [KeyValueStoreEntity],
|
|
75
|
+
}).initialize()
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
afterEach(async () => {
|
|
79
|
+
try {
|
|
80
|
+
(await dbConnection).destroy()
|
|
81
|
+
} catch (error) {
|
|
82
|
+
// the disconnect test will close the DB anyway
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
})
|
|
86
|
+
*/
|
|
87
|
+
it('should respect ttl', async () => {
|
|
88
|
+
const store = new KeyValueTypeORMStoreAdapter({ dbConnection, namespace: 'test' })
|
|
89
|
+
const keyv = new Keyv<string>(store)
|
|
90
|
+
await keyv.set('key', 'value', 100)
|
|
91
|
+
|
|
92
|
+
expect(await keyv.get('key')).toEqual('value')
|
|
93
|
+
timekeeper.freeze(Date.now() + 150)
|
|
94
|
+
expect(await keyv.get('key')).toBeUndefined()
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('should set a value that can be retrieved from the proper namespace', async () => {
|
|
98
|
+
const store = new KeyValueTypeORMStoreAdapter({ dbConnection, namespace: 'test' })
|
|
99
|
+
const keyv = new Keyv<string>(store)
|
|
100
|
+
await keyv.set('key', 'value')
|
|
101
|
+
expect(await keyv.get('key')).toEqual('value')
|
|
102
|
+
|
|
103
|
+
const alternateStore = new KeyValueTypeORMStoreAdapter({ dbConnection, namespace: 'another' })
|
|
104
|
+
const alternateKeyv = new Keyv<string>(alternateStore)
|
|
105
|
+
expect(await alternateKeyv.get('key')).toBeUndefined()
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('should set multiple values that can be retrieved', async () => {
|
|
109
|
+
const store = new KeyValueTypeORMStoreAdapter({ dbConnection, namespace: 'test' })
|
|
110
|
+
const keyv = new Keyv<string>(store)
|
|
111
|
+
for (let i = 0; i < 10; i++) {
|
|
112
|
+
await keyv.set(`${i}`, `value${i}`)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let values = await keyv.getMany(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'nope'])
|
|
116
|
+
expect(values).toHaveLength(11)
|
|
117
|
+
|
|
118
|
+
await keyv.delete('0')
|
|
119
|
+
expect(await keyv.getMany(['0'])).toHaveLength(1)
|
|
120
|
+
values = await keyv.getMany(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'nope'])
|
|
121
|
+
expect(values).toHaveLength(11)
|
|
122
|
+
expect(values[9]).toBe('value9')
|
|
123
|
+
expect(values[10]).toBeUndefined()
|
|
124
|
+
|
|
125
|
+
await keyv.clear()
|
|
126
|
+
values = await keyv.getMany(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'nope'])
|
|
127
|
+
expect(values).toHaveLength(11)
|
|
128
|
+
expect(values[0]).toBeUndefined()
|
|
129
|
+
expect(values[9]).toBeUndefined()
|
|
130
|
+
expect(values[10]).toBeUndefined()
|
|
131
|
+
})
|
|
132
|
+
it('should work for all methods', async () => {
|
|
133
|
+
const store = new KeyValueTypeORMStoreAdapter({ dbConnection, namespace: 'test-all' })
|
|
134
|
+
await testAllKeyvMethods(store)
|
|
135
|
+
await store.disconnect()
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
describe('keyv tiered store', () => {
|
|
140
|
+
it('should respect ttl', async () => {
|
|
141
|
+
const local: Map<string, string> = new Map()
|
|
142
|
+
const remote = new KeyValueTypeORMStoreAdapter({ dbConnection })
|
|
143
|
+
|
|
144
|
+
const options: KeyvOptions<string> = {
|
|
145
|
+
namespace: 'example',
|
|
146
|
+
store: new KeyValueTieredStoreAdapter({ local, remote }),
|
|
147
|
+
}
|
|
148
|
+
const keyv = new Keyv<string>(undefined, options)
|
|
149
|
+
await keyv.set('key', 'value', 100)
|
|
150
|
+
|
|
151
|
+
expect(await keyv.get('key')).toEqual('value')
|
|
152
|
+
timekeeper.freeze(Date.now() + 150)
|
|
153
|
+
expect(await keyv.get('key')).toBeUndefined()
|
|
154
|
+
await keyv.disconnect()
|
|
155
|
+
})
|
|
156
|
+
it('should work for all methods', async () => {
|
|
157
|
+
const local: Map<string, string> = new Map()
|
|
158
|
+
const remote = new KeyValueTypeORMStoreAdapter({ dbConnection })
|
|
159
|
+
|
|
160
|
+
const options: KeyvOptions<string> = {
|
|
161
|
+
namespace: 'example',
|
|
162
|
+
store: new KeyValueTieredStoreAdapter({ local, remote }),
|
|
163
|
+
}
|
|
164
|
+
const store = new Keyv<string>(undefined, options)
|
|
165
|
+
|
|
166
|
+
// No raw match test, as remote and local have different raw values
|
|
167
|
+
await testAllKeyvMethods(store, false)
|
|
168
|
+
})
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
async function testAllKeyvMethods(store: any, rawMatchTest = true) {
|
|
172
|
+
const keyv = new Keyv<string>(store)
|
|
173
|
+
|
|
174
|
+
// set value
|
|
175
|
+
await expect(keyv.set('key1', 'value1')).resolves.toEqual(true)
|
|
176
|
+
await expect(keyv.set('key2', 'value2')).resolves.toEqual(true)
|
|
177
|
+
|
|
178
|
+
// get value by key
|
|
179
|
+
await expect(keyv.get('key1', { raw: false })).resolves.toEqual('value1')
|
|
180
|
+
await expect(keyv.get('key1', { raw: true })).resolves.toMatchObject({ value: 'value1' })
|
|
181
|
+
|
|
182
|
+
// get value by non-existing key
|
|
183
|
+
await expect(keyv.get('key3', { raw: false })).resolves.toBeUndefined()
|
|
184
|
+
await expect(keyv.get('key3', { raw: true })).resolves.toBeUndefined()
|
|
185
|
+
|
|
186
|
+
// Get many as non-raw and raw
|
|
187
|
+
await expect(keyv.getMany(['key1', 'key2'], { raw: false })).resolves.toMatchObject(['value1', 'value2'])
|
|
188
|
+
if (rawMatchTest) {
|
|
189
|
+
await expect(keyv.getMany(['key1', 'key2'], { raw: true })).resolves.toMatchObject([{ value: 'value1' }, { value: 'value2' }])
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Check existence
|
|
193
|
+
await expect(keyv.has('key1')).resolves.toEqual(true)
|
|
194
|
+
await expect(keyv.has('key3')).resolves.toEqual(false)
|
|
195
|
+
|
|
196
|
+
// delete key1 only
|
|
197
|
+
await expect(keyv.delete('key1')).resolves.toEqual(true)
|
|
198
|
+
await expect(keyv.has('key1')).resolves.toEqual(false)
|
|
199
|
+
await expect(keyv.has('key2')).resolves.toEqual(true)
|
|
200
|
+
|
|
201
|
+
// clear
|
|
202
|
+
await keyv.clear()
|
|
203
|
+
await expect(keyv.has('key2')).resolves.toEqual(false)
|
|
204
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { KeyValueStore } from '../key-value-store'
|
|
2
|
+
import { IKeyValueStore } from '../key-value-types'
|
|
3
|
+
|
|
4
|
+
interface TestValue {
|
|
5
|
+
stringProp: string
|
|
6
|
+
numberProp: number
|
|
7
|
+
objectProp: {
|
|
8
|
+
value: string | number
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let testValues: TestValue[] = []
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < 10; i++) {
|
|
15
|
+
testValues.push({
|
|
16
|
+
stringProp: `stringValue${i}`,
|
|
17
|
+
numberProp: i,
|
|
18
|
+
objectProp: {
|
|
19
|
+
value: `objectValue${i}`,
|
|
20
|
+
},
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
describe('kvStore with MAP adapter', () => {
|
|
24
|
+
let kvStore: IKeyValueStore<TestValue>
|
|
25
|
+
beforeEach(async () => {
|
|
26
|
+
const store = new Map<string, TestValue>()
|
|
27
|
+
kvStore = new KeyValueStore({ store })
|
|
28
|
+
for (let i = 0; i < 10; i++) {
|
|
29
|
+
await kvStore.set(`key${i}`, testValues[i])
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
afterEach(async () => {
|
|
33
|
+
try {
|
|
34
|
+
await kvStore.clear()
|
|
35
|
+
await kvStore.disconnect()
|
|
36
|
+
} catch (error) {}
|
|
37
|
+
})
|
|
38
|
+
it('should get non-existing keys as undefined', async () => {
|
|
39
|
+
await expect(kvStore.get('nope')).resolves.toBeUndefined()
|
|
40
|
+
await expect(kvStore.getAsValueData('nope')).resolves.toMatchObject({
|
|
41
|
+
value: undefined,
|
|
42
|
+
expires: undefined,
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('should get single results', async () => {
|
|
47
|
+
for (let i = 0; i < 10; i++) {
|
|
48
|
+
await expect(kvStore.get(`key${i}`)).resolves.toEqual(testValues[i])
|
|
49
|
+
await expect(kvStore.getAsValueData(`key${i}`)).resolves.toMatchObject({
|
|
50
|
+
value: testValues[i],
|
|
51
|
+
expires: undefined,
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('should get multiple results', async () => {
|
|
57
|
+
// Let's get multiple results
|
|
58
|
+
const manyResult = await kvStore.getMany(['key1', 'nope', 'key4'])
|
|
59
|
+
expect(manyResult).toHaveLength(3)
|
|
60
|
+
expect(manyResult[0]).toEqual(testValues[1])
|
|
61
|
+
expect(manyResult[1]).toBeUndefined()
|
|
62
|
+
expect(manyResult[2]).toEqual(testValues[4])
|
|
63
|
+
const manyValueResult = await kvStore.getManyAsValueData(['key1', 'nope', 'key4'])
|
|
64
|
+
expect(manyValueResult).toHaveLength(3)
|
|
65
|
+
expect(manyValueResult[0]).toEqual({ value: testValues[1], expires: undefined })
|
|
66
|
+
expect(manyValueResult[1]).toEqual({ value: undefined, expires: undefined })
|
|
67
|
+
expect(manyValueResult[2]).toEqual({ value: testValues[4], expires: undefined })
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('should check existence of keys', async () => {
|
|
71
|
+
await expect(kvStore.has(`key1`)).resolves.toEqual(true)
|
|
72
|
+
await expect(kvStore.has(`nope`)).resolves.toEqual(false)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('should delete an existing key', async () => {
|
|
76
|
+
await expect(kvStore.has(`key1`)).resolves.toEqual(true)
|
|
77
|
+
await expect(kvStore.delete(`key1`)).resolves.toEqual(true)
|
|
78
|
+
await expect(kvStore.has(`key1`)).resolves.toEqual(false)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('should not throw an error when deleting a non-existing key', async () => {
|
|
82
|
+
await expect(kvStore.delete(`nope`)).resolves.toEqual(false)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('should delete multiple values', async () => {
|
|
86
|
+
await expect(kvStore.deleteMany(['key2', 'nope', 'key6'])).resolves.toEqual([true, false, true])
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('should clear all', async () => {
|
|
90
|
+
await kvStore.clear()
|
|
91
|
+
await expect(kvStore.has(`key1`)).resolves.toEqual(false)
|
|
92
|
+
})
|
|
93
|
+
})
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides a {@link @veramo/kv-store#KeyValueStore} for the
|
|
3
|
+
* {@link @veramo/core#Agent} plugin that implements {@link @veramo/kv-store#IKeyValueStore} interface
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
*/
|
|
7
|
+
export { KeyValueStore } from './key-value-store'
|
|
8
|
+
export * from './store-adapters/tiered/index'
|
|
9
|
+
export * from './store-adapters/typeorm/index'
|
|
10
|
+
export * from './key-value-types'
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { IKeyValueStore, IKeyValueStoreOnArgs, IKeyValueStoreOptions, IValueData } from './key-value-types'
|
|
2
|
+
import { Keyv } from './keyv/keyv'
|
|
3
|
+
import { KeyvDeserializedData, KeyvOptions, KeyvStoredData } from './keyv/keyv-types'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Agent plugin that implements {@link @veramo/kv-store#IKeyValueStore} interface
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export class KeyValueStore<ValueType> implements IKeyValueStore<ValueType> {
|
|
10
|
+
/**
|
|
11
|
+
* The main keyv typescript port which delegates to the storage adapters and takes care of some common functionality
|
|
12
|
+
*
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
private readonly keyv: Keyv<ValueType>
|
|
16
|
+
|
|
17
|
+
constructor(options: IKeyValueStoreOptions<ValueType>) {
|
|
18
|
+
this.keyv = new Keyv(options.uri, options as KeyvOptions<ValueType>)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async get(key: string): Promise<ValueType | undefined> {
|
|
22
|
+
const result = await this.keyv.get(key, { raw: false })
|
|
23
|
+
if (result === null || result === undefined) {
|
|
24
|
+
return undefined
|
|
25
|
+
}
|
|
26
|
+
return result as ValueType
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async getAsValueData(key: string): Promise<IValueData<ValueType>> {
|
|
30
|
+
const result = await this.keyv.get(key, { raw: true })
|
|
31
|
+
if (result === null || result === undefined) {
|
|
32
|
+
// We always return a ValueData object for this method
|
|
33
|
+
return { value: undefined, expires: undefined }
|
|
34
|
+
}
|
|
35
|
+
return this.toDeserializedValueData(result)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async getMany(keys: string[]): Promise<Array<ValueType | undefined>> {
|
|
39
|
+
if (!keys || keys.length === 0) {
|
|
40
|
+
return []
|
|
41
|
+
}
|
|
42
|
+
let result = await this.keyv.getMany(keys, { raw: false })
|
|
43
|
+
|
|
44
|
+
// Making sure we return the same array length as the amount of key(s) passed in
|
|
45
|
+
if (result === null || result === undefined || result.length === 0) {
|
|
46
|
+
result = new Array<ValueType | undefined>()
|
|
47
|
+
keys.forEach(() => result.push(undefined))
|
|
48
|
+
}
|
|
49
|
+
return result.map((v) => (!!v ? (v as ValueType) : undefined))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async getManyAsValueData(keys: string[]): Promise<Array<IValueData<ValueType>>> {
|
|
53
|
+
if (!keys || keys.length === 0) {
|
|
54
|
+
return []
|
|
55
|
+
}
|
|
56
|
+
let result = await this.keyv.getMany(keys, { raw: true })
|
|
57
|
+
|
|
58
|
+
// Making sure we return the same array length as the amount of key(s) passed in
|
|
59
|
+
if (result === null || result === undefined || result.length === 0) {
|
|
60
|
+
result = new Array<KeyvStoredData<ValueType>>()
|
|
61
|
+
keys.forEach(() => result.push({ value: undefined, expires: undefined } as KeyvDeserializedData<ValueType>))
|
|
62
|
+
}
|
|
63
|
+
return result.map((v) => (!!v ? this.toDeserializedValueData(v) : { value: undefined, expires: undefined }))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async set(key: string, value: ValueType, ttl?: number): Promise<IValueData<ValueType>> {
|
|
67
|
+
return this.keyv.set(key, value, ttl).then(() => this.getAsValueData(key))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async has(key: string): Promise<boolean> {
|
|
71
|
+
return this.keyv.has(key)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async delete(key: string): Promise<boolean> {
|
|
75
|
+
return this.keyv.delete(key)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async deleteMany(keys: string[]): Promise<boolean[]> {
|
|
79
|
+
return Promise.all(keys.map((key) => this.keyv.delete(key)))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async clear(): Promise<IKeyValueStore<ValueType>> {
|
|
83
|
+
return this.keyv.clear().then(() => this)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async disconnect(): Promise<void> {
|
|
87
|
+
return this.keyv.disconnect()
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Public so parties using the kv store directly can add listeners if they want
|
|
91
|
+
async kvStoreOn(args: IKeyValueStoreOnArgs): Promise<IKeyValueStore<ValueType>> {
|
|
92
|
+
this.keyv.on(args.eventName, args.listener)
|
|
93
|
+
return this
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private toDeserializedValueData<ValueType>(result: any): IValueData<ValueType> {
|
|
97
|
+
if (result === null || result === undefined) {
|
|
98
|
+
throw Error(`Result cannot be undefined or null at this this point`)
|
|
99
|
+
} else if (typeof result !== 'object') {
|
|
100
|
+
return { value: result, expires: undefined }
|
|
101
|
+
} else if (!('value' in result)) {
|
|
102
|
+
return { value: result, expires: undefined }
|
|
103
|
+
}
|
|
104
|
+
if (!('expires' in result)) {
|
|
105
|
+
result.expires = undefined
|
|
106
|
+
}
|
|
107
|
+
return result as IValueData<ValueType>
|
|
108
|
+
}
|
|
109
|
+
}
|