nodejs / nodejs/userland-migrations
`crypto.DEFAULT_ENCODING`
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 85
- Forks
- 55
- Avg merge
- 3d 11h
- Merged PRs (30d)
- 9
Description
Description
Since crypto.DEFAULT_ENCODING is deprecated (DEP0091) and has reached End-of-Life status in Node.js v20.0.0, we should provide a codemod to replace it.
- The codemod should remove references to
crypto.DEFAULT_ENCODING. - The codemod should replace usage with explicit encoding parameters where needed.
- The codemod should identify where the default encoding was being read or set.
- The codemod should add comments suggesting explicit encoding usage.
Additional Information
Note that crypto.DEFAULT_ENCODING was removed in Node.js v20.0.0. This property only existed for compatibility with Node.js releases prior to version 0.9.3. The property allowed changing the default encoding for crypto operations, but this practice led to unpredictable behavior.
Instead of relying on a default encoding, developers should explicitly specify the encoding in each crypto operation. This makes the code more explicit and easier to understand.
Examples
Example 1: Reading DEFAULT_ENCODING
Before:
const crypto = require("node:crypto");
const encoding = crypto.DEFAULT_ENCODING;
After:
// crypto.DEFAULT_ENCODING has been removed
// Use explicit encoding in crypto operations instead
const encoding = "binary"; // or your preferred default encoding
Example 2: Setting DEFAULT_ENCODING
Before:
const crypto = require("node:crypto");
crypto.DEFAULT_ENCODING = "hex";
After:
// crypto.DEFAULT_ENCODING has been removed
// Always specify encoding explicitly in crypto operations
// For example: hash.digest("hex")
Example 3: Using DEFAULT_ENCODING in hash operations
Before:
const crypto = require("node:crypto");
crypto.DEFAULT_ENCODING = "hex";
const hash = crypto.createHash("sha256");
hash.update("some data");
const result = hash.digest(); // Uses DEFAULT_ENCODING
After:
const crypto = require("node:crypto");
const hash = crypto.createHash("sha256");
hash.update("some data");
const result = hash.digest("hex"); // Explicitly specify encoding
Example 4: Conditional encoding based on DEFAULT_ENCODING
Before:
const crypto = require("node:crypto");
const encoding = crypto.DEFAULT_ENCODING || "binary";
const hash = crypto.createHash("md5").update("data").digest(encoding);
After:
const crypto = require("node:crypto");
const encoding = "binary"; // Specify your preferred default
const hash = crypto.createHash("md5").update("data").digest(encoding);
Example 5: ESM import with DEFAULT_ENCODING
Before:
import crypto from "node:crypto";
if (crypto.DEFAULT_ENCODING === "hex") {
// do something
}
After:
// crypto.DEFAULT_ENCODING has been removed
// Use explicit encoding parameters in crypto operations
// Consider using a constant for your application's default encoding
const DEFAULT_ENCODING = "hex";
if (DEFAULT_ENCODING === "hex") {
// do something
}
Refs
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No implementation files or tests are named. Start by reviewing the codemod requirements and the DEP0091 and Node.js removal references, then trace how existing migrations handle reads, writes, and crypto calls. Done means the codemod covers the listed cases, adds explicit encoding parameters where needed, and provides comments for ambiguous usage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js, typescript
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100