# Server Adapter House Style The server has five adapter families: **Secrets**, **KMS**, **Storage**, **CDN**, **Import** (read-only pull sources for `documents.importFrom`). They differ in _what they do_ but share _how they're structured_. This document is the contract a new adapter must follow. Unless stated otherwise, adapter source paths below are relative to `cloudpdf/server/src/`; test paths are relative to `cloudpdf/server/`. --- ## File layout Every family lives at the same shape: ``` / ├── .ts interface (role-noun, includes `info`) ├── create.ts factory function (switch on kind) ├── config/ │ ├── ConfigSchema.ts Zod discriminated union over `kind` │ └── loadConfigFromEnv.ts └── adapters/ ├── .ts one file per provider └── .ts ``` Examples: `cloudpdf/server/src/security/kms/adapters/AwsKmsKeyring.ts`, `cloudpdf/server/src/storage/adapters/S3ObjectStore.ts`, and `cloudpdf/server/src/cdn/adapters/BunnyCdnSigner.ts`. --- ## Interface convention ```typescript interface { readonly info: { kind: ; ...public_diagnostic_fields }; // family-specific methods } ``` Rules: - `kind` is the discriminator and matches the config Zod schema. - Other `info` fields are **public identifiers only** — bucket names, distribution domains, key IDs, hostnames. **Never secrets.** - Decorators (caching wrappers, etc.) add fields rather than mutating `kind`. Example: `info: { kind: 'aws-sm', cached: true }`, not `kind: 'aws-sm+cache'`. --- ## Lazy-load house pattern for cloud-SDK adapters Every cloud-SDK adapter follows this exact shape. The pattern is non-negotiable so installs don't need every cloud SDK and so server boot stays fast. ### Canonical form ```typescript import type { /* type-only imports for SDK shapes */ } from '@scope/package'; // NB: type-only imports don't trigger the lazy load — only `await import(...)` does. type Module = typeof import('@scope/package'); type Client = InstanceType; export class XxxAdapter implements YyyInterface { readonly info = { kind: 'xxx' as const, /* public diagnostics */ }; // Promise-typed field — stored, not awaited at construction private readonly clientPromise: Promise<{ client: Client; /* commands */ }>; constructor(private readonly opts: XxxConfig) { this.clientPromise = this.createClient(); } private async createClient(): Promise<{ client: Client; /* commands */ }> { const mod = (await import('@scope/package')) as Module; return { client: new mod.Client(/* ... */), // Capture command constructors here too — the SDK uses // command objects you instantiate per call. }; } async someOperation(...) { const { client, /* commands */ } = await this.clientPromise; return client.send(new commands.SomeCommand(/* ... */)); } } ``` ### Why this exact shape | Decision | Why | | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | | `await import('@scope/package')` in a private async method | Dynamic import doesn't run at module load. The SDK package never resolves unless an instance is constructed. | | `clientPromise` stored at construction (`this.clientPromise = this.createClient()`) | Single shared promise. First operation awaits it; subsequent operations reuse the same resolved client. No reconnection storms. | | **Stored, not awaited** at construction | Constructor stays synchronous. `new XxxAdapter(opts)` returns immediately. The connection happens on first use. | | Command constructors returned alongside the client | AWS SDK v3 / GCP / Azure use command objects. Capturing them in `createClient` avoids per-call `await import` calls. | | Type-only imports for SDK shapes | TypeScript can infer types from the SDK without forcing a runtime require. Pair `import type` with `typeof import(...)`. | ### Azure variant — parallel imports Azure adapters typically need two packages (`@azure/identity` for auth + a service SDK). Parallelize the imports: ```typescript private async createClient(): Promise { const [identity, keys] = await Promise.all([ import('@azure/identity') as Promise, import('@azure/keyvault-keys') as Promise, ]); return new keys.CryptographyClient(this.keyId, new identity.DefaultAzureCredential()); } ``` ### `optionalDependencies` Every cloud-SDK package **must** be listed in `cloudpdf/server/package.json` under `optionalDependencies`, never `dependencies`. This way: - `npm install` doesn't pull SDKs the user isn't using. - Errors at adapter construction time are clear: "Cannot find module '@scope/package' — install it to use the X adapter." - CI can install with `--no-optional` to verify the core server works without any cloud SDK. Current cloud-SDK adapters using the pattern (all match exactly): | Adapter | SDK package | | ------------------------------ | --------------------------------------------- | | `S3ObjectStore` | `@aws-sdk/client-s3` + `s3-request-presigner` | | `AwsKmsKeyring` | `@aws-sdk/client-kms` | | `GcpKmsKeyring` | `@google-cloud/kms` | | `AzureKeyVaultKeyring` | `@azure/identity` + `@azure/keyvault-keys` | | `AwsSecretsManagerProvider` | `@aws-sdk/client-secrets-manager` | | `GcpSecretManagerProvider` | `@google-cloud/secret-manager` | | `AzureKeyVaultSecretsProvider` | `@azure/identity` + `@azure/keyvault-secrets` | | `GcsObjectStore` | `@google-cloud/storage` | | `AzureBlobObjectStore` | `@azure/storage-blob` + `@azure/identity` | Any new cloud-SDK adapter (CDN CloudFront / CloudCDN / AzureFD purge) follows the same shape. ### Faking an SDK in tests A cloud SDK's test fake is one file per package in `cloudpdf/server/__mocks__/` (`__mocks__/@azure/storage-blob.ts`). A test file opts in with `vi.mock('@azure/storage-blob')`, with no factory, and imports the state the fake exports (`blobs`, `gcs`, `awsKms`, …) from that file to seed and inspect it. When a test needs something the fake can't do yet, extend the shared fake. Never give `vi.mock` a factory for an SDK an adapter imports lazily. While a factory resolves, vitest 2 marks it as in progress on the importing module's import stack, which every dynamic `import()` from that module shares. A second import of the same package that arrives in that window loads the real SDK. Every adapter instance starts its own import, so two instances built close together are enough, and the real client then calls the cloud. vitest serves a `__mocks__` file by path, with no such window. --- ## Config schema convention `/config/ConfigSchema.ts`: ```typescript export const ConfigSchema = z.discriminatedUnion('kind', [ z.object({ kind: z.literal('a'), ...fields }), z.object({ kind: z.literal('b'), ...fields }), ]); export type Config = z.inferConfigSchema>; ``` Fields that may hold secrets: `z.union([SecretRefSchema, z.string()])`. The string form is for local-dev convenience (literal value); the `SecretRef` form is for production. The factory's resolver injection (see below) handles both. --- ## Factory convention `/create.ts`: ```typescript export async function create( config: Config, opts: { resolver?: SecretResolver } = {}, ): Promise<> { switch (config.kind) { case 'a': return new A(config, opts); case 'b': return new B(config, opts); // exhaustive — TypeScript enforces no missing cases } } ``` The `{ resolver }` opt is the shared mechanism for resolving `SecretRef` fields in config. Each adapter that needs it pulls the resolver out of opts and resolves at construction (or lazily, depending on the adapter). --- ## Env-var loader convention `/config/loadConfigFromEnv.ts`: ```typescript export function loadConfigFromEnv(env: NodeJS.ProcessEnv): Config { const kind = env['CLOUDPDF__KIND'] ?? ''; const raw = { kind, ...kindSpecificFields(env, kind) }; return ConfigSchema.parse(raw); // throws with helpful message } ``` Env-var conventions: - `CLOUDPDF__KIND` — selector - `CLOUDPDF___` — variant-specific fields - Secret values accept the `secret:///?jsonKey=...&encoding=...` URI form (parsed to `SecretRef`) or plain strings (used literally) --- ## Redaction When a config is logged or surfaced through an admin endpoint, run it through `redactConfig` from `config/secrets/redact.ts`: ```typescript import { redactConfig } from '../../config/secrets/redact'; logger.info({ config: redactConfig(myConfig) }, 'starting'); ``` - `SecretRef`-shaped values become `` - Caller can pass `additionalSensitiveKeys` to redact literal-string secret values by field name A test (`cloudpdf/server/test/config-redact.test.ts`) pins that secret material never leaks through `JSON.stringify` of a redacted config. --- ## The bootstrap recipe In `cloudpdf/server/src/bin/cloudpdf-server.ts`: ```typescript // 1. Secrets — primary user-facing utility. // Registry takes the full SecretsConfig and applies // CachingSecretsProvider automatically when config.cache is set. const secrets = createSecretsProviderRegistry(loadSecretsConfigFromEnv(env)); const resolver = createSecretResolver(secrets); // 2. Each family loads its config, takes the resolver const [kms, objectStore, cdnSigner] = await Promise.all([ createKmsKeyring(loadKmsConfigFromEnv(env), { resolver }), createObjectStore(loadObjectStoreConfigFromEnv(env), { resolver }), createCdnSigner(loadCdnConfigFromEnv(env), { resolver }), ]); // 3. buildApp — Secrets is not here, stays in scope for user code await buildApp({ kms, objectStore, cdnSigner /* ... */ }); ``` No `createServerRuntime` or other bundler. Five lines of factory composition is the standard, and it keeps `secrets` and `resolver` first-class for user code that needs to fetch its own secrets (JWT signing keys, DB credentials, third-party integration tokens). ### Caching control `createSecretsProviderRegistry(config)` applies `CachingSecretsProvider` to every provider when `config.cache` is present. Three tiers of control: | Goal | How | | --------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Default deployment (caching on) | `config.cache: { ttlSec: 3600 }` — `loadSecretsConfigFromEnv` sets this by default | | Custom TTL | `config.cache: { ttlSec: 300 }` (or `CLOUDPDF_SECRETS_CACHE_TTL_SEC=300`) | | No caching | Omit `cache` from the programmatic config; or `CLOUDPDF_SECRETS_CACHE_TTL_SEC=0` | | Per-provider caching, custom decorators, mocked providers | Skip the registry helper — build providers manually via `createSecretsProvider`, compose into a `Map`, pass to `createSecretResolver` | --- ## Adding a new adapter — checklist 1. **Interface** — already exists for the family; you don't touch it. 2. **Zod schema variant** — add one entry to `/config/ConfigSchema.ts`'s discriminated union. 3. **Adapter class** — one file in `/adapters/.ts`. Implements the interface; exposes `info: { kind, ... }`; uses the lazy-load house pattern if it wraps a cloud SDK. 4. **Factory case** — one new `case` in `/create.ts`'s switch. TypeScript fails the build if you forget. 5. **Env loader case** — one new branch in `/config/loadConfigFromEnv.ts`'s kind-specific field reader. 6. **Optional dependency** — if the adapter wraps a cloud SDK, add the package to `cloudpdf/server/package.json`'s `optionalDependencies`. 7. **Test** — one file at `cloudpdf/server/test/-.test.ts` covering construction, key operations, and that `info` exposes only public identifiers. A cloud SDK is faked in `cloudpdf/server/__mocks__/` (see [Faking an SDK in tests](#faking-an-sdk-in-tests)). That's the entire surface. If a step doesn't apply (e.g., no cloud SDK → skip step 6), skip it explicitly in the PR description.