fix: address CodeRabbit review feedback

- Handle Headers instance in TSA request detection (not just plain objects)
- Wrap timestampPdf with createCorsAwareFetch for OCSP/cert chain requests
- Pass 'success' type to showAlert on successful timestamp
- Add TSA URL context to timestamp error messages with cause
- Remove redundant Uint8Array wrapping in TimestampNode
This commit is contained in:
InstalZDLL
2026-03-15 00:46:47 +01:00
parent dfd0ebcfc5
commit 70f0834fc0
3 changed files with 25 additions and 10 deletions

View File

@@ -208,10 +208,12 @@ function createCorsAwareFetch(): {
const isTsaRequest =
(init?.headers &&
typeof init.headers === 'object' &&
'Content-Type' in init.headers &&
(init.headers as Record<string, string>)['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<string, string>)['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): {

View File

@@ -238,7 +238,8 @@ async function processTimestamp(): Promise<void> {
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();

View File

@@ -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 {