Skip to content

Commit f938a8e

Browse files
committed
feat(web-server): Use isbinaryfile for binary file detection
Delete `lib/binary-extensions.json` and change the preprocessor to use the `isbinaryfile` module instead. Factor nextPreprocessor function creation into a factory function for code clarity. Update preprocessor specs to use binary data when mocking binary files. Closes #1070
1 parent 56073fc commit f938a8e

4 files changed

Lines changed: 67 additions & 195 deletions

File tree

lib/binary-extensions.json

Lines changed: 0 additions & 136 deletions
This file was deleted.

lib/preprocessor.js

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
var path = require('path')
21
var fs = require('graceful-fs')
32
var crypto = require('crypto')
43
var mm = require('minimatch')
5-
var extensions = require('./binary-extensions.json').extensions
4+
var isBinaryFile = require('isbinaryfile')
65

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

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

15-
var isBinary = Object.create(null)
16-
extensions.forEach(function (extension) {
17-
isBinary['.' + extension] = true
18-
})
14+
var createNextProcessor = function (preprocessors, file, done) {
15+
return function nextPreprocessor (error, content) {
16+
// normalize B-C
17+
if (arguments.length === 1 && typeof error === 'string') {
18+
content = error
19+
error = null
20+
}
21+
22+
if (error) {
23+
file.content = null
24+
file.contentPath = null
25+
return done(error)
26+
}
27+
28+
if (!preprocessors.length) {
29+
file.contentPath = null
30+
file.content = content
31+
file.sha = sha1(content)
32+
return done()
33+
}
34+
35+
preprocessors.shift()(content, file, nextPreprocessor)
36+
}
37+
}
1938

2039
var createPreprocessor = function (config, basePath, injector) {
2140
var alreadyDisplayedWarnings = {}
@@ -51,64 +70,49 @@ var createPreprocessor = function (config, basePath, injector) {
5170

5271
return function preprocess (file, done) {
5372
patterns = Object.keys(config)
54-
var thisFileIsBinary = isBinary[path.extname(file.originalPath).toLowerCase()]
55-
var preprocessors = []
56-
57-
var nextPreprocessor = function (error, content) {
58-
// normalize B-C
59-
if (arguments.length === 1 && typeof error === 'string') {
60-
content = error
61-
error = null
62-
}
63-
64-
if (error) {
65-
file.content = null
66-
file.contentPath = null
67-
return done(error)
68-
}
6973

70-
if (!preprocessors.length) {
71-
file.contentPath = null
72-
file.content = content
73-
file.sha = sha1(content)
74-
return done()
74+
return fs.readFile(file.originalPath, function (err, buffer) {
75+
if (err) {
76+
throw err
7577
}
7678

77-
preprocessors.shift()(content, file, nextPreprocessor)
78-
}
79-
80-
for (var i = 0; i < patterns.length; i++) {
81-
if (mm(file.originalPath, patterns[i], {dot: true})) {
82-
if (thisFileIsBinary) {
83-
log.warn('Ignoring preprocessing (%s) %s because it is a binary file.',
84-
config[patterns[i]].join(', '), file.originalPath)
85-
} else {
86-
config[patterns[i]].forEach(function (name) {
87-
var p = instances[name]
88-
if (p == null) {
89-
p = instantiatePreprocessor(name)
90-
}
79+
isBinaryFile(buffer, buffer.length, function (err, thisFileIsBinary) {
80+
if (err) {
81+
throw err
82+
}
9183

92-
if (p == null) {
93-
if (!alreadyDisplayedWarnings[name]) {
94-
alreadyDisplayedWarnings[name] = true
95-
log.warn('Failed to instantiate preprocessor %s', name)
96-
}
97-
return
84+
var preprocessors = []
85+
var nextPreprocessor = createNextProcessor(preprocessors, file, done)
86+
87+
for (var i = 0; i < patterns.length; i++) {
88+
if (mm(file.originalPath, patterns[i], {dot: true})) {
89+
if (thisFileIsBinary) {
90+
log.warn('Ignoring preprocessing (%s) %s because it is a binary file.',
91+
config[patterns[i]].join(', '), file.originalPath)
92+
} else {
93+
config[patterns[i]].forEach(function (name) {
94+
var p = instances[name]
95+
if (p == null) {
96+
p = instantiatePreprocessor(name)
97+
}
98+
99+
if (p == null) {
100+
if (!alreadyDisplayedWarnings[name]) {
101+
alreadyDisplayedWarnings[name] = true
102+
log.warn('Failed to instantiate preprocessor %s', name)
103+
}
104+
return
105+
}
106+
107+
instances[name] = p
108+
preprocessors.push(p)
109+
})
98110
}
99-
100-
instances[name] = p
101-
preprocessors.push(p)
102-
})
111+
}
103112
}
104-
}
105-
}
106113

107-
return fs.readFile(file.originalPath, function (err, buffer) {
108-
if (err) {
109-
throw err
110-
}
111-
nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString())
114+
nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString())
115+
})
112116
})
113117
}
114118
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
"glob": "^6.0.3",
266266
"graceful-fs": "^4.1.2",
267267
"http-proxy": "^1.11.1",
268+
"isbinaryfile": "^3.0.0",
268269
"lodash": "^3.8.0",
269270
"log4js": "^0.6.28",
270271
"mime": "^1.3.4",

test/unit/preprocessor.spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ describe('preprocessor', () => {
66
var m
77
var mockFs
88

9+
// mimic first few bytes of a pdf file
10+
var binarydata = new Buffer([0x25, 0x50, 0x44, 0x66, 0x46, 0x00])
11+
912
beforeEach(() => {
1013
mockFs = mocks.fs.create({
1114
some: {
1215
'a.js': mocks.fs.file(0, 'content'),
1316
'b.js': mocks.fs.file(0, 'content'),
1417
'a.txt': mocks.fs.file(0, 'some-text'),
15-
'photo.png': mocks.fs.file(0, 'binary'),
16-
'CAM_PHOTO.JPG': mocks.fs.file(0, 'binary'),
18+
'photo.png': mocks.fs.file(0, binarydata),
19+
'CAM_PHOTO.JPG': mocks.fs.file(0, binarydata),
1720
'.dir': {
1821
'a.js': mocks.fs.file(0, 'content')
1922
}

0 commit comments

Comments
 (0)