New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

stream-hasher

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stream-hasher

A through-stream that calculates hash-digests on the fly

latest
Source
npmnpm
Version
1.4.2
Version published
Maintainers
1
Created
Source

stream-hasher

npm version

A transform-stream that emits hash-digests of streams or vinyl-file-streams

Features

Works with vinyl-streams in buffer- and stream-mode, optionally renames files.

Usage

Single Data Stream

import fs from 'fs';
import streamHasher from 'stream-hasher';

const hasher = streamHasher({single: true});
hasher.on('digest', function(digest) {
  console.log('digest=%s', digest)
});

fs.createReadStream('package.json')
  .pipe(hasher)
  .resume();
  // it's a stream2, so pipe it along or dump it, otherwise it will stuck.

Vinyl File Stream

import vinylFs from 'vinyl-fs';
import streamHasher from 'stream-hasher';

const hasher = streamHasher();
hasher.on('digest', function(digest, tag) {
  console.log('digest=%s tag=%s', digest, tag)
});

vinylFs.src(['src/**/*.js'], {buffer: false}) // works with 'buffer: true', too 
  .pipe(hasher)
  .pipe(vinylFs.dest('dist'));

API

const hasher = streamHasher(options);

Creates a new hasher. Recognized options are:

  • algorithm (string, default: 'sha1'): the hash-algorithm to be used. See crypto.createHash for available algorithms.
  • digestEncoding (string, default: 'hex'): how the resulting digest is encoded. See Buffer#toString for available encodings. Use 'buffer' to get a bare buffer.
  • digestLength (number): if supplied, the digest length is limited to this length.
  • single (boolean, default: false): If true, create a hasher that transforms a single data-stream. If false, create a hasher to transform a vinyl-file-stream. In latter case, the following additional options are recognized:
    • tagger (function(file)): a function that generates the tag from the processed vinyl-file. Defaults to a function that returns file.path.
    • optioner (function(file)): a function that generates an object to overwrite options per vinyl-file.
    • rename: (function(basename, digest) or string): a function that takes the original file name (without extension) and the calculated digest and should return a replacement file name. The strings 'postfix' and 'prefix' can be used, too. They expose some standard replacers.
    • renameFile (function (file)): to obtain even finer control of renaming supply a function that takes a vinyl-file and the digest to directly manipulate the file's path.
    • maxSingleSize (number): In the special case of an stream-file to be renamed, the digest must me emitted before the file can be passed forward. Then is value is used to set the highWaterMark for processing that file to prevent deadlocking. Default is 16MB.

Event 'digest'

is emitted for every calculated hash-digest

  • digest: the calculated digest
  • tag: the file's tag
  • newTag: if renaming was specified, this is the file's tag after renaming

Keywords

stream

FAQs

Package last updated on 27 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts