about summary refs log tree commit diff
path: root/bskyogcard/src/context.ts
blob: f92651cafbbc829d9dce2eefccedae27ec06ac9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import {readFileSync} from 'node:fs'

import {AtpAgent} from '@atproto/api'
import * as path from 'path'
import {fileURLToPath} from 'url'

import {Config} from './config.js'

const __DIRNAME = path.dirname(fileURLToPath(import.meta.url))

export type AppContextOptions = {
  cfg: Config
  appviewAgent: AtpAgent
  fonts: {name: string; data: Buffer}[]
}

export class AppContext {
  cfg: Config
  appviewAgent: AtpAgent
  fonts: {name: string; data: Buffer}[]
  abortController = new AbortController()

  constructor(private opts: AppContextOptions) {
    this.cfg = this.opts.cfg
    this.appviewAgent = this.opts.appviewAgent
    this.fonts = this.opts.fonts
  }

  static async fromConfig(cfg: Config, overrides?: Partial<AppContextOptions>) {
    const appviewAgent = new AtpAgent({service: cfg.service.appviewUrl})
    const fonts = [
      {
        name: 'Inter',
        data: readFileSync(path.join(__DIRNAME, 'assets', 'Inter-Bold.ttf')),
      },
    ]
    return new AppContext({
      cfg,
      appviewAgent,
      fonts,
      ...overrides,
    })
  }
}