deep-framework
Version:
175 lines (140 loc) • 5.01 kB
JavaScript
/**
* @namespace Utils
*/
var _ = require('underscore');
var cls = require('continuation-local-storage');
var cls_mode = false;
var NAMESPACE ='AWSXRay';
var SEGMENT = 'segment';
var Utils = {
/**
* @memberof Utils
* @type {object}
* @namespace Utils.CLSUtils
*/
CLSUtils: {
getNamespace: function getNamespace() {
return cls.getNamespace(NAMESPACE);
},
/**
* Returns the current segment or subsegment. Available in CLS mode only.
* @memberof Utils.CLSUtils
* @method getSegment
* @returns {Segment|Subsegment}
*/
getSegment: function getSegment() {
if (cls_mode)
return cls.getNamespace(NAMESPACE).get(SEGMENT);
},
setSegment: function setSegment(segment) {
if (cls_mode)
return cls.getNamespace(NAMESPACE).set(SEGMENT, segment);
},
isCLSMode: function isCLSMode() {
return cls_mode;
},
/**
* Enables CLS mode. CLS mode uses 'continuation-local-storage'.
* @see https://github.com/othiym23/node-continuation-local-storage
* @memberof Utils.CLSUtils
* @function
*/
enableCLSMode: function enableCLSMode() {
cls_mode = true;
cls.createNamespace(NAMESPACE);
},
/**
* Disables CLS mode. Current segment or subsegment must be passed manually
* via the parent optional on capture, captureAsync etc.
* @memberof Utils.CLSUtils
* @function
*/
enableManualMode: function enableManualMode() {
cls_mode = false;
if (cls.getNamespace(NAMESPACE))
cls.destroyNamespace(NAMESPACE);
}
},
getCauseTypeFromHttpStatus: function getCauseTypeFromHttpStatus(status) {
var stat = status.toString();
if (!_.isNull(stat.match(/^[4][0-9]{2}$/)))
return 'error';
else if (!_.isNull(stat.match(/^[5][0-9]{2}$/)))
return 'fault';
},
/**
* Performs a case-insensitive wildcard match against two strings. This method works with pseduo-regex chars; specifically ? and * are supported.
* An asterisk (*) represents any combination of characters
* A question mark (?) represents any single character
*
* @param pattern - the regex-like pattern to be compared against.
* @param text - the string to compare against the pattern.
* @returns boolean
*/
wildcardMatch: function wildcardMatch(pattern, text) {
if (_.isUndefined(pattern) || _.isUndefined(text))
return false;
if (pattern.length === 1 && pattern.charAt(0) === '*')
return true;
var patternLength = pattern.length;
var textLength = text.length;
var indexOfGlob = pattern.indexOf('*');
pattern = pattern.toLowerCase();
text = text.toLowerCase();
// Infix globs are relatively rare, and the below search is expensive especially when
// Balsa is used a lot. Check for infix globs and, in their absence, do the simple thing
if (indexOfGlob === -1 || indexOfGlob === (patternLength - 1)) {
var match = function simpleWildcardMatch() {
var j = 0;
for(var i = 0; i < patternLength; i++) {
var patternChar = pattern.charAt(i);
if(patternChar === '*') {
// Presumption for this method is that globs only occur at end
return true;
} else if (patternChar === '?') {
if(j === textLength)
return false; // No character to match
j++;
} else {
if (j >= textLength || patternChar != text.charAt(j))
return false;
j++;
}
}
// Ate up all the pattern and didn't end at a glob, so a match will have consumed all
// the text
return j === textLength;
};
return match();
}
/*
* The matchArray[i] is used to record if there is a match between the first i chars in =
* text and the first j chars in pattern.
* So will return matchArray[textLength+1] in the end
* Loop from the beginning of the pattern
* case not '*': if text[i]==pattern[j] or pattern[j] is '?', and matchArray[i] is true,
* set matchArray[i+1] to true, otherwise false
* case '*': since '*' can match any globing, as long as there is a true in matchArray before i
* all the matchArray[i+1], matchArray[i+2],...,matchArray[textLength] could be true
*/
var matchArray = [];
matchArray[0] = true;
for (var j = 0; j < patternLength; j++) {
var i;
var patternChar = pattern.charAt(j);
if (patternChar != '*') {
for(i = textLength - 1; i >= 0; i--)
matchArray[i+1] = !!matchArray[i] && (patternChar === '?' || (patternChar === text.charAt(i)));
} else {
i = 0;
while (i <= textLength && !matchArray[i])
i++;
for(i; i <= textLength; i++)
matchArray[i] = true;
}
matchArray[0] = (matchArray[0] && patternChar === '*');
}
return matchArray[textLength];
}
};
module.exports = Utils;