Skip to content

Commit

Permalink
feat: export hashFile
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jul 31, 2024
1 parent 6704ee8 commit dc83813
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
15 changes: 15 additions & 0 deletions src/hasFile.spec.ts
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)
})
})
18 changes: 18 additions & 0 deletions src/hashFile.ts
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)
})
})
}
19 changes: 2 additions & 17 deletions src/hashFolder.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
import { createHash } from 'node:crypto'
import { createReadStream, readdirSync, statSync } from 'node:fs'
import { readdirSync, statSync } from 'node:fs'
import { join } from 'node:path'

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)
})
})
}
import { hashFile } from './hashFile.js'

const generateMd5ForFolder = async (
path: string,
Expand Down

0 comments on commit dc83813

Please sign in to comment.