nodecaf-redis
Advanced tools
Comparing version 0.0.4 to 0.1.0-rc1
@@ -0,0 +0,0 @@ # Nodecaf Redis Changelog |
const redis = require('async-redis'); | ||
const { createClient } = require('redis'); | ||
function crash(){ | ||
this.emit('error', new Error('Redis connection aborted')); | ||
} | ||
function initClient(conf){ | ||
const client = createClient({ | ||
socket: conf, | ||
...conf, | ||
disableOfflineQueue: true | ||
}); | ||
function initClient(client){ | ||
return new Promise( (done, fail) => { | ||
const end = err => { | ||
fail(err || new Error('Redis connection aborted')); | ||
}; | ||
client.once('error', end); | ||
client.once('end', end); | ||
client.once('ready', () => { | ||
client.off('error', end); | ||
client.off('end', end); | ||
client.on('end', crash); | ||
done(); | ||
}); | ||
client.on('error', err => { | ||
client.disconnect(); | ||
throw err; | ||
}); | ||
} | ||
module.exports = async function(conf, subChannel, onMessage){ | ||
conf = conf || {}; | ||
// return new Proxy(client, { | ||
// get(target, cmd){ | ||
// return target[cmd.toUpperCase()]; | ||
// } | ||
// }); | ||
const promises = []; | ||
return client; | ||
} | ||
conf['retry_strategy'] = () => new Error('Redis connection failed'); | ||
module.exports = async function(conf, sub){ | ||
const client = redis.createClient(conf); | ||
promises.push(initClient(client)); | ||
conf = conf || {}; | ||
let subClient; | ||
if(subChannel && onMessage){ | ||
subChannel = Array.isArray(subChannel) ? subChannel : [ subChannel ]; | ||
const client = initClient(conf); | ||
subClient = redis.createClient(conf); | ||
promises.push(initClient(subClient)); | ||
client.close = function(){ | ||
return Promise.all([ | ||
client.quit(), | ||
client.sub?.quit() | ||
]); | ||
}; | ||
client.subClient = subClient; | ||
subClient.on('message', onMessage); | ||
if(sub){ | ||
client.sub = initClient(conf); | ||
await client.sub.connect(); | ||
} | ||
client.close = async () => { | ||
client.off('end', crash); | ||
subClient && subClient.off('end', crash); | ||
subClient && await new Promise(done => subClient.quit(done)); | ||
await new Promise(done => client.quit(done)); | ||
}; | ||
await client.connect(); | ||
subChannel && onMessage && subChannel.forEach(chan => | ||
promises.push(subClient.subscribe(chan))); | ||
await Promise.all(promises); | ||
return client; | ||
} |
{ | ||
"name": "nodecaf-redis", | ||
"version": "0.0.4", | ||
"version": "0.1.0-rc1", | ||
"description": "A conveninent interface around node redis to work within a nodecaf app.", | ||
@@ -28,5 +28,4 @@ "main": "lib/main.js", | ||
"dependencies": { | ||
"async-redis": "^1.1.7" | ||
}, | ||
"devDependencies": {} | ||
"redis": "^4.0.4" | ||
} | ||
} |
@@ -0,0 +0,0 @@ # [Nodecaf Redis Wrapper](https://gitlab.com/nodecaf/redis) |
@@ -0,1 +1,3 @@ | ||
/* eslint-env mocha */ | ||
const assert = require('assert'); | ||
@@ -10,3 +12,3 @@ | ||
it('Should create a redis client', async function(){ | ||
let myr = await redis({ host: REDIS_HOST, port: '6379' }); | ||
const myr = await redis({ host: REDIS_HOST, port: '6379' }); | ||
await myr.set('abc', '23'); | ||
@@ -19,6 +21,7 @@ assert.strictEqual(await myr.get('abc'), '23'); | ||
(async function(){ | ||
const myr = await redis({ host: REDIS_HOST, port: '6379' }, 'foo-chan', async function(){ | ||
const myr = await redis({ host: REDIS_HOST, port: '6379' }, true); | ||
await myr.sub.subscribe('foo-chan', async function(){ | ||
await myr.close(); | ||
done(); | ||
}); | ||
}) | ||
myr.publish('foo-chan', 'here'); | ||
@@ -28,24 +31,22 @@ })(); | ||
it('Should support multiple channels to subscribe client', function(done){ | ||
let cool = 0; | ||
(async function(){ | ||
const myr = await redis({ host: REDIS_HOST, port: '6379' }, [ 'foo', 'bar' ], async function(){ | ||
cool++; | ||
if(cool == 2){ | ||
await myr.close(); | ||
done(); | ||
} | ||
}); | ||
myr.publish('foo', 'here'); | ||
myr.publish('bar', 'here'); | ||
})(); | ||
}); | ||
// it('Should crash on connection aborted', async function(){ | ||
// const proxy = require('node-tcp-proxy'); | ||
// const newProxy = proxy.createProxy(6000, REDIS_HOST, 6379); | ||
it('Should crash on connection aborted', async function(){ | ||
const myr = await redis({ host: REDIS_HOST, port: '6379' }); | ||
const p = new Promise(done => myr.on('error', done)); | ||
myr.quit(); | ||
await p; | ||
}); | ||
// const myr = await redis({ host: 'localhost', port: '6000' }); | ||
// const p = new Promise(done => myr.on('error', done)); | ||
// // await myr.quit(); | ||
// await myr.set('a', 1); | ||
// // console.log(await myr.get('a')); | ||
// // newProxy.end(); | ||
// newProxy.end(); | ||
// await p; | ||
// }); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
9449
10
68
+ Addedredis@^4.0.4
+ Added@redis/bloom@1.2.0(transitive)
+ Added@redis/client@1.6.0(transitive)
+ Added@redis/graph@1.1.1(transitive)
+ Added@redis/json@1.0.7(transitive)
+ Added@redis/search@1.2.0(transitive)
+ Added@redis/time-series@1.1.0(transitive)
+ Addedcluster-key-slot@1.1.2(transitive)
+ Addedgeneric-pool@3.9.0(transitive)
+ Addedredis@4.7.0(transitive)
+ Addedyallist@4.0.0(transitive)
- Removedasync-redis@^1.1.7
- Removedasync-redis@1.1.7(transitive)
- Removeddouble-ended-queue@2.1.0-0(transitive)
- Removedredis@2.8.0(transitive)
- Removedredis-commands@1.7.0(transitive)
- Removedredis-parser@2.6.0(transitive)