Skip to content

Commit

Permalink
refactor(middleware): update middleware/common to ES6 (#3028)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjbarton authored Jun 7, 2018
1 parent 31d9a08 commit c1a9567
Showing 1 changed file with 29 additions and 36 deletions.
65 changes: 29 additions & 36 deletions lib/middleware/common.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
/**
* This module contains some common helpers shared between middlewares
*/
'use strict'

var mime = require('mime')
var _ = require('lodash')
var parseRange = require('range-parser')
var Buffer = require('safe-buffer').Buffer
const mime = require('mime')
const _ = require('lodash')
const parseRange = require('range-parser')
const Buffer = require('safe-buffer').Buffer

var log = require('../logger').create('web-server')
const log = require('../logger').create('web-server')

var PromiseContainer = function () {
var promise
class PromiseContainer {
constructor () {
this.promise = null
}

this.then = function (success, error) {
then (success, error) {
error = error || _.noop
return promise.then(success).catch(error)
return this.promise.then(success).catch(error)
}

this.set = function (newPromise) {
promise = newPromise
set (newPromise) {
this.promise = newPromise
}
}

var serve404 = function (response, path) {
function serve404 (response, path) {
log.warn('404: ' + path)
response.writeHead(404)
return response.end('NOT FOUND')
}

var createServeFile = function (fs, directory, config) {
var cache = Object.create(null)
function createServeFile (fs, directory, config) {
const cache = Object.create(null)

return function (filepath, rangeHeader, response, transform, content, doNotCache) {
var responseData
let responseData

var convertForRangeRequest = function () {
var range = parseRange(responseData.length, rangeHeader)
const convertForRangeRequest = function () {
const range = parseRange(responseData.length, rangeHeader)
if (range === -2) {
// malformed header string
return 200
Expand All @@ -46,8 +49,8 @@ var createServeFile = function (fs, directory, config) {
} else if (range.type === 'bytes') {
responseData = Buffer.from(responseData)
if (range.length === 1) {
var start = range[0].start
var end = range[0].end
const start = range[0].start
const end = range[0].end
response.setHeader(
'Content-Range',
'bytes ' + start + '-' + end + '/' + responseData.length
Expand Down Expand Up @@ -76,7 +79,7 @@ var createServeFile = function (fs, directory, config) {

if (config && config.customHeaders && config.customHeaders.length > 0) {
config.customHeaders.forEach(function (header) {
var regex = new RegExp(header.match)
const regex = new RegExp(header.match)
if (regex.test(filepath)) {
log.debug('setting header: ' + header.name + ' for: ' + filepath)
response.setHeader(header.name, header.value)
Expand All @@ -91,12 +94,7 @@ var createServeFile = function (fs, directory, config) {
// call custom transform fn to transform the data
responseData = (transform && transform(content)) || content

if (rangeHeader) {
var code = convertForRangeRequest()
response.writeHead(code)
} else {
response.writeHead(200)
}
response.writeHead(rangeHeader ? convertForRangeRequest() : 200)

log.debug('serving (cached): ' + filepath)
return response.end(responseData)
Expand All @@ -116,33 +114,28 @@ var createServeFile = function (fs, directory, config) {
// call custom transform fn to transform the data
responseData = (transform && transform(data.toString())) || data

if (rangeHeader) {
var code = convertForRangeRequest()
response.writeHead(code)
} else {
response.writeHead(200)
}
response.writeHead(rangeHeader ? convertForRangeRequest() : 200)

log.debug('serving: ' + filepath)
return response.end(responseData)
})
}
}

var setNoCacheHeaders = function (response) {
function setNoCacheHeaders (response) {
response.setHeader('Cache-Control', 'no-cache')
response.setHeader('Pragma', 'no-cache')
response.setHeader('Expires', (new Date(0)).toUTCString())
}

var setHeavyCacheHeaders = function (response) {
function setHeavyCacheHeaders (response) {
response.setHeader('Cache-Control', 'public, max-age=31536000')
}

var initializeMimeTypes = function (config) {
function initializeMimeTypes (config) {
if (config && config.mime) {
_.forEach(config.mime, function (value, key) {
var map = {}
const map = {}
map[key] = value
mime.define(map)
})
Expand Down

0 comments on commit c1a9567

Please sign in to comment.