Last active
September 12, 2023 10:26
-
-
Save Half-Shot/ac4dba80888d3de9a01e2699d63a878c to your computer and use it in GitHub Desktop.
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
const TOML = require('@iarna/toml'); | |
// Promises are nicer. | |
const fs = require('fs/promises'); | |
async function updateInstance(instance) { | |
// Do not use var, ever. | |
try { | |
// Await await await | |
const wellKnownResponse = await fetch("https://" + instance.domain + "/.well-known/matrix/server"); | |
// You need to return these responses | |
// Use ?? to fall back to the old domain in case it fails.\ | |
const technicalDomain = await updateTechnicalDomain(wellKnownResponse) ?? instance.domain; | |
const versionResponse = await fetch("https://" + technicalDomain + "/_matrix/federation/v1/version"); | |
// Destructuring makes it easier on you. | |
const { server, version } = await updateInstanceServerAndVersion(versionResponse); | |
instance.server = server; | |
instance.version = version; | |
// I presume you want this to be part of instance. | |
instance.technicalDomain = technicalDomain; | |
// Return the instance back | |
return instance; | |
} catch (ex) { | |
throw Error(`Error when updating ${instance.title} ${ex}`); | |
} | |
} | |
async function updateTechnicalDomain(wellKnownResponse) { | |
if (!wellKnownResponse.ok) { | |
// Short circuiting is easier upon the eyes. | |
return null; | |
} | |
const response = await wellKnownResponse.json(); | |
if ('m.server' in response) { | |
console.log("Technical domain from response: " + response['m.server']) | |
return response['m.server']; | |
} else { | |
// No domain! | |
return null; | |
} | |
} | |
async function updateInstanceServerAndVersion(versionResponse) { | |
if (!versionResponse.ok) { | |
// Short circuiting is easier upon the eyes. | |
return null; | |
} | |
const response = await versionResponse.json(); | |
// TODO: You may want to check these values exist in the response? | |
console.log("Server software: " + response.server.name + " " + response.server.version); | |
return { | |
software: response.server.name, | |
version: response.server.version, | |
} | |
} | |
async function main() { | |
const data = await fs.readFile('./instances.toml', 'utf-8'); | |
const parsed = TOML.parse(data); | |
// Shorthand for a promise set from an array. | |
const promises = parsed.instances.map(instance => updateInstance(instance)); | |
for (const result of await Promise.allSettled(promises)) { | |
if (result.status === 'fulfilled') { | |
// Success! | |
// That's where I should dump parsed.instances back to a toml file, but I'm reaching this too early D: | |
} else { | |
// No dice | |
console.warn(`Failed to resolve server`, result.reason); | |
} | |
} | |
} | |
// Typical pattern for a script that allows you to execute async | |
// at top level. | |
main().then(() => { | |
console.log('Script completed with no errors'); | |
}).catch((ex) => { | |
process.exit = 1 | |
console.error('Script failed to run', ex); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment