Compare commits

...

2 commits

Author SHA1 Message Date
Bad
0113024be6 Switch to using .catch(()=>{}) instead of try catch
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2020-10-22 07:50:50 +02:00
Bad
1b97351ca0 Switch to using .catch(()=>{}) instead of try catch 2020-10-22 07:46:32 +02:00

View file

@ -71,6 +71,7 @@ class Form extends ElemJS {
this.on("submit", this.submit.bind(this)) this.on("submit", this.submit.bind(this))
} }
async submit() { async submit() {
if (this.processing) return if (this.processing) return
this.processing = true this.processing = true
@ -113,31 +114,32 @@ class Form extends ElemJS {
} }
async findHomeserver(address, maxDepth = 5) { async findHomeserver(address, maxDepth = 5) {
// Protects us from servers sending us on a redirect loop
//Protects us from servers sending us on a redirect loop
maxDepth-- maxDepth--
if (maxDepth <= 0) throw new Error(`Failed to look up homeserver, maximum search depth reached`) if(maxDepth<=0) throw new Error(`Failed to look up homeserver, maximum search depth reached`)
// Normalise the address //Preprocess the address
if (!address.match(/^https?:\/\//)) { if (!address.match(/^https?:\/\//)) {
console.warn(`${address} doesn't specify the protocol, assuming https`) console.warn(`${address} doesn't specify the protocol, assuming https`)
address = "https://" + address address = "https://" + address
} }
address = address.replace(/\/*$/, "") address = address.replace(/\/*$/, "")
this.status(`Looking up homeserver... trying ${address}`) this.status(`Looking up homeserver... trying ${address}`)
// Check if we found the actual matrix server // Check if we found the actual matrix server
try { const versionsReq = await fetch(`${address}/_matrix/client/versions`).catch(()=>{})
const versionsReq = await fetch(`${address}/_matrix/client/versions`) /* jshint ignore:start */
if (versionsReq.ok) { //JsHint doesn't support optional chaining
const versions = await versionsReq.json() // https://github.com/jshint/jshint/issues/3448
if (Array.isArray(versions.versions)) { if(versionsReq?.ok) {
return address const versions = await versionsReq.json().catch(()=>{})
} if (Array.isArray(versions?.versions)) return address
} }
} catch(e) {} /* jshint ignore:end */
// Find the next matrix server in the chain // find the next matrix server in the chain
const root = await fetch(`${address}/.well-known/matrix/client`).then(res => res.json()).catch(e => { const root = await fetch(`${address}/.well-known/matrix/client`).then(res => res.json()).catch(e => {
console.error(e) console.error(e)
throw new Error(`Failed to look up server ${address}`) throw new Error(`Failed to look up server ${address}`)
@ -153,6 +155,7 @@ class Form extends ElemJS {
return this.findHomeserver(nextAddress, maxDepth) return this.findHomeserver(nextAddress, maxDepth)
} }
status(message) { status(message) {
feedback.setLoading(true) feedback.setLoading(true)
feedback.message(message) feedback.message(message)
@ -166,3 +169,6 @@ class Form extends ElemJS {
} }
const form = new Form() const form = new Form()