Skip to content

Commit 4870a9b

Browse files
initial POC
1 parent 0358360 commit 4870a9b

11 files changed

Lines changed: 133 additions & 83 deletions

File tree

.eslintrc.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@
276276
"patterns": [
277277
"**/../lib/**",
278278
"mongodb-mock-server",
279-
"node:*"
279+
"node:*",
280+
"os"
280281
],
281282
"paths": [
282283
{
@@ -327,4 +328,4 @@
327328
}
328329
}
329330
]
330-
}
331+
}

src/cmap/auth/gssapi.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as dns from 'dns';
2-
import * as os from 'os';
32

43
import { getKerberos, type Kerberos, type KerberosClient } from '../../deps';
54
import { MongoInvalidArgumentError, MongoMissingCredentialsError } from '../../error';
@@ -69,9 +68,13 @@ export class GSSAPI extends AuthProvider {
6968
}
7069
}
7170

72-
async function makeKerberosClient(authContext: AuthContext): Promise<KerberosClient> {
73-
const { hostAddress } = authContext.options;
74-
const { credentials } = authContext;
71+
async function makeKerberosClient({
72+
options: {
73+
hostAddress,
74+
runtime: { os }
75+
},
76+
credentials
77+
}: AuthContext): Promise<KerberosClient> {
7578
if (!hostAddress || typeof hostAddress.host !== 'string' || !credentials) {
7679
throw new MongoInvalidArgumentError(
7780
'Connection must have host and port and credentials defined.'

src/cmap/connection.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { type MongoClientAuthProviders } from '../mongo_client_auth_providers';
3535
import { MongoLoggableComponent, type MongoLogger, SeverityLevel } from '../mongo_logger';
3636
import { type Abortable, type CancellationToken, TypedEventEmitter } from '../mongo_types';
3737
import { ReadPreference, type ReadPreferenceLike } from '../read_preference';
38+
import { type Runtime } from '../runtime_adapters';
3839
import { ServerType } from '../sdam/common';
3940
import { applySession, type ClientSession, updateSessionFromResponse } from '../sessions';
4041
import { type TimeoutContext, TimeoutError } from '../timeout';
@@ -118,8 +119,8 @@ export interface ProxyOptions {
118119
/** @public */
119120
export interface ConnectionOptions
120121
extends SupportedNodeConnectionOptions,
121-
StreamDescriptionOptions,
122-
ProxyOptions {
122+
StreamDescriptionOptions,
123+
ProxyOptions {
123124
// Internal creation info
124125
id: number | '<monitor>';
125126
generation: number;
@@ -143,6 +144,8 @@ export interface ConnectionOptions
143144
metadata: Promise<ClientMetadata>;
144145
/** @internal */
145146
mongoLogger?: MongoLogger | undefined;
147+
/** @internal */
148+
runtime: Runtime;
146149
}
147150

148151
/** @public */
@@ -526,10 +529,10 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
526529
options.documentsReturnedIn == null || !options.raw
527530
? options
528531
: {
529-
...options,
530-
raw: false,
531-
fieldsAsRaw: { [options.documentsReturnedIn]: true }
532-
};
532+
...options,
533+
raw: false,
534+
fieldsAsRaw: { [options.documentsReturnedIn]: true }
535+
};
533536

534537
/** MongoDBResponse instance or subclass */
535538
let document: MongoDBResponse | undefined = undefined;
@@ -692,9 +695,9 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
692695
options.agreedCompressor === 'none' || !OpCompressedRequest.canCompress(command)
693696
? command
694697
: new OpCompressedRequest(command, {
695-
agreedCompressor: options.agreedCompressor ?? 'none',
696-
zlibCompressionLevel: options.zlibCompressionLevel ?? 0
697-
});
698+
agreedCompressor: options.agreedCompressor ?? 'none',
699+
zlibCompressionLevel: options.zlibCompressionLevel ?? 0
700+
});
698701

699702
const buffer = Buffer.concat(await finalCommand.toBin());
700703

src/cmap/handshake/client_metadata.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as os from 'os';
21
import * as process from 'process';
32

43
import { BSON, type Document, Int32, NumberUtils } from '../../bson';
@@ -96,7 +95,8 @@ export class LimitedSizeDocument {
9695
}
9796
}
9897

99-
type MakeClientMetadataOptions = Pick<MongoOptions, 'appName'>;
98+
type MakeClientMetadataOptions = Pick<MongoOptions, 'appName' | 'runtime'>;
99+
100100
/**
101101
* From the specs:
102102
* Implementors SHOULD cumulatively update fields in the following order until the document is under the size limit:
@@ -107,7 +107,7 @@ type MakeClientMetadataOptions = Pick<MongoOptions, 'appName'>;
107107
*/
108108
export async function makeClientMetadata(
109109
driverInfoList: DriverInfo[],
110-
{ appName = '' }: MakeClientMetadataOptions
110+
{ appName = '', runtime: { os } }: MakeClientMetadataOptions
111111
): Promise<ClientMetadata> {
112112
const metadataDocument = new LimitedSizeDocument(512);
113113

src/connection_string.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import { MongoLoggableComponent, MongoLogger, SeverityLevel } from './mongo_logger';
2121
import { ReadConcern, type ReadConcernLevel } from './read_concern';
2222
import { ReadPreference, type ReadPreferenceMode } from './read_preference';
23+
import { type Runtime } from './runtime_adapters';
2324
import { ServerMonitoringMode } from './sdam/monitor';
2425
import type { TagSet } from './sdam/server_description';
2526
import {
@@ -538,6 +539,13 @@ export function parseOptions(
538539
}
539540
);
540541

542+
const runtime: Runtime = {
543+
// eslint-disable-next-line @typescript-eslint/no-require-imports
544+
os: options.runtimeAdapters?.os ?? require('os')
545+
};
546+
547+
mongoOptions.runtime = runtime;
548+
541549
return mongoOptions;
542550
}
543551

@@ -1061,6 +1069,9 @@ export const OPTIONS = {
10611069
default: true,
10621070
type: 'boolean'
10631071
},
1072+
runtimeAdapters: {
1073+
type: 'record'
1074+
},
10641075
serializeFunctions: {
10651076
type: 'boolean'
10661077
},

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ export type {
562562
ReadPreferenceLikeOptions,
563563
ReadPreferenceOptions
564564
} from './read_preference';
565+
export type { OsAdapter, Runtime, RuntimeAdapters } from './runtime_adapters';
565566
export type { ClusterTime } from './sdam/common';
566567
export type {
567568
Monitor,

src/mongo_client.ts

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { EndSessionsOperation } from './operations/end_sessions';
4646
import { executeOperation } from './operations/execute_operation';
4747
import type { ReadConcern, ReadConcernLevel, ReadConcernLike } from './read_concern';
4848
import { ReadPreference, type ReadPreferenceMode } from './read_preference';
49+
import { type Runtime, type RuntimeAdapters } from './runtime_adapters';
4950
import type { ServerMonitoringMode } from './sdam/monitor';
5051
import type { TagSet } from './sdam/server_description';
5152
import { DeprioritizedServers, readPreferenceServerSelector } from './sdam/server_selection';
@@ -318,6 +319,8 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
318319
connectionType?: typeof Connection;
319320
/** @internal */
320321
__skipPingOnConnect?: boolean;
322+
/** @experimental */
323+
runtimeAdapters?: RuntimeAdapters;
321324
}
322325

323326
/** @public */
@@ -1032,39 +1035,39 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
10321035
*/
10331036
export interface MongoOptions
10341037
extends Required<
1035-
Pick<
1036-
MongoClientOptions,
1037-
| 'autoEncryption'
1038-
| 'connectTimeoutMS'
1039-
| 'directConnection'
1040-
| 'driverInfo'
1041-
| 'forceServerObjectId'
1042-
| 'minHeartbeatFrequencyMS'
1043-
| 'heartbeatFrequencyMS'
1044-
| 'localThresholdMS'
1045-
| 'maxConnecting'
1046-
| 'maxIdleTimeMS'
1047-
| 'maxPoolSize'
1048-
| 'minPoolSize'
1049-
| 'monitorCommands'
1050-
| 'noDelay'
1051-
| 'pkFactory'
1052-
| 'raw'
1053-
| 'replicaSet'
1054-
| 'retryReads'
1055-
| 'retryWrites'
1056-
| 'serverSelectionTimeoutMS'
1057-
| 'socketTimeoutMS'
1058-
| 'srvMaxHosts'
1059-
| 'srvServiceName'
1060-
| 'tlsAllowInvalidCertificates'
1061-
| 'tlsAllowInvalidHostnames'
1062-
| 'tlsInsecure'
1063-
| 'waitQueueTimeoutMS'
1064-
| 'zlibCompressionLevel'
1065-
>
1066-
>,
1067-
SupportedNodeConnectionOptions {
1038+
Pick<
1039+
MongoClientOptions,
1040+
| 'autoEncryption'
1041+
| 'connectTimeoutMS'
1042+
| 'directConnection'
1043+
| 'driverInfo'
1044+
| 'forceServerObjectId'
1045+
| 'minHeartbeatFrequencyMS'
1046+
| 'heartbeatFrequencyMS'
1047+
| 'localThresholdMS'
1048+
| 'maxConnecting'
1049+
| 'maxIdleTimeMS'
1050+
| 'maxPoolSize'
1051+
| 'minPoolSize'
1052+
| 'monitorCommands'
1053+
| 'noDelay'
1054+
| 'pkFactory'
1055+
| 'raw'
1056+
| 'replicaSet'
1057+
| 'retryReads'
1058+
| 'retryWrites'
1059+
| 'serverSelectionTimeoutMS'
1060+
| 'socketTimeoutMS'
1061+
| 'srvMaxHosts'
1062+
| 'srvServiceName'
1063+
| 'tlsAllowInvalidCertificates'
1064+
| 'tlsAllowInvalidHostnames'
1065+
| 'tlsInsecure'
1066+
| 'waitQueueTimeoutMS'
1067+
| 'zlibCompressionLevel'
1068+
>
1069+
>,
1070+
SupportedNodeConnectionOptions {
10681071
appName?: string;
10691072
hosts: HostAddress[];
10701073
srvHost?: string;
@@ -1152,4 +1155,7 @@ export interface MongoOptions
11521155
timeoutMS?: number;
11531156
/** @internal */
11541157
__skipPingOnConnect?: boolean;
1158+
1159+
/** @internal */
1160+
runtime: Runtime;
11551161
}

test/unit/assorted/optional_require.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ describe('optionalRequire', function () {
4141
const gssapi = new GSSAPI();
4242

4343
const error = await gssapi
44-
.auth(new AuthContext(null, true, { hostAddress: new HostAddress('a'), credentials: true }))
44+
.auth(new AuthContext(null, true, {
45+
hostAddress: new HostAddress('a'), credentials: true, runtime: { os: require('os') }
46+
}))
4547
.then(
4648
() => null,
4749
e => e

test/unit/cmap/connect.test.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ describe('Connect Tests', function () {
210210
connection: {},
211211
options: {
212212
...CONNECT_DEFAULTS,
213-
metadata: makeClientMetadata([], {})
213+
metadata: makeClientMetadata([], {
214+
runtime: { os: require('os') }
215+
})
214216
}
215217
};
216218
});
@@ -239,7 +241,9 @@ describe('Connect Tests', function () {
239241
name: 's'.repeat(128)
240242
}
241243
],
242-
{ appName: longAppName }
244+
{
245+
appName: longAppName, runtime: { os: require('os') }
246+
}
243247
);
244248
const longAuthContext = {
245249
connection: {},
@@ -267,7 +271,9 @@ describe('Connect Tests', function () {
267271
connection: {},
268272
options: {
269273
...CONNECT_DEFAULTS,
270-
metadata: makeClientMetadata([], {})
274+
metadata: makeClientMetadata([], {
275+
runtime: { os: require('os') }
276+
})
271277
}
272278
};
273279
});
@@ -296,7 +302,10 @@ describe('Connect Tests', function () {
296302
name: 's'.repeat(128)
297303
}
298304
],
299-
{ appName: longAppName }
305+
{
306+
appName: longAppName,
307+
runtime: { os: require('os') }
308+
}
300309
);
301310
const longAuthContext = {
302311
connection: {},

0 commit comments

Comments
 (0)