Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-4830): lazily import aws module #3476

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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