UNPKG

6.54 kBJavaScriptView Raw
1import { getIfUpdate } from "@xmcl/net";
2import * as parser from "fast-html-parser";
3import { ForgeInstaller } from "./index";
4export var ForgeWebPage;
5(function (ForgeWebPage) {
6 /**
7 * Parse the html string of forge webpage
8 */
9 function parse(content) {
10 const dom = parser.parse(content);
11 const selected = dom.querySelector(".elem-active");
12 const mcversion = selected.text;
13 return {
14 timestamp: "",
15 mcversion,
16 versions: dom.querySelector(".download-list").querySelector("tbody").querySelectorAll("tr")
17 .map((e) => {
18 const links = e.querySelector(".download-links").childNodes
19 .filter((elem) => elem.tagName === "li")
20 .map((elem) => {
21 elem = elem.removeWhitespace();
22 /*
23 * <div class="info-tooltip">
24 * <strong>MD5:</strong> 31742b6c996f53af96f606b7a0c46e2a<br>
25 * <strong>SHA1:</strong> 8d6a23554839d6f6014fbdb7991e3cd8af7eca80
26 * </div>
27 */
28 const tooltipInfo = elem.querySelector(".info-tooltip");
29 const url = tooltipInfo.querySelector("a") || elem.querySelector("a");
30 // href is like /maven/net/minecraftforge/forge/1.14.4-28.1.70/forge-1.14.4-28.1.70-changelog.txt
31 const href = url.attributes.href.trim();
32 const matched = /forge-.+-.+-(\w+)\.\w+/.exec(href);
33 let name = "", sha1 = "", md5 = "";
34 if (matched) {
35 name = matched[1];
36 }
37 if (!name) {
38 throw new Error(`Cannot determine name for forge url "${href}". Maybe the forge webisite changed?`);
39 }
40 try {
41 md5 = tooltipInfo.childNodes[1].text.trim();
42 sha1 = tooltipInfo.childNodes[4].text.trim();
43 }
44 catch (_a) {
45 console.warn(`Error during fetching the sha1 and md5 for the forge "${href}". The result might be wrong.`);
46 }
47 const isSha1 = /\b([a-f0-9]{40})\b/i;
48 const isMd5 = /\b[a-f0-9]{32}\b/i;
49 if (!isMd5.test(md5.trim())) {
50 console.warn(`Illegal Md5 for "${href}": ${md5}`);
51 md5 = "";
52 }
53 if (!isSha1.test(sha1.trim())) {
54 console.warn(`Illegal Sha1 for "${href}": ${sha1}`);
55 sha1 = "";
56 }
57 return {
58 name,
59 md5,
60 sha1,
61 path: href,
62 };
63 });
64 const downloadVersionElem = e.querySelector(".download-version");
65 let version;
66 let type = "common";
67 const icon = downloadVersionElem.querySelector("i");
68 if (icon) {
69 if (icon.classNames.indexOf("promo-recommended") !== -1) {
70 type = "recommended";
71 }
72 else if (icon.classNames.indexOf("promo-latest") !== -1) {
73 type = "latest";
74 }
75 else if (icon.classNames.indexOf("fa-bug") !== -1) {
76 type = "buggy";
77 }
78 version = downloadVersionElem.firstChild.text.trim();
79 }
80 else {
81 version = downloadVersionElem.text.trim();
82 }
83 const installer = links.find((l) => l.name === "installer");
84 const universal = links.find((l) => l.name === "universal");
85 const changelog = links.find((l) => l.name === "changelog");
86 const installerWin = links.find((l) => l.name === "installer-win");
87 const source = links.find((l) => l.name === "source");
88 const launcher = links.find((l) => l.name === "launcher");
89 const mdk = links.find((l) => l.name === "mdk");
90 if (installer === undefined || universal === undefined) {
91 throw new Error("Cannot parse forge web since it missing installer and universal jar info.");
92 }
93 const result = {
94 version,
95 "date": e.querySelector(".download-time").text.trim(),
96 changelog,
97 installer,
98 mdk,
99 universal,
100 source,
101 launcher,
102 "installer-win": installerWin,
103 "mcversion": mcversion,
104 type,
105 };
106 return result;
107 }),
108 };
109 }
110 ForgeWebPage.parse = parse;
111 /**
112 * Query the webpage content from files.minecraftforge.net.
113 *
114 * You can put the last query result to the fallback option. It will check if your old result is up-to-date.
115 * It will request a new page only when the fallback option is outdated.
116 *
117 * @param option The option can control querying minecraft version, and page caching.
118 */
119 async function getWebPage(option = {}) {
120 const mcversion = option.mcversion || "";
121 const url = mcversion === "" ? "http://files.minecraftforge.net/maven/net/minecraftforge/forge/index.html" : `http://files.minecraftforge.net/maven/net/minecraftforge/forge/index_${mcversion}.html`;
122 const page = await getIfUpdate(url, parse, option.fallback);
123 return page;
124 }
125 ForgeWebPage.getWebPage = getWebPage;
126 let Version;
127 (function (Version) {
128 function to(webPageVersion) {
129 return {
130 universal: webPageVersion.universal,
131 installer: webPageVersion.installer,
132 mcversion: webPageVersion.mcversion,
133 version: webPageVersion.version,
134 };
135 }
136 Version.to = to;
137 })(Version = ForgeWebPage.Version || (ForgeWebPage.Version = {}));
138})(ForgeWebPage || (ForgeWebPage = {}));
139ForgeInstaller.VersionMeta = ForgeInstaller.VersionMeta || {};
140ForgeInstaller.VersionMeta.from = ForgeWebPage.Version.to;
141//# sourceMappingURL=forgeweb.js.map
\No newline at end of file