Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
//
// At top level try cache first. On recursive calls skip cache.
// !! Cache should be probed even for disabled services, to resolve old links.
//
let result;
if (nestingLeft === this.nesting) {
result = await this.cache.get(url_normalized);
// If cache exists - use it.
if (result || result === null) {
// forward hash if needed
if (hash && result) {
u = mdurl.parse(result, true);
u.hash = u.hash || hash;
result = mdurl.format(u);
}
return result;
}
}
//
// First pass validation (quick).
//
if (!this._matchAllRE.test(url_normalized)) break;
// Something found - run additional checks.
let domainConfig = this._domains.find(dc => dc._compiledMatch.exec(url_normalized));
if (nestingLeft < 0) {
throw new UnshortError('Too many redirects', 'EBADREDIRECT');
}
let result = (url !== origUrl) ? url : null;
if (shouldCache) {
// Cache result.
// !! use normalized original URL for cache key.
let uo = mdurl.parse(origUrl, true);
uo.hash = '';
uo.protocol = uo.protocol || 'http';
await this.cache.set(mdurl.format(uo), result);
}
return result;
};
if (parsed.hostname) {
// Encode hostnames in urls like:
// `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
//
// We don't encode unknown schemas, because it's likely that we encode
// something we shouldn't (e.g. `skype:name` treated as `skype:host`)
//
if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
try {
parsed.hostname = punycode.toASCII(parsed.hostname);
} catch (er) { /**/ }
}
}
return mdurl.encode(mdurl.format(parsed));
}
module.exports = normalizeLink;
if (!u.protocol) {
// set protocol for relative links like `//example.com`
u.protocol = 'http:';
}
// user-submitted url has weird protocol, just return `null` in this case
if (u.protocol !== 'http:' && u.protocol !== 'https:') break;
if (u.hash) {
// Copying browser-like behavior here: if we're not redirected to a hash,
// but original url has one, set it as a final hash.
hash = u.hash;
u.hash = '';
}
let url_normalized = mdurl.format(u);
//
// At top level try cache first. On recursive calls skip cache.
// !! Cache should be probed even for disabled services, to resolve old links.
//
let result;
if (nestingLeft === this.nesting) {
result = await this.cache.get(url_normalized);
// If cache exists - use it.
if (result || result === null) {
// forward hash if needed
if (hash && result) {
u = mdurl.parse(result, true);
u.hash = u.hash || hash;
// Encode hostnames in urls like:
// `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
//
// We don't encode unknown schemas, because it's likely that we encode
// something we shouldn't (e.g. `skype:name` treated as `skype:host`)
//
if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
try {
parsed.hostname = punycode.toASCII(parsed.hostname);
} catch (er) {
/**/
}
}
}
return mdurl.encode(mdurl.format(parsed));
};
url.hostname = subdomains.join('.');
url_str = mdurl.format(url);
if (text_length(url_str) <= max_path_length) return elide_text(url_str, max);
}
while (subdomains.length > 2) {
subdomains.shift();
url.hostname = '…' + subdomains.join('.');
url_str = mdurl.format(url);
if (text_length(url_str) <= max_path_length) return elide_text(url_str, max);
}
}
return elide_text(mdurl.format(url), max);
}