From f5f2ed5b77df4f3106281edc9df2c165b51ed5f8 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Mon, 14 Oct 2024 22:34:16 +0800 Subject: [PATCH 1/3] perf: use `hash` to replace `createHash` --- packages/vitest/src/integrations/css/css-modules.ts | 4 ++-- packages/vitest/src/node/cache/index.ts | 5 ++--- packages/vitest/src/node/pools/rpc.ts | 4 ++-- packages/vitest/src/node/sequencers/BaseSequencer.ts | 4 ++-- packages/vitest/src/utils/index.ts | 7 +++++++ 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/vitest/src/integrations/css/css-modules.ts b/packages/vitest/src/integrations/css/css-modules.ts index 95a50272989c..ed3523fa8db4 100644 --- a/packages/vitest/src/integrations/css/css-modules.ts +++ b/packages/vitest/src/integrations/css/css-modules.ts @@ -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( diff --git a/packages/vitest/src/node/cache/index.ts b/packages/vitest/src/node/cache/index.ts index 3d6008ae0e8d..9db764afcdd1 100644 --- a/packages/vitest/src/node/cache/index.ts +++ b/packages/vitest/src/node/cache/index.ts @@ -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' @@ -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) } diff --git a/packages/vitest/src/node/pools/rpc.ts b/packages/vitest/src/node/pools/rpc.ts index 9bfde0e6cb9c..51ddd730a2a6 100644 --- a/packages/vitest/src/node/pools/rpc.ts +++ b/packages/vitest/src/node/pools/rpc.ts @@ -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>() @@ -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) diff --git a/packages/vitest/src/node/sequencers/BaseSequencer.ts b/packages/vitest/src/node/sequencers/BaseSequencer.ts index 8b3715757355..c46ec62aad2d 100644 --- a/packages/vitest/src/node/sequencers/BaseSequencer.ts +++ b/packages/vitest/src/node/sequencers/BaseSequencer.ts @@ -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' @@ -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)) diff --git a/packages/vitest/src/utils/index.ts b/packages/vitest/src/utils/index.ts index 39e9cc847ba8..2216dea450b7 100644 --- a/packages/vitest/src/utils/index.ts +++ b/packages/vitest/src/utils/index.ts @@ -1,3 +1,4 @@ +import crypto from 'node:crypto' import { relative } from 'pathe' import { getWorkerState } from '../utils' @@ -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)) From 21627401f69c6aea6fb87432189e419c9dbb7690 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Mon, 14 Oct 2024 22:56:44 +0800 Subject: [PATCH 2/3] refactor: create hash file --- packages/vitest/src/integrations/css/css-modules.ts | 2 +- packages/vitest/src/node/cache/index.ts | 3 ++- packages/vitest/src/node/hash.ts | 7 +++++++ packages/vitest/src/node/pools/rpc.ts | 2 +- packages/vitest/src/node/sequencers/BaseSequencer.ts | 2 +- packages/vitest/src/utils/index.ts | 7 ------- 6 files changed, 12 insertions(+), 11 deletions(-) create mode 100644 packages/vitest/src/node/hash.ts diff --git a/packages/vitest/src/integrations/css/css-modules.ts b/packages/vitest/src/integrations/css/css-modules.ts index ed3523fa8db4..15948db5a27c 100644 --- a/packages/vitest/src/integrations/css/css-modules.ts +++ b/packages/vitest/src/integrations/css/css-modules.ts @@ -1,4 +1,4 @@ -import { hash } from '../../utils' +import { hash } from '../../node/hash' import type { CSSModuleScopeStrategy } from '../../node/types/config' export function generateCssFilenameHash(filepath: string) { diff --git a/packages/vitest/src/node/cache/index.ts b/packages/vitest/src/node/cache/index.ts index 9db764afcdd1..d6483db41bfa 100644 --- a/packages/vitest/src/node/cache/index.ts +++ b/packages/vitest/src/node/cache/index.ts @@ -1,5 +1,6 @@ import { resolve } from 'pathe' -import { hash, slash } from '../../utils' +import { slash } from '../../utils' +import { hash } from '../hash' import { FilesStatsCache } from './files' import { ResultsCache } from './results' diff --git a/packages/vitest/src/node/hash.ts b/packages/vitest/src/node/hash.ts new file mode 100644 index 000000000000..7c3c9edcf44a --- /dev/null +++ b/packages/vitest/src/node/hash.ts @@ -0,0 +1,7 @@ +import crypto from 'node:crypto' + +export const hash = crypto.hash ?? (( + algorithm: string, + data: crypto.BinaryLike, + outputEncoding: crypto.BinaryToTextEncoding, + ) => crypto.createHash(algorithm).update(data).digest(outputEncoding)) \ No newline at end of file diff --git a/packages/vitest/src/node/pools/rpc.ts b/packages/vitest/src/node/pools/rpc.ts index 51ddd730a2a6..21825c4f0705 100644 --- a/packages/vitest/src/node/pools/rpc.ts +++ b/packages/vitest/src/node/pools/rpc.ts @@ -3,7 +3,7 @@ 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' +import { hash } from '../hash' const created = new Set() const promises = new Map>() diff --git a/packages/vitest/src/node/sequencers/BaseSequencer.ts b/packages/vitest/src/node/sequencers/BaseSequencer.ts index c46ec62aad2d..92636512f60e 100644 --- a/packages/vitest/src/node/sequencers/BaseSequencer.ts +++ b/packages/vitest/src/node/sequencers/BaseSequencer.ts @@ -1,6 +1,6 @@ import { relative, resolve } from 'pathe' import { slash } from 'vite-node/utils' -import { hash } from '../../utils' +import { hash } from '../hash' import type { Vitest } from '../core' import type { WorkspaceSpec } from '../pool' import type { TestSequencer } from './types' diff --git a/packages/vitest/src/utils/index.ts b/packages/vitest/src/utils/index.ts index 2216dea450b7..39e9cc847ba8 100644 --- a/packages/vitest/src/utils/index.ts +++ b/packages/vitest/src/utils/index.ts @@ -1,4 +1,3 @@ -import crypto from 'node:crypto' import { relative } from 'pathe' import { getWorkerState } from '../utils' @@ -55,9 +54,3 @@ 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)) From 291761923d294d36e0b23f75a016e73c85b35d0f Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Mon, 14 Oct 2024 22:59:32 +0800 Subject: [PATCH 3/3] fix: lint --- packages/vitest/src/node/hash.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/vitest/src/node/hash.ts b/packages/vitest/src/node/hash.ts index 7c3c9edcf44a..cd93d8216d25 100644 --- a/packages/vitest/src/node/hash.ts +++ b/packages/vitest/src/node/hash.ts @@ -1,7 +1,7 @@ import crypto from 'node:crypto' export const hash = crypto.hash ?? (( - algorithm: string, - data: crypto.BinaryLike, - outputEncoding: crypto.BinaryToTextEncoding, - ) => crypto.createHash(algorithm).update(data).digest(outputEncoding)) \ No newline at end of file + algorithm: string, + data: crypto.BinaryLike, + outputEncoding: crypto.BinaryToTextEncoding, +) => crypto.createHash(algorithm).update(data).digest(outputEncoding))