-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwriteHTTP.js
43 lines (31 loc) · 1.17 KB
/
writeHTTP.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
const etag = require('etag')
const { curry } = require('ramda')
const { Stream } = require('stream')
const { byteLength, isBuffer } = Buffer
const writeHTTP = (req, res, data) => {
// bail if middleware has already ended the response
if (res.finished) return
const { body, headers = {}, statusCode = 200 } = data
for (let name in headers)
res.setHeader(name, headers[name])
const bodyEtag = !(body instanceof Stream) && etag(body || '')
const reqEtag = req.headers['if-none-match']
const supports304 = statusCode === 200 && ['GET', 'HEAD'].includes(req.method)
if (supports304 && reqEtag && reqEtag === bodyEtag) {
res.setHeader('etag', bodyEtag)
res.statusCode = 304
return res.end()
}
res.statusCode = statusCode
if (!body)
return res.end()
if (body instanceof Stream)
return body.pipe(res)
if (!res.getHeader('content-type'))
res.setHeader('content-type', 'application/octet-stream')
const length = isBuffer(body) ? body.length : byteLength(body)
res.setHeader('content-length', length)
res.setHeader('etag', bodyEtag)
res.end(req.method === 'HEAD' ? undefined : body)
}
exports.writeHTTP = curry(writeHTTP)