forked from OctoLinker/OctoLinker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch live resolver urls in background (OctoLinker#529)
- Loading branch information
1 parent
1ab8cfd
commit a0555f7
Showing
14 changed files
with
257 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/helper-normalise-resolver-results/__snapshots__/test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/", | ||
}, | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.