-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| schema_version: "1.0" | ||
| name: "@nodejs/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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "name": "@nodejs/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/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/outgoingmessage-headers/README.md", | ||
| "devDependencies": { | ||
| "@codemod.com/jssg-types": "^1.0.9" | ||
| }, | ||
| "dependencies": { | ||
| "@nodejs/codemod-utils": "*" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,90 @@ | ||||||||||||
| 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[] = []; | ||||||||||||
| let hasChanges = false; | ||||||||||||
|
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 matches = rootNode.findAll({ | ||||||||||||
| rule: { pattern: "$OBJ._headers[$KEY]" }, | ||||||||||||
|
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. main part here... Here if the user code have ah propriety expression I have to admit that the orignal issue examples wasn't 100% complete |
||||||||||||
| }); | ||||||||||||
| 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; | ||||||||||||
|
|
||||||||||||
| edits.push(m.replace(`${obj.text()}.getHeader(${key.text()})`)); | ||||||||||||
| hasChanges = true; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| { | ||||||||||||
| 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; | ||||||||||||
|
|
||||||||||||
| edits.push(m.replace(`${obj.text()}.hasHeader(${key.text()})`)); | ||||||||||||
| hasChanges = true; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| { | ||||||||||||
| const matches = rootNode.findAll({ | ||||||||||||
| rule: { pattern: "Object.keys($OBJ._headers)" }, | ||||||||||||
| }); | ||||||||||||
| for (const m of matches) { | ||||||||||||
| const obj = m.getMatch("OBJ"); | ||||||||||||
| if (!obj) continue; | ||||||||||||
|
|
||||||||||||
| edits.push(m.replace(`${obj.text()}.getHeaderNames()`)); | ||||||||||||
| hasChanges = true; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| { | ||||||||||||
| const matches = rootNode.findAll({ | ||||||||||||
| rule: { pattern: "$OBJ._headerNames" }, | ||||||||||||
| }); | ||||||||||||
| for (const m of matches) { | ||||||||||||
| const obj = m.getMatch("OBJ"); | ||||||||||||
| if (!obj) continue; | ||||||||||||
|
|
||||||||||||
| edits.push(m.replace(`${obj.text()}.getHeaderNames()`)); | ||||||||||||
| hasChanges = true; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| { | ||||||||||||
| const matches = rootNode.findAll({ | ||||||||||||
| rule: { pattern: "$OBJ._headers" }, | ||||||||||||
| }); | ||||||||||||
| for (const m of matches) { | ||||||||||||
| const obj = m.getMatch("OBJ"); | ||||||||||||
| if (!obj) 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()`)); | ||||||||||||
| hasChanges = true; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if (!hasChanges) return null; | ||||||||||||
| return rootNode.commitEdits(edits); | ||||||||||||
|
Comment on lines
+88
to
+89
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
the hasChanges logic isn't needed. |
||||||||||||
| } | ||||||||||||
| 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()); | ||
| } |
| 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); | ||
| } |
| 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 |
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.
not fan of the name but my proposition did look right @nodejs/userland-migrations any idea ?