Skip to content

Commit

Permalink
process-url: refactor external link
Browse files Browse the repository at this point in the history
Close #937
  • Loading branch information
myfreeer committed Oct 3, 2023
1 parent 9a79bbd commit f216d24
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 123 deletions.
31 changes: 31 additions & 0 deletions src/mdn/process-url/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,34 @@ export const downloadableHosts = arrayToMap([
'bcd.developer.mozilla.org',
'bcd.developer.allizom.org',
]);

export interface ExternalHost {
host: string;
prefix: string;
pathPrefix: string;
pathPrefixLength: number;
pattern: string;
}

export const externalHosts = [
['/interactive-examples/', 'interactive-examples.mdn.mozilla.net'],
['/mdn-github-io/', 'mdn.github.io'],
['/unpkg-com/', 'unpkg.com'],
['/mdn-github-io/', 'mdn.github.io'],
// https://github.com/website-local/mdn-local/issues/361
['/cdnjs-cloudflare-com/', 'cdnjs.cloudflare.com'],
// https://github.com/website-local/mdn-local/issues/448
['/cdn-jsdelivr-net/', 'cdn.jsdelivr.net'],
].map(([prefix, host]) => ({
host,
prefix,
pathPrefix: prefix.slice(0, -1),
pathPrefixLength: prefix.length - 1,
pattern: '//' + host + '/',
} as ExternalHost));

export const externalHostMap: Record<string, ExternalHost> = {};

for (const externalHost of externalHosts) {
externalHostMap[externalHost.host] = externalHost;
}
61 changes: 11 additions & 50 deletions src/mdn/process-url/redirect-download-link.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {Resource} from 'website-scrap-engine/lib/resource';
import URI from 'urijs';
import {externalHosts} from './consts';

export const redirectDownloadLink = (res: Resource): Resource => {
const url = res.downloadLink;
Expand All @@ -17,57 +18,17 @@ export const redirectDownloadLink = (res: Resource): Resource => {
res.downloadLink = uri.search('').host('mdn.mozillademos.org').toString();
return res;
}
if (path.startsWith('/interactive-examples/')) {
// interactive-examples
// redirect back to real url
res.downloadLink = uri.search('')
.host('interactive-examples.mdn.mozilla.net')
.path(path.slice('/interactive-examples'.length))
.toString();
return res;
}
if (path.startsWith('/mdn-github-io/')) {
// mdn.github.io
// redirect back to real url
path = path.slice('/mdn-github-io'.length);
res.downloadLink = uri.search('')
.host('mdn.github.io')
.path(path)
.toString();
return res;
}
if (path.startsWith('/unpkg-com/')) {
// unpkg.com
// redirect back to real url
res.downloadLink = uri.search('')
.host('unpkg.com')
.path(path.slice('/unpkg-com'.length))
.toString();
return res;
}

// https://github.com/website-local/mdn-local/issues/361
// cdnjs.cloudflare.com
if (path.startsWith('/cdnjs-cloudflare-com/')) {
// unpkg.com
// redirect back to real url
res.downloadLink = uri.search('')
.host('cdnjs.cloudflare.com')
.path(path.slice('/cdnjs-cloudflare-com'.length))
.toString();
return res;
}

// https://github.com/website-local/mdn-local/issues/448
// cdn.jsdelivr.net
if (path.startsWith('/cdn-jsdelivr-net/')) {
// cdn.jsdelivr.net
// redirect back to real url
res.downloadLink = uri.search('')
.host('cdn.jsdelivr.net')
.path(path.slice('/cdn-jsdelivr-net'.length))
.toString();
return res;
for (const externalHost of externalHosts) {
if (path.startsWith(externalHost.prefix)) {
// interactive-examples
// redirect back to real url
res.downloadLink = uri.search('')
.host(externalHost.host)
.path(path.slice(externalHost.pathPrefixLength))
.toString();
return res;
}
}

// https://github.com/website-local/mdn-local/issues/208
Expand Down
94 changes: 21 additions & 73 deletions src/mdn/process-url/redirect-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
appendDocsWebPath,
appendLocalePath,
downloadableHosts,
externalHostMap,
externalHosts,
localesMap,
redirectLocale
} from './consts';
Expand Down Expand Up @@ -107,39 +109,15 @@ export function redirectUrl(
u = u.host(mdnHost);
needToRebuildUrl = true;
break;
case 'interactive-examples.mdn.mozilla.net': // interactive-examples
// fake url, redirected back in redirectDownloadLink
u = u.host(mdnHost)
.path('/interactive-examples' + u.path());
shouldReturnEarly = true;
break;
case 'mdn.github.io': // mdn.github.io
// fake url, redirected back in redirectDownloadLink
u = u.host(mdnHost)
.path('/mdn-github-io' + u.path());
shouldReturnEarly = true;
break;
case 'unpkg.com': // unpkg.com
// fake url, redirected back in redirectDownloadLink
u = u.host(mdnHost)
.path('/unpkg-com' + u.path());
shouldReturnEarly = true;
break;
// https://github.com/website-local/mdn-local/issues/361
case 'cdnjs.cloudflare.com': // cdnjs.cloudflare.com
// fake url, redirected back in redirectDownloadLink
u = u.host(mdnHost)
.path('/cdnjs-cloudflare-com' + u.path());
shouldReturnEarly = true;
break;
// https://github.com/website-local/mdn-local/issues/448
case 'cdn.jsdelivr.net': // cdn.jsdelivr.net
// fake url, redirected back in redirectDownloadLink
u = u.host(mdnHost)
.path('/cdn-jsdelivr-net' + u.path());
shouldReturnEarly = true;
break;
default:
if (externalHostMap[host]) {
const externalHost = externalHostMap[host];
// fake url, redirected back in redirectDownloadLink
u = u.host(mdnHost)
.path(externalHost.pathPrefix + u.path());
shouldReturnEarly = true;
break;
}
// https://github.com/website-local/mdn-local/issues/208
if (host.endsWith('.mdn.mozit.cloud')) {
if (u.protocol() === 'http') {
Expand Down Expand Up @@ -203,47 +181,17 @@ export function redirectUrl(
.absoluteTo(parent.url)
.normalizePath();
}
if (parent && parent.downloadLink.includes('//interactive-examples.mdn.mozilla.net/') &&
!u.path().includes('/interactive-examples/')) {
// interactive-examples
// fake url, redirected back in redirectDownloadLink
return u.host(mdnHost)
.path('/interactive-examples' + u.path())
.toString();
}
if (parent && parent.downloadLink.includes('//mdn.github.io/') &&
!u.path().includes('/mdn-github-io/')) {
// mdn.github.io
// fake url, redirected back in redirectDownloadLink
return u.host(mdnHost)
.path('/mdn-github-io' + u.path())
.toString();
}
if (parent && parent.downloadLink.includes('//unpkg.com/') &&
!u.path().includes('/unpkg-com/')) {
// unpkg.com
// fake url, redirected back in redirectDownloadLink
return u.host(mdnHost)
.path('/unpkg-com' + u.path())
.toString();
}
// https://github.com/website-local/mdn-local/issues/361
if (parent && parent.downloadLink.includes('//cdnjs.cloudflare.com/') &&
!u.path().includes('/cdnjs-cloudflare-com/')) {
// cdnjs.cloudflare.com
// fake url, redirected back in redirectDownloadLink
return u.host(mdnHost)
.path('/cdnjs-cloudflare-com' + u.path())
.toString();
}
// https://github.com/website-local/mdn-local/issues/448
if (parent && parent.downloadLink.includes('//cdn.jsdelivr.net/') &&
!u.path().includes('/cdn-jsdelivr-net/')) {
// unpkg.com
// fake url, redirected back in redirectDownloadLink
return u.host(mdnHost)
.path('/cdn-jsdelivr-net' + u.path())
.toString();
if (parent && parent.downloadLink) {
const downloadLink = parent.downloadLink;
for (const externalHost of externalHosts) {
if (downloadLink.includes(externalHost.pattern) &&
!u.path().includes(externalHost.prefix)) {
// fake url, redirected back in redirectDownloadLink
return u.host(mdnHost)
.path(externalHost.pathPrefix + u.path())
.toString();
}
}
}
needToRebuildUrl = true;
}
Expand Down

0 comments on commit f216d24

Please sign in to comment.