Skip to content

Commit

Permalink
Merge pull request #1614 from mogita/master
Browse files Browse the repository at this point in the history
fix: misskey detection could fail due to an early axios throw
  • Loading branch information
h3poteto authored Mar 7, 2023
2 parents bf9c887 + fa53284 commit 75af240
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
30 changes: 21 additions & 9 deletions megalodon/src/megalodon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ type Instance = {

/**
* Detect SNS type.
* Now support Mastodon, Pleroma and Pixelfed.
* Now support Mastodon, Pleroma and Pixelfed. Throws an error when no known platform can be detected.
*
* @param url Base URL of SNS.
* @param proxyConfig Proxy setting, or set false if don't use proxy.
Expand All @@ -1352,16 +1352,28 @@ export const detector = async (url: string, proxyConfig: ProxyConfig | false = f
})
}

const res = await axios.get<Instance>(url + '/api/v1/instance', options)
if (res.data.title) {
if (res.data.pleroma) {
return 'pleroma'
try {
const res = await axios.get<Instance>(url + '/api/v1/instance', options)
if (res.data.title) {
if (res.data.pleroma) {
return 'pleroma'
} else {
return 'mastodon'
}
} else {
return 'mastodon'
throw new Error('no known platform could be detected')
}
} else {
await axios.post<{}>(url + '/api/meta', {}, options)
return 'misskey'
} catch (err: any) {
if (err && err.response && err.response.status === 404) {
try {
await axios.post<{}>(url + '/api/meta', {}, options)
return 'misskey'
} catch (err) {
throw new Error('no known platform could be detected')
}
}

throw err
}
}

Expand Down
8 changes: 8 additions & 0 deletions megalodon/test/integration/megalodon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ describe('detector', () => {
expect(misskey).toEqual('misskey')
})
})

describe('unknown', () => {
const url = 'https://google.com'
it('should be null', async () => {
const unknown = detector(url)
await expect(unknown).rejects.toThrow()
})
})
})

0 comments on commit 75af240

Please sign in to comment.