@openrewrite/recipes-nodejs 0.72.0 → 0.73.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/index.d.ts.map +1 -1
  2. package/dist/index.js +9 -0
  3. package/dist/index.js.map +1 -1
  4. package/dist/migrate/aws-sdk-smithy/aws-sdk-smithy-support.d.ts +11 -0
  5. package/dist/migrate/aws-sdk-smithy/aws-sdk-smithy-support.d.ts.map +1 -0
  6. package/dist/migrate/aws-sdk-smithy/aws-sdk-smithy-support.js +44 -0
  7. package/dist/migrate/aws-sdk-smithy/aws-sdk-smithy-support.js.map +1 -0
  8. package/dist/migrate/aws-sdk-smithy/keep-aws-sdk-retry-attempts.d.ts +19 -0
  9. package/dist/migrate/aws-sdk-smithy/keep-aws-sdk-retry-attempts.d.ts.map +1 -0
  10. package/dist/migrate/aws-sdk-smithy/keep-aws-sdk-retry-attempts.js +195 -0
  11. package/dist/migrate/aws-sdk-smithy/keep-aws-sdk-retry-attempts.js.map +1 -0
  12. package/dist/migrate/aws-sdk-smithy/keep-node-http-handler-idle-timeout.d.ts +9 -0
  13. package/dist/migrate/aws-sdk-smithy/keep-node-http-handler-idle-timeout.d.ts.map +1 -0
  14. package/dist/migrate/aws-sdk-smithy/keep-node-http-handler-idle-timeout.js +101 -0
  15. package/dist/migrate/aws-sdk-smithy/keep-node-http-handler-idle-timeout.js.map +1 -0
  16. package/dist/migrate/aws-sdk-smithy/migrate-aws-sdk-packages-to-smithy.d.ts +10 -0
  17. package/dist/migrate/aws-sdk-smithy/migrate-aws-sdk-packages-to-smithy.d.ts.map +1 -0
  18. package/dist/migrate/aws-sdk-smithy/migrate-aws-sdk-packages-to-smithy.js +43 -0
  19. package/dist/migrate/aws-sdk-smithy/migrate-aws-sdk-packages-to-smithy.js.map +1 -0
  20. package/dist/migrate/aws-sdk-smithy/replace-aws-sdk-smithy-imports.d.ts +9 -0
  21. package/dist/migrate/aws-sdk-smithy/replace-aws-sdk-smithy-imports.d.ts.map +1 -0
  22. package/dist/migrate/aws-sdk-smithy/replace-aws-sdk-smithy-imports.js +144 -0
  23. package/dist/migrate/aws-sdk-smithy/replace-aws-sdk-smithy-imports.js.map +1 -0
  24. package/dist/migrate/aws-sdk-smithy/smithy-twins.d.ts +7 -0
  25. package/dist/migrate/aws-sdk-smithy/smithy-twins.d.ts.map +1 -0
  26. package/dist/migrate/aws-sdk-smithy/smithy-twins.js +334 -0
  27. package/dist/migrate/aws-sdk-smithy/smithy-twins.js.map +1 -0
  28. package/dist/migrate/aws-sdk-smithy/swap-aws-sdk-smithy-dependencies.d.ts +36 -0
  29. package/dist/migrate/aws-sdk-smithy/swap-aws-sdk-smithy-dependencies.d.ts.map +1 -0
  30. package/dist/migrate/aws-sdk-smithy/swap-aws-sdk-smithy-dependencies.js +290 -0
  31. package/dist/migrate/aws-sdk-smithy/swap-aws-sdk-smithy-dependencies.js.map +1 -0
  32. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-node-support.d.ts +1 -0
  33. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-node-support.d.ts.map +1 -1
  34. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-node-support.js +7 -0
  35. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-node-support.js.map +1 -1
  36. package/dist/resources/advisories-npm.csv +54 -4
  37. package/package.json +2 -2
  38. package/src/index.ts +9 -0
  39. package/src/migrate/aws-sdk-smithy/aws-sdk-smithy-support.ts +66 -0
  40. package/src/migrate/aws-sdk-smithy/keep-aws-sdk-retry-attempts.ts +151 -0
  41. package/src/migrate/aws-sdk-smithy/keep-node-http-handler-idle-timeout.ts +89 -0
  42. package/src/migrate/aws-sdk-smithy/migrate-aws-sdk-packages-to-smithy.ts +33 -0
  43. package/src/migrate/aws-sdk-smithy/replace-aws-sdk-smithy-imports.ts +99 -0
  44. package/src/migrate/aws-sdk-smithy/smithy-twins.ts +346 -0
  45. package/src/migrate/aws-sdk-smithy/swap-aws-sdk-smithy-dependencies.ts +252 -0
  46. package/src/migrate/aws-sdk-v3/aws-sdk-v3-node-support.ts +10 -0
package/src/index.ts CHANGED
@@ -73,6 +73,10 @@ import {ReplaceHpagent} from "./migrate/hpagent/replace-hpagent";
73
73
  import {FindHpagentManualMigrations} from "./migrate/hpagent/find-hpagent-manual-migrations";
74
74
  import {SwapHpagentDependency} from "./migrate/hpagent/swap-hpagent-dependency";
75
75
  import {MigrateAwsSdkV2ToV3} from "./migrate/aws-sdk-v3/migrate-aws-sdk-v2-to-v3";
76
+ import {MigrateAwsSdkPackagesToSmithy} from "./migrate/aws-sdk-smithy/migrate-aws-sdk-packages-to-smithy";
77
+ import {ReplaceAwsSdkSmithyImports} from "./migrate/aws-sdk-smithy/replace-aws-sdk-smithy-imports";
78
+ import {KeepNodeHttpHandlerIdleTimeout} from "./migrate/aws-sdk-smithy/keep-node-http-handler-idle-timeout";
79
+ import {DeclareSmithyTwins, SwapAwsSdkSmithyDependencies} from "./migrate/aws-sdk-smithy/swap-aws-sdk-smithy-dependencies";
76
80
  import {ReplaceAwsSdkV2Clients} from "./migrate/aws-sdk-v3/replace-aws-sdk-v2-clients";
77
81
  import {FindAwsSdkV2ManualMigrations} from "./migrate/aws-sdk-v3/find-aws-sdk-v2-manual-migrations";
78
82
  import {AddAwsSdkV3Dependencies} from "./migrate/aws-sdk-v3/add-aws-sdk-v3-dependencies";
@@ -210,6 +214,11 @@ export async function activate(marketplace: RecipeMarketplace): Promise<void> {
210
214
  await marketplace.install(FindAwsSdkV2ManualMigrations, AwsSdk);
211
215
  await marketplace.install(AddAwsSdkV3Dependencies, AwsSdk);
212
216
  await marketplace.install(RemoveUnusedAwsSdkV2Dependency, AwsSdk);
217
+ await marketplace.install(MigrateAwsSdkPackagesToSmithy, AwsSdk);
218
+ await marketplace.install(ReplaceAwsSdkSmithyImports, AwsSdk);
219
+ await marketplace.install(KeepNodeHttpHandlerIdleTimeout, AwsSdk);
220
+ await marketplace.install(SwapAwsSdkSmithyDependencies, AwsSdk);
221
+ await marketplace.install(DeclareSmithyTwins, AwsSdk);
213
222
  await marketplace.install(MigrateSourceMapSupportToNative, SourceMapSupport);
214
223
  await marketplace.install(ReplaceSourceMapSupportInstall, SourceMapSupport);
215
224
  await marketplace.install(ReplaceSourceMapSupportApi, SourceMapSupport);
@@ -0,0 +1,66 @@
1
+ /*
2
+ * Copyright 2026 the original author or authors.
3
+ *
4
+ * Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
5
+ */
6
+
7
+ import {J} from "@openrewrite/rewrite/java";
8
+ import {withStringValue} from "@openrewrite/rewrite/javascript";
9
+ import {SmithyRelease, SMITHY_TWINS} from "./smithy-twins";
10
+
11
+ /** `@aws-sdk/<name>` for each twin, in a stable order. */
12
+ export const MOVED_PACKAGES: readonly string[] = Object.keys(SMITHY_TWINS).sort().map(name => `@aws-sdk/${name}`);
13
+
14
+ /** `@smithy/<name>` for a moved `@aws-sdk/<name>`, else undefined. */
15
+ export function smithyTwin(pkg: string): string | undefined {
16
+ const name = pkg.startsWith('@aws-sdk/') ? pkg.substring('@aws-sdk/'.length) : undefined;
17
+ return name !== undefined && SMITHY_TWINS[name] ? `@smithy/${name}` : undefined;
18
+ }
19
+
20
+ /**
21
+ * The `@smithy/<name>` range for a lock group whose oldest declared Node.js is `node` and whose `@aws-sdk/*` clients
22
+ * build on `@smithy/types` major `types`: the newest release of that line, so the clients and the package share
23
+ * their types, else the newest release that Node.js runs, or the newest of all where no Node.js is declared. Below
24
+ * the lowest floor, the newest that the oldest Node.js of any line runs.
25
+ */
26
+ export function smithyRangeFor(pkg: string, node: number | undefined, types?: number): string {
27
+ const lines = SMITHY_TWINS[pkg.substring(pkg.indexOf('/') + 1)];
28
+ const runs = (r: SmithyRelease) => node === undefined || r.node <= node;
29
+ const aligned = types === undefined ? undefined : lines.find(r => r.types === types && runs(r));
30
+ const lowest = Math.min(...lines.map(r => r.node));
31
+ return (aligned ?? lines.find(runs) ?? lines.find(r => r.node === lowest)!).range;
32
+ }
33
+
34
+ /** The module a specifier names: the package, without a subpath. */
35
+ function packageOf(specifier: string): string {
36
+ const parts = specifier.split('/');
37
+ return specifier.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
38
+ }
39
+
40
+ /** The specifier with a moved `@aws-sdk/<name>` replaced by `@smithy/<name>`, any subpath kept, else undefined. */
41
+ export function movedSpecifier(e: J | undefined): J.Literal | undefined {
42
+ if (e?.kind !== J.Kind.Literal) return undefined;
43
+ const literal = e as J.Literal;
44
+ if (typeof literal.value !== 'string') return undefined;
45
+ const pkg = packageOf(literal.value);
46
+ const twin = smithyTwin(pkg);
47
+ return twin ? withStringValue(literal, twin + literal.value.substring(pkg.length)) : undefined;
48
+ }
49
+
50
+ /** Exports a moved package had that its `@smithy/*` twin dropped in a later major: `StringWrapper` from 3.x on. */
51
+ export const DROPPED_EXPORTS: Readonly<Record<string, readonly string[]>> = {
52
+ '@aws-sdk/smithy-client': ['StringWrapper'],
53
+ };
54
+
55
+ /** The first `@aws-sdk/*` release whose packages depend on the `@smithy/*` twins rather than the moved packages. */
56
+ export const FIRST_ON_SMITHY = '3.363.0';
57
+
58
+ /**
59
+ * The first `@aws-sdk/*` release whose enum-typed members no longer also accept `string`, so a widened string literal
60
+ * that compiled before no longer does. A client is raised to the releases before it, which build on `@smithy/types` 2.
61
+ */
62
+ export const ENUMS_NARROWED = '3.429.0';
63
+ export const RAISED_SMITHY_TYPES = 2;
64
+
65
+ /** The first `aws-sdk-client-mock` release whose types accept a client from `FIRST_ON_SMITHY` on. */
66
+ export const CLIENT_MOCK_ON_SMITHY = '3.0.0';
@@ -0,0 +1,151 @@
1
+ /*
2
+ * Copyright 2026 the original author or authors.
3
+ *
4
+ * Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
5
+ */
6
+
7
+ import {ExecutionContext, foundSearchResult, ScanningRecipe, Tree, TreeVisitor} from "@openrewrite/rewrite";
8
+ import {isIdentifier, J} from "@openrewrite/rewrite/java";
9
+ import {Json} from "@openrewrite/rewrite/json";
10
+ import {findNodeResolutionResult, JavaScriptVisitor, JS, template} from "@openrewrite/rewrite/javascript";
11
+ import * as semver from "semver";
12
+ import {dirOf, isPackageJson, packageData} from "../package-json-support";
13
+ import {bindingsOf, importModule, sourcePathOf} from "../module-bindings";
14
+ import {memberNameOf} from "../config-object-support";
15
+ import {FIRST_ON_SMITHY} from "./aws-sdk-smithy-support";
16
+
17
+ /** The first `@aws-sdk/*` release whose retry strategy makes `maxAttempts` attempts rather than one more. */
18
+ const RETRIES_COUNTED = '3.362.0';
19
+
20
+ interface Installed {
21
+ declared: string;
22
+ version?: string;
23
+ }
24
+
25
+ interface Scan {
26
+ /** By directory, the `@aws-sdk/*` packages its `package.json` declares and the release its lock resolves. */
27
+ installed: Map<string, Map<string, Installed>>;
28
+ }
29
+
30
+ /**
31
+ * Before 3.362.0 an `@aws-sdk/*` client made `maxAttempts + 1` attempts of a request that failed with a retryable
32
+ * error. A client the migration raises past it makes `maxAttempts`, so its `maxAttempts` is raised by one to keep the
33
+ * attempts it made. Runs only before `DeclareSmithyTwins`, which leaves the client declared from 3.363.0
34
+ * on, so a later cycle leaves the raised `maxAttempts` as it is.
35
+ */
36
+ export class KeepAwsSdkRetryAttempts extends ScanningRecipe<Scan> {
37
+ readonly name = "org.openrewrite.node.migrate.aws-sdk-smithy.keep-aws-sdk-retry-attempts";
38
+ readonly displayName = "Keep the attempts an `@aws-sdk/*` client made before 3.362.0";
39
+ readonly description = "An `@aws-sdk/*` client before 3.362.0 makes one attempt more than its `maxAttempts` of a " +
40
+ "request that fails with a retryable error; from 3.362.0 it makes `maxAttempts`. Where the lock resolves a " +
41
+ "client before 3.362.0 that is declared below 3.363.0, and so moves past it with the `@smithy/*` migration, " +
42
+ "the `maxAttempts` its constructor is given is raised by one (`attempts - 1` becomes `attempts`).";
43
+ readonly tags = ["aws-sdk", "aws", "smithy"];
44
+
45
+ initialValue(_ctx: ExecutionContext): Scan {
46
+ return {installed: new Map()};
47
+ }
48
+
49
+ async scanner(scan: Scan): Promise<TreeVisitor<any, ExecutionContext>> {
50
+ return new class extends TreeVisitor<Tree, ExecutionContext> {
51
+ async visit<R extends Tree>(tree: Tree, _ctx: ExecutionContext): Promise<R | undefined> {
52
+ const sourcePath: string | undefined = (tree as any).sourcePath;
53
+ if (tree.kind !== Json.Kind.Document || !sourcePath || sourcePath.includes('node_modules/') ||
54
+ !isPackageJson(sourcePath.replace(/\\/g, '/'))) {
55
+ return tree as R;
56
+ }
57
+ const marker = findNodeResolutionResult(tree as Json.Document);
58
+ if (!marker) return tree as R;
59
+ // the range as the package.json now declares it, which the marker isn't updated with
60
+ const resolved = new Map<string, string | undefined>();
61
+ for (const dep of [...marker.dependencies, ...marker.devDependencies, ...marker.peerDependencies, ...marker.optionalDependencies]) {
62
+ resolved.set(dep.name, dep.resolved?.version);
63
+ }
64
+ const pkg = packageData(tree as Json.Document);
65
+ const installed = new Map<string, Installed>();
66
+ for (const section of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) {
67
+ for (const [name, declared] of Object.entries<any>(pkg?.[section] ?? {})) {
68
+ if (name.startsWith('@aws-sdk/') && typeof declared === 'string') {
69
+ installed.set(name, {declared, version: resolved.get(name)});
70
+ }
71
+ }
72
+ }
73
+ scan.installed.set(dirOf(sourcePath.replace(/\\/g, '/')), installed);
74
+ return tree as R;
75
+ }
76
+ };
77
+ }
78
+
79
+ async editorWithData(scan: Scan): Promise<TreeVisitor<any, ExecutionContext>> {
80
+ return new class extends JavaScriptVisitor<ExecutionContext> {
81
+ // by local name, the client classes of an @aws-sdk/* package that moves past 3.362.0
82
+ private clients = new Set<string>();
83
+
84
+ protected override async visitJsCompilationUnit(cu: JS.CompilationUnit, p: ExecutionContext): Promise<J | undefined> {
85
+ this.clients = new Set();
86
+ const path = sourcePathOf(this.cursor).replace(/\\/g, '/');
87
+ for (const stmt of cu.statements) {
88
+ const module = stmt.element?.kind === JS.Kind.Import ? importModule(stmt.element as JS.Import) : undefined;
89
+ if (!module?.startsWith('@aws-sdk/client-') || !movesPastCountedRetries(scan, path, module)) continue;
90
+ for (const [local, imported] of bindingsOf(cu, new Set([module]))) {
91
+ if (imported?.endsWith('Client')) this.clients.add(local);
92
+ }
93
+ }
94
+ return this.clients.size === 0 ? cu : super.visitJsCompilationUnit(cu, p);
95
+ }
96
+
97
+ protected override async visitNewClass(newClass: J.NewClass, p: ExecutionContext): Promise<J | undefined> {
98
+ const nc = await super.visitNewClass(newClass, p) as J.NewClass;
99
+ if (nc?.kind !== J.Kind.NewClass || !nc.class || !isIdentifier(nc.class) || !this.clients.has(nc.class.simpleName)) return nc;
100
+ const config = nc.arguments.elements[0]?.element;
101
+ if (config?.kind !== J.Kind.NewClass || (config as J.NewClass).class) return nc;
102
+ const statements = (config as J.NewClass).body?.statements ?? [];
103
+ const i = statements.findIndex(s => memberNameOf(s.element) === 'maxAttempts');
104
+ if (i < 0) return nc;
105
+ const property = statements[i].element as JS.PropertyAssignment;
106
+ const raised = property.initializer && await this.plusOne(property.initializer, p);
107
+ if (!raised) {
108
+ return foundSearchResult(nc, `${nc.class.simpleName} before ${RETRIES_COUNTED} made maxAttempts + 1 attempts; from ${RETRIES_COUNTED} it makes maxAttempts`);
109
+ }
110
+ const updated = [...statements];
111
+ updated[i] = {...statements[i], element: {...property, initializer: raised} as JS.PropertyAssignment};
112
+ const body = {...(config as J.NewClass).body!, statements: updated};
113
+ return {
114
+ ...nc,
115
+ arguments: {...nc.arguments, elements: [{...nc.arguments.elements[0], element: {...config, body}}, ...nc.arguments.elements.slice(1)]}
116
+ } as J.NewClass;
117
+ }
118
+
119
+ private async plusOne(e: J, p: ExecutionContext): Promise<J | undefined> {
120
+ if (e.kind === J.Kind.Literal && typeof (e as J.Literal).value === 'number') {
121
+ const n = (e as J.Literal).value as number + 1;
122
+ return {...e, value: n, valueSource: String(n)} as J.Literal;
123
+ }
124
+ if (e.kind === J.Kind.Binary) {
125
+ const b = e as J.Binary;
126
+ const right = b.right;
127
+ if (b.operator.element === J.Binary.Type.Subtraction && right.kind === J.Kind.Literal && (right as J.Literal).value === 1) {
128
+ return {...b.left, prefix: e.prefix} as J;
129
+ }
130
+ }
131
+ if (isIdentifier(e) || e.kind === J.Kind.FieldAccess || e.kind === J.Kind.MethodInvocation) {
132
+ return template`${e} + 1`.apply(e, this.cursor, {format: false}) as Promise<J | undefined>;
133
+ }
134
+ // a provider function, or an expression whose precedence a sum may change
135
+ return undefined;
136
+ }
137
+ };
138
+ }
139
+ }
140
+
141
+ function movesPastCountedRetries(scan: Scan, path: string, module: string): boolean {
142
+ for (let dir = dirOf(path); ; dir = dirOf(dir)) {
143
+ const installed = scan.installed.get(dir)?.get(module);
144
+ if (installed) {
145
+ const version = installed.version ?? (semver.validRange(installed.declared) ? semver.minVersion(installed.declared)?.version : undefined);
146
+ const min = semver.validRange(installed.declared) ? semver.minVersion(installed.declared) : null;
147
+ return !!version && semver.lt(version, RETRIES_COUNTED) && !!min && semver.lt(min, FIRST_ON_SMITHY);
148
+ }
149
+ if (dir === '' || dir === '.' || dir === dirOf(dir)) return false;
150
+ }
151
+ }
@@ -0,0 +1,89 @@
1
+ /*
2
+ * Copyright 2026 the original author or authors.
3
+ *
4
+ * Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
5
+ */
6
+
7
+ import {ExecutionContext, foundSearchResult, Recipe, TreeVisitor} from "@openrewrite/rewrite";
8
+ import {isIdentifier, J} from "@openrewrite/rewrite/java";
9
+ import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
10
+ import {bindingsOf, ModuleBindings} from "../module-bindings";
11
+ import {memberNameOf, renameKey, withoutMembers} from "../config-object-support";
12
+
13
+ const MODULES = new Set(['@aws-sdk/node-http-handler']);
14
+
15
+ /**
16
+ * Renames `requestTimeout` to `socketTimeout` in the options of a `NodeHttpHandler` from `@aws-sdk/node-http-handler`.
17
+ * Every `@aws-sdk/node-http-handler` release, and `@smithy/node-http-handler` before 4.4.0, took `requestTimeout` as
18
+ * the idle-socket timeout, rejecting with a `TimeoutError`, and `socketTimeout` as its alias. From 4.4.0
19
+ * `requestTimeout` bounds the whole request and only logs a warning when exceeded, so a call to a hung server never
20
+ * settles; `socketTimeout` keeps the old meaning in every release.
21
+ */
22
+ export class KeepNodeHttpHandlerIdleTimeout extends Recipe {
23
+ readonly name = "org.openrewrite.node.migrate.aws-sdk-smithy.keep-node-http-handler-idle-timeout";
24
+ readonly displayName = "Keep the idle-socket timeout of a `NodeHttpHandler`";
25
+ readonly description = "Renames the `requestTimeout` option of a `NodeHttpHandler` from " +
26
+ "`@aws-sdk/node-http-handler` to `socketTimeout`. Before `@smithy/node-http-handler` 4.4.0, `requestTimeout` " +
27
+ "was the time a socket may stay idle before the request is rejected with a `TimeoutError`; from 4.4.0 it " +
28
+ "bounds the whole request and, unless `throwOnRequestTimeout` is set, only logs a warning, so a request to a " +
29
+ "server that stops answering hangs. `socketTimeout` is the idle-socket timeout in every release.";
30
+ readonly tags = ["aws-sdk", "aws", "smithy"];
31
+
32
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
33
+ return new class extends JavaScriptVisitor<ExecutionContext> {
34
+ private bindings: ModuleBindings = new Map();
35
+
36
+ protected override async visitJsCompilationUnit(cu: JS.CompilationUnit, p: ExecutionContext): Promise<J | undefined> {
37
+ this.bindings = bindingsOf(cu, MODULES);
38
+ return this.bindings.size === 0 ? cu : super.visitJsCompilationUnit(cu, p);
39
+ }
40
+
41
+ protected override async visitNewClass(newClass: J.NewClass, p: ExecutionContext): Promise<J | undefined> {
42
+ const nc = await super.visitNewClass(newClass, p) as J.NewClass;
43
+ return nc?.kind === J.Kind.NewClass && this.isHandler(nc.class) ? this.withOptions(nc, nc.arguments.elements[0]?.element,
44
+ options => ({...nc, arguments: {...nc.arguments, elements: [{...nc.arguments.elements[0], element: options}, ...nc.arguments.elements.slice(1)]}}) as J.NewClass) : nc;
45
+ }
46
+
47
+ protected override async visitMethodInvocation(method: J.MethodInvocation, p: ExecutionContext): Promise<J | undefined> {
48
+ const m = await super.visitMethodInvocation(method, p) as J.MethodInvocation;
49
+ return m?.kind === J.Kind.MethodInvocation && m.name.simpleName === 'create' && this.isHandler(m.select?.element)
50
+ ? this.withOptions(m, m.arguments.elements[0]?.element,
51
+ options => ({...m, arguments: {...m.arguments, elements: [{...m.arguments.elements[0], element: options}, ...m.arguments.elements.slice(1)]}}) as J.MethodInvocation)
52
+ : m;
53
+ }
54
+
55
+ // NodeHttpHandler as imported, or as a member of the module's namespace
56
+ private isHandler(e: J | undefined): boolean {
57
+ if (!e) return false;
58
+ if (isIdentifier(e)) return this.bindings.get(e.simpleName) === 'NodeHttpHandler';
59
+ if (e?.kind !== J.Kind.FieldAccess) return false;
60
+ const fa = e as J.FieldAccess;
61
+ return fa.name.element.simpleName === 'NodeHttpHandler' && isIdentifier(fa.target) &&
62
+ this.bindings.has(fa.target.simpleName) && this.bindings.get(fa.target.simpleName) === undefined;
63
+ }
64
+
65
+ private withOptions<T extends J>(call: T, options: J | undefined, replace: (options: J.NewClass) => T): T {
66
+ if (options?.kind !== J.Kind.NewClass || (options as J.NewClass).class) return call;
67
+ const literal = options as J.NewClass;
68
+ const members = literal.body?.statements.map(s => s.element) ?? [];
69
+ const timeout = members.find(m => memberNameOf(m) === 'requestTimeout') as JS.PropertyAssignment | undefined;
70
+ if (!timeout || !timeout.initializer) {
71
+ // `{requestTimeout}` shorthand, or a spread that may carry it
72
+ return timeout || members.some(m => m && isIdentifier(m) && m.simpleName === 'requestTimeout' || m.kind === JS.Kind.Spread)
73
+ ? foundSearchResult(call, 'requestTimeout is the whole-request timeout from @smithy/node-http-handler 4.4.0, which only warns when exceeded; the idle-socket timeout it was is socketTimeout')
74
+ : call;
75
+ }
76
+ // requestTimeout took precedence over its socketTimeout alias
77
+ const kept = withoutMembers(literal, m => memberNameOf(m) === 'socketTimeout');
78
+ return replace({
79
+ ...kept,
80
+ body: {
81
+ ...kept.body!,
82
+ statements: kept.body!.statements.map(s => memberNameOf(s.element) === 'requestTimeout'
83
+ ? {...s, element: renameKey(s.element as JS.PropertyAssignment, 'socketTimeout')} : s)
84
+ }
85
+ } as J.NewClass);
86
+ }
87
+ };
88
+ }
89
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ * Copyright 2026 the original author or authors.
3
+ *
4
+ * Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
5
+ */
6
+
7
+ import {Recipe} from "@openrewrite/rewrite";
8
+ import {KeepAwsSdkRetryAttempts} from "./keep-aws-sdk-retry-attempts";
9
+ import {KeepNodeHttpHandlerIdleTimeout} from "./keep-node-http-handler-idle-timeout";
10
+ import {ReplaceAwsSdkSmithyImports} from "./replace-aws-sdk-smithy-imports";
11
+ import {SwapAwsSdkSmithyDependencies} from "./swap-aws-sdk-smithy-dependencies";
12
+
13
+ export class MigrateAwsSdkPackagesToSmithy extends Recipe {
14
+ readonly name = "org.openrewrite.node.migrate.aws-sdk-smithy.migrate-aws-sdk-packages-to-smithy";
15
+ readonly displayName = "Migrate `@aws-sdk/*` packages that moved to `@smithy/*`";
16
+ readonly description = "49 `@aws-sdk/*` packages, among them `node-http-handler`, `protocol-http`, " +
17
+ "`signature-v4`, `smithy-client`, `fetch-http-handler`, `abort-controller` and the `util-*` and " +
18
+ "`middleware-*` packages, are deprecated: from 3.363.0 each only re-exports the `@smithy/*` package of the " +
19
+ "same name, and its last release pins an old major of it. Points their imports, requires and mocks at the " +
20
+ "`@smithy/*` packages, which export the same names, and swaps each dependency at the release the project's " +
21
+ "Node.js runs, regenerating the lock file. An `@aws-sdk/*` client declared below 3.363.0 is raised to a release " +
22
+ "on the `@smithy/*` packages. Where the behavior changes, the code is adapted: a `NodeHttpHandler`'s " +
23
+ "`requestTimeout`, an idle-socket timeout before `@smithy/node-http-handler` 4.4.0, becomes `socketTimeout`, " +
24
+ "and the `maxAttempts` of a client raised past 3.362.0, which made one attempt more, is raised by one.";
25
+ readonly tags = ["aws-sdk", "aws", "smithy"];
26
+ // each removeDependency keeps the moved package while sources import it, until the cycle after the imports move
27
+ readonly causesAnotherCycle = true;
28
+
29
+ async recipeList(): Promise<Recipe[]> {
30
+ return [new KeepNodeHttpHandlerIdleTimeout(), new KeepAwsSdkRetryAttempts(), new ReplaceAwsSdkSmithyImports(),
31
+ new SwapAwsSdkSmithyDependencies()];
32
+ }
33
+ }
@@ -0,0 +1,99 @@
1
+ /*
2
+ * Copyright 2026 the original author or authors.
3
+ *
4
+ * Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
5
+ */
6
+
7
+ import {ExecutionContext, foundSearchResult, Recipe, TreeVisitor} from "@openrewrite/rewrite";
8
+ import {isIdentifier, J} from "@openrewrite/rewrite/java";
9
+ import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
10
+ import {DROPPED_EXPORTS, movedSpecifier} from "./aws-sdk-smithy-support";
11
+
12
+ const MOCK_OWNERS = new Set(['vi', 'jest']);
13
+ const MODULE_METHODS = new Set(['mock', 'doMock', 'unmock', 'unstable_mockModule', 'requireActual', 'requireMock', 'importActual', 'importMock']);
14
+
15
+ /** `require(...)`, `require.resolve(...)` and the test mocks and loaders of `jest` and `vi`, which name a module first. */
16
+ function namesModule(m: J.MethodInvocation): boolean {
17
+ const select = m.select?.element;
18
+ if (!select) return m.name.simpleName === 'require';
19
+ if (!isIdentifier(select)) return false;
20
+ return select.simpleName === 'require' && m.name.simpleName === 'resolve' ||
21
+ MOCK_OWNERS.has(select.simpleName) && MODULE_METHODS.has(m.name.simpleName);
22
+ }
23
+
24
+ async function identifiersIn(tree: J, p: ExecutionContext): Promise<Set<string>> {
25
+ const names = new Set<string>();
26
+ await new class extends JavaScriptVisitor<ExecutionContext> {
27
+ protected override async visitIdentifier(identifier: J.Identifier, ctx: ExecutionContext): Promise<J | undefined> {
28
+ names.add(identifier.simpleName);
29
+ return super.visitIdentifier(identifier, ctx);
30
+ }
31
+ }().visit(tree, p);
32
+ return names;
33
+ }
34
+
35
+ export class ReplaceAwsSdkSmithyImports extends Recipe {
36
+ readonly name = "org.openrewrite.node.migrate.aws-sdk-smithy.replace-aws-sdk-smithy-imports";
37
+ readonly displayName = "Replace imports of `@aws-sdk/*` packages that moved to `@smithy/*`";
38
+ readonly description = "Points every reference to one of the 49 deprecated `@aws-sdk/*` packages that moved to " +
39
+ "the `@smithy/*` package of the same name (`@aws-sdk/node-http-handler`, `@aws-sdk/protocol-http`, " +
40
+ "`@aws-sdk/signature-v4`, `@aws-sdk/smithy-client`, ...) at that package, which exports the same names: " +
41
+ "imports, `export ... from`, `require` and `require.resolve`, dynamic `import()`, `import('...')` types, and " +
42
+ "the module named by `jest.mock`, `vi.mock` and their kin. A subpath is kept. An import of `StringWrapper`, " +
43
+ "which `@smithy/smithy-client` dropped in 3.0.0, is marked for manual migration.";
44
+ readonly tags = ["aws-sdk", "aws", "smithy"];
45
+
46
+ async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
47
+ return new class extends JavaScriptVisitor<ExecutionContext> {
48
+ protected override async visitImportDeclaration(jsImport: JS.Import, p: ExecutionContext): Promise<J | undefined> {
49
+ const imp = await super.visitImportDeclaration(jsImport, p) as JS.Import;
50
+ const spec = imp.moduleSpecifier?.element;
51
+ const literal = movedSpecifier(spec);
52
+ if (!literal) return imp;
53
+ const moved = {...imp, moduleSpecifier: {...imp.moduleSpecifier!, element: literal}} as JS.Import;
54
+ const dropped = DROPPED_EXPORTS[(spec as J.Literal).value as string] ?? [];
55
+ const names = dropped.length > 0 ? await identifiersIn(imp, p) : new Set<string>();
56
+ const used = dropped.filter(name => names.has(name));
57
+ return used.length > 0
58
+ ? foundSearchResult(moved, `${used.join(', ')} was dropped from ${literal.value} in 3.0.0; replace it before upgrading past 2.x`)
59
+ : moved;
60
+ }
61
+
62
+ protected override async visitExportDeclaration(exportDeclaration: JS.ExportDeclaration, p: ExecutionContext): Promise<J | undefined> {
63
+ const exp = await super.visitExportDeclaration(exportDeclaration, p) as JS.ExportDeclaration;
64
+ const literal = movedSpecifier(exp.moduleSpecifier?.element);
65
+ return literal ? {...exp, moduleSpecifier: {...exp.moduleSpecifier!, element: literal}} as JS.ExportDeclaration : exp;
66
+ }
67
+
68
+ protected override async visitMethodInvocation(method: J.MethodInvocation, p: ExecutionContext): Promise<J | undefined> {
69
+ const m = await super.visitMethodInvocation(method, p) as J.MethodInvocation;
70
+ const args = m.arguments.elements;
71
+ const literal = args.length > 0 && namesModule(m) ? movedSpecifier(args[0].element) : undefined;
72
+ return literal
73
+ ? {...m, arguments: {...m.arguments, elements: [{...args[0], element: literal}, ...args.slice(1)]}} as J.MethodInvocation
74
+ : m;
75
+ }
76
+
77
+ protected override async visitFunctionCall(functionCall: JS.FunctionCall, p: ExecutionContext): Promise<J | undefined> {
78
+ const call = await super.visitFunctionCall(functionCall, p) as JS.FunctionCall;
79
+ const fn = call.function?.element;
80
+ const args = call.arguments.elements;
81
+ if (!fn || !isIdentifier(fn) || fn.simpleName !== 'import' || args.length === 0) return call;
82
+ const literal = movedSpecifier(args[0].element);
83
+ return literal
84
+ ? {...call, arguments: {...call.arguments, elements: [{...args[0], element: literal}, ...args.slice(1)]}} as JS.FunctionCall
85
+ : call;
86
+ }
87
+
88
+ protected override async visitImportType(importType: JS.ImportType, p: ExecutionContext): Promise<J | undefined> {
89
+ const type = await super.visitImportType(importType, p) as JS.ImportType;
90
+ const args = type.argumentAndAttributes.elements;
91
+ const first = args[0]?.element as unknown as {kind: string; literal?: J} | undefined;
92
+ const literal = first?.kind === JS.Kind.LiteralType ? movedSpecifier(first.literal) : undefined;
93
+ if (!literal) return type;
94
+ const element = {...first, literal} as unknown as typeof args[0]['element'];
95
+ return {...type, argumentAndAttributes: {...type.argumentAndAttributes, elements: [{...args[0], element}, ...args.slice(1)]}} as JS.ImportType;
96
+ }
97
+ };
98
+ }
99
+ }