-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
common.js
150 lines (124 loc) · 4.11 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* This module contains some common helpers shared between middlewares
*/
'use strict'
const mime = require('mime')
const _ = require('lodash')
const parseRange = require('range-parser')
const Buffer = require('safe-buffer').Buffer
const log = require('../logger').create('web-server')
class PromiseContainer {
constructor () {
this.promise = null
}
then (success, error) {
return this.promise.then(success, error)
}
set (newPromise) {
this.promise = newPromise
}
}
function serve404 (response, path) {
log.warn('404: ' + path)
response.writeHead(404)
return response.end('NOT FOUND')
}
function createServeFile (fs, directory, config) {
const cache = Object.create(null)
return function (filepath, rangeHeader, response, transform, content, doNotCache) {
let responseData
const convertForRangeRequest = function () {
const range = parseRange(responseData.length, rangeHeader)
if (range === -2) {
// malformed header string
return 200
} else if (range === -1) {
// unsatisfiable range
responseData = Buffer.alloc(0)
return 416
} else if (range.type === 'bytes') {
responseData = Buffer.from(responseData)
if (range.length === 1) {
const start = range[0].start
const end = range[0].end
response.setHeader(
'Content-Range',
'bytes ' + start + '-' + end + '/' + responseData.length
)
response.setHeader('Accept-Ranges', 'bytes')
response.setHeader('Content-Length', end - start + 1)
responseData = responseData.slice(start, end + 1)
return 206
} else {
// Multiple ranges are not supported. Maybe future?
responseData = Buffer.alloc(0)
return 416
}
}
// All other states, ignore
return 200
}
if (directory) {
filepath = directory + filepath
}
if (!content && cache[filepath]) {
content = cache[filepath]
}
if (config && config.customHeaders && config.customHeaders.length > 0) {
config.customHeaders.forEach(function (header) {
const regex = new RegExp(header.match)
if (regex.test(filepath)) {
log.debug('setting header: ' + header.name + ' for: ' + filepath)
response.setHeader(header.name, header.value)
}
})
}
// serve from cache
if (content && !doNotCache) {
response.setHeader('Content-Type', mime.lookup(filepath, 'text/plain'))
// call custom transform fn to transform the data
responseData = (transform && transform(content)) || content
response.writeHead(rangeHeader ? convertForRangeRequest() : 200)
log.debug('serving (cached): ' + filepath)
return response.end(responseData)
}
return fs.readFile(filepath, function (error, data) {
if (error) {
return serve404(response, filepath)
}
if (!doNotCache) {
cache[filepath] = data.toString()
}
response.setHeader('Content-Type', mime.lookup(filepath, 'text/plain'))
// call custom transform fn to transform the data
responseData = (transform && transform(data.toString())) || data
response.writeHead(rangeHeader ? convertForRangeRequest() : 200)
log.debug('serving: ' + filepath)
return response.end(responseData)
})
}
}
function setNoCacheHeaders (response) {
response.setHeader('Cache-Control', 'no-cache')
response.setHeader('Pragma', 'no-cache')
response.setHeader('Expires', (new Date(0)).toUTCString())
}
function setHeavyCacheHeaders (response) {
response.setHeader('Cache-Control', 'public, max-age=31536000')
}
function initializeMimeTypes (config) {
if (config && config.mime) {
_.forEach(config.mime, function (value, key) {
const map = {}
map[key] = value
mime.define(map)
})
}
}
// PUBLIC API
exports.PromiseContainer = PromiseContainer
exports.createServeFile = createServeFile
exports.setNoCacheHeaders = setNoCacheHeaders
exports.setHeavyCacheHeaders = setHeavyCacheHeaders
exports.initializeMimeTypes = initializeMimeTypes
exports.serve404 = serve404