@keyv/etcd 1.0.0

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.
@@ -0,0 +1 @@
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"}}
@@ -0,0 +1 @@
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"]}
@@ -0,0 +1 @@
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":[]}
@@ -0,0 +1 @@
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/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@keyv/etcd",
3
+ "version": "1.0.0",
4
+ "description": "Etcd storage adapter for Keyv",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "test": "xo && nyc ava",
8
+ "coverage": "nyc report --reporter=text-lcov > coverage.lcov",
9
+ "clean": "rm -rf node_modules && rm -rf .nyc_output && rm -rf coverage.lcov"
10
+ },
11
+ "xo": {
12
+ "extends": "xo-lukechilds",
13
+ "rules": {
14
+ "unicorn/prefer-module": 0,
15
+ "unicorn/no-array-reduce": 0,
16
+ "unicorn/prefer-object-from-entries": 0
17
+ }
18
+ },
19
+ "ava": {
20
+ "require": [
21
+ "requirable"
22
+ ]
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/jaredwray/keyv.git"
27
+ },
28
+ "keywords": [
29
+ "etcd",
30
+ "keyv",
31
+ "storage",
32
+ "adapter",
33
+ "key",
34
+ "value",
35
+ "store",
36
+ "cache",
37
+ "ttl"
38
+ ],
39
+ "author": "Jared Wray <me@jaredwray.com> (https://jaredwray.com)",
40
+ "license": "MIT",
41
+ "bugs": {
42
+ "url": "https://github.com/jaredwray/keyv/issues"
43
+ },
44
+ "homepage": "https://github.com/jaredwray/keyv",
45
+ "dependencies": {
46
+ "cockatiel": "^2.0.2",
47
+ "etcd3": "^1.1.0",
48
+ "pify": "^5.0.0"
49
+ },
50
+ "devDependencies": {
51
+ "@keyv/test-suite": "*",
52
+ "ava": "^3.15.0",
53
+ "eslint-config-xo-lukechilds": "^1.0.1",
54
+ "keyv": "*",
55
+ "nyc": "^15.1.0",
56
+ "requirable": "^1.0.5",
57
+ "this": "^1.1.0",
58
+ "xo": "^0.47.0"
59
+ }
60
+ }
package/src/index.js ADDED
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ const EventEmitter = require('events');
4
+ const { Etcd3 } = require('etcd3');
5
+ const { Policy } = require('cockatiel');
6
+
7
+ class KeyvEtcd extends EventEmitter {
8
+ constructor(url, options) {
9
+ super();
10
+ this.ttlSupport = options && typeof options.ttl === 'number';
11
+ url = url || {};
12
+ if (typeof url === 'string') {
13
+ url = { url };
14
+ }
15
+
16
+ if (url.uri) {
17
+ url = Object.assign({ url: url.uri }, url);
18
+ }
19
+
20
+ if (url.ttl) {
21
+ this.ttlSupport = typeof url.ttl === 'number' ? url.ttl : false;
22
+ }
23
+
24
+ this.opts = Object.assign(
25
+ {
26
+ url: '127.0.0.1:2379',
27
+ },
28
+ url,
29
+ options,
30
+ );
31
+
32
+ this.opts.url = this.opts.url.replace(/^etcd:\/\//, '');
33
+ const policy = Policy.handleAll().retry();
34
+ policy.onFailure(error => {
35
+ this.emit('error', error.reason);
36
+ });
37
+ this.client = new Etcd3(options = { hosts: this.opts.url,
38
+ faultHandling: {
39
+ host: () => policy,
40
+ global: policy,
41
+ },
42
+ });
43
+
44
+ // Https://github.com/microsoft/etcd3/issues/105
45
+ this.client.getRoles().catch(error => this.emit('error', error));
46
+
47
+ if (this.ttlSupport) {
48
+ this.lease = this.client.lease(this.opts.ttl / 1000, {
49
+ autoKeepAlive: false,
50
+ });
51
+ }
52
+ }
53
+
54
+ get(key) {
55
+ return this.client.get(key);
56
+ }
57
+
58
+ set(key, value) {
59
+ return this.opts.ttl ? this.lease.put(key).value(value) : this.client.put(key).value(value);
60
+ }
61
+
62
+ delete(key) {
63
+ if (typeof key !== 'string') {
64
+ return Promise.resolve(false);
65
+ }
66
+
67
+ return this.client.delete().key(key).then(key => key.deleted !== '0');
68
+ }
69
+
70
+ clear() {
71
+ const promise = this.namespace
72
+ ? this.client.delete().prefix(this.namespace)
73
+ : this.client.delete().all();
74
+ return promise.then(() => undefined);
75
+ }
76
+ }
77
+
78
+ module.exports = KeyvEtcd;
package/test/test.js ADDED
@@ -0,0 +1,42 @@
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
+