• Stars
    star
    195
  • Rank 199,374 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 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

i-html

HTML
86
star
6

R.js

Internationalisation Library for Javascript
JavaScript
75
star
7

ecmascript-operator-overloading-proposal

A proposal for operator overloading in ECMAScript
64
star
8

proposal-object-freeze-seal-syntax

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

load-testing-node

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

invokers-polyfill

JavaScript
54
star
11

csslex

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

hbs-cli

A CLI for handlebars
JavaScript
43
star
13

LinkyPass

LinkyPass Google Chrome Extension
JavaScript
36
star
14

eslint-plugin-escompat

Report errors for code which wont work in browsers without transpiling
JavaScript
36
star
15

hdx

Refreshing CSS
Rust
35
star
16

proxy-fluent-api

Make Fluent APIs using ES6 Proxies
JavaScript
28
star
17

sort-object-keys

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

gnome-shell-duckduckgo-search-provider

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

eslint-config-strict

ESLint sharable config for strict linting
JavaScript
25
star
20

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
21

mini-observable

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

reading-list

These are the books Im going to read
22
star
23

WebCumberNode

JavaScript
20
star
24

proxy-method-missing

JavaScript
19
star
25

rollup-plugin-brotli

JavaScript
15
star
26

nibstate

TypeScript
14
star
27

getbrightness

JavaScript
14
star
28

travis-ci-node-and-browser-qunit

JavaScript
13
star
29

fdgt

JavaScript
13
star
30

alfred-docker-wizard

Control Docker machines & containers with Alfred
Shell
11
star
31

dotfiles

⚫ These are my dotfiles. There are many like it but these are mine.
Lua
10
star
32

deno-protod

TypeScript
9
star
33

proxy-object-observe

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

custom-attributes

TypeScript
7
star
35

tickrs

Rust
7
star
36

proposal-array-compact

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

Avalanche

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

rollup-plugin-inline-invariant

JavaScript
6
star
39

GitAttributesTest

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

stylelint-config-strict

Stylelint sharable config for strict linting
JavaScript
5
star
41

mqtt-bridges

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

sourcemap-visualiser

JavaScript
4
star
43

deno-varint

Encode/Decode Number/BigInts into an array of Varint bytes
TypeScript
4
star
44

cv

πŸ“„ My curriculum vitae
HTML
3
star
45

probot-flood

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

wsb

JavaScript
3
star
47

sinomocha

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

k-router

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

hubot-seriouslyguys

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

proposal-array-filtermap

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

parse-packagejson-name

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

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
53

NobleClass

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

zero-env

JavaScript
2
star
55

hubot-soon

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

tempus-js.com

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

grunt-hooks

SCM Hook management for Grunt
JavaScript
2
star
58

function-guard

JavaScript
2
star
59

deno-scanner

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

proxy-hide-properties

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

qunit-logging

JavaScript
1
star
62

tar-grab-unpack

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

radon-css

Lightweight alternative to Radium for React
JavaScript
1
star
64

eslint-config-strict-react

eslint-config-strict for React
JavaScript
1
star
65

keithamus

1
star
66

evening-with-tc39

JavaScript
1
star
67

recipes

HTML
1
star
68

proposal-multi-module-files

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

vimstart

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