import { readFileSync, writeFileSync } from 'node:fs'; import { join, resolve } from 'node:path'; import { pathToFileURL } from 'node:url'; export const workerTarget = 'new Blob([_dsh_pdf_worker_default,'; export const marker = '// dsh-pdf-map-compat-v1'; export const compat = readFileSync(new URL('./pdf-map-compat.js', import.meta.url), 'utf8'); export function patchPdf(source, hostVersion, packageVersion) { if (hostVersion !== '0.2.1-rc.2' || packageVersion !== '0.2.1-rc.2') throw new Error('unsupported PDF preview version'); if (source.includes(marker)) throw new Error('PDF compatibility patch already installed'); if (source.split(workerTarget).length !== 2 || !source.includes('this.#methodPromises.getOrInsertComputed(')) { throw new Error('unexpected PDF.js bundle: expected main-thread upsert and exactly one worker Blob'); } return marker + '\n' + compat + '\n' + source.replace(workerTarget, 'new Blob([' + JSON.stringify(compat) + ', _dsh_pdf_worker_default,'); } export function install(root = '/usr/local/lib/node_modules/@deepseek-ai/dsh') { const dir = join(root, 'node_modules/@deepseek-ai/dsh-client-ui-sidebar-documentpreview'); const version = path => JSON.parse(readFileSync(join(path, 'package.json'), 'utf8')).version; const file = join(dir, 'lib/client.pdf.js'); writeFileSync(file, patchPdf(readFileSync(file, 'utf8'), version(root), version(dir))); console.log('Installed PDF Map compatibility patch (main thread + worker)'); } if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) install(process.argv[2]);