-
Notifications
You must be signed in to change notification settings - Fork 91
/
plugin-hub.js
309 lines (275 loc) · 8.69 KB
/
plugin-hub.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { createActions, handleActions } from 'redux-actions'
import { createSelector } from 'reselect'
import { getLatestRelease } from './bootstrap'
import { getExternalPlugins as getConfigExternalPlugins } from './config'
import api from '../api'
const pluginHubUrl = 'https://repo.runelite.net/plugins/'
const pluginHubApi = api(pluginHubUrl)
const runeliteApi = api('https://api.runelite.net/')
const githubApi = api('https://api.github.com/')
const githubRawApi = api(
'https://raw.githubusercontent.com/runelite/plugin-hub/master/plugins/'
)
// Actions
export const {
fetchExternalPlugins,
fetchExternalPluginInfo,
fetchPluginHubStats,
setExternalPlugins,
setExternalPluginInfo,
setPluginHubStats,
setPluginFilter,
setPluginSorting
} = createActions(
{
FETCH_EXTERNAL_PLUGINS: () => async (dispatch, getState) => {
const version = getLatestRelease(getState())
const response = await pluginHubApi(
`manifest/${version}_full.js`,
{ method: 'GET' },
true
)
const signatureSize = new DataView(response).getUint32(0)
// Removes the signature, and it's 4byte header, then converts the result into a string
const jsonStr = new TextDecoder('utf-8').decode(
new Uint8Array(response.slice(signatureSize + 4))
)
const pluginManifest = JSON.parse(jsonStr)
const pluginDisplay = pluginManifest.display
const pluginJars = pluginManifest.jars
const plugins = pluginDisplay.map(p => {
if (p.iconHash) {
p.imageUrl = `${pluginHubUrl}icon/${p.internalName}_${p.iconHash}.png`
}
p.working = !!pluginJars.find(j => j.internalName === p.internalName)
p.description = p.description
.replace(/<br\/?>/g, '\n')
.replace(/<[^>]+>/g, '')
p.unavailableReason = (p.unavailableReason || '')
.replace(/<br\/?>/g, '\n')
.replace(/<[^>]+>/g, '')
return p
})
dispatch(setExternalPlugins(plugins))
return plugins
},
FETCH_EXTERNAL_PLUGIN_INFO: internalName => async (dispatch, getState) => {
const response = await githubRawApi(internalName, {
method: 'GET'
})
let repository = ''
let commit = ''
response.split('\n').forEach(line => {
const kv = line.split('=')
if (kv[0] === 'repository') {
repository = kv[1]
}
if (kv[0] === 'commit') {
commit = kv[1]
}
})
if (!repository) {
return
}
const repoSplit = repository
.replace('https://', '')
.replace('http://', '')
.split('/')
if (repoSplit.length < 3) {
return
}
const user = repoSplit[1]
const repo = repoSplit[2].replace('.git', '')
let rawReadmeHTML = await githubApi(
`repos/${user}/${repo}/readme?ref=${commit}`,
{
method: 'GET',
headers: {
accept: 'application/vnd.github.VERSION.html'
}
}
)
let readme = ''
/**
* @param {string} url
* @param {HTMLElement} context
* @returns {string}
*/
function mungeURL(url, context) {
if (url.startsWith('#')) {
// this is a fragment only url; it should be relative to this page
// the ids in the page start with `user-content-`, but the urls do not
return '#user-content-' + url.substring(1)
}
// github allows users to make their url an absolute-path, but actually refer to their repo root
if (url.startsWith('/')) {
url = url.substring(1)
}
let newRoot = `https://github.com/${user}/${repo}/tree/${commit}/`
if (context.tagName === 'IMG') {
newRoot = `https://raw.githubusercontent.com/${user}/${repo}/${commit}/`
}
url = new URL(url, newRoot).toString()
return url
}
try {
let dom = new DOMParser().parseFromString(rawReadmeHTML, 'text/html')
dom.querySelectorAll('a').forEach(el => {
el.href = mungeURL(el.getAttribute('href'), el)
})
dom.querySelectorAll('img').forEach(el => {
let src = el.getAttribute('src')
// people like to use massive (50+ MiB) gifs, so turn these into links
if (src.endsWith('.gif')) {
let label = el.title || el.alt || src
if (el.parentElement?.tagName === 'A') {
// we are already in a link, just use that
el.parentElement.textContent = label
} else {
let replacement = document.createElement('a')
replacement.target = '_blank'
replacement.textContent = label
replacement.href = mungeURL(src, replacement)
el.replaceWith(replacement)
}
} else {
el.src = mungeURL(src, el)
}
})
// this is dumb, but react makes doing this right painful
readme = dom.body.innerHTML
} catch (e) {
console.error('Loading readme failed', e)
}
dispatch(
setExternalPluginInfo({
internalName,
github: {
readme,
user,
repo,
commit
}
})
)
return readme
},
FETCH_PLUGIN_HUB_STATS: () => async (dispatch, getState) => {
const version = getLatestRelease(getState())
const response = await runeliteApi(`runelite-${version}/pluginhub`, {
method: 'GET'
})
dispatch(setPluginHubStats(response))
return response
}
},
'SET_EXTERNAL_PLUGINS',
'SET_EXTERNAL_PLUGIN_INFO',
'SET_PLUGIN_HUB_STATS',
'SET_PLUGIN_FILTER',
'SET_PLUGIN_SORTING'
)
// Reducer
export default handleActions(
{
[setExternalPlugins]: (state, { payload }) => ({
...state,
data: payload
}),
[setExternalPluginInfo]: (state, { payload }) => ({
...state,
data: state.data
.filter(p => p.internalName !== payload.internalName)
.concat([
{
...state.data.find(d => d.internalName === payload.internalName),
...payload
}
])
}),
[setPluginHubStats]: (state, { payload }) => ({
...state,
stats: payload
}),
[setPluginFilter]: (state, { payload }) => ({
...state,
filter: {
...state.filter,
...payload
}
}),
[setPluginSorting]: (state, { payload }) => ({
...state,
sorting: payload
})
},
{
filter: {
name: ''
},
sorting: 'active installs',
data: [],
stats: {}
}
)
// Selectors
export const getExternalPlugins = state => state.externalPlugins.data
export const getPluginHubStats = state => state.externalPlugins.stats
export const getPluginFilter = state => state.externalPlugins.filter
export const getPluginSorting = state => state.externalPlugins.sorting
export const getExternalPluginsWithState = createSelector(
getExternalPlugins,
getPluginHubStats,
getConfigExternalPlugins,
(externalPlugins, pluginHubStats, configExternal) => {
return externalPlugins.map(p => {
p.installed = configExternal.includes(p.internalName)
p.count = pluginHubStats.hasOwnProperty(p.internalName)
? pluginHubStats[p.internalName]
: 0
return p
})
}
)
export const getFilteredExternalPlugins = createSelector(
getExternalPluginsWithState,
getPluginFilter,
(externalPlugins, filter) =>
externalPlugins.filter(
p =>
!filter.name ||
p.displayName.toLowerCase().indexOf(filter.name.toLowerCase()) !== -1 ||
p.author.toLowerCase().indexOf(filter.name.toLowerCase()) !== -1 ||
p.description.toLowerCase().indexOf(filter.name.toLowerCase()) !== -1 ||
(p.tags &&
p.tags.some(tag =>
tag.toLowerCase().includes(filter.name.toLowerCase())
))
)
)
export const getSortedExternalPlugins = createSelector(
getPluginSorting,
getFilteredExternalPlugins,
(pluginSorting, externalPlugins) => {
externalPlugins = externalPlugins
.sort((a, b) =>
a.displayName.toLowerCase().localeCompare(b.displayName.toLowerCase())
)
.sort((a, b) => b.working - a.working)
switch (pluginSorting) {
case 'active installs':
return externalPlugins.sort((a, b) => b.count - a.count)
case 'time updated':
return externalPlugins.sort((a, b) => b.lastUpdatedAt - a.lastUpdatedAt)
case 'time added':
return externalPlugins.sort((a, b) => b.createdAt - a.createdAt)
case 'installed':
return externalPlugins.sort((a, b) =>
a.installed === b.installed ? 0 : a.installed ? -1 : 1
)
case 'name':
default:
return externalPlugins
}
}
)