Skip to content
Open
Show file tree
Hide file tree
Changes from 16 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 Oct 6, 2025
806f4c3
Merge branch 'nodejs:main' into feat/bytesread-to-byteswritten
Eliekhoury17 Oct 6, 2025
62ec425
chore(bytesRead-to-bytesWritten): update package name in package.json
Eliekhoury17 Oct 6, 2025
5c34494
apply code review
Eliekhoury17 Oct 6, 2025
5e2c8cf
Update recipes/zlib-bytesread-to-byteswritten/workflow.yaml
Eliekhoury17 Oct 6, 2025
e92bac6
Update recipes/zlib-bytesread-to-byteswritten/README.md
Eliekhoury17 Oct 6, 2025
847e000
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 Oct 6, 2025
7e316ca
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 Oct 6, 2025
0909317
docs/cleanup: clarify import check and fix loop formatting
Eliekhoury17 Oct 6, 2025
ef52444
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 Oct 6, 2025
1cfa52d
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 Oct 6, 2025
bb1223d
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 Oct 6, 2025
e9df49a
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 Oct 6, 2025
a209d7e
Update recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Eliekhoury17 Oct 6, 2025
6e18827
Apply suggestions from code review
Eliekhoury17 Oct 6, 2025
147e783
chore(zlib-codemod): apply all review feedback
Eliekhoury17 Oct 6, 2025
5be0912
Apply suggestions from code review
Eliekhoury17 Oct 7, 2025
6d14a43
chore(zlib-codemod): apply all review feedback
Eliekhoury17 Oct 7, 2025
e626aab
chore(zlib-codemod): apply all review feedback
Eliekhoury17 Oct 7, 2025
89dea38
Merge branch 'main' into feat/bytesread-to-byteswritten
JakobJingleheimer Oct 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

139 changes: 139 additions & 0 deletions recipes/zlib-bytesread-to-byteswritten/README.md
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);
});
```
Comment on lines +15 to +33
Copy link
Member

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?

Copy link
Member

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


**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;
```
24 changes: 24 additions & 0 deletions recipes/zlib-bytesread-to-byteswritten/codemod.yaml
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
24 changes: 24 additions & 0 deletions recipes/zlib-bytesread-to-byteswritten/package.json
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": "*"
}
}
157 changes: 157 additions & 0 deletions recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
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",
];

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: 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.includes(binding)) factoryBindings.push(binding);
}
}

// 1.b Handle dynamic imports: `await import("node:zlib")
const allDynamicImports: typeof rootNode[] = [];

const declPatterns = [
'const $VAR = await import($MODULE)',
'let $VAR = await import($MODULE)',
'var $VAR = await import($MODULE)'
];

for (const pattern of declPatterns) {
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.push(`${varName}.${factory}`);
}
}
}
}

// If any import is found it's mean we can skip transformation on this file
if (!importNodes.length && allDynamicImports.length === 0) 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) {
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) {
edits.push(match.replace(match.text().replace(".bytesRead", ".bytesWritten")));
}
}

// Step 4: Replace .bytesRead → .bytesWritten for function parameters
const funcKinds = ["function_declaration", "function_expression", "arrow_function"] as const;

// Helper to find all identifier nodes within a given node
function findIdentifiers(node: typeof rootNode): 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;
}

for (const kind of funcKinds) {
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);
}
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;
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
};
});
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;
Loading