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 #6703

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions packages/vitest/src/integrations/css/css-modules.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createHash } from 'node:crypto'
import { hash } from '../../utils'
import type { CSSModuleScopeStrategy } from '../../node/types/config'

export function generateCssFilenameHash(filepath: string) {
return createHash('md5').update(filepath).digest('hex').slice(0, 6)
return hash('md5', filepath, 'hex').slice(0, 6)
}

export function generateScopedClassName(
Expand Down
5 changes: 2 additions & 3 deletions packages/vitest/src/node/cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import crypto from 'node:crypto'
import { resolve } from 'pathe'
import { slash } from '../../utils'
import { hash, slash } from '../../utils'
import { FilesStatsCache } from './files'
import { ResultsCache } from './results'

Expand All @@ -26,7 +25,7 @@ export class VitestCache {
? resolve(
root,
baseDir,
crypto.createHash('md5').update(projectName, 'utf-8').digest('hex'),
hash('md5', projectName, 'hex'),
)
: resolve(root, baseDir)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/pools/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createHash } from 'node:crypto'
import { mkdir, writeFile } from 'node:fs/promises'
import type { RawSourceMap } from 'vite-node'
import { join } from 'pathe'
import type { WorkspaceProject } from '../workspace'
import type { RuntimeRPC } from '../../types/rpc'
import { hash } from '../../utils'

const created = new Set()
const promises = new Map<string, Promise<void>>()
Expand Down Expand Up @@ -47,7 +47,7 @@ export function createMethodsRPC(project: WorkspaceProject, options: MethodsOpti
}

const dir = join(project.tmpDir, transformMode)
const name = createHash('sha1').update(id).digest('hex')
const name = hash('sha1', id, 'hex')
const tmp = join(dir, name)
if (promises.has(tmp)) {
await promises.get(tmp)
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/sequencers/BaseSequencer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createHash } from 'node:crypto'
import { relative, resolve } from 'pathe'
import { slash } from 'vite-node/utils'
import { hash } from '../../utils'
import type { Vitest } from '../core'
import type { WorkspaceSpec } from '../pool'
import type { TestSequencer } from './types'
Expand All @@ -25,7 +25,7 @@ export class BaseSequencer implements TestSequencer {
const specPath = fullPath?.slice(config.root.length)
return {
spec,
hash: createHash('sha1').update(specPath).digest('hex'),
hash: hash('sha1', specPath, 'hex'),
}
})
.sort((a, b) => (a.hash < b.hash ? -1 : a.hash > b.hash ? 1 : 0))
Expand Down
7 changes: 7 additions & 0 deletions packages/vitest/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from 'node:crypto'
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
import { relative } from 'pathe'
import { getWorkerState } from '../utils'

Expand Down Expand Up @@ -54,3 +55,9 @@ export function objectAttr(
}
return result
}

export const hash = crypto.hash ?? ((
algorithm: string,
data: crypto.BinaryLike,
outputEncoding: crypto.BinaryToTextEncoding,
) => crypto.createHash(algorithm).update(data).digest(outputEncoding))
Loading