feat!: use SHA256 for creating cache keys for dlx (#8531)

This commit is contained in:
Zoltan Kochan
2024-09-16 02:36:06 +02:00
committed by GitHub
parent bcffd4d72b
commit f10256d088
7 changed files with 20 additions and 11 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-script-runners": major
---
Update dlx cache key to use SHA256.

View File

@@ -2,9 +2,13 @@ import crypto from 'crypto'
import fs from 'fs'
export function createShortHash (input: string): string {
return createHexHash(input).substring(0, 32)
}
export function createHexHash (input: string): string {
const hash = crypto.createHash('sha256')
hash.update(input)
return hash.digest('hex').substring(0, 32)
return hash.digest('hex')
}
export function createHash (input: string): string {

View File

@@ -50,7 +50,7 @@
"@pnpm/common-cli-options-help": "workspace:*",
"@pnpm/config": "workspace:*",
"@pnpm/core-loggers": "workspace:*",
"@pnpm/crypto.base32-hash": "workspace:*",
"@pnpm/crypto.hash": "workspace:*",
"@pnpm/env.path": "workspace:*",
"@pnpm/error": "workspace:*",
"@pnpm/lifecycle": "workspace:*",

View File

@@ -4,7 +4,7 @@ import util from 'util'
import { docsUrl } from '@pnpm/cli-utils'
import { OUTPUT_OPTIONS } from '@pnpm/common-cli-options-help'
import { type Config, types } from '@pnpm/config'
import { createBase32Hash } from '@pnpm/crypto.base32-hash'
import { createHexHash } from '@pnpm/crypto.hash'
import { PnpmError } from '@pnpm/error'
import { add } from '@pnpm/plugin-commands-installation'
import { readPackageJsonFromDir } from '@pnpm/read-package-json'
@@ -190,7 +190,7 @@ export function createCacheKey (pkgs: string[], registries: Record<string, strin
const sortedPkgs = [...pkgs].sort((a, b) => a.localeCompare(b))
const sortedRegistries = Object.entries(registries).sort(([k1], [k2]) => k1.localeCompare(k2))
const hashStr = JSON.stringify([sortedPkgs, sortedRegistries])
return createBase32Hash(hashStr)
return createHexHash(hashStr)
}
function getValidCacheDir (cacheLink: string, dlxCacheMaxAge: number): string | undefined {

View File

@@ -1,4 +1,4 @@
import { createBase32Hash } from '@pnpm/crypto.base32-hash'
import { createHexHash } from '@pnpm/crypto.hash'
import { createCacheKey } from '../src/dlx'
test('creates a hash', () => {
@@ -6,7 +6,7 @@ test('creates a hash', () => {
default: 'https://registry.npmjs.com/',
'@foo': 'https://example.com/npm-registry/foo/',
})
const expected = createBase32Hash(JSON.stringify([['@foo/bar', 'shx'], [
const expected = createHexHash(JSON.stringify([['@foo/bar', 'shx'], [
['@foo', 'https://example.com/npm-registry/foo/'],
['default', 'https://registry.npmjs.com/'],
]]))

View File

@@ -27,6 +27,9 @@
{
"path": "../../config/config"
},
{
"path": "../../crypto/hash"
},
{
"path": "../../env/path"
},
@@ -36,9 +39,6 @@
{
"path": "../../packages/core-loggers"
},
{
"path": "../../packages/crypto.base32-hash"
},
{
"path": "../../packages/error"
},

4
pnpm-lock.yaml generated
View File

@@ -2153,9 +2153,9 @@ importers:
'@pnpm/core-loggers':
specifier: workspace:*
version: link:../../packages/core-loggers
'@pnpm/crypto.base32-hash':
'@pnpm/crypto.hash':
specifier: workspace:*
version: link:../../packages/crypto.base32-hash
version: link:../../crypto/hash
'@pnpm/env.path':
specifier: workspace:*
version: link:../../env/path