nodejs / nodejs/userland-migrations

`crypto.DEFAULT_ENCODING`

Open
#407 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

good first issue human help wanted let's do it
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.