@@ -2,13 +2,30 @@ import * as os from 'os';
2
2
import * as process from 'process' ;
3
3
4
4
import { BSON , type Document , Int32 } from '../../bson' ;
5
- import { MongoInvalidArgumentError , MongoRuntimeError } from '../../error' ;
6
- import type { MongoOptions } from '../../mongo_client' ;
5
+ import { MongoInvalidArgumentError } from '../../error' ;
6
+ import type { DriverInfo , MongoOptions } from '../../mongo_client' ;
7
7
import { fileIsAccessible } from '../../utils' ;
8
8
9
9
// eslint-disable-next-line @typescript-eslint/no-require-imports
10
10
const NODE_DRIVER_VERSION = require ( '../../../package.json' ) . version ;
11
11
12
+ /** @internal */
13
+ export function isDriverInfoEqual ( info1 : DriverInfo , info2 : DriverInfo ) : boolean {
14
+ /** for equality comparison, we consider "" as unset */
15
+ const nonEmptyCmp = ( s1 : string | undefined , s2 : string | undefined ) : boolean => {
16
+ s1 ||= undefined ;
17
+ s2 ||= undefined ;
18
+
19
+ return s1 === s2 ;
20
+ } ;
21
+
22
+ return (
23
+ nonEmptyCmp ( info1 . name , info2 . name ) &&
24
+ nonEmptyCmp ( info1 . platform , info2 . platform ) &&
25
+ nonEmptyCmp ( info1 . version , info2 . version )
26
+ ) ;
27
+ }
28
+
12
29
/**
13
30
* @public
14
31
* @deprecated This interface will be made internal in the next major release.
@@ -90,10 +107,7 @@ export class LimitedSizeDocument {
90
107
}
91
108
}
92
109
93
- type MakeClientMetadataOptions = Pick <
94
- MongoOptions ,
95
- 'appName' | 'driverInfo' | 'additionalDriverInfo'
96
- > ;
110
+ type MakeClientMetadataOptions = Pick < MongoOptions , 'appName' > ;
97
111
/**
98
112
* From the specs:
99
113
* Implementors SHOULD cumulatively update fields in the following order until the document is under the size limit:
@@ -102,34 +116,28 @@ type MakeClientMetadataOptions = Pick<
102
116
* 3. Omit the `env` document entirely.
103
117
* 4. Truncate `platform`. -- special we do not truncate this field
104
118
*/
105
- export function makeClientMetadata ( options : MakeClientMetadataOptions ) : ClientMetadata {
119
+ export function makeClientMetadata (
120
+ driverInfoList : DriverInfo [ ] ,
121
+ { appName = '' } : MakeClientMetadataOptions
122
+ ) : ClientMetadata {
106
123
const metadataDocument = new LimitedSizeDocument ( 512 ) ;
107
124
108
- const { appName = '' } = options ;
109
125
// Add app name first, it must be sent
110
126
if ( appName . length > 0 ) {
111
127
const name =
112
128
Buffer . byteLength ( appName , 'utf8' ) <= 128
113
- ? options . appName
129
+ ? appName
114
130
: Buffer . from ( appName , 'utf8' ) . subarray ( 0 , 128 ) . toString ( 'utf8' ) ;
115
131
metadataDocument . ifItFitsItSits ( 'application' , { name } ) ;
116
132
}
117
133
118
- const { name = '' , version = '' , platform = '' } = options . driverInfo ;
119
-
120
134
const driverInfo = {
121
- name : name . length > 0 ? `nodejs| ${ name } ` : 'nodejs' ,
122
- version : version . length > 0 ? ` ${ NODE_DRIVER_VERSION } | ${ version } ` : NODE_DRIVER_VERSION
135
+ name : 'nodejs' ,
136
+ version : NODE_DRIVER_VERSION
123
137
} ;
124
138
125
- if ( options . additionalDriverInfo == null ) {
126
- throw new MongoRuntimeError (
127
- 'Client options `additionalDriverInfo` must always default to an empty array'
128
- ) ;
129
- }
130
-
131
139
// This is where we handle additional driver info added after client construction.
132
- for ( const { name : n = '' , version : v = '' } of options . additionalDriverInfo ) {
140
+ for ( const { name : n = '' , version : v = '' } of driverInfoList ) {
133
141
if ( n . length > 0 ) {
134
142
driverInfo . name = `${ driverInfo . name } |${ n } ` ;
135
143
}
@@ -145,13 +153,10 @@ export function makeClientMetadata(options: MakeClientMetadataOptions): ClientMe
145
153
}
146
154
147
155
let runtimeInfo = getRuntimeInfo ( ) ;
148
- if ( platform . length > 0 ) {
149
- runtimeInfo = `${ runtimeInfo } |${ platform } ` ;
150
- }
151
-
152
- for ( const { platform : p = '' } of options . additionalDriverInfo ) {
153
- if ( p . length > 0 ) {
154
- runtimeInfo = `${ runtimeInfo } |${ p } ` ;
156
+ // This is where we handle additional driver info added after client construction.
157
+ for ( const { platform = '' } of driverInfoList ) {
158
+ if ( platform . length > 0 ) {
159
+ runtimeInfo = `${ runtimeInfo } |${ platform } ` ;
155
160
}
156
161
}
157
162
@@ -210,7 +215,9 @@ async function getContainerMetadata() {
210
215
* Re-add each metadata value.
211
216
* Attempt to add new env container metadata, but keep old data if it does not fit.
212
217
*/
213
- export async function addContainerMetadata ( originalMetadata : ClientMetadata ) {
218
+ export async function addContainerMetadata (
219
+ originalMetadata : ClientMetadata
220
+ ) : Promise < ClientMetadata > {
214
221
const containerMetadata = await getContainerMetadata ( ) ;
215
222
if ( Object . keys ( containerMetadata ) . length === 0 ) return originalMetadata ;
216
223
@@ -233,7 +240,7 @@ export async function addContainerMetadata(originalMetadata: ClientMetadata) {
233
240
extendedMetadata . ifItFitsItSits ( 'env' , extendedEnvMetadata ) ;
234
241
}
235
242
236
- return extendedMetadata . toObject ( ) ;
243
+ return extendedMetadata . toObject ( ) as ClientMetadata ;
237
244
}
238
245
239
246
/**
0 commit comments