-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6704ee8
commit dc83813
Showing
3 changed files
with
35 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { describe, it } from 'node:test' | ||
import assert from 'node:assert/strict' | ||
import path from 'node:path' | ||
import { hashFile } from './hashFile.js' | ||
import { fileURLToPath } from 'node:url' | ||
const __dirname = fileURLToPath(new URL('.', import.meta.url)) | ||
|
||
void describe('hashFile', () => { | ||
void it('should calculate correct MD5 hash value for a file', async () => { | ||
const filePath = path.join(__dirname, 'test-folder', 'test.txt') | ||
const expectedMd5 = '35fd70b6138a64d64c376a6549d6bf57' | ||
const actualMd5 = await hashFile(filePath) | ||
assert.equal(actualMd5, expectedMd5) | ||
}) | ||
}) |
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,18 @@ | ||
import { createHash } from 'crypto' | ||
import { createReadStream } from 'fs' | ||
|
||
export const hashFile = async (file: string): Promise<string> => { | ||
const hash = createHash('md5') | ||
const content = createReadStream(file) | ||
|
||
return new Promise((resolve, reject) => { | ||
content.pipe(hash) | ||
content | ||
.on('close', () => { | ||
return resolve(hash.digest('hex')) | ||
}) | ||
.on('error', (error) => { | ||
return reject(error) | ||
}) | ||
}) | ||
} |
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