2022-11-23 23:12:25 +00:00
|
|
|
// @ts-expect-error unstorage needs to provide backwards-compatible subpath types
|
|
|
|
import _fs from 'unstorage/drivers/fs'
|
|
|
|
// @ts-expect-error unstorage needs to provide backwards-compatible subpath types
|
|
|
|
import _kv from 'unstorage/drivers/cloudflare-kv-http'
|
2023-01-16 21:52:50 +00:00
|
|
|
// @ts-expect-error unstorage needs to provide backwards-compatible subpath types
|
|
|
|
import _memory from 'unstorage/drivers/memory'
|
2022-12-22 02:12:37 +00:00
|
|
|
|
2023-01-01 19:30:39 +00:00
|
|
|
import { stringifyQuery } from 'ufo'
|
2022-11-23 23:12:25 +00:00
|
|
|
|
2022-12-18 23:13:01 +00:00
|
|
|
import { $fetch } from 'ofetch'
|
2022-11-23 23:12:25 +00:00
|
|
|
import type { Storage } from 'unstorage'
|
|
|
|
|
|
|
|
import cached from './cache-driver'
|
|
|
|
|
2022-11-15 15:48:23 +00:00
|
|
|
import type { AppInfo } from '~/types'
|
2022-11-23 23:12:25 +00:00
|
|
|
import { APP_NAME } from '~/constants'
|
2022-11-15 14:29:46 +00:00
|
|
|
|
2022-11-27 18:39:40 +00:00
|
|
|
const config = useRuntimeConfig()
|
2022-11-25 16:57:29 +00:00
|
|
|
|
2022-11-23 23:12:25 +00:00
|
|
|
const fs = _fs as typeof import('unstorage/dist/drivers/fs')['default']
|
|
|
|
const kv = _kv as typeof import('unstorage/dist/drivers/cloudflare-kv-http')['default']
|
2023-01-16 21:52:50 +00:00
|
|
|
const memory = _memory as typeof import('unstorage/dist/drivers/memory')['default']
|
2022-11-15 14:29:46 +00:00
|
|
|
|
2022-11-23 23:12:25 +00:00
|
|
|
const storage = useStorage() as Storage
|
|
|
|
|
2022-12-06 21:09:47 +00:00
|
|
|
if (config.storage.driver === 'fs') {
|
2022-11-30 21:17:20 +00:00
|
|
|
storage.mount('servers', fs({ base: config.storage.fsBase }))
|
2022-11-23 23:12:25 +00:00
|
|
|
}
|
2022-12-22 02:12:37 +00:00
|
|
|
else if (config.storage.driver === 'cloudflare') {
|
2022-11-23 23:12:25 +00:00
|
|
|
storage.mount('servers', cached(kv({
|
|
|
|
accountId: config.cloudflare.accountId,
|
|
|
|
namespaceId: config.cloudflare.namespaceId,
|
|
|
|
apiToken: config.cloudflare.apiToken,
|
|
|
|
})))
|
|
|
|
}
|
2023-01-16 21:52:50 +00:00
|
|
|
else if (config.storage.driver === 'memory') {
|
|
|
|
storage.mount('servers', memory())
|
|
|
|
}
|
2022-11-30 21:17:20 +00:00
|
|
|
|
2023-01-01 19:30:39 +00:00
|
|
|
export function getRedirectURI(origin: string, server: string) {
|
|
|
|
return `${origin}/api/${server}/oauth?${stringifyQuery({ origin })}`
|
2022-11-29 22:55:53 +00:00
|
|
|
}
|
2022-11-23 23:12:25 +00:00
|
|
|
|
2023-01-01 19:30:39 +00:00
|
|
|
async function fetchAppInfo(origin: string, server: string) {
|
2022-11-23 23:12:25 +00:00
|
|
|
const app: AppInfo = await $fetch(`https://${server}/api/v1/apps`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
2023-01-04 13:26:30 +00:00
|
|
|
client_name: APP_NAME + (config.public.env !== 'release' ? ` (${config.public.env})` : ''),
|
2022-11-29 22:55:53 +00:00
|
|
|
website: 'https://elk.zone',
|
2023-01-01 19:30:39 +00:00
|
|
|
redirect_uris: getRedirectURI(origin, server),
|
2022-11-23 23:12:25 +00:00
|
|
|
scopes: 'read write follow push',
|
|
|
|
},
|
2022-11-15 15:48:23 +00:00
|
|
|
})
|
2022-11-23 23:12:25 +00:00
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2023-01-01 19:30:39 +00:00
|
|
|
export async function getApp(origin: string, server: string) {
|
2023-01-17 10:44:15 +00:00
|
|
|
const host = origin.replace(/^https?:\/\//, '').replace(/[^\w\d]/g, '-')
|
|
|
|
const key = `servers:v2:${server}:${host}.json`.toLowerCase()
|
2022-11-23 23:12:25 +00:00
|
|
|
|
|
|
|
try {
|
2022-11-27 18:22:39 +00:00
|
|
|
if (await storage.hasItem(key))
|
|
|
|
return await storage.getItem(key) as Promise<AppInfo>
|
2023-01-01 19:30:39 +00:00
|
|
|
const appInfo = await fetchAppInfo(origin, server)
|
2022-11-23 23:12:25 +00:00
|
|
|
await storage.setItem(key, appInfo)
|
|
|
|
return appInfo
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
return null
|
|
|
|
}
|
2022-11-15 14:29:46 +00:00
|
|
|
}
|
2022-12-24 00:07:38 +00:00
|
|
|
|
2023-01-15 09:34:17 +00:00
|
|
|
export async function deleteApp(server: string) {
|
2023-01-17 10:44:15 +00:00
|
|
|
const keys = (await storage.getKeys(`servers:v2:${server}:`))
|
2023-01-15 09:34:17 +00:00
|
|
|
for (const key of keys)
|
|
|
|
await storage.removeItem(key)
|
|
|
|
}
|
|
|
|
|
2022-12-24 00:07:38 +00:00
|
|
|
export async function listServers() {
|
2023-01-17 10:44:15 +00:00
|
|
|
const keys = await storage.getKeys('servers:v2:')
|
2022-12-24 00:07:38 +00:00
|
|
|
const servers = new Set<string>()
|
|
|
|
for await (const key of keys) {
|
2023-01-17 10:44:15 +00:00
|
|
|
const id = key.split(':')[2]
|
2022-12-24 00:07:38 +00:00
|
|
|
if (id)
|
2022-12-24 00:51:45 +00:00
|
|
|
servers.add(id.toLocaleLowerCase())
|
2022-12-24 00:07:38 +00:00
|
|
|
}
|
|
|
|
return Array.from(servers).sort()
|
|
|
|
}
|