compression.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.decompress = exports.compress = exports.uncompressibleCommands = exports.Compressor = void 0;
  4. const zlib = require("zlib");
  5. const constants_1 = require("../../constants");
  6. const deps_1 = require("../../deps");
  7. const error_1 = require("../../error");
  8. /** @public */
  9. exports.Compressor = Object.freeze({
  10. none: 0,
  11. snappy: 1,
  12. zlib: 2,
  13. zstd: 3
  14. });
  15. exports.uncompressibleCommands = new Set([
  16. constants_1.LEGACY_HELLO_COMMAND,
  17. 'saslStart',
  18. 'saslContinue',
  19. 'getnonce',
  20. 'authenticate',
  21. 'createUser',
  22. 'updateUser',
  23. 'copydbSaslStart',
  24. 'copydbgetnonce',
  25. 'copydb'
  26. ]);
  27. const MAX_COMPRESSOR_ID = 3;
  28. const ZSTD_COMPRESSION_LEVEL = 3;
  29. // Facilitate compressing a message using an agreed compressor
  30. function compress(self, dataToBeCompressed, callback) {
  31. const zlibOptions = {};
  32. switch (self.options.agreedCompressor) {
  33. case 'snappy': {
  34. if ('kModuleError' in deps_1.Snappy) {
  35. return callback(deps_1.Snappy['kModuleError']);
  36. }
  37. if (deps_1.Snappy[deps_1.PKG_VERSION].major <= 6) {
  38. deps_1.Snappy.compress(dataToBeCompressed, callback);
  39. }
  40. else {
  41. deps_1.Snappy.compress(dataToBeCompressed)
  42. .then(buffer => callback(undefined, buffer))
  43. .catch(error => callback(error));
  44. }
  45. break;
  46. }
  47. case 'zlib':
  48. // Determine zlibCompressionLevel
  49. if (self.options.zlibCompressionLevel) {
  50. zlibOptions.level = self.options.zlibCompressionLevel;
  51. }
  52. zlib.deflate(dataToBeCompressed, zlibOptions, callback);
  53. break;
  54. case 'zstd':
  55. if ('kModuleError' in deps_1.ZStandard) {
  56. return callback(deps_1.ZStandard['kModuleError']);
  57. }
  58. deps_1.ZStandard.compress(dataToBeCompressed, ZSTD_COMPRESSION_LEVEL).then(buffer => callback(undefined, buffer), error => callback(error));
  59. break;
  60. default:
  61. throw new error_1.MongoInvalidArgumentError(`Unknown compressor ${self.options.agreedCompressor} failed to compress`);
  62. }
  63. }
  64. exports.compress = compress;
  65. // Decompress a message using the given compressor
  66. function decompress(compressorID, compressedData, callback) {
  67. if (compressorID < 0 || compressorID > MAX_COMPRESSOR_ID) {
  68. throw new error_1.MongoDecompressionError(`Server sent message compressed using an unsupported compressor. (Received compressor ID ${compressorID})`);
  69. }
  70. switch (compressorID) {
  71. case exports.Compressor.snappy: {
  72. if ('kModuleError' in deps_1.Snappy) {
  73. return callback(deps_1.Snappy['kModuleError']);
  74. }
  75. if (deps_1.Snappy[deps_1.PKG_VERSION].major <= 6) {
  76. deps_1.Snappy.uncompress(compressedData, { asBuffer: true }, callback);
  77. }
  78. else {
  79. deps_1.Snappy.uncompress(compressedData, { asBuffer: true })
  80. .then(buffer => callback(undefined, buffer))
  81. .catch(error => callback(error));
  82. }
  83. break;
  84. }
  85. case exports.Compressor.zstd: {
  86. if ('kModuleError' in deps_1.ZStandard) {
  87. return callback(deps_1.ZStandard['kModuleError']);
  88. }
  89. deps_1.ZStandard.decompress(compressedData).then(buffer => callback(undefined, buffer), error => callback(error));
  90. break;
  91. }
  92. case exports.Compressor.zlib:
  93. zlib.inflate(compressedData, callback);
  94. break;
  95. default:
  96. callback(undefined, compressedData);
  97. }
  98. }
  99. exports.decompress = decompress;
  100. //# sourceMappingURL=compression.js.map