-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(http): compute checksum after download (#1196)
- Loading branch information
Showing
7 changed files
with
130 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import fs from 'node:fs/promises'; | ||
import { env } from 'node:process'; | ||
import { describe, expect, test } from 'vitest'; | ||
import { hash, hashFile } from './hash'; | ||
|
||
describe('hash', () => { | ||
test('should hash data with sha256', () => { | ||
expect(hash('https://example.com/test.txt', 'sha256')).toBe( | ||
'd1dc63218c42abba594fff6450457dc8c4bfdd7c22acf835a50ca0e5d2693020' | ||
); | ||
}); | ||
|
||
test('should hash file with sha256', async () => { | ||
const file = `${env.CONTAINERBASE_CACHE_DIR}/test.txt`; | ||
await fs.writeFile(file, 'https://example.com/test.txt'); | ||
expect(await hashFile(file, 'sha256')).toBe( | ||
'd1dc63218c42abba594fff6450457dc8c4bfdd7c22acf835a50ca0e5d2693020' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import crypto from 'node:crypto'; | ||
import fs from 'node:fs/promises'; | ||
import type { LiteralUnion } from 'type-fest'; | ||
|
||
export type AlgorithmName = LiteralUnion< | ||
'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512', | ||
string | ||
>; | ||
|
||
export function hash(data: string | Buffer, algorithm: AlgorithmName): string { | ||
const hash = crypto.createHash(algorithm); | ||
hash.update(data); | ||
return hash.digest('hex'); | ||
} | ||
|
||
export async function hashFile( | ||
file: string, | ||
algorithm: AlgorithmName | ||
): Promise<string> { | ||
const data = await fs.readFile(file); | ||
const hash = crypto.createHash(algorithm); | ||
hash.update(data); | ||
return hash.digest('hex'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { sep } from 'node:path'; | ||
import { env } from 'node:process'; | ||
|
||
export function cacheFile(path: string): string { | ||
return `${env.CONTAINERBASE_CACHE_DIR}/${path}`.replace(/\/+/g, sep); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters