Skip to content

Commit 513c965

Browse files
moaxacajd20DreadPirateSergeyjakesylvestredependabot[bot]
authored
Release 2.0.0 (#32)
* Decorate Multi objects returned by client (#15) * Decorate Multi objects returned by client * Fix linter warnings * Remove mutating of original redisClient object. (#37) * Add link to node redis library * Bump stringstream from 0.0.5 to 0.0.6 (#27) Bumps [stringstream](https://github.com/mhart/StringStream) from 0.0.5 to 0.0.6. - [Release notes](https://github.com/mhart/StringStream/releases) - [Commits](mhart/StringStream@v0.0.5...v0.0.6) Signed-off-by: dependabot[bot] <[email protected]> * Remove mutating of original redisClient object Co-authored-by: Jake <[email protected]> Co-authored-by: Matthew Oaxaca <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * upgrading redis to 3.0.0 (#36) * Add link to node redis library * Bump stringstream from 0.0.5 to 0.0.6 (#27) Bumps [stringstream](https://github.com/mhart/StringStream) from 0.0.5 to 0.0.6. - [Release notes](https://github.com/mhart/StringStream/releases) - [Commits](mhart/StringStream@v0.0.5...v0.0.6) Signed-off-by: dependabot[bot] <[email protected]> * upgrading redis Co-authored-by: Jake <[email protected]> Co-authored-by: Matthew Oaxaca <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump handlebars from 4.0.11 to 4.5.3 (#35) * Add link to node redis library * Bump stringstream from 0.0.5 to 0.0.6 (#27) Bumps [stringstream](https://github.com/mhart/StringStream) from 0.0.5 to 0.0.6. - [Release notes](https://github.com/mhart/StringStream/releases) - [Commits](mhart/StringStream@v0.0.5...v0.0.6) Signed-off-by: dependabot[bot] <[email protected]> * Bump handlebars from 4.0.11 to 4.5.3 Bumps [handlebars](https://github.com/wycats/handlebars.js) from 4.0.11 to 4.5.3. - [Release notes](https://github.com/wycats/handlebars.js/releases) - [Changelog](https://github.com/wycats/handlebars.js/blob/master/release-notes.md) - [Commits](handlebars-lang/handlebars.js@v4.0.11...v4.5.3) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Jake <[email protected]> Co-authored-by: Matthew Oaxaca <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump eslint from 4.17.0 to 4.18.2 (#31) * Add link to node redis library * Bump stringstream from 0.0.5 to 0.0.6 (#27) Bumps [stringstream](https://github.com/mhart/StringStream) from 0.0.5 to 0.0.6. - [Release notes](https://github.com/mhart/StringStream/releases) - [Commits](mhart/StringStream@v0.0.5...v0.0.6) Signed-off-by: dependabot[bot] <[email protected]> * Bump sshpk from 1.13.1 to 1.16.1 (#33) Bumps [sshpk](https://github.com/joyent/node-sshpk) from 1.13.1 to 1.16.1. - [Release notes](https://github.com/joyent/node-sshpk/releases) - [Commits](TritonDataCenter/node-sshpk@v1.13.1...v1.16.1) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump eslint from 4.17.0 to 4.18.2 Bumps [eslint](https://github.com/eslint/eslint) from 4.17.0 to 4.18.2. - [Release notes](https://github.com/eslint/eslint/releases) - [Changelog](https://github.com/eslint/eslint/blob/master/CHANGELOG.md) - [Commits](eslint/eslint@v4.17.0...v4.18.2) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Jake <[email protected]> Co-authored-by: Matthew Oaxaca <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump js-yaml from 3.10.0 to 3.13.1 (#30) * Add link to node redis library * Bump stringstream from 0.0.5 to 0.0.6 (#27) Bumps [stringstream](https://github.com/mhart/StringStream) from 0.0.5 to 0.0.6. - [Release notes](https://github.com/mhart/StringStream/releases) - [Commits](mhart/StringStream@v0.0.5...v0.0.6) Signed-off-by: dependabot[bot] <[email protected]> * Bump sshpk from 1.13.1 to 1.16.1 (#33) Bumps [sshpk](https://github.com/joyent/node-sshpk) from 1.13.1 to 1.16.1. - [Release notes](https://github.com/joyent/node-sshpk/releases) - [Commits](TritonDataCenter/node-sshpk@v1.13.1...v1.16.1) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump js-yaml from 3.10.0 to 3.13.1 Bumps [js-yaml](https://github.com/nodeca/js-yaml) from 3.10.0 to 3.13.1. - [Release notes](https://github.com/nodeca/js-yaml/releases) - [Changelog](https://github.com/nodeca/js-yaml/blob/master/CHANGELOG.md) - [Commits](nodeca/js-yaml@3.10.0...3.13.1) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Jake <[email protected]> Co-authored-by: Matthew Oaxaca <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump lodash from 4.17.5 to 4.17.15 (#28) * Add link to node redis library * Bump stringstream from 0.0.5 to 0.0.6 (#27) Bumps [stringstream](https://github.com/mhart/StringStream) from 0.0.5 to 0.0.6. - [Release notes](https://github.com/mhart/StringStream/releases) - [Commits](mhart/StringStream@v0.0.5...v0.0.6) Signed-off-by: dependabot[bot] <[email protected]> * Bump sshpk from 1.13.1 to 1.16.1 (#33) Bumps [sshpk](https://github.com/joyent/node-sshpk) from 1.13.1 to 1.16.1. - [Release notes](https://github.com/joyent/node-sshpk/releases) - [Commits](TritonDataCenter/node-sshpk@v1.13.1...v1.16.1) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump lodash from 4.17.5 to 4.17.15 Bumps [lodash](https://github.com/lodash/lodash) from 4.17.5 to 4.17.15. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](lodash/lodash@4.17.5...4.17.15) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Jake <[email protected]> Co-authored-by: Matthew Oaxaca <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Dependency Update & Lint * Feature/Proxy Implementation (#41) * WIP Proxy Implementation * WIP Test Coverage * Fixing Tests Co-authored-by: Jason <[email protected]> Co-authored-by: Sergey Kazantsev <[email protected]> Co-authored-by: Jake <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Chris Peddecord <[email protected]>
1 parent 7ffd3b7 commit 513c965

20 files changed

+2047
-2048
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
REDIS_HOST=localhost
2+
REDIS_PORT=6379

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
test/
1+
node_modules/

.eslintrc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
{
22
"root": true,
3-
"extends": [
4-
"airbnb"
5-
],
3+
"extends": ["eslint:recommended"],
4+
"parserOptions": {
5+
"ecmaVersion": 8,
6+
"sourceType": "script"
7+
},
68
"env": {
7-
"node": true
9+
"es6": true,
10+
"node": true,
11+
"mocha": true
812
}
913
}
File renamed without changes.

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ To install the stable version:
1616
npm install --save async-redis
1717
```
1818

19-
## Typescript Support
20-
```
21-
npm install --save-dev @types/async-redis
22-
```
23-
24-
[types](https://www.npmjs.com/package/@types/async-redis)
25-
2619
## Usage Example
2720

2821
### Creating Connection

examples/create-redis.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
const asyncRedis = require("../src");
1+
/* eslint no-console: 0 */
2+
const asyncRedis = require('../src');
3+
24
const client = asyncRedis.createClient();
35

4-
client.on("error", function (err) {
5-
console.log("Error " + err);
6+
client.on('error', (err) => {
7+
console.log(`Error ${err}`);
68
});
79

8-
let addToSet = async () => {
9-
let key = 'exampleSet';
10-
let values = [
10+
const addToSet = async () => {
11+
const key = 'exampleSet';
12+
const values = [
1113
'item_1',
1214
'item_2',
1315
'item_3',
1416
'item_4',
1517
'item_5',
1618
];
17-
let promises = values.map((value) => {
18-
return client.sadd(key, value);
19-
});
19+
const promises = values.map(value => client.sadd(key, value));
2020
await Promise.all(promises);
21-
return await client.smembers(key);
21+
return client.smembers(key);
2222
};
2323

24-
let flush = async () => {
25-
return await client.flushall();
26-
};
24+
const flush = async () => client.flushall();
2725

2826
addToSet()
2927
.then((results) => {

package.json

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
{
22
"private": false,
3-
"version": "1.1.7",
3+
"author": "Matthew Oaxaca",
4+
"license": "MIT",
5+
"version": "2.0.0",
46
"name": "async-redis",
57
"keywords": [
68
"redis",
79
"async",
810
"promise",
911
"es6"
1012
],
11-
"author": "Matthew Oaxaca",
12-
"license": "MIT",
1313
"description": "Light wrapper over redis_node with first class async & promise support.",
14+
"repository": {
15+
"type": "git",
16+
"url": "git://github.com/moaxaca/async-redis.git"
17+
},
18+
"bugs": {
19+
"url": "https://github.com/moaxaca/async-redis/issues"
20+
},
1421
"main": "src/index.js",
15-
"typings": "./index.d.ts",
22+
"typings": "src/index.d.ts",
1623
"engines": {
1724
"node": ">=7.6.0"
1825
},
26+
"directories": {
27+
"example": "examples",
28+
"test": "test"
29+
},
1930
"scripts": {
2031
"coveralls": "nyc yarn test && nyc report --reporter=text-lcov | coveralls",
21-
"lint": "eslint --fix --ext .js, src",
22-
"test": "mocha",
32+
"lint": "eslint --fix --ext .js, .",
33+
"test": "mocha test/**/*.spec.js --config test/setup.js --exit",
34+
"test:unit": "mocha test/unit/*.spec.js --config test/setup.js --exit",
2335
"version:patch": "npm version patch",
2436
"version:minor": "npm version minor",
2537
"version:major": "npm version major"
@@ -29,26 +41,14 @@
2941
"redis-commands": "^1.3.1"
3042
},
3143
"devDependencies": {
32-
"chai": "^3.5.0",
44+
"@types/node": "^14.0.27",
45+
"@types/redis": "^2.8.25",
46+
"chai": "^4.2.0",
3347
"chai-as-promised": "^7.1.1",
34-
"coveralls": "^3.0.0",
35-
"eslint": "^4.17.0",
36-
"eslint-config-airbnb": "^16.1.0",
37-
"eslint-plugin-import": "^2.8.0",
38-
"eslint-plugin-jsx-a11y": "^6.0.3",
39-
"eslint-plugin-react": "^7.6.1",
40-
"mocha": "^3.2.0",
41-
"nyc": "^11.4.1"
42-
},
43-
"repository": {
44-
"type": "git",
45-
"url": "git://github.com/moaxaca/async-redis.git"
46-
},
47-
"bugs": {
48-
"url": "https://github.com/moaxaca/async-redis/issues"
49-
},
50-
"directories": {
51-
"example": "examples",
52-
"test": "test"
48+
"coveralls": "^3.1.0",
49+
"dotenv": "^8.2.0",
50+
"eslint": "^7.6.0",
51+
"mocha": "^8.1.0",
52+
"nyc": "^15.1.0"
5353
}
5454
}

src/index.d.ts

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import { Commands, RedisClient, ClientOpts, ServerInfo } from 'redis';
2+
import {EventEmitter} from "events";
3+
4+
type Callback<T> = (err: Error | null, reply: T) => void;
5+
6+
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
7+
type Omitted = Omit<RedisClient, keyof Commands<boolean>>;
8+
type OkOrError = 'OK'|Error
9+
10+
interface OverloadedCommand<T, R> {
11+
(arg1: T, arg2: T, arg3: T, arg4: T, arg5: T, arg6: T): R;
12+
(arg1: T, arg2: T, arg3: T, arg4: T, arg5: T): R;
13+
(arg1: T, arg2: T, arg3: T, arg4: T): R;
14+
(arg1: T, arg2: T, arg3: T): R;
15+
(arg1: T, arg2: T | T[]): R;
16+
(arg1: T | T[]): R;
17+
(...args: Array<T>): R;
18+
}
19+
20+
interface Promisified<T = RedisClient> extends Omitted, Commands<Promise<boolean>> {}
21+
22+
interface AsyncRedisConstructor {
23+
new (port: number, host?: string, options?: ClientOpts): Promisified;
24+
new (unix_socket: string, options?: ClientOpts): Promisified;
25+
new (redis_url: string, options?: ClientOpts): Promisified;
26+
new (options?: ClientOpts): Promisified;
27+
28+
createClient(port: number, host?: string, options?: ClientOpts): Promisified;
29+
createClient(unix_socket: string, options?: ClientOpts): Promisified;
30+
createClient(redis_url: string, options?: ClientOpts): Promisified;
31+
createClient(options?: ClientOpts): Promisified;
32+
33+
decorate: (client: RedisClient) => Promisified;
34+
}
35+
36+
interface AsyncRedisEventHandlers extends EventEmitter {
37+
on(event: 'message' | 'message_buffer', listener: (channel: string, message: string) => void): this;
38+
on(event: 'pmessage' | 'pmessage_buffer', listener: (pattern: string, channel: string, message: string) => void): this;
39+
on(event: 'subscribe' | 'unsubscribe', listener: (channel: string, count: number) => void): this;
40+
on(event: 'psubscribe' | 'punsubscribe', listener: (pattern: string, count: number) => void): this;
41+
on(event: string, listener: (...args: any[]) => void): this;
42+
}
43+
44+
interface AsyncRedisCommands<R> {
45+
/**
46+
* Listen for all requests received by the server in real time.
47+
*/
48+
monitor(cb?: Callback<undefined>): any;
49+
MONITOR(cb?: Callback<undefined>): any;
50+
51+
/**
52+
* Get information and statistics about the server.
53+
*/
54+
info(): Promise<ServerInfo|boolean>;
55+
info(section?: string | string[]): Promise<ServerInfo|boolean>;
56+
INFO(): Promise<ServerInfo|boolean>;
57+
INFO(section?: string | string[]): Promise<ServerInfo|boolean>;
58+
59+
/**
60+
* Ping the server.
61+
*/
62+
ping(): Promise<string|boolean>;
63+
ping(message: string): Promise<string|boolean>;
64+
PING(): Promise<string|boolean>;
65+
PING(message: string): Promise<string|boolean>;
66+
67+
/**
68+
* Authenticate to the server.
69+
*/
70+
auth(password: string): Promise<string>;
71+
AUTH(password: string): Promise<string>;
72+
73+
/**
74+
* Get array of Redis command details.
75+
*
76+
* COUNT - Get total number of Redis commands.
77+
* GETKEYS - Extract keys given a full Redis command.
78+
* INFO - Get array of specific REdis command details.
79+
*/
80+
command(cb?: Callback<Array<[string, number, string[], number, number, number]>>): R;
81+
COMMAND(cb?: Callback<Array<[string, number, string[], number, number, number]>>): R;
82+
83+
/**
84+
* Get array of Redis command details.
85+
*
86+
* COUNT - Get array of Redis command details.
87+
* GETKEYS - Extract keys given a full Redis command.
88+
* INFO - Get array of specific Redis command details.
89+
* GET - Get the value of a configuration parameter.
90+
* REWRITE - Rewrite the configuration file with the in memory configuration.
91+
* SET - Set a configuration parameter to the given value.
92+
* RESETSTAT - Reset the stats returned by INFO.
93+
*/
94+
config: OverloadedCommand<string, boolean>;
95+
CONFIG: OverloadedCommand<string, boolean>;
96+
97+
/**
98+
* Return the number of keys in the selected database.
99+
*/
100+
dbsize(): Promise<number>;
101+
DBSIZE(): Promise<number>;
102+
103+
/**
104+
* OBJECT - Get debugging information about a key.
105+
* SEGFAULT - Make the server crash.
106+
*/
107+
debug: OverloadedCommand<string, boolean>;
108+
DEBUG: OverloadedCommand<string, boolean>;
109+
110+
/**
111+
* PubSub Commands
112+
*/
113+
/**
114+
* Post a message to a channel.
115+
*/
116+
publish(channel: string, value: string): Promise<number|boolean>;
117+
PUBLISH(channel: string, value: string): Promise<number|boolean>;
118+
119+
/**
120+
* CRUD Commands
121+
*/
122+
123+
/**
124+
* Append a value to a key.
125+
*/
126+
append(key: string, value: string): Promise<number>;
127+
APPEND(key: string, value: string): Promise<number>;
128+
129+
/**
130+
* Asynchronously rewrite the append-only file.
131+
*/
132+
bgrewriteaof(): Promise<OkOrError>;
133+
BGREWRITEAOF(): Promise<OkOrError>;
134+
135+
/**
136+
* Asynchronously save the dataset to disk.
137+
*/
138+
bgsave(): Promise<OkOrError>;
139+
BGSAVE(): Promise<OkOrError>;
140+
141+
/**
142+
* Determine if a key exists.
143+
*/
144+
exists: OverloadedCommand<string, R>;
145+
EXISTS: OverloadedCommand<string, R>;
146+
147+
/**
148+
* Set the string value of a key.
149+
*/
150+
set(key: string, value: string): Promise<string|boolean>;
151+
set(key: string, value: string, flag: string): Promise<string|boolean>;
152+
set(key: string, value: string, mode: string, duration: number): Promise<string|undefined>;
153+
set(key: string, value: string, mode: string, duration: number, flag: string): Promise<string|undefined>;
154+
SET(key: string, value: string): Promise<string|boolean>;
155+
SET(key: string, value: string, flag: string): Promise<string|boolean>;
156+
SET(key: string, value: string, mode: string, duration: number): Promise<string|undefined>;
157+
SET(key: string, value: string, mode: string, duration: number, flag: string): Promise<string|undefined>;
158+
}
159+
160+
interface AsyncRedisInterface extends AsyncRedisConstructor, AsyncRedisEventHandlers, AsyncRedisCommands<boolean> {}
161+
declare const AsyncRedis: AsyncRedisInterface;
162+
export = AsyncRedis;

src/index.js

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
1-
/* eslint func-names: ["error", "as-needed"] */
2-
31
const redis = require('redis');
4-
const commands = require('redis-commands').list;
52
const objectDecorator = require('./object-decorator');
3+
const objectPromisify = require('./object-promisify');
4+
const redisCommands = require('./redis-commands');
5+
6+
const redisClients = new Map();
67

7-
const AsyncRedis = function (args) {
8-
const client = Array.isArray(args) ? redis.createClient(...args) : redis.createClient(args);
9-
return AsyncRedis.decorate(client);
8+
/**
9+
* @return RedisClient
10+
*/
11+
const AsyncRedis = function (args=null) {
12+
if (args) {
13+
const serializedArgs = JSON.stringify(args);
14+
if (!redisClients.has(serializedArgs)) {
15+
redisClients.set(serializedArgs, Array.isArray(args) ? redis.createClient(...args) : redis.createClient(args));
16+
}
17+
this.setup(redisClients.get(serializedArgs));
18+
}
1019
};
1120

12-
AsyncRedis.createClient = (...args) => new AsyncRedis(args);
21+
AsyncRedis.prototype.setup = function(redisClient) {
22+
this.__redisClient = redisClient;
23+
const commandConfigs = redisCommands(redisClient);
24+
objectDecorator(redisClient, (name, method) => {
25+
if (commandConfigs.commands.has(name)) {
26+
objectPromisify(this, redisClient, name);
27+
} else if (commandConfigs.queueCommands.has(name)) {
28+
return (...args) => {
29+
const multi = method.apply(redisClient, args);
30+
return objectDecorator(multi, (multiName, multiMethod) => {
31+
if (commandConfigs.multiCommands.has(multiName)) {
32+
return objectPromisify(multi, multiMethod);
33+
}
34+
return multiMethod;
35+
});
36+
}
37+
}
38+
});
39+
};
1340

14-
// this is the set of commands to NOT promisify
15-
const commandsToSkipSet = new Set(['multi']);
16-
// this is the set of commands to promisify
17-
const commandSet = new Set(commands.filter(c => !commandsToSkipSet.has(c)));
41+
AsyncRedis.createClient = (...args) => new AsyncRedis(args);
1842

19-
AsyncRedis.decorate = redisClient => objectDecorator(redisClient, (name, method) => {
20-
if (commandSet.has(name)) {
21-
return (...args) => new Promise((resolve, reject) => {
22-
args.push((error, ...results) => {
23-
if (error) {
24-
reject(error, ...results);
25-
} else {
26-
resolve(...results);
27-
}
28-
});
29-
method.apply(redisClient, args);
30-
});
31-
}
32-
return method;
33-
});
43+
AsyncRedis.decorate = (redisClient) => {
44+
const asyncClient = new AsyncRedis();
45+
asyncClient.setup(redisClient);
46+
return asyncClient;
47+
};
3448

3549
module.exports = AsyncRedis;

0 commit comments

Comments
 (0)