-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFileParser.js
More file actions
51 lines (44 loc) · 1.38 KB
/
FileParser.js
File metadata and controls
51 lines (44 loc) · 1.38 KB
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
import axios from 'axios';
import extractMeta from '../extractMeta.js';
export default class FileParser {
#updateURL;
#downloadURL;
constructor(updateURL, downloadURL) {
this.#updateURL = updateURL;
this.#downloadURL = downloadURL;
}
parseData(data) {
if (
typeof data === 'string' &&
data.includes('// ==UserScript==') &&
data.includes('// ==/UserScript==')
) {
return extractMeta(data);
}
return data;
}
parseVersion({ version } = {}) {
return version;
}
parseDownload({ downloadURL } = {}) {
return downloadURL;
}
async getUpdateData(url = this.#updateURL) {
const { data } = await axios.get(url);
return this.parseData(data);
}
async getVersion(data = this.getUpdateData()) {
const resolvedData = await Promise.resolve(data);
if (!resolvedData) throw new Error('File not found', this.#updateURL);
const version = this.parseVersion(resolvedData);
if (!version || typeof version !== 'string') throw new Error('Version not found');
return version;
}
async getDownload(data) {
if (this.#downloadURL) return this.#downloadURL;
const resolvedData = await Promise.resolve(data || this.getUpdateData());
const downloadURL = this.parseDownload(resolvedData);
if (!downloadURL || typeof downloadURL !== 'string') throw new Error('URL not found');
return downloadURL;
}
}