-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attempt to fix version sorting (#205)
* rearrange version catalogue order * remove external import utility links * output errors in worker script
- Loading branch information
Showing
6 changed files
with
54 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,66 @@ | ||
import { xml2js } from "xml-js"; | ||
import * as semverSort from "semver-sort"; | ||
import semverSort from "semver-sort"; | ||
|
||
const META_GAME_VERSIONS = "https://meta.quiltmc.org/v3/versions/game" | ||
const MAVEN = "https://maven.quiltmc.org/repository/release/org/quiltmc/" | ||
|
||
async function fetchMetadata(path) { | ||
const metadataUrl = MAVEN + path + "/maven-metadata.xml" | ||
const metadataRequest = await fetch(metadataUrl, { headers: { "User-Agent": "QuiltMC Website API" } }) | ||
const metadataUrl = MAVEN + path + "/maven-metadata.xml"; | ||
const metadataRequest = await fetch(metadataUrl, { headers: { "User-Agent": "QuiltMC Website API" } }); | ||
|
||
return xml2js(await metadataRequest.text(), { compact: true, ignoreAttributes: true }).metadata | ||
if (metadataRequest.ok) { | ||
return await metadataRequest.text(); | ||
} | ||
|
||
throw new Error(`Failed to fetch metadata from ${metadataUrl}`); | ||
} | ||
|
||
function getLatestVersion(versions) { | ||
// handle null, undefined, empty array | ||
if (!versions) return null; | ||
|
||
return semverSort.asc(versions).toReversed()[0]; | ||
} | ||
|
||
function getLatestVersionFromMavenMetadataXML(metaXML, latestVersionSelector) { | ||
const json = xml2js(metaXML, { compact: true, ignoreAttributes: true }); | ||
|
||
const allVersions = json.metadata.versioning.versions.version.map((version) => version._text); | ||
|
||
return latestVersionSelector(allVersions); | ||
} | ||
|
||
export async function onRequest(context) { | ||
const gameVersionsRequest = await fetch(META_GAME_VERSIONS, { headers: { "User-Agent": "QuiltMC Website API" } }) | ||
const gameVersions = await gameVersionsRequest.json() | ||
try { | ||
const gameVersionsRequest = await fetch(META_GAME_VERSIONS, { headers: { "User-Agent": "QuiltMC Website API" } }) | ||
const gameVersions = await gameVersionsRequest.json() | ||
|
||
const output = {} | ||
const output = {} | ||
|
||
const loomVersion = (await fetchMetadata("loom")).versioning.latest._text | ||
const loaderVersion = (await fetchMetadata("quilt-loader")).versioning.latest._text | ||
const loomVersion = await fetchMetadata("loom").then(meta => getLatestVersionFromMavenMetadataXML(meta, getLatestVersion)); | ||
const loaderVersion = await fetchMetadata("quilt-loader").then(meta => getLatestVersionFromMavenMetadataXML(meta, getLatestVersion)); | ||
|
||
const mappingsVersions = (await fetchMetadata("quilt-mappings")) | ||
.versioning.versions.version.map((versioning) => versioning._text) | ||
.sort((a, b) => b.split(".").slice(-1) - a.split(".").slice(-1)) | ||
const mappingsVersions = await fetchMetadata("quilt-mappings").then(meta => getLatestVersionFromMavenMetadataXML(meta, versions => { | ||
return versions.sort((a, b) => b.split(".").slice(-1) - a.split(".").slice(-1)); | ||
})); | ||
|
||
const qfapiVersions = semverSort.asc((await fetchMetadata("quilted-fabric-api/quilted-fabric-api")) | ||
.versioning.versions.version.map((versioning) => versioning._text)) | ||
.toReversed() // semverSort.desc chokes on those inputs | ||
const qfapiVersions = await fetchMetadata("quilted-fabric-api/quilted-fabric-api").then(meta => getLatestVersionFromMavenMetadataXML(meta, versions => semverSort.asc(versions).toReversed())); | ||
|
||
for (const { version } of gameVersions) { | ||
const mappingsVersion = mappingsVersions.find((versioning) => versioning.split("+")[0] === version) || null | ||
const qfapiVersion = qfapiVersions.find((versioning) => versioning.split("-").at(-1) === version) || null | ||
for (const { version } of gameVersions) { | ||
const mappingsVersion = mappingsVersions.find((versioning) => versioning.split("+")[0] === version) || null | ||
const qfapiVersion = qfapiVersions.find((versioning) => versioning.split("-").at(-1) === version) || null | ||
|
||
output[version] = { | ||
"quilt_loader": loaderVersion, | ||
"loom": loomVersion, | ||
"quilt_mappings": mappingsVersion, | ||
"quilted_fabric_api": qfapiVersion | ||
output[version] = { | ||
"quilt_loader": loaderVersion, | ||
"loom": loomVersion, | ||
"quilt_mappings": mappingsVersion, | ||
"quilted_fabric_api": qfapiVersion | ||
} | ||
} | ||
} | ||
|
||
return new Response(JSON.stringify(output), { status: 200, headers: { "Content-Type": "text/json" } }) | ||
return new Response(JSON.stringify(output), { status: 200, headers: { "Content-Type": "text/json" } }); | ||
} | ||
catch (error) { | ||
return new Response(JSON.stringify({ error: error.message }), { status: 500, headers: { "Content-Type": "text/json" } }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0d1352b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See preview on Cloudflare Pages: https://24fb3b06.quiltmc-org.pages.dev