Socket
Socket
Sign inDemoInstall

redis-connection-pool

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redis-connection-pool - npm Package Compare versions

Comparing version 2.0.4 to 3.0.0

51

dist/index.d.ts

@@ -26,3 +26,2 @@ /// <reference types="node" />

declare type IdentifierType = string;
export default function redisConnectionPoolFactory(uid: IdentifierType, cfg: RedisConnectionPoolConfig): RedisConnectionPool;
/**

@@ -50,7 +49,2 @@ * Function: redisConnectionPoolFactory

*
* cfg.perform_checks - (boolean) - Perform a series of redis checks,
* currently this checks to see if
* blocking push/pops can be used.
* (default: false)
*
* cfg.redis - (object) - A redis config object

@@ -62,9 +56,12 @@ *

*/
export default function redisConnectionPoolFactory(uid: IdentifierType, cfg?: RedisConnectionPoolConfig): Promise<RedisConnectionPool>;
/**
* RedisConnectionPool
*/
export declare class RedisConnectionPool {
uid: IdentifierType;
max_clients: number;
perform_checks: boolean;
redis: RedisClientOptions;
pool: Pool<RedisClientType<RedisModules, RedisScripts>>;
constructor(uid: IdentifierType, cfg?: RedisConnectionPoolConfig);
private initializing;
constructor(cfg?: RedisConnectionPoolConfig);
init(): Promise<void>;

@@ -82,3 +79,3 @@ /**

*/
expire(key: string, ttl: number): Promise<string | number | boolean | Buffer | {
expire(key: string, ttl: number): Promise<string | number | boolean | Buffer | (string | Buffer)[] | {
key: string | Buffer;

@@ -99,3 +96,3 @@ element: string | Buffer;

*/
del(key: string): Promise<string | number | boolean | Buffer | {
del(key: string): Promise<string | number | boolean | Buffer | (string | Buffer)[] | {
key: string | Buffer;

@@ -107,2 +104,18 @@ element: string | Buffer;

/**
* Function: keys
*
* Execute a redis KEYS command
*
* Parameters:
*
* key - (string) - The prefix of the keys to return
*
*/
keys(key: string): Promise<string | number | boolean | Buffer | (string | Buffer)[] | {
key: string | Buffer;
element: string | Buffer;
} | {
[x: string]: string | Buffer;
}>;
/**
* Function: hdel

@@ -118,3 +131,3 @@ *

*/
hdel(key: string, fields: Array<string>): Promise<string | number | boolean | Buffer | {
hdel(key: string, fields: Array<string>): Promise<string | number | boolean | Buffer | (string | Buffer)[] | {
key: string | Buffer;

@@ -126,3 +139,3 @@ element: string | Buffer;

/**
* Function: send_command
* Function: sendCommand
*

@@ -140,3 +153,3 @@ * Sends an explicit command to the redis server. Helpful for new commands in redis

*/
send_command(command_name: any, args: any): Promise<string | number | boolean | Buffer | {
sendCommand(command_name: any, args: any): Promise<string | number | boolean | Buffer | (string | Buffer)[] | {
key: string | Buffer;

@@ -288,12 +301,8 @@ element: string | Buffer;

/**
* Function: clean
* Function: shutdown
*
* Clean the redis key namespace
* Drain the pool and close all connections to Redis.
*
* Parameters:
*
* key - (string) - The key of the value you wish to clear (can use wildcard *)
*
*/
clean(key: string): Promise<void>;
shutdown(): Promise<void>;
private singleCommand;

@@ -300,0 +309,0 @@ private getFuncs;

@@ -16,39 +16,74 @@ "use strict";

const chai_1 = require("chai");
// import * as sinon from 'sinon';
const index_1 = __importDefault(require("./index"));
const channel = 'redis-connection-pool-tests:';
const uid = 'redisPoolTest1';
describe('Redis Pool', () => {
let pool;
beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
console.log('init');
pool = (0, index_1.default)('redisPoolTest1', {
max_clients: 5,
pool = yield (0, index_1.default)(uid, {
redis: {
url: '127.0.0.1:6379',
database: 1
},
url: 'redis://localhost:6379'
}
});
yield pool.init();
}));
afterEach(() => {
console.log('clean');
pool.clean('*');
pool = undefined;
});
afterEach(() => __awaiter(void 0, void 0, void 0, function* () {
const keys = yield pool.keys(`${channel}*`);
for (const key of keys) {
yield pool.del(`${key}`);
}
}));
it('can connect to database', () => {
(0, chai_1.expect)(typeof pool).to.eql('object');
(0, chai_1.expect)(pool.uid).to.eql('redisPoolTest1');
(0, chai_1.expect)(pool.redis.url).to.eql('127.0.0.1:6379');
(0, chai_1.expect)(pool.redis.url).to.eql('redis://localhost:6379');
});
describe('store and fetch', () => {
console.log('-0');
it('set', () => __awaiter(void 0, void 0, void 0, function* () {
console.log('-1');
(0, chai_1.expect)(yield pool.set('an id', 'a value')).to.eql('OK');
}));
it('get', () => __awaiter(void 0, void 0, void 0, function* () {
console.log('-2');
(0, chai_1.expect)(yield pool.get('an id')).to.equal('a value');
}));
});
it('basic store and fetch', () => __awaiter(void 0, void 0, void 0, function* () {
(0, chai_1.expect)(yield pool.set(channel, 'a value')).to.eql('OK');
(0, chai_1.expect)(yield pool.get(channel)).to.equal('a value');
}));
it('hset and hget', () => __awaiter(void 0, void 0, void 0, function* () {
(0, chai_1.expect)(yield pool.hset(channel, 'a name', 'a value')).to.eql(1);
(0, chai_1.expect)(yield pool.hget(channel, 'a name')).to.equal('a value');
}));
it('hgetall', () => __awaiter(void 0, void 0, void 0, function* () {
(0, chai_1.expect)(yield pool.hset(channel, 'a name', 'a value')).to.eql(1);
(0, chai_1.expect)(yield pool.hset(channel, 'b name', 'b value')).to.eql(1);
(0, chai_1.expect)(yield pool.hset(channel, 'c name', 'c value')).to.eql(1);
(0, chai_1.expect)(yield pool.hset(channel, 'd name', 'd value')).to.eql(1);
(0, chai_1.expect)(yield pool.hset(channel, 'e name', 'e value')).to.eql(1);
(0, chai_1.expect)(yield pool.hgetall(channel)).to.eql({
'a name': 'a value',
'b name': 'b value',
'c name': 'c value',
'd name': 'd value',
'e name': 'e value'
});
}));
it('push and pop ', () => __awaiter(void 0, void 0, void 0, function* () {
(0, chai_1.expect)(yield pool.rpush(channel, 'foo1')).to.eql(1);
(0, chai_1.expect)(yield pool.rpush(channel, 'foo2')).to.eql(2);
(0, chai_1.expect)(yield pool.rpush(channel, 'foo3')).to.eql(3);
(0, chai_1.expect)(yield pool.lpush(channel, 'foo4')).to.eql(4);
(0, chai_1.expect)(yield pool.lpush(channel, 'foo5')).to.eql(5);
(0, chai_1.expect)(yield pool.brpop(channel)).to.eql({
key: channel,
element: 'foo3'
});
(0, chai_1.expect)(yield pool.blpop(channel)).to.eql({
key: channel,
element: 'foo5'
});
}));
it('incr', () => __awaiter(void 0, void 0, void 0, function* () {
(0, chai_1.expect)(yield pool.set(channel, 1)).to.eql('OK');
(0, chai_1.expect)(yield pool.incr(channel)).to.eql(2);
(0, chai_1.expect)(yield pool.incr(channel)).to.eql(3);
(0, chai_1.expect)(yield pool.get(channel)).to.eql('3');
}));
});
describe("Shutdown", () => {
it('', () => __awaiter(void 0, void 0, void 0, function* () {
const pool = yield (0, index_1.default)(uid);
yield pool.shutdown();
}));
});
//# sourceMappingURL=/index.integration.js.map

@@ -37,9 +37,2 @@ "use strict";

const connectionPools = new Map();
function redisConnectionPoolFactory(uid, cfg) {
if (!connectionPools.has(uid)) {
connectionPools.set(uid, new RedisConnectionPool(uid, cfg));
}
return connectionPools.get(uid);
}
exports.default = redisConnectionPoolFactory;
/**

@@ -67,7 +60,2 @@ * Function: redisConnectionPoolFactory

*
* cfg.perform_checks - (boolean) - Perform a series of redis checks,
* currently this checks to see if
* blocking push/pops can be used.
* (default: false)
*
* cfg.redis - (object) - A redis config object

@@ -79,9 +67,25 @@ *

*/
function redisConnectionPoolFactory(uid, cfg = {}) {
return __awaiter(this, void 0, void 0, function* () {
let pool;
if (!connectionPools.has(uid)) {
pool = new RedisConnectionPool(cfg);
connectionPools.set(uid, pool);
yield pool.init();
}
else {
pool = connectionPools.get(uid);
}
return pool;
});
}
exports.default = redisConnectionPoolFactory;
/**
* RedisConnectionPool
*/
class RedisConnectionPool {
constructor(uid, cfg = {}) {
this.max_clients = 10;
this.perform_checks = false;
this.uid = uid;
constructor(cfg = {}) {
this.max_clients = 5;
this.initializing = false;
this.max_clients = cfg.max_clients || this.max_clients;
this.perform_checks = this.perform_checks || this.perform_checks;
this.redis = cfg.redis;

@@ -94,5 +98,14 @@ }

log('create');
if (this.initializing) {
log('Create method already called. (Redis config error? ' +
'or maybe you forgot to await the init function?)');
throw Error('Create method already called. (Redis config error? ' +
'or maybe you forgot to await the init function?)');
}
else {
this.initializing = true;
}
const client = (0, redis_1.createClient)(this.redis);
client.on('error', function (err) {
log(err);
client.on('error', (err) => {
throw new Error(err);
});

@@ -104,2 +117,3 @@ client.on('ready', () => {

yield client.connect();
this.initializing = false;
return client;

@@ -147,2 +161,17 @@ }),

/**
* Function: keys
*
* Execute a redis KEYS command
*
* Parameters:
*
* key - (string) - The prefix of the keys to return
*
*/
keys(key) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.singleCommand('KEYS', [key]);
});
}
/**
* Function: hdel

@@ -164,3 +193,3 @@ *

/**
* Function: send_command
* Function: sendCommand
*

@@ -178,3 +207,3 @@ * Sends an explicit command to the redis server. Helpful for new commands in redis

*/
send_command(command_name, args) {
sendCommand(command_name, args) {
return __awaiter(this, void 0, void 0, function* () {

@@ -385,27 +414,11 @@ return yield this.singleCommand(command_name, args);

/**
* Function: clean
* Function: shutdown
*
* Clean the redis key namespace
* Drain the pool and close all connections to Redis.
*
* Parameters:
*
* key - (string) - The key of the value you wish to clear (can use wildcard *)
*
*/
clean(key) {
shutdown() {
return __awaiter(this, void 0, void 0, function* () {
log('clearing redis key ' + key);
const client = (0, redis_1.createClient)(this.redis);
yield client.connect();
const keys = yield client.keys(key);
yield client.quit();
if (Array.isArray(keys)) {
yield keys.forEach((name) => {
log('deleting name ' + name);
this.del(name);
});
}
else {
log(`ERROR couldn't get keys list on key '${key}': `, keys);
}
yield this.pool.drain();
yield this.pool.clear();
});

@@ -412,0 +425,0 @@ }

{
"name": "redis-connection-pool",
"version": "2.0.4",
"version": "3.0.0",
"description": "a redis client connection pool",

@@ -30,3 +30,4 @@ "license": "MIT",

"test": "c8 -x src/bootstrap -x \"src/**/*.test.*\" mocha -r ts-node/register src/*.test.ts",
"integration": "yarn run build && jaribu",
"coverage": "c8 check-coverage --statements 70 --branches 90 --functions 60 --lines 70",
"integration": "mocha -r ts-node/register src/index.integration.ts && yarn run build && mocha dist/index.integration.js",
"doc": "jsdoc2md --files ./src/index.ts --configure ./jsdoc2md.json > ./API.md"

@@ -58,3 +59,2 @@ },

"eslint-plugin-security-node": "1.1.1",
"jaribu": "2.2.3",
"jsdoc-babel": "0.5.0",

@@ -61,0 +61,0 @@ "jsdoc-to-markdown": "7.1.1",

@@ -1,3 +0,3 @@

node-redis-connection-pool
==========================
redis-connection-pool
=====================

@@ -16,4 +16,2 @@ A node.js connection pool for Redis.

**NOTE** Version 2.x is a rewrite and not backward compatible, please re-read the documentation to update your code.
## Installation

@@ -28,15 +26,23 @@

```javascript
var redisPool = require('redis-connection-pool')('myRedisPool', {
max_clients: 10, // defalut
import redisPoolFactory from 'redis-connection-pool';
const redisPool = await redisPoolFactory('myRedisPool', {
max_clients: 5, // default
redis: {
host: '127.0.0.1',
port: 6379
url: 'redis://localhost:6379'
}
});
redisPool.init();
await redisPool.set('test-key', 'foobar');
const foo = await redisPool.get('test-key');
// returns 'foobar'
```
Or you can create a pool instance directly
```javascript
import RedisConnectionPool from 'redis-connection-pool';
const redisPool = new RedisConnectionPool();
await redisPool.init();
```
## Implemented methods

@@ -109,3 +115,12 @@

* **sendCommand**
```javascript
sendCommand(commandName, [args])
```
* **shutdown**
```javascript
shutdown()
```
## API Documentation

@@ -112,0 +127,0 @@ node-redis-connection-pool uses jsdoc-to-markdown to generate the [API.md](API.md) from the source code.

import { expect } from 'chai';
// import * as sinon from 'sinon';
import redisConnectionPoolFactory from './index';
const channel = 'redis-connection-pool-tests:';
const uid = 'redisPoolTest1';
describe('Redis Pool', () => {
let pool;
beforeEach(async () => {
console.log('init');
pool = redisConnectionPoolFactory('redisPoolTest1', {
max_clients: 5,
redis: {
url: '127.0.0.1:6379',
database: 1
},
});
await pool.init();
pool = await redisConnectionPoolFactory(uid,
{
redis: {
url: 'redis://localhost:6379'
}
});
});
afterEach(() => {
console.log('clean');
pool.clean('*');
pool = undefined;
afterEach(async () => {
const keys = await pool.keys(`${channel}*`);
for (const key of keys) {
await pool.del(`${key}`);
}
});

@@ -27,19 +27,62 @@

expect(typeof pool).to.eql('object');
expect(pool.uid).to.eql('redisPoolTest1');
expect(pool.redis.url).to.eql('127.0.0.1:6379');
expect(pool.redis.url).to.eql('redis://localhost:6379');
});
describe('store and fetch', () => {
console.log('-0')
it('basic store and fetch', async () => {
expect(await pool.set(channel, 'a value')).to.eql('OK');
expect(await pool.get(channel)).to.equal('a value');
});
it('set', async () => {
console.log('-1')
expect(await pool.set('an id', 'a value')).to.eql('OK');
it('hset and hget', async () => {
expect(await pool.hset(channel, 'a name', 'a value')).to.eql(1);
expect(await pool.hget(channel, 'a name')).to.equal('a value');
});
it('hgetall', async () => {
expect(await pool.hset(channel, 'a name', 'a value')).to.eql(1);
expect(await pool.hset(channel, 'b name', 'b value')).to.eql(1);
expect(await pool.hset(channel, 'c name', 'c value')).to.eql(1);
expect(await pool.hset(channel, 'd name', 'd value')).to.eql(1);
expect(await pool.hset(channel, 'e name', 'e value')).to.eql(1);
expect(await pool.hgetall(channel)).to.eql({
'a name': 'a value',
'b name': 'b value',
'c name': 'c value',
'd name': 'd value',
'e name': 'e value'
});
it('get', async () => {
console.log('-2')
expect(await pool.get('an id')).to.equal('a value');
});
it('push and pop ', async () => {
expect(await pool.rpush(channel, 'foo1')).to.eql(1);
expect(await pool.rpush(channel, 'foo2')).to.eql(2);
expect(await pool.rpush(channel, 'foo3')).to.eql(3);
expect(await pool.lpush(channel, 'foo4')).to.eql(4);
expect(await pool.lpush(channel, 'foo5')).to.eql(5);
expect(await pool.brpop(channel)).to.eql({
key: channel,
element: 'foo3'
});
expect(await pool.blpop(channel)).to.eql({
key: channel,
element: 'foo5'
});
});
it('incr', async () => {
expect(await pool.set(channel, 1)).to.eql('OK');
expect(await pool.incr(channel)).to.eql(2);
expect(await pool.incr(channel)).to.eql(3);
expect(await pool.get(channel)).to.eql('3');
});
});
describe("Shutdown", () => {
it('', async () => {
const pool = await redisConnectionPoolFactory(uid);
await pool.shutdown();
});
});

@@ -27,3 +27,5 @@ import { expect } from 'chai';

},
release: async () => {}
release: async () => {},
drain: sinon.stub(),
clear: sinon.stub()
}

@@ -46,4 +48,4 @@ }

beforeEach(() => {
pool = redisConnectionPoolFactory('foo', {
beforeEach(async () => {
pool = await redisConnectionPoolFactory('foo', {
max_clients: 99,

@@ -55,3 +57,3 @@ redis: {

});
expect(pool.uid).to.equal('foo');
expect(pool.max_clients).to.equal(99);
for (const key of Object.keys(clientFake)) {

@@ -68,3 +70,2 @@ clientFake[key].reset();

it('can initialize pool', () => {
expect(pool.pool).to.be.undefined;
pool.init();

@@ -150,13 +151,5 @@ expect(typeof pool.pool).to.equal('object');

it('clean', async () => {
clientFake.keys.returns(['foo', 'bar']);
pool.del = sinon.stub();
await pool.clean();
sinon.assert.calledOnce(clientFake.connect);
sinon.assert.calledOnce(clientFake.quit);
sinon.assert.calledOnce(clientFake.keys);
sinon.assert.calledTwice(pool.del);
sinon.assert.calledWith(pool.del, 'foo');
sinon.assert.calledWith(pool.del, 'bar');
})
it('shutdown', async () => {
await pool.shutdown();
});
});

@@ -37,15 +37,5 @@ /**

type FuncNameType = 'HDEL' | 'DEL' | 'GET' | 'HGETALL' | 'TTL' | 'INCR' |
'BRPOP' | 'HGET' | 'BLPOP' | 'BRPOPLPUSH' | 'EXPIRE'
'BRPOP' | 'HGET' | 'BLPOP' | 'BRPOPLPUSH' | 'EXPIRE' | 'KEYS'
type IdentifierType = string;
export default function redisConnectionPoolFactory(
uid: IdentifierType,
cfg: RedisConnectionPoolConfig
): RedisConnectionPool {
if (! connectionPools.has(uid)) {
connectionPools.set(uid, new RedisConnectionPool(uid, cfg));
}
return connectionPools.get(uid);
}
/**

@@ -73,7 +63,2 @@ * Function: redisConnectionPoolFactory

*
* cfg.perform_checks - (boolean) - Perform a series of redis checks,
* currently this checks to see if
* blocking push/pops can be used.
* (default: false)
*
* cfg.redis - (object) - A redis config object

@@ -85,13 +70,28 @@ *

*/
export default async function redisConnectionPoolFactory(
uid: IdentifierType,
cfg: RedisConnectionPoolConfig = {}
): Promise<RedisConnectionPool> {
let pool;
if (! connectionPools.has(uid)) {
pool = new RedisConnectionPool(cfg);
connectionPools.set(uid, pool);
await pool.init();
} else {
pool = connectionPools.get(uid);
}
return pool;
}
/**
* RedisConnectionPool
*/
export class RedisConnectionPool {
uid: IdentifierType;
max_clients = 10;
perform_checks = false;
max_clients = 5;
redis: RedisClientOptions;
pool: Pool<RedisClientType<RedisModules, RedisScripts>>;
private initializing = false;
constructor(uid: IdentifierType, cfg: RedisConnectionPoolConfig = {}) {
this.uid = uid;
constructor(cfg: RedisConnectionPoolConfig = {}) {
this.max_clients = cfg.max_clients || this.max_clients;
this.perform_checks = this.perform_checks || this.perform_checks;
this.redis = cfg.redis;

@@ -104,5 +104,15 @@ }

log('create');
if (this.initializing) {
log(
'Create method already called. (Redis config error? ' +
'or maybe you forgot to await the init function?)');
throw Error(
'Create method already called. (Redis config error? ' +
'or maybe you forgot to await the init function?)');
} else {
this.initializing = true;
}
const client = createClient(this.redis);
client.on('error', function (err) {
log(err);
client.on('error', (err) => {
throw new Error(err);
});

@@ -114,2 +124,3 @@ client.on('ready', () => {

await client.connect();
this.initializing = false;
return client;

@@ -155,2 +166,16 @@ },

/**
* Function: keys
*
* Execute a redis KEYS command
*
* Parameters:
*
* key - (string) - The prefix of the keys to return
*
*/
async keys(key: string) {
return await this.singleCommand('KEYS', [key]);
}
/**
* Function: hdel

@@ -171,3 +196,3 @@ *

/**
* Function: send_command
* Function: sendCommand
*

@@ -185,3 +210,3 @@ * Sends an explicit command to the redis server. Helpful for new commands in redis

*/
async send_command(command_name, args) {
async sendCommand(command_name, args) {
return await this.singleCommand(command_name, args);

@@ -381,26 +406,10 @@ }

/**
* Function: clean
* Function: shutdown
*
* Clean the redis key namespace
* Drain the pool and close all connections to Redis.
*
* Parameters:
*
* key - (string) - The key of the value you wish to clear (can use wildcard *)
*
*/
async clean(key: string) {
log('clearing redis key ' + key);
const client = createClient(this.redis);
await client.connect();
const keys = await client.keys(key);
await client.quit();
if (Array.isArray(keys)) {
await keys.forEach((name) => {
log('deleting name ' + name);
this.del(name);
});
} else {
log(`ERROR couldn't get keys list on key '${key}': `, keys);
}
async shutdown() {
await this.pool.drain();
await this.pool.clear();
}

@@ -407,0 +416,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc