Skip to content

Conversation

@technologic-technologic
Copy link

This pull request implements the codemod to replace deprecated tls.createSecurePair() with TLSSocket, addressing DEP0064.

Changes included:

  • Updated the codemod workflow to transform createSecurePair(...) into new TLSSocket(underlyingSocket, {...}), preserving tls.TLSSocket for namespace usage.
  • Rewrote imports to replace { createSecurePair } with { TLSSocket } in CJS/ESM (keeps namespace imports intact).
  • Renamed assigned variables pairsocket where applicable.
  • Added tests for cjs/esm (destructured, namespace, flags, and mixed symbols).
  • Ensured idempotent runs and minimal, repo-style edits.
  • Added README.md and package.json metadata.
  • Added tests for assigned variables, destructured imports.

This PR is part of the feature branch feat/tls-createSecurePair-to-TLSSocket.

@AugustinMauroy
Copy link
Member

AugustinMauroy commented Nov 6, 2025

npm i on root should update the lock file and fix the ci

Missing import test case

  • défaut import ESM
  • ESM dynamic import

…uded default and dynamic ESM imports + needed refactors as PR comments)

WHAT Changed:
- AST-based migration
- ESM/default/dynamic cases
- Normalize rewritten CJS requires to `node:tls` as suggested to code section
- Remove duplicated `getNodeImportStatements/getNodeRequireCalls` invocations
  that attempted to handle `'node:tls'` vs `'tls'`
…d dynamic import (await/then)

WHAT :
- ESM default import
- ESM dynamic import (await assignment)
- ESM dynamic import (then callback)
…cation`)' into feat(`tls-createSecurePair-deprecation`)
...collectDefaultImportBindings(tlsStmts),
...collectDynamicImportIdentifiers(rootNode),
]);
const callSites: CallSite[] = [...findCreateSecurePairCalls(rootNode)];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why spread the result of findCreateSecurePairCalls (which is already an array)?

...getNodeRequireCalls(root, 'tls'),
];
const cspBindings = unique([
...tlsStmts.map(s => resolveBindingPath(s as unknown as SgNode<JS>, '$.createSecurePair')).filter(Boolean) as string[],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the type casting (as … as) here seems strange 🤔

Comment on lines +35 to +37
edits.push(...rewriteTlsImports(rootNode));
if (edits.length === 0) return null;
return rootNode.commitEdits(edits);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
edits.push(...rewriteTlsImports(rootNode));
if (edits.length === 0) return null;
return rootNode.commitEdits(edits);
edits.push(...rewriteTlsImports(rootNode));
if (edits.length === 0) return null;
return rootNode.commitEdits(edits);

Comment on lines +41 to +52
const pats = [
'$X.createSecurePair($A, $B, $C, $D)',
'$X.createSecurePair($A, $B, $C)',
'$X.createSecurePair($A, $B)',
'$X.createSecurePair($A)',
'$X.createSecurePair()',
'createSecurePair($A, $B, $C, $D)',
'createSecurePair($A, $B, $C)',
'createSecurePair($A, $B)',
'createSecurePair($A)',
'createSecurePair()',
];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: pats is static and unchanged. better to move the declaration outside findCreateSecurePairCalls (ex below line 63) to avoid needlessly re-creating them on each execution of findCreateSecurePairCalls.

for (const n of nodes) {
const x = (n as SgNode<JS>).getMatch('X')?.text();
const binding = x ? `${x}.createSecurePair` : 'createSecurePair';
out.push({ call: n as SgNode<JS>, binding });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it shouldn't be necessary to cast n as SgNode<JS> 🤨

Comment on lines +179 to +180
name = p.field('key')?.text() || '';
alias = p.field('value')?.text() || '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does text() return on "failure"? If nullish, nit: I think better to use ?? instead of ||.

if (!name || name === 'createSecurePair') continue;
kept.push(alias && alias !== name ? `${name}: ${alias}` : name);
}
if (kept.indexOf('TLSSocket') === -1) kept.push('TLSSocket');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (kept.indexOf('TLSSocket') === -1) kept.push('TLSSocket');
if (kept.includes('TLSSocket')) kept.push('TLSSocket');

Comment on lines +192 to +193
const k = cur.kind();
if (k === 'lexical_declaration' || k === 'variable_declaration') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single letter var names usually indicate an index/counter. We're not short on space, so I think better to name it kind.

Suggested change
const k = cur.kind();
if (k === 'lexical_declaration' || k === 'variable_declaration') {
const kind = cur.kind();
if (kind === 'lexical_declaration' || kind === 'variable_declaration') {

Comment on lines +218 to +227
const pats = [
'const $ID = await import(\'tls\')',
'const $ID = await import("tls")',
'const $ID = await import(\'node:tls\')',
'const $ID = await import("node:tls")',
'const $ID = import(\'tls\')',
'const $ID = import("tls")',
'const $ID = import(\'node:tls\')',
'const $ID = import("node:tls")',
];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think good to extract this.

if (id) out.push(`${id}.createSecurePair`);
}
}
return Array.from(new Set(out));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use your unique utility?

@JakobJingleheimer
Copy link
Member

Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: handle tls.createSecurePair() depreciation

3 participants