[6a3a178] | 1 | /**
|
---|
| 2 | * This module contains some common helpers shared between middlewares
|
---|
| 3 | */
|
---|
| 4 | 'use strict'
|
---|
| 5 |
|
---|
| 6 | const mime = require('mime')
|
---|
| 7 | const parseRange = require('range-parser')
|
---|
| 8 | const log = require('../logger').create('web-server')
|
---|
| 9 |
|
---|
| 10 | function createServeFile (fs, directory, config) {
|
---|
| 11 | const cache = Object.create(null)
|
---|
| 12 |
|
---|
| 13 | return function (filepath, rangeHeader, response, transform, content, doNotCache) {
|
---|
| 14 | let responseData
|
---|
| 15 |
|
---|
| 16 | function convertForRangeRequest () {
|
---|
| 17 | const range = parseRange(responseData.length, rangeHeader)
|
---|
| 18 | if (range === -2) {
|
---|
| 19 | return 200 // malformed header string
|
---|
| 20 | } else if (range === -1) {
|
---|
| 21 | responseData = Buffer.alloc(0) // unsatisfiable range
|
---|
| 22 | return 416
|
---|
| 23 | } else if (range.type === 'bytes') {
|
---|
| 24 | responseData = Buffer.from(responseData)
|
---|
| 25 | if (range.length === 1) {
|
---|
| 26 | const { start, end } = range[0]
|
---|
| 27 | response.setHeader('Content-Range', `bytes ${start}-${end}/${responseData.length}`)
|
---|
| 28 | response.setHeader('Accept-Ranges', 'bytes')
|
---|
| 29 | response.setHeader('Content-Length', end - start + 1)
|
---|
| 30 | responseData = responseData.slice(start, end + 1)
|
---|
| 31 | return 206
|
---|
| 32 | } else {
|
---|
| 33 | responseData = Buffer.alloc(0) // Multiple ranges are not supported. Maybe future?
|
---|
| 34 | return 416
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 | return 200 // All other states, ignore
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | if (directory) {
|
---|
| 41 | filepath = directory + filepath
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | if (!content && cache[filepath]) {
|
---|
| 45 | content = cache[filepath]
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | if (config && config.customHeaders && config.customHeaders.length > 0) {
|
---|
| 49 | config.customHeaders.forEach((header) => {
|
---|
| 50 | const regex = new RegExp(header.match)
|
---|
| 51 | if (regex.test(filepath)) {
|
---|
| 52 | log.debug(`setting header: ${header.name} for: ${filepath}`)
|
---|
| 53 | response.setHeader(header.name, header.value)
|
---|
| 54 | }
|
---|
| 55 | })
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | if (content && !doNotCache) {
|
---|
| 59 | log.debug(`serving (cached): ${filepath}`)
|
---|
| 60 | response.setHeader('Content-Type', mime.getType(filepath, 'text/plain'))
|
---|
| 61 | responseData = (transform && transform(content)) || content
|
---|
| 62 | response.writeHead(rangeHeader ? convertForRangeRequest() : 200)
|
---|
| 63 | return response.end(responseData)
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | return fs.readFile(filepath, function (error, data) {
|
---|
| 67 | if (error) {
|
---|
| 68 | return serve404(response, filepath)
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | if (!doNotCache) {
|
---|
| 72 | cache[filepath] = data.toString()
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | log.debug('serving: ' + filepath)
|
---|
| 76 | response.setHeader('Content-Type', mime.getType(filepath, 'text/plain'))
|
---|
| 77 | responseData = (transform && transform(data.toString())) || data
|
---|
| 78 | response.writeHead(rangeHeader ? convertForRangeRequest() : 200)
|
---|
| 79 |
|
---|
| 80 | return response.end(responseData)
|
---|
| 81 | })
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | function serve404 (response, path) {
|
---|
| 86 | log.warn(`404: ${path}`)
|
---|
| 87 | response.writeHead(404)
|
---|
| 88 | return response.end('NOT FOUND')
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | function setNoCacheHeaders (response) {
|
---|
| 92 | response.setHeader('Cache-Control', 'no-cache')
|
---|
| 93 | response.setHeader('Pragma', 'no-cache')
|
---|
| 94 | response.setHeader('Expires', (new Date(0)).toUTCString())
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | function setHeavyCacheHeaders (response) {
|
---|
| 98 | response.setHeader('Cache-Control', 'public, max-age=31536000')
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | // PUBLIC API
|
---|
| 102 | exports.createServeFile = createServeFile
|
---|
| 103 | exports.setNoCacheHeaders = setNoCacheHeaders
|
---|
| 104 | exports.setHeavyCacheHeaders = setHeavyCacheHeaders
|
---|
| 105 | exports.serve404 = serve404
|
---|