oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.68k stars 2.72k forks source link

Hashing files - ERROR: Digest already called #7706

Open KodeurKubik opened 9 months ago

KodeurKubik commented 9 months ago

What version of Bun is running?

1.0.18+36c316a24

What platform is your computer?

Darwin 23.1.0 arm64 arm

What steps can reproduce the bug?

Getting the sha256 of a file with crypto and stream throws an error. Use this code:

const { randomBytes, createHash } = require("node:crypto");
const { createReadStream } = require('fs');
const stream = require('stream/promises');

async computeHash(filepath, hashing = 'sha256') {
    const input = createReadStream(filepath);
    const hash = createHash(hashing);

    await stream.pipeline(input, hash);

    return hash.digest('hex');
}

And use the function computeHash on any file, bun will throw the following error:

10 |         const input = createReadStream(filepath);
11 |         const hash = createHash(hashing);
12 | 
13 |         await stream.pipeline(input, hash);
14 | 
15 |         return hash.digest('hex');
                    ^
error: Digest already called
 code: "ERR_CRYPTO_HASH_FINALIZED"

      at _checkFinalized (node:crypto:1175:137)
      at update (node:crypto:1173:101)
      at /path/to/file/index.js:15:16
      at processTicksAndRejections (:61:77)

What is the expected behavior?

Expected behavior is like using node, the function should return an hexadecimal sha256 string.

What do you see instead?

Well, the error like written in "What steps can reproduce the bug"

Additional information

No response

RobertOliyam commented 6 months ago

+1