Skip to content

Commit

Permalink
Fetch live resolver urls in background (OctoLinker#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbuck authored Dec 16, 2018
1 parent 1ab8cfd commit a0555f7
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 39 deletions.
57 changes: 35 additions & 22 deletions packages/core/click-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ const LINK_SELECTOR = '.octolinker-link';
const $body = $('body');
let matches;

function openUrl(url, newWindow = false, newWindowActive = true) {
function openUrl(event, url) {
if (!url) {
return;
}

const newWindow =
storage.get('newWindow') ||
event.metaKey ||
event.ctrlKey ||
event.which === 2;
const newWindowActive = storage.get('newWindowActive');

if (newWindow) {
chrome.runtime.sendMessage({
type: 'newTab',
Expand All @@ -29,39 +36,52 @@ function openUrl(url, newWindow = false, newWindowActive = true) {
}
}

function getResolverUrls(urls) {
const BASE_URL = 'https://github.com';

return [].concat(urls).map(url => {
if (!url) {
function getResolverUrls(event, urls) {
return [].concat(urls).map(item => {
if (!item) {
return null;
}

// github-search resolver returns a function
if (typeof url === 'function') {
if (item.type === 'function') {
return {
func: url,
func: item.handler,
};
}

// Live-query resolver results
if (url.startsWith('https://githublinker.herokuapp.com')) {
if (item.type === 'registry') {
let cacheResult;

try {
cacheResult = global.__ocotlinker_cache[item.registry][item.target];
} catch (error) {
//
}

if (cacheResult) {
openUrl(event, cacheResult);
return [];
}

return {
url,
url: `https://githublinker.herokuapp.com/q/${item.registry}/${
item.target
}`,
method: 'GET',
};
}

// Relative file
if (url.startsWith('{BASE_URL}') || url.startsWith(BASE_URL)) {
if (item.type === 'internal-link') {
return {
url: url.replace('{BASE_URL}', BASE_URL),
url: item.url,
};
}

// External urls
return {
url: `https://githublinker.herokuapp.com/ping?url=${url}`,
url: `https://githublinker.herokuapp.com/ping?url=${item.url}`,
method: 'GET',
};
});
Expand All @@ -83,7 +103,7 @@ async function onClick(event) {

showTooltip($tooltipTarget, PROCESS);

const urls = getResolverUrls(found.urls);
const urls = getResolverUrls(event, found.urls);

if (!urls.length) {
return;
Expand All @@ -93,14 +113,7 @@ async function onClick(event) {
const { url, res } = await fetch(urls);

showTooltip($tooltipTarget, RESOLVED);

const newWindow =
storage.get('newWindow') ||
event.metaKey ||
event.ctrlKey ||
event.which === 2;
const newWindowActive = storage.get('newWindowActive');
openUrl((res || {}).url || url, newWindow, newWindowActive);
openUrl(event, (res || {}).url || url);
removeTooltip($tooltipTarget);
} catch (err) {
showTooltip($tooltipTarget, SORRY);
Expand Down
48 changes: 48 additions & 0 deletions packages/core/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function createStore(json, payload) {
const store = global.__ocotlinker_cache || {};

payload.forEach(({ registry, target }, index) => {
store[registry] = store[registry] || {};
store[registry][target] = json[index];
});

global.__ocotlinker_cache = store;
}

async function runLiveQuery(matches) {
if (!matches.length) {
return [];
}

const payload = [].concat(...matches.map(match => match.urls));

const response = await fetch('https://githublinker.herokuapp.com/bulk', {
method: 'POST',
body: JSON.stringify(payload),
headers: new Headers({
'Content-Type': 'application/json',
}),
});
const json = await response.json();

createStore(json, payload);
}

function filterLiveResolver(matches) {
return matches.reduce((memo, match) => {
match.urls.forEach(url => {
if (url.type !== 'registry') {
return;
}

memo.push(match);
});

return memo;
}, []);
}

export default function(matches) {
const registryMatch = filterLiveResolver(matches);
runLiveQuery(registryMatch);
}
16 changes: 9 additions & 7 deletions packages/core/octo-linker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import BlobReader from '@octolinker/blob-reader';
import insertLink from '@octolinker/helper-insert-link';
import * as storage from '@octolinker/helper-settings';
import helperSortUrls from '@octolinker/helper-sort-urls';
import normaliseResolverResults from '@octolinker/helper-normalise-resolver-results';
import notification from './notification';
import clickHandler from './click-handler';
import Plugins from './plugin-manager.js';
import debugMode from './debug-mode.js';
import loader from './loader.js';
import * as loadPlugins from './load-plugins';

function initialize(self) {
Expand Down Expand Up @@ -50,19 +52,19 @@ async function run(self) {
matches = matches
.filter(result => result !== undefined)
.map(({ link, urls }) => {
let finalUrls = urls;

// Some urls are single object e.g. live-resolver-query results
if (Array.isArray(urls)) {
finalUrls = helperSortUrls(urls, link.innerText);
}
const urlsSorted = helperSortUrls(urls, link.innerText);

return {
link,
urls: finalUrls,
urls: normaliseResolverResults(urlsSorted),
};
});

// Prefetch live resolver results in background
requestIdleCallback(() => {
loader(matches);
});

clickHandler(matches);
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@octolinker/helper-insert-link": "1.0.0",
"@octolinker/helper-settings": "1.0.0",
"@octolinker/helper-sort-urls": "1.0.0",
"@octolinker/helper-normalise-resolver-results": "1.0.0",
"@octolinker/plugin-bower-manifest": "1.0.0",
"@octolinker/plugin-composer-manifest": "1.0.0",
"@octolinker/plugin-css": "1.0.0",
Expand Down
7 changes: 7 additions & 0 deletions packages/helper-insert-link/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,12 @@ describe('insert-link', () => {
expect(helper(input).matches.length).toBe(1);
expect(helper(input).matches).toMatchSnapshot();
});

it('returns an array with values', () => {
const input = 'foo <span>"bar"</span>';

fakePlugin.resolve.mockReturnValue([undefined, null, '', 'bar']);
expect(helper(input).matches[0].urls).toEqual(['bar']);
});
});
});
8 changes: 7 additions & 1 deletion packages/helper-insert-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,15 @@ export default function(blob, regex, plugin, meta = {}) {
.slice(1)
.map(item => item.replace(/['|"]/g, ''));

let urls = plugin.resolve(blob.path, values, meta);

if (Array.isArray(urls)) {
urls = urls.filter(Boolean);
}

matches.push({
link,
urls: plugin.resolve(blob.path, values, meta),
urls,
});

return node;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`normaliseResolverResults converts [object Object] 1`] = `
Array [
Object {
"registry": "npm",
"target": "foo",
"type": "registry",
},
]
`;

exports[`normaliseResolverResults converts {BASE_URL}/foo/bar/blob/1ab8cfd3b65d3b43335130d6cefbf8c62482680f/file.js 1`] = `
Array [
Object {
"branch": "1ab8cfd3b65d3b43335130d6cefbf8c62482680f",
"path": "file.js",
"repo": "bar",
"type": "internal-link",
"url": "https://github.com/foo/bar/blob/1ab8cfd3b65d3b43335130d6cefbf8c62482680f/file.js",
"user": "foo",
},
]
`;

exports[`normaliseResolverResults converts {BASE_URL}/foo/bar/blob/master/file.js 1`] = `
Array [
Object {
"branch": "master",
"path": "file.js",
"repo": "bar",
"type": "internal-link",
"url": "https://github.com/foo/bar/blob/master/file.js",
"user": "foo",
},
]
`;

exports[`normaliseResolverResults converts function () {} 1`] = `
Array [
Object {
"handler": [Function],
"type": "function",
},
]
`;

exports[`normaliseResolverResults converts https://foosearch.org/ 1`] = `
Array [
Object {
"type": "external-link",
"url": "https://foosearch.org/",
},
]
`;
57 changes: 57 additions & 0 deletions packages/helper-normalise-resolver-results/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import ghParse from 'github-url-parse';

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

// Resource within this repositroy
const internal = url => {
const fullUrl = url.replace('{BASE_URL}', BASE_URL);
const { user, repo, branch, path } = ghParse(fullUrl);

return {
type: 'internal-link',
url: fullUrl,
user,
repo,
branch,
path,
};
};

// An external url like a documenation page
const external = url => ({
type: 'external-link',
url,
});

// Async resolver
const func = handler => ({
type: 'function',
handler,
});

// Needs to be validated through https://githublinker.herokuapp.com/
const registry = ({ registry: type, target }) => ({
type: 'registry',
registry: type,
target,
});

export default function(urls) {
return [].concat(urls).map(url => {
if (typeof url === 'string') {
if (url.startsWith('{BASE_URL}')) {
return internal(url);
}

return external(url);
}

if (url.registry) {
return registry(url);
}

if (typeof url === 'function') {
return func(url);
}
});
}
11 changes: 11 additions & 0 deletions packages/helper-normalise-resolver-results/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@octolinker/helper-normalise-resolver-results",
"version": "1.0.0",
"description": "",
"repository": "https://github.com/octolinker/octolinker/tree/master/packages/helper-normalise-resolver-results",
"license": "MIT",
"main": "./index.js",
"dependencies": {
"github-url-parse": "^0.1.0"
}
}
13 changes: 13 additions & 0 deletions packages/helper-normalise-resolver-results/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import normaliseResolverResults from './index.js';

describe('normaliseResolverResults', () => {
test.each([
'{BASE_URL}/foo/bar/blob/master/file.js',
'{BASE_URL}/foo/bar/blob/1ab8cfd3b65d3b43335130d6cefbf8c62482680f/file.js',
'https://foosearch.org/',
{ registry: 'npm', target: 'foo' },
() => {},
])('converts %s', url => {
expect(normaliseResolverResults([url])).toMatchSnapshot();
});
});
7 changes: 4 additions & 3 deletions packages/plugin-java/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ describe('Java', () => {
});

it('resolves community packages', () => {
expect(Java.resolve(path, ['com.company.app'])).toBe(
'https://githublinker.herokuapp.com/q/java/com.company.app',
);
expect(Java.resolve(path, ['com.company.app'])).toEqual({
registry: 'java',
target: 'com.company.app',
});
});
});
Loading

0 comments on commit a0555f7

Please sign in to comment.