This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
/
deposito
executable file
·146 lines (122 loc) · 3.07 KB
/
deposito
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
#!/usr/bin/env node
'use strict'
var http = require('http')
var url = require('url')
var path = require('path')
var connect = require('connect')
var send = require('send')
var yaml = require('js-yaml')
var fs = require('fs')
var mime = send.mime
var portfinder = require('portfinder')
var config
try {
config = yaml.safeLoad(fs.readFileSync(path.resolve(__dirname, '../default.yml'), 'utf8'))
} catch (e) {
throw e
}
try {
var customConfig = yaml.safeLoad(fs.readFileSync('deposito.yml', 'utf8'))
for (var i in config) {
if (i in customConfig) {
config[i] = customConfig[i]
}
}
} catch (e) {}
if (config.mime) {
mime.define(config.mime)
}
var csp = ''
for (var k in config.CSP) {
csp += k + ': '
if (typeof config.CSP[k] === 'string') {
csp += config.CSP[k] + '; '
} else if (Array.isArray(config.CSP[k])) {
config.CSP[k].forEach(function (src, a) {
csp += src + (a === config.CSP[k].length - 1 ? '; ' : ', ')
})
}
}
var app = connect()
app.use(function (req, res, next) {
// handle method OPTIONS
if (req.method === 'OPTIONS') {
res.setHeader('Allow', 'GET, HEAD, OPTIONS')
res.writeHead(200)
return res.end()
}
//
// handle unsupported methods
//
if (req.method !== 'GET' && req.method !== 'HEAD') {
res.writeHead(405)
return res.end()
}
var pathname = url.parse(req.url).pathname
// handle index
if (pathname === '/') {
return sendDocument(config.index, req, res)
}
// redirect to index
if (pathname === '/' + config.index) {
return redirectToIndex()
}
next()
})
var acceptDocument = function (p, req) {
var ext = path.extname(p)
var type = send.mime.lookup(ext)
var accept = req.headers.accept
if (accept.indexOf('*/*') > -1 || accept.indexOf(type) > -1) {
return true
}
return false
}
var sendDocument = function (p, req, res) {
// Content-Security-Policy
if (config.CSP) {
res.setHeader('Content-Security-Policy', csp)
}
// X-Frame-Option
if (config.frame) {
res.setHeader('X-Frame-Option', config.frame)
}
if (config['xss-protection']) {
res.setHeader('X-XSS-Protection', config['xss-protection'])
}
send(req, p, {root: config.root, index: config.index})
.pipe(res)
}
var redirectToIndex = function (req, res) {
res.writeHead(301, {
'Location': '/'
})
res.end()
}
var fileServer = function (req, res) {
function error () {
if (config['history API'] === true && acceptDocument(config.index, req)) {
return sendDocument(config.index, req, res)
}
res.writeHead(404)
if (config['not found'] && acceptDocument(config['not found'], req)) {
sendDocument(config['not found'], req, res)
} else {
res.end()
}
}
var pathname = url.parse(req.url).pathname
send(req, pathname, {root: config.root})
.on('error', error)
.pipe(res)
}
app.use(fileServer)
// listen
portfinder.basePort = config.port
portfinder.getPort(function (err, port) {
if (err) throw err
http.createServer(app).listen(port, function (error) {
if (error) throw error
console.log('deposito listening on', port)
})
})