Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: misskey detection could fail due to an early axios throw #1614

Merged
merged 4 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})
})