Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
result = mdurl.format(u);
}
return result;
}
}
//
// First pass validation (quick).
//
if (!this._matchAllRE.test(url_normalized)) break;
// Something found - run additional checks.
function normalizeLinkText(url) {
var parsed = mdurl.parse(url, true);
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.toUnicode(parsed.hostname);
} catch (er) {/**/}
}
}
return mdurl.decode(mdurl.format(parsed));
let domainConfig = this._domains.find(dc => dc._compiledMatch.exec(url_normalized));
if (!domainConfig || !domainConfig.validate(url_normalized)) break;
// Valid redirector => should cache result
shouldCache = true;
result = await domainConfig.fetch.call(this, url_normalized, domainConfig);
// If unshortener has persistent fail - stop.
if (!result) break;
// Parse and check url
//
u = mdurl.parse(result, true);
if (!u.hostname) {
// relative urls are not supported for now
throw new UnshortError('Redirected to an invalid location', 'EBADREDIRECT');
}
if (u.protocol && u.protocol !== 'http:' && u.protocol !== 'https:') {
// Accept:
//
// - http:// protocol (e.g. http://example.org/)
// - https:// protocol (e.g. https://example.org/)
// - protocol-relative links (e.g. //example.org/)
//
// Restriction is done for security reasons. Even though browsers
// can redirect anywhere, most shorteners have similar restrictions.
//
function normalizeLink(url) {
var parsed = mdurl.parse(url, true);
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));
function beautify_url(url_str, max_length) {
if (typeof url_str === 'undefined' || url_str === null) return '';
var url = mdurl.parse(String(url_str), true);
// urls without host and protocol, e.g. "example.org/foo"
if (!url.protocol && !url.slashes && !url.hostname) {
url = mdurl.parse('//' + url_str, true);
}
try {
if (url.hostname) {
url.hostname = punycode.toUnicode(url.hostname);
}
} catch (e) {}
// Decode url-encoded characters
//
if (url.auth) url.auth = mdurl.decode(url.auth);
if (url.hash) url.hash = mdurl.decode(url.hash);
if (url.search) url.search = mdurl.decode(url.search);
if (url.pathname) url.pathname = mdurl.decode(url.pathname);
// Omit protocol if it's http, https or mailto
function normalizeLink(url) {
var parsed = mdurl.parse(url, true);
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));
function normalizeLink(url) {
var parsed = mdurl.parse(url, true);
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));
export const normalizeLinkText = (url: string): string => {
var parsed = mdurl.parse(url, true);
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.toUnicode(parsed.hostname);
} catch (er) {
/**/
}
}
}
try {
res = await this.request(url, { method: 'HEAD' });
} catch (e) {
if (e.statusCode >= 400 && e.statusCode < 500) return null;
throw new UnshortError(
`Remote server error, code ${e.code}, statusCode ${e.statusCode}`,
'EHTTP',
e.statusCode);
}
if (isRedirect(res.statusCode)) {
let uSrc = mdurl.parse(url, true);
let uDst = mdurl.parse(res.headers.location, true);
if (!uDst.hostname) { uDst.hostname = uSrc.hostname; }
if (!uDst.protocol) { uDst.protocol = uSrc.protocol; }
if (!uDst.slashes) { uDst.slashes = uSrc.slashes; }
url = mdurl.format(uDst);
continue;
}
// reached destination
if (res.statusCode >= 200 && res.statusCode < 300) return url;
throw new UnshortError(`Unexpected status code: ${res.statusCode}`, 'EHTTP', res.statusCode);
}
return null;
function o(e) {
var t = f.parse(e, true);
if (t.hostname && (!t.protocol || y.indexOf(t.protocol) >= 0)) {
try {
t.hostname = m.toASCII(t.hostname);
} catch (n) {}
}
return f.encode(f.format(t));
}