From 31d302355730ab484f45d8b08c8f25fb5f8a6266 Mon Sep 17 00:00:00 2001 From: alphmth <39932421+alphmth@users.noreply.github.com> Date: Mon, 15 Aug 2022 22:07:14 +0500 Subject: [PATCH] fixed undefined array issue (#432) * fixed undefined array issue * fixed etcd3 * revert some code * fixed linting * fixed linting * linting fix * linting fix Co-authored-by: alphmth <> --- packages/etcd/src/index.js | 2 +- packages/keyv/src/index.js | 8 ++------ packages/keyv/test/test.js | 4 ++-- packages/memcache/src/index.d.ts | 5 ++--- packages/memcache/src/index.js | 2 +- packages/memcache/test/test.js | 2 +- packages/mongo/src/index.js | 4 ++-- packages/mongo/test/test.js | 2 +- packages/mysql/src/index.js | 4 ---- packages/postgres/src/index.js | 4 ---- packages/redis/src/index.js | 2 +- packages/sqlite/src/index.js | 4 ---- packages/test-suite/src/api.js | 4 ++-- packages/tiered/src/index.js | 2 +- packages/tiered/test/test.js | 2 +- 15 files changed, 17 insertions(+), 34 deletions(-) diff --git a/packages/etcd/src/index.js b/packages/etcd/src/index.js index a802bc451..3b89fdd16 100644 --- a/packages/etcd/src/index.js +++ b/packages/etcd/src/index.js @@ -70,7 +70,7 @@ class KeyvEtcd extends EventEmitter { } } - return data.every(x => x === undefined) ? [] : data; + return data; }); } diff --git a/packages/keyv/src/index.js b/packages/keyv/src/index.js index bbce61ebd..16fe334cc 100644 --- a/packages/keyv/src/index.js +++ b/packages/keyv/src/index.js @@ -144,7 +144,7 @@ class Keyv extends EventEmitter { data.push(value.value); } - return data.every(x => x === undefined) ? [] : data; + return data; }); } @@ -159,10 +159,6 @@ class Keyv extends EventEmitter { if (isArray) { const result = []; - if (data.length === 0) { - return []; - } - for (let row of data) { if ((typeof row === 'string')) { row = this.opts.deserialize(row); @@ -181,7 +177,7 @@ class Keyv extends EventEmitter { } } - return result.every(x => x === undefined) ? [] : result; + return result; } if (typeof data.expires === 'number' && Date.now() > data.expires) { diff --git a/packages/keyv/test/test.js b/packages/keyv/test/test.js index 181f11bb6..04c1f76d0 100644 --- a/packages/keyv/test/test.js +++ b/packages/keyv/test/test.js @@ -277,7 +277,7 @@ test.serial('keyv.get([keys]) should return empty array when expires sqlite', as }); const values = await keyv.get(['foo', 'foo1', 'foo2']); t.is(Array.isArray(values), true); - t.is(values.length, 0); + t.is(values.length, 3); }); test.serial('keyv.get([keys]) should return array raw values sqlite', async t => { @@ -324,7 +324,7 @@ test.serial('keyv.get([keys]) should return empty array for all no existent keys const keyv = new Keyv({store: new Map()}); const values = await keyv.get(['foo', 'foo1', 'foo2']); t.is(Array.isArray(values), true); - t.deepEqual(values, []); + t.deepEqual(values, [undefined, undefined, undefined]); }); test('pass compress options', async t => { diff --git a/packages/memcache/src/index.d.ts b/packages/memcache/src/index.d.ts index 0b0665db5..366dedf58 100644 --- a/packages/memcache/src/index.d.ts +++ b/packages/memcache/src/index.d.ts @@ -1,4 +1,4 @@ -/* eslint-disable @typescript-eslint/unified-signatures */ + import {EventEmitter} from 'node:events'; import {Store, StoredData} from 'keyv'; @@ -6,8 +6,7 @@ declare class KeyvMemcache extends EventEmitter implements Store { ttlSupport: boolean; namespace?: string | undefined; - constructor(uri?: string); - constructor(options?: KeyvMemcache.Options); + constructor(uri?: string | KeyvMemcache.Options); get(key: string): Promise; getMany?( keys: string[] diff --git a/packages/memcache/src/index.js b/packages/memcache/src/index.js index b7c9bf58e..484cac8b0 100644 --- a/packages/memcache/src/index.js +++ b/packages/memcache/src/index.js @@ -69,7 +69,7 @@ class KeyvMemcache extends EventEmitter { data.push(value.value); } - return data.every(x => x.value === undefined) ? [] : data; + return data; }); } diff --git a/packages/memcache/test/test.js b/packages/memcache/test/test.js index c0e45d626..7a9f470a5 100644 --- a/packages/memcache/test/test.js +++ b/packages/memcache/test/test.js @@ -127,7 +127,7 @@ test.serial('keyvMemcache getMany', async t => { const value = await keyvMemcache.getMany(['foo0', 'Foo1']); t.is(Array.isArray(value), true); - t.is(value[0], undefined); + t.deepEqual(value[0], {expires: 0, value: undefined}); }); test.serial('keyv has / false', async t => { diff --git a/packages/mongo/src/index.js b/packages/mongo/src/index.js index 01554a971..3316f8a1a 100644 --- a/packages/mongo/src/index.js +++ b/packages/mongo/src/index.js @@ -167,7 +167,7 @@ class KeyvMongo extends EventEmitter { data.push(value.value); } - return data.every(x => x === undefined) ? [] : data; + return data; }); } @@ -190,7 +190,7 @@ class KeyvMongo extends EventEmitter { i++; } - return results.every(x => x === undefined) ? [] : results; + return results; }), ); } diff --git a/packages/mongo/test/test.js b/packages/mongo/test/test.js index 06d166a2f..4c4ecbaf8 100644 --- a/packages/mongo/test/test.js +++ b/packages/mongo/test/test.js @@ -198,7 +198,7 @@ test.serial('.getMany([keys]) using GridFS should return empty array for all no await keyv.clearUnusedFor(0); const values = await keyv.getMany(['foo', 'foo1', 'foo2']); t.is(Array.isArray(values), true); - t.deepEqual(values, []); + t.deepEqual(values, [undefined, undefined, undefined]); }); test.serial('Clears entire cache store', async t => { diff --git a/packages/mysql/src/index.js b/packages/mysql/src/index.js index ad771539c..bf9a6f69d 100644 --- a/packages/mysql/src/index.js +++ b/packages/mysql/src/index.js @@ -51,10 +51,6 @@ class KeyvMysql extends EventEmitter { const sql = `SELECT * FROM ${this.opts.table} WHERE id IN (?)`; const select = mysql.format(sql, [keys]); return this.query(select).then(rows => { - if (rows.length === 0) { - return []; - } - const results = [...keys]; let i = 0; for (const key of keys) { diff --git a/packages/postgres/src/index.js b/packages/postgres/src/index.js index 87c17aff8..89baaacfe 100644 --- a/packages/postgres/src/index.js +++ b/packages/postgres/src/index.js @@ -45,10 +45,6 @@ class KeyvPostgres extends EventEmitter { getMany(keys) { const getMany = `SELECT * FROM ${this.opts.table} WHERE key = ANY($1)`; return this.query(getMany, [keys]).then(rows => { - if (rows.length === 0) { - return []; - } - const results = [...keys]; let i = 0; for (const key of keys) { diff --git a/packages/redis/src/index.js b/packages/redis/src/index.js index 68ecf8fdc..942953e1a 100644 --- a/packages/redis/src/index.js +++ b/packages/redis/src/index.js @@ -35,7 +35,7 @@ class KeyvRedis extends EventEmitter { getMany(keys) { return this.redis.mget(keys) - .then(rows => rows.every(row => row === null) ? [] : rows); + .then(rows => rows); } set(key, value, ttl) { diff --git a/packages/sqlite/src/index.js b/packages/sqlite/src/index.js index 704d419f1..25e127601 100644 --- a/packages/sqlite/src/index.js +++ b/packages/sqlite/src/index.js @@ -62,10 +62,6 @@ class KeyvSqlite extends EventEmitter { getMany(keys) { const select = `SELECT * FROM ${this.opts.table} WHERE key IN (SELECT value FROM json_each(?))`; return this.query(select, JSON.stringify(keys)).then(rows => { - if (rows.length === 0) { - return []; - } - const results = [...keys]; let i = 0; for (const key of keys) { diff --git a/packages/test-suite/src/api.js b/packages/test-suite/src/api.js index 69dec412d..c404ed321 100644 --- a/packages/test-suite/src/api.js +++ b/packages/test-suite/src/api.js @@ -92,11 +92,11 @@ const keyvApiTests = (test, Keyv, store) => { t.is(values[2], 'bar2'); }); - test.serial('.get([keys]) should return empty array for all no existent keys', async t => { + test.serial('.get([keys]) should return undefined array for all no existent keys', async t => { const keyv = new Keyv({store: store()}); const values = await keyv.get(['foo', 'foo1', 'foo2']); t.is(Array.isArray(values), true); - t.deepEqual(values, []); + t.deepEqual(values, [undefined, undefined, undefined]); }); test.serial('.delete(key) returns a Promise', t => { diff --git a/packages/tiered/src/index.js b/packages/tiered/src/index.js index 9b9f9d546..a47525393 100644 --- a/packages/tiered/src/index.js +++ b/packages/tiered/src/index.js @@ -45,7 +45,7 @@ class KeyvTiered extends EventEmitter { data.push(value); } - return data.every(x => x === undefined) ? [] : data; + return data; }); } diff --git a/packages/tiered/test/test.js b/packages/tiered/test/test.js index f6f1d975a..bc2105234 100644 --- a/packages/tiered/test/test.js +++ b/packages/tiered/test/test.js @@ -114,7 +114,7 @@ test.serial('.getMany() deletes both stores', async t => { t.deepEqual(value, ['buzz', 'buzz1']); value = await store.getMany(['fizz3', 'fizz4']); - t.deepEqual(value, []); + t.deepEqual(value, [undefined, undefined]); }); test.serial(