-
-
Notifications
You must be signed in to change notification settings - Fork 20
feat(migrate-legacy-buffer-atob-btoa): introduce
#206
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
Open
nekojanai
wants to merge
9
commits into
nodejs:main
Choose a base branch
from
nekojanai:feat(`migrate-legacy-buffer-atob-btoa`)
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.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f6f823e
feat(migrate-legacy-buffer-atob-btoa): introduce
nekojanai 5b5ab63
chore(package-lock.json): add recipe
nekojanai 6660cc2
chore(workflow.ts): replace template literals with string literals
nekojanai b216c52
Merge branch 'main' into feat(`migrate-legacy-buffer-atob-btoa`)
nekojanai 5137bdf
chore(workflow.ts): handle dynamic and esm imports
nekojanai b6a2bd5
chore(workflow.ts): improve handle dynamic and esm imports
nekojanai 8c8ef55
Merge branch 'main' into feat(`migrate-legacy-buffer-atob-btoa`)
nekojanai f4bd4ef
Merge branch 'main' into feat(`migrate-legacy-buffer-atob-btoa`)
nekojanai e02d531
chore(workflow.ts): add comment and empty edits check
nekojanai 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,42 @@ | ||
| # Migrate legacy `buffer.atob()` and `buffer.btoa()` APIs | ||
|
|
||
| Migrates usage of the legacy APIs `buffer.atob()` and `buffer.btoa()` to the current recommended approaches. | ||
|
|
||
| ## Example | ||
|
|
||
| ### Migrating buffer.atob(data) | ||
|
|
||
| **Before:** | ||
| ```js | ||
| const buffer = require('node:buffer'); | ||
| const data = 'SGVsbG8gV29ybGQh'; // "Hello World!" in base64 | ||
| const decodedData = buffer.atob(data); | ||
| console.log(decodedData); // Outputs: Hello World! | ||
| ``` | ||
|
|
||
| **After:** | ||
| ```js | ||
| const data = 'SGVsbG8gV29ybGQh'; // "Hello World!" in base64 | ||
| const decodedData = Buffer.from(data, 'base64').toString('binary'); | ||
| console.log(decodedData); // Outputs: Hello World! | ||
| ``` | ||
|
|
||
| ### Migrating buffer.btoa(data) | ||
|
|
||
| **Before:** | ||
| ```js | ||
| const buffer = require('node:buffer'); | ||
| const data = 'Hello World!'; | ||
| const encodedData = buffer.btoa(data); | ||
| console.log(encodedData); // Outputs: SGVsbG8gV29ybGQh | ||
| ``` | ||
|
|
||
| **After:** | ||
| ```js | ||
| const data = 'Hello World!'; | ||
| const encodedData = Buffer.from(data, 'binary').toString('base64'); | ||
| console.log(encodedData); // Outputs: SGVsbG8gV29ybGQh | ||
| ``` | ||
|
|
||
| ## REFS | ||
| * [Node.js Documentation: Buffer](https://nodejs.org/api/buffer.html) |
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,21 @@ | ||
| schema_version: "1.0" | ||
| name: "@nodejs/buffer-atob-btoa" | ||
| version: "1.0.0" | ||
| description: Migrates usage of the legacy APIs `buffer.atob()` and `buffer.btoa()` to the current recommended approaches | ||
| author: nekojanai (Jana) | ||
| license: MIT | ||
| workflow: workflow.yaml | ||
| category: migration | ||
|
|
||
| targets: | ||
| languages: | ||
| - javascript | ||
| - typescript | ||
|
|
||
| keywords: | ||
| - transformation | ||
| - migration | ||
|
|
||
| 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,21 @@ | ||
| { | ||
| "name": "@nodejs/buffer-atob-btoa", | ||
| "version": "1.0.0", | ||
| "description": "Migrates usage of the legacy APIs `buffer.atob()` and `buffer.btoa()` to the current recommended approaches", | ||
| "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/buffer-atob-btoa", | ||
| "bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
| }, | ||
| "author": "nekojanai (Jana)", | ||
| "license": "MIT", | ||
| "homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/buffer-atob-btoa/README.md", | ||
| "dependencies": { | ||
nekojanai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "@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,79 @@ | ||
| import type { Edit, Kinds, Range, SgNode, SgRoot } from '@codemod.com/jssg-types/main'; | ||
| import type Js from '@codemod.com/jssg-types/langs/javascript'; | ||
| import { resolveBindingPath } from '@nodejs/codemod-utils/ast-grep/resolve-binding-path'; | ||
| import { getNodeRequireCalls } from '@nodejs/codemod-utils/ast-grep/require-call'; | ||
| import { getNodeImportStatements } from '@nodejs/codemod-utils/ast-grep/import-statement'; | ||
| import { removeLines } from '@nodejs/codemod-utils/ast-grep/remove-lines'; | ||
| import { removeBinding } from '@nodejs/codemod-utils/ast-grep/remove-binding'; | ||
|
|
||
| export default function transform(root: SgRoot<Js>): string | null { | ||
| const rootNode = root.root(); | ||
| const bindingStatementFnTuples: [string, SgNode<Js, Kinds<Js>>, (arg: string) => string][] = []; | ||
nekojanai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const edits: Edit[] = []; | ||
| const linesToRemove: Range[] = []; | ||
|
|
||
| const updates = [ | ||
| { | ||
| oldBind: "$.atob", | ||
| replaceFn: (arg: string) => `Buffer.from(${arg}, 'base64').toString('binary')` | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| { | ||
| oldBind: "$.btoa", | ||
| replaceFn: (arg: string) => `Buffer.from(${arg}, 'binary').toString('base64')` | ||
| } | ||
| ]; | ||
|
|
||
| const statements = [...getNodeRequireCalls(root, 'buffer'), ...getNodeImportStatements(root, 'buffer')]; | ||
nekojanai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| for (const statement of statements) { | ||
| for (const update of updates) { | ||
| const binding = resolveBindingPath(statement, update.oldBind); | ||
| if (binding) bindingStatementFnTuples.push([binding, statement, update.replaceFn]); | ||
| } | ||
| } | ||
|
|
||
| for (const [binding, statement, fn] of bindingStatementFnTuples) { | ||
|
|
||
| const result = removeBinding(statement, binding); | ||
|
|
||
| if (result?.edit) edits.push(result.edit); | ||
| if (result?.lineToRemove) linesToRemove.push(result.lineToRemove); | ||
|
|
||
| const calls = rootNode.findAll({ | ||
| rule: { | ||
| pattern: `${binding}($ARG)` | ||
| } | ||
| }); | ||
|
|
||
| const otherCalls = rootNode.findAll({ | ||
nekojanai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| rule: { | ||
| all: [ | ||
| { | ||
| pattern: 'buffer.$FN' | ||
| }, | ||
| { | ||
| not: { | ||
| pattern: '$.btoa($ARG)' | ||
| } | ||
| }, | ||
| { | ||
| not: { | ||
| pattern: '$.atob($ARG)' | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| }); | ||
|
|
||
| for (const call of calls) { | ||
| const argMatch = call.getMatch("ARG"); | ||
| if (argMatch) edits.push(call.replace(fn(argMatch.text()))); | ||
| } | ||
|
|
||
| if (calls.length === otherCalls.length) { | ||
| linesToRemove.push(statement.range()); | ||
| } | ||
| } | ||
|
|
||
nekojanai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return removeLines(rootNode.commitEdits(edits), linesToRemove); | ||
| } | ||
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,3 @@ | ||
| const data = 'SGVsbG8gV29ybGQh'; // "Hello World!" in base64 | ||
| const decodedData = Buffer.from(data, 'base64').toString('binary'); | ||
| console.log(decodedData); // Outputs: Hello World! |
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,3 @@ | ||
| const data = 'Hello World!'; | ||
| const encodedData = Buffer.from(data, 'binary').toString('base64'); | ||
| console.log(encodedData); // Outputs: SGVsbG8gV29ybGQh |
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,3 @@ | ||
| const data = 'Hello World!'; | ||
| const encodedData = Buffer.from(data, 'binary').toString('base64'); | ||
| console.log(encodedData); // Outputs: SGVsbG8gV29ybGQh |
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,3 @@ | ||
| const data = 'Hello World!'; | ||
| const encodedData = Buffer.from(data, 'binary').toString('base64'); | ||
| console.log(encodedData); // Outputs: SGVsbG8gV29ybGQh |
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,4 @@ | ||
| import buffer from "node:buffer"; | ||
| buffer.constants.MAX_LENGTH; | ||
| const data = 'Hello World!'; | ||
| Buffer.from(data, 'binary').toString('base64'); |
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,4 @@ | ||
| import { isUtf8 } from "node:buffer"; | ||
| const data = 'Hello World!'; | ||
| isUtf8(data); | ||
| Buffer.from(data, 'base64').toString('binary'); |
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,4 @@ | ||
| const buffer = require('node:buffer'); | ||
| const data = 'SGVsbG8gV29ybGQh'; // "Hello World!" in base64 | ||
| const decodedData = buffer.atob(data); | ||
| console.log(decodedData); // Outputs: Hello World! |
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,4 @@ | ||
| const buffer = require('node:buffer'); | ||
| const data = 'Hello World!'; | ||
| const encodedData = buffer.btoa(data); | ||
| console.log(encodedData); // Outputs: SGVsbG8gV29ybGQh |
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,4 @@ | ||
| import buffer from "node:buffer"; | ||
| const data = 'Hello World!'; | ||
| const encodedData = buffer.btoa(data); | ||
| console.log(encodedData); // Outputs: SGVsbG8gV29ybGQh |
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,4 @@ | ||
| const buffer = require("node:buffer"); | ||
| const data = 'Hello World!'; | ||
| const encodedData = buffer.btoa(data); | ||
| console.log(encodedData); // Outputs: SGVsbG8gV29ybGQh |
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,4 @@ | ||
| import buffer from "node:buffer"; | ||
| buffer.constants.MAX_LENGTH; | ||
| const data = 'Hello World!'; | ||
| buffer.btoa(data); |
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,4 @@ | ||
| import { atob, isUtf8 } from "node:buffer"; | ||
| const data = 'Hello World!'; | ||
| isUtf8(data); | ||
| atob(data); |
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,27 @@ | ||
| # 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 | ||
| runtime: | ||
| type: direct | ||
| steps: | ||
| - name: Migrates usage of the legacy APIs `buffer.atob()` and `buffer.btoa()` to the current recommended approaches | ||
| js-ast-grep: | ||
| js_file: src/workflow.ts | ||
| base_path: . | ||
| include: | ||
| - "**/*.cjs" | ||
| - "**/*.js" | ||
| - "**/*.jsx" | ||
| - "**/*.mjs" | ||
| - "**/*.cts" | ||
| - "**/*.mts" | ||
| - "**/*.ts" | ||
| - "**/*.tsx" | ||
| exclude: | ||
| - "**/node_modules/**" | ||
| language: typescript |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.