UNPKG

9.41 kBJavaScriptView Raw
1var Item = require("./item.js");
2
3// Create a string or buffer Item depending on input value
4function createItem(value) {
5 return (value instanceof Buffer) ? Item.createBuffer(value) : Item.createString(value);
6}
7
8// Allowable types for string operations
9function validType(item) {
10 return item.type === 'string' || item.type === 'buffer';
11}
12
13/**
14 * Set
15 */
16exports.set = function (mockInstance, key, value, callback) {
17
18 mockInstance.storage[key] = createItem(value);
19
20 mockInstance._callCallback(callback, null, "OK");
21};
22
23/**
24 * Ping
25 */
26exports.ping = function (mockInstance, callback) {
27
28 mockInstance._callCallback(callback, null, "PONG");
29};
30
31/**
32* Setnx
33*/
34exports.setnx = function (mockInstance, key, value, callback) {
35 if (key in mockInstance.storage) {
36 mockInstance._callCallback(callback, null, 0);
37 } else {
38 exports.set(mockInstance, key, value, /*callback);,*/ function() {
39 mockInstance._callCallback(callback, null, 1);
40 });
41 }
42};
43
44/**
45 * Get
46 */
47exports.get = function (mockInstance, key, callback) {
48
49 var value = null;
50 var err = null;
51
52 var storedValue = mockInstance.storage[key];
53 if (storedValue) {
54 if (!validType(storedValue)) {
55 err = new Error("WRONGTYPE Operation against a key holding the wrong kind of value");
56 } else if (storedValue.type === 'string') {
57 value = storedValue.value;
58 if (key instanceof Buffer) {
59 value = new Buffer(value);
60 }
61 } else if (storedValue.type === 'buffer') {
62 value = storedValue.value;
63 if (typeof key === 'string') {
64 value = value.toString();
65 }
66 }
67 }
68
69 mockInstance._callCallback(callback, err, value);
70}
71
72/**
73 * Getset
74 */
75exports.getset = function (mockInstance, key, value, callback) {
76
77 exports.get(mockInstance, key, /*callback);,*/ function(err, oldValue) {
78 if (err) {
79 return mockInstance._callCallback(callback, err, null);
80 }
81
82 mockInstance.storage[key] = createItem(value);
83
84 mockInstance._callCallback(callback, err, oldValue);
85 });
86};
87
88/**
89 * mget
90 */
91exports.mget = function (mockInstance) {
92
93 var keys = [];
94 var err = null;
95
96 // Build up the set of keys
97 if ('object' == typeof arguments[1]) {
98 keys = arguments[1];
99 } else {
100 for (var i = 1; i < arguments.length; i++) {
101 var key = arguments[i];
102 if ('function' !== typeof key) {
103 keys.push(key);
104 }
105 }
106 }
107
108 var values = [];
109 for (var j = 0; j < keys.length; j++) {
110 exports.get(mockInstance, keys[j], function(e, value) {
111 if (e) {
112 err = e;
113 } else {
114 values.push(value);
115 }
116 });
117 }
118
119 if ('function' === typeof arguments[arguments.length - 1]) {
120 mockInstance._callCallback(arguments[arguments.length - 1], err, values);
121 }
122
123}
124
125/**
126 * mset
127 */
128exports.mset = function (mockInstance, useNX) { // eslint-disable-line complexity
129
130 var keys = [];
131 var values = [];
132 var err = null;
133 var callback;
134 var numCallbacks;
135
136 if ('object' === typeof arguments[2]) {
137 if ((arguments[2].length & 1) === 1) { // eslint-disable-line no-bitwise
138 err = {
139 command: useNX ? "MSETNX" : "MSET",
140 args: arguments[1],
141 code: "ERR"
142 };
143 } else {
144 for (var i = 0; i < arguments[2].length; i++) {
145 if (i % 2 == 0) {
146 keys.push(arguments[2][i]);
147 } else {
148 values.push(arguments[2][i]);
149 }
150 }
151 }
152 callback = arguments[3]
153 } else {
154 var args = [];
155 var last;
156 for ( i = 2; i < arguments.length; i++) {
157 last = args[i - 2] = arguments[i];
158 }
159 if ('function' === typeof last) {
160 callback = args.pop();
161 }
162 if ((args.length & 1) === 1) { // eslint-disable-line no-bitwise
163 err = {
164 command: useNX ? "MSETNX" : "MSET",
165 args: args,
166 code: "ERR"
167 };
168 } else {
169 while (args.length !== 0) {
170 keys.push(args.shift())
171 values.push(args.shift())
172 }
173 }
174 }
175
176 numCallbacks = keys.length;
177 if (numCallbacks == 0) {
178 err = err || {
179 command: useNX ? "MSETNX" : "MSET",
180 code: "ERR"
181 };
182 mockInstance._callCallback(callback, err);
183 } else {
184 if (useNX) {
185 var allClear = true;
186 for (i = 0; i < keys.length; i++) {
187 if (keys[i] in mockInstance.storage) {
188 allClear = false;
189 break;
190 }
191 }
192 if (!allClear) {
193 mockInstance._callCallback(callback, null, 0);
194 return
195 }
196 }
197 for (i = 0; i < keys.length; i++) {
198 exports.set(mockInstance, keys[i], values[i], function(cberr) {
199 if (cberr) {
200 err = cberr;
201 }
202 if (--numCallbacks == 0) {
203 var response = useNX ? 1 : "OK";
204 mockInstance._callCallback(callback, err, err ? undefined : response);
205 }
206 });
207 }
208 }
209}
210
211/**
212 * Incr
213 */
214exports.incr = function (mockInstance, key, callback) {
215
216 function _isInteger(s) {
217 return parseInt(s, 10) == s;
218 }
219
220 if (!mockInstance.storage[key]) {
221 var number = 0 + 1;
222 exports.set(mockInstance, key, number);
223 mockInstance._callCallback(callback, null, number);
224
225 } else if (mockInstance.storage[key].type !== "string") {
226 var err = new Error("WRONGTYPE Operation against a key holding the wrong kind of value");
227 mockInstance._callCallback(callback, err, null);
228
229 } else if (_isInteger(mockInstance.storage[key].value)) {
230 number = parseInt(mockInstance.storage[key].value, 10) + 1;
231 mockInstance.storage[key].value = number.toString();
232 mockInstance._callCallback(callback, null, number);
233
234 } else {
235 err = new Error("ERR value is not an integer or out of range");
236 mockInstance._callCallback(callback, err, null);
237 }
238}
239
240/**
241 * Incrby
242 */
243exports.incrby = function (mockInstance, key, value, callback) {
244
245 function _isInteger(s) {
246 return parseInt(s, 10) == s;
247 }
248
249 value = parseInt(value);
250
251 if (!mockInstance.storage[key]) {
252 var number = 0 + value;
253 exports.set(mockInstance, key, number);
254 mockInstance._callCallback(callback, null, number);
255
256 } else if (mockInstance.storage[key].type !== "string") {
257 var err = new Error("WRONGTYPE Operation against a key holding the wrong kind of value");
258 mockInstance._callCallback(callback, err, null);
259
260 } else if (_isInteger(mockInstance.storage[key].value)) {
261 number = parseInt(mockInstance.storage[key].value, 10) + value;
262 mockInstance.storage[key].value = number.toString();
263 mockInstance._callCallback(callback, null, number);
264
265 } else {
266 err = new Error("ERR value is not an integer or out of range");
267 mockInstance._callCallback(callback, err, null);
268 }
269}
270
271/**
272 * Incrbyfloat
273 */
274exports.incrbyfloat = function (mockInstance, key, value, callback) {
275
276 function _isFloat(s) {
277 return parseFloat(s, 10) == s;
278 }
279
280 if (!mockInstance.storage[key]) {
281 var number = 0 + parseFloat(value, 10);
282 exports.set(mockInstance, key, number.toString());
283 mockInstance._callCallback(callback, null, number.toString());
284
285 } else if (mockInstance.storage[key].type !== "string") {
286 var err = new Error("WRONGTYPE Operation against a key holding the wrong kind of value");
287 mockInstance._callCallback(callback, err, null);
288
289 } else if (_isFloat(mockInstance.storage[key].value) && _isFloat(value)) {
290 number = parseFloat(mockInstance.storage[key].value, 10) + parseFloat(value, 10);
291 mockInstance.storage[key].value = number.toString();
292 mockInstance._callCallback(callback, null, number.toString());
293
294 } else {
295 err = new Error("ERR value is not a valid float");
296 mockInstance._callCallback(callback, err, null);
297 }
298}
299
300/**
301 * Decr
302 */
303exports.decr = function (mockInstance, key, callback) {
304
305 function _isInteger(s) {
306 return parseInt(s, 10) == s;
307 }
308
309 if (!mockInstance.storage[key]) {
310 var number = 0 - 1;
311 exports.set(mockInstance, key, number);
312 mockInstance._callCallback(callback, null, number);
313
314 } else if (mockInstance.storage[key].type !== "string") {
315 var err = new Error("WRONGTYPE Operation against a key holding the wrong kind of value");
316 mockInstance._callCallback(callback, err, null);
317
318 } else if (_isInteger(mockInstance.storage[key].value)) {
319 number = parseInt(mockInstance.storage[key].value, 10) - 1;
320 mockInstance.storage[key].value = number.toString();
321 mockInstance._callCallback(callback, null, number);
322
323 } else {
324 err = new Error("ERR value is not an integer or out of range");
325 mockInstance._callCallback(callback, err, null);
326 }
327}
328
329/**
330 * Decrby
331 */
332exports.decrby = function (mockInstance, key, value, callback) {
333
334 function _isInteger(s) {
335 return parseInt(s, 10) == s;
336 }
337
338 value = parseInt(value);
339
340 if (!mockInstance.storage[key]) {
341 var number = 0 - value;
342 exports.set(mockInstance, key, number);
343 mockInstance._callCallback(callback, null, number);
344
345 } else if (mockInstance.storage[key].type !== "string") {
346 var err = new Error("WRONGTYPE Operation against a key holding the wrong kind of value");
347 mockInstance._callCallback(callback, err, null);
348
349 } else if (_isInteger(mockInstance.storage[key].value)) {
350 number = parseInt(mockInstance.storage[key].value, 10) - value;
351 mockInstance.storage[key].value = number.toString();
352 mockInstance._callCallback(callback, null, number);
353
354 } else {
355 err = new Error("ERR value is not an integer or out of range");
356 mockInstance._callCallback(callback, err, null);
357 }
358}