Skip to content

Commit 3dc99d5

Browse files
Added OpenSSL capability, updated readme.md and removed only dependency
Added OpenSSL mask hash methods. Updated README.md reflecting additional mask hash methods. Removed dependency on 'md5-file' plugin.
1 parent 9258218 commit 3dc99d5

File tree

5 files changed

+94
-33
lines changed

5 files changed

+94
-33
lines changed

CHANGELOG.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
9+
## [1.1.0] - 2019-03-16
10+
11+
### Added
12+
- Added OpenSSL mask hash methods.
13+
14+
### Changed
15+
- Updated README.md reflecting additional mask hash methods.
16+
17+
### Security
18+
- Removed dependency on 'md5-file' plugin.
19+
20+
## [1.0.2] - 2019-03-15
21+
22+
### Added
23+
- Added ability to replace asset name in template(s) even when asset name is out of sync.
24+
25+
## [1.0.1] - 2019-03-14
26+
27+
### Fixed
28+
- Incorrect counter reference in loop.
29+
30+
### Changed
31+
- Updated README.md reflecting correct usage.
32+
33+
## [1.0.0] - 2019-03-13
34+
35+
### Added
36+
- Initial commit.

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ Then look no further...!
66

77
This grunt plugin inserts a **cache avoiding** string into your asset filename, then looks for and updates any reference to it within your template file(s).
88

9-
Find this npm plugin at [https://www.npmjs.com/package/grunt-cache-killer](https://www.npmjs.com/package/grunt-cache-killer).
9+
Find this plugin at:
10+
- [https://www.npmjs.com/package/grunt-cache-killer](https://www.npmjs.com/package/grunt-cache-killer).
11+
- [https://github.com/midnight-coding/grunt-cache-killer](https://github.com/midnight-coding/grunt-cache-killer).
1012

1113
## Getting Started
1214

@@ -57,7 +59,16 @@ grunt.initConfig({
5759
- If a **cacherKiller** mask function is used, then the string generated from that internal function is inserted. CacheKiller's mask functions include:
5860
- `{timestamp}` eg: `1551278199614`
5961
- `{datetimestamp}` eg: `20190228123639`
60-
- `{md5}` eg: `70a1d7fe6502fa887f5b810d9063da07`
62+
- All [OpenSSL](https://www.openssl.org/) algorithms available on your system. Some common algorithims include:
63+
- `{md4}` eg: `1f73e014cf3341a8b0715b27c031b188`
64+
- `{md5}` eg: `70a1d7fe6502fa887f5b810d9063da07`
65+
- `{sha1}` eg: `a20a181e3C2a813ae08c22fb9d61133c315517bb`
66+
- `{sha224}` eg: `d157aefcf36cdc966737aa0dc4ea85d720652185550c248de9d018f9`
67+
- `{sha256}` eg: `8736ba042ee82bc70676c964b6f7b05e063e1957c95Cb80e4f15f8b01e69c9ad`
68+
69+
For a full list of available algorithms, at the command prompt type `openssl list -digest-algorithms`.<br>
70+
For older versions of OpenSSL, at the command prompt type `openssl list-message-digest-algorithms`.<br>
71+
6172
- If a **string** is used, then that string is inserted. eg: `mask: 'my-string'`
6273

6374
- `length` (number) - A number value that is used to set the length of the mask. The default value is `-1`.
@@ -184,8 +195,6 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.
184195

185196
## Release History
186197

187-
| Date | Version | Comments |
188-
| :--------: | :-----: | :----------------------------------------------------------------------------------------------------------------|
189-
| 01-03-2019 | 1.0.0 | <ul><li>Initial commit.</li></ul> |
190-
| 14-03-2019 | 1.0.1 | <ul><li>Fixed incorrect counter reference in loop.</li><li>Updated README.md reflecting correct usage.</li></ul> |
191-
| 15-03-2019 | 1.0.2 | <ul><li>Added ability to replace asset name in template(s) even when asset name is out of sync.</li></ul> |
198+
Find more on the release history at:
199+
200+
[https://github.com/midnight-coding/grunt-cache-killer/blob/master/CHANGELOG.md](https://github.com/midnight-coding/grunt-cache-killer/blob/master/CHANGELOG.md).

package-lock.json

Lines changed: 1 addition & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "grunt-cache-killer",
33
"description": "Kill your asset cache file problems by updating their filenames and any references to them.",
4-
"version": "1.0.2",
4+
"version": "1.1.0",
55
"homepage": "https://github.com/midnight-coding/grunt-cache-killer",
66
"author": {
77
"name": "Matthew Rath",
@@ -28,9 +28,6 @@
2828
"grunt-contrib-watch": "^1.1.0",
2929
"load-grunt-tasks": "^4.0.0"
3030
},
31-
"dependencies": {
32-
"md5-file": "^4.0.0"
33-
},
3431
"keywords": [
3532
"gruntplugin",
3633
"css",

tasks/cacheKiller.js

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'use strict';
1010

1111
// Load the required dependencies.
12-
var md5File = require('md5-file');
12+
var crypto = require('crypto');
1313
var fileSystem = require('fs');
1414

1515
// Grunt task module.
@@ -63,34 +63,58 @@ module.exports = function (grunt) {
6363
}
6464

6565
/**
66-
* Get md5 of file.
66+
* Get hash of file.
6767
*
6868
* @param $file {string}
69+
* @param $algorithm {string}
6970
* @returns {string}
7071
*/
71-
function getMd5($file) {
72-
return md5File.sync($file);
72+
function getHash($file, $algorithm) {
73+
var $length = 8192;
74+
var $fileDescriptor = fileSystem.openSync($file, 'r');
75+
var $hash = crypto.createHash($algorithm);
76+
var $buffer = Buffer.alloc($length);
77+
78+
try {
79+
var $bytesRead;
80+
81+
do {
82+
$bytesRead = fileSystem.readSync($fileDescriptor, $buffer, 0, $length, 0);
83+
84+
$hash.update($buffer.slice(0, $bytesRead));
85+
} while ($bytesRead === $length);
86+
} finally {
87+
fileSystem.closeSync($fileDescriptor);
88+
}
89+
90+
return $hash.digest('hex');
7391
}
7492

7593
/**
7694
* Get mask value.
7795
*
7896
* @param $maskType {string}
7997
* @param $file {string}
80-
* @param $value {string}
8198
* @returns {string}
8299
*/
83-
function getMaskValue($maskType, $file, $value) {
84-
switch ($maskType) {
85-
case '{timestamp}':
86-
return getTimeStamp().toString();
87-
case '{datetimestamp}':
88-
return getDateTimeStamp();
89-
case '{md5}':
90-
return getMd5($file);
91-
default:
92-
return $value;
100+
function getMaskValue($maskType, $file) {
101+
// If using a timestamp.
102+
if ($maskType === '{timetsmap}') {
103+
return getTimeStamp.toString();
104+
}
105+
106+
// If using a datetime stamp.
107+
if($maskType === '{datetimestamp}') {
108+
return getDateTimeStamp();
93109
}
110+
111+
// If using an OpenSSL digest algorithm.
112+
if (/{.*}/.test($maskType)) {
113+
return getHash($file, $maskType.replace(new RegExp(/[{}]/, 'g'), ""));
114+
}
115+
116+
// Just a string.
117+
return $maskType;
94118
}
95119

96120
/**
@@ -183,7 +207,7 @@ module.exports = function (grunt) {
183207

184208
$tasks[i].asset.mask.prepend = $options.prepend;
185209
$tasks[i].asset.mask.value = {};
186-
$tasks[i].asset.mask.value.raw = getMaskValue($options.mask, $tasks[i].asset.rename.from.full, $options.mask);
210+
$tasks[i].asset.mask.value.raw = getMaskValue($options.mask, $tasks[i].asset.rename.from.full);
187211
$tasks[i].asset.mask.value.computed = trimByLength($tasks[i].asset.mask.value.raw, $options.length);
188212
$tasks[i].asset.mask.apend = $options.append;
189213

0 commit comments

Comments
 (0)