Something went wrong. Try again.
your personal website on atproto - mirror blento.app
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267import { error } from '@sveltejs/kit';import { command, getRequestEvent } from '$app/server';import * as v from 'valibot';import { isAllowedCollection, isAllowedOrigin } from './allowlist';import { contrail, ensureInit } from '$lib/contrail';
const originSchema = v.string();
const collectionSchema = v.pipe( v.string(), v.regex(/^[a-zA-Z][a-zA-Z0-9-]*(\.[a-zA-Z][a-zA-Z0-9-]*){2,}$/));
const rkeySchema = v.pipe(v.string(), v.regex(/^[a-zA-Z0-9._:~-]{1,512}$/));
const recordSchema = v.record(v.string(), v.unknown());
const writeSchema = v.union([ v.object({ $type: v.literal('create'), collection: collectionSchema, rkey: v.optional(rkeySchema), value: recordSchema }), v.object({ $type: v.literal('update'), collection: collectionSchema, rkey: rkeySchema, value: recordSchema }), v.object({ $type: v.literal('delete'), collection: collectionSchema, rkey: rkeySchema })]);
function requireAuth() { const { locals } = getRequestEvent(); if (!locals.client || !locals.did) error(401, 'no_session'); return { client: locals.client, did: locals.did };}
function checkOrigin(origin: string) { if (!isAllowedOrigin(origin)) error(403, 'origin_not_allowed');}
function checkCollection(origin: string, collection: string) { if (!isAllowedCollection(origin, collection)) error(403, 'collection_not_allowed');}
async function notifyContrail(uri: string) { const { platform } = getRequestEvent(); const db = platform?.env?.DB; if (!db) return; await ensureInit(db); await contrail.notify(uri, db).catch(() => {});}
export const embedCreateRecord = command( v.object({ origin: originSchema, collection: collectionSchema, rkey: v.optional(rkeySchema), record: recordSchema }), async ({ origin, collection, rkey, record }) => { const { client, did } = requireAuth(); checkOrigin(origin); checkCollection(origin, collection);
const response = await client.post('com.atproto.repo.createRecord', { input: { collection: collection as `${string}.${string}.${string}`, repo: did, rkey, record } });
if (!response.ok) { console.error('embedCreateRecord failed', { origin, collection, status: response.status, data: response.data }); error(502, 'pds_error'); }
await notifyContrail(response.data.uri);
return { uri: response.data.uri, cid: response.data.cid }; });
export const embedPutRecord = command( v.object({ origin: originSchema, collection: collectionSchema, rkey: rkeySchema, record: recordSchema }), async ({ origin, collection, rkey, record }) => { const { client, did } = requireAuth(); checkOrigin(origin); checkCollection(origin, collection);
const valueWithType = record.$type === collection ? record : { ...record, $type: collection };
const response = await client.post('com.atproto.repo.putRecord', { input: { collection: collection as `${string}.${string}.${string}`, repo: did, rkey, record: valueWithType } });
if (!response.ok) { console.error('embedPutRecord failed', { origin, collection, rkey, status: response.status, data: response.data }); error(502, 'pds_error'); }
await notifyContrail(response.data.uri);
return { uri: response.data.uri, cid: response.data.cid }; });
export const embedDeleteRecord = command( v.object({ origin: originSchema, collection: collectionSchema, rkey: rkeySchema }), async ({ origin, collection, rkey }) => { const { client, did } = requireAuth(); checkOrigin(origin); checkCollection(origin, collection);
const response = await client.post('com.atproto.repo.deleteRecord', { input: { collection: collection as `${string}.${string}.${string}`, repo: did, rkey } });
if (response.ok) { await notifyContrail(`at://${did}/${collection}/${rkey}`); }
return { ok: response.ok }; });
export const embedApplyWrites = command( v.object({ origin: originSchema, writes: v.array(writeSchema), validate: v.optional(v.boolean()) }), async ({ origin, writes, validate }) => { const { client, did } = requireAuth(); checkOrigin(origin); for (const w of writes) checkCollection(origin, w.collection);
const atprotoWrites = writes.map((w) => { if (w.$type === 'create') { return { $type: 'com.atproto.repo.applyWrites#create' as const, collection: w.collection as `${string}.${string}.${string}`, rkey: w.rkey, value: (w.value as { $type?: string }).$type === w.collection ? w.value : { ...w.value, $type: w.collection } }; } if (w.$type === 'update') { return { $type: 'com.atproto.repo.applyWrites#update' as const, collection: w.collection as `${string}.${string}.${string}`, rkey: w.rkey, value: (w.value as { $type?: string }).$type === w.collection ? w.value : { ...w.value, $type: w.collection } }; } return { $type: 'com.atproto.repo.applyWrites#delete' as const, collection: w.collection as `${string}.${string}.${string}`, rkey: w.rkey }; });
const response = await client.post('com.atproto.repo.applyWrites', { input: { repo: did, validate, writes: atprotoWrites } });
if (!response.ok) { console.error('embedApplyWrites failed', { origin, count: writes.length, status: response.status, data: response.data }); error(502, 'pds_error'); }
const results = response.data.results?.map((r) => ({ uri: 'uri' in r ? (r.uri as string | undefined) : undefined, cid: 'cid' in r ? (r.cid as string | undefined) : undefined })) ?? [];
for (const r of results) { if (r.uri) await notifyContrail(r.uri); }
return { results }; });
export const embedUploadBlob = command( v.object({ origin: originSchema, bytes: v.array(v.number()), mimeType: v.string() }), async ({ origin, bytes, mimeType }) => { const { client } = requireAuth(); checkOrigin(origin);
const blob = new Blob([new Uint8Array(bytes)], { type: mimeType });
const response = await client.post('com.atproto.repo.uploadBlob', { input: blob });
if (!response.ok) { console.error('embedUploadBlob failed', { origin, size: bytes.length, status: response.status, data: response.data }); error(502, 'pds_error'); }
return response.data.blob as { $type: 'blob'; ref: { $link: string }; mimeType: string; size: number; }; });