-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.js
37 lines (34 loc) · 1.01 KB
/
routes.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
var auth = require('./auth')
var browserify = require('browserify')
var path = require('path')
var options = require('./options')
var replaceStream = require('replacestream')
module.exports = routes
function routes (name, opt) {
opt = options(name, opt)
var replaceOptions = { keyEncoding: opt.encoding.keyEncoding, valueEncoding: opt.encoding.valueEncoding, retry: true }
return {
html: handleAuth((q, r, params) => r.end('<script src="/repl.js"></script>'), opt),
js: handleAuth(
(q, r, params) =>
browserify({ debug: true })
.add(path.join(__dirname, '/repl.js'))
.bundle()
.pipe(replaceStream('\'replacedbyserver\'', JSON.stringify(replaceOptions)))
.pipe(r)
, opt
)
}
}
function handleAuth (fn, opt) {
return (q, r, params, splat) => {
auth(q, opt, (error) => {
if (error) {
r.writeHead(401, { 'WWW-Authenticate': 'Basic' })
r.end(error.message)
} else {
fn(q, r, params, splat)
}
})
}
}