keyv 4.0.5 → 4.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.
package/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  > Simple key-value storage with support for multiple backends
8
8
 
9
- [![build](https://github.com/jaredwray/keyv/actions/workflows/build.yaml/badge.svg)](https://github.com/jaredwray/keyv/actions/workflows/build.yaml)
9
+ [![build](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml/badge.svg)](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml)
10
10
  [![codecov](https://codecov.io/gh/jaredwray/keyv/branch/master/graph/badge.svg?token=bRzR3RyOXZ)](https://codecov.io/gh/jaredwray/keyv)
11
11
  [![npm](https://img.shields.io/npm/dm/keyv.svg)](https://www.npmjs.com/package/keyv)
12
12
  [![npm](https://img.shields.io/npm/v/keyv.svg)](https://www.npmjs.com/package/keyv)
@@ -44,6 +44,7 @@ npm install --save @keyv/mongo
44
44
  npm install --save @keyv/sqlite
45
45
  npm install --save @keyv/postgres
46
46
  npm install --save @keyv/mysql
47
+ npm install --save @keyv/etcd
47
48
  ```
48
49
 
49
50
  Create a new Keyv instance, passing your connection string if applicable. Keyv will automatically load the correct storage adapter.
@@ -58,6 +59,7 @@ const keyv = new Keyv('mongodb://user:pass@localhost:27017/dbname');
58
59
  const keyv = new Keyv('sqlite://path/to/database.sqlite');
59
60
  const keyv = new Keyv('postgresql://user:pass@localhost:5432/dbname');
60
61
  const keyv = new Keyv('mysql://user:pass@localhost:3306/dbname');
62
+ const keyv = new Keyv('etcd://localhost:2379');
61
63
 
62
64
  // Handle DB connection errors
63
65
  keyv.on('error', err => console.log('Connection Error', err));
@@ -100,7 +102,7 @@ const keyv = new Keyv({ serialize: JSON.stringify, deserialize: JSON.parse });
100
102
 
101
103
  ## Official Storage Adapters
102
104
 
103
- The official storage adapters are covered by [over 150 integration tests](https://github.com/jaredwray/keyv/actions/workflows/build.yaml) to guarantee consistent behaviour. They are lightweight, efficient wrappers over the DB clients making use of indexes and native TTLs where available.
105
+ The official storage adapters are covered by [over 150 integration tests](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml) to guarantee consistent behaviour. They are lightweight, efficient wrappers over the DB clients making use of indexes and native TTLs where available.
104
106
 
105
107
  Database | Adapter | Native TTL
106
108
  ---|---|---
@@ -109,6 +111,7 @@ MongoDB | [@keyv/mongo](https://github.com/jaredwray/keyv/tree/master/packages/m
109
111
  SQLite | [@keyv/sqlite](https://github.com/jaredwray/keyv/tree/master/packages/sqlite) | No
110
112
  PostgreSQL | [@keyv/postgres](https://github.com/jaredwray/keyv/tree/master/packages/postgres) | No
111
113
  MySQL | [@keyv/mysql](https://github.com/jaredwray/keyv/tree/master/packages/mysql) | No
114
+ Etcd | [@keyv/etcd](https://github.com/jaredwray/keyv/tree/master/packages/etcd) | Yes
112
115
 
113
116
  ## Third-party Storage Adapters
114
117
 
@@ -286,6 +289,84 @@ Delete all entries in the current namespace.
286
289
 
287
290
  Returns a promise which is resolved when the entries have been cleared.
288
291
 
292
+ # How to Contribute
293
+
294
+ In this section of the documentation we will cover:
295
+
296
+ 1) How to set up this repository locally
297
+ 2) How to get started with running commands
298
+ 3) How to contribute changes using Pull Requests
299
+
300
+ ## Dependencies
301
+
302
+ This package requires the following dependencies to run:
303
+
304
+ 1) [Yarn V1](https://yarnpkg.com/getting-started/install)
305
+ 2) [Lerna](https://lerna.js.org/)
306
+ 3) [Docker](https://docs.docker.com/get-docker/)
307
+
308
+ ## Setting up your workspace
309
+
310
+ To contribute to this repository, start by setting up this project locally:
311
+
312
+ 1) Fork this repository into your Git account
313
+ 2) Clone the forked repository to your local directory using `git clone`
314
+ 3) Install any of the above missing dependencies
315
+
316
+ ## Launching the project
317
+
318
+ Once the project is installed locally, you are ready to start up its services:
319
+
320
+ 1) Ensure that your Docker service is running.
321
+ 2) From the root directory of your project, run the `yarn` command in the command prompt to install yarn.
322
+ 3) Run the `yarn bootstrap` command to install any necessary dependencies.
323
+ 4) Run `yarn test:services:start` to start up this project's Docker container. The container will launch all services within your workspace.
324
+
325
+ ## Available Commands
326
+
327
+ Once the project is running, you can execute a variety of commands. The root workspace and each subpackage contain a `package.json` file with a `scripts` field listing all the commands that can be executed from that directory. This project also supports native `yarn`, `lerna`, and `docker` commands.
328
+
329
+ Here, we'll cover the primary commands that can be executed from the root directory. Unless otherwise noted, these commands can also be executed from a subpackage. If executed from a subpackage, they will only affect that subpackage, rather than the entire workspace.
330
+
331
+ ### `yarn`
332
+
333
+ The `yarn` command installs yarn in the workspace.
334
+
335
+ ### `yarn bootstrap`
336
+
337
+ The `yarn bootstrap` command installs all dependencies in the workspace.
338
+
339
+ ### `yarn test:services:start`
340
+
341
+ The `yarn test:services:start` command starts up the project's Docker container, launching all services in the workspace. This command must be executed from the root directory.
342
+
343
+ ### `yarn test:services:stop`
344
+
345
+ The `yarn test:services:stop` command brings down the project's Docker container, halting all services. This command must be executed from the root directory.
346
+
347
+ ### `yarn test`
348
+
349
+ The `yarn test` command runs all tests in the workspace.
350
+
351
+ ### `yarn clean`
352
+
353
+ The `yarn clean` command removes yarn and all dependencies installed by yarn. After executing this command, you must repeat the steps in *Setting up your workspace* to rebuild your workspace.
354
+
355
+ ## Contributing Changes
356
+
357
+ Now that you've set up your workspace, you're ready to contribute changes to the `keyv` repository.
358
+
359
+ 1) Make any changes that you would like to contribute in your local workspace.
360
+ 2) After making these changes, ensure that the project's tests still pass by executing the `yarn test` command in the root directory.
361
+ 3) Commit your changes and push them to your forked repository.
362
+ 4) Navigate to the original `keyv` repository and go the *Pull Requests* tab.
363
+ 5) Click the *New pull request* button, and open a pull request for the branch in your repository that contains your changes.
364
+ 6) Once your pull request is created, ensure that all checks have passed and that your branch has no conflicts with the base branch. If there are any issues, resolve these changes in your local repository, and then commit and push them to git.
365
+ 7) Similarly, respond to any reviewer comments or requests for changes by making edits to your local repository and pushing them to Git.
366
+ 8) Once the pull request has been reviewed, those with write access to the branch will be able to merge your changes into the `keyv` repository.
367
+
368
+ If you need more information on the steps to create a pull request, you can find a detailed walkthrough in the [Github documentation](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork)
369
+
289
370
  ## License
290
371
 
291
372
  MIT © Jared Wray & Luke Childs
package/package.json CHANGED
@@ -1,52 +1,58 @@
1
1
  {
2
- "name": "keyv",
3
- "version": "4.0.5",
4
- "description": "Simple key-value storage with support for multiple backends",
5
- "main": "src/index.js",
6
- "scripts": {
7
- "test": "xo && nyc ava --serial",
8
- "coverage": "nyc report --reporter=text-lcov > coverage.lcov",
9
- "clean": "rm -rf node_modules && rm -rf .nyc_output && rm -rf coverage.lcov && rm -rf ./test/testdb.sqlite"
10
- },
11
- "xo": {
12
- "extends": "xo-lukechilds",
13
- "rules": {
14
- "unicorn/prefer-module": 0
15
- }
16
- },
17
- "repository": {
18
- "type": "git",
19
- "url": "git+https://github.com/jaredwray/keyv.git"
20
- },
21
- "keywords": [
22
- "key",
23
- "value",
24
- "store",
25
- "cache",
26
- "ttl"
27
- ],
28
- "author": "Jared Wray <me@jaredwray.com> (http://jaredwray.com)",
29
- "license": "MIT",
30
- "bugs": {
31
- "url": "https://github.com/jaredwray/keyv/issues"
32
- },
33
- "homepage": "https://github.com/jaredwray/keyv",
34
- "dependencies": {
35
- "json-buffer": "3.0.1"
36
- },
37
- "devDependencies": {
38
- "@keyv/mongo": "*",
39
- "@keyv/mysql": "*",
40
- "@keyv/postgres": "*",
41
- "@keyv/redis": "*",
42
- "@keyv/sqlite": "*",
43
- "@keyv/test-suite": "*",
44
- "ava": "^3.15.0",
45
- "eslint-config-xo-lukechilds": "^1.0.0",
46
- "eslint-plugin-promise": "^5.1.1",
47
- "nyc": "^15.1.0",
48
- "this": "^1.0.2",
49
- "timekeeper": "^2.0.0",
50
- "xo": "^0.46.3"
51
- }
2
+ "name": "keyv",
3
+ "version": "4.2.0",
4
+ "description": "Simple key-value storage with support for multiple backends",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "test": "xo && nyc ava --serial",
8
+ "coverage": "nyc report --reporter=text-lcov > coverage.lcov",
9
+ "clean": "rm -rf node_modules && rm -rf .nyc_output && rm -rf coverage.lcov && rm -rf ./test/testdb.sqlite"
10
+ },
11
+ "xo": {
12
+ "extends": "xo-lukechilds",
13
+ "rules": {
14
+ "unicorn/prefer-module": 0
15
+ }
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/jaredwray/keyv.git"
20
+ },
21
+ "keywords": [
22
+ "key",
23
+ "value",
24
+ "store",
25
+ "cache",
26
+ "ttl"
27
+ ],
28
+ "author": "Jared Wray <me@jaredwray.com> (http://jaredwray.com)",
29
+ "license": "MIT",
30
+ "bugs": {
31
+ "url": "https://github.com/jaredwray/keyv/issues"
32
+ },
33
+ "homepage": "https://github.com/jaredwray/keyv",
34
+ "dependencies": {
35
+ "compress-brotli": "^1.3.6",
36
+ "json-buffer": "3.0.1"
37
+ },
38
+ "devDependencies": {
39
+ "@keyv/test-suite": "*",
40
+ "ava": "^4.1.0",
41
+ "eslint-config-xo-lukechilds": "^1.0.1",
42
+ "eslint-plugin-promise": "^6.0.0",
43
+ "nyc": "^15.1.0",
44
+ "pify": "5.0.0",
45
+ "this": "^1.1.0",
46
+ "timekeeper": "^2.2.0",
47
+ "tsd": "^0.20.0",
48
+ "typescript": "^4.6.3",
49
+ "xo": "^0.48.0"
50
+ },
51
+ "tsd" : {
52
+ "directory" : "test"
53
+ },
54
+ "types": "./src/index.d.ts",
55
+ "files": [
56
+ "src"
57
+ ]
52
58
  }
package/src/index.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import EventEmitter from 'node:events';
2
+
3
+ declare class Keyv extends EventEmitter {
4
+ constructor(uri: string, options: Keyv.Options);
5
+ get(key: string, options: any): Promise<any>;
6
+ set(key: string, value: any, ttl: number): Promise<boolean>;
7
+ delete(key: string): Promise<boolean>;
8
+ clear(): Promise<void>;
9
+ has(key: string): Promise<boolean>;
10
+ }
11
+
12
+ declare namespace Keyv {
13
+ interface Options {
14
+ namespace?: string | undefined;
15
+ serialize?: ((data: any) => string) | undefined;
16
+ deserialize?: ((data: string) => any | undefined) | undefined;
17
+ ttl?: number | undefined;
18
+ }
19
+ }
20
+
21
+ export = Keyv;
package/src/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  const EventEmitter = require('events');
4
4
  const JSONB = require('json-buffer');
5
+ const compressBrotli = require('compress-brotli');
5
6
 
6
7
  const loadStore = options => {
7
8
  const adapters = {
@@ -12,6 +13,7 @@ const loadStore = options => {
12
13
  postgresql: '@keyv/postgres',
13
14
  postgres: '@keyv/postgres',
14
15
  mysql: '@keyv/mysql',
16
+ etcd: '@keyv/etcd',
15
17
  };
16
18
  if (options.adapter || options.uri) {
17
19
  const adapter = options.adapter || /^[^:]*/.exec(options.uri)[0];
@@ -21,49 +23,168 @@ const loadStore = options => {
21
23
  return new Map();
22
24
  };
23
25
 
26
+ const iterableAdapters = [
27
+ 'sqlite',
28
+ 'postgres',
29
+ 'mysql',
30
+ 'mongo',
31
+ 'redis',
32
+ ];
33
+
24
34
  class Keyv extends EventEmitter {
25
35
  constructor(uri, options) {
26
36
  super();
27
- this.opts = Object.assign(
28
- {
29
- namespace: 'keyv',
30
- serialize: JSONB.stringify,
31
- deserialize: JSONB.parse,
32
- },
33
- (typeof uri === 'string') ? { uri } : uri,
34
- options,
35
- );
37
+ this.opts = {
38
+ namespace: 'keyv',
39
+ serialize: JSONB.stringify,
40
+ deserialize: JSONB.parse,
41
+ ...((typeof uri === 'string') ? { uri } : uri),
42
+ ...options,
43
+ };
36
44
 
37
45
  if (!this.opts.store) {
38
- const adapterOptions = Object.assign({}, this.opts);
46
+ const adapterOptions = { ...this.opts };
39
47
  this.opts.store = loadStore(adapterOptions);
40
48
  }
41
49
 
50
+ if (this.opts.compress) {
51
+ const brotli = compressBrotli(this.opts.compress.opts);
52
+ this.opts.serialize = async ({ value, expires }) => brotli.serialize({ value: await brotli.compress(value), expires });
53
+ this.opts.deserialize = async data => {
54
+ const { value, expires } = brotli.deserialize(data);
55
+ return { value: await brotli.decompress(value), expires };
56
+ };
57
+ }
58
+
42
59
  if (typeof this.opts.store.on === 'function') {
43
60
  this.opts.store.on('error', error => this.emit('error', error));
44
61
  }
45
62
 
46
63
  this.opts.store.namespace = this.opts.namespace;
64
+
65
+ const generateIterator = iterator =>
66
+ async function * () {
67
+ for await (const [key, raw] of typeof iterator === 'function'
68
+ ? iterator(this.opts.store.namespace)
69
+ : iterator) {
70
+ const data = typeof raw === 'string' ? this.opts.deserialize(raw) : raw;
71
+ if (this.opts.store.namespace && !key.includes(this.opts.store.namespace)) {
72
+ continue;
73
+ }
74
+
75
+ if (typeof data.expires === 'number' && Date.now() > data.expires) {
76
+ this.delete(key);
77
+ continue;
78
+ }
79
+
80
+ yield [this._getKeyUnprefix(key), data.value];
81
+ }
82
+ };
83
+
84
+ // Attach iterators
85
+ if (typeof this.opts.store[Symbol.iterator] === 'function' && this.opts.store instanceof Map) {
86
+ this.iterator = generateIterator(this.opts.store);
87
+ } else if (typeof this.opts.store.iterator === 'function' && this.opts.store.opts
88
+ && this._checkIterableAdaptar()) {
89
+ this.iterator = generateIterator(this.opts.store.iterator.bind(this.opts.store));
90
+ }
91
+ }
92
+
93
+ _checkIterableAdaptar() {
94
+ return iterableAdapters.includes(this.opts.store.opts.dialect)
95
+ || iterableAdapters.findIndex(element => this.opts.store.opts.url.includes(element)) >= 0;
47
96
  }
48
97
 
49
98
  _getKeyPrefix(key) {
50
99
  return `${this.opts.namespace}:${key}`;
51
100
  }
52
101
 
102
+ _getKeyPrefixArray(keys) {
103
+ return keys.map(key => `${this.opts.namespace}:${key}`);
104
+ }
105
+
106
+ _getKeyUnprefix(key) {
107
+ return this.opts.store.namespace
108
+ ? key
109
+ .split(':')
110
+ .splice(1)
111
+ .join(':')
112
+ : key;
113
+ }
114
+
53
115
  get(key, options) {
54
- const keyPrefixed = this._getKeyPrefix(key);
55
116
  const { store } = this.opts;
117
+ const isArray = Array.isArray(key);
118
+ const keyPrefixed = isArray ? this._getKeyPrefixArray(key) : this._getKeyPrefix(key);
119
+ if (isArray && store.getMany === undefined) {
120
+ const promises = [];
121
+ for (const key of keyPrefixed) {
122
+ promises.push(Promise.resolve()
123
+ .then(() => store.get(key))
124
+ .then(data => (typeof data === 'string') ? this.opts.deserialize(data) : data)
125
+ .then(data => {
126
+ if (data === undefined || data === null) {
127
+ return undefined;
128
+ }
129
+
130
+ if (typeof data.expires === 'number' && Date.now() > data.expires) {
131
+ return this.delete(key).then(() => undefined);
132
+ }
133
+
134
+ return (options && options.raw) ? data : data.value;
135
+ }),
136
+ );
137
+ }
138
+
139
+ return Promise.allSettled(promises)
140
+ .then(values => {
141
+ const data = [];
142
+ for (const value of values) {
143
+ data.push(value.value);
144
+ }
145
+
146
+ return data.every(x => x === undefined) ? [] : data;
147
+ });
148
+ }
149
+
56
150
  return Promise.resolve()
57
- .then(() => store.get(keyPrefixed))
151
+ .then(() => isArray ? store.getMany(keyPrefixed) : store.get(keyPrefixed))
58
152
  .then(data => (typeof data === 'string') ? this.opts.deserialize(data) : data)
59
153
  .then(data => {
60
- if (data === undefined) {
154
+ // Console.log('get', data);
155
+ if (data === undefined || data === null) {
61
156
  return undefined;
62
157
  }
63
158
 
159
+ if (isArray) {
160
+ const result = [];
161
+ if (data.length === 0) {
162
+ return [];
163
+ }
164
+
165
+ for (let row of data) {
166
+ if ((typeof row === 'string')) {
167
+ row = this.opts.deserialize(row);
168
+ }
169
+
170
+ if (row === undefined || row === null) {
171
+ result.push(undefined);
172
+ continue;
173
+ }
174
+
175
+ if (typeof row.expires === 'number' && Date.now() > row.expires) {
176
+ this.delete(key).then(() => undefined);
177
+ result.push(undefined);
178
+ } else {
179
+ result.push((options && options.raw) ? row : row.value);
180
+ }
181
+ }
182
+
183
+ return result.every(x => x === undefined) ? [] : result;
184
+ }
185
+
64
186
  if (typeof data.expires === 'number' && Date.now() > data.expires) {
65
- this.delete(key);
66
- return undefined;
187
+ return this.delete(key).then(() => undefined);
67
188
  }
68
189
 
69
190
  return (options && options.raw) ? data : data.value;
@@ -85,6 +206,10 @@ class Keyv extends EventEmitter {
85
206
  return Promise.resolve()
86
207
  .then(() => {
87
208
  const expires = (typeof ttl === 'number') ? (Date.now() + ttl) : null;
209
+ if (typeof value === 'symbol') {
210
+ this.emit('error', 'symbol cannot be serialized');
211
+ }
212
+
88
213
  value = { value, expires };
89
214
  return this.opts.serialize(value);
90
215
  })
@@ -93,8 +218,24 @@ class Keyv extends EventEmitter {
93
218
  }
94
219
 
95
220
  delete(key) {
96
- const keyPrefixed = this._getKeyPrefix(key);
97
221
  const { store } = this.opts;
222
+ if (Array.isArray(key)) {
223
+ const keyPrefixed = this._getKeyPrefixArray(key);
224
+ if (store.deleteMany === undefined) {
225
+ const promises = [];
226
+ for (const key of keyPrefixed) {
227
+ promises.push(store.delete(key));
228
+ }
229
+
230
+ return Promise.allSettled(promises)
231
+ .then(values => values.every(x => x.value === true));
232
+ }
233
+
234
+ return Promise.resolve()
235
+ .then(() => store.deleteMany(keyPrefixed));
236
+ }
237
+
238
+ const keyPrefixed = this._getKeyPrefix(key);
98
239
  return Promise.resolve()
99
240
  .then(() => store.delete(keyPrefixed));
100
241
  }
@@ -104,6 +245,20 @@ class Keyv extends EventEmitter {
104
245
  return Promise.resolve()
105
246
  .then(() => store.clear());
106
247
  }
248
+
249
+ has(key) {
250
+ const keyPrefixed = this._getKeyPrefix(key);
251
+ const { store } = this.opts;
252
+ return Promise.resolve()
253
+ .then(async () => {
254
+ if (typeof store.has === 'function') {
255
+ return store.has(keyPrefixed);
256
+ }
257
+
258
+ const value = await store.get(keyPrefixed);
259
+ return value !== undefined;
260
+ });
261
+ }
107
262
  }
108
263
 
109
264
  module.exports = Keyv;
@@ -1 +0,0 @@
1
- {"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/keyv/src/index.js":{"path":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/keyv/src/index.js","statementMap":{"0":{"start":{"line":3,"column":21},"end":{"line":3,"column":38}},"1":{"start":{"line":4,"column":14},"end":{"line":4,"column":36}},"2":{"start":{"line":6,"column":18},"end":{"line":22,"column":1}},"3":{"start":{"line":7,"column":18},"end":{"line":15,"column":2}},"4":{"start":{"line":16,"column":1},"end":{"line":19,"column":2}},"5":{"start":{"line":17,"column":18},"end":{"line":17,"column":66}},"6":{"start":{"line":18,"column":2},"end":{"line":18,"column":51}},"7":{"start":{"line":21,"column":1},"end":{"line":21,"column":18}},"8":{"start":{"line":26,"column":2},"end":{"line":26,"column":10}},"9":{"start":{"line":27,"column":2},"end":{"line":35,"column":4}},"10":{"start":{"line":37,"column":2},"end":{"line":40,"column":3}},"11":{"start":{"line":38,"column":26},"end":{"line":38,"column":54}},"12":{"start":{"line":39,"column":3},"end":{"line":39,"column":47}},"13":{"start":{"line":42,"column":2},"end":{"line":44,"column":3}},"14":{"start":{"line":43,"column":3},"end":{"line":43,"column":67}},"15":{"start":{"line":43,"column":40},"end":{"line":43,"column":65}},"16":{"start":{"line":46,"column":2},"end":{"line":46,"column":50}},"17":{"start":{"line":50,"column":2},"end":{"line":50,"column":41}},"18":{"start":{"line":54,"column":22},"end":{"line":54,"column":45}},"19":{"start":{"line":55,"column":20},"end":{"line":55,"column":29}},"20":{"start":{"line":56,"column":2},"end":{"line":70,"column":6}},"21":{"start":{"line":57,"column":15},"end":{"line":57,"column":37}},"22":{"start":{"line":58,"column":17},"end":{"line":58,"column":80}},"23":{"start":{"line":60,"column":4},"end":{"line":62,"column":5}},"24":{"start":{"line":61,"column":5},"end":{"line":61,"column":22}},"25":{"start":{"line":64,"column":4},"end":{"line":67,"column":5}},"26":{"start":{"line":65,"column":5},"end":{"line":65,"column":22}},"27":{"start":{"line":66,"column":5},"end":{"line":66,"column":22}},"28":{"start":{"line":69,"column":4},"end":{"line":69,"column":56}},"29":{"start":{"line":74,"column":22},"end":{"line":74,"column":45}},"30":{"start":{"line":75,"column":2},"end":{"line":77,"column":3}},"31":{"start":{"line":76,"column":3},"end":{"line":76,"column":23}},"32":{"start":{"line":79,"column":2},"end":{"line":81,"column":3}},"33":{"start":{"line":80,"column":3},"end":{"line":80,"column":19}},"34":{"start":{"line":83,"column":20},"end":{"line":83,"column":29}},"35":{"start":{"line":85,"column":2},"end":{"line":92,"column":21}},"36":{"start":{"line":87,"column":20},"end":{"line":87,"column":73}},"37":{"start":{"line":88,"column":4},"end":{"line":88,"column":31}},"38":{"start":{"line":89,"column":4},"end":{"line":89,"column":38}},"39":{"start":{"line":91,"column":18},"end":{"line":91,"column":52}},"40":{"start":{"line":92,"column":15},"end":{"line":92,"column":19}},"41":{"start":{"line":96,"column":22},"end":{"line":96,"column":45}},"42":{"start":{"line":97,"column":20},"end":{"line":97,"column":29}},"43":{"start":{"line":98,"column":2},"end":{"line":99,"column":42}},"44":{"start":{"line":99,"column":15},"end":{"line":99,"column":40}},"45":{"start":{"line":103,"column":20},"end":{"line":103,"column":29}},"46":{"start":{"line":104,"column":2},"end":{"line":105,"column":30}},"47":{"start":{"line":105,"column":15},"end":{"line":105,"column":28}},"48":{"start":{"line":109,"column":0},"end":{"line":109,"column":22}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":6,"column":18},"end":{"line":6,"column":19}},"loc":{"start":{"line":6,"column":29},"end":{"line":22,"column":1}},"line":6},"1":{"name":"(anonymous_1)","decl":{"start":{"line":25,"column":1},"end":{"line":25,"column":2}},"loc":{"start":{"line":25,"column":27},"end":{"line":47,"column":2}},"line":25},"2":{"name":"(anonymous_2)","decl":{"start":{"line":43,"column":31},"end":{"line":43,"column":32}},"loc":{"start":{"line":43,"column":40},"end":{"line":43,"column":65}},"line":43},"3":{"name":"(anonymous_3)","decl":{"start":{"line":49,"column":1},"end":{"line":49,"column":2}},"loc":{"start":{"line":49,"column":20},"end":{"line":51,"column":2}},"line":49},"4":{"name":"(anonymous_4)","decl":{"start":{"line":53,"column":1},"end":{"line":53,"column":2}},"loc":{"start":{"line":53,"column":19},"end":{"line":71,"column":2}},"line":53},"5":{"name":"(anonymous_5)","decl":{"start":{"line":57,"column":9},"end":{"line":57,"column":10}},"loc":{"start":{"line":57,"column":15},"end":{"line":57,"column":37}},"line":57},"6":{"name":"(anonymous_6)","decl":{"start":{"line":58,"column":9},"end":{"line":58,"column":10}},"loc":{"start":{"line":58,"column":17},"end":{"line":58,"column":80}},"line":58},"7":{"name":"(anonymous_7)","decl":{"start":{"line":59,"column":9},"end":{"line":59,"column":10}},"loc":{"start":{"line":59,"column":17},"end":{"line":70,"column":4}},"line":59},"8":{"name":"(anonymous_8)","decl":{"start":{"line":73,"column":1},"end":{"line":73,"column":2}},"loc":{"start":{"line":73,"column":22},"end":{"line":93,"column":2}},"line":73},"9":{"name":"(anonymous_9)","decl":{"start":{"line":86,"column":9},"end":{"line":86,"column":10}},"loc":{"start":{"line":86,"column":15},"end":{"line":90,"column":4}},"line":86},"10":{"name":"(anonymous_10)","decl":{"start":{"line":91,"column":9},"end":{"line":91,"column":10}},"loc":{"start":{"line":91,"column":18},"end":{"line":91,"column":52}},"line":91},"11":{"name":"(anonymous_11)","decl":{"start":{"line":92,"column":9},"end":{"line":92,"column":10}},"loc":{"start":{"line":92,"column":15},"end":{"line":92,"column":19}},"line":92},"12":{"name":"(anonymous_12)","decl":{"start":{"line":95,"column":1},"end":{"line":95,"column":2}},"loc":{"start":{"line":95,"column":13},"end":{"line":100,"column":2}},"line":95},"13":{"name":"(anonymous_13)","decl":{"start":{"line":99,"column":9},"end":{"line":99,"column":10}},"loc":{"start":{"line":99,"column":15},"end":{"line":99,"column":40}},"line":99},"14":{"name":"(anonymous_14)","decl":{"start":{"line":102,"column":1},"end":{"line":102,"column":2}},"loc":{"start":{"line":102,"column":9},"end":{"line":106,"column":2}},"line":102},"15":{"name":"(anonymous_15)","decl":{"start":{"line":105,"column":9},"end":{"line":105,"column":10}},"loc":{"start":{"line":105,"column":15},"end":{"line":105,"column":28}},"line":105}},"branchMap":{"0":{"loc":{"start":{"line":16,"column":1},"end":{"line":19,"column":2}},"type":"if","locations":[{"start":{"line":16,"column":1},"end":{"line":19,"column":2}},{"start":{"line":16,"column":1},"end":{"line":19,"column":2}}],"line":16},"1":{"loc":{"start":{"line":16,"column":5},"end":{"line":16,"column":35}},"type":"binary-expr","locations":[{"start":{"line":16,"column":5},"end":{"line":16,"column":20}},{"start":{"line":16,"column":24},"end":{"line":16,"column":35}}],"line":16},"2":{"loc":{"start":{"line":17,"column":18},"end":{"line":17,"column":66}},"type":"binary-expr","locations":[{"start":{"line":17,"column":18},"end":{"line":17,"column":33}},{"start":{"line":17,"column":37},"end":{"line":17,"column":66}}],"line":17},"3":{"loc":{"start":{"line":33,"column":3},"end":{"line":33,"column":44}},"type":"cond-expr","locations":[{"start":{"line":33,"column":31},"end":{"line":33,"column":38}},{"start":{"line":33,"column":41},"end":{"line":33,"column":44}}],"line":33},"4":{"loc":{"start":{"line":37,"column":2},"end":{"line":40,"column":3}},"type":"if","locations":[{"start":{"line":37,"column":2},"end":{"line":40,"column":3}},{"start":{"line":37,"column":2},"end":{"line":40,"column":3}}],"line":37},"5":{"loc":{"start":{"line":42,"column":2},"end":{"line":44,"column":3}},"type":"if","locations":[{"start":{"line":42,"column":2},"end":{"line":44,"column":3}},{"start":{"line":42,"column":2},"end":{"line":44,"column":3}}],"line":42},"6":{"loc":{"start":{"line":58,"column":17},"end":{"line":58,"column":80}},"type":"cond-expr","locations":[{"start":{"line":58,"column":46},"end":{"line":58,"column":73}},{"start":{"line":58,"column":76},"end":{"line":58,"column":80}}],"line":58},"7":{"loc":{"start":{"line":60,"column":4},"end":{"line":62,"column":5}},"type":"if","locations":[{"start":{"line":60,"column":4},"end":{"line":62,"column":5}},{"start":{"line":60,"column":4},"end":{"line":62,"column":5}}],"line":60},"8":{"loc":{"start":{"line":64,"column":4},"end":{"line":67,"column":5}},"type":"if","locations":[{"start":{"line":64,"column":4},"end":{"line":67,"column":5}},{"start":{"line":64,"column":4},"end":{"line":67,"column":5}}],"line":64},"9":{"loc":{"start":{"line":64,"column":8},"end":{"line":64,"column":69}},"type":"binary-expr","locations":[{"start":{"line":64,"column":8},"end":{"line":64,"column":40}},{"start":{"line":64,"column":44},"end":{"line":64,"column":69}}],"line":64},"10":{"loc":{"start":{"line":69,"column":11},"end":{"line":69,"column":55}},"type":"cond-expr","locations":[{"start":{"line":69,"column":38},"end":{"line":69,"column":42}},{"start":{"line":69,"column":45},"end":{"line":69,"column":55}}],"line":69},"11":{"loc":{"start":{"line":69,"column":12},"end":{"line":69,"column":34}},"type":"binary-expr","locations":[{"start":{"line":69,"column":12},"end":{"line":69,"column":19}},{"start":{"line":69,"column":23},"end":{"line":69,"column":34}}],"line":69},"12":{"loc":{"start":{"line":75,"column":2},"end":{"line":77,"column":3}},"type":"if","locations":[{"start":{"line":75,"column":2},"end":{"line":77,"column":3}},{"start":{"line":75,"column":2},"end":{"line":77,"column":3}}],"line":75},"13":{"loc":{"start":{"line":79,"column":2},"end":{"line":81,"column":3}},"type":"if","locations":[{"start":{"line":79,"column":2},"end":{"line":81,"column":3}},{"start":{"line":79,"column":2},"end":{"line":81,"column":3}}],"line":79},"14":{"loc":{"start":{"line":87,"column":20},"end":{"line":87,"column":73}},"type":"cond-expr","locations":[{"start":{"line":87,"column":49},"end":{"line":87,"column":65}},{"start":{"line":87,"column":69},"end":{"line":87,"column":73}}],"line":87}},"s":{"0":1,"1":1,"2":1,"3":3,"4":3,"5":2,"6":2,"7":1,"8":187,"9":187,"10":187,"11":3,"12":3,"13":187,"14":178,"15":1,"16":187,"17":92,"18":45,"19":45,"20":45,"21":45,"22":45,"23":45,"24":9,"25":36,"26":5,"27":5,"28":31,"29":37,"30":37,"31":32,"32":37,"33":1,"34":37,"35":37,"36":37,"37":37,"38":37,"39":37,"40":37,"41":10,"42":10,"43":10,"44":10,"45":155,"46":155,"47":155,"48":1},"f":{"0":3,"1":187,"2":1,"3":92,"4":45,"5":45,"6":45,"7":45,"8":37,"9":37,"10":37,"11":37,"12":10,"13":10,"14":155,"15":155},"b":{"0":[2,1],"1":[3,3],"2":[2,2],"3":[2,185],"4":[3,184],"5":[178,9],"6":[36,9],"7":[9,36],"8":[5,31],"9":[36,13],"10":[1,30],"11":[31,1],"12":[32,5],"13":[1,36],"14":[6,31]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"68d63c1da21bdd17ca5b4661ccb46d430a48aa3d","contentHash":"600469b33bfd3413852cfb4a0f6eb2248472b9c0ecd031c4b7ee54f59a8c8e2c"}}
@@ -1 +0,0 @@
1
- {"parent":null,"pid":74271,"argv":["/Users/jaredwray/.nvm/versions/node/v16.13.1/bin/node","/Users/jaredwray/src/github.com/jaredwray/keyv/packages/keyv/node_modules/.bin/ava","--serial"],"execArgv":[],"cwd":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/keyv","time":1641659048071,"ppid":74270,"coverageFilename":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/keyv/.nyc_output/37411020-4142-4b08-b825-343ba2427bf7.json","externalId":"","uuid":"37411020-4142-4b08-b825-343ba2427bf7","files":[]}
@@ -1 +0,0 @@
1
- {"parent":"37411020-4142-4b08-b825-343ba2427bf7","pid":74274,"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/keyv","time":1641659048289,"ppid":74271,"coverageFilename":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/keyv/.nyc_output/cd79f2de-c8b8-4493-bc15-b2bde28efa4b.json","externalId":"","uuid":"cd79f2de-c8b8-4493-bc15-b2bde28efa4b","files":["/Users/jaredwray/src/github.com/jaredwray/keyv/packages/keyv/src/index.js"]}
@@ -1 +0,0 @@
1
- {"processes":{"37411020-4142-4b08-b825-343ba2427bf7":{"parent":null,"children":["cd79f2de-c8b8-4493-bc15-b2bde28efa4b"]},"cd79f2de-c8b8-4493-bc15-b2bde28efa4b":{"parent":"37411020-4142-4b08-b825-343ba2427bf7","children":[]}},"files":{"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/keyv/src/index.js":["cd79f2de-c8b8-4493-bc15-b2bde28efa4b"]},"externalIds":{}}
package/test/test.js DELETED
@@ -1,132 +0,0 @@
1
- const test = require('ava');
2
- const keyvTestSuite = require('@keyv/test-suite').default;
3
- const { keyvOfficialTests } = require('@keyv/test-suite');
4
- const Keyv = require('this');
5
- const tk = require('timekeeper');
6
- const KeyvSqlite = require('@keyv/sqlite');
7
-
8
- keyvOfficialTests(test, Keyv, 'sqlite://test/testdb.sqlite', 'sqlite://non/existent/database.sqlite');
9
-
10
- const store = () => new KeyvSqlite({ uri: 'sqlite://test/testdb.sqlite', busyTimeout: 3000 });
11
- keyvTestSuite(test, Keyv, store);
12
-
13
- test.serial('Keyv is a class', t => {
14
- t.is(typeof Keyv, 'function');
15
- t.throws(() => Keyv()); // eslint-disable-line new-cap
16
- t.notThrows(() => new Keyv());
17
- });
18
-
19
- test.serial('Keyv accepts storage adapters', async t => {
20
- const store = new Map();
21
- const keyv = new Keyv({ store });
22
- t.is(store.size, 0);
23
- await keyv.set('foo', 'bar');
24
- t.is(await keyv.get('foo'), 'bar');
25
- t.is(store.size, 1);
26
- });
27
-
28
- test.serial('Keyv passes tll info to stores', async t => {
29
- t.plan(1);
30
- const store = new Map();
31
- const storeSet = store.set;
32
- store.set = (key, value, ttl) => {
33
- t.is(ttl, 100);
34
- storeSet.call(store, key, value, ttl);
35
- };
36
-
37
- const keyv = new Keyv({ store });
38
- await keyv.set('foo', 'bar', 100);
39
- });
40
-
41
- test.serial('Keyv respects default tll option', async t => {
42
- const store = new Map();
43
- const keyv = new Keyv({ store, ttl: 100 });
44
- await keyv.set('foo', 'bar');
45
- t.is(await keyv.get('foo'), 'bar');
46
- tk.freeze(Date.now() + 150);
47
- t.is(await keyv.get('foo'), undefined);
48
- t.is(store.size, 0);
49
- tk.reset();
50
- });
51
-
52
- test.serial('.set(key, val, ttl) overwrites default tll option', async t => {
53
- const startTime = Date.now();
54
- tk.freeze(startTime);
55
- const store = new Map();
56
- const keyv = new Keyv({ store, ttl: 200 });
57
- await keyv.set('foo', 'bar');
58
- await keyv.set('fizz', 'buzz', 100);
59
- await keyv.set('ping', 'pong', 300);
60
- t.is(await keyv.get('foo'), 'bar');
61
- t.is(await keyv.get('fizz'), 'buzz');
62
- t.is(await keyv.get('ping'), 'pong');
63
- tk.freeze(startTime + 150);
64
- t.is(await keyv.get('foo'), 'bar');
65
- t.is(await keyv.get('fizz'), undefined);
66
- t.is(await keyv.get('ping'), 'pong');
67
- tk.freeze(startTime + 250);
68
- t.is(await keyv.get('foo'), undefined);
69
- t.is(await keyv.get('ping'), 'pong');
70
- tk.freeze(startTime + 350);
71
- t.is(await keyv.get('ping'), undefined);
72
- tk.reset();
73
- });
74
-
75
- test.serial('.set(key, val, ttl) where ttl is "0" overwrites default tll option and sets key to never expire', async t => {
76
- const startTime = Date.now();
77
- tk.freeze(startTime);
78
- const store = new Map();
79
- const keyv = new Keyv({ store, ttl: 200 });
80
- await keyv.set('foo', 'bar', 0);
81
- t.is(await keyv.get('foo'), 'bar');
82
- tk.freeze(startTime + 250);
83
- t.is(await keyv.get('foo'), 'bar');
84
- tk.reset();
85
- });
86
-
87
- test.serial('.get(key, {raw: true}) returns the raw object instead of the value', async t => {
88
- const store = new Map();
89
- const keyv = new Keyv({ store });
90
- await keyv.set('foo', 'bar');
91
- const value = await keyv.get('foo');
92
- const rawObject = await keyv.get('foo', { raw: true });
93
- t.is(value, 'bar');
94
- t.is(rawObject.value, 'bar');
95
- });
96
-
97
- test.serial('Keyv uses custom serializer when provided instead of json-buffer', async t => {
98
- t.plan(3);
99
- const store = new Map();
100
- const serialize = data => {
101
- t.pass();
102
- return JSON.stringify(data);
103
- };
104
-
105
- const deserialize = data => {
106
- t.pass();
107
- return JSON.parse(data);
108
- };
109
-
110
- const keyv = new Keyv({ store, serialize, deserialize });
111
- await keyv.set('foo', 'bar');
112
- t.is(await keyv.get('foo'), 'bar');
113
- });
114
-
115
- test.serial('Keyv supports async serializer/deserializer', async t => {
116
- t.plan(3);
117
- const store = new Map();
118
-
119
- const serialize = async data => {
120
- t.pass();
121
- return JSON.stringify(data);
122
- };
123
-
124
- const deserialize = async data => {
125
- t.pass();
126
- return JSON.parse(data);
127
- };
128
-
129
- const keyv = new Keyv({ store, serialize, deserialize });
130
- await keyv.set('foo', 'bar');
131
- t.is(await keyv.get('foo'), 'bar');
132
- });
Binary file