haraka-plugin-redis
Advanced tools
Comparing version 2.0.3 to 2.0.4
### 2.0.3 - 2022-05-24 | ||
### 2.0.4 - 2022-05-25 | ||
- fix: rename p* methods -> * (required in redis v4) | ||
- fix: rename p\* methods -> * (required in redis v4) | ||
- fix: add `await ...connect()` as is now required, fixes #32 | ||
- fix: make redis_ping async function | ||
- dep(redis): bump 4.0 -> 4.1 | ||
- ci: updated syntax | ||
- test: added tests for init_redis_plugin | ||
@@ -10,0 +12,0 @@ |
40
index.js
@@ -38,11 +38,4 @@ 'use strict'; | ||
// backwards compat with node-redis < 4 | ||
if (rc.server && !rc.socket) { | ||
rc.socket = rc.server | ||
delete rc.server | ||
} | ||
// same as above | ||
if (rc.db && !rc.database) { | ||
rc.database = rc.db | ||
delete rc.db | ||
} | ||
@@ -63,7 +56,6 @@ | ||
exports.init_redis_shared = function (next, server) { | ||
const plugin = this; | ||
let calledNext = false; | ||
function nextOnce (e) { | ||
if (e) plugin.logerror(`Redis error: ${e.message}`); | ||
if (e) this.logerror(`Redis error: ${e.message}`); | ||
if (calledNext) return; | ||
@@ -77,3 +69,3 @@ calledNext = true; | ||
if (!server.notes.redis) { | ||
plugin.get_redis_client(plugin.redisCfg.server).then(client => { | ||
this.get_redis_client(this.redisCfg.server).then(client => { | ||
server.notes.redis = client | ||
@@ -88,3 +80,3 @@ nextOnce() | ||
plugin.loginfo('already connected'); | ||
this.loginfo('already connected'); | ||
nextOnce(); // connection is good | ||
@@ -142,15 +134,8 @@ }); | ||
this.redis_pings=false; | ||
if (!this.db) throw new Error('redis not initialized'); | ||
if (!this.db) { | ||
return new Error('redis not initialized'); | ||
} | ||
try { | ||
const r = await this.db.ping() | ||
if (r !== 'PONG') return new Error('not PONG'); | ||
this.redis_pings=true | ||
} | ||
catch (e) { | ||
this.logerror(e.message) | ||
} | ||
const r = await this.db.ping() | ||
if (r !== 'PONG') throw new Error('not PONG'); | ||
this.redis_pings=true | ||
return true | ||
} | ||
@@ -160,3 +145,3 @@ | ||
let msg = `redis://${opts?.socket?.host}:${opts?.socket?.port}`; | ||
if (opts.database) msg += `/${opts.database}`; | ||
if (opts?.database) msg += `/${opts?.database}`; | ||
if (client?.server_info?.redis_version) { | ||
@@ -185,3 +170,3 @@ msg += `\tv${client?.server_info?.redis_version}`; | ||
if (opts.database) client.dbid = opts.database | ||
if (opts?.database) client.dbid = opts?.database | ||
@@ -196,2 +181,3 @@ client.server_info = await client.info() | ||
console.error(e) | ||
this.logerror(e); | ||
} | ||
@@ -212,3 +198,3 @@ } | ||
this.redis = await redis.createClient(this.redisCfg.pubsub) | ||
this.redis = redis.createClient(this.redisCfg.pubsub) | ||
await this.redis.connect() | ||
@@ -231,3 +217,3 @@ | ||
connection.notes.redis = await redis.createClient(this.redisCfg.pubsub) | ||
connection.notes.redis = redis.createClient(this.redisCfg.pubsub) | ||
await connection.notes.redis.connect() | ||
@@ -234,0 +220,0 @@ |
{ | ||
"name": "haraka-plugin-redis", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Redis plugin for Haraka & other plugins to inherit from", | ||
@@ -19,4 +19,4 @@ "main": "index.js", | ||
"scripts": { | ||
"lint": "npx eslint *.js test/*.js", | ||
"lintfix": "npx eslint --fix *.js test/*.js", | ||
"lint": "npx eslint *.js test", | ||
"lintfix": "npx eslint --fix *.js test", | ||
"cover": "NODE_ENV=cov npx nyc --reporter=lcovonly npm run test", | ||
@@ -23,0 +23,0 @@ "versions": "npx dependency-version-checker check", |
@@ -44,3 +44,3 @@ # haraka-plugin-redis | ||
if (server.notes.redis) { | ||
server.notes.redis.hgetall(...); | ||
server.notes.redis.hGetAll(...); | ||
// or any other redis command | ||
@@ -47,0 +47,0 @@ } |
@@ -93,3 +93,3 @@ 'use strict'; | ||
assert.ok(redis); | ||
redis.disconnect() | ||
await redis.quit() | ||
}) | ||
@@ -111,4 +111,33 @@ | ||
assert.ok(client) | ||
await client.disconnect() | ||
await client.quit() | ||
}) | ||
}) | ||
describe('init_redis_plugin', function () { | ||
before(async function () { | ||
this.server = { notes: { } } | ||
this.plugin = new fixtures.plugin('index') | ||
this.plugin.register() | ||
}) | ||
after(function () { | ||
this.plugin.db.quit() | ||
}) | ||
it('connects to redis', function (done) { | ||
this.plugin.init_redis_plugin(() => { | ||
assert.ok(this.plugin.db?.server_info) | ||
done() | ||
}, this.server) | ||
}) | ||
it('pings and gets PONG answer', function (done) { | ||
this.plugin.redis_ping() | ||
.then(r => { | ||
assert.equal(r, true) | ||
done() | ||
}) | ||
.catch(done) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17595
297