pdf-license-bundle.client.spec.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { spawnSync } from 'node:child_process'
  2. import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
  3. import { tmpdir } from 'node:os'
  4. import { dirname, join, resolve } from 'node:path'
  5. import { createRequire } from 'node:module'
  6. import { describe, expect, it } from 'vitest'
  7. const packageRoot = resolve(import.meta.dirname, '..')
  8. const bundlePath = join(packageRoot, 'lib/client.js')
  9. const pdfChunkPath = join(packageRoot, 'lib/client.pdf.js')
  10. const require = createRequire(import.meta.url)
  11. const licenseNames = [
  12. 'LICENSE',
  13. 'cmaps/LICENSE',
  14. 'standard_fonts/LICENSE_FOXIT',
  15. 'standard_fonts/LICENSE_LIBERATION',
  16. 'wasm/LICENSE_JBIG2',
  17. 'wasm/LICENSE_OPENJPEG',
  18. 'wasm/LICENSE_PDFJS_JBIG2',
  19. 'wasm/LICENSE_PDFJS_OPENJPEG',
  20. 'wasm/LICENSE_PDFJS_QCMS',
  21. 'wasm/LICENSE_QCMS',
  22. ] as const
  23. function run(command: string, args: string[], cwd: string, timeout: number): string {
  24. const result = spawnSync(command, args, { cwd, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, timeout })
  25. expect(result.error).toBeUndefined()
  26. expect(result.signal, result.stderr).toBeNull()
  27. expect(result.status, result.stderr).toBe(0)
  28. return result.stdout
  29. }
  30. function runPnpm(args: string[], cwd: string, timeout: number): string {
  31. const entrypoint = process.env.npm_execpath
  32. if (entrypoint === undefined || entrypoint === '') {
  33. if (process.platform === 'win32') throw new Error('npm_execpath is required to run pnpm on Windows')
  34. return run('pnpm', args, cwd, timeout)
  35. }
  36. return /\.[cm]?js$/iu.test(entrypoint)
  37. ? run(process.execPath, [entrypoint, ...args], cwd, timeout)
  38. : run(entrypoint, args, cwd, timeout)
  39. }
  40. describe('published PDF.js licenses', () => {
  41. it.skipIf(!existsSync(bundlePath))('keeps every bundled license in the packed PDF chunk', ({ task }) => {
  42. expect(existsSync(pdfChunkPath)).toBe(true)
  43. const output = mkdtempSync(join(tmpdir(), 'dsh-document-preview-pack-'))
  44. try {
  45. const packed = JSON.parse(runPnpm([
  46. 'pack', '--json', '--pack-destination', output,
  47. ], packageRoot, task.timeout)) as { filename: string; files: { path: string }[] }
  48. expect(packed.files.map(file => file.path)).toContain('lib/client.js')
  49. expect(packed.files.map(file => file.path)).toContain('lib/client.pdf.js')
  50. expect(packed.files.some(file => file.path.endsWith('pdfjs-NOTICES.txt'))).toBe(false)
  51. const client = run('tar', ['-xOf', resolve(packageRoot, packed.filename), 'package/lib/client.js'], packageRoot, task.timeout)
  52. const pdf = run('tar', ['-xOf', resolve(packageRoot, packed.filename), 'package/lib/client.pdf.js'], packageRoot, task.timeout)
  53. expect([...client.matchAll(/require\.async\("(\.\/client[^"/]*\.js)"\)/gu)].map(match => match[1]))
  54. .toEqual(['./client.pdf.js'])
  55. expect(client).not.toMatch(/\brequire\("\.\/client[^"/]*\.js"\)/u)
  56. expect([...pdf.matchAll(/require\("(\.\/client[^"/]*\.js)"\)/gu)].map(match => match[1]))
  57. .toEqual([])
  58. expect(client).not.toContain('//! Bundled PDF.js license notices')
  59. expect(client).not.toContain('/pdfjs-dist/')
  60. expect(pdf).toContain('//! Bundled PDF.js license notices')
  61. const pdfRoot = dirname(require.resolve('pdfjs-dist/package.json'))
  62. for (const name of licenseNames) {
  63. const source = readFileSync(join(pdfRoot, name), 'utf8').trimEnd()
  64. const commented = [`// ${name}`, '// ', ...source.split('\n').map(line => `// ${line}`)].join('\n')
  65. expect(pdf, `${name} must be visible in package/lib/client.pdf.js`).toContain(commented)
  66. }
  67. } finally {
  68. rmSync(output, { recursive: true, force: true })
  69. }
  70. })
  71. })