Skip to content

Commit 4c2a001

Browse files
committed
just use an incrementing unique connection id
1 parent 16242e0 commit 4c2a001

File tree

4 files changed

+90
-37
lines changed

4 files changed

+90
-37
lines changed

package-lock.json

Lines changed: 69 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import crypto from 'crypto';
21
import completer from '@mongosh/autocomplete';
32
import { MongoshInternalError, MongoshWarning } from '@mongosh/errors';
43
import { changeHistory } from '@mongosh/history';
@@ -8,7 +7,6 @@ import type {
87
} from '@mongosh/service-provider-core';
98
import type {
109
EvaluationListener,
11-
Mongo,
1210
OnLoadResult,
1311
ShellCliOptions,
1412
} from '@mongosh/shell-api';
@@ -438,35 +436,20 @@ class MongoshNodeRepl implements EvaluationListener {
438436
this.runtimeState().context.history = history;
439437
}
440438

441-
// TODO: probably better to have this on instanceState
442-
public _getMongoByConnectionId(
443-
instanceState: ShellInstanceState,
444-
connectionId: string
445-
): Mongo {
446-
for (const mongo of instanceState.mongos) {
447-
if (connectionIdFromURI(mongo.getURI()) === connectionId) {
448-
return mongo;
449-
}
450-
}
451-
throw new Error(`mongo with connection id ${connectionId} not found`);
452-
}
453-
454439
public getAutocompletionContext(
455440
instanceState: ShellInstanceState
456441
): AutocompletionContext {
457442
return {
458443
currentDatabaseAndConnection: () => {
459444
return {
460-
connectionId: connectionIdFromURI(
461-
instanceState.currentDb.getMongo().getURI()
462-
),
445+
connectionId: instanceState.currentDb.getMongo().getConnectionId(),
463446
databaseName: instanceState.currentDb.getName(),
464447
};
465448
},
466449
databasesForConnection: async (
467450
connectionId: string
468451
): Promise<string[]> => {
469-
const mongo = this._getMongoByConnectionId(instanceState, connectionId);
452+
const mongo = instanceState.getMongoByConnectionId(connectionId);
470453
try {
471454
const dbNames = await mongo._getDatabaseNamesForCompletion();
472455
return dbNames.filter(
@@ -485,7 +468,7 @@ class MongoshNodeRepl implements EvaluationListener {
485468
connectionId: string,
486469
databaseName: string
487470
): Promise<string[]> => {
488-
const mongo = this._getMongoByConnectionId(instanceState, connectionId);
471+
const mongo = instanceState.getMongoByConnectionId(connectionId);
489472
try {
490473
const collectionNames = await mongo
491474
._getDb(databaseName)
@@ -507,7 +490,7 @@ class MongoshNodeRepl implements EvaluationListener {
507490
databaseName: string,
508491
collectionName: string
509492
): Promise<JSONSchema> => {
510-
const mongo = this._getMongoByConnectionId(instanceState, connectionId);
493+
const mongo = instanceState.getMongoByConnectionId(connectionId);
511494
const docs = await mongo
512495
._getDb(databaseName)
513496
.getCollection(collectionName)
@@ -1435,12 +1418,4 @@ async function enterAsynchronousExecutionForPrompt(): Promise<void> {
14351418
await new Promise(setImmediate);
14361419
}
14371420

1438-
function connectionIdFromURI(uri: string): string {
1439-
// turn the uri into something we can safely use as part of a "filename"
1440-
// inside autocomplete
1441-
const hash = crypto.createHash('sha256');
1442-
hash.update(uri);
1443-
return hash.digest('hex');
1444-
}
1445-
14461421
export default MongoshNodeRepl;

packages/shell-api/src/mongo.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ type Mutable<T> = {
7373
-readonly [P in keyof T]: T[P];
7474
};
7575

76+
let nextId = 1;
77+
7678
@shellApiClassDefault
7779
@classPlatforms(['CLI'])
7880
export default class Mongo<
@@ -81,6 +83,7 @@ export default class Mongo<
8183
private __serviceProvider: ServiceProvider | null = null;
8284
public readonly _databases: Record<StringKey<M>, DatabaseWithSchema<M>> =
8385
Object.create(null);
86+
private _connectionId: number;
8487
public _instanceState: ShellInstanceState;
8588
public _connectionInfo: ConnectionInfo;
8689
private _explicitEncryptionOnly = false;
@@ -97,6 +100,7 @@ export default class Mongo<
97100
sp?: ServiceProvider
98101
) {
99102
super();
103+
this._connectionId = nextId++;
100104
this._instanceState = instanceState;
101105
if (sp) {
102106
this.__serviceProvider = sp;
@@ -299,6 +303,10 @@ export default class Mongo<
299303
) as CollectionWithSchema<M, M[KD], M[KD][KC]>;
300304
}
301305

306+
getConnectionId(): string {
307+
return `connection_${this._connectionId}`;
308+
}
309+
302310
getURI(): string {
303311
return this._uri;
304312
}

packages/shell-api/src/shell-instance-state.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,15 @@ export class ShellInstanceState {
402402
this.evaluationListener = listener;
403403
}
404404

405+
public getMongoByConnectionId(connectionId: string): Mongo {
406+
for (const mongo of this.mongos) {
407+
if (mongo.getConnectionId() === connectionId) {
408+
return mongo;
409+
}
410+
}
411+
throw new Error(`mongo with connection id ${connectionId} not found`);
412+
}
413+
405414
public getAutocompleteParameters(): AutocompleteParameters {
406415
return {
407416
topology: () => {

0 commit comments

Comments
 (0)