-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserve-handler.js
61 lines (54 loc) · 1.72 KB
/
serve-handler.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
import { parse } from 'url'
import fs from 'fs'
import send from 'send'
const panelsVersion = require('panels/package.json').version
const panelsJs = require.resolve(`panels/bundle/panels-${panelsVersion}.js`)
const panelsJsonFile = `${__dirname}/panels.json`
const playgroundFile = `${__dirname}/playground.html`
const FILES = {
// serve the panels runtime
'panels.js': panelsJs,
// serve panels.json if it wasn't served from assets
'panels.json': panelsJsonFile,
// serve the panels runtime source map
[`panels-${panelsVersion}.js.map`]: `${panelsJs}.map`
}
function isFile(file) {
try {
const stat = fs.statSync(file)
return stat.isFile()
} catch(err) {
return false
}
}
export default app => (req, res) => {
try {
const pathname = parse(req.url).pathname.replace(app.root, '')
const assetFile = `${app.assets}${pathname}`
let file
if (pathname === 'app.js') {
// serve panels packaged app.js'
file = app.tmp
} else if (isFile(assetFile)) {
// serve static assets
file = assetFile
} else if (FILES[pathname]) {
file = FILES[pathname]
} else if (app.serveAsIs.find(regex => regex.test(pathname))) {
// serve files that the user defined they want them like that
if (isFile(assetFile)) {
file = assetFile
}
} else {
// catch all for index
const customIndexFile = `${app.assets}/index.html`
file = fs.existsSync(customIndexFile) ? customIndexFile : playgroundFile
}
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
send(req, file).pipe(res)
} catch(err) {
res.writeHead(404)
res.end()
}
}