Skip to content

Commit 7b62d79

Browse files
committed
refactor: add all wire protocol methods to the connection class
1 parent 9be9b97 commit 7b62d79

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

lib/cmap/connection.js

+39-15
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ class Connection extends EventEmitter {
1919
constructor(stream, options) {
2020
super(options);
2121

22-
if (typeof options.generation === 'undefined') {
23-
throw new TypeError('connection requires a pool generation');
24-
}
25-
2622
this.id = options.id;
2723
this.address = streamIdentifier(stream);
2824
this.bson = options.bson;
@@ -61,7 +57,7 @@ class Connection extends EventEmitter {
6157
}
6258

6359
get generation() {
64-
return this[kGeneration];
60+
return this[kGeneration] || 0;
6561
}
6662

6763
get idleTime() {
@@ -102,19 +98,47 @@ class Connection extends EventEmitter {
10298
});
10399
}
104100

101+
// Wire protocol methods
105102
command(ns, cmd, options, callback) {
106-
// NOTE: The wire protocol methods will eventually be migrated to this class, but for now
107-
// we need to pretend we _are_ a server.
108-
const server = {
109-
description: this.description,
110-
s: {
111-
bson: this.bson,
112-
pool: { write: write.bind(this) }
113-
}
114-
};
103+
wp.command(makeServerTrampoline(this), ns, cmd, options, callback);
104+
}
115105

116-
wp.command(server, ns, cmd, options, callback);
106+
query(ns, cmd, cursorState, options, callback) {
107+
wp.query(makeServerTrampoline(this), ns, cmd, cursorState, options, callback);
117108
}
109+
110+
getMore(ns, cursorState, batchSize, options, callback) {
111+
wp.getMore(makeServerTrampoline(this), ns, cursorState, batchSize, options, callback);
112+
}
113+
114+
killCursors(ns, cursorState, callback) {
115+
wp.killCursors(makeServerTrampoline(this), ns, cursorState, callback);
116+
}
117+
118+
insert(ns, ops, options, callback) {
119+
wp.insert(makeServerTrampoline(this), ns, ops, options, callback);
120+
}
121+
122+
update(ns, ops, options, callback) {
123+
wp.update(makeServerTrampoline(this), ns, ops, options, callback);
124+
}
125+
126+
remove(ns, ops, options, callback) {
127+
wp.remove(makeServerTrampoline(this), ns, ops, options, callback);
128+
}
129+
}
130+
131+
/// This lets us emulate a legacy `Server` instance so we can work with the existing wire
132+
/// protocol methods. Eventually, the operation executor will return a `Connection` to execute
133+
/// against.
134+
function makeServerTrampoline(server) {
135+
return {
136+
description: server.description,
137+
s: {
138+
bson: server.bson,
139+
pool: { write: write.bind(server) }
140+
}
141+
};
118142
}
119143

120144
function messageHandler(conn) {

0 commit comments

Comments
 (0)