forked from 3rd-Eden/memcached
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
164 lines (135 loc) · 4.46 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"use strict";
var createHash = require('crypto').createHash
, toString = Object.prototype.toString;
exports.validateArg = function validateArg (args, config) {
var err;
args.validate.forEach(function (tokens) {
var key = tokens[0]
, value = args[key];
switch(tokens[1]){
case Number:
if (toString.call(value) !== '[object Number]') {
err = 'Argument "' + key + '" is not a valid Number.';
}
break;
case Boolean:
if (toString.call(value) !== '[object Boolean]') {
err = 'Argument "' + key + '" is not a valid Boolean.';
}
break;
case Array:
if (toString.call(value) !== '[object Array]') {
err = 'Argument "' + key + '" is not a valid Array.';
}
if (!err && key === 'key') {
for (var vKey=0; vKey<value.length; vKey++) {
var vValue = value[vKey];
var result = validateKeySize(config, vKey, vValue);
if (result.err) {
err = result.err;
} else {
args.command = args.command.replace(vValue, result['value']);
}
}
}
break;
case Object:
if (toString.call(value) !== '[object Object]') {
err = 'Argument "' + key + '" is not a valid Object.';
}
break;
case Function:
if (toString.call(value) !== '[object Function]') {
err = 'Argument "' + key + '" is not a valid Function.';
}
break;
case String:
if (toString.call(value) !== '[object String]') {
err = 'Argument "' + key + '" is not a valid String.';
}
if (!err && key === 'key') {
var result = validateKeySize(config, key, value);
if (result.err) {
err = result.err;
} else {
args.command = reallocString(args.command).replace(value, result['value']);
}
}
break;
default:
if (toString.call(value) === '[object global]' && !tokens[2]) {
err = 'Argument "' + key + '" is not defined.';
}
}
});
if (err){
if (args.callback) args.callback(new Error(err));
return false;
}
return true;
};
var validateKeySize = function validateKeySize(config, key, value) {
if (value.length > config.maxKeySize) {
if (config.keyCompression){
return { err: false, value: createHash('md5').update(value).digest('hex') };
} else {
return { err: 'Argument "' + key + '" is longer than the maximum allowed length of ' + config.maxKeySize };
}
} else if (/[\s\n\r]/.test(value)) {
return { err: 'The key should not contain any whitespace or new lines' };
} else {
return { err: false, value: value };
}
};
// a small util to use an object for eventEmitter
exports.fuse = function fuse (target, handlers) {
for (var i in handlers)
if (handlers.hasOwnProperty(i)){
target.on(i, handlers[i]);
}
};
// merges a object's proppertys / values with a other object
exports.merge = function merge (target, obj) {
for (var i in obj) {
target[i] = obj[i];
}
return target;
};
// curry/bind functions
exports.curry = function curry (context, fn) {
var copy = Array.prototype.slice
, args = copy.call(arguments, 2);
return function bowlofcurry () {
return fn.apply(context || this, args.concat(copy.call(arguments)));
};
};
// a small items iterator
exports.Iterator = function iterator (collection, callback) {
var arr = Array.isArray(collection)
, keys = !arr ? Object.keys(collection) : false
, index = 0
, maximum = arr ? collection.length : keys.length
, self = this;
// returns next item
this.next = function next () {
var obj = arr ? collection[index] : { key: keys[index], value: collection[keys[index]] };
callback(obj, index++, collection, self);
};
// check if we have more items
this.hasNext = function hasNext () {
return index < maximum;
};
};
//Escapes values by putting backslashes before line breaks
exports.escapeValue = function(value) {
return value.replace(/(\r|\n)/g, '\\$1');
};
//Unescapes escaped values by removing backslashes before line breaks
exports.unescapeValue = function(value) {
return value.replace(/\\(\r|\n)/g, '$1');
};
var reallocString = exports.reallocString = function(value) {
// Reallocate string to fix slow string operations in node 0.10
// see https://code.google.com/p/v8/issues/detail?id=2869 for details
return (' ' + value).substr(1);
};