Something went wrong. Try again.
message board on atproto atchan.net
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109import type { Handle } from '@sveltejs/kit/hooks'import type { Cookies } from '@sveltejs/kit'import { isExpectedSessionError } from '@atproto/oauth-client-node'import { LIVE_PATH, PREFLIGHT_PATH } from '#lib/live-protocol.js'import { restore_session } from '#lib/server/auth/oauth.js'import { SESSION_COOKIE, read_session_cookie, seal_session, session_cookie_options} from '#lib/server/auth/session.js'import { start_blob_cache_sweep } from '#lib/server/blob/blob-cache.js'import { start_handle_sweep } from '#lib/server/sweep/handle-sweep.js'import { start_session_sweep } from '#lib/server/sweep/session-sweep.js'import { start_preferences_sweep } from '#lib/server/sweep/preferences-sweep.js'import { start_indexer } from '#lib/server/indexer/index.js'import { start_label_consumer } from '#lib/server/indexer/labels.js'import { logger } from '#lib/server/log.js'import { DEFAULT_THEME, theme_attributes } from '#lib/ui/themes.js'import { CUSTOM_THEME_STYLE_ID, custom_theme_css} from '#lib/ui/custom-theme.js'
const log = logger('hooks')
start_blob_cache_sweep(5 * 60 * 1000)// hourly, not minutely: indexer/identity catches a move as it happens, and// this only has to cover the ones no #identity event reached us forstart_session_sweep(60 * 60 * 1000)start_handle_sweep(30 * 60 * 1000)start_preferences_sweep(24 * 60 * 60 * 1000)start_indexer().catch((e) => { log.error(e, 'failed to start the indexer')})start_label_consumer()
async function restore_from_cookie( cookies: Cookies, locals: App.Locals): Promise<void> { const sealed = cookies.get(SESSION_COOKIE) if (!sealed) return
const cookie = await read_session_cookie(sealed) if (!cookie) { cookies.delete(SESSION_COOKIE, { path: '/' }) return }
try { locals.session = await restore_session(cookie.user.did) } catch (err) { // a gone session clears the cookie; an unavailable one keeps it for the // next request if (isExpectedSessionError(err)) { cookies.delete(SESSION_COOKIE, { path: '/' }) } else { log.error(err, 'failed to restore session') } return }
locals.user = cookie.user
if (cookie.renew) { cookies.set( SESSION_COOKIE, await seal_session(cookie.user), session_cookie_options() ) }}
// The probes answer for the process, not for a visitor. Resolving a session on// the way in would make them depend on the account's PDS, which is exactly the// kind of outage they must stay answerable through. The live stream carries no// per-visitor data, so it skips the session too.export const handle: Handle = async ({ event, resolve }) => { const path = event.url.pathname if (path === '/readyz' || path === PREFLIGHT_PATH || path === LIVE_PATH) return resolve(event) if (event.route.id?.startsWith('/api/(media)/')) return resolve(event)
event.locals.user = null event.locals.session = null
await restore_from_cookie(event.cookies, event.locals)
const fallback = theme_attributes(DEFAULT_THEME) return resolve(event, { transformPageChunk: ({ html }) => { const theme = event.locals.theme if (!theme) return html const attributes = theme_attributes(theme) if (attributes !== fallback) html = html.replace(fallback, attributes)
const css = custom_theme_css(theme.custom) if (css) { html = html.replace( '</head>', `<style id="${CUSTOM_THEME_STYLE_ID}">${css}</style></head>` ) } return html } })}