From 662111a775725b93ccbeaae2ee4211f18ad29cf6 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Sun, 28 Apr 2024 16:20:28 +0200 Subject: [PATCH] refactor: avoid p-event dependency --- packages/redis/package.json | 3 +-- packages/redis/src/index.js | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/redis/package.json b/packages/redis/package.json index 755a9b4..5cf1e7f 100644 --- a/packages/redis/package.json +++ b/packages/redis/package.json @@ -30,8 +30,7 @@ "value" ], "dependencies": { - "ioredis": "~5.4.1", - "p-event": "~4.2.0" + "ioredis": "~5.4.1" }, "devDependencies": { "@keyvhq/core": "latest", diff --git a/packages/redis/src/index.js b/packages/redis/src/index.js index c2d2d3c..9b5f074 100644 --- a/packages/redis/src/index.js +++ b/packages/redis/src/index.js @@ -1,8 +1,14 @@ 'use strict' -const pEvent = require('p-event') const Redis = require('ioredis') +const { promisify } = require('util') +const stream = require('stream') + +const { Transform } = stream + +const pipeline = promisify(stream.pipeline) + const normalizeArguments = (input, options) => { if (input instanceof Redis) return input const { uri, ...opts } = Object.assign( @@ -37,13 +43,16 @@ class KeyvRedis { async clear (namespace) { const match = namespace ? `${namespace}:*` : '*' const stream = this.redis.scanStream({ match }) - const keys = [] - stream.on('data', matchedKeys => keys.push(...matchedKeys)) - await pEvent(stream, 'end') - if (keys.length > 0) { - await this.redis.unlink(keys) - } + const collectKeys = new Transform({ + objectMode: true, + transform (chunk, _, next) { + keys.push.apply(keys, chunk) + next() + } + }) + await pipeline(stream, collectKeys) + if (keys.length > 0) await this.redis.unlink(keys) } async * iterator (namespace) {