kopia lustrzana https://github.com/cloudflare/wildebeest
improve typing around getDataBase
rodzic
279431fcdf
commit
f20fb8052b
|
@ -1,6 +1,6 @@
|
||||||
import { type Database } from 'wildebeest/backend/src/database'
|
import { type Database } from 'wildebeest/backend/src/database'
|
||||||
import type { Env } from 'wildebeest/backend/src/types/env'
|
import type { Env } from 'wildebeest/backend/src/types/env'
|
||||||
|
|
||||||
export default function make(env: Env): Database {
|
export default function make({ DATABASE }: Pick<Env, 'DATABASE'>): Database {
|
||||||
return env.DATABASE
|
return DATABASE
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,6 @@ export interface PreparedStatement {
|
||||||
raw<T = unknown>(): Promise<T[]>
|
raw<T = unknown>(): Promise<T[]>
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDatabase(env: Env): Database {
|
export function getDatabase(env: Pick<Env, 'DATABASE'>): Database {
|
||||||
return d1(env)
|
return d1(env)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,12 +8,12 @@ import { deliverToActor } from 'wildebeest/backend/src/activitypub/deliver'
|
||||||
|
|
||||||
export async function handleDeliverMessage(env: Env, actor: Actor, message: DeliverMessageBody) {
|
export async function handleDeliverMessage(env: Env, actor: Actor, message: DeliverMessageBody) {
|
||||||
const toActorId = new URL(message.toActorId)
|
const toActorId = new URL(message.toActorId)
|
||||||
const targetActor = await actors.getAndCache(toActorId, getDatabase(env as any))
|
const targetActor = await actors.getAndCache(toActorId, getDatabase(env))
|
||||||
if (targetActor === null) {
|
if (targetActor === null) {
|
||||||
console.warn(`actor ${toActorId} not found`)
|
console.warn(`actor ${toActorId} not found`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const signingKey = await getSigningKey(message.userKEK, getDatabase(env as any), actor)
|
const signingKey = await getSigningKey(message.userKEK, getDatabase(env), actor)
|
||||||
await deliverToActor(signingKey, actor, targetActor, message.activity, env.DOMAIN)
|
await deliverToActor(signingKey, actor, targetActor, message.activity, env.DOMAIN)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import type { Env } from './'
|
||||||
|
|
||||||
export async function handleInboxMessage(env: Env, actor: Actor, message: InboxMessageBody) {
|
export async function handleInboxMessage(env: Env, actor: Actor, message: InboxMessageBody) {
|
||||||
const domain = env.DOMAIN
|
const domain = env.DOMAIN
|
||||||
const db = getDatabase(env as any)
|
const db = getDatabase(env)
|
||||||
const adminEmail = env.ADMIN_EMAIL
|
const adminEmail = env.ADMIN_EMAIL
|
||||||
const cache = cacheFromEnv(env)
|
const cache = cacheFromEnv(env)
|
||||||
const activity = message.activity
|
const activity = message.activity
|
||||||
|
|
|
@ -20,7 +20,7 @@ export type Env = {
|
||||||
export default {
|
export default {
|
||||||
async queue(batch: MessageBatch<MessageBody>, env: Env, ctx: ExecutionContext) {
|
async queue(batch: MessageBatch<MessageBody>, env: Env, ctx: ExecutionContext) {
|
||||||
const sentry = initSentryQueue(env, ctx)
|
const sentry = initSentryQueue(env, ctx)
|
||||||
const db = getDatabase(env as any)
|
const db = getDatabase(env)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (const message of batch.messages) {
|
for (const message of batch.messages) {
|
||||||
|
|
|
@ -14,7 +14,7 @@ export const clientLoader = loader$<Promise<Client>, { DATABASE: D1Database }>(a
|
||||||
const client_id = query.get('client_id') || ''
|
const client_id = query.get('client_id') || ''
|
||||||
let client: Client | null = null
|
let client: Client | null = null
|
||||||
try {
|
try {
|
||||||
client = await getClientById(getDatabase(platform as any), client_id)
|
client = await getClientById(getDatabase(platform), client_id)
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
const error = e as { stack: string; cause: string }
|
const error = e as { stack: string; cause: string }
|
||||||
console.warn(error.stack, error.cause)
|
console.warn(error.stack, error.cause)
|
||||||
|
@ -49,10 +49,10 @@ export const userLoader = loader$<
|
||||||
throw html(500, getErrorHtml("The Access JWT doesn't contain an email"))
|
throw html(500, getErrorHtml("The Access JWT doesn't contain an email"))
|
||||||
}
|
}
|
||||||
|
|
||||||
const person = await getPersonByEmail(getDatabase(platform as any), payload.email)
|
const person = await getPersonByEmail(getDatabase(platform), payload.email)
|
||||||
if (person === null) {
|
if (person === null) {
|
||||||
const isFirstLogin = true
|
const isFirstLogin = true
|
||||||
const res = await buildRedirect(getDatabase(platform as any), request as Request, isFirstLogin, jwt.value)
|
const res = await buildRedirect(getDatabase(platform), request as Request, isFirstLogin, jwt.value)
|
||||||
if (res.status === 302) {
|
if (res.status === 302) {
|
||||||
throw redirect(302, res.headers.get('location') || '')
|
throw redirect(302, res.headers.get('location') || '')
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { component$ } from '@builder.io/qwik'
|
import { component$ } from '@builder.io/qwik'
|
||||||
import { getDatabase } from 'wildebeest/backend/src/database'
|
import { Database, getDatabase } from 'wildebeest/backend/src/database'
|
||||||
import { MastodonStatus, StatusContext } from '~/types'
|
import { MastodonStatus, StatusContext } from '~/types'
|
||||||
import Status from '~/components/Status'
|
import Status from '~/components/Status'
|
||||||
import * as statusAPI from 'wildebeest/functions/api/v1/statuses/[id]'
|
import * as statusAPI from 'wildebeest/functions/api/v1/statuses/[id]'
|
||||||
|
@ -13,13 +13,13 @@ import { Person } from 'wildebeest/backend/src/activitypub/actors'
|
||||||
|
|
||||||
export const statusLoader = loader$<
|
export const statusLoader = loader$<
|
||||||
Promise<{ status: MastodonStatus; statusTextContent: string; context: StatusContext }>,
|
Promise<{ status: MastodonStatus; statusTextContent: string; context: StatusContext }>,
|
||||||
{ DATABASE: D1Database }
|
{ DATABASE: Database }
|
||||||
>(async ({ request, html, platform, params }) => {
|
>(async ({ request, html, platform, params }) => {
|
||||||
const domain = new URL(request.url).hostname
|
const domain = new URL(request.url).hostname
|
||||||
let statusText = ''
|
let statusText = ''
|
||||||
try {
|
try {
|
||||||
const statusResponse = await statusAPI.handleRequestGet(
|
const statusResponse = await statusAPI.handleRequestGet(
|
||||||
getDatabase(platform as any),
|
getDatabase(platform),
|
||||||
params.statusId,
|
params.statusId,
|
||||||
domain,
|
domain,
|
||||||
{} as Person
|
{} as Person
|
||||||
|
@ -37,7 +37,7 @@ export const statusLoader = loader$<
|
||||||
const statusTextContent = await getTextContent(status.content)
|
const statusTextContent = await getTextContent(status.content)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const contextResponse = await contextAPI.handleRequest(domain, getDatabase(platform as any), params.statusId)
|
const contextResponse = await contextAPI.handleRequest(domain, getDatabase(platform), params.statusId)
|
||||||
const contextText = await contextResponse.text()
|
const contextText = await contextResponse.text()
|
||||||
const context = JSON.parse(contextText ?? null) as StatusContext | null
|
const context = JSON.parse(contextText ?? null) as StatusContext | null
|
||||||
if (!context) {
|
if (!context) {
|
||||||
|
|
|
@ -22,7 +22,7 @@ export const statusesLoader = loader$<
|
||||||
|
|
||||||
const handle = parseHandle(accountId)
|
const handle = parseHandle(accountId)
|
||||||
accountId = handle.localPart
|
accountId = handle.localPart
|
||||||
const response = await getLocalStatuses(request as Request, getDatabase(platform as any), handle, 0, false)
|
const response = await getLocalStatuses(request as Request, getDatabase(platform), handle, 0, false)
|
||||||
statuses = await response.json<Array<MastodonStatus>>()
|
statuses = await response.json<Array<MastodonStatus>>()
|
||||||
} catch {
|
} catch {
|
||||||
throw html(
|
throw html(
|
||||||
|
|
|
@ -25,14 +25,14 @@ export const accountPageLoader = loader$<
|
||||||
const accountId = url.pathname.split('/')[1]
|
const accountId = url.pathname.split('/')[1]
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const statusResponse = await statusAPI.handleRequestGet(getDatabase(platform as any), params.statusId, domain)
|
const statusResponse = await statusAPI.handleRequestGet(getDatabase(platform), params.statusId, domain)
|
||||||
const statusText = await statusResponse.text()
|
const statusText = await statusResponse.text()
|
||||||
isValidStatus = !!statusText
|
isValidStatus = !!statusText
|
||||||
} catch {
|
} catch {
|
||||||
isValidStatus = false
|
isValidStatus = false
|
||||||
}
|
}
|
||||||
|
|
||||||
account = await getAccount(domain, accountId, getDatabase(platform as any))
|
account = await getAccount(domain, accountId, getDatabase(platform))
|
||||||
} catch {
|
} catch {
|
||||||
throw html(
|
throw html(
|
||||||
500,
|
500,
|
||||||
|
|
|
@ -23,7 +23,7 @@ export const statusesLoader = loader$<
|
||||||
|
|
||||||
const handle = parseHandle(accountId)
|
const handle = parseHandle(accountId)
|
||||||
accountId = handle.localPart
|
accountId = handle.localPart
|
||||||
const response = await getLocalStatuses(request as Request, getDatabase(platform as any), handle, 0, true)
|
const response = await getLocalStatuses(request as Request, getDatabase(platform), handle, 0, true)
|
||||||
statuses = await response.json<Array<MastodonStatus>>()
|
statuses = await response.json<Array<MastodonStatus>>()
|
||||||
} catch {
|
} catch {
|
||||||
throw html(
|
throw html(
|
||||||
|
|
|
@ -11,7 +11,7 @@ export const statusesLoader = loader$<Promise<MastodonStatus[]>, { DATABASE: D1D
|
||||||
async ({ platform, html }) => {
|
async ({ platform, html }) => {
|
||||||
try {
|
try {
|
||||||
// TODO: use the "trending" API endpoint here.
|
// TODO: use the "trending" API endpoint here.
|
||||||
const response = await timelines.handleRequest(platform.domain, getDatabase(platform as any))
|
const response = await timelines.handleRequest(platform.domain, getDatabase(platform))
|
||||||
const results = await response.text()
|
const results = await response.text()
|
||||||
// Manually parse the JSON to ensure that Qwik finds the resulting objects serializable.
|
// Manually parse the JSON to ensure that Qwik finds the resulting objects serializable.
|
||||||
return JSON.parse(results) as MastodonStatus[]
|
return JSON.parse(results) as MastodonStatus[]
|
||||||
|
|
|
@ -12,7 +12,7 @@ export const statusesLoader = loader$<Promise<MastodonStatus[]>, { DATABASE: D1D
|
||||||
async ({ platform, html }) => {
|
async ({ platform, html }) => {
|
||||||
try {
|
try {
|
||||||
// TODO: use the "trending" API endpoint here.
|
// TODO: use the "trending" API endpoint here.
|
||||||
const response = await timelines.handleRequest(platform.domain, getDatabase(platform as any))
|
const response = await timelines.handleRequest(platform.domain, getDatabase(platform))
|
||||||
const results = await response.text()
|
const results = await response.text()
|
||||||
// Manually parse the JSON to ensure that Qwik finds the resulting objects serializable.
|
// Manually parse the JSON to ensure that Qwik finds the resulting objects serializable.
|
||||||
return JSON.parse(results) as MastodonStatus[]
|
return JSON.parse(results) as MastodonStatus[]
|
||||||
|
|
|
@ -12,7 +12,7 @@ export const statusesLoader = loader$<Promise<MastodonStatus[]>, { DATABASE: D1D
|
||||||
async ({ platform, html }) => {
|
async ({ platform, html }) => {
|
||||||
try {
|
try {
|
||||||
// TODO: use the "trending" API endpoint here.
|
// TODO: use the "trending" API endpoint here.
|
||||||
const response = await timelines.handleRequest(platform.domain, getDatabase(platform as any), { local: true })
|
const response = await timelines.handleRequest(platform.domain, getDatabase(platform), { local: true })
|
||||||
const results = await response.text()
|
const results = await response.text()
|
||||||
// Manually parse the JSON to ensure that Qwik finds the resulting objects serializable.
|
// Manually parse the JSON to ensure that Qwik finds the resulting objects serializable.
|
||||||
return JSON.parse(results) as MastodonStatus[]
|
return JSON.parse(results) as MastodonStatus[]
|
||||||
|
|
Ładowanie…
Reference in New Issue