Skip to content

Commit 9e0e0ff

Browse files
cleanup implementation, fix tests
1 parent 58d8976 commit 9e0e0ff

4 files changed

Lines changed: 38 additions & 15 deletions

File tree

src/connection_string.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +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';
23+
import { resolveRuntimeAdapters } from './runtime_adapters';
2424
import { ServerMonitoringMode } from './sdam/monitor';
2525
import type { TagSet } from './sdam/server_description';
2626
import {
@@ -539,12 +539,7 @@ export function parseOptions(
539539
}
540540
);
541541

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;
542+
mongoOptions.runtime = resolveRuntimeAdapters(options);
548543

549544
return mongoOptions;
550545
}

src/mongo_client.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,11 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
319319
connectionType?: typeof Connection;
320320
/** @internal */
321321
__skipPingOnConnect?: boolean;
322-
/** @experimental */
322+
/**
323+
* @experimental
324+
*
325+
* If provided, any adapters provided will be used in place of the corresponding Node.js module.
326+
*/
323327
runtimeAdapters?: RuntimeAdapters;
324328
}
325329

src/runtime_adapters.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
/* eslint-disable no-restricted-imports */
2+
// We squash the restricted import errors here because we are using type-only imports, which
3+
// do not impact the driver's actual runtime dependencies.
4+
5+
import type * as os from 'os';
6+
7+
import { type MongoClientOptions } from './mongo_client';
8+
19
/**
210
* @public
311
* @experimental
12+
*
13+
* Represents the set of dependencies that the driver uses from the [Node.js OS module](https://nodejs.org/api/os.html).
414
*/
5-
export type OsAdapter = Pick<typeof import('os'), 'platform' | 'release' | 'arch' | 'type'>;
15+
export type OsAdapter = Pick<typeof os, 'release' | 'platform' | 'arch' | 'type'>;
616

717
/**
818
* @public
919
* @experimental
1020
*
11-
* This type represents the interface that the driver needs from the runtime in order to function.
21+
* This type represents the set of dependencies that the driver needs from the Javascript runtime in order to function.
1222
*/
1323
export interface RuntimeAdapters {
1424
os?: OsAdapter;
@@ -18,8 +28,21 @@ export interface RuntimeAdapters {
1828
* @internal
1929
*
2030
* Represents a complete, parsed set of runtime adapters. After options parsing, all adapters
21-
* are always present (either using the user's provided adapter, or defaulting to Nodejs' module).
31+
* are always present (either using the user's provided adapter, or defaulting to the Node.js module).
2232
*/
2333
export interface Runtime {
2434
os: OsAdapter;
2535
}
36+
37+
/**
38+
* @internal
39+
*
40+
* Given a MongoClientOptions, this function resolves the set of runtime options, providing Nodejs implementations if
41+
* not provided by in `options`, and returns a `Runtime`.
42+
*/
43+
export function resolveRuntimeAdapters(options: MongoClientOptions): Runtime {
44+
return {
45+
// eslint-disable-next-line @typescript-eslint/no-require-imports
46+
os: options.runtimeAdapters?.os ?? require('os')
47+
};
48+
}

test/tools/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import * as path from 'node:path';
77
import { EJSON } from 'bson';
88
import * as BSON from 'bson';
99
import { expect } from 'chai';
10-
import * as os from 'os';
1110
import * as process from 'process';
1211
import { Readable } from 'stream';
1312
import { setTimeout } from 'timers';
@@ -24,6 +23,7 @@ import {
2423
type TopologyOptions
2524
} from '../../src';
2625
import { OP_MSG } from '../../src/cmap/wire_protocol/constants';
26+
import { resolveRuntimeAdapters } from '../../src/runtime_adapters';
2727
import { Topology } from '../../src/sdam/topology';
2828
import { processTimeMS } from '../../src/utils';
2929
import { type TestConfiguration } from './runner/config';
@@ -607,6 +607,7 @@ export function configureMongocryptdSpawnHooks(
607607
};
608608
}
609609

610-
export const runtime: Runtime = {
611-
os
612-
};
610+
/**
611+
* A `Runtime` that resolves to entirely Nodejs modules, useful when tests must provide a default `runtime` object to an API.
612+
*/
613+
export const runtime: Runtime = resolveRuntimeAdapters({});

0 commit comments

Comments
 (0)