refactor(cafs): create a separate function for creating index file path (#8508)

This commit is contained in:
Zoltan Kochan
2024-09-08 14:43:09 +02:00
committed by GitHub
parent 681fba52e4
commit db420ab592
16 changed files with 50 additions and 49 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/store.cafs": major
---
`getFilePathInCafs` renamed to `getIndexFilePathInCafs`.

View File

@@ -1,6 +1,6 @@
import fs from 'fs'
import path from 'path'
import { getFilePathInCafs } from '@pnpm/store.cafs'
import { getIndexFilePathInCafs } from '@pnpm/store.cafs'
import { getIntegrity, REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
export interface StoreAssertions {
@@ -25,7 +25,7 @@ export function assertStore (
getPkgIndexFilePath (pkgName: string, version?: string): string {
const cafsDir = path.join(storePath, 'files')
const integrity = version ? getIntegrity(pkgName, version) : pkgName
return getFilePathInCafs(cafsDir, integrity, 'index')
return getIndexFilePathInCafs(cafsDir, integrity)
},
cafsHas (pkgName: string, version?: string): void {
const pathToCheck = store.getPkgIndexFilePath(pkgName, version)

View File

@@ -1,7 +1,7 @@
import assert from 'assert'
import path from 'path'
import util from 'util'
import { getFilePathInCafs, type PackageFilesIndex } from '@pnpm/store.cafs'
import { getIndexFilePathInCafs, type PackageFilesIndex } from '@pnpm/store.cafs'
import { calcDepState, lockfileToDepGraph, type DepsStateCache } from '@pnpm/calc-dep-state'
import {
LAYOUT_VERSION,
@@ -316,7 +316,7 @@ async function _rebuild (
const resolution = (pkgSnapshot.resolution as TarballResolution)
let sideEffectsCacheKey: string | undefined
if (opts.skipIfHasSideEffectsCache && resolution.integrity) {
const filesIndexFile = getFilePathInCafs(cafsDir, resolution.integrity!.toString(), 'index')
const filesIndexFile = getIndexFilePathInCafs(cafsDir, resolution.integrity!.toString())
const pkgFilesIndex = await loadJsonFile<PackageFilesIndex>(filesIndexFile)
sideEffectsCacheKey = calcDepState(depGraph, depsStateCache, depPath, {
isBuilt: true,
@@ -341,7 +341,7 @@ async function _rebuild (
})
if (hasSideEffects && (opts.sideEffectsCacheWrite ?? true) && resolution.integrity) {
builtDepPaths.add(depPath)
const filesIndexFile = getFilePathInCafs(cafsDir, resolution.integrity!.toString(), 'index')
const filesIndexFile = getIndexFilePathInCafs(cafsDir, resolution.integrity!.toString())
try {
if (!sideEffectsCacheKey) {
sideEffectsCacheKey = calcDepState(depGraph, depsStateCache, depPath, {

View File

@@ -1,7 +1,7 @@
/// <reference path="../../../__typings__/index.d.ts" />
import fs from 'fs'
import path from 'path'
import { getFilePathInCafs } from '@pnpm/store.cafs'
import { getIndexFilePathInCafs } from '@pnpm/store.cafs'
import { ENGINE_NAME, WANTED_LOCKFILE } from '@pnpm/constants'
import { hashObject } from '@pnpm/crypto.object-hasher'
import { rebuild } from '@pnpm/plugin-commands-rebuild'
@@ -75,7 +75,7 @@ test('rebuilds dependencies', async () => {
}
const cafsDir = path.join(storeDir, 'v3/files')
const cacheIntegrityPath = getFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'), 'index')
const cacheIntegrityPath = getIndexFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'))
const cacheIntegrity = loadJsonFile.sync<any>(cacheIntegrityPath) // eslint-disable-line @typescript-eslint/no-explicit-any
expect(cacheIntegrity!.sideEffects).toBeTruthy()
const sideEffectsKey = `${ENGINE_NAME}-${hashObject({ '@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`
@@ -100,7 +100,7 @@ test('skipIfHasSideEffectsCache', async () => {
])
const cafsDir = path.join(storeDir, 'v3/files')
const cacheIntegrityPath = getFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'), 'index')
const cacheIntegrityPath = getIndexFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'))
let cacheIntegrity = loadJsonFile.sync<any>(cacheIntegrityPath) // eslint-disable-line @typescript-eslint/no-explicit-any
const sideEffectsKey = `${ENGINE_NAME}-${hashObject({ '@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`
cacheIntegrity.sideEffects = {

View File

@@ -1,6 +1,6 @@
// cspell:ignore ents
import fs from 'fs'
import { getFilePathInCafs, getFilePathByModeInCafs, type PackageFilesIndex } from '@pnpm/store.cafs'
import { getIndexFilePathInCafs, getFilePathByModeInCafs, type PackageFilesIndex } from '@pnpm/store.cafs'
import { type Lockfile, readWantedLockfile, type PackageSnapshot, type TarballResolution } from '@pnpm/lockfile.fs'
import {
nameVerFromPkgSnapshot,
@@ -180,7 +180,7 @@ export function createFuseHandlersFromLockfile (lockfile: Lockfile, cafsDir: str
if (!pkgSnapshotCache.has(depPath)) {
const pkgSnapshot = lockfile.packages?.[depPath as DepPath]
if (pkgSnapshot == null) return undefined
const indexPath = getFilePathInCafs(cafsDir, (pkgSnapshot.resolution as TarballResolution).integrity!, 'index')
const indexPath = getIndexFilePathInCafs(cafsDir, (pkgSnapshot.resolution as TarballResolution).integrity!)
pkgSnapshotCache.set(depPath, {
...nameVerFromPkgSnapshot(depPath, pkgSnapshot),
pkgSnapshot,

View File

@@ -2,7 +2,7 @@ import fs from 'fs'
import path from 'path'
import { addDependenciesToPackage, install } from '@pnpm/core'
import { hashObject } from '@pnpm/crypto.object-hasher'
import { getFilePathInCafs, getFilePathByModeInCafs, type PackageFilesIndex } from '@pnpm/store.cafs'
import { getIndexFilePathInCafs, getFilePathByModeInCafs, type PackageFilesIndex } from '@pnpm/store.cafs'
import { getIntegrity, REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
import { prepareEmpty } from '@pnpm/prepare'
import { ENGINE_NAME } from '@pnpm/constants'
@@ -83,7 +83,7 @@ test('using side effects cache', async () => {
const manifest = await addDependenciesToPackage({}, ['@pnpm.e2e/pre-and-postinstall-scripts-example@1.0.0'], opts)
const cafsDir = path.join(opts.storeDir, 'files')
const filesIndexFile = getFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'), 'index')
const filesIndexFile = getIndexFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'))
const filesIndex = loadJsonFile.sync<PackageFilesIndex>(filesIndexFile)
expect(filesIndex.sideEffects).toBeTruthy() // files index has side effects
const sideEffectsKey = `${ENGINE_NAME}-${hashObject({ '@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`
@@ -157,7 +157,7 @@ test('uploading errors do not interrupt installation', async () => {
expect(fs.existsSync('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall.js')).toBeTruthy()
const cafsDir = path.join(opts.storeDir, 'files')
const filesIndexFile = getFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'), 'index')
const filesIndexFile = getIndexFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'))
const filesIndex = loadJsonFile.sync<PackageFilesIndex>(filesIndexFile)
expect(filesIndex.sideEffects).toBeFalsy()
})
@@ -175,7 +175,7 @@ test('a postinstall script does not modify the original sources added to the sto
expect(fs.readFileSync('node_modules/@pnpm/postinstall-modifies-source/empty-file.txt', 'utf8')).toContain('hello')
const cafsDir = path.join(opts.storeDir, 'files')
const filesIndexFile = getFilePathInCafs(cafsDir, getIntegrity('@pnpm/postinstall-modifies-source', '1.0.0'), 'index')
const filesIndexFile = getIndexFilePathInCafs(cafsDir, getIntegrity('@pnpm/postinstall-modifies-source', '1.0.0'))
const filesIndex = loadJsonFile.sync<PackageFilesIndex>(filesIndexFile)
const patchedFileIntegrity = filesIndex.sideEffects?.[`${ENGINE_NAME}-${hashObject({})}`]['empty-file.txt']?.integrity
expect(patchedFileIntegrity).toBeTruthy()
@@ -184,7 +184,7 @@ test('a postinstall script does not modify the original sources added to the sto
// The integrity of the original file differs from the integrity of the patched file
expect(originalFileIntegrity).not.toEqual(patchedFileIntegrity)
expect(fs.readFileSync(getFilePathInCafs(cafsDir, originalFileIntegrity, 'nonexec'), 'utf8')).toEqual('')
expect(fs.readFileSync(getFilePathByModeInCafs(cafsDir, originalFileIntegrity, 420), 'utf8')).toEqual('')
})
test('a corrupted side-effects cache is ignored', async () => {
@@ -198,7 +198,7 @@ test('a corrupted side-effects cache is ignored', async () => {
const manifest = await addDependenciesToPackage({}, ['@pnpm.e2e/pre-and-postinstall-scripts-example@1.0.0'], opts)
const cafsDir = path.join(opts.storeDir, 'files')
const filesIndexFile = getFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'), 'index')
const filesIndexFile = getIndexFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'))
const filesIndex = loadJsonFile.sync<PackageFilesIndex>(filesIndexFile)
expect(filesIndex.sideEffects).toBeTruthy() // files index has side effects
const sideEffectsKey = `${ENGINE_NAME}-${hashObject({ '@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`

View File

@@ -3,7 +3,7 @@ import fs from 'fs'
import path from 'path'
import { assertProject } from '@pnpm/assert-project'
import { hashObject } from '@pnpm/crypto.object-hasher'
import { getFilePathInCafs } from '@pnpm/store.cafs'
import { getIndexFilePathInCafs } from '@pnpm/store.cafs'
import { ENGINE_NAME, WANTED_LOCKFILE } from '@pnpm/constants'
import {
type PackageManifestLog,
@@ -678,7 +678,7 @@ test.each([['isolated'], ['hoisted']])('using side effects cache with nodeLinker
await headlessInstall(opts)
const cafsDir = path.join(opts.storeDir, 'files')
const cacheIntegrityPath = getFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'), 'index')
const cacheIntegrityPath = getIndexFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/pre-and-postinstall-scripts-example', '1.0.0'))
const cacheIntegrity = loadJsonFile.sync<any>(cacheIntegrityPath) // eslint-disable-line @typescript-eslint/no-explicit-any
expect(cacheIntegrity!.sideEffects).toBeTruthy()
const sideEffectsKey = `${ENGINE_NAME}-${hashObject({ '@pnpm.e2e/hello-world-js-bin@1.0.0': {} })}`

View File

@@ -2,9 +2,8 @@ import { createReadStream, promises as fs } from 'fs'
import os from 'os'
import path from 'path'
import {
type FileType,
getFilePathByModeInCafs as _getFilePathByModeInCafs,
getFilePathInCafs as _getFilePathInCafs,
getIndexFilePathInCafs as _getIndexFilePathInCafs,
type PackageFilesIndex,
} from '@pnpm/store.cafs'
import { fetchingProgressLogger, progressLogger } from '@pnpm/core-loggers'
@@ -109,14 +108,14 @@ export function createPackageRequester (
})
const cafsDir = path.join(opts.storeDir, 'files')
const getFilePathInCafs = _getFilePathInCafs.bind(null, cafsDir)
const getIndexFilePathInCafs = _getIndexFilePathInCafs.bind(null, cafsDir)
const fetch = fetcher.bind(null, opts.fetchers, opts.cafs)
const fetchPackageToStore = fetchToStore.bind(null, {
readPkgFromCafs: _readPkgFromCafs.bind(null, cafsDir, opts.verifyStoreIntegrity),
fetch,
fetchingLocker: new Map(),
getFilePathByModeInCafs: _getFilePathByModeInCafs.bind(null, cafsDir),
getFilePathInCafs,
getIndexFilePathInCafs,
requestsQueue: Object.assign(requestsQueue, {
counter: 0,
concurrency: networkConcurrency,
@@ -139,7 +138,7 @@ export function createPackageRequester (
return Object.assign(requestPackage, {
fetchPackageToStore,
getFilesIndexFilePath: getFilesIndexFilePath.bind(null, {
getFilePathInCafs,
getIndexFilePathInCafs,
storeDir: opts.storeDir,
virtualStoreDirMaxLength: opts.virtualStoreDirMaxLength,
}),
@@ -309,7 +308,7 @@ interface FetchLock {
function getFilesIndexFilePath (
ctx: {
getFilePathInCafs: (integrity: string, fileType: FileType) => string
getIndexFilePathInCafs: (integrity: string) => string
storeDir: string
virtualStoreDirMaxLength: number
},
@@ -318,7 +317,7 @@ function getFilesIndexFilePath (
const targetRelative = depPathToFilename(opts.pkg.id, ctx.virtualStoreDirMaxLength)
const target = path.join(ctx.storeDir, targetRelative)
const filesIndexFile = (opts.pkg.resolution as TarballResolution).integrity
? ctx.getFilePathInCafs((opts.pkg.resolution as TarballResolution).integrity!, 'index')
? ctx.getIndexFilePathInCafs((opts.pkg.resolution as TarballResolution).integrity!)
: path.join(target, opts.ignoreScripts ? 'integrity-not-built.json' : 'integrity.json')
return { filesIndexFile, target }
}
@@ -335,7 +334,7 @@ function fetchToStore (
opts: FetchOptions
) => Promise<FetchResult>
fetchingLocker: Map<string, FetchLock>
getFilePathInCafs: (integrity: string, fileType: FileType) => string
getIndexFilePathInCafs: (integrity: string) => string
getFilePathByModeInCafs: (integrity: string, mode: number) => string
requestsQueue: {
add: <T>(fn: () => Promise<T>, opts: { priority: number }) => Promise<T>

View File

@@ -6,7 +6,7 @@ import { prepare, prepareEmpty, preparePackages } from '@pnpm/prepare'
import { readPackageJsonFromDir } from '@pnpm/read-package-json'
import { readProjectManifest } from '@pnpm/read-project-manifest'
import { getIntegrity } from '@pnpm/registry-mock'
import { getFilePathInCafs } from '@pnpm/store.cafs'
import { getIndexFilePathInCafs } from '@pnpm/store.cafs'
import { writeProjectManifest } from '@pnpm/write-project-manifest'
import dirIsCaseSensitive from 'dir-is-case-sensitive'
import { sync as readYamlFile } from 'read-yaml-file'
@@ -519,7 +519,7 @@ test('installation fails when the stored package name and version do not match t
await execPnpm(['add', '@pnpm.e2e/dep-of-pkg-with-1-dep@100.1.0', ...settings])
const cafsDir = path.join(storeDir, 'v3/files')
const cacheIntegrityPath = getFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/dep-of-pkg-with-1-dep', '100.1.0'), 'index')
const cacheIntegrityPath = getIndexFilePathInCafs(cafsDir, getIntegrity('@pnpm.e2e/dep-of-pkg-with-1-dep', '100.1.0'))
const cacheIntegrity = loadJsonFile.sync<any>(cacheIntegrityPath) // eslint-disable-line @typescript-eslint/no-explicit-any
cacheIntegrity.name = 'foo'
writeJsonFile.sync(cacheIntegrityPath, {

View File

@@ -7,7 +7,7 @@ import pLimit from 'p-limit'
import { type PackageManifest, type Registries } from '@pnpm/types'
import {
getFilePathByModeInCafs,
getFilePathInCafs,
getIndexFilePathInCafs,
type PackageFileInfo,
type PackageFilesIndex,
} from '@pnpm/store.cafs'
@@ -258,10 +258,9 @@ export async function readPackageIndexFile (
let pkgIndexFilePath
if (isPackageWithIntegrity) {
// Retrieve all the index file of all files included in the package
pkgIndexFilePath = getFilePathInCafs(
pkgIndexFilePath = getIndexFilePathInCafs(
opts.cafsDir,
packageResolution.integrity as string,
'index'
packageResolution.integrity as string
)
} else if (!packageResolution.type && packageResolution.tarball) {
const packageDirInStore = depPathToFilename(parse(id).nonSemverVersion ?? id, opts.virtualStoreDirMaxLength)

View File

@@ -66,7 +66,7 @@ export interface Cafs {
cafsDir: string
addFilesFromDir: (dir: string) => AddToStoreResult
addFilesFromTarball: (buffer: Buffer) => AddToStoreResult
getFilePathInCafs: (integrity: string | IntegrityLike, fileType: FileType) => string
getIndexFilePathInCafs: (integrity: string | IntegrityLike, fileType: FileType) => string
getFilePathByModeInCafs: (integrity: string | IntegrityLike, mode: number) => string
importPackage: ImportPackageFunction
tempDir: () => Promise<string>

View File

@@ -14,12 +14,11 @@ export function getFilePathByModeInCafs (
return path.join(cafsDir, contentPathFromIntegrity(integrity, fileType))
}
export function getFilePathInCafs (
export function getIndexFilePathInCafs (
cafsDir: string,
integrity: string | IntegrityLike,
fileType: FileType
integrity: string | IntegrityLike
): string {
return path.join(cafsDir, contentPathFromIntegrity(integrity, fileType))
return path.join(cafsDir, contentPathFromIntegrity(integrity, 'index'))
}
function contentPathFromIntegrity (

View File

@@ -10,7 +10,7 @@ import {
} from './checkPkgFilesIntegrity'
import { readManifestFromStore } from './readManifestFromStore'
import {
getFilePathInCafs,
getIndexFilePathInCafs,
contentPathFromHex,
type FileType,
getFilePathByModeInCafs,
@@ -25,7 +25,7 @@ export {
readManifestFromStore,
type FileType,
getFilePathByModeInCafs,
getFilePathInCafs,
getIndexFilePathInCafs,
type PackageFileInfo,
type PackageFilesIndex,
type SideEffects,
@@ -44,7 +44,7 @@ export interface CreateCafsOpts {
export interface CafsFunctions {
addFilesFromDir: (dirname: string, opts?: { files?: string[], readManifest?: boolean }) => AddToStoreResult
addFilesFromTarball: (tarballBuffer: Buffer, readManifest?: boolean) => AddToStoreResult
getFilePathInCafs: (integrity: string | ssri.IntegrityLike, fileType: FileType) => string
getIndexFilePathInCafs: (integrity: string | ssri.IntegrityLike, fileType: FileType) => string
getFilePathByModeInCafs: (integrity: string | ssri.IntegrityLike, mode: number) => string
}
@@ -54,7 +54,7 @@ export function createCafs (cafsDir: string, { ignoreFile, cafsLocker }: CreateC
return {
addFilesFromDir: addFilesFromDir.bind(null, addBuffer),
addFilesFromTarball: addFilesFromTarball.bind(null, addBuffer, ignoreFile ?? null),
getFilePathInCafs: getFilePathInCafs.bind(null, cafsDir),
getIndexFilePathInCafs: getIndexFilePathInCafs.bind(null, cafsDir),
getFilePathByModeInCafs: getFilePathByModeInCafs.bind(null, cafsDir),
}
}

View File

@@ -5,7 +5,7 @@ import tempy from 'tempy'
import {
createCafs,
checkPkgFilesIntegrity,
getFilePathInCafs,
getFilePathByModeInCafs,
} from '../src'
describe('cafs', () => {
@@ -31,7 +31,7 @@ describe('cafs', () => {
let addFilesResult = addFiles()
// Modifying the file in the store
const filePath = getFilePathInCafs(storeDir, addFilesResult.filesIndex['foo.txt'].integrity, 'nonexec')
const filePath = getFilePathByModeInCafs(storeDir, addFilesResult.filesIndex['foo.txt'].integrity, 420)
fs.appendFileSync(filePath, 'bar')
addFilesResult = addFiles()

View File

@@ -6,7 +6,7 @@ import { type TarballResolution } from '@pnpm/lockfile.types'
import { PnpmError } from '@pnpm/error'
import { getStorePath } from '@pnpm/store-path'
import { getFilePathInCafs, type PackageFilesIndex } from '@pnpm/store.cafs'
import { getIndexFilePathInCafs, type PackageFilesIndex } from '@pnpm/store.cafs'
import { pickRegistryForPackage } from '@pnpm/pick-registry-for-package'
import { parseWantedDependency } from '@pnpm/parse-wanted-dependency'
import sortKeys from 'sort-keys'
@@ -83,10 +83,9 @@ export async function handler (opts: CatIndexCommandOptions, params: string[]):
}
)
const filesIndexFile = getFilePathInCafs(
const filesIndexFile = getIndexFilePathInCafs(
cafsDir,
(pkgSnapshot.resolution as TarballResolution).integrity!.toString(),
'index'
(pkgSnapshot.resolution as TarballResolution).integrity!.toString()
)
try {
const pkgFilesIndex = await loadJsonFile<PackageFilesIndex>(filesIndexFile)

View File

@@ -1,5 +1,5 @@
import path from 'path'
import { getFilePathInCafs, type PackageFilesIndex } from '@pnpm/store.cafs'
import { getIndexFilePathInCafs, type PackageFilesIndex } from '@pnpm/store.cafs'
import { getContextForSingleImporter } from '@pnpm/get-context'
import {
nameVerFromPkgSnapshot,
@@ -51,7 +51,7 @@ export async function storeStatus (maybeOpts: StoreStatusOptions): Promise<strin
const cafsDir = path.join(storeDir, 'files')
const modified = await pFilter(pkgs, async ({ id, integrity, depPath, name }) => {
const pkgIndexFilePath = integrity
? getFilePathInCafs(cafsDir, integrity, 'index')
? getIndexFilePathInCafs(cafsDir, integrity)
: path.join(storeDir, dp.depPathToFilename(id, maybeOpts.virtualStoreDirMaxLength), 'integrity.json')
const { files } = await loadJsonFile<PackageFilesIndex>(pkgIndexFilePath)
return (await dint.check(path.join(virtualStoreDir, dp.depPathToFilename(depPath, maybeOpts.virtualStoreDirMaxLength), 'node_modules', name), files)) === false