npm audit --omit=dev went from 11 advisories (6 high) to 0: - pdfjs-dist 5.7.284 -> 6.3.289 (GHSA-hq66-cqwq-w95j, arbitrary JS on opening a malicious PDF; the client has an inline PDF viewer). Only API change for us: getDocument() no longer accepts a bare string. - react-router-dom 7.15.1 -> 7.18.3 (open redirect, DoS, RSC advisories) - sanitize-html 2.17.4 -> 2.17.7 (javascript: URI bypasses; not exploitable in our config per the audit, bumped anyway) - katex 0.16.11 -> 0.16.47 (\htmlData attribute validation) - uuid under @giphy/js-util overridden to >= 11.1.1 (giphy imports v4, which is unchanged) - npm audit fix for transitive brace-expansion / js-yaml / nanoid / postcss Verified: tsc clean, 1000 tests pass, production build succeeds and dist/pdf.worker.min.js is still copied from the new package. Fixes #24 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { useCallback } from 'react';
|
|
import type * as PdfJsDist from 'pdfjs-dist';
|
|
import type { GetViewportParameters } from 'pdfjs-dist/types/src/display/api';
|
|
import { useAsyncCallback } from '../hooks/useAsyncCallback';
|
|
import { trimTrailingSlash } from '../utils/common';
|
|
|
|
export const usePdfJSLoader = () =>
|
|
useAsyncCallback(
|
|
useCallback(async () => {
|
|
const pdf = await import('pdfjs-dist');
|
|
pdf.GlobalWorkerOptions.workerSrc = `${trimTrailingSlash(
|
|
import.meta.env.BASE_URL,
|
|
)}/pdf.worker.min.js`;
|
|
return pdf;
|
|
}, []),
|
|
);
|
|
|
|
export const usePdfDocumentLoader = (pdfJS: typeof PdfJsDist | undefined, src: string) =>
|
|
useAsyncCallback(
|
|
useCallback(async () => {
|
|
if (!pdfJS) {
|
|
throw new Error('PdfJS is not loaded');
|
|
}
|
|
// pdfjs-dist 6 dropped the bare-string overload; pass the URL explicitly.
|
|
const doc = await pdfJS.getDocument({ url: src }).promise;
|
|
return doc;
|
|
}, [pdfJS, src]),
|
|
);
|
|
|
|
export const createPage = async (
|
|
doc: PdfJsDist.PDFDocumentProxy,
|
|
pNo: number,
|
|
opts: GetViewportParameters,
|
|
): Promise<HTMLCanvasElement> => {
|
|
const page = await doc.getPage(pNo);
|
|
const pageViewport = page.getViewport(opts);
|
|
const canvas = document.createElement('canvas');
|
|
const context = canvas.getContext('2d');
|
|
|
|
if (!context) throw new Error('failed to render page.');
|
|
|
|
canvas.width = pageViewport.width;
|
|
canvas.height = pageViewport.height;
|
|
|
|
page.render({
|
|
canvas,
|
|
canvasContext: context,
|
|
viewport: pageViewport,
|
|
});
|
|
|
|
return canvas;
|
|
};
|