@keyv/etcd 1.0.1 → 1.0.2
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/package.json +15 -7
- package/src/index.d.ts +20 -0
- package/src/index.js +45 -12
- package/.nyc_output/5f094529-52cc-4112-aec5-6b026e26b96c.json +0 -1
- package/.nyc_output/e9963487-a432-48bb-9646-1abfbe82a156.json +0 -1
- package/.nyc_output/processinfo/5f094529-52cc-4112-aec5-6b026e26b96c.json +0 -1
- package/.nyc_output/processinfo/e9963487-a432-48bb-9646-1abfbe82a156.json +0 -1
- package/.nyc_output/processinfo/index.json +0 -1
- package/test/test.js +0 -42
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keyv/etcd",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Etcd storage adapter for Keyv",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
"clean": "rm -rf node_modules && rm -rf .nyc_output && rm -rf coverage.lcov"
|
|
10
10
|
},
|
|
11
11
|
"xo": {
|
|
12
|
-
"extends": "xo-lukechilds",
|
|
13
12
|
"rules": {
|
|
14
13
|
"unicorn/prefer-module": 0,
|
|
15
14
|
"unicorn/no-array-reduce": 0,
|
|
16
|
-
"unicorn/prefer-object-from-entries": 0
|
|
15
|
+
"unicorn/prefer-object-from-entries": 0,
|
|
16
|
+
"unicorn/prefer-node-protocol": 0
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"ava": {
|
|
@@ -49,12 +49,20 @@
|
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@keyv/test-suite": "*",
|
|
52
|
-
"ava": "^
|
|
53
|
-
"eslint-config-xo-lukechilds": "^1.0.1",
|
|
52
|
+
"ava": "^4.0.1",
|
|
54
53
|
"keyv": "*",
|
|
55
54
|
"nyc": "^15.1.0",
|
|
56
55
|
"requirable": "^1.0.5",
|
|
57
56
|
"this": "^1.1.0",
|
|
58
|
-
"
|
|
59
|
-
|
|
57
|
+
"tsd": "^0.19.1",
|
|
58
|
+
"typescript": "^4.6.2",
|
|
59
|
+
"xo": "^0.48.0"
|
|
60
|
+
},
|
|
61
|
+
"tsd" : {
|
|
62
|
+
"directory" : "test"
|
|
63
|
+
},
|
|
64
|
+
"types": "./src/index.d.ts",
|
|
65
|
+
"files": [
|
|
66
|
+
"src"
|
|
67
|
+
]
|
|
60
68
|
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {EventEmitter} from 'events';
|
|
2
|
+
import Etcd3 from 'etcd3';
|
|
3
|
+
|
|
4
|
+
declare class KeyvEtcd extends EventEmitter {
|
|
5
|
+
ttlSupport: any;
|
|
6
|
+
opts: any;
|
|
7
|
+
client: Etcd3;
|
|
8
|
+
lease: import('etcd3').Lease;
|
|
9
|
+
constructor(url: any, options: any);
|
|
10
|
+
get(key: string): Promise<string | undefined>;
|
|
11
|
+
getMany(keys: string[]): Promise<string[] | undefined>;
|
|
12
|
+
set(key: string, value: string | undefined): Promise<any>;
|
|
13
|
+
delete(key: string): boolean;
|
|
14
|
+
deleteMany(keys: string[]): boolean;
|
|
15
|
+
clear(): Promise<void>;
|
|
16
|
+
iterator(namespace: string | undefined): AsyncGenerator<any, void, any>;
|
|
17
|
+
has(key: string): boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export = KeyvEtcd;
|
package/src/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const EventEmitter = require('events');
|
|
4
|
-
const {
|
|
5
|
-
const {
|
|
4
|
+
const {Etcd3} = require('etcd3');
|
|
5
|
+
const {Policy} = require('cockatiel');
|
|
6
6
|
|
|
7
7
|
class KeyvEtcd extends EventEmitter {
|
|
8
8
|
constructor(url, options) {
|
|
@@ -10,31 +10,29 @@ class KeyvEtcd extends EventEmitter {
|
|
|
10
10
|
this.ttlSupport = options && typeof options.ttl === 'number';
|
|
11
11
|
url = url || {};
|
|
12
12
|
if (typeof url === 'string') {
|
|
13
|
-
url = {
|
|
13
|
+
url = {url};
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
if (url.uri) {
|
|
17
|
-
url =
|
|
17
|
+
url = {url: url.uri, ...url};
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
if (url.ttl) {
|
|
21
21
|
this.ttlSupport = typeof url.ttl === 'number' ? url.ttl : false;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
this.opts =
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
options,
|
|
30
|
-
);
|
|
24
|
+
this.opts = {
|
|
25
|
+
url: '127.0.0.1:2379',
|
|
26
|
+
...url,
|
|
27
|
+
...options,
|
|
28
|
+
};
|
|
31
29
|
|
|
32
30
|
this.opts.url = this.opts.url.replace(/^etcd:\/\//, '');
|
|
33
31
|
const policy = Policy.handleAll().retry();
|
|
34
32
|
policy.onFailure(error => {
|
|
35
33
|
this.emit('error', error.reason);
|
|
36
34
|
});
|
|
37
|
-
this.client = new Etcd3(options = {
|
|
35
|
+
this.client = new Etcd3(options = {hosts: this.opts.url,
|
|
38
36
|
faultHandling: {
|
|
39
37
|
host: () => policy,
|
|
40
38
|
global: policy,
|
|
@@ -55,6 +53,27 @@ class KeyvEtcd extends EventEmitter {
|
|
|
55
53
|
return this.client.get(key);
|
|
56
54
|
}
|
|
57
55
|
|
|
56
|
+
getMany(keys) {
|
|
57
|
+
const promises = [];
|
|
58
|
+
for (const key of keys) {
|
|
59
|
+
promises.push(this.get(key));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return Promise.allSettled(promises)
|
|
63
|
+
.then(values => {
|
|
64
|
+
const data = [];
|
|
65
|
+
for (const value of values) {
|
|
66
|
+
if (value.value === null) {
|
|
67
|
+
data.push(undefined);
|
|
68
|
+
} else {
|
|
69
|
+
data.push(value.value);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return data.every(x => x === undefined) ? [] : data;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
58
77
|
set(key, value) {
|
|
59
78
|
return this.opts.ttl ? this.lease.put(key).value(value) : this.client.put(key).value(value);
|
|
60
79
|
}
|
|
@@ -67,12 +86,26 @@ class KeyvEtcd extends EventEmitter {
|
|
|
67
86
|
return this.client.delete().key(key).then(key => key.deleted !== '0');
|
|
68
87
|
}
|
|
69
88
|
|
|
89
|
+
deleteMany(keys) {
|
|
90
|
+
const promises = [];
|
|
91
|
+
for (const key of keys) {
|
|
92
|
+
promises.push(this.delete(key));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return Promise.allSettled(promises)
|
|
96
|
+
.then(values => values.every(x => x.value === true));
|
|
97
|
+
}
|
|
98
|
+
|
|
70
99
|
clear() {
|
|
71
100
|
const promise = this.namespace
|
|
72
101
|
? this.client.delete().prefix(this.namespace)
|
|
73
102
|
: this.client.delete().all();
|
|
74
103
|
return promise.then(() => undefined);
|
|
75
104
|
}
|
|
105
|
+
|
|
106
|
+
has(key) {
|
|
107
|
+
return this.client.get(key).exists();
|
|
108
|
+
}
|
|
76
109
|
}
|
|
77
110
|
|
|
78
111
|
module.exports = KeyvEtcd;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/etcd/src/index.js":{"path":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/etcd/src/index.js","statementMap":{"0":{"start":{"line":3,"column":21},"end":{"line":3,"column":38}},"1":{"start":{"line":4,"column":18},"end":{"line":4,"column":34}},"2":{"start":{"line":5,"column":19},"end":{"line":5,"column":39}},"3":{"start":{"line":9,"column":2},"end":{"line":9,"column":10}},"4":{"start":{"line":10,"column":2},"end":{"line":10,"column":63}},"5":{"start":{"line":11,"column":2},"end":{"line":11,"column":18}},"6":{"start":{"line":12,"column":2},"end":{"line":14,"column":3}},"7":{"start":{"line":13,"column":3},"end":{"line":13,"column":17}},"8":{"start":{"line":16,"column":2},"end":{"line":18,"column":3}},"9":{"start":{"line":17,"column":3},"end":{"line":17,"column":46}},"10":{"start":{"line":20,"column":2},"end":{"line":22,"column":3}},"11":{"start":{"line":21,"column":3},"end":{"line":21,"column":67}},"12":{"start":{"line":24,"column":2},"end":{"line":30,"column":4}},"13":{"start":{"line":32,"column":2},"end":{"line":32,"column":58}},"14":{"start":{"line":33,"column":17},"end":{"line":33,"column":43}},"15":{"start":{"line":34,"column":2},"end":{"line":36,"column":5}},"16":{"start":{"line":35,"column":3},"end":{"line":35,"column":36}},"17":{"start":{"line":37,"column":2},"end":{"line":42,"column":5}},"18":{"start":{"line":39,"column":16},"end":{"line":39,"column":22}},"19":{"start":{"line":45,"column":2},"end":{"line":45,"column":67}},"20":{"start":{"line":45,"column":40},"end":{"line":45,"column":65}},"21":{"start":{"line":47,"column":2},"end":{"line":51,"column":3}},"22":{"start":{"line":48,"column":3},"end":{"line":50,"column":6}},"23":{"start":{"line":55,"column":2},"end":{"line":55,"column":30}},"24":{"start":{"line":59,"column":2},"end":{"line":59,"column":94}},"25":{"start":{"line":63,"column":2},"end":{"line":65,"column":3}},"26":{"start":{"line":64,"column":3},"end":{"line":64,"column":33}},"27":{"start":{"line":67,"column":2},"end":{"line":67,"column":72}},"28":{"start":{"line":67,"column":51},"end":{"line":67,"column":70}},"29":{"start":{"line":71,"column":18},"end":{"line":73,"column":31}},"30":{"start":{"line":74,"column":2},"end":{"line":74,"column":39}},"31":{"start":{"line":74,"column":28},"end":{"line":74,"column":37}},"32":{"start":{"line":78,"column":0},"end":{"line":78,"column":26}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":8,"column":1},"end":{"line":8,"column":2}},"loc":{"start":{"line":8,"column":27},"end":{"line":52,"column":2}},"line":8},"1":{"name":"(anonymous_1)","decl":{"start":{"line":34,"column":19},"end":{"line":34,"column":20}},"loc":{"start":{"line":34,"column":28},"end":{"line":36,"column":3}},"line":34},"2":{"name":"(anonymous_2)","decl":{"start":{"line":39,"column":10},"end":{"line":39,"column":11}},"loc":{"start":{"line":39,"column":16},"end":{"line":39,"column":22}},"line":39},"3":{"name":"(anonymous_3)","decl":{"start":{"line":45,"column":31},"end":{"line":45,"column":32}},"loc":{"start":{"line":45,"column":40},"end":{"line":45,"column":65}},"line":45},"4":{"name":"(anonymous_4)","decl":{"start":{"line":54,"column":1},"end":{"line":54,"column":2}},"loc":{"start":{"line":54,"column":10},"end":{"line":56,"column":2}},"line":54},"5":{"name":"(anonymous_5)","decl":{"start":{"line":58,"column":1},"end":{"line":58,"column":2}},"loc":{"start":{"line":58,"column":17},"end":{"line":60,"column":2}},"line":58},"6":{"name":"(anonymous_6)","decl":{"start":{"line":62,"column":1},"end":{"line":62,"column":2}},"loc":{"start":{"line":62,"column":13},"end":{"line":68,"column":2}},"line":62},"7":{"name":"(anonymous_7)","decl":{"start":{"line":67,"column":44},"end":{"line":67,"column":45}},"loc":{"start":{"line":67,"column":51},"end":{"line":67,"column":70}},"line":67},"8":{"name":"(anonymous_8)","decl":{"start":{"line":70,"column":1},"end":{"line":70,"column":2}},"loc":{"start":{"line":70,"column":9},"end":{"line":75,"column":2}},"line":70},"9":{"name":"(anonymous_9)","decl":{"start":{"line":74,"column":22},"end":{"line":74,"column":23}},"loc":{"start":{"line":74,"column":28},"end":{"line":74,"column":37}},"line":74}},"branchMap":{"0":{"loc":{"start":{"line":10,"column":20},"end":{"line":10,"column":62}},"type":"binary-expr","locations":[{"start":{"line":10,"column":20},"end":{"line":10,"column":27}},{"start":{"line":10,"column":31},"end":{"line":10,"column":62}}],"line":10},"1":{"loc":{"start":{"line":11,"column":8},"end":{"line":11,"column":17}},"type":"binary-expr","locations":[{"start":{"line":11,"column":8},"end":{"line":11,"column":11}},{"start":{"line":11,"column":15},"end":{"line":11,"column":17}}],"line":11},"2":{"loc":{"start":{"line":12,"column":2},"end":{"line":14,"column":3}},"type":"if","locations":[{"start":{"line":12,"column":2},"end":{"line":14,"column":3}},{"start":{"line":12,"column":2},"end":{"line":14,"column":3}}],"line":12},"3":{"loc":{"start":{"line":16,"column":2},"end":{"line":18,"column":3}},"type":"if","locations":[{"start":{"line":16,"column":2},"end":{"line":18,"column":3}},{"start":{"line":16,"column":2},"end":{"line":18,"column":3}}],"line":16},"4":{"loc":{"start":{"line":20,"column":2},"end":{"line":22,"column":3}},"type":"if","locations":[{"start":{"line":20,"column":2},"end":{"line":22,"column":3}},{"start":{"line":20,"column":2},"end":{"line":22,"column":3}}],"line":20},"5":{"loc":{"start":{"line":21,"column":21},"end":{"line":21,"column":66}},"type":"cond-expr","locations":[{"start":{"line":21,"column":51},"end":{"line":21,"column":58}},{"start":{"line":21,"column":61},"end":{"line":21,"column":66}}],"line":21},"6":{"loc":{"start":{"line":47,"column":2},"end":{"line":51,"column":3}},"type":"if","locations":[{"start":{"line":47,"column":2},"end":{"line":51,"column":3}},{"start":{"line":47,"column":2},"end":{"line":51,"column":3}}],"line":47},"7":{"loc":{"start":{"line":59,"column":9},"end":{"line":59,"column":93}},"type":"cond-expr","locations":[{"start":{"line":59,"column":25},"end":{"line":59,"column":57}},{"start":{"line":59,"column":60},"end":{"line":59,"column":93}}],"line":59},"8":{"loc":{"start":{"line":63,"column":2},"end":{"line":65,"column":3}},"type":"if","locations":[{"start":{"line":63,"column":2},"end":{"line":65,"column":3}},{"start":{"line":63,"column":2},"end":{"line":65,"column":3}}],"line":63},"9":{"loc":{"start":{"line":71,"column":18},"end":{"line":73,"column":31}},"type":"cond-expr","locations":[{"start":{"line":72,"column":5},"end":{"line":72,"column":48}},{"start":{"line":73,"column":5},"end":{"line":73,"column":31}}],"line":71}},"s":{"0":1,"1":1,"2":1,"3":157,"4":157,"5":157,"6":157,"7":1,"8":157,"9":154,"10":157,"11":1,"12":157,"13":157,"14":157,"15":157,"16":1,"17":157,"18":157,"19":157,"20":0,"21":157,"22":1,"23":29,"24":28,"25":7,"26":1,"27":6,"28":6,"29":131,"30":131,"31":131,"32":1},"f":{"0":157,"1":1,"2":157,"3":0,"4":29,"5":28,"6":7,"7":6,"8":131,"9":131},"b":{"0":[157,0],"1":[157,1],"2":[1,156],"3":[154,3],"4":[1,156],"5":[1,0],"6":[1,156],"7":[1,27],"8":[1,6],"9":[131,0]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"f5ef7b4fe02cf354e13bf79cdfaa4f261d71c9a6","contentHash":"966d2e368e426b4ec3bdaa2262935f909989963c4caa45bf568d8bcf22900c10"}}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"parent":"e9963487-a432-48bb-9646-1abfbe82a156","pid":28171,"argv":["/Users/jaredwray/.nvm/versions/node/v16.13.1/bin/node","/Users/jaredwray/src/github.com/jaredwray/keyv/node_modules/ava/lib/worker/subprocess.js"],"execArgv":[],"cwd":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/etcd","time":1643651036076,"ppid":28157,"coverageFilename":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/etcd/.nyc_output/5f094529-52cc-4112-aec5-6b026e26b96c.json","externalId":"","uuid":"5f094529-52cc-4112-aec5-6b026e26b96c","files":["/Users/jaredwray/src/github.com/jaredwray/keyv/packages/etcd/src/index.js"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"parent":null,"pid":28157,"argv":["/Users/jaredwray/.nvm/versions/node/v16.13.1/bin/node","/Users/jaredwray/src/github.com/jaredwray/keyv/packages/etcd/node_modules/.bin/ava"],"execArgv":[],"cwd":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/etcd","time":1643651035816,"ppid":28153,"coverageFilename":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/etcd/.nyc_output/e9963487-a432-48bb-9646-1abfbe82a156.json","externalId":"","uuid":"e9963487-a432-48bb-9646-1abfbe82a156","files":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"processes":{"5f094529-52cc-4112-aec5-6b026e26b96c":{"parent":"e9963487-a432-48bb-9646-1abfbe82a156","children":[]},"e9963487-a432-48bb-9646-1abfbe82a156":{"parent":null,"children":["5f094529-52cc-4112-aec5-6b026e26b96c"]}},"files":{"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/etcd/src/index.js":["5f094529-52cc-4112-aec5-6b026e26b96c"]},"externalIds":{}}
|
package/test/test.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
const test = require('ava');
|
|
2
|
-
const KeyvEtcd = require('this');
|
|
3
|
-
const Keyv = require('keyv');
|
|
4
|
-
const keyvTestSuite = require('@keyv/test-suite').default;
|
|
5
|
-
const { keyvOfficialTests } = require('@keyv/test-suite');
|
|
6
|
-
|
|
7
|
-
const etcdURL = 'etcd://127.0.0.1:2379';
|
|
8
|
-
|
|
9
|
-
keyvOfficialTests(test, Keyv, etcdURL, 'etcd://foo');
|
|
10
|
-
|
|
11
|
-
const store = () => new KeyvEtcd({ uri: etcdURL, busyTimeout: 3000 });
|
|
12
|
-
|
|
13
|
-
keyvTestSuite(test, Keyv, store);
|
|
14
|
-
|
|
15
|
-
test('default options', t => {
|
|
16
|
-
const store = new KeyvEtcd();
|
|
17
|
-
t.deepEqual(store.opts, {
|
|
18
|
-
url: '127.0.0.1:2379',
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
function sleep(ms) {
|
|
23
|
-
return new Promise(resolve => {
|
|
24
|
-
setTimeout(resolve, ms);
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
test.serial('KeyvEtcd respects default tll option', async t => {
|
|
29
|
-
const store = new Map();
|
|
30
|
-
const keyv = new KeyvEtcd({ store, ttl: 1000 });
|
|
31
|
-
await keyv.set('foo', 'bar');
|
|
32
|
-
t.is(await keyv.get('foo'), 'bar');
|
|
33
|
-
await sleep(3000);
|
|
34
|
-
t.is(await keyv.get('foo'), null);
|
|
35
|
-
t.is(store.size, 0);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test('.delete() with key as number', async t => {
|
|
39
|
-
const store = new KeyvEtcd(etcdURL);
|
|
40
|
-
t.false(await store.delete(123));
|
|
41
|
-
});
|
|
42
|
-
|