Skip to content

Latest commit

Β 

History

History
73 lines (46 loc) Β· 1.58 KB

File metadata and controls

73 lines (46 loc) Β· 1.58 KB

n/prefer-global/crypto

πŸ“ Enforce either crypto or require("crypto").webcrypto.

An implementation of the Web Crypto API is defined as a global variable.

console.log(crypto === require("node:crypto").webcrypto) //β†’ true

It will be readable if we use crypto consistently.

πŸ“– Rule Details

This rule enforces whether to use the global crypto variable or the webcrypto property from the crypto module.

Options

This rule has a string option.

{
    "n/prefer-global/crypto": ["error", "always" | "never"]
}
  • "always" (default) ... enforces to use the global variable crypto rather than require("crypto").webcrypto.
  • "never" ... enforces to use require("crypto").webcrypto rather than the global variable crypto.

always

Examples of πŸ‘Ž incorrect code for this rule:

/*eslint n/prefer-global/crypto: [error]*/

const { webcrypto } = require("crypto")
webcrypto.randomUUID()

Examples of πŸ‘ correct code for this rule:

/*eslint n/prefer-global/crypto: [error]*/

crypto.randomUUID()

never

Examples of πŸ‘Ž incorrect code for the "never" option:

/*eslint n/prefer-global/crypto: [error, never]*/

crypto.randomUUID()

Examples of πŸ‘ correct code for the "never" option:

/*eslint n/prefer-global/crypto: [error, never]*/

const { webcrypto } = require("crypto")
webcrypto.randomUUID()

πŸ”Ž Implementation