UNPKG

7.86 kBJavaScriptView Raw
1'use strict';
2
3var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4
5var _events = require('events');
6
7var _ioredis = require('ioredis');
8
9var _commands = require('./commands');
10
11var commands = _interopRequireWildcard(_commands);
12
13var _commandsStream = require('./commands-stream');
14
15var commandsStream = _interopRequireWildcard(_commandsStream);
16
17var _command = require('./command');
18
19var _command2 = _interopRequireDefault(_command);
20
21var _data = require('./data');
22
23var _data2 = _interopRequireDefault(_data);
24
25var _expires = require('./expires');
26
27var _expires2 = _interopRequireDefault(_expires);
28
29var _emitConnectEvent = require('./commands-utils/emitConnectEvent');
30
31var _emitConnectEvent2 = _interopRequireDefault(_emitConnectEvent);
32
33var _pipeline = require('./pipeline');
34
35var _pipeline2 = _interopRequireDefault(_pipeline);
36
37var _promiseContainer = require('./promise-container');
38
39var _promiseContainer2 = _interopRequireDefault(_promiseContainer);
40
41var _keyspaceNotifications = require('./keyspace-notifications');
42
43var _keyspaceNotifications2 = _interopRequireDefault(_keyspaceNotifications);
44
45function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
46
47function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
48
49function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
50
51function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); }
52
53function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
54
55function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
56
57function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
58
59var defaultOptions = {
60 data: {},
61 keyPrefix: '',
62 lazyConnect: false,
63 notifyKeyspaceEvents: '' // string pattern as specified in https://redis.io/topics/notifications#configuration e.g. 'gxK'
64};
65
66var RedisMock = function (_EventEmitter) {
67 _inherits(RedisMock, _EventEmitter);
68
69 function RedisMock() {
70 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
71
72 _classCallCheck(this, RedisMock);
73
74 var _this = _possibleConstructorReturn(this, (RedisMock.__proto__ || Object.getPrototypeOf(RedisMock)).call(this));
75
76 _this.channels = new _events.EventEmitter();
77 _this.patternChannels = new _events.EventEmitter();
78 _this.batch = undefined;
79 _this.connected = false;
80 _this.subscriberMode = false;
81
82 var optionsWithDefault = Object.assign({}, defaultOptions, options);
83
84 _this.expires = (0, _expires2.default)(optionsWithDefault.keyPrefix);
85
86 _this.data = (0, _data2.default)(_this.expires, optionsWithDefault.data, optionsWithDefault.keyPrefix);
87
88 _this._initCommands();
89
90 _this.keyspaceEvents = (0, _keyspaceNotifications2.default)(optionsWithDefault.notifyKeyspaceEvents);
91
92 if (optionsWithDefault.lazyConnect === false) {
93 _this.connected = true;
94 (0, _emitConnectEvent2.default)(_this);
95 }
96 return _this;
97 }
98
99 _createClass(RedisMock, [{
100 key: 'multi',
101 value: function multi() {
102 var _this2 = this;
103
104 var batch = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
105
106 this.batch = new _pipeline2.default(this);
107 // eslint-disable-next-line no-underscore-dangle
108 this.batch._transactions += 1;
109
110 batch.forEach(function (_ref) {
111 var _batch;
112
113 var _ref2 = _toArray(_ref),
114 command = _ref2[0],
115 options = _ref2.slice(1);
116
117 return (_batch = _this2.batch)[command].apply(_batch, _toConsumableArray(options));
118 });
119
120 return this.batch;
121 }
122 }, {
123 key: 'pipeline',
124 value: function pipeline() {
125 var _this3 = this;
126
127 var batch = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
128
129 this.batch = new _pipeline2.default(this);
130
131 batch.forEach(function (_ref3) {
132 var _batch2;
133
134 var _ref4 = _toArray(_ref3),
135 command = _ref4[0],
136 options = _ref4.slice(1);
137
138 return (_batch2 = _this3.batch)[command].apply(_batch2, _toConsumableArray(options));
139 });
140
141 return this.batch;
142 }
143 }, {
144 key: 'exec',
145 value: function exec(callback) {
146 var Promise = _promiseContainer2.default.get();
147
148 if (!this.batch) {
149 return Promise.reject(new Error('ERR EXEC without MULTI'));
150 }
151 var pipeline = this.batch;
152 this.batch = undefined;
153 return pipeline.exec(callback);
154 }
155 }, {
156 key: 'createConnectedClient',
157 value: function createConnectedClient() {
158 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
159
160 var mock = new RedisMock(options);
161 mock.expires = typeof options.keyPrefix === 'string' ? this.expires.withKeyPrefix(options.keyPrefix) : this.expires;
162 mock.data = typeof options.keyPrefix === 'string' ? this.data.withKeyPrefix(options.keyPrefix) : this.data;
163 mock.channels = this.channels;
164 mock.patternChannels = this.patternChannels;
165 return mock;
166 }
167
168 // eslint-disable-next-line class-methods-use-this
169
170 }, {
171 key: 'disconnect',
172 value: function disconnect() {
173 // no-op
174 }
175 }, {
176 key: '_initCommands',
177 value: function _initCommands() {
178 var _this4 = this;
179
180 Object.keys(commands).forEach(function (command) {
181 var commandName = command === 'evaluate' ? 'eval' : command;
182 _this4[commandName] = (0, _command2.default)(commands[command].bind(_this4), commandName, _this4);
183 });
184
185 Object.keys(commandsStream).forEach(function (command) {
186 _this4[command] = commandsStream[command].bind(_this4);
187 });
188 }
189 }]);
190
191 return RedisMock;
192}(_events.EventEmitter);
193
194RedisMock.prototype.Command = {
195 // eslint-disable-next-line no-underscore-dangle
196 transformers: _ioredis.Command._transformer,
197 setArgumentTransformer: function setArgumentTransformer(name, func) {
198 RedisMock.prototype.Command.transformers.argument[name] = func;
199 },
200
201 setReplyTransformer: function setReplyTransformer(name, func) {
202 RedisMock.prototype.Command.transformers.reply[name] = func;
203 }
204};
205
206Object.defineProperty(RedisMock, 'Promise', {
207 get: function get() {
208 return _promiseContainer2.default.get();
209 },
210 set: function set(lib) {
211 return _promiseContainer2.default.set(lib);
212 }
213});
214
215module.exports = RedisMock;
\No newline at end of file