ImageOptimiser.js 732 B

12345678910111213141516171819202122232425262728293031323334
  1. import logger from '@overleaf/logger'
  2. import metrics from '@overleaf/metrics'
  3. import { callbackify } from 'node:util'
  4. import SafeExec from './SafeExec.js'
  5. export default {
  6. compressPng: callbackify(compressPng),
  7. promises: {
  8. compressPng,
  9. },
  10. }
  11. async function compressPng(localPath, callback) {
  12. const timer = new metrics.Timer('compressPng')
  13. const args = ['optipng', localPath]
  14. const opts = {
  15. timeout: 30 * 1000,
  16. killSignal: 'SIGKILL',
  17. }
  18. try {
  19. await SafeExec.promises(args, opts)
  20. timer.done()
  21. } catch (err) {
  22. if (err.code === 'SIGKILL') {
  23. logger.warn(
  24. { err, stderr: err.stderr, localPath },
  25. 'optimiser timeout reached'
  26. )
  27. } else {
  28. throw err
  29. }
  30. }
  31. }