mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 14:13:43 +01:00
* Add png to pdf converter for optimising image loading in pdftex * refactor * move fixtures * refactor and add unit tests * Remove gamma correction from PNG processing and update related tests * Add crc32fast dependency and enhance PNG processing tests * Add fixture test cases for PNG processing * fix alpha channel to match pdftex pdftex always uses an 8 bit alpha channel in the output even for 16 bit images * Refactor PNG processing to use Option for alpha stream and update related logic * Enhance PDF writing by adding DecodeParms for alpha stream and optimize PNG encoding process * Add pixel dimensions support in PNG processing and PDF writing * [copybara] move clsi excludes * [clsi] move gitignore rules * [png2pdf] move into dockerfiles folder * [png2pdf] use atomic writes * [clsi] integrate png2pdf * [saas-e2e] add coverage for png2pdf * [png2pdf] make compatible with seccomp profile in clsi * [copybara] remove exclude following move of png2pdf * [copybara] remove spurious excludes * fix broken detection of malformed chunk headers Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * [project-history] add build script flag for depending on doc-updater types * [png2pdf] run tests as part of clsi pipeline * [png2pdf] fix calculation of PDF page size * [web] put png2pdf behind feature flag and make it opt-in * [web] fix ClsiManager.promises.sendRequest invocation after merge * extend is_copy_safe function to follow pdftex see the conditions in pdftex's writepng.c https://svn.tug.org:8369/texlive/trunk/Build/source/texk/web2c/pdftexdir/writepng.c?revision=78081&view=markup#l575 the criteria are not an exact match because the png crate does not support all the png chunk types https://github.com/image-rs/image-png/issues/116 if the criteria result in a png being converted or skipped the final result should be the same. in one case the conversion will be done by pdftex (at runtime) in the other case the conversion will be done by png2pdf - both conversions should give the same result. * include tests in png2pdf Dockerfile * [clsi] explicitly disable png2pdf conversion for project conversion * [clsi] png2pdf: count converted files * [png2pdf] bump date in docker image tag * [web] make draft-setting option wider when png2pdf is available --------- Co-authored-by: Brian Gough <brian.gough@overleaf.com> GitOrigin-RevId: d0d952ee009bf93c72bba2ffe763ae0160d396e4
157 lines
4.6 KiB
JavaScript
157 lines
4.6 KiB
JavaScript
import Path from 'node:path'
|
|
import sinon from 'sinon'
|
|
import { vi, describe, beforeEach, it, expect } from 'vitest'
|
|
|
|
const MODULE_PATH = Path.join(import.meta.dirname, '../../../app/js/Png2Pdf')
|
|
|
|
describe('Png2Pdf', function () {
|
|
beforeEach(async function (ctx) {
|
|
ctx.CommandRunner = {
|
|
promises: {
|
|
run: sinon.stub().resolves({ stdout: '', stderr: '', exitCode: 0 }),
|
|
},
|
|
}
|
|
|
|
ctx.Settings = {
|
|
png2pdfImage: 'mock-png2pdf-image',
|
|
enablePng2pdfConversions: true,
|
|
conversionTimeoutSeconds: 60,
|
|
clsi: { dockerRunner: true },
|
|
path: { sandboxedCompilesHostDirCache: '/host/cache' },
|
|
}
|
|
|
|
vi.doMock('../../../app/js/CommandRunner', () => ({
|
|
default: ctx.CommandRunner,
|
|
}))
|
|
|
|
vi.doMock('@overleaf/settings', () => ({ default: ctx.Settings }))
|
|
|
|
vi.doMock('@overleaf/logger', () => ({
|
|
default: { debug: sinon.stub(), warn: sinon.stub(), err: sinon.stub() },
|
|
}))
|
|
|
|
ctx.Metrics = {
|
|
Timer: sinon.stub().returns({ done: sinon.stub().returns(42) }),
|
|
count: sinon.stub(),
|
|
}
|
|
vi.doMock('@overleaf/metrics', () => ({ default: ctx.Metrics }))
|
|
|
|
ctx.Png2Pdf = (await import(MODULE_PATH)).default
|
|
})
|
|
|
|
describe('isEnabled', function () {
|
|
it('is true when enabled, sandboxed, a cache host dir and an image are set', function (ctx) {
|
|
expect(ctx.Png2Pdf.isEnabled()).to.equal(true)
|
|
})
|
|
|
|
it('is false when conversions are disabled', function (ctx) {
|
|
ctx.Settings.enablePng2pdfConversions = false
|
|
expect(ctx.Png2Pdf.isEnabled()).to.equal(false)
|
|
})
|
|
|
|
it('is false without the docker runner', function (ctx) {
|
|
ctx.Settings.clsi.dockerRunner = false
|
|
expect(ctx.Png2Pdf.isEnabled()).to.equal(false)
|
|
})
|
|
|
|
it('is false without a cache host dir', function (ctx) {
|
|
ctx.Settings.path.sandboxedCompilesHostDirCache = undefined
|
|
expect(ctx.Png2Pdf.isEnabled()).to.equal(false)
|
|
})
|
|
|
|
it('is false without an image', function (ctx) {
|
|
ctx.Settings.png2pdfImage = undefined
|
|
expect(ctx.Png2Pdf.isEnabled()).to.equal(false)
|
|
})
|
|
})
|
|
|
|
describe('convertPngFilesInCacheDir', function () {
|
|
it('runs png2pdf over the files in the cache dir', async function (ctx) {
|
|
ctx.CommandRunner.promises.run.resolves({
|
|
stdout: 'Converted a.tmp to PDF\nConverted b.tmp to PDF\n',
|
|
stderr: '',
|
|
exitCode: 0,
|
|
})
|
|
const stats = {}
|
|
const timings = {}
|
|
await ctx.Png2Pdf.convertPngFilesInCacheDir(
|
|
'project-1',
|
|
'/cache/dir',
|
|
['a.tmp', 'b.tmp'],
|
|
stats,
|
|
timings
|
|
)
|
|
sinon.assert.calledWith(
|
|
ctx.CommandRunner.promises.run,
|
|
'project-1',
|
|
['--in-place', '--', 'a.tmp', 'b.tmp'],
|
|
'/cache/dir',
|
|
'mock-png2pdf-image',
|
|
60 * 1000,
|
|
{},
|
|
'png2pdf',
|
|
null
|
|
)
|
|
expect(stats.png2pdf).to.equal(2)
|
|
expect(timings.png2pdf).to.equal(42)
|
|
})
|
|
|
|
it('records only the files actually converted (skipped pngs print nothing)', async function (ctx) {
|
|
// The tool converted one of the two attempted files; the other was
|
|
// already fast-includable and skipped silently.
|
|
ctx.CommandRunner.promises.run.resolves({
|
|
stdout: 'Converted a.tmp to PDF\n',
|
|
stderr: '',
|
|
exitCode: 0,
|
|
})
|
|
const stats = {}
|
|
await ctx.Png2Pdf.convertPngFilesInCacheDir(
|
|
'project-1',
|
|
'/cache/dir',
|
|
['a.tmp', 'b.tmp'],
|
|
stats,
|
|
{}
|
|
)
|
|
expect(stats.png2pdf).to.equal(1)
|
|
sinon.assert.calledWith(ctx.Metrics.count, 'png2pdf-converted', 1)
|
|
})
|
|
|
|
it('does nothing when disabled', async function (ctx) {
|
|
ctx.Settings.clsi.dockerRunner = false
|
|
const stats = {}
|
|
const timings = {}
|
|
await ctx.Png2Pdf.convertPngFilesInCacheDir(
|
|
'project-1',
|
|
'/cache/dir',
|
|
['a.tmp'],
|
|
stats,
|
|
timings
|
|
)
|
|
sinon.assert.notCalled(ctx.CommandRunner.promises.run)
|
|
expect(stats.png2pdf).to.be.undefined
|
|
expect(timings.png2pdf).to.be.undefined
|
|
})
|
|
|
|
it('records the timing but not stats on a non-zero exit code', async function (ctx) {
|
|
ctx.CommandRunner.promises.run.resolves({
|
|
stdout: '',
|
|
stderr: 'boom',
|
|
exitCode: 1,
|
|
})
|
|
const stats = {}
|
|
const timings = {}
|
|
await expect(
|
|
ctx.Png2Pdf.convertPngFilesInCacheDir(
|
|
'project-1',
|
|
'/cache/dir',
|
|
['a.tmp'],
|
|
stats,
|
|
timings
|
|
)
|
|
).to.be.rejectedWith('non-zero exit code from png2pdf')
|
|
expect(stats.png2pdf).to.be.undefined
|
|
expect(timings.png2pdf).to.equal(42)
|
|
})
|
|
})
|
|
})
|