Skip to content

Commit

Permalink
feat(source-files): Forcing preprocessor run on nocache files (karma-…
Browse files Browse the repository at this point in the history
  • Loading branch information
fvclaus committed Nov 26, 2019
1 parent adc6a66 commit ddc74be
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
6 changes: 1 addition & 5 deletions lib/file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ class FileList {
})
.map((path) => new File(path, mg.statCache[path].mtime, nocache, type))

if (nocache) {
log.debug(`Not preprocessing "${pattern}" due to nocache`)
} else {
await Promise.map(files, (file) => this._preprocess(file))
}
await Promise.map(files, (file) => this._preprocess(file))

this.buckets.set(pattern, files)

Expand Down
2 changes: 2 additions & 0 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class File {
this.doNotCache = doNotCache === undefined ? false : doNotCache

this.type = type

this.isPreprocessed = false
}

toString () {
Expand Down
17 changes: 12 additions & 5 deletions lib/middleware/source_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function composeUrl (url, basePath, urlRoot) {
}

// Source Files middleware is responsible for serving all the source files under the test.
function createSourceFilesMiddleware (filesPromise, serveFile, basePath, urlRoot) {
function createSourceFilesMiddleware (fileList, serveFile, basePath, urlRoot) {
return function (request, response, next) {
const requestedFilePath = composeUrl(request.url, basePath, urlRoot)
// When a path contains HTML-encoded characters (e.g %2F used by Jenkins for branches with /)
Expand All @@ -29,16 +29,23 @@ function createSourceFilesMiddleware (filesPromise, serveFile, basePath, urlRoot
log.debug(`Requesting ${request.url}`)
log.debug(`Fetching ${requestedFilePath}`)

return filesPromise.then(function (files) {
return Promise.resolve(fileList.files).then(async function (files) {
// TODO(vojta): change served to be a map rather then an array
const file = findByPath(files.served, requestedFilePath) || findByPath(files.served, requestedFilePathUnescaped)
var file = findByPath(files.served, requestedFilePath) || findByPath(files.served, requestedFilePathUnescaped)
const rangeHeader = request.headers['range']

if (file) {
if (file.isPreprocessed && file.doNotCache) {
log.debug(`Forcing preprocessor run on ${file.originalPath}`)
files = await fileList.changeFile(file.originalPath, true)
file = findByPath(files.served, requestedFilePath) || findByPath(files.served, requestedFilePathUnescaped)
}

const acceptEncodingHeader = request.headers['accept-encoding']
const matchedEncoding = Object.keys(file.encodings).find(
(encoding) => new RegExp(`(^|.*, ?)${encoding}(,|$)`).test(acceptEncodingHeader)
)

const content = file.encodings[matchedEncoding] || file.content

serveFile(file.contentPath || file.path, rangeHeader, response, function () {
Expand All @@ -50,7 +57,7 @@ function createSourceFilesMiddleware (filesPromise, serveFile, basePath, urlRoot
if (matchedEncoding) {
response.setHeader('Content-Encoding', matchedEncoding)
}
}, content, file.doNotCache)
}, content, file.doNotCache && !file.isPreprocessed)
} else {
next()
}
Expand All @@ -61,7 +68,7 @@ function createSourceFilesMiddleware (filesPromise, serveFile, basePath, urlRoot
}

createSourceFilesMiddleware.$inject = [
'filesPromise', 'serveFile', 'config.basePath', 'config.urlRoot'
'fileList', 'serveFile', 'config.basePath', 'config.urlRoot'
]

exports.create = createSourceFilesMiddleware
1 change: 1 addition & 0 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ async function runProcessors (preprocessors, file, content) {
file.contentPath = null
file.content = content
file.sha = CryptoUtils.sha1(content)
file.isPreprocessed = preprocessors.length > 0
}

function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath, injector) {
Expand Down

0 comments on commit ddc74be

Please sign in to comment.