-
Notifications
You must be signed in to change notification settings - Fork 20
/
helpers.js
284 lines (255 loc) · 8.37 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* global chrome */
const $ = window.jQuery
const fetch = window.fetch
// const delay = require('delay')
const {lastIndexOfRegex} = require('index-of-regex')
var waitToken = new Promise((resolve, reject) => {
chrome.storage.sync.get('token', (res) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError.message)
} else {
resolve(res.token)
}
})
})
function gh (path) {
return waitToken
.then(token =>
fetch(`https://api.github.com/${path}`, {
headers: {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'github.com/fiatjaf/module-linker',
'Authorization': `token ${token}`
}
})
)
.then(r => r.json())
}
module.exports.treePromise = treePromise
var treePromiseCache = {}
function treePromise (postProcess) {
let pp = typeof postProcess !== 'undefined' ? postProcess.name : ''
let { user, repo, ref } = window.pathdata
let xhrkey = `${user}:${repo}:${ref}`
let ppkey = xhrkey + ':' + pp
// full cache hit
if (treePromiseCache[ppkey]) {
return treePromiseCache[ppkey]
}
// cached tree, but not with the postProcess fn
// (happens when there is more than one postProcess function
// in the same user/repo/ref)
if (treePromiseCache[xhrkey]) {
let processedPromise = treePromiseCache[xhrkey]
.then(tree => {
if (postProcess) {
return postProcess(tree)
}
return tree
})
treePromiseCache[ppkey] = processedPromise
return processedPromise
}
// nothing found. fetch the tree then recall this same function
let xhrPromise = Promise.resolve()
.then(() => {
if (ref.length >= 40) {
return ref // ref is the commit sha itself
}
// try to fetch the commit sha from the page's html
let commitTeaseSha = $('.commit-tease-sha')
if (commitTeaseSha.length) {
return commitTeaseSha.attr('href').split('/').slice(-1)[0]
}
// fallback to the API
return gh(`repos/${user}/${repo}/git/refs/heads/${ref}`)
.then(data => data.object.sha)
})
.then(sha =>
gh(`repos/${user}/${repo}/git/trees/${sha}?recursive=4`)
)
.then(data => data.tree.map(blob => blob.path))
treePromiseCache[xhrkey] = xhrPromise
return treePromise(postProcess)
}
module.exports.bloburl = function (user, repo, ref, path) {
path = path[0] === '/' ? path.slice(1) : path
return `https://github.com/${user}/${repo}/blob/${ref}/${path}`
}
module.exports.treeurl = function (user, repo, ref, path) {
path = path[0] === '/' ? path.slice(1) : path
return `https://github.com/${user}/${repo}/tree/${ref}/${path}`
}
module.exports.createLink = function (elem, moduleName, url, backwards = false) {
if (!moduleName || !url) return
elem.innerHTML = module.exports.htmlWithLink(elem.innerHTML, moduleName, url, backwards)
}
module.exports.htmlWithLink = function (baseHTML, moduleName, url, backwards = false) {
let kind = url.kind || 'relative'
var link = $('<a>')
.addClass('module-linker')
.attr('href', url.url || url)
.text(moduleName)
.addClass(kind)
if (url.desc) {
link = $('<span>')
.attr('data-wenk', url.desc)
.append(link)
}
link = link.get(0).outerHTML
let regexN = moduleName.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
let regex = new RegExp(
(moduleName[0].match(/\w/) ? '\\b' : '') +
regexN +
(moduleName.slice(-1)[0].match(/w/) ? '\\b' : '')
)
if (backwards) {
let index = lastIndexOfRegex(baseHTML, regex)
return baseHTML.slice(0, index) +
link +
baseHTML.slice(index + moduleName.length)
} else {
// use regex to replace the module name, but escape regex special characters before
return baseHTML.replace(
regex,
link
)
}
}
var moduleCache = {} // a cache of promises to external modules.
module.exports.external = external
function external (registry, module) {
let key = `${registry}::${module}`
if (moduleCache[key]) return moduleCache[key]
var w
switch (registry) {
case 'composer':
w = json(`https://packagist.org/packages/${module}.json`)
.then(info => ({
url: info.package.repository || `https://packagist.org/packages/${module}`,
kind: 'external',
desc: info.package.description
}))
break
case 'rubygems':
w = json(`https://rubygems.org/api/v1/gems/${module}.json`)
.then(info => ({
url: info.source_code_uri || info.homepage_uri ||
info.project_url || `https://rubygems.org/gems/${module}`,
docs: info.documentation_uri,
kind: 'external',
desc: info.info
}))
break
case 'npm':
w = json(`https://registry.npmjs.org/${module.replace('/', '%2f')}`)
.then(info => ({
url: info.url || info.homepage || info.repository.url
? info.repository.url.replace('git+', '')
: `https://npmjs.com/package/${module}`,
kind: 'external',
desc: info.description || info.keywords.join(', ')
}))
break
case 'pypi':
w = json(`https://pypi.python.org/pypi/${module}/json`)
.then(info => ({
url: info.info.home_page || info.info.package_url || `https://pypi.python.org/pypi/${module}`,
kind: 'external',
desc: info.info.summary
}))
break
case 'crates':
w = json(`https://crates.io/api/v1/crates/${module}`)
.then(info => ({
url: info.crate.repository ||
info.crate.homepage ||
`https://crates.io/crates/${module}`,
docs: info.crate.documentation || info.crate.homepage,
kind: 'external',
desc: info.crate.description || info.categories.length && info.categories[0].description
}))
break
case 'dart':
w = json(`http://pub.dartlang.org/api/packages/${module}`)
.then(info => ({
url: info.latest.pubspec.homepage ||
`https://pub.dartlang.org/packages/${module}`,
docs: info.latest.pubspec.documentation,
kind: 'external',
desc: info.latest.pubspec.description
}))
break
case 'julia':
w = text(`https://raw.githubusercontent.com/JuliaLang/METADATA.jl/metadata-v2/${module}/url`)
.then(url => ({url: url.trim().replace('git://', 'https://')}))
.catch(() =>
html('http://pkg.julialang.org')
.then($ =>
$('.pkgnamedesc a')
.map((i, a) => console.log(i, a) || a)
.filter((_, a) => $(a).text() === module)
.map((_, a) => ({
url: $(a).attr('href'),
kind: 'external',
desc: $(a).parent().parent().find('h4').text()
}))
.get(0)
)
)
break
case 'hackage':
w = html(`https://hackage.haskell.org/package/${module}`)
.then($ => {
let url = $('a[href*="github.com"]').attr('href')
if (url) return {url: url.replace('git://', 'https://')}
return {
kind: 'external',
url: `https://hackage.haskell.org/package/$${module}`
}
})
break
case 'crystal':
w = text(`https://jsonbin.org/fiatjaf/crystal/${module}`)
.then(url => ({
kind: 'external',
url: url
}))
break
default:
w = Promise.reject(new Error('no registry found with that name.'))
}
w = w
.then(data => {
if (data.desc) {
data.desc = data.desc.slice(0, 250)
}
return data
})
moduleCache[key] = w
return moduleCache[key]
}
var httpCache = {} // a cache of promises to external http results
module.exports.cached = function cachedHttpRequest (url) {
let key = url
if (httpCache[key]) return Promise.resolve(httpCache[key])
httpCache[key] = fetch(url)
.then(r => {
if (r.status >= 300) {
throw new Error(`failed to fetch ${url}.`)
}
return r.text()
})
return httpCache[key]
}
const text = module.exports.text = function cachedTextHttpRequest (url) {
return module.exports.cached(url)
}
const json = module.exports.json = function cachedJsonHttpRequest (url) {
return module.exports.cached(url)
.then(text => JSON.parse(text))
}
const html = module.exports.html = function cachedHtmlHttpRequest (url) {
return module.exports.cached(url)
.then(text => $.parseHTML(text))
}