Home Reference Source

lib/Audits.js

  1. const util = require('util')
  2. const { assert, helper } = require('./helper')
  3.  
  4. /**
  5. * Audits domain allows investigation of page violations and possible improvements. EXPERIMENTAL
  6. * @see https://chromedevtools.github.io/devtools-protocol/tot/Audits
  7. * @since chrome-remote-interface-extra
  8. */
  9. class Audits {
  10. /**
  11. * @param {Chrome|CRIConnection|CDPSession|Object} client
  12. */
  13. constructor (client) {
  14. /**
  15. * @type {Chrome|CRIConnection|CDPSession|Object}
  16. * @private
  17. */
  18. this._client = client
  19.  
  20. /**
  21. * @type {Set<string>}
  22. */
  23. this._allowedEncodings = new Set(['webp', 'jpeg', 'png'])
  24. }
  25.  
  26. /**
  27. * Returns the response body and size if it were re-encoded with the specified settings.
  28. * Only applies to images
  29. * @param {GetEncodedResponseArgs} opts
  30. * @return {Promise<GetEncodedResponseResults>}
  31. * @see https://chromedevtools.github.io/devtools-protocol/tot/Audits#method-getEncodedResponse
  32. */
  33. getEncodedResponse ({ requestId, encoding, quality, sizeOnly }) {
  34. assert(
  35. helper.isString(requestId),
  36. `The requestId param is required and should be a string, received ${typeof requestId}`
  37. )
  38. assert(
  39. helper.isString(encoding),
  40. `The encoding param is required and should be a string, received ${typeof requestId}`
  41. )
  42. assert(
  43. this._allowedEncodings.has(encoding),
  44. `The encoding param should be one of 'webp', 'jpeg', 'png', received ${encoding}`
  45. )
  46. if (quality != null) {
  47. helper.assertNumberWithin(quality, 0, 1, 'quality')
  48. }
  49. if (sizeOnly != null) {
  50. assert(helper.isBoolean(sizeOnly), `The optional sizeOnly param should be a boolean, received ${typeof sizeOnly}`)
  51. }
  52. return this._client.send(
  53. 'Audits.getEncodedResponse',
  54. Object.assign(
  55. { quality: 1, sizOnly: false },
  56. { requestId, encoding, quality, sizeOnly }
  57. )
  58. )
  59. }
  60.  
  61. /** @ignore */
  62. // eslint-disable-next-line space-before-function-paren
  63. [util.inspect.custom](depth, options) {
  64. return options.stylize('[Audits]', 'special')
  65. }
  66. }
  67.  
  68. /**
  69. * @type {Audits}
  70. */
  71. module.exports = Audits
  72.  
  73. /**
  74. * @typedef {Object} GetEncodedResponseArgs
  75. * @property {string} requestId - Identifier of the network request to get content for
  76. * @property {string} encoding - The encoding to use. Allowed values: webp, jpeg, png
  77. * @property {number} [quality] - The quality of the encoding (0-1). (defaults to 1)
  78. * @property {boolean} [sizeOnly] - Whether to only return the size information (defaults to false)
  79. */
  80.  
  81. /**
  82. * @typedef {Object} GetEncodedResponseResults
  83. * @property {string} [body] - The encoded body as a base64 string. Omitted if sizeOnly is true.
  84. * @property {number} originalSize - Size before re-encoding
  85. * @property {number} encodedSize - Size after re-encoding
  86. */