-
-
Notifications
You must be signed in to change notification settings - Fork 31
feat(outgoingmessage-headers) #281
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
Draft
elvessilvavieira
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
elvessilvavieira:feat(outgoingmessage-headers)
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| schema_version: "1.0" | ||
| name: "@nodejs/http-outgoingmessage-headers" | ||
| version: 1.0.0 | ||
| description: Migrate deprecated usages of OutgoingMessage.prototype._headers and ._headerNames to public HTTP header APIs | ||
| author: Node.js Team | ||
| license: MIT | ||
| workflow: workflow.yaml | ||
| category: migration | ||
|
|
||
| targets: | ||
| languages: | ||
| - javascript | ||
| - typescript | ||
|
|
||
| keywords: | ||
| - transformation | ||
| - migration | ||
| - http | ||
|
|
||
| registry: | ||
| access: public | ||
| visibility: public |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "name": "@nodejs/http-outgoingmessage-headers", | ||
| "version": "1.0.0", | ||
| "description": "Migrate deprecated usages of OutgoingMessage.prototype._headers and ._headerNames to the official HTTP header APIs.", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "npx codemod jssg test -l typescript ./src/workflow.ts ./" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/nodejs/userland-migrations.git", | ||
| "directory": "recipes/http-outgoingmessage-headers", | ||
| "bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
| }, | ||
| "author": "elvessilvavieira (Elves Vieira)", | ||
| "license": "MIT", | ||
| "homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/http-outgoingmessage-headers/README.md", | ||
| "devDependencies": { | ||
| "@codemod.com/jssg-types": "^1.0.9" | ||
| }, | ||
| "dependencies": { | ||
| "@nodejs/codemod-utils": "*" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,118 @@ | ||||||||
| import type { SgRoot, Edit } from "@codemod.com/jssg-types/main"; | ||||||||
| import type Js from "@codemod.com/jssg-types/langs/javascript"; | ||||||||
|
|
||||||||
| export default function transform(root: SgRoot<Js>): string | null { | ||||||||
| const rootNode = root.root(); | ||||||||
| const edits: Edit[] = []; | ||||||||
|
|
||||||||
| { | ||||||||
| const matches = rootNode.findAll({ | ||||||||
| rule: { pattern: "$OBJ._headers[$KEY]" }, | ||||||||
| }); | ||||||||
| for (const m of matches) { | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| const obj = m.getMatch("OBJ"); | ||||||||
| const key = m.getMatch("KEY"); | ||||||||
| if (!obj || !key) continue; | ||||||||
|
|
||||||||
| const name = obj.text(); | ||||||||
| if (!looksLikeOutgoingMessage(rootNode, name)) continue; | ||||||||
|
|
||||||||
| edits.push(m.replace(`${obj.text()}.getHeader(${key.text()})`)); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| { | ||||||||
| const matches = rootNode.findAll({ | ||||||||
| rule: { pattern: "$KEY in $OBJ._headers" }, | ||||||||
| }); | ||||||||
| for (const m of matches) { | ||||||||
| const obj = m.getMatch("OBJ"); | ||||||||
| const key = m.getMatch("KEY"); | ||||||||
| if (!obj || !key) continue; | ||||||||
|
|
||||||||
| const name = obj.text(); | ||||||||
| if (!looksLikeOutgoingMessage(rootNode, name)) continue; | ||||||||
|
|
||||||||
| edits.push(m.replace(`${obj.text()}.hasHeader(${key.text()})`)); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| { | ||||||||
| const matches = rootNode.findAll({ | ||||||||
| rule: { pattern: "Object.keys($OBJ._headers)" }, | ||||||||
| }); | ||||||||
| for (const m of matches) { | ||||||||
| const obj = m.getMatch("OBJ"); | ||||||||
| if (!obj) continue; | ||||||||
|
|
||||||||
| const name = obj.text(); | ||||||||
| if (!looksLikeOutgoingMessage(rootNode, name)) continue; | ||||||||
|
|
||||||||
| edits.push(m.replace(`${obj.text()}.getHeaderNames()`)); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| { | ||||||||
| const matches = rootNode.findAll({ | ||||||||
| rule: { pattern: "$OBJ._headerNames" }, | ||||||||
| }); | ||||||||
| for (const m of matches) { | ||||||||
| const obj = m.getMatch("OBJ"); | ||||||||
| if (!obj) continue; | ||||||||
|
|
||||||||
| const name = obj.text(); | ||||||||
| if (!looksLikeOutgoingMessage(rootNode, name)) continue; | ||||||||
|
|
||||||||
| edits.push(m.replace(`${obj.text()}.getHeaderNames()`)); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| { | ||||||||
| const matches = rootNode.findAll({ | ||||||||
| rule: { pattern: "$OBJ._headers" }, | ||||||||
| }); | ||||||||
| for (const m of matches) { | ||||||||
| const obj = m.getMatch("OBJ"); | ||||||||
| if (!obj) continue; | ||||||||
|
|
||||||||
| const name = obj.text(); | ||||||||
| if (!looksLikeOutgoingMessage(rootNode, name)) continue; | ||||||||
|
|
||||||||
| const text = m.text(); | ||||||||
| if (text.includes("[")) continue; | ||||||||
|
|
||||||||
| const parent = m.parent(); | ||||||||
| const parentText = parent?.text() ?? ""; | ||||||||
|
|
||||||||
| if (parentText.startsWith("Object.keys(")) continue; | ||||||||
|
|
||||||||
| if (parentText.includes("=") && parentText.trim().startsWith(obj.text())) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| edits.push(m.replace(`${obj.text()}.getHeaders()`)); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| if (!edits.length) return null; | ||||||||
| return rootNode.commitEdits(edits); | ||||||||
| } | ||||||||
|
|
||||||||
| function looksLikeOutgoingMessage( | ||||||||
| file: ReturnType<SgRoot<Js>["root"]>, | ||||||||
| name: string | ||||||||
| ): boolean { | ||||||||
| const methods = [ | ||||||||
| "setHeader", | ||||||||
| "getHeader", | ||||||||
| "getHeaders", | ||||||||
| "hasHeader", | ||||||||
| "removeHeader", | ||||||||
| "writeHead", | ||||||||
| ]; | ||||||||
| for (const m of methods) { | ||||||||
| const hit = file.find({ rule: { pattern: `${name}.${m}($$)` } }); | ||||||||
| if (hit) return true; | ||||||||
| } | ||||||||
| return false; | ||||||||
| } | ||||||||
13 changes: 13 additions & 0 deletions
13
recipes/http-outgoingmessage-headers/tests/expected/basic.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| function example(response, request) { | ||
| const all = response.getHeaders(); | ||
| const ct = response.getHeader("content-type"); | ||
| if (response.hasHeader("content-length")) console.log("has length"); | ||
| console.log(response.getHeaderNames()); | ||
| console.log(response.getHeaderNames()); | ||
|
|
||
| const allReq = request.getHeaders(); | ||
| const ua = request.getHeader('user-agent'); | ||
| if (request.hasHeader('accept')) console.log('has accept'); | ||
| console.log(request.getHeaderNames()); | ||
| console.log(request.getHeaderNames()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| function example(response, request) { | ||
| const all = response._headers; | ||
| const ct = response._headers["content-type"]; | ||
| if ("content-length" in response._headers) console.log("has length"); | ||
| console.log(Object.keys(response._headers)); | ||
| console.log(response._headerNames); | ||
|
|
||
| const allReq = request._headers; | ||
| const ua = request._headers['user-agent']; | ||
| if ('accept' in request._headers) console.log('has accept'); | ||
| console.log(Object.keys(request._headers)); | ||
| console.log(request._headerNames); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # yaml-language-server: $schema=https://raw.githubusercontent.com/codemod-com/codemod/refs/heads/main/schemas/workflow.json | ||
|
|
||
| version: "1" | ||
|
|
||
| nodes: | ||
| - id: apply-transforms | ||
| name: Apply AST Transformations | ||
| type: automatic | ||
| steps: | ||
| - name: Replace deprecated OutgoingMessage private header fields. | ||
| js-ast-grep: | ||
| js_file: src/workflow.ts | ||
| base_path: . | ||
| include: | ||
| - "**/*.js" | ||
| - "**/*.jsx" | ||
| - "**/*.mjs" | ||
| - "**/*.cjs" | ||
| - "**/*.cts" | ||
| - "**/*.mts" | ||
| - "**/*.ts" | ||
| - "**/*.tsx" | ||
| exclude: | ||
| - "**/node_modules/**" | ||
| language: typescript |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
main part here...
Here if the user code have ah propriety expression
_headersit's will be catch. We need a more complex logic to catch http usage and -> catch response objects -> catch _headersI have to admit that the orignal issue examples wasn't 100% complete