index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const zlib_1 = require("zlib");
  4. const stream_1 = require("stream");
  5. const fs_1 = require("fs");
  6. const util_1 = require("util");
  7. const duplexer = require('duplexer');
  8. const readFilePromise = util_1.promisify(fs_1.readFile);
  9. const bufferFormatter = (incoming) => typeof incoming === 'string' ? Buffer.from(incoming, 'utf8') : incoming;
  10. const optionFormatter = (passed, toEncode) => ({
  11. params: {
  12. [zlib_1.constants.BROTLI_PARAM_MODE]: passed && 'mode' in passed && passed.mode || zlib_1.constants.BROTLI_DEFAULT_MODE,
  13. [zlib_1.constants.BROTLI_PARAM_QUALITY]: passed && 'quality' in passed && passed.quality || zlib_1.constants.BROTLI_MAX_QUALITY,
  14. [zlib_1.constants.BROTLI_PARAM_SIZE_HINT]: toEncode ? toEncode.byteLength : 0,
  15. }
  16. });
  17. /**
  18. * @param incoming Either a Buffer or string of the value to encode.
  19. * @param options Subset of Encoding Parameters.
  20. * @return Promise that resolves with the encoded Buffer length.
  21. */
  22. async function size(incoming, options) {
  23. const buffer = bufferFormatter(incoming);
  24. return new Promise(function (resolve, reject) {
  25. zlib_1.brotliCompress(buffer, optionFormatter(options, buffer), (error, result) => {
  26. if (error !== null) {
  27. reject(error);
  28. }
  29. resolve(result.byteLength);
  30. });
  31. });
  32. }
  33. exports.default = size;
  34. /**
  35. * @param incoming Either a Buffer or string of the value to encode.
  36. * @param options Subset of Encoding Parameters.
  37. * @return Length of encoded Buffer.
  38. */
  39. function sync(incoming, options) {
  40. const buffer = bufferFormatter(incoming);
  41. return zlib_1.brotliCompressSync(buffer, optionFormatter(options, buffer)).byteLength;
  42. }
  43. exports.sync = sync;
  44. /**
  45. * @param options
  46. * @return PassThroughStream for the contents being compressed
  47. */
  48. function stream(options) {
  49. const input = new stream_1.PassThrough();
  50. const output = new stream_1.PassThrough();
  51. const wrapper = duplexer(input, output);
  52. let size = 0;
  53. const brotli = zlib_1.createBrotliCompress(optionFormatter(options))
  54. .on('data', buf => {
  55. size += buf.length;
  56. })
  57. .on('error', () => {
  58. wrapper.brotliSize = 0;
  59. })
  60. .on('end', () => {
  61. wrapper.brotliSize = size;
  62. wrapper.emit('brotli-size', size);
  63. output.end();
  64. });
  65. input.pipe(brotli);
  66. input.pipe(output, { end: false });
  67. return wrapper;
  68. }
  69. exports.stream = stream;
  70. /**
  71. * @param path File Path for the file to compress.
  72. * @param options Subset of Encoding Parameters.
  73. * @return Promise that resolves with size of encoded file.
  74. */
  75. async function file(path, options) {
  76. const file = await readFilePromise(path);
  77. return (await size(file, options));
  78. }
  79. exports.file = file;
  80. /**
  81. * @param path File Path for the file to compress.
  82. * @param options Subset of Encoding Parameters.
  83. * @return size of encoded file.
  84. */
  85. function fileSync(path, options) {
  86. const file = fs_1.readFileSync(path);
  87. return sync(file, options);
  88. }
  89. exports.fileSync = fileSync;
  90. //# sourceMappingURL=index.js.map