UNPKG

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