Skip to content

Commit e9c32ed

Browse files
committed
chore: symbol properties and access modifiers
1 parent 5b36a19 commit e9c32ed

File tree

2 files changed

+52
-106
lines changed

2 files changed

+52
-106
lines changed

src/cmap/connection.ts

Lines changed: 49 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type Readable, Transform, type TransformCallback } from 'stream';
2-
import { setTimeout } from 'timers';
2+
import { clearTimeout, setTimeout } from 'timers';
33
import { promisify } from 'util';
44

55
import type { BSONSerializeOptions, Document, ObjectId } from '../bson';
@@ -62,19 +62,6 @@ import { type CompressorName, decompressResponse } from './wire_protocol/compres
6262
import { onData } from './wire_protocol/on_data';
6363
import { getReadPreference, isSharded } from './wire_protocol/shared';
6464

65-
/** @internal */
66-
const kGeneration = Symbol('generation');
67-
/** @internal */
68-
const kLastUseTime = Symbol('lastUseTime');
69-
/** @internal */
70-
const kClusterTime = Symbol('clusterTime');
71-
/** @internal */
72-
const kDescription = Symbol('description');
73-
/** @internal */
74-
const kHello = Symbol('hello');
75-
/** @internal */
76-
const kAutoEncrypter = Symbol('autoEncrypter');
77-
7865
/** @internal */
7966
export interface CommandOptions extends BSONSerializeOptions {
8067
secondaryOk?: boolean;
@@ -154,15 +141,6 @@ export function hasSessionSupport(conn: Connection): boolean {
154141
return description.logicalSessionTimeoutMinutes != null;
155142
}
156143

157-
function supportsOpMsg(conn: Connection) {
158-
const description = conn.description;
159-
if (description == null) {
160-
return false;
161-
}
162-
163-
return maxWireVersion(conn) >= 6 && !description.__nodejs_mock_server__;
164-
}
165-
166144
function streamIdentifier(stream: Stream, options: ConnectionOptions): string {
167145
if (options.proxyHost) {
168146
// If proxy options are specified, the properties of `stream` itself
@@ -178,37 +156,26 @@ function streamIdentifier(stream: Stream, options: ConnectionOptions): string {
178156
return uuidV4().toString('hex');
179157
}
180158

181-
/** in-progress connection layer */
182-
183159
/** @internal */
184160
export class Connection extends TypedEventEmitter<ConnectionEvents> {
185-
id: number | '<monitor>';
186-
address: string;
187-
socketTimeoutMS: number;
188-
monitorCommands: boolean;
189-
lastHelloMS?: number;
190-
serverApi?: ServerApi;
191-
helloOk?: boolean;
192-
/** @internal */
193-
authContext?: AuthContext;
194-
195-
delayedTimeoutId: NodeJS.Timeout | null = null;
196-
/** @internal */
197-
[kDescription]: StreamDescription;
198-
/** @internal */
199-
[kGeneration]: number;
200-
/** @internal */
201-
[kLastUseTime]: number;
202-
161+
public id: number | '<monitor>';
162+
public address: string;
163+
public lastHelloMS?: number;
164+
public serverApi?: ServerApi;
165+
public helloOk?: boolean;
166+
public authContext?: AuthContext;
167+
public delayedTimeoutId: NodeJS.Timeout | null = null;
168+
public generation: number;
169+
public readonly description: Readonly<StreamDescription>;
170+
171+
private lastUseTime: number;
172+
private socketTimeoutMS: number;
173+
private monitorCommands: boolean;
203174
private socket: Stream;
204175
private controller: AbortController;
205176
private messageStream: Readable;
206177
private socketWrite: (buffer: Uint8Array) => Promise<void>;
207-
208-
/** @internal */
209-
[kHello]: Document | null;
210-
/** @internal */
211-
[kClusterTime]: Document | null;
178+
private clusterTime: Document | null = null;
212179

213180
/** @event */
214181
static readonly COMMAND_STARTED = COMMAND_STARTED;
@@ -235,12 +202,10 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
235202
this.socketTimeoutMS = options.socketTimeoutMS ?? 0;
236203
this.monitorCommands = options.monitorCommands;
237204
this.serverApi = options.serverApi;
238-
this[kHello] = null;
239-
this[kClusterTime] = null;
240205

241-
this[kDescription] = new StreamDescription(this.address, options);
242-
this[kGeneration] = options.generation;
243-
this[kLastUseTime] = now();
206+
this.description = new StreamDescription(this.address, options);
207+
this.generation = options.generation;
208+
this.lastUseTime = now();
244209

245210
this.socket = stream;
246211
this.controller = new AbortController();
@@ -259,89 +224,62 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
259224
}
260225

261226
/** Indicates that the connection (including underlying TCP socket) has been closed. */
262-
get closed(): boolean {
227+
public get closed(): boolean {
263228
return this.controller.signal.aborted;
264229
}
265230

266-
get description(): StreamDescription {
267-
return this[kDescription];
268-
}
269-
270-
get hello(): Document | null {
271-
return this[kHello];
272-
}
273-
274231
// the `connect` method stores the result of the handshake hello on the connection
275-
set hello(response: Document | null) {
276-
this[kDescription].receiveResponse(response);
277-
this[kDescription] = Object.freeze(this[kDescription]);
278-
279-
// TODO: remove this, and only use the `StreamDescription` in the future
280-
this[kHello] = response;
232+
public set hello(response: Document | null) {
233+
this.description.receiveResponse(response);
234+
Object.freeze(this.description);
281235
}
282236

283-
get serviceId(): ObjectId | undefined {
237+
public get serviceId(): ObjectId | undefined {
284238
return this.hello?.serviceId;
285239
}
286240

287-
get loadBalanced(): boolean {
241+
public get loadBalanced(): boolean {
288242
return this.description.loadBalanced;
289243
}
290244

291-
get generation(): number {
292-
return this[kGeneration] || 0;
293-
}
294-
295-
set generation(generation: number) {
296-
this[kGeneration] = generation;
297-
}
298-
299-
get idleTime(): number {
300-
return calculateDurationInMs(this[kLastUseTime]);
301-
}
302-
303-
get clusterTime(): Document | null {
304-
return this[kClusterTime];
245+
public get idleTime(): number {
246+
return calculateDurationInMs(this.lastUseTime);
305247
}
306248

307-
get stream(): Stream {
308-
return this.socket;
309-
}
310-
311-
get hasSessionSupport(): boolean {
249+
private get hasSessionSupport(): boolean {
312250
return this.description.logicalSessionTimeoutMinutes != null;
313251
}
314252

315-
get supportsOpMsg(): boolean {
253+
private get supportsOpMsg(): boolean {
316254
return (
317255
this.description != null &&
318-
maxWireVersion(this as any as Connection) >= 6 &&
256+
maxWireVersion(this) >= 6 &&
319257
!this.description.__nodejs_mock_server__
320258
);
321259
}
322260

323-
markAvailable(): void {
324-
this[kLastUseTime] = now();
261+
public markAvailable(): void {
262+
this.lastUseTime = now();
325263
}
326264

327-
onError(error?: Error) {
265+
public onError(error?: Error) {
328266
this.cleanup(error);
329267
}
330268

331-
onClose() {
269+
private onClose() {
332270
const message = `connection ${this.id} to ${this.address} closed`;
333271
this.cleanup(new MongoNetworkError(message));
334272
}
335273

336-
onTimeout() {
274+
private onTimeout() {
337275
this.delayedTimeoutId = setTimeout(() => {
338276
const message = `connection ${this.id} to ${this.address} timed out`;
339277
const beforeHandshake = this.hello == null;
340278
this.cleanup(new MongoNetworkTimeoutError(message, { beforeHandshake }));
341279
}, 1).unref(); // No need for this timer to hold the event loop open
342280
}
343281

344-
destroy(options: DestroyOptions, callback?: Callback): void {
282+
public destroy(options: DestroyOptions, callback?: Callback): void {
345283
if (this.closed) {
346284
if (typeof callback === 'function') process.nextTick(callback);
347285
return;
@@ -476,7 +414,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
476414
}
477415

478416
if (document.$clusterTime) {
479-
this[kClusterTime] = document.$clusterTime;
417+
this.clusterTime = document.$clusterTime;
480418
this.emit(Connection.CLUSTER_TIME_RECEIVED, document.$clusterTime);
481419
}
482420
}
@@ -495,7 +433,11 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
495433
}
496434
}
497435

498-
async *sendCommand(ns: MongoDBNamespace, command: Document, options: CommandOptions = {}) {
436+
private async *sendCommand(
437+
ns: MongoDBNamespace,
438+
command: Document,
439+
options: CommandOptions = {}
440+
) {
499441
const message = this.prepareCommand(ns.db, command, options);
500442

501443
let started = 0;
@@ -555,7 +497,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
555497
}
556498
}
557499

558-
async command(
500+
public async command(
559501
ns: MongoDBNamespace,
560502
command: Document,
561503
options: CommandOptions = {}
@@ -567,7 +509,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
567509
throw new MongoUnexpectedServerResponseError('Unable to get response from server');
568510
}
569511

570-
exhaustCommand(
512+
public exhaustCommand(
571513
ns: MongoDBNamespace,
572514
command: Document,
573515
options: CommandOptions,
@@ -590,7 +532,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
590532
* Writes an OP_MSG or OP_QUERY request to the socket, optionally compressing the command. This method
591533
* waits until the socket's buffer has emptied (the Nodejs socket `drain` event has fired).
592534
*/
593-
async writeCommand(
535+
private async writeCommand(
594536
command: WriteProtocolMessageType,
595537
options: { agreedCompressor?: CompressorName; zlibCompressionLevel?: number }
596538
): Promise<void> {
@@ -616,7 +558,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
616558
*
617559
* Note that `for-await` loops call `return` automatically when the loop is exited.
618560
*/
619-
async *readMany(): AsyncGenerator<OpMsgResponse | OpQueryResponse> {
561+
private async *readMany(): AsyncGenerator<OpMsgResponse | OpQueryResponse> {
620562
for await (const message of onData(this.messageStream, { signal: this.controller.signal })) {
621563
const response = await decompressResponse(message);
622564
yield response;
@@ -638,6 +580,7 @@ export class SizedMessageTransform extends Transform {
638580
this.bufferPool = new BufferPool();
639581
this.connection = connection;
640582
}
583+
641584
override _transform(chunk: Buffer, encoding: unknown, callback: TransformCallback): void {
642585
if (this.connection.delayedTimeoutId != null) {
643586
clearTimeout(this.connection.delayedTimeoutId);
@@ -667,11 +610,11 @@ export class SizedMessageTransform extends Transform {
667610
/** @internal */
668611
export class CryptoConnection extends Connection {
669612
/** @internal */
670-
[kAutoEncrypter]?: AutoEncrypter;
613+
autoEncrypter?: AutoEncrypter;
671614

672615
constructor(stream: Stream, options: ConnectionOptions) {
673616
super(stream, options);
674-
this[kAutoEncrypter] = options.autoEncrypter;
617+
this.autoEncrypter = options.autoEncrypter;
675618
}
676619

677620
/** @internal @override */
@@ -680,7 +623,7 @@ export class CryptoConnection extends Connection {
680623
cmd: Document,
681624
options: CommandOptions
682625
): Promise<Document> {
683-
const autoEncrypter = this[kAutoEncrypter];
626+
const { autoEncrypter } = this;
684627
if (!autoEncrypter) {
685628
throw new MongoMissingDependencyError('No AutoEncrypter available for encryption');
686629
}

src/cmap/stream_description.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export class StreamDescription {
3737

3838
zlibCompressionLevel?: number;
3939

40+
public hello: Document | null = null;
41+
4042
constructor(address: string, options?: StreamDescriptionOptions) {
4143
this.address = address;
4244
this.type = ServerType.Unknown;
@@ -57,6 +59,7 @@ export class StreamDescription {
5759
if (response == null) {
5860
return;
5961
}
62+
this.hello = response;
6063
this.type = parseServerType(response);
6164
for (const field of RESPONSE_FIELDS) {
6265
if (response[field] != null) {

0 commit comments

Comments
 (0)