forked from transloadit/uppy
/
server.js
executable file
·155 lines (135 loc) · 2.99 KB
/
server.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
151
152
153
154
155
#!/usr/bin/env node
/* eslint-disable compat/compat */
import http from 'node:http'
import qs from 'node:querystring'
import he from 'he'
const e = he.encode
/**
* A very haxxor server that outputs some of the data it receives in a POST form parameter.
*/
const server = http.createServer(onrequest)
server.listen(9967)
function onrequest (req, res) {
if (req.url !== '/test') {
res.writeHead(404, { 'content-type': 'text/html' })
res.end('404')
return
}
function onbody (body) {
const fields = qs.parse(body)
const result = JSON.parse(fields.uppyResult)
const assemblies = result[0].transloadit
res.setHeader('content-type', 'text/html')
res.write(Header())
res.write(FormFields(fields))
assemblies.forEach((assembly) => {
res.write(AssemblyResult(assembly))
})
res.end(Footer())
}
{
let body = ''
req.on('data', (chunk) => { body += chunk })
req.on('end', () => {
onbody(body)
})
}
}
function Header () {
return `
<!DOCTYPE html>
<html>
<head>
<style>
body { background: #f1f1f1; }
main {
padding: 20px;
font: 12pt sans-serif;
background: white;
width: 800px;
margin: auto;
}
</style>
</head>
<body>
<main>
`
}
function Footer () {
return `
</main>
</body>
</html>
`
}
function FormFields (fields) {
return `
<h1>Form Fields</h1>
<dl>
${Object.entries(fields).map(Field).join('\n')}
</dl>
`
function Field ([name, value]) {
if (name === 'transloadit') return ''
let isValueJSON = false
if (value.startsWith('{') || value.startsWith('[')) {
try {
// eslint-disable-next-line no-param-reassign
value = JSON.stringify(
JSON.parse(value),
null,
2,
)
isValueJSON = true
} catch {
// Nothing
}
}
const prettyValue = isValueJSON ? `
<details open>
<code>
<pre style="max-width: 100%; max-height: 400px; white-space: pre-wrap; overflow: auto;">${e(value)}</pre>
</code>
</details>
` : e(value)
return `
<dt>${e(name)}</dt>
<dd>
${prettyValue}
</dd>
`
}
}
function AssemblyResult (assembly) {
return `
<h1>${e(assembly.assembly_id)} (${e(assembly.ok)})</h1>
${UploadsList(assembly.uploads)}
${ResultsList(assembly.results)}
`
}
function UploadsList (uploads) {
return `
<ul>
${uploads.map(Upload).join('\n')}
</ul>
`
function Upload (upload) {
return `<li>${e(upload.name)}</li>`
}
}
function ResultsList (results) {
return Object.keys(results)
.map(ResultsSection)
.join('\n')
function ResultsSection (stepName) {
return `
<h2>${e(stepName)}</h2>
<ul>
${results[stepName].map(Result).join('\n')}
</ul>
`
}
function Result (result) {
return `<li>${e(result.name)} <a href="${result.ssl_url}" target="_blank">View</a></li>`
}
}