Skip to content

Commit

Permalink
fix: remove dropBufferSupport option
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Mar 14, 2022
1 parent 32eb381 commit 04e68ac
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 198 deletions.
96 changes: 69 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ ioredis is a robust, full-featured Redis client that is
used in the world's biggest online commerce company [Alibaba](http://www.alibaba.com/) and many other awesome companies.

0. Full-featured. It supports [Cluster](http://redis.io/topics/cluster-tutorial), [Sentinel](http://redis.io/topics/sentinel), [Streams](https://redis.io/topics/streams-intro), [Pipelining](http://redis.io/topics/pipelining), and of course [Lua scripting](http://redis.io/commands/eval), [Redis Functions](https://redis.io/topics/functions-intro), [Pub/Sub](http://redis.io/topics/pubsub) (with the support of binary messages).
0. High performance 🚀.
0. Delightful API 😄. It works with Node callbacks and Native promises.
0. Transformation of command arguments and replies.
0. Transparent key prefixing.
0. Abstraction for Lua scripting, allowing you to [define custom commands](https://github.com/luin/ioredis#lua-scripting).
0. Supports [binary data](https://github.com/luin/ioredis#handle-binary-data).
0. Supports [TLS](https://github.com/luin/ioredis#tls-options) 🔒.
0. Supports offline queue and ready checking.
0. Supports ES6 types, such as `Map` and `Set`.
0. Supports GEO commands 📍.
0. Supports Redis ACL.
0. Sophisticated error handling strategy.
0. Supports NAT mapping.
0. Supports autopipelining
1. High performance 🚀.
2. Delightful API 😄. It works with Node callbacks and Native promises.
3. Transformation of command arguments and replies.
4. Transparent key prefixing.
5. Abstraction for Lua scripting, allowing you to [define custom commands](https://github.com/luin/ioredis#lua-scripting).
6. Supports [binary data](https://github.com/luin/ioredis#handle-binary-data).
7. Supports [TLS](https://github.com/luin/ioredis#tls-options) 🔒.
8. Supports offline queue and ready checking.
9. Supports ES6 types, such as `Map` and `Set`.
10. Supports GEO commands 📍.
11. Supports Redis ACL.
12. Sophisticated error handling strategy.
13. Supports NAT mapping.
14. Supports autopipelining

# Versions

Expand Down Expand Up @@ -105,35 +105,77 @@ $ npm install ioredis

```javascript
const Redis = require("ioredis");
const redis = new Redis(); // uses defaults unless given configuration object

// ioredis supports all Redis commands:
redis.set("foo", "bar"); // returns promise which resolves to string, "OK"
// First, you need to create a Redis instance.
// We are going to cover how to specify host, port, and other connection
// options soon.
const redis = new Redis();

// the format is: redis[SOME_REDIS_COMMAND_IN_LOWERCASE](ARGUMENTS_ARE_JOINED_INTO_COMMAND_STRING)
// the js: ` redis.set("mykey", "Hello") ` is equivalent to the cli: ` redis> SET mykey "Hello" `
// Invoke the SET command. This is equivalent to the cli `redis> SET name Bob`:
redis.set("name", "Bob"); // Returns a Promise

// ioredis supports the node.js callback style
redis.get("foo", function (err, result) {
// ioredis provides two kind of APIs, Node.js callback and Promise.
// 1. You can pass a callback as the last parameter. It will be called when
// we get a response from the Redis server:
redis.get("name", (err, value) => {
if (err) {
console.error(err);
} else {
console.log(result); // Promise resolves to "bar"
console.log(value); // "Bob"
}
});

// 2. Additionally, every command method returns a Promise
// representing the server response:
redis.get("name").then(
(value) => {
console.log(value);
},
(err) => {
console.error(err);
}
);

//

// Every

async function main() {
await redis.set("mykey", "Hello, World!");
const value = await redis.get("mykey"); // value === "Hello, World!"
}

main();
```

// ioredis supports all Redis commands:
redis.set("foo", "bar"); // returns promise which resolves to string, "OK"

// the format is: redis[REDIS_COMMAND_NAME_IN_LOWERCASE](ARGUMENTS_ARE_JOINED_INTO_COMMAND_STRING)
// the js: `redis.set("mykey", "Hello")` is equivalent to the cli: ` redis> SET mykey "Hello"`

// ioredis supports the Node.js callback style
redis.get("foo", (err, result) => {
if (err) {
console.error(err);
} else {
console.log(result); // Promise resolves to "bar"
}
});

// Or ioredis returns a promise if the last argument isn't a function
redis.get("foo").then(function (result) {
console.log(result); // Prints "bar"
redis.get("foo").then((result) => {
console.log(result); // Prints "bar"
});

// Most responses are strings, or arrays of strings
redis.zadd("sortedSet", 1, "one", 2, "dos", 4, "quatro", 3, "three");
redis.zrange("sortedSet", 0, 2, "WITHSCORES").then((res) => console.log(res)); // Promise resolves to ["one", "1", "dos", "2", "three", "3"] as if the command was ` redis> ZRANGE sortedSet 0 2 WITHSCORES `
redis.zrange("sortedSet", 0, 2, "WITHSCORES").then((res) => console.log(res)); // Promise resolves to ["one", "1", "dos", "2", "three", "3"] as if the command was `redis> ZRANGE sortedSet 0 2 WITHSCORES`

// All arguments are passed directly to the redis server:
redis.set("key", 100, "EX", 10);
```

````
See the `examples/` folder for more examples.
Expand All @@ -155,7 +197,7 @@ new Redis({
password: "auth",
db: 0,
});
```
````

You can also specify connection options as a [`redis://` URL](http://www.iana.org/assignments/uri-schemes/prov/redis) or [`rediss://` URL](https://www.iana.org/assignments/uri-schemes/prov/rediss) when using [TLS encryption](#tls-options):

Expand Down
3 changes: 1 addition & 2 deletions lib/DataHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ interface DataHandledable extends EventEmitter {

interface ParserOptions {
stringNumbers: boolean;
dropBufferSupport: boolean;
}

export default class DataHandler {
constructor(private redis: DataHandledable, parserOptions: ParserOptions) {
const parser = new RedisParser({
stringNumbers: parserOptions.stringNumbers,
returnBuffers: !parserOptions.dropBufferSupport,
returnBuffers: true,
returnError: (err: Error) => {
this.returnError(err);
},
Expand Down
6 changes: 1 addition & 5 deletions lib/Pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ Pipeline.prototype.multi = function () {

// @ts-expect-error
const execBuffer = Pipeline.prototype.execBuffer;
const exec = Pipeline.prototype.exec;
// @ts-expect-error
Pipeline.prototype.execBuffer = deprecate(function () {
if (this._transactions > 0) {
Expand Down Expand Up @@ -278,10 +277,7 @@ Pipeline.prototype.exec = function (callback: Callback): Promise<Array<any>> {

if (this._transactions > 0) {
this._transactions -= 1;
return (this.options.dropBufferSupport ? exec : execBuffer).apply(
this,
arguments
);
return execBuffer.apply(this, arguments);
}
if (!this.nodeifiedPromise) {
this.nodeifiedPromise = true;
Expand Down
1 change: 0 additions & 1 deletion lib/connectors/SentinelConnector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ export default class SentinelConnector extends AbstractConnector {
enableReadyCheck: false,
connectTimeout: this.options.connectTimeout,
commandTimeout: this.options.sentinelCommandTimeout,
dropBufferSupport: true,
...options,
});
// @ts-expect-error
Expand Down
2 changes: 0 additions & 2 deletions lib/redis/RedisOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ interface CommonRedisOptions extends CommanderOptions {
username?: string;
password?: string;
db?: number;
dropBufferSupport?: boolean;
autoResubscribe?: boolean;
autoResendUnfulfilledCommands?: boolean;
reconnectOnError?: ReconnectOnError;
Expand Down Expand Up @@ -76,7 +75,6 @@ export const DEFAULT_REDIS_OPTIONS: RedisOptions = {
password: null,
db: 0,
// Others
dropBufferSupport: false,
enableOfflineQueue: true,
enableReadyCheck: true,
autoResubscribe: true,
Expand Down
1 change: 0 additions & 1 deletion lib/redis/event_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export function connectHandler(self) {
*/
new DataHandler(self, {
stringNumbers: self.options.stringNumbers,
dropBufferSupport: self.options.dropBufferSupport,
});

if (self.options.enableReadyCheck) {
Expand Down
35 changes: 8 additions & 27 deletions lib/utils/Commander.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { list } from "@ioredis/commands";
import asCallback from "standard-as-callback";
import {
executeWithAutoPipelining,
shouldUseAutoPipelining,
Expand All @@ -14,11 +13,6 @@ export interface CommanderOptions {
showFriendlyErrorStack?: boolean;
}

const DROP_BUFFER_SUPPORT_ERROR =
"*Buffer methods are not available " +
'because "dropBufferSupport" option is enabled.' +
"Refer to https://github.com/luin/ioredis/wiki/Improve-Performance for more details.";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
class Commander<Context extends ClientContext = { type: "default" }> {
options: CommanderOptions = {};
Expand Down Expand Up @@ -154,13 +148,6 @@ function generateFunction(
replyEncoding: _encoding,
};

if (this.options.dropBufferSupport && !_encoding) {
return asCallback(
Promise.reject(new Error(DROP_BUFFER_SUPPORT_ERROR)),
callback as Callback | undefined
);
}

// No auto pipeline, use regular command sending
if (!shouldUseAutoPipelining(this, functionName, commandName)) {
return this.sendCommand(
Expand All @@ -185,24 +172,18 @@ function generateScriptingFunction(
functionName: string,
commandName: string,
script: Script,
encoding: unknown
encoding: BufferEncoding | null
) {
return function (...args) {
return function (...args: any[]) {
const callback =
typeof args[args.length - 1] === "function" ? args.pop() : undefined;

let options;
if (this.options.dropBufferSupport) {
if (!encoding) {
return asCallback(
Promise.reject(new Error(DROP_BUFFER_SUPPORT_ERROR)),
callback
);
}
options = { replyEncoding: null };
} else {
options = { replyEncoding: encoding };
}
const options: {
replyEncoding: BufferEncoding | null;
errorStack?: Error;
} = {
replyEncoding: encoding,
};

if (this.options.showFriendlyErrorStack) {
options.errorStack = new Error();
Expand Down
Loading

0 comments on commit 04e68ac

Please sign in to comment.