-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For binary files, we can't cache the content as a string (utf8), we need to use a binary buffer. All the preprocessors are currently implemented to deal with strings, as that makes sense for text files. If we changed preprocessors to deal with buffers then each preprocessor would have to convert the buffer to a string, do the string manipulation and then convert it back to a buffer. I don't think that's worthy as there are no binary preprocessors. This change disables preprocessing of binary files to avoid weird errors. It shows warning. If there is a reasonable use case for preprocessing binary files we can figure out something. Closes #864 Closes #885
- Loading branch information
Showing
2 changed files
with
45 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
var path = require('path'); | ||
var fs = require('graceful-fs'); | ||
var crypto = require('crypto'); | ||
var mm = require('minimatch'); | ||
|
@@ -10,12 +11,29 @@ var sha1 = function(data) { | |
return hash.digest('hex'); | ||
}; | ||
|
||
var isBinary = Object.create(null); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
[ | ||
'adp', 'au', 'mid', 'mp4a', 'mpga', 'oga', 's3m', 'sil', 'eol', 'dra', 'dts', 'dtshd', 'lvp', | ||
'pya', 'ecelp4800', 'ecelp7470', 'ecelp9600', 'rip', 'weba', 'aac', 'aif', 'caf', 'flac', 'mka', | ||
'm3u', 'wax', 'wma', 'wav', 'xm', 'flac', '3gp', '3g2', 'h261', 'h263', 'h264', 'jpgv', 'jpm', | ||
'mj2', 'mp4', 'mpeg', 'ogv', 'qt', 'uvh', 'uvm', 'uvp', 'uvs', 'dvb', 'fvt', 'mxu', 'pyv', 'uvu', | ||
'viv', 'webm', 'f4v', 'fli', 'flv', 'm4v', 'mkv', 'mng', 'asf', 'vob', 'wm', 'wmv', 'wmx', 'wvx', | ||
'movie', 'smv', 'ts', 'bmp', 'cgm', 'g3', 'gif', 'ief', 'jpg', 'jpeg', 'ktx', 'png', 'btif', | ||
'sgi', 'svg', 'tiff', 'psd', 'uvi', 'sub', 'djvu', 'dwg', 'dxf', 'fbs', 'fpx', 'fst', 'mmr', | ||
'rlc', 'mdi', 'wdp', 'npx', 'wbmp', 'xif', 'webp', '3ds', 'ras', 'cmx', 'fh', 'ico', 'pcx', 'pic', | ||
'pnm', 'pbm', 'pgm', 'ppm', 'rgb', 'tga', 'xbm', 'xpm', 'xwd', 'zip', 'rar', 'tar', 'bz2', 'eot', | ||
'ttf', 'woff' | ||
].forEach(function(extension) { | ||
isBinary['.' + extension] = true; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
vojtajina
Author
Contributor
|
||
}); | ||
|
||
// TODO(vojta): instantiate preprocessors at the start to show warnings immediately | ||
var createPreprocessor = function(config, basePath, injector) { | ||
var patterns = Object.keys(config); | ||
var alreadyDisplayedWarnings = Object.create(null); | ||
|
||
return function(file, done) { | ||
var thisFileIsBinary = isBinary[path.extname(file.originalPath)]; | ||
var preprocessors = []; | ||
var nextPreprocessor = function(error, content) { | ||
// normalize B-C | ||
|
@@ -61,18 +79,18 @@ var createPreprocessor = function(config, basePath, injector) { | |
// TODO(vojta): should we cache this ? | ||
for (var i = 0; i < patterns.length; i++) { | ||
if (mm(file.originalPath, patterns[i])) { | ||
config[patterns[i]].forEach(instantiatePreprocessor); | ||
if (thisFileIsBinary) { | ||
log.warn('Ignoring preprocessing (%s) %s because it is a binary file.', | ||
config[patterns[i]].join(', '), file.originalPath); | ||
} else { | ||
config[patterns[i]].forEach(instantiatePreprocessor); | ||
} | ||
} | ||
} | ||
|
||
// compute SHA of the content | ||
preprocessors.push(function(content, file, done) { | ||
file.sha = sha1(content); | ||
done(content); | ||
}); | ||
|
||
return fs.readFile(file.originalPath, function(err, buffer) { | ||
nextPreprocessor(buffer.toString()); | ||
file.sha = sha1(buffer); | ||
nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString()); | ||
}); | ||
}; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit 8a30cf5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a use case of processing binary files :-p
Temporarily worked around in bbc/peaks.js@e54759e
We basically serve binary data to describe audio waveforms, because they are smaller in size and memory footprint.
Hopefully we have a JSON fallback but now we cannot tests the binary cases.
So in our case, just adding
dat
in that list would suffice. I'll create a PR for that.