Skip to content

Commit 3bc6165

Browse files
committed
fix(cluster): passing frozen natMap option causes crash
Closes: #887
1 parent 1d606e4 commit 3bc6165

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/cluster/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ class Cluster extends EventEmitter {
438438
const mapped = this.options.natMap[key]
439439
if (mapped) {
440440
debug('NAT mapping %s -> %O', key, mapped)
441-
return mapped
441+
return Object.assign({}, mapped)
442442
}
443443
}
444444
return typeof nodeKey === 'string' ? nodeKeyToRedisOptions(nodeKey) : nodeKey

test/functional/cluster/nat.js

+27
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,31 @@ describe('NAT', () => {
124124

125125
cluster.get('foo')
126126
})
127+
128+
it('keeps options immutable', (done) => {
129+
const slotTable = [
130+
[0, 16383, ['192.168.1.1', 30001]]
131+
]
132+
133+
new MockServer(30001, null, slotTable)
134+
135+
const cluster = new Redis.Cluster([{
136+
host: '127.0.0.1',
137+
port: 30001
138+
}], Object.freeze({
139+
natMap: Object.freeze({
140+
'192.168.1.1:30001': Object.freeze({host: '127.0.0.1', port: 30001})
141+
})
142+
}))
143+
144+
const reset = spy(cluster.connectionPool, 'reset')
145+
146+
cluster.on('ready', () => {
147+
expect(reset.secondCall.args[0]).to.deep.equal([
148+
{host: '127.0.0.1', port: 30001, readOnly: false}
149+
])
150+
cluster.disconnect()
151+
done()
152+
})
153+
})
127154
})

0 commit comments

Comments
 (0)