Skip to content

Commit d45d33c

Browse files
committed
refactor: factor common sdam types into commonly accesible file
1 parent cf5b97c commit d45d33c

File tree

14 files changed

+72
-59
lines changed

14 files changed

+72
-59
lines changed

lib/core/sdam/common.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
// shared state names
4+
const STATE_CLOSING = 'closing';
5+
const STATE_CLOSED = 'closed';
6+
const STATE_CONNECTING = 'connecting';
7+
const STATE_CONNECTED = 'connected';
8+
9+
// An enumeration of topology types we know about
10+
const TopologyType = {
11+
Single: 'Single',
12+
ReplicaSetNoPrimary: 'ReplicaSetNoPrimary',
13+
ReplicaSetWithPrimary: 'ReplicaSetWithPrimary',
14+
Sharded: 'Sharded',
15+
Unknown: 'Unknown'
16+
};
17+
18+
// An enumeration of server types we know about
19+
const ServerType = {
20+
Standalone: 'Standalone',
21+
Mongos: 'Mongos',
22+
PossiblePrimary: 'PossiblePrimary',
23+
RSPrimary: 'RSPrimary',
24+
RSSecondary: 'RSSecondary',
25+
RSArbiter: 'RSArbiter',
26+
RSOther: 'RSOther',
27+
RSGhost: 'RSGhost',
28+
Unknown: 'Unknown'
29+
};
30+
31+
module.exports = {
32+
STATE_CLOSING,
33+
STATE_CLOSED,
34+
STATE_CONNECTING,
35+
STATE_CONNECTED,
36+
TopologyType,
37+
ServerType
38+
};

lib/core/sdam/monitoring.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const ServerDescription = require('./server_description').ServerDescription;
3+
const ServerDescription = require('./server_description');
44
const calculateDurationInMs = require('../utils').calculateDurationInMs;
55

66
// pulled from `Server` implementation

lib/core/sdam/server.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const wireProtocol = require('../wireprotocol');
77
const BSON = require('../connection/utils').retrieveBSON();
88
const createClientInfo = require('../topologies/shared').createClientInfo;
99
const Logger = require('../connection/logger');
10-
const ServerDescription = require('./server_description').ServerDescription;
10+
const ServerDescription = require('./server_description');
1111
const ReadPreference = require('../topologies/read_preference');
1212
const monitorServer = require('./monitoring').monitorServer;
1313
const MongoParseError = require('../error').MongoParseError;
@@ -16,6 +16,7 @@ const collationNotSupported = require('../utils').collationNotSupported;
1616
const debugOptions = require('../connection/utils').debugOptions;
1717
const isSDAMUnrecoverableError = require('../error').isSDAMUnrecoverableError;
1818
const makeStateMachine = require('../utils').makeStateMachine;
19+
const common = require('./common');
1920

2021
// Used for filtering out fields for logging
2122
const DEBUG_FIELDS = [
@@ -45,10 +46,10 @@ const DEBUG_FIELDS = [
4546
'servername'
4647
];
4748

48-
const STATE_CLOSING = 'closing';
49-
const STATE_CLOSED = 'closed';
50-
const STATE_CONNECTING = 'connecting';
51-
const STATE_CONNECTED = 'connected';
49+
const STATE_CLOSING = common.STATE_CLOSING;
50+
const STATE_CLOSED = common.STATE_CLOSED;
51+
const STATE_CONNECTING = common.STATE_CONNECTING;
52+
const STATE_CONNECTED = common.STATE_CONNECTED;
5253
const stateTransition = makeStateMachine({
5354
[STATE_CLOSED]: [STATE_CLOSED, STATE_CONNECTING],
5455
[STATE_CONNECTING]: [STATE_CONNECTING, STATE_CLOSING, STATE_CONNECTED, STATE_CLOSED],

lib/core/sdam/server_description.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,7 @@
22

33
const arrayStrictEqual = require('../utils').arrayStrictEqual;
44
const tagsStrictEqual = require('../utils').tagsStrictEqual;
5-
6-
// An enumeration of server types we know about
7-
const ServerType = {
8-
Standalone: 'Standalone',
9-
Mongos: 'Mongos',
10-
PossiblePrimary: 'PossiblePrimary',
11-
RSPrimary: 'RSPrimary',
12-
RSSecondary: 'RSSecondary',
13-
RSArbiter: 'RSArbiter',
14-
RSOther: 'RSOther',
15-
RSGhost: 'RSGhost',
16-
Unknown: 'Unknown'
17-
};
5+
const ServerType = require('./common').ServerType;
186

197
const WRITABLE_SERVER_TYPES = new Set([
208
ServerType.RSPrimary,
@@ -186,7 +174,4 @@ function parseServerType(ismaster) {
186174
return ServerType.Standalone;
187175
}
188176

189-
module.exports = {
190-
ServerDescription,
191-
ServerType
192-
};
177+
module.exports = ServerDescription;

lib/core/sdam/server_selectors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
const ServerType = require('./server_description').ServerType;
3-
const TopologyType = require('./topology_description').TopologyType;
2+
const ServerType = require('./common').ServerType;
3+
const TopologyType = require('./common').TopologyType;
44
const ReadPreference = require('../topologies/read_preference');
55
const MongoError = require('../error').MongoError;
66

lib/core/sdam/topology.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22
const EventEmitter = require('events');
3-
const ServerDescription = require('./server_description').ServerDescription;
4-
const ServerType = require('./server_description').ServerType;
5-
const TopologyDescription = require('./topology_description').TopologyDescription;
6-
const TopologyType = require('./topology_description').TopologyType;
3+
const ServerDescription = require('./server_description');
4+
const ServerType = require('./common').ServerType;
5+
const TopologyDescription = require('./topology_description');
6+
const TopologyType = require('./common').TopologyType;
77
const monitoring = require('./monitoring');
88
const calculateDurationInMs = require('../utils').calculateDurationInMs;
99
const MongoTimeoutError = require('../error').MongoTimeoutError;
@@ -27,6 +27,7 @@ const SrvPoller = require('./srv_polling').SrvPoller;
2727
const getMMAPError = require('../topologies/shared').getMMAPError;
2828
const makeStateMachine = require('../utils').makeStateMachine;
2929
const eachAsync = require('../utils').eachAsync;
30+
const common = require('./common');
3031

3132
// Global state
3233
let globalTopologyCounter = 0;
@@ -62,10 +63,10 @@ const LOCAL_SERVER_EVENTS = SERVER_RELAY_EVENTS.concat([
6263
'ended'
6364
]);
6465

65-
const STATE_CLOSING = 'closing';
66-
const STATE_CLOSED = 'closed';
67-
const STATE_CONNECTING = 'connecting';
68-
const STATE_CONNECTED = 'connected';
66+
const STATE_CLOSING = common.STATE_CLOSING;
67+
const STATE_CLOSED = common.STATE_CLOSED;
68+
const STATE_CONNECTING = common.STATE_CONNECTING;
69+
const STATE_CONNECTED = common.STATE_CONNECTED;
6970
const stateTransition = makeStateMachine({
7071
[STATE_CLOSED]: [STATE_CLOSED, STATE_CONNECTING],
7172
[STATE_CONNECTING]: [STATE_CONNECTING, STATE_CLOSING, STATE_CONNECTED, STATE_CLOSED],

lib/core/sdam/topology_description.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
'use strict';
2-
const ServerType = require('./server_description').ServerType;
3-
const ServerDescription = require('./server_description').ServerDescription;
2+
const ServerType = require('./common').ServerType;
3+
const ServerDescription = require('./server_description');
44
const WIRE_CONSTANTS = require('../wireprotocol/constants');
5+
const TopologyType = require('./common').TopologyType;
56

67
// contstants related to compatability checks
78
const MIN_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_SERVER_VERSION;
89
const MAX_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_SERVER_VERSION;
910
const MIN_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_WIRE_VERSION;
1011
const MAX_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_WIRE_VERSION;
1112

12-
// An enumeration of topology types we know about
13-
const TopologyType = {
14-
Single: 'Single',
15-
ReplicaSetNoPrimary: 'ReplicaSetNoPrimary',
16-
ReplicaSetWithPrimary: 'ReplicaSetWithPrimary',
17-
Sharded: 'Sharded',
18-
Unknown: 'Unknown'
19-
};
20-
2113
// Representation of a deployment of servers
2214
class TopologyDescription {
2315
/**
@@ -404,7 +396,4 @@ function checkHasPrimary(serverDescriptions) {
404396
return TopologyType.ReplicaSetNoPrimary;
405397
}
406398

407-
module.exports = {
408-
TopologyType,
409-
TopologyDescription
410-
};
399+
module.exports = TopologyDescription;

lib/core/topologies/shared.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const os = require('os');
44
const ReadPreference = require('./read_preference');
55
const Buffer = require('safe-buffer').Buffer;
6-
const TopologyType = require('../sdam/topology_description').TopologyType;
6+
const TopologyType = require('../sdam/common').TopologyType;
77
const MongoError = require('../error').MongoError;
88

99
const MMAPv1_RETRY_WRITES_ERROR_CODE = 20;

lib/core/wireprotocol/shared.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const ReadPreference = require('../topologies/read_preference');
44
const MongoError = require('../error').MongoError;
5-
const ServerType = require('../sdam/server_description').ServerType;
6-
const TopologyDescription = require('../sdam/topology_description').TopologyDescription;
5+
const ServerType = require('../sdam/common').ServerType;
6+
const TopologyDescription = require('../sdam/topology_description');
77

88
const MESSAGE_HEADER_SIZE = 16;
99
const COMPRESSION_DETAILS_SIZE = 9; // originalOpcode + uncompressedSize, compressorID

test/core/functional/srv_polling_tests.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict';
22

33
const Topology = require('../../../lib/core/sdam/topology');
4-
const _topologyDescription = require('../../../lib/core/sdam/topology_description');
5-
const TopologyDescription = _topologyDescription.TopologyDescription;
6-
const TopologyType = _topologyDescription.TopologyType;
4+
const TopologyDescription = require('../../../lib/core/sdam/topology_description');
5+
const TopologyType = require('../../../lib/core/sdam/common').TopologyType;
76
const monitoring = require('../../../lib/core/sdam/monitoring');
87
const SrvPoller = require('../../../lib/core/sdam/srv_polling').SrvPoller;
98
const SrvPollingEvent = require('../../../lib/core/sdam/srv_polling').SrvPollingEvent;

0 commit comments

Comments
 (0)