diff --git a/lib/cluster/index.ts b/lib/cluster/index.ts index 4b39d8fa..9ae7e932 100644 --- a/lib/cluster/index.ts +++ b/lib/cluster/index.ts @@ -385,6 +385,29 @@ class Cluster extends EventEmitter { ); } + /** + * Create a new instance with the same startup nodes and options as the current one. + * + * @example + * ```js + * var cluster = new Redis.Cluster([{ host: "127.0.0.1", port: "30001" }]); + * var anotherCluster = cluster.duplicate(); + * ``` + * + * @public + * @param {(Array)} [overrideStartupNodes=[]] + * @param {IClusterOptions} [overrideOptions={}] + * @memberof Cluster + */ + public duplicate(overrideStartupNodes = [], overrideOptions = {}) { + const startupNodes = + overrideStartupNodes.length > 0 + ? overrideStartupNodes + : this.startupNodes.slice(0); + const options = Object.assign({}, this.options, overrideOptions); + return new Cluster(startupNodes, options); + } + /** * Get nodes with the specified role * diff --git a/test/functional/cluster/duplicate.ts b/test/functional/cluster/duplicate.ts new file mode 100644 index 00000000..be28e5cd --- /dev/null +++ b/test/functional/cluster/duplicate.ts @@ -0,0 +1,21 @@ +import MockServer from "../../helpers/mock_server"; +import { Cluster } from "../../../lib"; +import { expect } from "chai"; + +describe("cluster:duplicate", () => { + it("clone the options", done => { + var node = new MockServer(30001); + var cluster = new Cluster([]); + var duplicatedCluster = cluster.duplicate([ + { host: "127.0.0.1", port: "30001" } + ]); + + node.once("connect", function() { + expect(duplicatedCluster.nodes()).to.have.lengthOf(1); + expect(duplicatedCluster.nodes()[0].options.port).to.eql(30001); + cluster.disconnect(); + duplicatedCluster.disconnect(); + done(); + }); + }); +});