Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use hash to replace createHash #8629

Merged
merged 7 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
11 changes: 10 additions & 1 deletion packages/crypto.base32-hash/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ import crypto from 'crypto'
import fs from 'fs'
import { base32 } from 'rfc4648'

const hash =
// @ts-expect-error -- crypto.hash is supported in Node 21.7.0+, 20.12.0+
crypto.hash ??
((
algorithm: string,
data: crypto.BinaryLike,
outputEncoding: crypto.BinaryToTextEncoding
) => crypto.createHash(algorithm).update(data).digest(outputEncoding))

export function createBase32Hash (str: string): string {
return base32.stringify(crypto.createHash('md5').update(str).digest()).replace(/(=+)$/, '').toLowerCase()
return base32.stringify(hash('md5', str, 'buffer')).replace(/(=+)$/, '').toLowerCase()
btea marked this conversation as resolved.
Show resolved Hide resolved
}

export async function createBase32HashFromFile (file: string): Promise<string> {
Expand Down
11 changes: 10 additions & 1 deletion pkg-manager/core/src/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,18 @@ Note that in CI environments, this setting is enabled by default.`,
}
}

const hash =
// @ts-expect-error -- crypto.hash is supported in Node 21.7.0+, 20.12.0+
crypto.hash ??
((
algorithm: string,
data: crypto.BinaryLike,
outputEncoding: crypto.BinaryToTextEncoding
) => crypto.createHash(algorithm).update(data).digest(outputEncoding))

export function createObjectChecksum (obj: Record<string, unknown>): string {
const s = JSON.stringify(sortKeys(obj, { deep: true }))
return crypto.createHash('md5').update(s).digest('hex')
return hash('md5', s, 'hex')
}

function cacheExpired (prunedAt: string, maxAgeInMinutes: number): boolean {
Expand Down
11 changes: 10 additions & 1 deletion resolving/npm-resolver/src/pickPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,18 @@ function clearMeta (pkg: PackageMeta): PackageMeta {
}
}

const hash =
// @ts-expect-error -- crypto.hash is supported in Node 21.7.0+, 20.12.0+
crypto.hash ??
((
algorithm: string,
data: crypto.BinaryLike,
outputEncoding: crypto.BinaryToTextEncoding
) => crypto.createHash(algorithm).update(data).digest(outputEncoding))

function encodePkgName (pkgName: string): string {
if (pkgName !== pkgName.toLowerCase()) {
return `${pkgName}_${crypto.createHash('md5').update(pkgName).digest('hex')}`
return `${pkgName}_${hash('md5', pkgName, 'hex')}`
}
return pkgName
}
Expand Down
13 changes: 11 additions & 2 deletions reviewing/list/src/pruneTree.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { type DependenciesHierarchy, type PackageNode } from '@pnpm/reviewing.dependencies-hierarchy'
import { type PackageDependencyHierarchy } from './types'
import { createHash } from 'crypto'
import crypto from 'crypto'

const hash =
// @ts-expect-error -- crypto.hash is supported in Node 21.7.0+, 20.12.0+
crypto.hash ??
((
algorithm: string,
data: crypto.BinaryLike,
outputEncoding: crypto.BinaryToTextEncoding
) => crypto.createHash(algorithm).update(data).digest(outputEncoding))

export function pruneDependenciesTrees (trees: PackageDependencyHierarchy[] | null, limit: number): PackageDependencyHierarchy[] {
if (trees === null) {
Expand Down Expand Up @@ -57,7 +66,7 @@ export function pruneDependenciesTrees (trees: PackageDependencyHierarchy[] | nu

for (const node of path) {
pathSoFar += `${node.name}@${node.version},`
const id = createHash('sha256').update(pathSoFar).digest('hex')
const id = hash('sha256', pathSoFar, 'hex')
let existingNode = map.get(id)

if (!existingNode) {
Expand Down
11 changes: 10 additions & 1 deletion worker/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ const INTEGRITY_REGEX: RegExp = /^([^-]+)-([A-Za-z0-9+/=]+)$/

parentPort!.on('message', handleMessage)

const hash =
// @ts-expect-error -- crypto.hash is supported in Node 21.7.0+, 20.12.0+
crypto.hash ??
((
algorithm: string,
data: crypto.BinaryLike,
outputEncoding: crypto.BinaryToTextEncoding
) => crypto.createHash(algorithm).update(data).digest(outputEncoding))

const cafsCache = new Map<string, CafsFunctions>()
const cafsStoreCache = new Map<string, Cafs>()
const cafsLocker = new Map<string, number>()
Expand Down Expand Up @@ -129,7 +138,7 @@ function addTarballToStore ({ buffer, cafsDir, integrity, filesIndexFile }: Tarb
// Compensate for the possibility of non-uniform Base64 padding
const normalizedRemoteHash: string = Buffer.from(integrityHash, 'base64').toString('hex')

const calculatedHash: string = crypto.createHash(algo).update(buffer).digest('hex')
const calculatedHash: string = hash(algo, buffer, 'hex')
if (calculatedHash !== normalizedRemoteHash) {
return {
status: 'error',
Expand Down
Loading