-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
150 lines (134 loc) · 3.52 KB
/
index.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
'use strict'
const childProcess = require('child_process')
const toHtml = require('./toHtml')
const parseCmd = require('./parseCmd')
const { interpolate } = require('reserve')
const HTML_MIME_TYPE = 'text/html'
const TEXT_MIME_TYPE = 'text/plain'
function buildExecFileParameters (mapping, match, redirect) {
const parts = parseCmd(redirect)
return {
file: parts[0],
args: parts.slice(1),
options: {
cwd: mapping.cwd,
env: Object.assign({}, interpolate(match, mapping.env), process.env),
timeout: mapping.timeout | 0
}
}
}
function pre (response, contentType) {
if (!response.headersSent) {
response.writeHead(200, {
'Content-Type': contentType
})
response.flushHeaders()
return true
}
}
function preHtml (mapping, match, response) {
if (pre(response, HTML_MIME_TYPE)) {
response.write('<html><head>')
response.write(interpolate(match, mapping['html-header']))
if (mapping['html-tracking']) {
response.write(`<script>
var _lastScrollPos
function scroll () {
var pos = document.scrollingElement.scrollHeight
if (pos !== _lastScrollPos) {
_lastScrollPos = pos
document.scrollingElement.scrollTop = pos
}
}
</script>`)
}
response.write('</head><body><pre>')
}
}
function writeHtml ({ mapping, match, response }, chunk) {
preHtml(mapping, match, response)
response.write(toHtml(chunk.toString()))
if (mapping['html-tracking']) {
response.write('<script>scroll()</script>')
}
}
function writeText ({ response }, chunk) {
pre(response, TEXT_MIME_TYPE)
response.write(chunk.toString())
}
function postHtml ({ mapping, match, response }, end) {
preHtml(mapping, match, response)
response.write('</pre>')
response.write(interpolate(match, mapping['html-footer']))
response.write('</body></html>')
end()
}
const handlers = {}
handlers.GET = async ({ mapping, match, redirect, request, response }) => {
let end
const promise = new Promise(resolve => {
end = () => {
response.end()
resolve()
}
})
// FIRST implementation: If any mention of text/html, use it. Text otherwise
const accept = request.headers.accept || ''
let write
let post
if (accept.includes(HTML_MIME_TYPE) && !mapping['text-only']) {
write = writeHtml
post = postHtml
} else {
write = writeText
post = end
}
const { file, args, options } = buildExecFileParameters(mapping, match, redirect)
const cmd = childProcess.execFile(file, args, options)
const params = { mapping, match, response }
cmd.stdout.on('data', write.bind(null, params))
cmd.stderr.on('data', write.bind(null, params))
cmd.on('error', error => {
const message = error.toString()
response.writeHead(500, {
'Content-Type': TEXT_MIME_TYPE,
'Content-Length': message.length
})
response.write(message)
end()
})
cmd.on('close', post.bind(null, params, end))
return promise
}
module.exports = {
schema: {
env: {
type: 'object',
defaultValue: {}
},
'html-footer': {
type: 'string',
defaultValue: ''
},
'html-header': {
type: 'string',
defaultValue: ''
},
'html-tracking': {
type: 'boolean',
defaultValue: false
},
'text-only': {
type: 'boolean',
defaultValue: false
},
timeout: {
type: 'number',
defaultValue: 0
}
},
method: Object.keys(handlers),
async redirect ({ mapping, match, redirect, request, response }) {
return handlers[request.method]({ mapping, match, redirect, request, response })
}
}