Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
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 replace 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 replace zlib.bytesRead with zlib.bytesWritten in all zlib transform streams and it handle 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": "*"
}
}
96 changes: 96 additions & 0 deletions recipes/zlib-bytesread-to-byteswritten/src/workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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 zlib imports/requires
const nodeRequires = getNodeRequireCalls(root, "node:zlib");
const nodeImports = getNodeImportStatements(root, "node:zlib");
const importNodes = [...nodeRequires, ...nodeImports];
if (!importNodes.length) return null;

const factoryBindings: string[] = [];
const streamVariables: string[] = [];

for (const node of importNodes) {
const baseBind = resolveBindingPath(node, "$");
if (baseBind) {
for (const factory of ZLIB_FACTORIES) factoryBindings.push(`${baseBind}.${factory}`);
}
for (const factory of ZLIB_FACTORIES) {
const binding = resolveBindingPath(node, `$.${factory}`);
if (binding && !factoryBindings.includes(binding)) factoryBindings.push(binding);
}
}

// 2️ Track variables assigned from factories
for (const factory of factoryBindings) {
const matches = rootNode.findAll({ rule: { pattern: `const $$$VAR = ${factory}($$$ARGS)` } });
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")));
}
}

// 4️ Replace .bytesRead → .bytesWritten for function parameters
const funcPatterns = [
"function $$$NAME($$$PARAMS) { $$$BODY }"
];

for (const pattern of funcPatterns) {
const funcs = rootNode.findAll({ rule: { pattern } });

for (const func of funcs) {
const params = func.getMultipleMatches("PARAMS");

for (const param of params) {
const paramNames = param
.text()
.split(",")
.map((p) => p.replace(/\/\*.*\*\//, "").trim())
.filter(Boolean);

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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createGzip } from "node:zlib";
const gzip = createGzip();
const bytesProcessed = gzip.bytesWritten;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const zlib = require("node:zlib");
const gzip = zlib.createGzip();
gzip.on("end", () => {
console.log("Bytes processed:", gzip.bytesWritten);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const zlib = require("node:zlib");
function trackProgress(stream) {
setInterval(() => {
console.log(`Progress: ${stream.bytesWritten} bytes`);
}, 1000);
}
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.bytesRead;
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.bytesRead,
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.bytesRead;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createGzip } from "node:zlib";
const gzip = createGzip();
const bytesProcessed = gzip.bytesRead;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const zlib = require("node:zlib");
const gzip = zlib.createGzip();
gzip.on("end", () => {
console.log("Bytes processed:", gzip.bytesRead);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const zlib = require("node:zlib");
function trackProgress(stream) {
setInterval(() => {
console.log(`Progress: ${stream.bytesRead} bytes`);
}, 1000);
}
25 changes: 25 additions & 0 deletions recipes/zlib-bytesread-to-byteswritten/workflow.yaml
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: Handle DEP0108 by replacing deprecated `zlib.bytesRead` with `zlib.bytesWritten` in Node.js transform streams`
js-ast-grep:
js_file: src/workflow.ts
base_path: .
include:
- "**/*.cjs"
- "**/*.cts"
- "**/*.js"
- "**/*.jsx"
- "**/*.mjs"
- "**/*.mts"
- "**/*.ts"
- "**/*.tsx"
exclude:
- "**/node_modules/**"
language: typescript
Loading