Commit 26dd4cb9 authored by Paul Merlin's avatar Paul Merlin
Browse files

Make file hashing more robust

parent 317ca35d
Loading
Loading
Loading
Loading
+1 −1
Changes for .eslintrc.json: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@
      "@typescript-eslint/prefer-includes": "error",
      "@typescript-eslint/prefer-string-starts-ends-with": "error",
      "@typescript-eslint/promise-function-async": "error",
      "@typescript-eslint/require-array-sort-compare": "error",
      "@typescript-eslint/require-array-sort-compare": ["error", {"ignoreStringArrays":  true}],
      "@typescript-eslint/restrict-plus-operands": "error",
      "semi": "off",
      "@typescript-eslint/semi": ["error", "never"],
+9 −3
Changes for __tests__/crypto-utils.test.ts: 9 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -8,7 +8,9 @@ describe('crypto-utils', () => {
                path.resolve('__tests__/data/basic/gradle')
            )
            expect(hash).toBe(
                '4ebb65b45e6f6796d5ec6ace96e9471cc6573d294c54f99c4920fe5328e75bab'
                process.platform === 'win32'
                    ? '3364336e94e746ce65a31748a6371b7efd7d499e18ad605c74c91cde0edc0a44'
                    : '4ebb65b45e6f6796d5ec6ace96e9471cc6573d294c54f99c4920fe5328e75bab'
            )
        })
        it('a directory with a glob', async () => {
@@ -17,7 +19,9 @@ describe('crypto-utils', () => {
                ['gradle/**']
            )
            expect(hash).toBe(
                '4ebb65b45e6f6796d5ec6ace96e9471cc6573d294c54f99c4920fe5328e75bab'
                process.platform === 'win32'
                    ? '3364336e94e746ce65a31748a6371b7efd7d499e18ad605c74c91cde0edc0a44'
                    : '4ebb65b45e6f6796d5ec6ace96e9471cc6573d294c54f99c4920fe5328e75bab'
            )
        })
        it('a directory with globs', async () => {
@@ -26,7 +30,9 @@ describe('crypto-utils', () => {
                ['**/*.gradle', 'gradle/**']
            )
            expect(hash).toBe(
                '2db1d5291774949ab89e18e9d82ee24748ca0f6cc78de69ea9104357c50ad4a5'
                process.platform === 'win32'
                    ? 'd9b66fded38f79f601ce745d64ed726a8df8c0b242b02bcd2c1d331f54742ad6'
                    : 'aa72a837158799fbadd1c4aff94fcc2b5bb9dc6ad8d12f6337d047d4b0c8f79e'
            )
        })
    })
+1 −1

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1

File changed.

Preview size limit exceeded, changes collapsed.

+9 −5
Changes for src/crypto-utils.ts: 9 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -12,9 +12,10 @@ export async function hashFiles(
    followSymbolicLinks = false
): Promise<string | null> {
    let hasMatch = false
    const result = crypto.createHash('sha256')
    type FileHashes = Record<string, Buffer>
    const hashes: FileHashes = {}
    for await (const globPattern of globs) {
        const globMatch = `${baseDir}/${globPattern}`
        const globMatch = `${baseDir}${path.sep}${globPattern}`
        const globber = await glob.create(globMatch, {followSymbolicLinks})
        for await (const file of globber.globGenerator()) {
            // console.log(file)
@@ -29,12 +30,15 @@ export async function hashFiles(
            const hash = crypto.createHash('sha256')
            const pipeline = util.promisify(stream.pipeline)
            await pipeline(fs.createReadStream(file), hash)
            result.write(hash.digest())
            if (!hasMatch) {
            hashes[path.relative(baseDir, file)] = hash.digest()
            hasMatch = true
        }
    }
    if (!hasMatch) return null
    const result = crypto.createHash('sha256')
    for (const file of Object.keys(hashes).sort()) {
        result.update(hashes[file])
    }
    result.end()
    return hasMatch ? result.digest('hex') : null
    return result.digest('hex')
}