Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. const crypto = require('crypto');
  3. const requireOptional = require('require_optional');
  4. /**
  5. * Generate a UUIDv4
  6. */
  7. const uuidV4 = () => {
  8. const result = crypto.randomBytes(16);
  9. result[6] = (result[6] & 0x0f) | 0x40;
  10. result[8] = (result[8] & 0x3f) | 0x80;
  11. return result;
  12. };
  13. /**
  14. * Returns the duration calculated from two high resolution timers in milliseconds
  15. *
  16. * @param {Object} started A high resolution timestamp created from `process.hrtime()`
  17. * @returns {Number} The duration in milliseconds
  18. */
  19. const calculateDurationInMs = started => {
  20. const hrtime = process.hrtime(started);
  21. return (hrtime[0] * 1e9 + hrtime[1]) / 1e6;
  22. };
  23. /**
  24. * Relays events for a given listener and emitter
  25. *
  26. * @param {EventEmitter} listener the EventEmitter to listen to the events from
  27. * @param {EventEmitter} emitter the EventEmitter to relay the events to
  28. */
  29. function relayEvents(listener, emitter, events) {
  30. events.forEach(eventName => listener.on(eventName, event => emitter.emit(eventName, event)));
  31. }
  32. function retrieveKerberos() {
  33. let kerberos;
  34. try {
  35. kerberos = requireOptional('kerberos');
  36. } catch (err) {
  37. if (err.code === 'MODULE_NOT_FOUND') {
  38. throw new Error('The `kerberos` module was not found. Please install it and try again.');
  39. }
  40. throw err;
  41. }
  42. return kerberos;
  43. }
  44. // Throw an error if an attempt to use EJSON is made when it is not installed
  45. const noEJSONError = function() {
  46. throw new Error('The `mongodb-extjson` module was not found. Please install it and try again.');
  47. };
  48. // Facilitate loading EJSON optionally
  49. function retrieveEJSON() {
  50. let EJSON = null;
  51. try {
  52. EJSON = requireOptional('mongodb-extjson');
  53. } catch (error) {} // eslint-disable-line
  54. if (!EJSON) {
  55. EJSON = {
  56. parse: noEJSONError,
  57. deserialize: noEJSONError,
  58. serialize: noEJSONError,
  59. stringify: noEJSONError,
  60. setBSONModule: noEJSONError,
  61. BSON: noEJSONError
  62. };
  63. }
  64. return EJSON;
  65. }
  66. /*
  67. * Checks that collation is supported by server.
  68. *
  69. * @param {Server} [server] to check against
  70. * @param {object} [cmd] object where collation may be specified
  71. * @param {function} [callback] callback function
  72. * @return true if server does not support collation
  73. */
  74. function collationNotSupported(server, cmd) {
  75. return cmd && cmd.collation && server.ismaster && server.ismaster.maxWireVersion < 5;
  76. }
  77. module.exports = {
  78. uuidV4,
  79. calculateDurationInMs,
  80. relayEvents,
  81. collationNotSupported,
  82. retrieveEJSON,
  83. retrieveKerberos
  84. };