Skip to content

Commit

Permalink
refactor(middleware): update lib/middleware/source_files to ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
lusarz committed Jun 11, 2018
1 parent d6060d4 commit f47d901
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions lib/middleware/source_files.js
@@ -1,31 +1,28 @@
var querystring = require('querystring')
var _ = require('lodash')
'use strict'

var common = require('./common')
var logger = require('../logger')
var log = logger.create('middleware:source-files')
const querystring = require('querystring')
const common = require('./common')

// Files is a Set
var findByPath = function (files, path) {
return _.find(Array.from(files), function (file) {
return file.path === path
})
const log = require('../logger').create('middleware:source-files')

function findByPath (files, path) {
return Array.from(files).find((file) => file.path === path)
}

var composeUrl = function (url, basePath, urlRoot, mustEscape) {
return (mustEscape ? querystring.unescape(url) : url)
function composeUrl (url, basePath, urlRoot) {
return url
.replace(urlRoot, '/')
.replace(/\?.*$/, '')
.replace(/^\/absolute/, '')
.replace(/^\/base/, basePath)
}

// Source Files middleware is responsible for serving all the source files under the test.
var createSourceFilesMiddleware = function (filesPromise, serveFile, basePath, urlRoot) {
function createSourceFilesMiddleware (filesPromise, serveFile, basePath, urlRoot) {
return function (request, response, next) {
var requestedFilePath = composeUrl(request.url, basePath, urlRoot, true)
const requestedFilePath = composeUrl(request.url, basePath, urlRoot)
// When a path contains HTML-encoded characters (e.g %2F used by Jenkins for branches with /)
var requestedFilePathUnescaped = composeUrl(request.url, basePath, urlRoot, false)
const requestedFilePathUnescaped = composeUrl(querystring.unescape(request.url), basePath, urlRoot)

request.pause()

Expand All @@ -34,18 +31,15 @@ var createSourceFilesMiddleware = function (filesPromise, serveFile, basePath, u

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

if (file) {
serveFile(file.contentPath || file.path, rangeHeader, response, function () {
if (/\?\w+/.test(request.url)) {
// files with timestamps - cache one year, rely on timestamps
common.setHeavyCacheHeaders(response)
common.setHeavyCacheHeaders(response) // files with timestamps - cache one year, rely on timestamps
} else {
// without timestamps - no cache (debug)
common.setNoCacheHeaders(response)
common.setNoCacheHeaders(response) // without timestamps - no cache (debug)
}
}, file.content, file.doNotCache)
} else {
Expand All @@ -61,5 +55,4 @@ createSourceFilesMiddleware.$inject = [
'filesPromise', 'serveFile', 'config.basePath', 'config.urlRoot'
]

// PUBLIC API
exports.create = createSourceFilesMiddleware

0 comments on commit f47d901

Please sign in to comment.