# Doors — the law for the viewer product line [`packages.md`](./packages.md) is the law for where a package lives, and [`tooling/build/README.md`](../../tooling/build/README.md) for how packages build. This file is the law for **how one viewer serves several deliveries**: EmbedPDF with the built-in engine, CloudPDF with a remote one, a CDN snippet, and one wrapper per framework — without forking the viewer once. A **door** is an entry point that decides one thing: which engine is in the bundle. Everything else is shared, names included: the CloudPDF viewer is the same ``, `init()`, `` and `` with the cloud engine as the default, as `@embedpdf/viewer` is with the built-in one. Unqualified `kernel/`, `local/`, and `doors/` paths below are relative to `packages/viewer/main/src/`. ## The five laws 1. **One artifact, many doors.** There is a single viewer implementation. A door is an entry file, not a copy — if you are about to duplicate component or element code per delivery, stop; the difference belongs in a door. 2. **The kernel is door-blind.** Nothing under `kernel/` may import from `doors/` or `local/`, and the word "local" may not appear there. Same rule one level up: a framework wrapper's `component.*` imports the viewer for types only (`import type`), so it holds no runtime edge to any engine. 3. **A door declares its own truth.** A config type may only promise what that door's imports actually deliver. The local door's `engine` is optional and accepts the built-in engine's options bag; the `/core` door's is required and takes only a real `Engine | EngineFactory`. Both are called `ViewerConfig` — one name per door, so samples and docs never pick a door-specific type name. An engine's OPTIONS type lives next to the code that interprets it (`LocalEngineConfig` in `viewer/main/src/local`, `CloudEngineOptions` in `@cloudpdf/engine`), never in shared code. 4. **Doors never import doors.** Two deliveries that share a surface share a module, not each other — that is why `local/surface.ts` exists and the npm, portable and snippet doors all re-export it, differing only in the engine entry and the wasm default they register. 5. **The implementation sits at the widest type; doors narrow by assignment.** Parameter types are contravariant, so a function taking the wider config satisfies a narrower signature — never the reverse. Hence `initViewer` and `` are typed with the kernel's `ElementConfig` (engine seam `unknown`), and each door re-types them by assignment: ```ts const init: (options: InitOptions) => EmbedPdfViewerElement = initViewer; ``` **If you find yourself writing `as` here, you have it backwards.** ## Why the element's engine seam is `unknown` `EmbedPdfViewerElement` is one compiled class serving every door, so its `.config` must carry a value only the sending door understands. It opens the envelope far enough to recognise an `Engine` or a factory, and otherwise hands it to the provider its door registered (`EmbedPdfViewerElement.defaultEngineProvider`, set by `registerLocalEngine()` or `@cloudpdf/viewer`'s `registerCloudEngine()`; the `/core` door sets none, which is exactly what makes its `engine` required). What the provider returns (`DoorEngine`) is the engine, and what the element can't know about it: the fonts its settings name, how a `src` in that engine's words opens (`sourceOf`: the cloud's `{ token }` is the engine's `{ kind: 'token' }`; the element reads only URLs, files, bytes and open inputs itself), and plugin settings the engine needs under the config's own (`plugins`: the cloud's stamp libraries open in a local engine). The element has one default engine, so an app imports one door. Typing that seam narrower would either forbid a legal door vocabulary or advertise one a different door cannot honour. So the imperative `el.config =` path stays opaque and runtime-checked on purpose; typed config belongs at `init()` and ``, which are per-door. ## The shape ``` packages/viewer/main/src/ kernel/ door-blind: element, config base, init, public surface local/ the built-in engine's vocabulary + registration (not an entry) doors/ entries only — local.ts, portable.ts, core.ts, snippet.ts ``` Every framework wrapper is the same four files, in this order: ``` src/component.ts the wrapper, engine-blind, `import type` only src/parts.ts `./component`: the component at the widest config, and the components and hooks every door shares src/index.ts the local door: import '@embedpdf/viewer'; + parts + narrowed props src/core.ts the core door: import '@embedpdf/viewer/core'; + parts + narrowed props ``` (Angular's are entry folders: `component/`, `core/`, and `src/` for the local door.) Read one wrapper and you have read them all. ## Doors in another package `@cloudpdf/viewer` and its wrappers are doors too, the same shape across a package line: `@cloudpdf/viewer` registers the cloud engine and is otherwise `@embedpdf/viewer/core`'s surface with the cloud's `ViewerConfig`; each `@cloudpdf/viewer-` imports it, re-exports `@embedpdf/viewer-/component`, and narrows the component to the cloud's config. Its npm build leaves every `@embedpdf/*` package to the app's bundler, so the app has one element class, the one the engine is registered on. ## Checklist: adding a framework wrapper 1. `packages/viewer/` — the name falls out of [`packages.md`](./packages.md) (`@embedpdf/viewer-`). 2. `component.*`: types only from `@embedpdf/viewer/core`; props are `ElementConfig & Extras`. Set `.config` before the element's deferred mount, so the viewer boots exactly once — make that an acceptance test. 3. `parts.*` (`./component`): the component and every framework part, for doors in this package and others. `index.*` / `core.*`: one side-effect import each, re-export the parts, then the component narrowed to that door's `ViewerConfig`. 4. `package.json`: add `"./core": "./src/core.*"` to `exports` (the build derives entries from it — see `tooling/build/README.md`) and list both door entries in `sideEffects`. A pure re-export barrel with `sideEffects: false` can be dropped by a bundler, taking the element registration with it. 5. Children pass through as light DOM. There is no slot bridge: a slot is a child. 6. Prove both doors. A probe of `@ts-expect-error` lines is self-validating — if a door stops rejecting a bad shape, tsc fails with "unused directive". Assert the bundle too: the `/core` door must emit no `FPDF_` symbols and no wasm asset. ## The delivery build `@embedpdf/viewer` is built by Vite as an app, not by the package preset (`packages/viewer/main/vite.config.ts`): - **Preact stands in for React here and only here.** The config aliases `react`, `react-dom` and the JSX runtimes to `preact/compat`. The aliases are absolute file paths, because the imports they replace live in other workspace packages, whose `node_modules` have no Preact. - **Every asset URL is relative** (`renderBuiltUrl`), so `dist/` can be served from any CDN path. - **`process.env.NODE_ENV` compiles to `'production'`**, so the viewer checks the layout in every build: a CDN user's mistyped id warns with the id and the closest one that exists. - **A child of the element with a `slot` attribute** is shown in the layout's matching `custom()` socket. It stays in the page: the page's CSS and the host framework's state keep working, and the toolbar measures the box it is shown in. ## Enforcement `eslint.config.js` carries the import-boundary rules for laws 2 and 3 (`component.*` is `import type` only; `cloudpdf/**` may not import the local doors). They are **latent until lint is wired up in CI** — there is no root `tsconfig.json` and no `lint` script today, so treat the tree and the type system as the real guardrails until that lands.