@keyv/etcd 1.1.5 → 1.2.1
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.d.ts +29 -0
- package/dist/index.js +160 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +17 -22
- package/src/index.d.ts +0 -33
- package/src/index.js +0 -115
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
import { Etcd3, type Lease } from 'etcd3';
|
|
4
|
+
import type { StoredData } from 'keyv';
|
|
5
|
+
import type { ClearOutput, DeleteManyOutput, DeleteOutput, GetOutput, HasOutput, SetOutput } from './types';
|
|
6
|
+
type KeyvEtcdOptions = {
|
|
7
|
+
url?: string;
|
|
8
|
+
uri?: string;
|
|
9
|
+
ttl?: number;
|
|
10
|
+
busyTimeout?: number;
|
|
11
|
+
};
|
|
12
|
+
declare class KeyvEtcd<Value = any> extends EventEmitter {
|
|
13
|
+
ttlSupport: boolean;
|
|
14
|
+
opts: KeyvEtcdOptions;
|
|
15
|
+
client: Etcd3;
|
|
16
|
+
lease?: Lease;
|
|
17
|
+
namespace?: string;
|
|
18
|
+
constructor(url?: KeyvEtcdOptions | string, options?: KeyvEtcdOptions);
|
|
19
|
+
get(key: string): GetOutput<Value>;
|
|
20
|
+
getMany(keys: string[]): Promise<Array<StoredData<Value>>>;
|
|
21
|
+
set(key: string, value: Value): SetOutput;
|
|
22
|
+
delete(key: string): DeleteOutput;
|
|
23
|
+
deleteMany(keys: string[]): DeleteManyOutput;
|
|
24
|
+
clear(): ClearOutput;
|
|
25
|
+
iterator(namespace?: string): AsyncGenerator<(string | Awaited<Value> | undefined)[], void, unknown>;
|
|
26
|
+
has(key: string): HasOutput;
|
|
27
|
+
disconnect(): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
export = KeyvEtcd;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
|
12
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
13
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
14
|
+
var m = o[Symbol.asyncIterator], i;
|
|
15
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
16
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
17
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
18
|
+
};
|
|
19
|
+
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
20
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
21
|
+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
22
|
+
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
23
|
+
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
|
|
24
|
+
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
25
|
+
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
26
|
+
function fulfill(value) { resume("next", value); }
|
|
27
|
+
function reject(value) { resume("throw", value); }
|
|
28
|
+
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
29
|
+
};
|
|
30
|
+
const events_1 = require("events");
|
|
31
|
+
const etcd3_1 = require("etcd3");
|
|
32
|
+
class KeyvEtcd extends events_1.EventEmitter {
|
|
33
|
+
constructor(url, options) {
|
|
34
|
+
super();
|
|
35
|
+
this.ttlSupport = typeof (options === null || options === void 0 ? void 0 : options.ttl) === 'number';
|
|
36
|
+
url = url !== null && url !== void 0 ? url : {};
|
|
37
|
+
if (typeof url === 'string') {
|
|
38
|
+
url = { url };
|
|
39
|
+
}
|
|
40
|
+
if (url.uri) {
|
|
41
|
+
url = Object.assign({ url: url.uri }, url);
|
|
42
|
+
}
|
|
43
|
+
if (url.ttl) {
|
|
44
|
+
this.ttlSupport = typeof url.ttl === 'number';
|
|
45
|
+
}
|
|
46
|
+
this.opts = Object.assign(Object.assign({ url: '127.0.0.1:2379' }, url), options);
|
|
47
|
+
this.opts.url = this.opts.url.replace(/^etcd:\/\//, '');
|
|
48
|
+
this.client = new etcd3_1.Etcd3({
|
|
49
|
+
hosts: this.opts.url,
|
|
50
|
+
});
|
|
51
|
+
// Https://github.com/microsoft/etcd3/issues/105
|
|
52
|
+
this.client.getRoles().catch(error => this.emit('error', error));
|
|
53
|
+
if (this.ttlSupport) {
|
|
54
|
+
this.lease = this.client.lease(this.opts.ttl / 1000, {
|
|
55
|
+
autoKeepAlive: false,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
get(key) {
|
|
60
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
return this.client.get(key);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
getMany(keys) {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
const promises = [];
|
|
67
|
+
for (const key of keys) {
|
|
68
|
+
promises.push(this.get(key));
|
|
69
|
+
}
|
|
70
|
+
return Promise.allSettled(promises)
|
|
71
|
+
.then(values => {
|
|
72
|
+
const data = [];
|
|
73
|
+
for (const value of values) {
|
|
74
|
+
// @ts-expect-error - value is an object
|
|
75
|
+
if (value.value === null) {
|
|
76
|
+
data.push(undefined);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
// @ts-expect-error - value is an object
|
|
80
|
+
data.push(value.value);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return data;
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
set(key, value) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
let client = 'client';
|
|
90
|
+
if (this.opts.ttl) {
|
|
91
|
+
client = 'lease';
|
|
92
|
+
}
|
|
93
|
+
// @ts-expect-error - Value needs to be number, string or buffer
|
|
94
|
+
return this[client].put(key).value(value);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
delete(key) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
if (typeof key !== 'string') {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
return this.client.delete().key(key).then(key => key.deleted !== '0');
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
deleteMany(keys) {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
const promises = [];
|
|
108
|
+
for (const key of keys) {
|
|
109
|
+
promises.push(this.delete(key));
|
|
110
|
+
}
|
|
111
|
+
// @ts-expect-error - x is an object
|
|
112
|
+
return Promise.allSettled(promises).then(values => values.every(x => x.value === true));
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
clear() {
|
|
116
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
const promise = this.namespace
|
|
118
|
+
? this.client.delete().prefix(this.namespace)
|
|
119
|
+
: this.client.delete().all();
|
|
120
|
+
return promise.then(() => undefined);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
iterator(namespace) {
|
|
124
|
+
return __asyncGenerator(this, arguments, function* iterator_1() {
|
|
125
|
+
var _a, e_1, _b, _c;
|
|
126
|
+
const iterator = yield __await(this.client
|
|
127
|
+
.getAll()
|
|
128
|
+
.prefix(namespace ? namespace + ':' : '')
|
|
129
|
+
.keys());
|
|
130
|
+
try {
|
|
131
|
+
for (var _d = true, iterator_2 = __asyncValues(iterator), iterator_2_1; iterator_2_1 = yield __await(iterator_2.next()), _a = iterator_2_1.done, !_a; _d = true) {
|
|
132
|
+
_c = iterator_2_1.value;
|
|
133
|
+
_d = false;
|
|
134
|
+
const key = _c;
|
|
135
|
+
const value = yield __await(this.get(key));
|
|
136
|
+
yield yield __await([key, value]);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
140
|
+
finally {
|
|
141
|
+
try {
|
|
142
|
+
if (!_d && !_a && (_b = iterator_2.return)) yield __await(_b.call(iterator_2));
|
|
143
|
+
}
|
|
144
|
+
finally { if (e_1) throw e_1.error; }
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
has(key) {
|
|
149
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
150
|
+
return this.client.get(key).exists();
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
disconnect() {
|
|
154
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
155
|
+
return this.client.close();
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158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SAAS,CAAC,CAAC,CAAC,SAAS,GAAG,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;iBACxC,IAAI,EAAE,CAAA,CAAC;;gBAET,KAAwB,eAAA,aAAA,cAAA,QAAQ,CAAA,cAAA,2FAAE;oBAAV,wBAAQ;oBAAR,WAAQ;oBAArB,MAAM,GAAG,KAAA,CAAA;oBACnB,MAAM,KAAK,GAAG,cAAM,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA,CAAC;oBAClC,oBAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA,CAAC;iBACnB;;;;;;;;;QACF,CAAC;KAAA;IAEK,GAAG,CAAC,GAAW;;YACpB,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC;QACtC,CAAC;KAAA;IAEK,UAAU;;YACf,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QAC5B,CAAC;KAAA;CACD;AAED,iBAAS,QAAQ,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type StoredData } from 'keyv';
|
|
2
|
+
export type GetOutput<Value> = Promise<Value | undefined>;
|
|
3
|
+
export type GetManyOutput<Value> = Promise<Array<StoredData<Value | undefined> | undefined>>;
|
|
4
|
+
export type SetOutput = Promise<any>;
|
|
5
|
+
export type DeleteOutput = Promise<boolean>;
|
|
6
|
+
export type DeleteManyOutput = Promise<boolean>;
|
|
7
|
+
export type ClearOutput = Promise<void>;
|
|
8
|
+
export type HasOutput = Promise<boolean>;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keyv/etcd",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Etcd storage adapter for Keyv",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
|
-
"build": "
|
|
8
|
+
"build": "tsc --project tsconfig.dist.json",
|
|
8
9
|
"prepare": "yarn build",
|
|
9
10
|
"test": "xo && c8 ava --serial",
|
|
10
11
|
"test:ci": "xo && ava --serial",
|
|
@@ -16,15 +17,11 @@
|
|
|
16
17
|
"unicorn/no-array-reduce": 0,
|
|
17
18
|
"unicorn/prefer-object-from-entries": 0,
|
|
18
19
|
"unicorn/prefer-node-protocol": 0,
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"ts"
|
|
25
|
-
]
|
|
26
|
-
}
|
|
27
|
-
]
|
|
20
|
+
"import/extensions": 0,
|
|
21
|
+
"@typescript-eslint/no-confusing-void-expression": 0,
|
|
22
|
+
"@typescript-eslint/promise-function-async": 0,
|
|
23
|
+
"eslint-comments/no-unused-disable": 0,
|
|
24
|
+
"unicorn/prefer-event-target": 0
|
|
28
25
|
}
|
|
29
26
|
},
|
|
30
27
|
"ava": {
|
|
@@ -59,26 +56,24 @@
|
|
|
59
56
|
},
|
|
60
57
|
"homepage": "https://github.com/jaredwray/keyv",
|
|
61
58
|
"dependencies": {
|
|
62
|
-
"
|
|
63
|
-
"etcd3": "^1.1.0"
|
|
59
|
+
"etcd3": "^1.1.2"
|
|
64
60
|
},
|
|
65
61
|
"devDependencies": {
|
|
66
62
|
"@keyv/test-suite": "*",
|
|
67
|
-
"ava": "^5.
|
|
68
|
-
"c8": "^
|
|
63
|
+
"ava": "^5.3.1",
|
|
64
|
+
"c8": "^8.0.1",
|
|
69
65
|
"keyv": "*",
|
|
70
66
|
"requirable": "^1.0.5",
|
|
71
67
|
"ts-node": "^10.9.1",
|
|
72
|
-
"tsd": "^0.28.
|
|
73
|
-
"typescript": "^5.
|
|
74
|
-
"webpack": "^5.
|
|
75
|
-
"xo": "^0.
|
|
68
|
+
"tsd": "^0.28.1",
|
|
69
|
+
"typescript": "^5.1.6",
|
|
70
|
+
"webpack": "^5.88.2",
|
|
71
|
+
"xo": "^0.56.0"
|
|
76
72
|
},
|
|
77
73
|
"tsd": {
|
|
78
74
|
"directory": "test"
|
|
79
75
|
},
|
|
80
|
-
"types": "./src/index.d.ts",
|
|
81
76
|
"files": [
|
|
82
|
-
"
|
|
77
|
+
"dist"
|
|
83
78
|
]
|
|
84
79
|
}
|
package/src/index.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import {EventEmitter} from 'events';
|
|
2
|
-
import type Etcd3 from 'etcd3';
|
|
3
|
-
import type {Store, StoredData} from 'keyv';
|
|
4
|
-
|
|
5
|
-
declare class KeyvEtcd<Value=any> extends EventEmitter implements Store<Value> {
|
|
6
|
-
ttlSupport: any;
|
|
7
|
-
opts: any;
|
|
8
|
-
client: Etcd3;
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
|
10
|
-
lease: import('etcd3').Lease;
|
|
11
|
-
constructor(options?: string | KeyvEtcd.Options);
|
|
12
|
-
get(key: string): Promise<Value>;
|
|
13
|
-
getMany?(
|
|
14
|
-
keys: string[]
|
|
15
|
-
): Array<StoredData<Value>> | Promise<Array<StoredData<Value>>> | undefined;
|
|
16
|
-
set(key: string, value: Value, ttl?: number): any;
|
|
17
|
-
delete(key: string): boolean | Promise<boolean>;
|
|
18
|
-
deleteMany(keys: string[]): boolean;
|
|
19
|
-
clear(): void | Promise<void>;
|
|
20
|
-
iterator(namespace: string | undefined): AsyncGenerator<any, void, any>;
|
|
21
|
-
has?(key: string): boolean | Promise<boolean>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
declare namespace KeyvEtcd {
|
|
25
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
26
|
-
interface Options {
|
|
27
|
-
url?: string | undefined;
|
|
28
|
-
uri?: string | undefined;
|
|
29
|
-
ttl?: number | undefined;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export = KeyvEtcd;
|
package/src/index.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const EventEmitter = require('events');
|
|
4
|
-
const {Etcd3} = require('etcd3');
|
|
5
|
-
const {handleAll, retry, ExponentialBackoff} = 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 = {url: url.uri, ...url};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (url.ttl) {
|
|
21
|
-
this.ttlSupport = typeof url.ttl === 'number';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
this.opts = {
|
|
25
|
-
url: '127.0.0.1:2379',
|
|
26
|
-
...url,
|
|
27
|
-
...options,
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
this.opts.url = this.opts.url.replace(/^etcd:\/\//, '');
|
|
31
|
-
const policy = retry(handleAll, {backoff: new ExponentialBackoff()});
|
|
32
|
-
policy.onFailure(error => {
|
|
33
|
-
this.emit('error', error.reason);
|
|
34
|
-
});
|
|
35
|
-
this.client = new Etcd3(options = {hosts: this.opts.url,
|
|
36
|
-
faultHandling: {
|
|
37
|
-
host: () => policy,
|
|
38
|
-
global: policy,
|
|
39
|
-
},
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
// Https://github.com/microsoft/etcd3/issues/105
|
|
43
|
-
this.client.getRoles().catch(error => this.emit('error', error));
|
|
44
|
-
|
|
45
|
-
if (this.ttlSupport) {
|
|
46
|
-
this.lease = this.client.lease(this.opts.ttl / 1000, {
|
|
47
|
-
autoKeepAlive: false,
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
get(key) {
|
|
53
|
-
return this.client.get(key);
|
|
54
|
-
}
|
|
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;
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
set(key, value) {
|
|
78
|
-
return this.opts.ttl ? this.lease.put(key).value(value) : this.client.put(key).value(value);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
delete(key) {
|
|
82
|
-
if (typeof key !== 'string') {
|
|
83
|
-
return Promise.resolve(false);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return this.client.delete().key(key).then(key => key.deleted !== '0');
|
|
87
|
-
}
|
|
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
|
-
|
|
99
|
-
clear() {
|
|
100
|
-
const promise = this.namespace
|
|
101
|
-
? this.client.delete().prefix(this.namespace)
|
|
102
|
-
: this.client.delete().all();
|
|
103
|
-
return promise.then(() => undefined);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
has(key) {
|
|
107
|
-
return this.client.get(key).exists();
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
disconnect() {
|
|
111
|
-
return this.client.close();
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
module.exports = KeyvEtcd;
|