Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const replacements = {
'<': '<',
'>': '>',
'"': '"',
};
function normhtml(str) {
// Like escapehtml but doesn't escape & since we don't want to have
// to decode and then reencode.
return str.replace(/[<>"]/g, (chr) => replacements[chr]);
}
// A registry name should end in a TLD. We sort TLDs so that the RegExp greedily matches
// the longest TLD: e.g. .community should come before .com.
const REG_NAME = String.raw`(?:[a-z0-9\-_~!$&'()*+,;=%]+[.])+(?:${ tlds.sort(descLength).join('|') })[.]?`;
// TODO: range restrict
// eslint-disable-next-line id-match
const IPV4 = String.raw`\d+(?:\.\d+){3}(?=[/?#]|:\d)`;
// TODO: count hex digits properly.
// eslint-disable-next-line id-match
const IPV6 = String.raw`\[(?!\])(?:[0-9a-f]+(?:[:][0-9a-f]+)*)?(?:[:][:](?:[0-9a-f]+(?:[:][0-9a-f]+)*))?\]`;
const PROTOCOL_REGEX = String.raw`\b([a-z][a-z0-9+\-.]*):`;
const PROTOCOL_PATTERN = new RegExp(`^${ PROTOCOL_REGEX }`, 'i');
const AUTHORITY_REGEX = String.raw`[^/:?#\s<>!,;]*`;
const AUTHORITY_PATTERN = new RegExp(`^(?:[^:/#?]*:)?//(${ AUTHORITY_REGEX })`);
export default (_opts) => {
const opts = Object.assign({ strict: true }, _opts);
const protocol = `(?:(?:[a-z]+:)?//)${opts.strict ? '' : '?'}`;
const auth = '(?:\\S+(?::\\S*)?@)?';
const ip = ipRegex.v4().source;
const host = '(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)';
const domain = '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*';
const tld = `(?:\\.${opts.strict ? '(?:[a-z\\u00a1-\\uffff]{2,})' : `(?:${tlds.sort((a, b) => b.length - a.length).join('|')})`})\\.?`;
const port = '(?::\\d{2,5})?';
const path = '(?:[/?#][^\\s"]*)?';
const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ip}|${host}${domain}${tld})${port}${path}`;
return opts.exact ? new RegExp(`(?:^${regex}$)`, 'i') : new RegExp(regex, 'ig');
};
export default (_opts) => {
const opts = Object.assign({ strict: true }, _opts);
const protocol = `(?:(?:[a-z]+:)?//)${opts.strict ? '' : '?'}`;
const auth = '(?:\\S+(?::\\S*)?@)?';
const ip = ipRegex.v4().source;
const host = '(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)';
const domain = '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*';
const tld = `(?:\\.${opts.strict ? '(?:[a-z\\u00a1-\\uffff]{2,})' : `(?:${tlds.sort((a, b) => b.length - a.length).join('|')})`})\\.?`;
const port = '(?::\\d{2,5})?';
const path = '(?:[/?#][^\\s"]*)?';
const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ip}|${host}${domain}${tld})${port}${path}`;
return opts.exact ? new RegExp(`(?:^${regex}$)`, 'i') : new RegExp(regex, 'ig');
};
'use strict';
var fs = require('fs');
var path = require('path');
var tlds = require('tlds');
tlds = tlds.sort(function (a, b) {
return b.length - a.length;
});
fs.writeFileSync(path.join(__dirname, '../tlds.json'), JSON.stringify(tlds));
module.exports = options => {
options = {
strict: true,
...options
};
const protocol = `(?:(?:[a-z]+:)?//)${options.strict ? '' : '?'}`;
const auth = '(?:\\S+(?::\\S*)?@)?';
const ip = ipRegex.v4().source;
const host = '(?:(?:[a-z\\u00a1-\\uffff0-9][-_]*)*[a-z\\u00a1-\\uffff0-9]+)';
const domain = '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*';
const tld = `(?:\\.${options.strict ? '(?:[a-z\\u00a1-\\uffff]{2,})' : `(?:${tlds.sort((a, b) => b.length - a.length).join('|')})`})\\.?`;
const port = '(?::\\d{2,5})?';
const path = '(?:[/?#][^\\s"]*)?';
const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ip}|${host}${domain}${tld})${port}${path}`;
return options.exact ? new RegExp(`(?:^${regex}$)`, 'i') : new RegExp(regex, 'ig');
};