@@ -5,8 +5,11 @@ var crypto = require('crypto');
55var path = require ( 'path' ) ;
66var through = require ( 'through2' ) ;
77var cheerio = require ( 'cheerio' ) ;
8+ var semver = require ( 'semver' ) ;
89var PluginError = require ( 'gulp-util' ) . PluginError ;
910
11+ var NODE_VERSION_WITH_NEW_BUFFER_API = '4.5.0' ;
12+ var useDeprecatedBufferApi = semver . lt ( process . version , NODE_VERSION_WITH_NEW_BUFFER_API ) ;
1013var PLUGIN_NAME = 'gulp-sri-hash' ;
1114var DEFAULT_ALGO = 'sha384' ;
1215var DEFAULT_SELECTOR = 'link[href][rel=stylesheet]:not([integrity]), script[src]:not([integrity])' ;
@@ -16,6 +19,14 @@ var cache;
1619module . exports = gulpSriHashPlugin ;
1720module . exports . PLUGIN_NAME = PLUGIN_NAME ;
1821
22+ function makeBufferFromString ( string ) {
23+ if ( useDeprecatedBufferApi ) {
24+ return new Buffer ( string ) ;
25+ }
26+
27+ return Buffer . from ( string ) ;
28+ }
29+
1930function normalizePath ( node , config ) {
2031 var src = node . name == 'script' ? node . attribs . src : node . attribs . href ;
2132
@@ -51,9 +62,8 @@ function resolveAbsolutePath(file, localPath) {
5162
5263function calculateSri ( fullPath , algorithm ) {
5364 var file = fs . readFileSync ( fullPath ) ;
54- var digest = crypto . createHash ( algorithm ) . update ( file ) . digest ( ) ;
5565
56- return Buffer . from ( digest ) . toString ( 'base64' ) ;
66+ return crypto . createHash ( algorithm ) . update ( file ) . digest ( 'base64' ) ;
5767}
5868
5969function getFileHash ( fullPath , algorithm ) {
@@ -73,7 +83,7 @@ function updateDOM(file, config) {
7383
7484 if ( $candidates . length > 0 ) {
7585 $candidates . each ( addIntegrityAttribute ) ;
76- file . contents = Buffer . from ( $ . html ( ) ) ;
86+ file . contents = makeBufferFromString ( $ . html ( ) ) ;
7787 }
7888
7989 return file ;
0 commit comments