Skip to content

Commit 708ae13

Browse files
authoredFeb 20, 2020
feat(preprocessor): obey Pattern.isBinary when set (#3422)
Add support for isBinary. If set, use it and don't probe file to detect binary status. Fixes #3405
1 parent 00d536f commit 708ae13

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed
 

‎lib/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ try {
3232
} catch (e) {}
3333

3434
class Pattern {
35-
constructor (pattern, served, included, watched, nocache, type) {
35+
constructor (pattern, served, included, watched, nocache, type, isBinary) {
3636
this.pattern = pattern
3737
this.served = helper.isDefined(served) ? served : true
3838
this.included = helper.isDefined(included) ? included : true
3939
this.watched = helper.isDefined(watched) ? watched : true
4040
this.nocache = helper.isDefined(nocache) ? nocache : false
4141
this.weight = helper.mmPatternWeight(pattern)
4242
this.type = type
43+
this.isBinary = isBinary
4344
}
4445

4546
compare (other) {

‎lib/file-list.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class FileList {
6161

6262
let lastCompletedRefresh = this._refreshing
6363
lastCompletedRefresh = Promise
64-
.map(this._patterns, async ({ pattern, type, nocache }) => {
64+
.map(this._patterns, async ({ pattern, type, nocache, isBinary }) => {
6565
if (helper.isUrlAbsolute(pattern)) {
6666
this.buckets.set(pattern, [new Url(pattern, type)])
6767
return
@@ -81,7 +81,7 @@ class FileList {
8181
return true
8282
}
8383
})
84-
.map((path) => new File(path, mg.statCache[path].mtime, nocache, type))
84+
.map((path) => new File(path, mg.statCache[path].mtime, nocache, type, isBinary))
8585

8686
if (nocache) {
8787
log.debug(`Not preprocessing "${pattern}" due to nocache`)

‎lib/file.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* File object used for tracking files in `file-list.js`.
55
*/
66
class File {
7-
constructor (path, mtime, doNotCache, type) {
7+
constructor (path, mtime, doNotCache, type, isBinary) {
88
// used for serving (processed path, eg some/file.coffee -> some/file.coffee.js)
99
this.path = path
1010

@@ -24,6 +24,9 @@ class File {
2424
this.doNotCache = doNotCache === undefined ? false : doNotCache
2525

2626
this.type = type
27+
28+
// Tri state: null means probe file for binary.
29+
this.isBinary = isBinary === undefined ? null : isBinary
2730
}
2831

2932
toString () {

‎lib/preprocessor.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath
102102

103103
return async function preprocess (file) {
104104
const buffer = await tryToRead(file.originalPath, log)
105-
const isBinary = await isBinaryFile(buffer, buffer.length)
105+
let isBinary = file.isBinary
106+
if (isBinary == null) {
107+
// Pattern did not specify, probe for it.
108+
isBinary = await isBinaryFile(buffer, buffer.length)
109+
}
106110

107111
const preprocessorNames = Object.keys(config).reduce((ppNames, pattern) => {
108112
if (mm(file.originalPath, pattern, { dot: true })) {

‎test/unit/preprocessor.spec.js

+38
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('preprocessor', () => {
2121
'a.txt': mocks.fs.file(0, 'some-text'),
2222
'photo.png': mocks.fs.file(0, binarydata),
2323
'CAM_PHOTO.JPG': mocks.fs.file(0, binarydata),
24+
'proto.pb': mocks.fs.file(0, Buffer.from('mixed-content', 'utf8')),
2425
'.dir': {
2526
'a.js': mocks.fs.file(0, 'content')
2627
}
@@ -325,6 +326,43 @@ describe('preprocessor', () => {
325326
expect(file.content).to.be.an.instanceof(Buffer)
326327
})
327328

329+
it('should not preprocess files configured to be binary', async () => {
330+
const fakePreprocessor = sinon.spy((content, file, done) => {
331+
done(null, content)
332+
})
333+
334+
const injector = new di.Injector([{
335+
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
336+
}, emitterSetting])
337+
338+
const pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, injector)
339+
340+
const file = { originalPath: '/some/proto.pb', path: 'path', isBinary: true }
341+
342+
await pp(file)
343+
expect(fakePreprocessor).not.to.have.been.called
344+
expect(file.content).to.be.an.instanceof(Buffer)
345+
})
346+
347+
it('should preprocess files configured not to be binary', async () => {
348+
const fakePreprocessor = sinon.spy((content, file, done) => {
349+
done(null, content)
350+
})
351+
352+
const injector = new di.Injector([{
353+
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
354+
}, emitterSetting])
355+
356+
const pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, injector)
357+
358+
// Explicit false for isBinary
359+
const file = { originalPath: '/some/proto.pb', path: 'path', isBinary: false }
360+
361+
await pp(file)
362+
expect(fakePreprocessor).to.have.been.calledOnce
363+
expect(typeof file.content).to.equal('string')
364+
})
365+
328366
it('should preprocess binary files if handleBinaryFiles=true', async () => {
329367
const fakePreprocessor = sinon.spy((content, file, done) => {
330368
done(null, content)

0 commit comments

Comments
 (0)
Please sign in to comment.