-
-
Notifications
You must be signed in to change notification settings - Fork 19
feat(bytesRead-to-bytesWritten): implement Node.js DEP0108 migration #224
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
Eliekhoury17
wants to merge
20
commits into
nodejs:main
Choose a base branch
from
Eliekhoury17:feat/bytesread-to-byteswritten
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.
+503
−0
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
af2bd57
feat(bytesRead-to-bytesWritten): handle Node.js DEP0108 migration (#196)
Eliekhoury17 806f4c3
Merge branch 'nodejs:main' into feat/bytesread-to-byteswritten
Eliekhoury17 62ec425
chore(bytesRead-to-bytesWritten): update package name in package.json
Eliekhoury17 5c34494
apply code review
Eliekhoury17 5e2c8cf
Update recipes/zlib-bytesread-to-byteswritten/workflow.yaml
Eliekhoury17 e92bac6
Update recipes/zlib-bytesread-to-byteswritten/README.md
Eliekhoury17 847e000
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 7e316ca
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 0909317
docs/cleanup: clarify import check and fix loop formatting
Eliekhoury17 ef52444
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 1cfa52d
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 bb1223d
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 e9df49a
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 a209d7e
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 6e18827
Apply suggestions from code review
Eliekhoury17 147e783
chore(zlib-codemod): apply all review feedback
Eliekhoury17 5be0912
Apply suggestions from code review
Eliekhoury17 6d14a43
chore(zlib-codemod): apply all review feedback
Eliekhoury17 e626aab
chore(zlib-codemod): apply all review feedback
Eliekhoury17 89dea38
Merge branch 'main' into feat/bytesread-to-byteswritten
JakobJingleheimer 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
Some comments aren't visible on the classic Files Changed page.
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,139 @@ | ||
# `zlib.bytesRead` → `zlib.bytesWritten` DEP0108 | ||
|
||
This codemod replaces zlib.bytesRead with zlib.bytesWritten for consistent stream property naming. It's useful to migrate code that uses the deprecated property which has been removed. | ||
|
||
It replaces zlib.bytesRead with zlib.bytesWritten in all zlib transform streams and it handles both CommonJS and ESM imports. | ||
|
||
See [DEP0108](https://nodejs.org/api/deprecations.html#DEP0108). | ||
|
||
--- | ||
|
||
## Example | ||
|
||
**Case 1** | ||
|
||
Before: | ||
|
||
```js | ||
const zlib = require("node:zlib"); | ||
const gzip = zlib.createGzip(); | ||
gzip.on("end", () => { | ||
console.log("Bytes processed:", gzip.bytesRead); | ||
}); | ||
``` | ||
|
||
After: | ||
|
||
```js | ||
const zlib = require("node:zlib"); | ||
const gzip = zlib.createGzip(); | ||
gzip.on("end", () => { | ||
console.log("Bytes processed:", gzip.bytesWritten); | ||
}); | ||
``` | ||
|
||
**Case 2** | ||
|
||
Before: | ||
|
||
```js | ||
const zlib = require("node:zlib"); | ||
const deflate = zlib.createDeflate(); | ||
deflate.on("finish", () => { | ||
const stats = { | ||
input: deflate.bytesRead, | ||
output: deflate.bytesWritten | ||
}; | ||
}); | ||
``` | ||
|
||
After: | ||
|
||
```js | ||
const zlib = require("node:zlib"); | ||
const deflate = zlib.createDeflate(); | ||
deflate.on("finish", () => { | ||
const stats = { | ||
input: deflate.bytesWritten, | ||
output: deflate.bytesWritten | ||
}; | ||
}); | ||
``` | ||
|
||
**Case 3** | ||
|
||
Before: | ||
|
||
```js | ||
const zlib = require("node:zlib"); | ||
function trackProgress(stream) { | ||
setInterval(() => { | ||
console.log(`Progress: ${stream.bytesRead} bytes`); | ||
}, 1000); | ||
} | ||
``` | ||
|
||
After: | ||
|
||
```js | ||
const zlib = require("node:zlib"); | ||
function trackProgress(stream) { | ||
setInterval(() => { | ||
console.log(`Progress: ${stream.bytesWritten} bytes`); | ||
}, 1000); | ||
} | ||
``` | ||
|
||
**Case 4** | ||
|
||
Before: | ||
|
||
```js | ||
import { createGzip } from "node:zlib"; | ||
const gzip = createGzip(); | ||
const bytesProcessed = gzip.bytesRead; | ||
``` | ||
|
||
After: | ||
|
||
```js | ||
import { createGzip } from "node:zlib"; | ||
const gzip = createGzip(); | ||
const bytesProcessed = gzip.bytesWritten; | ||
``` | ||
|
||
**Case 5** | ||
|
||
Before: | ||
|
||
```js | ||
const zlib = require("node:zlib"); | ||
const gzip = zlib.createGzip(); | ||
const processed = gzip.bytesRead; | ||
``` | ||
|
||
After: | ||
|
||
```js | ||
const zlib = require("node:zlib"); | ||
const gzip = zlib.createGzip(); | ||
const processed = gzip.bytesWritten; | ||
``` | ||
|
||
**Case 6** | ||
|
||
Before: | ||
|
||
```js | ||
const { createGzip } = require("node:zlib"); | ||
const gzip = createGzip(); | ||
const bytes = gzip.bytesRead; | ||
``` | ||
|
||
After: | ||
|
||
```js | ||
const { createGzip } = require("node:zlib"); | ||
const gzip = createGzip(); | ||
const bytes = gzip.bytesWritten; | ||
``` |
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 @@ | ||
schema_version: "1.0" | ||
name: "@nodejs/zlib-bytesread-to-byteswritten" | ||
version: 1.0.0 | ||
description: Handle DEP0108 by replacing deprecated `zlib.bytesRead` with `zlib.bytesWritten` in Node.js transform streams | ||
author: Elie Khoury | ||
license: MIT | ||
workflow: workflow.yaml | ||
category: migration | ||
|
||
targets: | ||
languages: | ||
- javascript | ||
- typescript | ||
|
||
keywords: | ||
- transformation | ||
- migration | ||
- zlib | ||
- bytesRead | ||
- bytesWritten | ||
|
||
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/zlib-bytesread-to-byteswritten", | ||
"version": "1.0.0", | ||
"description": "Replace deprecated `zlib.bytesRead` with `zlib.bytesWritten` in Node.js transform streams", | ||
"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/zlib-bytesread-to-byteswritten", | ||
"bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
}, | ||
"author": "Elie Khoury", | ||
"license": "MIT", | ||
"homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/zlib-bytesread-to-byteswritten/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,156 @@ | ||
import { getNodeRequireCalls } from "@nodejs/codemod-utils/ast-grep/require-call"; | ||
import { getNodeImportStatements } from "@nodejs/codemod-utils/ast-grep/import-statement"; | ||
import { resolveBindingPath } from "@nodejs/codemod-utils/ast-grep/resolve-binding-path"; | ||
import { removeLines } from "@nodejs/codemod-utils/ast-grep/remove-lines"; | ||
import type { Edit, Range, SgRoot } from "@codemod.com/jssg-types/main"; | ||
import type Js from "@codemod.com/jssg-types/langs/javascript"; | ||
|
||
const ZLIB_FACTORIES = [ | ||
"createGzip", | ||
"createGunzip", | ||
"createDeflate", | ||
"createInflate", | ||
"createBrotliCompress", | ||
"createBrotliDecompress", | ||
"createUnzip", | ||
]; | ||
|
||
const DECL_PATTERNS = [ | ||
'const $VAR = await import($MODULE)', | ||
'let $VAR = await import($MODULE)', | ||
'var $VAR = await import($MODULE)' | ||
]; | ||
|
||
const FUNC_KINDS = ["function_declaration", "function_expression", "arrow_function"] as const; | ||
|
||
// Helper to find all identifier nodes within a given node | ||
function findIdentifiers(node: ReturnType<SgRoot<Js>["root"]>): string[] { | ||
const identifiers: string[] = []; | ||
const stack = [node]; | ||
|
||
while (stack.length) { | ||
const current = stack.pop()!; | ||
if (current.kind() === "identifier") { | ||
identifiers.push(current.text()); | ||
} | ||
stack.push(...current.children()); | ||
} | ||
|
||
return identifiers; | ||
} | ||
|
||
export default function transform(root: SgRoot<Js>): string | null { | ||
const rootNode = root.root(); | ||
const edits: Edit[] = []; | ||
const linesToRemove: Range[] = []; | ||
|
||
// 1 Find all static zlib imports/requires | ||
const importNodes = [ | ||
...getNodeRequireCalls(root, "node:zlib"), | ||
...getNodeImportStatements(root, "node:zlib") | ||
]; | ||
|
||
const factoryBindings = new Set<string>(); | ||
const streamVariables: string[] = []; | ||
|
||
// 1.a Handle static imports: `import { createGzip } from "node:zlib" | ||
for (const node of importNodes) { | ||
for (const factory of ZLIB_FACTORIES) { | ||
const binding = resolveBindingPath(node, `$.${factory}`); | ||
if (binding) factoryBindings.add(binding); | ||
} | ||
} | ||
|
||
// 1.b Handle dynamic imports: `await import("node:zlib") | ||
const allDynamicImports: typeof rootNode[] = []; | ||
|
||
for (const pattern of DECL_PATTERNS) { | ||
const dynamicImports = rootNode.findAll({ rule: { pattern } }); | ||
allDynamicImports.push(...dynamicImports); | ||
} | ||
|
||
for (const imp of allDynamicImports) { | ||
const moduleName = imp.find({ | ||
rule: { | ||
kind: "string_fragment", | ||
inside: { | ||
kind: "string", | ||
inside: { kind: "arguments" } | ||
} | ||
} | ||
})?.text(); | ||
|
||
if (moduleName === "node:zlib") { | ||
const varName = imp.find({ | ||
rule: { | ||
kind: "identifier", | ||
inside: { kind: "variable_declarator" } | ||
} | ||
})?.text(); | ||
|
||
if (varName) { | ||
for (const factory of ZLIB_FACTORIES) { | ||
factoryBindings.add(`${varName}.${factory}`); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// If no import is found that means we can skip transformation on this file | ||
if (!importNodes.length && !allDynamicImports.length) return null; | ||
|
||
// 2 Track variables assigned from factories (const, let, var) | ||
for (const binding of factoryBindings) { | ||
const patterns = [ | ||
`const $$$VAR = ${binding}($$$ARGS)`, | ||
`let $$$VAR = ${binding}($$$ARGS)`, | ||
`var $$$VAR = ${binding}($$$ARGS)` | ||
]; | ||
|
||
for (const pattern of patterns) { | ||
const matches = rootNode.findAll({ rule: { pattern } }); | ||
|
||
for (const match of matches) { | ||
const varMatch = match.getMultipleMatches("VAR"); | ||
|
||
if (varMatch.length) { | ||
Eliekhoury17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const varName = varMatch[0].text(); | ||
if (!streamVariables.includes(varName)) streamVariables.push(varName); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// 3 Replace .bytesRead → .bytesWritten for tracked variables | ||
for (const variable of streamVariables) { | ||
const matches = rootNode.findAll({ rule: { pattern: `${variable}.bytesRead` } }); | ||
|
||
for (const match of matches) { | ||
Eliekhoury17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
edits.push(match.replace(match.text().replace(".bytesRead", ".bytesWritten"))); | ||
} | ||
} | ||
|
||
// Step 4: Replace .bytesRead → .bytesWritten for function parameters | ||
for (const kind of FUNC_KINDS) { | ||
const funcs = rootNode.findAll({ rule: { kind } }); | ||
|
||
for (const func of funcs) { | ||
const formalParamsNode = func.children().find(child => child.kind() === "formal_parameters"); | ||
|
||
if (!formalParamsNode) continue; | ||
|
||
const paramNames = findIdentifiers(formalParamsNode); | ||
|
||
for (const paramName of paramNames) { | ||
const matches = rootNode.findAll({ rule: { pattern: `${paramName}.bytesRead` } }); | ||
for (const match of matches) { | ||
edits.push(match.replace(match.text().replace(".bytesRead", ".bytesWritten"))); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!edits.length) return null; | ||
|
||
return removeLines(rootNode.commitEdits(edits), linesToRemove); | ||
} |
3 changes: 3 additions & 0 deletions
3
recipes/zlib-bytesread-to-byteswritten/tests/expected/assigned_bytesRead_variable.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,3 @@ | ||
const zlib = require("node:zlib"); | ||
const gzip = zlib.createGzip(); | ||
const processed = gzip.bytesWritten; |
8 changes: 8 additions & 0 deletions
8
recipes/zlib-bytesread-to-byteswritten/tests/expected/deflate_finish_stats.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,8 @@ | ||
const zlib = require("node:zlib"); | ||
const deflate = zlib.createDeflate(); | ||
deflate.on("finish", () => { | ||
const stats = { | ||
input: deflate.bytesWritten, | ||
output: deflate.bytesWritten | ||
}; | ||
}); |
3 changes: 3 additions & 0 deletions
3
recipes/zlib-bytesread-to-byteswritten/tests/expected/destructured_createGzip.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,3 @@ | ||
const { createGzip } = require("node:zlib"); | ||
const gzip = createGzip(); | ||
const bytes = gzip.bytesWritten; |
Oops, something went wrong.
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.
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.
@alexbit-codemod what was the verdict about
diff
fenced blocks?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.
@JakobJingleheimer FYI I asked for that on codemod slack