Skip to content

Commit 5a865f0

Browse files
authored
chore(smithy-client): emitWarningIfUnsupportedVersion (#2619)
1 parent 0d11e90 commit 5a865f0

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
describe("emitWarningIfUnsupportedVersion", () => {
2+
let emitWarningIfUnsupportedVersion;
3+
const emitWarning = process.emitWarning;
4+
const supportedVersion = "12.0.0";
5+
6+
beforeEach(() => {
7+
const module = require("./emitWarningIfUnsupportedVersion");
8+
emitWarningIfUnsupportedVersion = module.emitWarningIfUnsupportedVersion;
9+
});
10+
11+
afterEach(() => {
12+
jest.clearAllMocks();
13+
jest.resetModules();
14+
process.emitWarning = emitWarning;
15+
});
16+
17+
describe(`emits warning for Node.js <${supportedVersion}`, () => {
18+
const getPreviousMajorVersion = (major: number) => (major === 0 ? 0 : major - 1);
19+
20+
const getPreviousMinorVersion = ([major, minor]: [number, number]) =>
21+
minor === 0 ? [getPreviousMajorVersion(major), 9] : [major, minor - 1];
22+
23+
const getPreviousPatchVersion = ([major, minor, patch]: [number, number, number]) =>
24+
patch === 0 ? [...getPreviousMinorVersion([major, minor]), 9] : [major, minor, patch - 1];
25+
26+
const [major, minor, patch] = supportedVersion.split(".").map(Number);
27+
it.each(
28+
[
29+
getPreviousPatchVersion([major, minor, patch]),
30+
[...getPreviousMinorVersion([major, minor]), 0],
31+
[getPreviousMajorVersion(major), 0, 0],
32+
].map((arr) => `v${arr.join(".")}`)
33+
)(`%s`, async (unsupportedVersion) => {
34+
process.emitWarning = jest.fn();
35+
emitWarningIfUnsupportedVersion(unsupportedVersion);
36+
37+
// Verify that the warning was emitted.
38+
expect(process.emitWarning).toHaveBeenCalledTimes(1);
39+
expect(process.emitWarning).toHaveBeenCalledWith(
40+
`The AWS SDK for JavaScript (v3) will\n` +
41+
`no longer support Node.js ${unsupportedVersion} as of January 1, 2022.\n` +
42+
`To continue receiving updates to AWS services, bug fixes, and security\n` +
43+
`updates please upgrade to Node.js 12.x or later.\n\n` +
44+
`More information can be found at: https://a.co/1l6FLnu`,
45+
`NodeDeprecationWarning`
46+
);
47+
48+
// Verify that the warning emits only once.
49+
emitWarningIfUnsupportedVersion(unsupportedVersion);
50+
expect(process.emitWarning).toHaveBeenCalledTimes(1);
51+
});
52+
});
53+
54+
describe(`emits no warning for Node.js >=${supportedVersion}`, () => {
55+
const [major, minor, patch] = supportedVersion.split(".").map(Number);
56+
it.each(
57+
[
58+
[major, minor, patch],
59+
[major, minor, patch + 1],
60+
[major, minor + 1, 0],
61+
[major + 1, 0, 0],
62+
].map((arr) => `v${arr.join(".")}`)
63+
)(`%s`, async (unsupportedVersion) => {
64+
process.emitWarning = jest.fn();
65+
emitWarningIfUnsupportedVersion(unsupportedVersion);
66+
expect(process.emitWarning).not.toHaveBeenCalled();
67+
});
68+
});
69+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Stores whether the warning was already emitted.
2+
let warningEmitted = false;
3+
4+
/**
5+
* Emits warning if the provided Node.js version string is pending deprecation.
6+
*
7+
* @param {string} version - The Node.js version string.
8+
*/
9+
export const emitWarningIfUnsupportedVersion = (version: string) => {
10+
if (version && !warningEmitted && parseInt(version.substring(1, version.indexOf("."))) < 12) {
11+
warningEmitted = true;
12+
process.emitWarning(
13+
`The AWS SDK for JavaScript (v3) will\n` +
14+
`no longer support Node.js ${version} as of January 1, 2022.\n` +
15+
`To continue receiving updates to AWS services, bug fixes, and security\n` +
16+
`updates please upgrade to Node.js 12.x or later.\n\n` +
17+
`More information can be found at: https://a.co/1l6FLnu`,
18+
`NodeDeprecationWarning`
19+
);
20+
}
21+
};

packages/smithy-client/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./client";
22
export * from "./command";
3+
export * from "./emitWarningIfUnsupportedVersion";
34
export * from "./extended-encode-uri-component";
45
export * from "./get-array-if-single-item";
56
export * from "./get-value-from-text-node";

0 commit comments

Comments
 (0)