compression.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. });
  14. exports.uncompressibleCommands = new Set([
  15. constants_1.LEGACY_HELLO_COMMAND,
  16. 'saslStart',
  17. 'saslContinue',
  18. 'getnonce',
  19. 'authenticate',
  20. 'createUser',
  21. 'updateUser',
  22. 'copydbSaslStart',
  23. 'copydbgetnonce',
  24. 'copydb'
  25. ]);
  26. // Facilitate compressing a message using an agreed compressor
  27. function compress(self, dataToBeCompressed, callback) {
  28. const zlibOptions = {};
  29. switch (self.options.agreedCompressor) {
  30. case 'snappy': {
  31. if ('kModuleError' in deps_1.Snappy) {
  32. return callback(deps_1.Snappy['kModuleError']);
  33. }
  34. if (deps_1.Snappy[deps_1.PKG_VERSION].major <= 6) {
  35. deps_1.Snappy.compress(dataToBeCompressed, callback);
  36. }
  37. else {
  38. deps_1.Snappy.compress(dataToBeCompressed)
  39. .then(buffer => callback(undefined, buffer))
  40. .catch(error => callback(error));
  41. }
  42. break;
  43. }
  44. case 'zlib':
  45. // Determine zlibCompressionLevel
  46. if (self.options.zlibCompressionLevel) {
  47. zlibOptions.level = self.options.zlibCompressionLevel;
  48. }
  49. zlib.deflate(dataToBeCompressed, zlibOptions, callback);
  50. break;
  51. default:
  52. throw new error_1.MongoInvalidArgumentError(`Unknown compressor ${self.options.agreedCompressor} failed to compress`);
  53. }
  54. }
  55. exports.compress = compress;
  56. // Decompress a message using the given compressor
  57. function decompress(compressorID, compressedData, callback) {
  58. if (compressorID < 0 || compressorID > Math.max(2)) {
  59. throw new error_1.MongoDecompressionError(`Server sent message compressed using an unsupported compressor. (Received compressor ID ${compressorID})`);
  60. }
  61. switch (compressorID) {
  62. case exports.Compressor.snappy: {
  63. if ('kModuleError' in deps_1.Snappy) {
  64. return callback(deps_1.Snappy['kModuleError']);
  65. }
  66. if (deps_1.Snappy[deps_1.PKG_VERSION].major <= 6) {
  67. deps_1.Snappy.uncompress(compressedData, { asBuffer: true }, callback);
  68. }
  69. else {
  70. deps_1.Snappy.uncompress(compressedData, { asBuffer: true })
  71. .then(buffer => callback(undefined, buffer))
  72. .catch(error => callback(error));
  73. }
  74. break;
  75. }
  76. case exports.Compressor.zlib:
  77. zlib.inflate(compressedData, callback);
  78. break;
  79. default:
  80. callback(undefined, compressedData);
  81. }
  82. }
  83. exports.decompress = decompress;
  84. //# sourceMappingURL=compression.js.map