All files create-hash.ts

100% Statements 15/15
88.88% Branches 8/9
100% Functions 4/4
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2838x         1x 9x 1x   8x 8x   9x 1x   8x 8x     7x 1x   6x     8x    
import {Bytes, Checksum256} from '@wharfkit/antelope'
 
// Aliased over the `create-hash` package when scatter-ts is bundled: its one sha256 call site
// otherwise drags in cipher-base and readable-stream, which require the `stream` and `events`
// Node builtins and break bundlers targeting runtimes without them.
export default function createHash(algorithm: string) {
    if (algorithm !== 'sha256') {
        throw new Error(`create-hash shim: unsupported algorithm '${algorithm}'`)
    }
    let buffer = ''
    const hash = {
        update(data: string) {
            if (typeof data !== 'string') {
                throw new Error('create-hash shim: only string input is supported')
            }
            buffer += data
            return hash
        },
        digest(encoding: string) {
            if (encoding !== 'hex') {
                throw new Error(`create-hash shim: unsupported encoding '${encoding}'`)
            }
            return Checksum256.hash(Bytes.fromString(buffer, 'utf8')).hexString
        },
    }
    return hash
}