express-user-agent-blocker
Version:
Express middleware for blocking access based on User Agent
21 lines (20 loc) • 974 B
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildUaBlockRegex = void 0;
const _1 = require("./");
/**
* Builds the regex to test UAs against
* @param {string[]} userAgentToBlock - one or more (partial) User Agent strings to block
* @param {Options} [options] - optional options which may contain custom log function
* @return {RegExp|null} the regex to test against, or null if invalid input provided
*/
const buildUaBlockRegex = (userAgentToBlock, options) => {
const log = _1.getLogger('euab:buildUaBlockRegex', options);
if (userAgentToBlock.join('').trim().length > 0) {
const blockRegex = new RegExp(`^.*(${userAgentToBlock.join('|').toLowerCase()}).*$`);
log(`created blocked regex: ${blockRegex}`);
return blockRegex;
}
log(`!!! could not create regex from '${userAgentToBlock}' - middleware will call 'next' on all requests`);
return null;
};
exports.buildUaBlockRegex = buildUaBlockRegex;