From 4bc28621817ddee65a00dd1515938cf1d5014370 Mon Sep 17 00:00:00 2001 From: Jacobtread Date: Sun, 17 May 2026 12:34:42 +1200 Subject: [PATCH] feat: patch away the internal fetch logic from pdfmake This is negatively impacting my scorecard so I'm patching it straight out of the library --- patches/pdfmake.patch | 80 ++++++++++++++++++++++++++++++++++++ pnpm-lock.yaml | 9 +++- pnpm-workspace.yaml | 2 + src/export/pdf/definition.ts | 8 ++-- 4 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 patches/pdfmake.patch create mode 100644 pnpm-workspace.yaml diff --git a/patches/pdfmake.patch b/patches/pdfmake.patch new file mode 100644 index 0000000..0e155f6 --- /dev/null +++ b/patches/pdfmake.patch @@ -0,0 +1,80 @@ +diff --git a/build/pdfmake.js b/build/pdfmake.js +index 97e3b9b54d6a885a0d9c73c03dec538c0d5a8b7d..4322c19c79e9a15ca7f4f70c8f03dc51583a01da 100644 +--- a/build/pdfmake.js ++++ b/build/pdfmake.js +@@ -8517,56 +8517,6 @@ function calculatePageHeight(page, margins) { + // EXTERNAL MODULE: ./src/virtual-fs.js + var virtual_fs = __webpack_require__(6811); + ;// ./src/URLResolver.js +-const MAX_REDIRECTS = 30; +- +-/** +- * @param {string} url +- * @param {object} headers +- * @param {(url: string) => boolean} urlAccessPolicy +- * @returns {Promise} +- */ +-async function fetchUrl(url, headers, urlAccessPolicy) { +- if (headers === void 0) { +- headers = {}; +- } +- for (let i = 0; i <= MAX_REDIRECTS; i++) { +- if (typeof urlAccessPolicy !== 'undefined' && urlAccessPolicy(url) !== true) { +- throw new Error(`Access to URL denied by resource access policy: ${url}`); +- } +- try { +- let response = await fetch(url, { +- headers, +- redirect: 'manual' +- }); +- +- // redirect url +- if (response.status >= 300 && response.status < 400) { +- let location = response.headers.get('location'); +- if (!location) { +- throw new Error('Redirect response missing Location header'); +- } +- url = new URL(location, url).href; +- continue; +- } +- +- // browsers do not support redirect: 'manual' +- if (response.type === 'opaqueredirect') { +- response = await fetch(url, { +- headers +- }); +- } +- if (!response.ok) { +- throw new Error(`Failed to fetch (status code: ${response.status})`); +- } +- return response; +- } catch (error) { +- throw new Error(`Network request failed (url: "${url}", error: ${error.message})`, { +- cause: error +- }); +- } +- } +- throw new Error(`Network request failed (url: "${url}", error: Too many redirects)`); +-} + class URLResolver { + constructor(fs) { + this.fs = fs; +@@ -8589,16 +8539,8 @@ class URLResolver { + if (this.fs.existsSync(url)) { + return; // url was downloaded earlier + } +- const response = await fetchUrl(url, headers, this.urlAccessPolicy); +- +- // validate access policy on redirected url (in browsers, only the final URL is validated) +- if (response.redirected) { +- if (typeof this.urlAccessPolicy !== 'undefined' && this.urlAccessPolicy(response.url) !== true) { +- throw new Error(`Access to URL denied by resource access policy: ${response.url}`); +- } +- } +- const buffer = await response.arrayBuffer(); +- this.fs.writeFileSync(url, buffer); ++ ++ throw new Error("External resource loading disallowed by obsidian-timekeep") + } + // else cannot be resolved + }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e211f6b..20214b6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,6 +4,11 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false +patchedDependencies: + pdfmake: + hash: 6d54e252a007864832feb784df02f056f9ea1b527c0d79fc05b23b31db3c6e73 + path: patches/pdfmake.patch + importers: .: @@ -19,7 +24,7 @@ importers: version: 7.3.0 pdfmake: specifier: ^0.3.7 - version: 0.3.8 + version: 0.3.8(patch_hash=6d54e252a007864832feb784df02f056f9ea1b527c0d79fc05b23b31db3c6e73) valibot: specifier: ^1.4.0 version: 1.4.0(typescript@6.0.2) @@ -1657,7 +1662,7 @@ snapshots: linebreak: 1.1.0 png-js: 1.1.0 - pdfmake@0.3.8: + pdfmake@0.3.8(patch_hash=6d54e252a007864832feb784df02f056f9ea1b527c0d79fc05b23b31db3c6e73): dependencies: linebreak: 1.1.0 pdfkit: 0.18.0 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml new file mode 100644 index 0000000..f3849d8 --- /dev/null +++ b/pnpm-workspace.yaml @@ -0,0 +1,2 @@ +patchedDependencies: + pdfmake: patches/pdfmake.patch diff --git a/src/export/pdf/definition.ts b/src/export/pdf/definition.ts index 5e84a28..833084a 100644 --- a/src/export/pdf/definition.ts +++ b/src/export/pdf/definition.ts @@ -1,8 +1,6 @@ import type { Moment } from "moment"; import type { Content, DynamicContent, TableCell, TDocumentDefinitions } from "pdfmake/interfaces"; -import pdfMake from "pdfmake"; - import type { TimekeepSettings } from "@/settings"; import { NameSegmentType, parseNameSegments } from "@/utils/name"; @@ -98,8 +96,8 @@ function createPdfHeader( currentDate: string, totalDuration: string, totalDurationShort: string -): pdfMake.Content { - const detail = (title: string, value: string): pdfMake.Content => ({ +): Content { + const detail = (title: string, value: string): Content => ({ text: [ { text: title + ": ", fontSize: 8, bold: true }, { text: value, fontSize: 8 }, @@ -151,7 +149,7 @@ function createPdfTable( totalDuration: string, settings: TimekeepSettings, currentTime: Moment -): pdfMake.Content { +): Content { const rows = createPdfTableRows(timekeep.entries, settings, currentTime); const cellPadding = () => 8;