diff --git a/src/js/logic/digital-sign-pdf.ts b/src/js/logic/digital-sign-pdf.ts index bacfba1..0bd2726 100644 --- a/src/js/logic/digital-sign-pdf.ts +++ b/src/js/logic/digital-sign-pdf.ts @@ -208,10 +208,12 @@ function createCorsAwareFetch(): { const isTsaRequest = (init?.headers && - typeof init.headers === 'object' && - 'Content-Type' in init.headers && - (init.headers as Record)['Content-Type'] === - 'application/timestamp-query') || + (init.headers instanceof Headers + ? init.headers.get('Content-Type') === 'application/timestamp-query' + : typeof init.headers === 'object' && + !Array.isArray(init.headers) && + (init.headers as Record)['Content-Type'] === + 'application/timestamp-query')) || url.includes('timestamp') || url.includes('/tsa') || url.includes('/tsr') || @@ -350,8 +352,14 @@ export async function timestampPdf( const signer = new PdfSigner(signOptions); - const timestampedPdfBytes = await signer.sign(pdfBytes); - return new Uint8Array(timestampedPdfBytes); + const { restore } = createCorsAwareFetch(); + + try { + const timestampedPdfBytes = await signer.sign(pdfBytes); + return new Uint8Array(timestampedPdfBytes); + } finally { + restore(); + } } export function getCertificateInfo(certificate: forge.pki.Certificate): { diff --git a/src/js/logic/timestamp-pdf-page.ts b/src/js/logic/timestamp-pdf-page.ts index 6793300..97b8d69 100644 --- a/src/js/logic/timestamp-pdf-page.ts +++ b/src/js/logic/timestamp-pdf-page.ts @@ -238,7 +238,8 @@ async function processTimestamp(): Promise { showAlert( 'Success', - 'PDF timestamped successfully! The timestamp can be verified in Adobe Acrobat and other PDF readers.' + 'PDF timestamped successfully! The timestamp can be verified in Adobe Acrobat and other PDF readers.', + 'success' ); resetState(); diff --git a/src/js/workflow/nodes/timestamp-node.ts b/src/js/workflow/nodes/timestamp-node.ts index e77e870..7fde80c 100644 --- a/src/js/workflow/nodes/timestamp-node.ts +++ b/src/js/workflow/nodes/timestamp-node.ts @@ -43,9 +43,15 @@ export class TimestampNode extends BaseWorkflowNode { return { pdf: await processBatch(pdfInputs, async (input) => { - const timestampedBytes = await timestampPdf(input.bytes, tsaUrl); - - const bytes = new Uint8Array(timestampedBytes); + let bytes: Uint8Array; + try { + bytes = await timestampPdf(input.bytes, tsaUrl); + } catch (err) { + throw new Error( + `Failed to timestamp using TSA ${tsaUrl}: ${err instanceof Error ? err.message : err}`, + { cause: err } + ); + } const document = await PDFDocument.load(bytes); return {