Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web-server): Use isbinaryfile for binary file detection #1816

Merged
merged 1 commit into from
Jan 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 0 additions & 136 deletions lib/binary-extensions.json

This file was deleted.

118 changes: 61 additions & 57 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var path = require('path')
var fs = require('graceful-fs')
var crypto = require('crypto')
var mm = require('minimatch')
var extensions = require('./binary-extensions.json').extensions
var isBinaryFile = require('isbinaryfile')

var log = require('./logger').create('preprocess')

Expand All @@ -12,10 +11,30 @@ var sha1 = function (data) {
return hash.digest('hex')
}

var isBinary = Object.create(null)
extensions.forEach(function (extension) {
isBinary['.' + extension] = true
})
var createNextProcessor = function (preprocessors, file, done) {
return function nextPreprocessor (error, content) {
// normalize B-C
if (arguments.length === 1 && typeof error === 'string') {
content = error
error = null
}

if (error) {
file.content = null
file.contentPath = null
return done(error)
}

if (!preprocessors.length) {
file.contentPath = null
file.content = content
file.sha = sha1(content)
return done()
}

preprocessors.shift()(content, file, nextPreprocessor)
}
}

var createPreprocessor = function (config, basePath, injector) {
var alreadyDisplayedWarnings = {}
Expand Down Expand Up @@ -51,64 +70,49 @@ var createPreprocessor = function (config, basePath, injector) {

return function preprocess (file, done) {
patterns = Object.keys(config)
var thisFileIsBinary = isBinary[path.extname(file.originalPath).toLowerCase()]
var preprocessors = []

var nextPreprocessor = function (error, content) {
// normalize B-C
if (arguments.length === 1 && typeof error === 'string') {
content = error
error = null
}

if (error) {
file.content = null
file.contentPath = null
return done(error)
}

if (!preprocessors.length) {
file.contentPath = null
file.content = content
file.sha = sha1(content)
return done()
return fs.readFile(file.originalPath, function (err, buffer) {
if (err) {
throw err
}

preprocessors.shift()(content, file, nextPreprocessor)
}

for (var i = 0; i < patterns.length; i++) {
if (mm(file.originalPath, patterns[i], {dot: true})) {
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(function (name) {
var p = instances[name]
if (p == null) {
p = instantiatePreprocessor(name)
}
isBinaryFile(buffer, buffer.length, function (err, thisFileIsBinary) {
if (err) {
throw err
}

if (p == null) {
if (!alreadyDisplayedWarnings[name]) {
alreadyDisplayedWarnings[name] = true
log.warn('Failed to instantiate preprocessor %s', name)
}
return
var preprocessors = []
var nextPreprocessor = createNextProcessor(preprocessors, file, done)

for (var i = 0; i < patterns.length; i++) {
if (mm(file.originalPath, patterns[i], {dot: true})) {
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(function (name) {
var p = instances[name]
if (p == null) {
p = instantiatePreprocessor(name)
}

if (p == null) {
if (!alreadyDisplayedWarnings[name]) {
alreadyDisplayedWarnings[name] = true
log.warn('Failed to instantiate preprocessor %s', name)
}
return
}

instances[name] = p
preprocessors.push(p)
})
}

instances[name] = p
preprocessors.push(p)
})
}
}
}
}

return fs.readFile(file.originalPath, function (err, buffer) {
if (err) {
throw err
}
nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString())
nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString())
})
})
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
"glob": "^6.0.3",
"graceful-fs": "^4.1.2",
"http-proxy": "^1.11.1",
"isbinaryfile": "^3.0.0",
"lodash": "^3.8.0",
"log4js": "^0.6.28",
"mime": "^1.3.4",
Expand Down
7 changes: 5 additions & 2 deletions test/unit/preprocessor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ describe('preprocessor', () => {
var m
var mockFs

// mimic first few bytes of a pdf file
var binarydata = new Buffer([0x25, 0x50, 0x44, 0x66, 0x46, 0x00])

beforeEach(() => {
mockFs = mocks.fs.create({
some: {
'a.js': mocks.fs.file(0, 'content'),
'b.js': mocks.fs.file(0, 'content'),
'a.txt': mocks.fs.file(0, 'some-text'),
'photo.png': mocks.fs.file(0, 'binary'),
'CAM_PHOTO.JPG': mocks.fs.file(0, 'binary'),
'photo.png': mocks.fs.file(0, binarydata),
'CAM_PHOTO.JPG': mocks.fs.file(0, binarydata),
'.dir': {
'a.js': mocks.fs.file(0, 'content')
}
Expand Down