@keyv/etcd 1.1.5 → 1.2.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,29 @@
1
+ /// <reference types="node" />
2
+ import { EventEmitter } from 'events';
3
+ import type { Lease } from 'etcd3';
4
+ import { Etcd3 } from 'etcd3';
5
+ import type { Store, StoredData } from 'keyv';
6
+ type KeyvEtcdOptions = {
7
+ url?: string;
8
+ uri?: string;
9
+ ttl?: number;
10
+ busyTimeout?: number;
11
+ };
12
+ type GetOutput<Value> = Value | Promise<Value | undefined> | undefined;
13
+ declare class KeyvEtcd<Value = any> extends EventEmitter implements Store<Value> {
14
+ ttlSupport: boolean;
15
+ opts: KeyvEtcdOptions;
16
+ client: Etcd3;
17
+ lease?: Lease;
18
+ namespace?: string;
19
+ constructor(url?: KeyvEtcdOptions | string, options?: KeyvEtcdOptions);
20
+ get(key: string): GetOutput<Value>;
21
+ getMany(keys: string[]): Promise<Array<StoredData<Value>>>;
22
+ set(key: string, value: Value): import("etcd3").PutBuilder;
23
+ delete(key: string): Promise<boolean>;
24
+ deleteMany(keys: string[]): Promise<boolean>;
25
+ clear(): Promise<void>;
26
+ has(key: string): Promise<boolean>;
27
+ disconnect(): void;
28
+ }
29
+ export = KeyvEtcd;
package/dist/index.js ADDED
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-unsafe-argument */
3
+ const events_1 = require("events");
4
+ const etcd3_1 = require("etcd3");
5
+ const cockatiel_1 = require("cockatiel");
6
+ class KeyvEtcd extends events_1.EventEmitter {
7
+ constructor(url, options) {
8
+ super();
9
+ this.ttlSupport = typeof (options === null || options === void 0 ? void 0 : options.ttl) === 'number';
10
+ url = url !== null && url !== void 0 ? url : {};
11
+ if (typeof url === 'string') {
12
+ url = { url };
13
+ }
14
+ if (url.uri) {
15
+ url = Object.assign({ url: url.uri }, url);
16
+ }
17
+ if (url.ttl) {
18
+ this.ttlSupport = typeof url.ttl === 'number';
19
+ }
20
+ this.opts = Object.assign(Object.assign({ url: '127.0.0.1:2379' }, url), options);
21
+ this.opts.url = this.opts.url.replace(/^etcd:\/\//, '');
22
+ const policy = (0, cockatiel_1.retry)(cockatiel_1.handleAll, { backoff: new cockatiel_1.ExponentialBackoff() });
23
+ policy.onFailure(error => {
24
+ this.emit('error', error.reason);
25
+ });
26
+ this.client = new etcd3_1.Etcd3({
27
+ hosts: this.opts.url,
28
+ faultHandling: {
29
+ // @ts-expect-error - iPolicy
30
+ host: () => policy,
31
+ // @ts-expect-error - iPolicy
32
+ global: policy,
33
+ },
34
+ });
35
+ // Https://github.com/microsoft/etcd3/issues/105
36
+ this.client.getRoles().catch(error => this.emit('error', error));
37
+ if (this.ttlSupport) {
38
+ this.lease = this.client.lease(this.opts.ttl / 1000, {
39
+ autoKeepAlive: false,
40
+ });
41
+ }
42
+ }
43
+ get(key) {
44
+ return this.client.get(key);
45
+ }
46
+ getMany(keys) {
47
+ const promises = [];
48
+ for (const key of keys) {
49
+ promises.push(this.get(key));
50
+ }
51
+ return Promise.allSettled(promises)
52
+ .then(values => {
53
+ const data = [];
54
+ for (const value of values) {
55
+ // @ts-expect-error - value is an object
56
+ if (value.value === null) {
57
+ data.push(undefined);
58
+ }
59
+ else {
60
+ // @ts-expect-error - value is an object
61
+ data.push(value.value);
62
+ }
63
+ }
64
+ return data;
65
+ });
66
+ }
67
+ set(key, value) {
68
+ let client = 'client';
69
+ if (this.opts.ttl) {
70
+ client = 'lease';
71
+ }
72
+ // @ts-expect-error - Value needs to be number, string or buffer
73
+ return this[client].put(key).value(value);
74
+ }
75
+ delete(key) {
76
+ if (typeof key !== 'string') {
77
+ return Promise.resolve(false);
78
+ }
79
+ return this.client.delete().key(key).then(key => key.deleted !== '0');
80
+ }
81
+ deleteMany(keys) {
82
+ const promises = [];
83
+ for (const key of keys) {
84
+ promises.push(this.delete(key));
85
+ }
86
+ // @ts-expect-error - x is an object
87
+ return Promise.allSettled(promises).then(values => values.every(x => x.value === true));
88
+ }
89
+ clear() {
90
+ const promise = this.namespace
91
+ ? this.client.delete().prefix(this.namespace)
92
+ : this.client.delete().all();
93
+ return promise.then(() => undefined);
94
+ }
95
+ has(key) {
96
+ return this.client.get(key).exists();
97
+ }
98
+ disconnect() {
99
+ return this.client.close();
100
+ }
101
+ }
102
+ module.exports = KeyvEtcd;
103
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,0DAA0D;AAC1D,mCAAoC;AAEpC,iCAA4B;AAC5B,yCAA+D;AAY/D,MAAM,QAAsB,SAAQ,qBAAY;IAO/C,YAAY,GAA8B,EAAE,OAAyB;QACpE,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,UAAU,GAAG,OAAO,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,CAAA,KAAK,QAAQ,CAAC;QAEnD,GAAG,GAAG,GAAG,aAAH,GAAG,cAAH,GAAG,GAAI,EAAE,CAAC;QAEhB,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC5B,GAAG,GAAG,EAAC,GAAG,EAAC,CAAC;SACZ;QAED,IAAI,GAAG,CAAC,GAAG,EAAE;YACZ,GAAG,mBAAI,GAAG,EAAE,GAAG,CAAC,GAAG,IAAK,GAAG,CAAC,CAAC;SAC7B;QAED,IAAI,GAAG,CAAC,GAAG,EAAE;YACZ,IAAI,CAAC,UAAU,GAAG,OAAO,GAAG,CAAC,GAAG,KAAK,QAAQ,CAAC;SAC9C;QAED,IAAI,CAAC,IAAI,iCACR,GAAG,EAAE,gBAAgB,IAClB,GAAG,GACH,OAAO,CACV,CAAC;QAEF,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAI,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;QAEzD,MAAM,MAAM,GAAG,IAAA,iB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AAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC;IACzF,CAAC;IAED,KAAK;QACJ,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS;YAC7B,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC;YAC7C,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC;QAC9B,OAAO,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;IACtC,CAAC;IAED,GAAG,CAAC,GAAW;QACd,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC;IACtC,CAAC;IAED,UAAU;QACT,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAC5B,CAAC;CACD;AAED,iBAAS,QAAQ,CAAC"}
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@keyv/etcd",
3
- "version": "1.1.5",
3
+ "version": "1.2.0",
4
4
  "description": "Etcd storage adapter for Keyv",
5
- "main": "src/index.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "scripts": {
7
- "build": "echo 'No build step required.'",
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,10 @@
16
17
  "unicorn/no-array-reduce": 0,
17
18
  "unicorn/prefer-object-from-entries": 0,
18
19
  "unicorn/prefer-node-protocol": 0,
19
- "ava/no-ignored-test-files": [
20
- "error",
21
- {
22
- "extensions": [
23
- "js",
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
28
24
  }
29
25
  },
30
26
  "ava": {
@@ -69,16 +65,15 @@
69
65
  "keyv": "*",
70
66
  "requirable": "^1.0.5",
71
67
  "ts-node": "^10.9.1",
72
- "tsd": "^0.28.0",
73
- "typescript": "^5.0.2",
74
- "webpack": "^5.76.2",
75
- "xo": "^0.53.1"
68
+ "tsd": "^0.28.1",
69
+ "typescript": "^5.0.4",
70
+ "webpack": "^5.79.0",
71
+ "xo": "^0.54.1"
76
72
  },
77
73
  "tsd": {
78
74
  "directory": "test"
79
75
  },
80
- "types": "./src/index.d.ts",
81
76
  "files": [
82
- "src"
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;