ImageOptimiser.js 746 B

12345678910111213141516171819202122232425262728293031323334
  1. const logger = require('@overleaf/logger')
  2. const metrics = require('@overleaf/metrics')
  3. const { callbackify } = require('util')
  4. const safeExec = require('./SafeExec').promises
  5. module.exports = {
  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(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. }