Skip to content

chore(smithy-client): emitWarningIfUnsupportedVersion #2619

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

Merged
merged 2 commits into from
Jul 26, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
describe("emitWarningIfUnsupportedVersion", () => {
let emitWarningIfUnsupportedVersion;
const emitWarning = process.emitWarning;
const supportedVersion = "12.0.0";

beforeEach(() => {
const module = require("./emitWarningIfUnsupportedVersion");
emitWarningIfUnsupportedVersion = module.emitWarningIfUnsupportedVersion;
});

afterEach(() => {
jest.clearAllMocks();
jest.resetModules();
process.emitWarning = emitWarning;
});

describe(`emits warning for Node.js <${supportedVersion}`, () => {
const getPreviousMajorVersion = (major: number) => (major === 0 ? 0 : major - 1);

const getPreviousMinorVersion = ([major, minor]: [number, number]) =>
minor === 0 ? [getPreviousMajorVersion(major), 9] : [major, minor - 1];

const getPreviousPatchVersion = ([major, minor, patch]: [number, number, number]) =>
patch === 0 ? [...getPreviousMinorVersion([major, minor]), 9] : [major, minor, patch - 1];

const [major, minor, patch] = supportedVersion.split(".").map(Number);
it.each(
[
getPreviousPatchVersion([major, minor, patch]),
[...getPreviousMinorVersion([major, minor]), 0],
[getPreviousMajorVersion(major), 0, 0],
].map((arr) => `v${arr.join(".")}`)
)(`%s`, async (unsupportedVersion) => {
process.emitWarning = jest.fn();
emitWarningIfUnsupportedVersion(unsupportedVersion);

// Verify that the warning was emitted.
expect(process.emitWarning).toHaveBeenCalledTimes(1);
expect(process.emitWarning).toHaveBeenCalledWith(
`The AWS SDK for JavaScript (v3) will\n` +
`no longer support Node.js ${unsupportedVersion} as of January 1, 2022.\n` +
`To continue receiving updates to AWS services, bug fixes, and security\n` +
`updates please upgrade to Node.js 12.x or later.\n\n` +
`More information can be found at: https://a.co/1l6FLnu`,
`NodeDeprecationWarning`
);

// Verify that the warning emits only once.
emitWarningIfUnsupportedVersion(unsupportedVersion);
expect(process.emitWarning).toHaveBeenCalledTimes(1);
});
});

describe(`emits no warning for Node.js >=${supportedVersion}`, () => {
const [major, minor, patch] = supportedVersion.split(".").map(Number);
it.each(
[
[major, minor, patch],
[major, minor, patch + 1],
[major, minor + 1, 0],
[major + 1, 0, 0],
].map((arr) => `v${arr.join(".")}`)
)(`%s`, async (unsupportedVersion) => {
process.emitWarning = jest.fn();
emitWarningIfUnsupportedVersion(unsupportedVersion);
expect(process.emitWarning).not.toHaveBeenCalled();
});
});
});
21 changes: 21 additions & 0 deletions packages/smithy-client/src/emitWarningIfUnsupportedVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Stores whether the warning was already emitted.
let warningEmitted = false;

/**
* Emits warning if the provided Node.js version string is pending deprecation.
*
* @param {string} version - The Node.js version string.
*/
export const emitWarningIfUnsupportedVersion = (version: string) => {
if (version && !warningEmitted && parseInt(version.substring(1, version.indexOf("."))) < 12) {
warningEmitted = true;
process.emitWarning(
`The AWS SDK for JavaScript (v3) will\n` +
`no longer support Node.js ${version} as of January 1, 2022.\n` +
`To continue receiving updates to AWS services, bug fixes, and security\n` +
`updates please upgrade to Node.js 12.x or later.\n\n` +
`More information can be found at: https://a.co/1l6FLnu`,
`NodeDeprecationWarning`
);
}
};
1 change: 1 addition & 0 deletions packages/smithy-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./client";
export * from "./command";
export * from "./emitWarningIfUnsupportedVersion";
export * from "./extended-encode-uri-component";
export * from "./get-array-if-single-item";
export * from "./get-value-from-text-node";
Expand Down