Skip to content

Commit

Permalink
fix(NODE-4830): lazily import aws module (mongodb#3476)
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Nov 29, 2022
1 parent cb68bf3 commit b33edd0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/cmap/auth/mongodb_aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as url from 'url';

import type { Binary, BSONSerializeOptions } from '../../bson';
import * as BSON from '../../bson';
import { aws4, credentialProvider } from '../../deps';
import { aws4, getAwsCredentialProvider } from '../../deps';
import {
MongoAWSError,
MongoCompatibilityError,
Expand Down Expand Up @@ -198,6 +198,8 @@ function makeTempCredentials(credentials: MongoCredentials, callback: Callback<M
);
}

const credentialProvider = getAwsCredentialProvider();

// Check if the AWS credential provider from the SDK is present. If not,
// use the old method.
if ('kModuleError' in credentialProvider) {
Expand Down
28 changes: 16 additions & 12 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,22 @@ type CredentialProvider = {
fromNodeProviderChain(this: void): () => Promise<AWSCredentials>;
};

export let credentialProvider: CredentialProvider | { kModuleError: MongoMissingDependencyError } =
makeErrorModule(
new MongoMissingDependencyError(
'Optional module `@aws-sdk/credential-providers` not found.' +
' Please install it to enable getting aws credentials via the official sdk.'
)
);

try {
// Ensure you always wrap an optional require in the try block NODE-3199
credentialProvider = require('@aws-sdk/credential-providers');
} catch {} // eslint-disable-line
export function getAwsCredentialProvider():
| CredentialProvider
| { kModuleError: MongoMissingDependencyError } {
try {
// Ensure you always wrap an optional require in the try block NODE-3199
const credentialProvider = require('@aws-sdk/credential-providers');
return credentialProvider;
} catch {
return makeErrorModule(
new MongoMissingDependencyError(
'Optional module `@aws-sdk/credential-providers` not found.' +
' Please install it to enable getting aws credentials via the official sdk.'
)
);
}
}

type SnappyLib = {
[PKG_VERSION]: { major: number; minor: number; patch: number };
Expand Down

0 comments on commit b33edd0

Please sign in to comment.