stream_description.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.StreamDescription = void 0;
  4. const bson_1 = require("../bson");
  5. const common_1 = require("../sdam/common");
  6. const server_description_1 = require("../sdam/server_description");
  7. const RESPONSE_FIELDS = [
  8. 'minWireVersion',
  9. 'maxWireVersion',
  10. 'maxBsonObjectSize',
  11. 'maxMessageSizeBytes',
  12. 'maxWriteBatchSize',
  13. 'logicalSessionTimeoutMinutes'
  14. ];
  15. /** @public */
  16. class StreamDescription {
  17. constructor(address, options) {
  18. this.hello = null;
  19. this.address = address;
  20. this.type = common_1.ServerType.Unknown;
  21. this.minWireVersion = undefined;
  22. this.maxWireVersion = undefined;
  23. this.maxBsonObjectSize = 16777216;
  24. this.maxMessageSizeBytes = 48000000;
  25. this.maxWriteBatchSize = 100000;
  26. this.logicalSessionTimeoutMinutes = options?.logicalSessionTimeoutMinutes;
  27. this.loadBalanced = !!options?.loadBalanced;
  28. this.compressors =
  29. options && options.compressors && Array.isArray(options.compressors)
  30. ? options.compressors
  31. : [];
  32. this.serverConnectionId = null;
  33. }
  34. receiveResponse(response) {
  35. if (response == null) {
  36. return;
  37. }
  38. this.hello = response;
  39. this.type = (0, server_description_1.parseServerType)(response);
  40. if ('connectionId' in response) {
  41. this.serverConnectionId = this.parseServerConnectionID(response.connectionId);
  42. }
  43. else {
  44. this.serverConnectionId = null;
  45. }
  46. for (const field of RESPONSE_FIELDS) {
  47. if (response[field] != null) {
  48. this[field] = response[field];
  49. }
  50. // testing case
  51. if ('__nodejs_mock_server__' in response) {
  52. this.__nodejs_mock_server__ = response['__nodejs_mock_server__'];
  53. }
  54. }
  55. if (response.compression) {
  56. this.compressor = this.compressors.filter(c => response.compression?.includes(c))[0];
  57. }
  58. }
  59. /* @internal */
  60. parseServerConnectionID(serverConnectionId) {
  61. // Connection ids are always integral, so it's safe to coerce doubles as well as
  62. // any integral type.
  63. return bson_1.Long.isLong(serverConnectionId)
  64. ? serverConnectionId.toBigInt()
  65. : // @ts-expect-error: Doubles are coercible to number
  66. BigInt(serverConnectionId);
  67. }
  68. }
  69. exports.StreamDescription = StreamDescription;
  70. //# sourceMappingURL=stream_description.js.map