2* @description MeshCentral Common Library
3* @author Ylian Saint-Hilaire
4* @copyright Intel Corporation 2018-2022
10/*xjslint plusplus: true */
11/*xjslint maxlen: 256 */
13/*jshint strict: false */
14/*jshint esversion: 6 */
17const fs = require('fs');
18const crypto = require('crypto');
19const path = require('path');
21// Binary encoding and decoding functions
22module.exports.ReadShort = function (v, p) { return (v.charCodeAt(p) << 8) + v.charCodeAt(p + 1); };
23module.exports.ReadShortX = function (v, p) { return (v.charCodeAt(p + 1) << 8) + v.charCodeAt(p); };
24module.exports.ReadInt = function (v, p) { return (v.charCodeAt(p) * 0x1000000) + (v.charCodeAt(p + 1) << 16) + (v.charCodeAt(p + 2) << 8) + v.charCodeAt(p + 3); }; // We use "*0x1000000" instead of "<<24" because the shift converts the number to signed int32.
25module.exports.ReadIntX = function (v, p) { return (v.charCodeAt(p + 3) * 0x1000000) + (v.charCodeAt(p + 2) << 16) + (v.charCodeAt(p + 1) << 8) + v.charCodeAt(p); };
26module.exports.ShortToStr = function (v) { return String.fromCharCode((v >> 8) & 0xFF, v & 0xFF); };
27module.exports.ShortToStrX = function (v) { return String.fromCharCode(v & 0xFF, (v >> 8) & 0xFF); };
28module.exports.IntToStr = function (v) { return String.fromCharCode((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF); };
29module.exports.IntToStrX = function (v) { return String.fromCharCode(v & 0xFF, (v >> 8) & 0xFF, (v >> 16) & 0xFF, (v >> 24) & 0xFF); };
30module.exports.MakeToArray = function (v) { if (!v || v == null || typeof v == 'object') return v; return [v]; };
31module.exports.SplitArray = function (v) { return v.split(','); };
32module.exports.Clone = function (v) { return JSON.parse(JSON.stringify(v)); };
33module.exports.IsFilenameValid = (function () { var x1 = /^[^\\/:\*\?"<>\|]+$/, x2 = /^\./, x3 = /^(nul|prn|con|lpt[0-9]|com[0-9])(\.|$)/i; return function isFilenameValid(fname) { return module.exports.validateString(fname, 1, 4096) && x1.test(fname) && !x2.test(fname) && !x3.test(fname) && (fname[0] != '.'); }; })();
34module.exports.makeFilename = function (v) { return v.split('\\').join('').split('/').join('').split(':').join('').split('*').join('').split('?').join('').split('"').join('').split('<').join('').split('>').join('').split('|').join('').split(' ').join('').split('\'').join(''); }
35module.exports.joinPath = function (base, path_) { return path.isAbsolute(path_) ? path_ : path.join(base, path_); }
37// Move an element from one position in an array to a new position
38module.exports.ArrayElementMove = function(arr, from, to) { arr.splice(to, 0, arr.splice(from, 1)[0]); };
40// Format a string with arguments, "replaces {0} and {1}..."
41module.exports.format = function (format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function (match, number) { return typeof args[number] != 'undefined' ? args[number] : match; }); };
43// Print object for HTML
44module.exports.ObjectToStringEx = function (x, c) {
46 if (x != 0 && (!x || x == null)) return "(Null)";
47 if (x instanceof Array) { for (i in x) { r += '<br />' + gap(c) + "Item #" + i + ": " + module.exports.ObjectToStringEx(x[i], c + 1); } }
48 else if (x instanceof Object) { for (i in x) { r += '<br />' + gap(c) + i + " = " + module.exports.ObjectToStringEx(x[i], c + 1); } }
53// Print object for console
54module.exports.ObjectToStringEx2 = function (x, c) {
56 if (x != 0 && (!x || x == null)) return "(Null)";
57 if (x instanceof Array) { for (i in x) { r += '\r\n' + gap2(c) + "Item #" + i + ": " + module.exports.ObjectToStringEx2(x[i], c + 1); } }
58 else if (x instanceof Object) { for (i in x) { r += '\r\n' + gap2(c) + i + " = " + module.exports.ObjectToStringEx2(x[i], c + 1); } }
64module.exports.gap = function (c) { var x = ''; for (var i = 0; i < (c * 4); i++) { x += ' '; } return x; };
65module.exports.gap2 = function (c) { var x = ''; for (var i = 0; i < (c * 4); i++) { x += ' '; } return x; };
67// Print an object in html
68module.exports.ObjectToString = function (x) { return module.exports.ObjectToStringEx(x, 0); };
69module.exports.ObjectToString2 = function (x) { return module.exports.ObjectToStringEx2(x, 0); };
71// Convert a hex string to a raw string
72module.exports.hex2rstr = function (d) {
73 var r = '', m = ('' + d).match(/../g), t;
74 while (t = m.shift()) { r += String.fromCharCode('0x' + t); }
78// Convert decimal to hex
79module.exports.char2hex = function (i) { return (i + 0x100).toString(16).substr(-2).toUpperCase(); };
81// Convert a raw string to a hex string
82module.exports.rstr2hex = function (input) {
84 for (i = 0; i < input.length; i++) { r += module.exports.char2hex(input.charCodeAt(i)); }
88// UTF-8 encoding & decoding functions
89module.exports.encode_utf8 = function (s) { return unescape(encodeURIComponent(s)); };
90module.exports.decode_utf8 = function (s) { return decodeURIComponent(escape(s)); };
92// Convert a string into a blob
93module.exports.data2blob = function (data) {
94 var bytes = new Array(data.length);
95 for (var i = 0; i < data.length; i++) bytes[i] = data.charCodeAt(i);
96 var blob = new Blob([new Uint8Array(bytes)]);
100// Generate random numbers between 0 and max without bias.
101module.exports.random = function (max) {
102 const crypto = require('crypto');
104 while (maxmask < max) { maxmask = (maxmask << 1) + 1; }
105 do { r = (crypto.randomBytes(4).readUInt32BE(0) & maxmask); } while (r > max);
109// Split a comma separated string, ignoring commas in quotes.
110module.exports.quoteSplit = function (str) {
111 var tmp = '', quote = 0, result = [];
112 for (var i in str) { if (str[i] == '"') { quote = (quote + 1) % 2; } if ((str[i] == ',') && (quote == 0)) { tmp = tmp.trim(); result.push(tmp); tmp = ''; } else { tmp += str[i]; } }
113 if (tmp.length > 0) result.push(tmp.trim());
117// Convert list of "name = value" into object
118module.exports.parseNameValueList = function (list) {
120 for (var i in list) {
121 var j = list[i].indexOf('=');
123 var v = list[i].substring(j + 1).trim();
124 if ((v[0] == '"') && (v[v.length - 1] == '"')) { v = v.substring(1, v.length - 1); }
125 result[list[i].substring(0, j).trim()] = v;
131// Compute the MD5 digest hash for a set of values
132module.exports.ComputeDigesthash = function (username, password, realm, method, path, qop, nonce, nc, cnonce) {
133 var ha1 = crypto.createHash('md5').update(username + ":" + realm + ":" + password).digest('hex');
134 var ha2 = crypto.createHash('md5').update(method + ":" + path).digest('hex');
135 return crypto.createHash('md5').update(ha1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" + ha2).digest("hex");
138module.exports.toNumber = function (str) { var x = parseInt(str); if (x == str) return x; return str; };
139module.exports.escapeHtml = function (string) { return String(string).replace(/[&<>"'`=\/]/g, function (s) { return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '/': '/', '`': '`', '=': '=' }[s]; }); };
140module.exports.escapeHtmlBreaks = function (string) { return String(string).replace(/[&<>"'`=\/]/g, function (s) { return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '/': '/', '`': '`', '=': '=', '\r': '<br />', '\n': '' }[s]; }); };
141module.exports.zeroPad = function(num, c) { if (c == null) { c = 2; } var s = '000000' + num; return s.substr(s.length - c); }
143// Lowercase all the names in a object recursively
144// Allow for exception keys, child of exceptions will not get lower-cased.
145// Exceptions is an array of "keyname" or "parent\keyname"
146module.exports.objKeysToLower = function (obj, exceptions, parent) {
148 if ((typeof obj[i] == 'object') &&
149 ((exceptions == null) || (exceptions.indexOf(i.toLowerCase()) == -1) && ((parent == null) || (exceptions.indexOf(parent.toLowerCase() + '/' + i.toLowerCase()) == -1)))
151 module.exports.objKeysToLower(obj[i], exceptions, i); // LowerCase all key names in the child object
153 if (i.toLowerCase() !== i) { obj[i.toLowerCase()] = obj[i]; delete obj[i]; } // LowerCase all key names
158// Escape and unescape field names so there are no invalid characters for MongoDB/NeDB ("$", ",", ".", see https://github.com/seald/nedb/tree/master?tab=readme-ov-file#inserting-documents)
159module.exports.escapeFieldName = function (name) { if ((name.indexOf(',') == -1) && (name.indexOf('%') == -1) && (name.indexOf('.') == -1) && (name.indexOf('$') == -1)) return name; return name.split('%').join('%25').split('.').join('%2E').split('$').join('%24').split(',').join('%2C'); };
160module.exports.unEscapeFieldName = function (name) { if (name.indexOf('%') == -1) return name; return name.split('%2C').join(',').split('%2E').join('.').split('%24').join('$').split('%25').join('%'); };
162// Escape all links, SSH and RDP usernames
163// This is required for databases like NeDB that don't accept "." or "," as part of a field name.
164module.exports.escapeLinksFieldNameEx = function (docx) { if ((docx.links == null) && (docx.ssh == null) && (docx.rdp == null)) { return docx; } return module.exports.escapeLinksFieldName(docx); };
165module.exports.escapeLinksFieldName = function (docx) {
166 var doc = Object.assign({}, docx);
167 if (doc.links != null) { doc.links = Object.assign({}, doc.links); for (var i in doc.links) { var ue = module.exports.escapeFieldName(i); if (ue !== i) { doc.links[ue] = doc.links[i]; delete doc.links[i]; } } }
168 if (doc.ssh != null) { doc.ssh = Object.assign({}, doc.ssh); for (var i in doc.ssh) { var ue = module.exports.escapeFieldName(i); if (ue !== i) { doc.ssh[ue] = doc.ssh[i]; delete doc.ssh[i]; } } }
169 if (doc.rdp != null) { doc.rdp = Object.assign({}, doc.rdp); for (var i in doc.rdp) { var ue = module.exports.escapeFieldName(i); if (ue !== i) { doc.rdp[ue] = doc.rdp[i]; delete doc.rdp[i]; } } }
172module.exports.unEscapeLinksFieldName = function (doc) {
173 if (doc.links != null) { for (var j in doc.links) { var ue = module.exports.unEscapeFieldName(j); if (ue !== j) { doc.links[ue] = doc.links[j]; delete doc.links[j]; } } }
174 if (doc.ssh != null) { for (var j in doc.ssh) { var ue = module.exports.unEscapeFieldName(j); if (ue !== j) { doc.ssh[ue] = doc.ssh[j]; delete doc.ssh[j]; } } }
175 if (doc.rdp != null) { for (var j in doc.rdp) { var ue = module.exports.unEscapeFieldName(j); if (ue !== j) { doc.rdp[ue] = doc.rdp[j]; delete doc.rdp[j]; } } }
178//module.exports.escapeAllLinksFieldName = function (docs) { for (var i in docs) { module.exports.escapeLinksFieldName(docs[i]); } return docs; };
179module.exports.unEscapeAllLinksFieldName = function (docs) { for (var i in docs) { docs[i] = module.exports.unEscapeLinksFieldName(docs[i]); } return docs; };
181// Escape field names for aceBase
182var aceEscFields = ['links', 'ssh', 'rdp', 'notify'];
183module.exports.aceEscapeFieldNames = function (docx) { var doc = Object.assign({}, docx); for (var k in aceEscFields) { if (typeof doc[aceEscFields[k]] == 'object') { doc[aceEscFields[k]] = Object.assign({}, doc[aceEscFields[k]]); for (var i in doc[aceEscFields[k]]) { var ue = encodeURIComponent(i); if (ue !== i) { doc[aceEscFields[k]][ue] = doc[aceEscFields[k]][i]; delete doc[aceEscFields[k]][i]; } } } } return doc; };
184module.exports.aceUnEscapeFieldNames = function (doc) { for (var k in aceEscFields) { if (typeof doc[aceEscFields[k]] == 'object') { for (var j in doc[aceEscFields[k]]) { var ue = decodeURIComponent(j); if (ue !== j) { doc[aceEscFields[k]][ue] = doc[aceEscFields[k]][j]; delete doc[aceEscFields[k]][j]; } } } } return doc; };
185module.exports.aceUnEscapeAllFieldNames = function (docs) { for (var i in docs) { docs[i] = module.exports.aceUnEscapeFieldNames(docs[i]); } return docs; };
188module.exports.validateString = function (str, minlen, maxlen) { return ((str != null) && (typeof str == 'string') && ((minlen == null) || (str.length >= minlen)) && ((maxlen == null) || (str.length <= maxlen))); };
189module.exports.validateInt = function (int, minval, maxval) { return ((int != null) && (typeof int == 'number') && ((minval == null) || (int >= minval)) && ((maxval == null) || (int <= maxval))); };
190module.exports.validateArray = function (array, minlen, maxlen) { return ((array != null) && Array.isArray(array) && ((minlen == null) || (array.length >= minlen)) && ((maxlen == null) || (array.length <= maxlen))); };
191module.exports.validateStrArray = function (array, minlen, maxlen) { if (((array != null) && Array.isArray(array)) == false) return false; for (var i in array) { if ( (typeof array[i] != 'string') || ((minlen != null) && (array[i].length < minlen)) || ((maxlen != null) && (array[i].length > maxlen))) return false; } return true; };
192module.exports.validateObject = function (obj) { return ((obj != null) && (typeof obj == 'object')); };
193module.exports.validateEmail = function (email, minlen, maxlen) { if (module.exports.validateString(email, minlen, maxlen) == false) return false; var emailReg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return emailReg.test(email); };
194module.exports.validateUsername = function (username, minlen, maxlen) { return (module.exports.validateString(username, minlen, maxlen) && (username.indexOf(' ') == -1) && (username.indexOf('"') == -1) && (username.indexOf(',') == -1)); };
195module.exports.isAlphaNumeric = function (str) { return (str.match(/^[A-Za-z0-9]+$/) != null); };
196module.exports.validateAlphaNumericArray = function (array, minlen, maxlen) { if (((array != null) && Array.isArray(array)) == false) return false; for (var i in array) { if ((typeof array[i] != 'string') || (module.exports.isAlphaNumeric(array[i]) == false) || ((minlen != null) && (array[i].length < minlen)) || ((maxlen != null) && (array[i].length > maxlen)) ) return false; } return true; };
197module.exports.getEmailDomain = function(email) {
198 if (!module.exports.validateEmail(email, 1, 1024)) {
201 const i = email.indexOf('@');
202 return email.substring(i + 1).toLowerCase();
205module.exports.validateEmailDomain = function(email, allowedDomains) {
206 // Check if this request is for an allows email domain
207 if ((allowedDomains != null) && Array.isArray(allowedDomains)) {
208 const emaildomain = module.exports.getEmailDomain(email);
209 if (emaildomain === '') {
213 for (var i in allowedDomains) { if (emaildomain == allowedDomains[i].toLowerCase()) { emailok = true; } }
219// Check password requirements
220module.exports.checkPasswordRequirements = function(password, requirements) {
221 if ((requirements == null) || (requirements == '') || (typeof requirements != 'object')) return true;
222 if (requirements.min) { if (password.length < requirements.min) return false; }
223 if (requirements.max) { if (password.length > requirements.max) return false; }
224 var numeric = 0, lower = 0, upper = 0, nonalpha = 0;
225 for (var i = 0; i < password.length; i++) {
226 if (/\d/.test(password[i])) { numeric++; }
227 if (/[a-z]/.test(password[i])) { lower++; }
228 if (/[A-Z]/.test(password[i])) { upper++; }
229 if (/\W/.test(password[i])) { nonalpha++; }
231 if (requirements.numeric && (numeric < requirements.numeric)) return false;
232 if (requirements.lower && (lower < requirements.lower)) return false;
233 if (requirements.upper && (upper < requirements.upper)) return false;
234 if (requirements.nonalpha && (nonalpha < requirements.nonalpha)) return false;
239// Limits the number of tasks running to a fixed limit placing the rest in a pending queue.
240// This is useful to limit the number of agents upgrading at the same time, to not swamp
241// the network with traffic.
242module.exports.createTaskLimiterQueue = function (maxTasks, maxTaskTime, cleaningInterval) {
243 var obj = { maxTasks: maxTasks, maxTaskTime: (maxTaskTime * 1000), nextTaskId: 0, currentCount: 0, current: {}, pending: [[], [], []], timer: null };
245 // Add a task to the super queue
246 // Priority: 0 = High, 1 = Medium, 2 = Low
247 obj.launch = function (func, arg, pri) {
248 if (typeof pri != 'number') { pri = 2; }
249 if (obj.currentCount < obj.maxTasks) {
251 const id = obj.nextTaskId++;
252 obj.current[id] = Date.now() + obj.maxTaskTime;
254 //console.log('ImmidiateLaunch ' + id);
255 func(arg, id, obj); // Start the task
256 if (obj.timer == null) { obj.timer = setInterval(obj.clean, cleaningInterval * 1000); }
259 //console.log('Holding');
260 obj.pending[pri].push({ func: func, arg: arg });
264 // Called when a task is completed
265 obj.completed = function (taskid) {
266 //console.log('Completed ' + taskid);
267 if (obj.current[taskid]) { delete obj.current[taskid]; obj.currentCount--; } else { return; }
268 while ((obj.currentCount < obj.maxTasks) && ((obj.pending[0].length > 0) || (obj.pending[1].length > 0) || (obj.pending[2].length > 0))) {
271 if (obj.pending[0].length > 0) { t = obj.pending[0].shift(); }
272 else if (obj.pending[1].length > 0) { t = obj.pending[1].shift(); }
273 else if (obj.pending[2].length > 0) { t = obj.pending[2].shift(); }
274 const id = obj.nextTaskId++;
275 obj.current[id] = Date.now() + obj.maxTaskTime;
277 //console.log('PendingLaunch ' + id);
278 t.func(t.arg, id, obj); // Start the task
280 if ((obj.currentCount == 0) && (obj.pending[0].length == 0) && (obj.pending[1].length == 0) && (obj.pending[2].length == 0) && (obj.timer != null)) {
281 // All done, clear the timer
282 clearInterval(obj.timer); obj.timer = null;
286 // Look for long standing tasks and clean them up
287 obj.clean = function () {
288 const t = Date.now();
289 for (var i in obj.current) { if (obj.current[i] < t) { obj.completed(parseInt(i)); } }
295// Convert string translations to a standardized JSON we can use in GitHub
296// Strings are sorder by english source and object keys are sorted
297module.exports.translationsToJson = function(t) {
298 var arr2 = [], arr = t.strings;
300 var names = [], el = arr[i], el2 = {};
301 for (var j in el) { names.push(j); }
302 names.sort(function (a, b) { if (a == b) { return 0; } if (a == 'xloc') { return 1; } if (b == 'xloc') { return -1; } return a - b });
303 for (var j in names) { el2[names[j]] = el[names[j]]; }
304 if (el2.xloc != null) { el2.xloc.sort(); }
307 arr2.sort(function (a, b) { if (a.en > b.en) return 1; if (a.en < b.en) return -1; return 0; });
308 return JSON.stringify({ strings: arr2 }, null, ' ');
311module.exports.copyFile = function(source, target, cb) {
312 var cbCalled = false, rd = fs.createReadStream(source);
313 rd.on('error', function (err) { done(err); });
314 var wr = fs.createWriteStream(target);
315 wr.on('error', function (err) { done(err); });
316 wr.on('close', function (ex) { done(); });
318 function done(err) { if (!cbCalled) { cb(err); cbCalled = true; } }
321module.exports.meshServerRightsArrayToNumber = function (val) {
322 if (val == null) return null;
323 if (typeof val == 'number') return val;
324 if (Array.isArray(val)) {
325 var newAccRights = 0;
327 var r = val[j].toLowerCase();
328 if (r == 'fulladmin') { newAccRights = 4294967295; } // 0xFFFFFFFF
329 if (r == 'serverbackup') { newAccRights |= 1; }
330 if (r == 'manageusers') { newAccRights |= 2; }
331 if (r == 'serverrestore') { newAccRights |= 4; }
332 if (r == 'fileaccess') { newAccRights |= 8; }
333 if (r == 'serverupdate') { newAccRights |= 16; }
334 if (r == 'locked') { newAccRights |= 32; }
335 if (r == 'nonewgroups') { newAccRights |= 64; }
336 if (r == 'notools') { newAccRights |= 128; }
337 if (r == 'usergroups') { newAccRights |= 256; }
338 if (r == 'recordings') { newAccRights |= 512; }
339 if (r == 'locksettings') { newAccRights |= 1024; }
340 if (r == 'allevents') { newAccRights |= 2048; }
341 if (r == 'nonewdevices') { newAccRights |= 4096; }
348// Sort an object by key
349module.exports.sortObj = function (obj) { return Object.keys(obj).sort().reduce(function (result, key) { result[key] = obj[key]; return result; }, {}); }
351// Validate an object to make sure it can be stored in MongoDB
352module.exports.validateObjectForMongo = function (obj, maxStrLen) {
353 return validateObjectForMongoRec(obj, maxStrLen);
356function validateObjectForMongoRec(obj, maxStrLen) {
357 if (typeof obj != 'object') return false;
359 // Check the key name is not too long
360 if (i.length > 100) return false;
361 // Check if all chars are alpha-numeric or underscore.
362 for (var j in i) { const c = i.charCodeAt(j); if ((c < 48) || ((c > 57) && (c < 65)) || ((c > 90) && (c < 97) && (c != 95)) || (c > 122)) return false; }
363 // If the value is a string, check it's not too long
364 if ((typeof obj[i] == 'string') && (obj[i].length > maxStrLen)) return false;
365 // If the value is an object, check it.
366 if ((typeof obj[i] == 'object') && (Array.isArray(obj[i]) == false) && (validateObjectForMongoRec(obj[i], maxStrLen) == false)) return false;
371// Parse a version string of the type n.n.n.n
372module.exports.parseVersion = function (verstr) {
373 if (typeof verstr != 'string') return null;
374 const r = [], verstrsplit = verstr.split('.');
375 if (verstrsplit.length != 4) return null;
376 for (var i in verstrsplit) {
377 var n = parseInt(verstrsplit[i]);
378 if (isNaN(n) || (n < 0) || (n > 65535)) return null;
384// Move old files. If we are about to overwrite a file, we can move if first just in case the change needs to be reverted
385module.exports.moveOldFiles = function (filelist) {
386 // Fine an old extension that works for all files in the file list
387 var oldFileExt, oldFileExtCount = 0, extOk;
390 if (++oldFileExtCount == 1) { oldFileExt = '-old'; } else { oldFileExt = '-old' + oldFileExtCount; }
391 for (var i in filelist) { if (fs.existsSync(filelist[i] + oldFileExt) == true) { extOk = false; } }
392 } while (extOk == false);
393 for (var i in filelist) { try { fs.renameSync(filelist[i], filelist[i] + oldFileExt); } catch (ex) { } }
396// Convert strArray to Array, returns array if strArray or null if any other type
397module.exports.convertStrArray = function (object, split) {
398 if (split && typeof object === 'string') {
399 return object.split(split)
400 } else if (typeof object === 'string') {
401 return Array(object);
402 } else if (Array.isArray(object)) {
409module.exports.uniqueArray = function (a) {
414 for(var i = 0; i < len; i++) {
416 if(seen[item] !== 1) {
424// Replace placeholders in a string with values from an object or a function
425module.exports.replacePlaceholders = function (template, values) {
426 return template.replace(/\{(\w+)\}/g, (match, key) => {
427 if (typeof values === 'function') {
430 else if (values && typeof values === 'object') {
431 return values[key] !== undefined ? values[key] : match;
434 return values !== undefined ? values : match;