-
-
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
f6f823e
5b5ab63
6660cc2
b216c52
5137bdf
b6a2bd5
8c8ef55
File filter
Filter by extension
Conversations
Jump to
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.
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) |
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 |
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": { | ||
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. Maybe need to add jssg types as devdep to have typing on codemod context 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. @nekojanai you have to add it for strictness |
||
"@nodejs/codemod-utils": "*" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { Edit, Range, SgRoot } from '@codemod.com/jssg-types/main'; | ||
import type Js from '@codemod.com/jssg-types/langs/javascript'; | ||
import { getNodeRequireCalls } from '@nodejs/codemod-utils/ast-grep/require-call'; | ||
import { removeLines } from '@nodejs/codemod-utils/ast-grep/remove-lines'; | ||
|
||
export default function transform(root: SgRoot<Js>): string | null { | ||
const rootNode = root.root() | ||
const edits: Edit[] = []; | ||
const linesToRemove: Range[] = []; | ||
|
||
// @ts-expect-error - ast-grep types are not fully compatible with JSSG types | ||
Check failure on line 11 in recipes/buffer-atob-btoa/src/workflow.ts
|
||
nekojanai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const requireStatements = getNodeRequireCalls(root, 'buffer'); | ||
const atobFunctionCalls = rootNode.findAll({ | ||
rule: { | ||
pattern: 'buffer.atob($ARG)' | ||
} | ||
}); | ||
nekojanai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// Remove all buffer require statements | ||
for (const statement of requireStatements) { | ||
linesToRemove.push(statement.range()); | ||
} | ||
|
||
// Rewrite atob function calls | ||
for (const call of atobFunctionCalls) { | ||
const argMatch = call.getMatch("ARG"); | ||
if (argMatch) { | ||
const arg = argMatch.text(); | ||
const replacement = `Buffer.from(${arg}, 'base64').toString('binary')`; | ||
edits.push(call.replace(replacement)); | ||
} | ||
} | ||
|
||
const btoaFunctionCalls = rootNode.findAll({ | ||
rule: { | ||
pattern: 'buffer.btoa($ARG)' | ||
nekojanai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
}); | ||
|
||
// Rewrite btoa function calls | ||
for (const call of btoaFunctionCalls) { | ||
const argMatch = call.getMatch("ARG"); | ||
if (argMatch) { | ||
const arg = argMatch.text(); | ||
const replacement = `Buffer.from(${arg}, 'binary').toString('base64')`; | ||
edits.push(call.replace(replacement)); | ||
} | ||
} | ||
nekojanai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
return removeLines(rootNode.commitEdits(edits), linesToRemove); | ||
} |
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! |
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 |
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! |
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 |
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 |
Uh oh!
There was an error while loading. Please reload this page.