Skip to content

Commit 03e5e13

Browse files
added tests
added tests - no docker tests removed extraneous export and newline removed extraneous export and newline removed unnecesary helper funcs removed unnecesary let, changed to const added more tests to comply w kickoff - no docker tests still cleared up logic
1 parent 7c4ce27 commit 03e5e13

File tree

8 files changed

+82
-70
lines changed

8 files changed

+82
-70
lines changed

Diff for: src/cmap/connect.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
type ConnectionOptions,
2626
CryptoConnection
2727
} from './connection';
28-
import { addAllEnvClientMetadata, type ClientMetadata } from './handshake/client_metadata';
28+
import { addContainerMetadata } from './handshake/client_metadata';
2929
import {
3030
MAX_SUPPORTED_SERVER_VERSION,
3131
MAX_SUPPORTED_WIRE_VERSION,
@@ -183,7 +183,7 @@ export interface HandshakeDocument extends Document {
183183
ismaster?: boolean;
184184
hello?: boolean;
185185
helloOk?: boolean;
186-
client: ClientMetadata;
186+
client: Document;
187187
compression: string[];
188188
saslSupportedMechs?: string;
189189
loadBalanced?: boolean;
@@ -200,11 +200,12 @@ export async function prepareHandshakeDocument(
200200
const options = authContext.options;
201201
const compressors = options.compressors ? options.compressors : [];
202202
const { serverApi } = authContext.connection;
203-
const clientMetadata = await addAllEnvClientMetadata(options.internalMetadata);
203+
const clientMetadata = await addContainerMetadata(options.metadata);
204+
204205
const handshakeDoc: HandshakeDocument = {
205206
[serverApi?.version || options.loadBalanced === true ? 'hello' : LEGACY_HELLO_COMMAND]: 1,
206207
helloOk: true,
207-
client: clientMetadata.toObject(),
208+
client: clientMetadata,
208209
compression: compressors
209210
};
210211

Diff for: src/cmap/connection.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import {
5757
type WriteProtocolMessageType
5858
} from './commands';
5959
import type { Stream } from './connect';
60-
import type { ClientMetadata, LimitedSizeDocument } from './handshake/client_metadata';
60+
import type { ClientMetadata } from './handshake/client_metadata';
6161
import { StreamDescription, type StreamDescriptionOptions } from './stream_description';
6262
import { type CompressorName, decompressResponse } from './wire_protocol/compression';
6363
import { onData } from './wire_protocol/on_data';
@@ -119,8 +119,6 @@ export interface ConnectionOptions
119119
cancellationToken?: CancellationToken;
120120
metadata: ClientMetadata;
121121
/** @internal */
122-
internalMetadata: LimitedSizeDocument;
123-
/** @internal */
124122
mongoLogger?: MongoLogger | undefined;
125123
}
126124

Diff for: src/cmap/handshake/client_metadata.ts

+26-39
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { promises as fs } from 'fs';
22
import * as os from 'os';
33
import * as process from 'process';
44

5-
import { BSON, Int32 } from '../../bson';
5+
import { BSON, type Document, Int32 } from '../../bson';
66
import { MongoInvalidArgumentError } from '../../error';
77
import type { MongoOptions } from '../../mongo_client';
88

@@ -72,13 +72,13 @@ export class LimitedSizeDocument {
7272
return true;
7373
}
7474

75-
toObject(): ClientMetadata {
75+
toObject(): Document {
7676
return BSON.deserialize(BSON.serialize(this.document), {
7777
promoteLongs: false,
7878
promoteBuffers: false,
7979
promoteValues: false,
8080
useBigInt64: false
81-
}) as ClientMetadata;
81+
});
8282
}
8383
}
8484

@@ -92,17 +92,8 @@ type MakeClientMetadataOptions = Pick<MongoOptions, 'appName' | 'driverInfo'>;
9292
* 4. Truncate `platform`. -- special we do not truncate this field
9393
*/
9494
export function makeClientMetadata(options: MakeClientMetadataOptions): ClientMetadata {
95-
let metadataDocument = new LimitedSizeDocument(512);
96-
metadataDocument = addNonEnvClientMetadata(options, metadataDocument);
97-
metadataDocument = addFAASOnlyEnvClientMetadata(metadataDocument);
98-
return metadataDocument.toObject();
99-
}
95+
const metadataDocument = new LimitedSizeDocument(512);
10096

101-
/** @internal */
102-
export function addNonEnvClientMetadata(
103-
options: MakeClientMetadataOptions,
104-
metadataDocument: LimitedSizeDocument
105-
): LimitedSizeDocument {
10697
const { appName = '' } = options;
10798
// Add app name first, it must be sent
10899
if (appName.length > 0) {
@@ -152,11 +143,6 @@ export function addNonEnvClientMetadata(
152143
}
153144
}
154145

155-
return metadataDocument;
156-
}
157-
158-
/** @internal */
159-
function addFAASOnlyEnvClientMetadata(metadataDocument: LimitedSizeDocument): LimitedSizeDocument {
160146
const faasEnv = getFAASEnv();
161147
if (faasEnv != null) {
162148
if (!metadataDocument.ifItFitsItSits('env', faasEnv)) {
@@ -167,15 +153,13 @@ function addFAASOnlyEnvClientMetadata(metadataDocument: LimitedSizeDocument): Li
167153
}
168154
}
169155
}
170-
return metadataDocument;
156+
return metadataDocument.toObject() as ClientMetadata;
171157
}
172158

173159
let isDocker: boolean;
174160
let dockerPromise: any;
175161
/** @internal */
176-
export async function addAllEnvClientMetadata(metadataDocument: LimitedSizeDocument) {
177-
const faasEnv = getFAASEnv();
178-
162+
export async function addContainerMetadata(originalMetadata: ClientMetadata) {
179163
async function getContainerMetadata() {
180164
const containerMetadata: Record<string, any> = {};
181165
if (isDocker == null) {
@@ -189,30 +173,33 @@ export async function addAllEnvClientMetadata(metadataDocument: LimitedSizeDocum
189173
}
190174
const isKubernetes = process.env.KUBERNETES_SERVICE_HOST ? true : false;
191175

192-
if (isDocker || isKubernetes) {
193-
if (isDocker) {
194-
containerMetadata['runtime'] = 'docker';
195-
}
196-
if (isKubernetes) {
197-
containerMetadata['orchestrator'] = 'kubernetes';
198-
}
199-
}
176+
if (isDocker) containerMetadata['runtime'] = 'docker';
177+
if (isKubernetes) containerMetadata['orchestrator'] = 'kubernetes';
178+
200179
return containerMetadata;
201180
}
202181

203182
const containerMetadata = await getContainerMetadata();
204-
const envMetadata = faasEnv ?? new Map();
205-
envMetadata.set('container', containerMetadata);
206-
if (envMetadata != null) {
207-
if (!metadataDocument.ifItFitsItSits('env', envMetadata)) {
208-
for (const key of envMetadata.keys()) {
209-
envMetadata.delete(key);
210-
if (envMetadata.size === 0) break;
211-
if (metadataDocument.ifItFitsItSits('env', envMetadata)) break;
183+
if (Object.keys(containerMetadata).length === 0) return originalMetadata;
184+
185+
const extendedMetadata = new LimitedSizeDocument(512);
186+
const envMetadata = { ...originalMetadata?.env, container: containerMetadata };
187+
188+
for (const [key, val] of Object.entries(originalMetadata)) {
189+
if (key !== 'env') {
190+
extendedMetadata.ifItFitsItSits(key, val);
191+
} else {
192+
if (!extendedMetadata.ifItFitsItSits('env', envMetadata)) {
193+
extendedMetadata.ifItFitsItSits('env', val);
212194
}
213195
}
214196
}
215-
return metadataDocument;
197+
198+
if (!('env' in originalMetadata)) {
199+
extendedMetadata.ifItFitsItSits('env', envMetadata);
200+
}
201+
202+
return extendedMetadata.toObject();
216203
}
217204

218205
/**

Diff for: src/connection_string.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { URLSearchParams } from 'url';
55
import type { Document } from './bson';
66
import { MongoCredentials } from './cmap/auth/mongo_credentials';
77
import { AUTH_MECHS_AUTH_SRC_EXTERNAL, AuthMechanism } from './cmap/auth/providers';
8-
import { LimitedSizeDocument, addNonEnvClientMetadata, makeClientMetadata } from './cmap/handshake/client_metadata';
8+
import { makeClientMetadata } from './cmap/handshake/client_metadata';
99
import { Compressor, type CompressorName } from './cmap/wire_protocol/compression';
1010
import { Encrypter } from './encrypter';
1111
import {
@@ -544,7 +544,6 @@ export function parseOptions(
544544
);
545545

546546
mongoOptions.metadata = makeClientMetadata(mongoOptions);
547-
mongoOptions.internalMetadata = addNonEnvClientMetadata(mongoOptions, new LimitedSizeDocument(512));
548547

549548
return mongoOptions;
550549
}

Diff for: src/index.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,7 @@ export type {
287287
WaitQueueMember,
288288
WithConnectionCallback
289289
} from './cmap/connection_pool';
290-
export type {
291-
ClientMetadata,
292-
ClientMetadataOptions,
293-
LimitedSizeDocument
294-
} from './cmap/handshake/client_metadata';
290+
export type { ClientMetadata, ClientMetadataOptions } from './cmap/handshake/client_metadata';
295291
export type { ConnectionPoolMetrics } from './cmap/metrics';
296292
export type { StreamDescription, StreamDescriptionOptions } from './cmap/stream_description';
297293
export type { CompressorName } from './cmap/wire_protocol/compression';

Diff for: src/mongo_client.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import { AuthMechanism } from './cmap/auth/providers';
1515
import type { LEGAL_TCP_SOCKET_OPTIONS, LEGAL_TLS_SOCKET_OPTIONS } from './cmap/connect';
1616
import type { Connection } from './cmap/connection';
17-
import type { ClientMetadata, LimitedSizeDocument } from './cmap/handshake/client_metadata';
17+
import type { ClientMetadata } from './cmap/handshake/client_metadata';
1818
import type { CompressorName } from './cmap/wire_protocol/compression';
1919
import { parseOptions, resolveSRVRecord } from './connection_string';
2020
import { MONGO_CLIENT_EVENTS } from './constants';
@@ -897,11 +897,4 @@ export interface MongoOptions
897897
* TODO: NODE-5671 - remove internal flag
898898
*/
899899
mongodbLogPath?: 'stderr' | 'stdout' | MongoDBLogWritable;
900-
901-
/**
902-
* @internal
903-
* Metadata as BSON bytes.
904-
* Does not contain any environment information.
905-
*/
906-
internalMetadata: LimitedSizeDocument;
907900
}

Diff for: src/sdam/topology.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { BSONSerializeOptions, Document } from '../bson';
44
import type { MongoCredentials } from '../cmap/auth/mongo_credentials';
55
import type { ConnectionEvents, DestroyOptions } from '../cmap/connection';
66
import type { CloseOptions, ConnectionPoolEvents } from '../cmap/connection_pool';
7-
import type { ClientMetadata, LimitedSizeDocument } from '../cmap/handshake/client_metadata';
7+
import type { ClientMetadata } from '../cmap/handshake/client_metadata';
88
import { DEFAULT_OPTIONS, FEATURE_FLAGS } from '../connection_string';
99
import {
1010
CLOSE,
@@ -158,7 +158,6 @@ export interface TopologyOptions extends BSONSerializeOptions, ServerOptions {
158158
directConnection: boolean;
159159
loadBalanced: boolean;
160160
metadata: ClientMetadata;
161-
internalMetadata: LimitedSizeDocument;
162161
serverMonitoringMode: ServerMonitoringMode;
163162
/** MongoDB server API version */
164163
serverApi?: ServerApi;

Diff for: test/unit/cmap/connect.test.ts

+46-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const CONNECT_DEFAULTS = {
2626
loadBalanced: false
2727
};
2828

29-
describe('Connect Tests', function () {
29+
describe('Connect Tests', async function () {
3030
context('when PLAIN auth enabled', () => {
3131
const test: {
3232
server?: any;
@@ -185,9 +185,48 @@ describe('Connect Tests', function () {
185185
expect(error).to.be.instanceOf(MongoNetworkError);
186186
});
187187

188-
context('prepareHandshakeDocument', () => {
188+
context('prepareHandshakeDocument', async () => {
189+
context('when container is present', async () => {
190+
const authContext = {
191+
connection: {},
192+
options: { ...CONNECT_DEFAULTS }
193+
};
194+
195+
context('when only kubernetes is present', async () => {
196+
beforeEach(() => {
197+
process.env.KUBERNETES_SERVICE_HOST = 'I exist';
198+
});
199+
200+
afterEach(() => {
201+
process.env.KUBERNETES_SERVICE_HOST = '';
202+
});
203+
204+
it(`should include { orchestrator: 'kubernetes'} in client.env.container`, async () => {
205+
const handshakeDocument = await prepareHandshakeDocument(authContext);
206+
expect(handshakeDocument.client.env.container.orchestrator).to.equal('kubernetes');
207+
});
208+
209+
it(`should not have 'name' property in client.env `, async () => {
210+
const handshakeDocument = await prepareHandshakeDocument(authContext);
211+
expect(handshakeDocument.client.env).to.not.have.property('name');
212+
});
213+
});
214+
});
215+
216+
context('when container nor FAAS env is not present', async () => {
217+
const authContext = {
218+
connection: {},
219+
options: { ...CONNECT_DEFAULTS }
220+
};
221+
222+
it(`should not have 'env' property in client`, async () => {
223+
const handshakeDocument = await prepareHandshakeDocument(authContext);
224+
expect(handshakeDocument.client).to.not.have.property('env');
225+
});
226+
});
227+
189228
context('when serverApi.version is present', () => {
190-
const options = {};
229+
const options = { ...CONNECT_DEFAULTS };
191230
const authContext = {
192231
connection: { serverApi: { version: '1' } },
193232
options
@@ -200,7 +239,7 @@ describe('Connect Tests', function () {
200239
});
201240

202241
context('when serverApi is not present', () => {
203-
const options = {};
242+
const options = { ...CONNECT_DEFAULTS };
204243
const authContext = {
205244
connection: {},
206245
options
@@ -216,7 +255,7 @@ describe('Connect Tests', function () {
216255
context('when loadBalanced is not set as an option', () => {
217256
const authContext = {
218257
connection: {},
219-
options: {}
258+
options: { ...CONNECT_DEFAULTS }
220259
};
221260

222261
it('does not set loadBalanced on the handshake document', async () => {
@@ -238,7 +277,7 @@ describe('Connect Tests', function () {
238277
context('when loadBalanced is set to false', () => {
239278
const authContext = {
240279
connection: {},
241-
options: { loadBalanced: false }
280+
options: { ...CONNECT_DEFAULTS, loadBalanced: false }
242281
};
243282

244283
it('does not set loadBalanced on the handshake document', async () => {
@@ -260,7 +299,7 @@ describe('Connect Tests', function () {
260299
context('when loadBalanced is set to true', () => {
261300
const authContext = {
262301
connection: {},
263-
options: { loadBalanced: true }
302+
options: { ...CONNECT_DEFAULTS, loadBalanced: true }
264303
};
265304

266305
it('sets loadBalanced on the handshake document', async () => {

0 commit comments

Comments
 (0)