Skip to content

Build your personal knowledge base with Trilium Notes

ci(pdfjs): own the pdf.js bump end to end

Elian Doran4db6943d2234
diff --git a/.github/workflows/update-pdfjs-viewer.yml b/.github/workflows/update-pdfjs-viewer.yml
new file mode 100644
index 0000000000..f1b2312582
--- /dev/null
+++ b/.github/workflows/update-pdfjs-viewer.yml
@@ -0,0 +1,111 @@
+name: Update PDF.js viewer
+
+# Owns the whole pdfjs-dist bump, because half of one is worse than none.
+#
+# Two copies of pdf.js meet at runtime: the viewer application vendored under
+# packages/pdfjs-viewer/viewer, and the library copied out of the npm package at build
+# time. pdf.js compares their versions when the viewer starts, so bumping the
+# dependency without re-vendoring produces a tree that cannot open a single PDF. That
+# is why Renovate is disabled for pdfjs-dist (see renovate.json) — a bare version bump
+# is never a useful change.
+#
+# So this polls npm instead: bump the dependency, re-vendor the viewer from the
+# matching pdf.js release, verify, and open one PR carrying all of it.
+#
+# Deliberately not triggered by pushes to packages/pdfjs-viewer/package.json: a human
+# pinning pdfjs-dist to a particular version would find it dragged straight back to
+# latest. A hand bump that forgets the re-vendor is caught by src/vendored_viewer.spec.ts
+# instead.
+on:
+ schedule:
+ - cron: "0 5 * * 2" # Tuesdays 05:00 UTC
+ workflow_dispatch:
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.ref }}
+ cancel-in-progress: true
+
+permissions:
+ contents: read
+
+jobs:
+ vendor-viewer:
+ # Matched to the e2e-pdfjs-viewer job in playwright.yml, which runs the same suite.
+ runs-on: ubuntu-22.04
+ name: Bump pdf.js and re-vendor the viewer
+ permissions:
+ contents: write
+ pull-requests: write
+ steps:
+ - uses: actions/checkout@v7
+ - uses: pnpm/action-setup@v6
+ - name: Set up node & dependencies
+ uses: actions/setup-node@v7
+ with:
+ node-version: 24
+ cache: "pnpm"
+ - name: Install dependencies
+ run: pnpm install --frozen-lockfile
+ - name: Bump pdfjs-dist
+ id: bump
+ # `update --latest` writes both package.json and the lockfile, and keeps the
+ # exact pin exact. It installs too, which the re-vendor and the suites below
+ # need — build.ts copies the library straight out of node_modules.
+ run: |
+ pnpm --filter pdfjs-viewer update pdfjs-dist --latest
+ git diff --quiet -- packages/pdfjs-viewer/package.json && changed=false || changed=true
+ echo "changed=$changed" >> "$GITHUB_OUTPUT"
+ version=$(node -p "require('./packages/pdfjs-viewer/package.json').devDependencies['pdfjs-dist']")
+ echo "version=$version" >> "$GITHUB_OUTPUT"
+ - name: Re-vendor the viewer
+ # Fails loudly if our viewer.html patches no longer match upstream's markup.
+ if: steps.bump.outputs.changed == 'true'
+ run: pnpm --filter pdfjs-viewer update-viewer
+ - name: Unit tests
+ if: steps.bump.outputs.changed == 'true'
+ run: pnpm --filter pdfjs-viewer test
+ - name: Install Playwright browsers
+ if: steps.bump.outputs.changed == 'true'
+ run: pnpm exec playwright install --with-deps chromium
+ - name: End-to-end tests
+ # Worth running here even though playwright.yml covers pull requests: a PR opened
+ # by a *scheduled* run carries the Actions bot as its triggering actor, and GitHub
+ # will not start workflows for it (the exception is a run someone dispatched by
+ # hand, which propagates their identity). So on the normal path this is the only
+ # thing that drives the regenerated viewer in a browser — which is the only way an
+ # upstream change that breaks our integration, rather than our patches, shows up.
+ if: steps.bump.outputs.changed == 'true'
+ run: pnpm --filter pdfjs-viewer e2e
+ - name: Upload test report
+ if: failure()
+ uses: actions/upload-artifact@v7
+ with:
+ name: pdfjs-viewer e2e report
+ path: packages/pdfjs-viewer/test-output
+ - name: Open a PR
+ if: steps.bump.outputs.changed == 'true'
+ uses: peter-evans/create-pull-request@v8
+ with:
+ add-paths: |
+ packages/pdfjs-viewer/package.json
+ packages/pdfjs-viewer/viewer
+ pnpm-lock.yaml
+ branch: chore/update-pdfjs-viewer
+ commit-message: "build(pdfjs): update to pdf.js ${{ steps.bump.outputs.version }}"
+ title: "build(pdfjs): update to pdf.js ${{ steps.bump.outputs.version }}"
+ body: |
+ Updates `pdfjs-dist` to ${{ steps.bump.outputs.version }} **and** re-vendors
+ the viewer under `packages/pdfjs-viewer/viewer` from the matching pdf.js
+ release. The two have to move together — pdf.js refuses to start when their
+ versions disagree — which is why Renovate does not handle this dependency.
+
+ Re-vendoring re-applies our patches to `viewer.html` (the custom stylesheet
+ and script, and the relaxed `style-src-elem` CSP) and fails if upstream's
+ markup moved out from under them.
+
+ CI ran the unit and Playwright suites against the result before opening this
+ PR, since a scheduled run's PR does not start workflows of its own.
+
+ The diff is large and machine-generated; `viewer.html` is the file worth
+ reading, since that is the only one we patch.
+ labels: dependencies
diff --git a/CLAUDE.md b/CLAUDE.md
index bd91092cd4..9770127d37 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -373,10 +373,15 @@ Tools are defined using `defineTools()` in `apps/server/src/services/llm/tools/`
5. Use ETAPI (`apps/server/src/etapi/`) as inspiration for what fields to expose, but **do not import ETAPI mappers** — inline the field mappings directly in the tool so the LLM layer stays decoupled from the API layer
### Updating PDF.js
-1. Update `pdfjs-dist` version in `packages/pdfjs-viewer/package.json`
-2. Run `npx tsx scripts/update-viewer.ts` from that directory
-3. Run `pnpm build` to verify success
-4. Commit all changes including updated viewer files
+The viewer under `packages/pdfjs-viewer/viewer/` is vendored from the pdf.js GitHub release matching the `pdfjs-dist` dependency, and pdf.js refuses to start when the two versions disagree — so a bump must always be followed by a re-vendor. `src/vendored_viewer.spec.ts` fails when they drift.
+
+**This is automated**: the weekly `update-pdfjs-viewer` workflow bumps `pdfjs-dist`, re-vendors, verifies, and opens a single PR carrying both. Renovate is deliberately disabled for `pdfjs-dist` (see `renovate.json`) because a bare version bump is never usable on its own. To do it by hand:
+1. Update the `pdfjs-dist` version in `packages/pdfjs-viewer/package.json`
+2. Run `pnpm --filter pdfjs-viewer update-viewer`
+3. Run `pnpm --filter pdfjs-viewer test` and `pnpm --filter pdfjs-viewer e2e` to verify
+4. Commit all changes including the updated viewer files
+
+`update-viewer.ts` re-applies our patches to `viewer.html` (custom stylesheet/script, relaxed `style-src-elem` CSP) and throws if an upstream markup change means it can no longer find them.
### Database Migrations
- Add migration scripts in `apps/server/src/migrations/`
diff --git a/packages/pdfjs-viewer/package.json b/packages/pdfjs-viewer/package.json
index a67c983f4c..5c52ed934b 100644
--- a/packages/pdfjs-viewer/package.json
+++ b/packages/pdfjs-viewer/package.json
@@ -5,6 +5,7 @@
"scripts": {
"build": "tsx scripts/build.ts",
"watch": "tsx scripts/build.ts --watch",
+ "update-viewer": "tsx scripts/update-viewer.ts",
"test": "vitest",
"e2e": "playwright test"
},
diff --git a/packages/pdfjs-viewer/src/persistence.spec.ts b/packages/pdfjs-viewer/src/persistence.spec.ts
index aa0b0403a3..28246549f9 100644
--- a/packages/pdfjs-viewer/src/persistence.spec.ts
+++ b/packages/pdfjs-viewer/src/persistence.spec.ts
@@ -1,12 +1,12 @@
// @vitest-environment happy-dom
-import { existsSync, readFileSync } from "node:fs";
-import { dirname, join } from "node:path";
+import { readFileSync } from "node:fs";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import interceptPersistence from "./persistence";
import { allFeaturesPdf } from "./test/fixture_pdf";
import { InstalledViewer, installViewerApp, uninstallViewerApp } from "./test/viewer_app";
+import { resolveViewerBundle } from "./test/viewer_bundle";
const SIGNATURE_KEY = "pdfjs.signature";
const ENTRY = { description: "Mine", signatureData: "data-1" };
@@ -272,25 +272,6 @@ function loadRealSignatureStorage(): { SignatureStorage: any } {
return { SignatureStorage };
}
-/** Finds `viewer/viewer.mjs` by walking up from the working directory (or its package root). */
-function resolveViewerBundle(): string {
- const relative = join("viewer", "viewer.mjs");
- let dir = process.cwd();
- for (;;) {
- const candidates = [ join(dir, relative), join(dir, "packages", "pdfjs-viewer", relative) ];
- for (const candidate of candidates) {
- if (existsSync(candidate)) {
- return candidate;
- }
- }
- const parent = dirname(dir);
- if (parent === dir) {
- throw new Error("Could not locate the pdf.js viewer bundle (viewer/viewer.mjs)");
- }
- dir = parent;
- }
-}
-
/** Returns the index just past the `}` that closes the first `{` at or after `from`. */
function matchingBraceEnd(source: string, from: number): number {
let depth = 0;
diff --git a/packages/pdfjs-viewer/src/test/viewer_bundle.ts b/packages/pdfjs-viewer/src/test/viewer_bundle.ts
new file mode 100644
index 0000000000..765344321c
--- /dev/null
+++ b/packages/pdfjs-viewer/src/test/viewer_bundle.ts
@@ -0,0 +1,29 @@
+/**
+ * Locates the vendored pdf.js viewer bundle (`viewer/viewer.mjs`) on disk.
+ *
+ * Tests that need the bundle read it as *text* rather than importing it: it is a
+ * self-executing application bundle, not a module with exports. happy-dom rewrites
+ * `import.meta.url` to an http:// origin, so the path is resolved from the working
+ * directory instead — walking up so the suite runs the same from the package
+ * directory and from the monorepo root.
+ */
+import { existsSync } from "node:fs";
+import { dirname, join } from "node:path";
+
+export function resolveViewerBundle(): string {
+ const relative = join("viewer", "viewer.mjs");
+ let dir = process.cwd();
+ for (;;) {
+ const candidates = [ join(dir, relative), join(dir, "packages", "pdfjs-viewer", relative) ];
+ for (const candidate of candidates) {
+ if (existsSync(candidate)) {
+ return candidate;
+ }
+ }
+ const parent = dirname(dir);
+ if (parent === dir) {
+ throw new Error("Could not locate the pdf.js viewer bundle (viewer/viewer.mjs)");
+ }
+ dir = parent;
+ }
+}
diff --git a/packages/pdfjs-viewer/src/vendored_viewer.spec.ts b/packages/pdfjs-viewer/src/vendored_viewer.spec.ts
new file mode 100644
index 0000000000..771a0c55ce
--- /dev/null
+++ b/packages/pdfjs-viewer/src/vendored_viewer.spec.ts
@@ -0,0 +1,39 @@
+/**
+ * Guards the one coupling a `pdfjs-dist` bump silently breaks.
+ *
+ * Two copies of pdf.js meet at runtime: the viewer application vendored under `viewer/`
+ * (extracted from the GitHub release by `scripts/update-viewer.ts`) and the library
+ * `scripts/build.ts` copies out of the npm package. pdf.js compares the two at
+ * construction — `The API version "x" does not match the Viewer version "y"` — so
+ * bumping the dependency without re-running the vendoring script leaves the viewer
+ * throwing on every PDF a user opens.
+ *
+ * The e2e suite would eventually catch it, as a browser-side error some way into a
+ * Playwright run. This says the same thing in milliseconds, and names the fix.
+ */
+import { readFileSync } from "node:fs";
+import { dirname, join } from "node:path";
+
+import { describe, expect, it } from "vitest";
+
+import { resolveViewerBundle } from "./test/viewer_bundle";
+
+describe("vendored pdf.js viewer", () => {
+ it("is built from the same pdf.js release as the pdfjs-dist dependency", () => {
+ const bundlePath = resolveViewerBundle();
+ const packageRoot = dirname(dirname(bundlePath));
+ const packageJson = JSON.parse(readFileSync(join(packageRoot, "package.json"), "utf8"));
+ const declared = packageJson.devDependencies["pdfjs-dist"];
+
+ // pdf.js embeds the version it compares against; if the declaration is ever
+ // renamed the check has stopped meaning anything, so fail rather than skip.
+ const match = readFileSync(bundlePath, "utf8").match(/const viewerVersion = "([^"]+)"/);
+ expect(match, "viewer.mjs no longer declares `const viewerVersion`").not.toBeNull();
+
+ expect(
+ match?.[1],
+ `viewer/ was vendored from pdf.js ${match?.[1]} but pdfjs-dist is ${declared}. `
+ + "Re-run `pnpm --filter pdfjs-viewer update-viewer` and commit the result."
+ ).toBe(declared);
+ });
+});
diff --git a/renovate.json b/renovate.json
index 15739ba588..f029afe21a 100644
--- a/renovate.json
+++ b/renovate.json
@@ -37,6 +37,11 @@
"matchPackageNames": ["com.android.tools.build:gradle"],
"enabled": false
},
+ {
+ "description": "pdfjs-dist is owned by the update-pdfjs-viewer workflow, which bumps it together with the viewer vendored under packages/pdfjs-viewer/viewer. pdf.js refuses to start when the two versions disagree, so a bare version bump is never a usable change",
+ "matchPackageNames": ["pdfjs-dist"],
+ "enabled": false
+ },
{
"matchPackageNames": ["@fsegurai/codemirror-theme-**", "@catppuccin/codemirror"],
"groupName": "codemirror themes"