forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {readdirSync, readFileSync} from 'node:fs'
2import * as path from 'node:path'
3import {fileURLToPath} from 'node:url'
4
5import {AtpAgent} from '@atproto/api'
6
7import {Config} from './config.js'
8
9const __DIRNAME = path.dirname(fileURLToPath(import.meta.url))
10
11export type AppContextOptions = {
12 cfg: Config
13 appviewAgent: AtpAgent
14 fonts: {name: string; data: Buffer}[]
15}
16
17export class AppContext {
18 cfg: Config
19 appviewAgent: AtpAgent
20 fonts: {name: string; data: Buffer}[]
21 abortController = new AbortController()
22
23 constructor(private opts: AppContextOptions) {
24 this.cfg = this.opts.cfg
25 this.appviewAgent = this.opts.appviewAgent
26 this.fonts = this.opts.fonts
27 }
28
29 static async fromConfig(cfg: Config, overrides?: Partial<AppContextOptions>) {
30 const appviewAgent = new AtpAgent({service: cfg.service.appviewUrl})
31 const fontDirectory = path.join(__DIRNAME, 'assets', 'fonts')
32 const fontFiles = readdirSync(fontDirectory)
33 const fonts = fontFiles.map(file => {
34 return {
35 name: path.basename(file, path.extname(file)),
36 data: readFileSync(path.join(fontDirectory, file)),
37 }
38 })
39 return new AppContext({
40 cfg,
41 appviewAgent,
42 fonts,
43 ...overrides,
44 })
45 }
46}