-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
44 lines (38 loc) · 1.15 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
import fs from 'fs'
import { basename } from 'path'
import { STATUS_CODES } from 'http'
export const status = (res, code) => {
res.statusCode = code
res.end(STATUS_CODES[code])
}
export const json = (res, json) => {
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify(json))
}
export const redirect = (req, res, location, status = 302) => {
res.statusCode = status
res.setHeader('location', location)
const body = req.method === 'HEAD' ? '' : `-> ${location}`
res.end(body)
}
export const stream = async (res, stream) => {
stream.pipe(res)
await new Promise((resolve, reject) => {
stream.on('error', reject)
res.on('close', () => {
stream.destroy()
resolve()
})
res.on('finish', resolve)
})
}
export const file = async (res, path, opts) => {
const stat = await fs.promises.stat(path)
res.setHeader('content-length', stat.size)
await stream(res, fs.createReadStream(path, opts))
}
export const download = async (res, path, opts) => {
const filename = basename(path).replace(/"/, '\\"')
res.setHeader('content-disposition', `attachment; filename="${filename}"`)
await file(res, path, opts)
}