utils.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. const require_optional = require('require_optional');
  3. function debugOptions(debugFields, options) {
  4. var finaloptions = {};
  5. debugFields.forEach(function(n) {
  6. finaloptions[n] = options[n];
  7. });
  8. return finaloptions;
  9. }
  10. function retrieveBSON() {
  11. var BSON = require('bson');
  12. BSON.native = false;
  13. try {
  14. var optionalBSON = require_optional('bson-ext');
  15. if (optionalBSON) {
  16. optionalBSON.native = true;
  17. return optionalBSON;
  18. }
  19. } catch (err) {} // eslint-disable-line
  20. return BSON;
  21. }
  22. // Throw an error if an attempt to use Snappy is made when Snappy is not installed
  23. function noSnappyWarning() {
  24. throw new Error(
  25. 'Attempted to use Snappy compression, but Snappy is not installed. Install or disable Snappy compression and try again.'
  26. );
  27. }
  28. // Facilitate loading Snappy optionally
  29. function retrieveSnappy() {
  30. var snappy = null;
  31. try {
  32. snappy = require_optional('snappy');
  33. } catch (error) {} // eslint-disable-line
  34. if (!snappy) {
  35. snappy = {
  36. compress: noSnappyWarning,
  37. uncompress: noSnappyWarning,
  38. compressSync: noSnappyWarning,
  39. uncompressSync: noSnappyWarning
  40. };
  41. }
  42. return snappy;
  43. }
  44. module.exports = {
  45. debugOptions,
  46. retrieveBSON,
  47. retrieveSnappy
  48. };