~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
108 lines · 5.4 KB Raw History
// The environment `pds serve` is started with (spec/PDS.md, "What it serves",
// spec/ACCOUNTS.md, spec/PROXY.md, and spec/OAUTH-CLIENTS.md's development fetch).

import { Secp256k1Keypair } from '@atproto/crypto'
import { CDN_PATTERN } from './services.mjs'

/// The domains handles may end in, as the reference's `availableUserDomains`.
export const HANDLE_DOMAINS = ['.test', '.example']

/// The server's `PDS_HOSTNAME`, which its emails name (spec/EMAIL.md, "The message").
export const HOSTNAME = 'localhost'

/// The reference's admin password, which the server is given too
/// (spec/LIMITS.md, "Environment", "The admin").
export const ADMIN_PASSWORD = 'admin-pass'

/// The import size both sides are started with (spec/LIMITS.md,
/// PDS_MAX_REPO_IMPORT_SIZE): small, so that a body just past it is cheap
/// to send.
export const MAX_REPO_IMPORT = 1048576

/// The upload limit both sides are started with in --limits
/// (spec/LIMITS.md, PDS_BLOB_UPLOAD_LIMIT): past the default 5 MiB, and
/// not a round number, so that a server keeping the default, or rounding,
/// is seen.
export const LIMITS_UPLOAD_LIMIT = 6291459

/// The proxies the reference trusts in `X-Forwarded-For` (Express's
/// `loopback`, `linklocal` and `uniquelocal`), which the server is told to
/// trust too, so that both read the harness's headers alike
/// (spec/LIMITS.md, "Environment", "The client address").
export const REFERENCE_TRUSTED_PROXIES = '127.0.0.0/8,::1/128,169.254.0.0/16,fe80::/10,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,fc00::/7'

/// The server's environment, to listen on `port`, keep its data in
/// `dataDir`, and register DIDs with the PLC directory at `plc`, with a new
/// rotation key and `recoveryKey`, if given, before it, telling `crawlers`,
/// if given, of itself, forwarding to the AppView `appView`, if given,
/// `{url, did}`, and fetching OAuth clients' documents through
/// `clientFetch`, if given; with either, without SSRF protection, as the
/// reference runs. Answers it, and the rotation key's `did:key`. It trusts
/// the proxies the reference trusts. With
/// `limits`, its rate limits are on, its uploads limited to
/// `LIMITS_UPLOAD_LIMIT` bytes, and its DID documents kept as long as
/// it likes; without, as the reference runs, off, and every `describeRepo`
/// asks the directory. With `invites`, invite codes are required. With
/// `backfillMs`, the firehose's backfill window is that, and with
/// `cacheBytes`, its cache that many bytes (spec/STORE.md, "Environment").
/// With `lexiconAuthority`, a DID, every NSID's lexicon is resolved from
/// that DID's repository (spec/OAUTH-PERMISSION-SETS.md). With `handles`,
/// the harness's handle service, `{dns, fetch}`, handles are resolved
/// through it (spec/IDENTITY.md, "Environment"). With `blobstore`,
/// `{url, bucket, accessKeyId, secretAccessKey, timeoutMs}`, its blobs are
/// kept in that bucket of the S3 store at `url`, path-style, with that
/// timeout for uploads and every other request (spec/BLOBS.md). With
/// `smtp`, `{url, from}`, it sends each email through the SMTP server at
/// `url`, from the address `from` (spec/EMAIL.md, "SMTP").
export async function serverEnv({ port, dataDir, plc, recoveryKey, crawlers, appView, clientFetch, handles, limits = false, invites = false, backfillMs, cacheBytes, lexiconAuthority, blobstore, smtp }) {
  const rotation = await Secp256k1Keypair.create({ exportable: true })
  const env = {
    PDS_PORT: String(port),
    PDS_DATA_DIR: dataDir,
    PDS_HOSTNAME: HOSTNAME,
    PDS_PUBLIC_URL: `http://127.0.0.1:${port}`,
    PDS_HANDLE_DOMAINS: HANDLE_DOMAINS.join(','),
    PDS_PLC_URL: plc,
    PDS_PLC_ROTATION_KEY_HEX: Buffer.from(await rotation.export()).toString('hex'),
    PDS_ADMIN_PASSWORD: ADMIN_PASSWORD,
    PDS_INVITE_REQUIRED: String(invites),
    PDS_RATE_LIMITS_ENABLED: String(limits),
    PDS_TRUSTED_PROXIES: REFERENCE_TRUSTED_PROXIES,
    PDS_MAX_REPO_IMPORT_SIZE: String(MAX_REPO_IMPORT),
  }
  if (limits) env.PDS_BLOB_UPLOAD_LIMIT = String(LIMITS_UPLOAD_LIMIT)
  else env.PDS_DID_CACHE_SECS = '0'
  if (recoveryKey) env.PDS_RECOVERY_DID_KEY = recoveryKey
  if (crawlers?.length) env.PDS_CRAWLERS = crawlers.join(',')
  if (appView) {
    env.PDS_BSKY_APP_VIEW_URL = appView.url
    env.PDS_BSKY_APP_VIEW_DID = appView.did
    env.PDS_BSKY_APP_VIEW_CDN_URL_PATTERN = CDN_PATTERN
  }
  if (clientFetch) env.PDS_OAUTH_CLIENT_FETCH = clientFetch
  if (handles) {
    env.PDS_HANDLE_DNS_SERVER = handles.dns
    env.PDS_HANDLE_FETCH = handles.fetch
  }
  // The services, and the development fetches' webs, are on this machine (spec/PROXY.md, spec/OAUTH-CLIENTS.md, spec/IDENTITY.md).
  if (appView || clientFetch || handles) env.PDS_DISABLE_SSRF_PROTECTION = 'true'
  if (backfillMs) env.PDS_REPO_BACKFILL_LIMIT_MS = String(backfillMs)
  if (cacheBytes) env.PDS_CACHE_BYTES = String(cacheBytes)
  if (lexiconAuthority) env.PDS_LEXICON_AUTHORITY_DID = lexiconAuthority
  if (blobstore) {
    Object.assign(env, {
      PDS_BLOBSTORE_S3_BUCKET: blobstore.bucket,
      PDS_BLOBSTORE_S3_ENDPOINT: blobstore.url,
      PDS_BLOBSTORE_S3_FORCE_PATH_STYLE: 'true',
      PDS_BLOBSTORE_S3_ACCESS_KEY_ID: blobstore.accessKeyId,
      PDS_BLOBSTORE_S3_SECRET_ACCESS_KEY: blobstore.secretAccessKey,
      PDS_BLOBSTORE_S3_UPLOAD_TIMEOUT_MS: String(blobstore.timeoutMs),
      PDS_BLOBSTORE_S3_REQUEST_TIMEOUT_MS: String(blobstore.timeoutMs),
    })
  }
  if (smtp) {
    env.PDS_EMAIL_SMTP_URL = smtp.url
    env.PDS_EMAIL_FROM = smtp.from
  }
  return { env, rotationKey: rotation.did() }
}