Skip to content

Commit

Permalink
Remove invalid ping urls before calling resolver api (OctoLinker#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbuck authored Oct 16, 2019
1 parent d7c7e05 commit fdf7965
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,9 @@ Array [
},
]
`;

exports[`normaliseResolverResults converts https://novalidurl 1`] = `
Array [
Object {},
]
`;
24 changes: 20 additions & 4 deletions packages/helper-normalise-resolver-results/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import ghParse from 'github-url-parse';

const BASE_URL = 'https://github.com';

// https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url/22648406#22648406
function isURL(str) {
const urlRegex =
'^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$';
const url = new RegExp(urlRegex, 'i');
return str.length < 2083 && url.test(str);
}

// Resource within this repositroy
const internal = url => {
const fullUrl = url.replace('{BASE_URL}', BASE_URL);
Expand All @@ -18,10 +26,18 @@ const internal = url => {
};

// An external url like a documenation page
const external = url => ({
type: 'ping',
target: url.replace('{BASE_URL}', BASE_URL),
});
const external = url => {
const target = url.replace('{BASE_URL}', BASE_URL);

if (!isURL(target)) {
return {};
}

return {
type: 'ping',
target,
};
};

// Async resolver
const func = handler => ({
Expand Down
1 change: 1 addition & 0 deletions packages/helper-normalise-resolver-results/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('normaliseResolverResults', () => {
'{BASE_URL}/foo/bar/blob/master/file.js',
'{BASE_URL}/foo/bar/blob/1ab8cfd3b65d3b43335130d6cefbf8c62482680f/file.js',
'https://foosearch.org/',
'https://novalidurl',
['{BASE_URL}/foo/bar/blob/master/file.js'],
[
'{BASE_URL}/foo/bar/blob/master/file.js',
Expand Down

0 comments on commit fdf7965

Please sign in to comment.