• Stars
    star
    195
  • Rank 192,604 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Take contents of a file (or stdin), and output as new file with a hash in the name

HashMark

Build Status

Sponsor

HashMark is a small utility which takes a file (or sdtin) as input, and writes the contents of the input to a file named with a hash digest of the file. This is useful for cache busting sticky caches - files can have far future expires headers and when the code changes, a new filename is created.

Examples:

Shell

cat file.js | ./bin/hashmark 'file.{hash}.js' # Writes to test.3eae1599bb7f187b86d6427942d172ba8dd7ee5962aab03e0839ad9d59c37eb0.js
> Computed hash: 3eae1599bb7f187b86d6427942d172ba8dd7ee5962aab03e0839ad9d59c37eb0
>
cat file.js | ./bin/hashmark -l 8 'file.{hash}.js' # Writes to test.3eae1599.js
> Computed hash: 3eae1599
>
cat file.js | ./bin/hashmark -l 4 -d md5 'dist/{hash}.js' # Writes to dist/cbd8.js
> Computed hash: cbd8
>

It is useful to use globs โ€” meaning you can read in many files and it will output hashed versions of each:

./bin/hashmark path/to/*.js 'dist/{name}.{hash}.js'
./bin/hashmark path/to/{filea,fileb,filec}.js 'dist/{name}.{hash}.js'
./bin/hashmark **/*.js 'dist/{dir}/{name}.{hash}.js'
./bin/hashmark **/*.{js,css} 'dist/{dir}/{name}.{hash}{ext}'
./bin/hashmark **/*.{js,css} 'dist/{dir}/{hash}-{base}'

The above even works in shells that do not support globs (such as cmd.exe on Windows), because hashmark has support for expanding globs itself. Note that if your shell supports the * glob but not the ** glob (such as bash before version 4), the shell will interpret ** as two * in a row, which means that hashmark never receives the ** and therefore cannot expand it. In that case you need to quote your argument. Therefore, if you want to write cross-platform scripts it is best to always quote the args:

Available pattern keys includes all the keys returned by path.parse(filename) and hash (ie: hash, root, dir, name, base, ext).

./bin/hashmark 'dir/**/*.{js,css}' 'test/*.js' 'dist/{dir}/{name}.{hash}{ext}'

The hashmark command will output some JSON to stdout with a map of filenames and their new hashes, meaning you can pipe the hash to other programs. To make hashmark completely silent - simply pass the --silent or -s flag.

./bin/hashmark -l 4 file.js 'dist/{hash}.js' --silent

You can also output the JSON map to a file, by passing the --asset-map or -m flag. It will still be logged to stdout unless you pass --silent

./bin/hashmark -l 4 file.js 'dist/{hash}.js' --asset-map assets.json

You can specify from which directory to work from with --cwd or -c. Note: asset-map will be relative to this directory.

mkdir dist/subdir
echo 'abracadabra' > dist/subdir/file.js
./bin/hashmark --cwd dist -d md5 -l 8 '**/*.js' '{dir}/{name}-{hash}{ext}'
> {"subdir/file.js":"subdir/file-97640ef5.js"}

Integrations

replaceinfiles

Now that your assets have been renamed, you might want to update references to them in some other files (ex:background image reference in your css files). That's exactly what replaceinfiles is made for. Just pipe it to hashmark. It just workโ„ข. Full example.

Programmatically

The hashmark function can be used programmatically. You can pass it a String, Buffer or Stream as the first argument, an options object as the second argument, and a callback as the third.

The callback receives an error as the first argument (or null) and an object which maps each given file to the newly hashed file name.

var hashmark = require('hashmark');
var file = fs.createReadStream('file.js');

hashmark(file, { length: 8, digest: 'md5', 'pattern': '{hash}'}, function (err, map) {
    console.log(map);
});

The function also returns an event emitter which emits error, file and end events. File events get fired when an individual file has been hashed, and the end event is fired when all have been done. file is given two arguments - the files old name, and the new calculated filename (given the template string), and the end event is given an object mapping of all files.

var hashmark = require('hashmark');
var file = fs.createReadStream('file.js');

hashmark(file, { length: 8, digest: 'md5', pattern: 'hash'})
    .on('file', function (oldFileName, newFileName) {
        console.log('File hashed!', oldFileName, newFileName);
    })
    .on('end', function (jsonMap) {
        console.log('~FIN');
    })

Files can be a single Stream, or filename String, or an Array of Streams and/or filename Strings.

var hashmark = require('hashmark');
var file = fs.createReadStream('file.js');

hashmark([file, 'file2.js'], { length: 8, digest: 'md5', file: 'file.#.js'}, function (err, hash) {
    console.log('File written to file.' + hash + '.js');
    console.log(hash);
});

Contributing

This is such a small utility - there's very little point in contributing. If it doesn't do something that you really think it should, feel free to raise an issue - just be aware I might say no. If you can make it faster/better/stronger without changing the API/functionality then send a PR!

More Repositories

1

jwerty

โŒจ Awesome handling of keyboard events
JavaScript
1,213
star
2

npm-scripts-example

An example of how to use NPM scripts over Grunt/Gulp & Friends. http://blog.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool
JavaScript
831
star
3

sort-package-json

Sort an Object or package.json based on the well-known package.json keys
JavaScript
712
star
4

tempus

Tempus - Time for a new Date()
JavaScript
94
star
5

R.js

Internationalisation Library for Javascript
JavaScript
75
star
6

ecmascript-operator-overloading-proposal

A proposal for operator overloading in ECMAScript
64
star
7

proposal-object-freeze-seal-syntax

A JavaScript TC39 proposal for Object.freeze & Object.seal syntax
59
star
8

load-testing-node

Various setups for Load Balancing multiple Node.js processes on a single VM
Ruby
57
star
9

invokers-polyfill

JavaScript
44
star
10

hbs-cli

A CLI for handlebars
JavaScript
43
star
11

csslex

A very small and very fast spec compliant css lexer
JavaScript
39
star
12

LinkyPass

LinkyPass Google Chrome Extension
JavaScript
36
star
13

eslint-plugin-escompat

Report errors for code which wont work in browsers without transpiling
JavaScript
29
star
14

proxy-fluent-api

Make Fluent APIs using ES6 Proxies
JavaScript
28
star
15

sort-object-keys

Sort an object's keys, including an optional key list
JavaScript
28
star
16

hdx

Refreshing CSS
Rust
26
star
17

gnome-shell-duckduckgo-search-provider

This is a Search Provider for Gnome Shell, and it provides DuckDuckGo Search functionality.
TypeScript
25
star
18

eslint-config-strict

ESLint sharable config for strict linting
JavaScript
25
star
19

postcss-hash

PostCSS plugin to replace output file names with HASH algorithms (md5, sha256, sha512, etc) and string length of your choice - for cache busting
JavaScript
24
star
20

reading-list

These are the books Im going to read
22
star
21

mini-observable

A mini implementation of TC39 observables, plus some utils!
TypeScript
22
star
22

WebCumberNode

JavaScript
20
star
23

proxy-method-missing

JavaScript
19
star
24

rollup-plugin-brotli

JavaScript
15
star
25

nibstate

TypeScript
14
star
26

getbrightness

JavaScript
14
star
27

fdgt

JavaScript
14
star
28

travis-ci-node-and-browser-qunit

JavaScript
13
star
29

alfred-docker-wizard

Control Docker machines & containers with Alfred
Shell
11
star
30

dotfiles

โšซ These are my dotfiles. There are many like it but these are mine.
Lua
10
star
31

deno-protod

TypeScript
9
star
32

proxy-object-observe

An Object.observe inspired util, based on ES6 proxies
JavaScript
7
star
33

custom-attributes

TypeScript
7
star
34

proposal-array-compact

A proposal for adding a `compact()` method to Arrays
HTML
6
star
35

Avalanche

A web-based GUI for torrent applications (such as rTorrent)
JavaScript
6
star
36

rollup-plugin-inline-invariant

JavaScript
6
star
37

GitAttributesTest

Showing how gitattributes can help you maintain a config file while keeping your config secrets from commits
Shell
5
star
38

deno-varint

Encode/Decode Number/BigInts into an array of Varint bytes
TypeScript
5
star
39

stylelint-config-strict

Stylelint sharable config for strict linting
JavaScript
5
star
40

mqtt-bridges

A platform for bridging APIs into mqtt messages
JavaScript
4
star
41

sourcemap-visualiser

JavaScript
4
star
42

cv

๐Ÿ“„ My curriculum vitae
HTML
3
star
43

wsb

JavaScript
3
star
44

sinomocha

Sinon.JS integration for the Mocha test harness
JavaScript
3
star
45

k-router

A MVC/MVT style router for Connect/Express apps
JavaScript
3
star
46

hubot-seriouslyguys

A hubot script for showing a 'Srsly Guise' gif when someone says 'Seriously Guys' or 'Srsly Guise'
CoffeeScript
3
star
47

probot-flood

A bot that automatically locks issues if they get flooded with comments
JavaScript
3
star
48

proposal-array-filtermap

A proposal for adding a `filterMap()` method to Arrays
HTML
3
star
49

deno-protoc-parser

Parse Google Protocol Buffer DSL into an AST, which can be converted into JSON or back into the Protocol Buffer DSL.
TypeScript
2
star
50

NobleClass

A JavaScript ES5 compliant Class implementation for Browser and NodeJS
JavaScript
2
star
51

zero-env

JavaScript
2
star
52

function-guard

JavaScript
2
star
53

tempus-js.com

Website for Tempus (https://github.com/tempusjs/tempus)
HTML
2
star
54

grunt-hooks

SCM Hook management for Grunt
JavaScript
2
star
55

parse-packagejson-name

Parse an npm package name and returns some mildly interesting details about it
JavaScript
2
star
56

hubot-soon

A hubot script for showing a 'SOON' image when someone says 'soon'
CoffeeScript
2
star
57

deno-scanner

Take a Deno.Reader and perform Lexical Analysis/Tokenization on it, returning a stream of tokens.
TypeScript
2
star
58

qunit-logging

JavaScript
1
star
59

proxy-hide-properties

Hide a property from all conceivable ways of being detected
JavaScript
1
star
60

radon-css

Lightweight alternative to Radium for React
JavaScript
1
star
61

eslint-config-strict-react

eslint-config-strict for React
JavaScript
1
star
62

tar-grab-unpack

Node.js utlity to grab tar files and unpack them
JavaScript
1
star
63

keithamus

1
star
64

evening-with-tc39

JavaScript
1
star
65

recipes

HTML
1
star
66

proposal-multi-module-files

A JavaScript TC39 Proposal for expressing multiple ES6 modules in a single file
JavaScript
1
star
67

vimstart

specify custom VIM start screen text using custom shell scripts
Vim Script
1
star