response/index.js

284 lines
6.0 KiB
JavaScript
Raw Normal View History

2020-09-16 16:01:46 +08:00
/**
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/16 14:52:58
*/
2020-09-21 16:22:46 +08:00
import fs from 'iofs'
2020-09-20 16:27:35 +08:00
import STATUS_TEXT from './lib/http-code.js'
2023-10-31 12:28:27 +08:00
import { MIME_TYPES, CHARSET_TYPES } from './lib/mime-tpyes.js'
2020-09-21 17:53:21 +08:00
import { serialize } from './lib/cookie.js'
2020-09-16 16:01:46 +08:00
export default class Response {
2023-10-31 12:28:27 +08:00
#req = null
#res = null
2023-10-30 18:45:06 +08:00
2023-10-31 12:28:27 +08:00
#charset = 'utf-8'
status = 200
#ended = false
2023-10-30 18:45:06 +08:00
2023-10-31 12:28:27 +08:00
constructor(req, res) {
2023-10-30 18:45:06 +08:00
this.#req = req
this.#res = res
2023-10-31 12:28:27 +08:00
this.#ended = !!res.ended
}
get ended() {
return this.#ended
}
set charset(v) {
this.#charset = v || 'utf-8'
}
get type() {
return this.#res.getHeader('content-type')
}
set type(v) {
let mime = MIME_TYPES[v] || MIME_TYPES.stream
mime += CHARSET_TYPES[v] ? '; charset=' + this.#charset : ''
this.set('Content-Type', mime)
}
set body(buf = null) {
if (this.#ended) {
return this
}
this.#ended = true
this.#res.writeHead(this.status, STATUS_TEXT[this.status])
this.#res.end(buf)
2020-09-16 16:01:46 +08:00
}
/**
* [error http 错误显示]
* @param {Number} code [http错误码]
* @param {String} msg [错误提示信息]
*/
error(msg, code = 500) {
2023-10-31 12:28:27 +08:00
if (this.#ended) {
2020-09-16 16:01:46 +08:00
return
}
msg = msg || STATUS_TEXT[code]
2023-10-31 12:28:27 +08:00
this.status = code
this.type = 'html'
this.body = `<fieldset><legend>Http Status: ${code}</legend><pre>${msg}</pre></fieldset>`
2020-09-16 16:01:46 +08:00
}
/**
* [redirect 页面跳转]
* @param {String} url [要跳转的URL]
* @param {Boolean} f [是否永久重定向]
*/
redirect(url, f = false) {
2023-10-31 12:28:27 +08:00
if (this.#ended) {
2020-09-16 16:01:46 +08:00
return
}
if (!/^(http[s]?|ftp):\/\//.test(url)) {
url = '//' + url
}
this.set('Location', url)
2023-10-31 12:28:27 +08:00
this.status = f ? 301 : 302
this.body = null
2020-09-16 16:01:46 +08:00
}
/**
* [location 页面跳转(前端的方式)]
*/
location(url) {
2023-10-31 12:28:27 +08:00
let html = `<html><head><meta http-equiv="refresh" content="0;url=${url}"></head></html>`
if (this.#ended) {
2020-09-16 16:01:46 +08:00
return
}
this.render(html)
}
// 以html格式向前端输出内容
render(data, code) {
2023-10-31 12:28:27 +08:00
if (this.#ended) {
2020-09-16 16:01:46 +08:00
return
}
data += ''
data = data || STATUS_TEXT[code]
2023-10-31 12:28:27 +08:00
this.type = 'html'
2020-09-16 16:01:46 +08:00
this.set('Content-Length', Buffer.byteLength(data))
if (code) {
2023-10-31 12:28:27 +08:00
this.status = code
2020-09-16 16:01:46 +08:00
}
2023-10-31 12:28:27 +08:00
this.body = data
2020-09-16 16:01:46 +08:00
}
// 文件下载
2023-10-31 12:28:27 +08:00
sendfile(target, filename = 'untitled') {
if (this.#ended) {
2020-09-16 16:01:46 +08:00
return
}
2023-10-31 12:28:27 +08:00
let data, start, end
if (!this.type) {
this.status = 206
this.type = 'stream'
this.set('Content-Disposition', `attachment;filename="${filename}"`)
this.set('Accept-Ranges', 'bytes')
let range = this.#req.headers['range'] || ''
if (range) {
range = range.replace('bytes=', '')
if (range.includes(',')) {
// 多重范围的range请求, 暂时不支持, 直接返回整个文件
} else {
range = range.split('-').map(n => +n)
;[start, end] = range
if (end === 0) {
end = void 0
}
}
}
}
2020-09-21 16:22:46 +08:00
if (Buffer.isBuffer(target)) {
data = target
} else {
if (typeof target === 'string') {
2023-10-31 12:28:27 +08:00
let stat = fs.stat(target)
2020-09-21 16:22:46 +08:00
if (stat.isFile()) {
2023-10-31 12:28:27 +08:00
let size = stat.size
if (start !== void 0) {
if (end === void 0) {
size -= start
} else {
size = end - start
}
}
this.set('Content-Length', size)
return fs.origin
.createReadStream(target, { start, end })
.pipe(this.#res)
2020-09-21 16:22:46 +08:00
}
}
data = Buffer.from(target + '')
}
2023-10-31 12:28:27 +08:00
if (start !== void 0) {
data = data.slice(start, end)
}
2020-09-21 16:22:46 +08:00
this.set('Content-Length', data.length)
2023-10-31 12:28:27 +08:00
this.body = data
2020-09-16 16:01:46 +08:00
}
/**
* [send json格式输出]
* @param {Num} code [返回码]
* @param {Str} msg [提示信息]
* @param {Str/Obj} data [额外数据]
* @param {Str} callback [回调函数名]
*/
2023-10-31 12:28:27 +08:00
send(code = 200, msg = '', data, callback) {
let output
2020-09-16 16:01:46 +08:00
2023-10-31 12:28:27 +08:00
if (this.#ended) {
2020-09-16 16:01:46 +08:00
return
}
2023-10-31 12:28:27 +08:00
if (msg && typeof msg === 'object') {
callback = data
2020-09-16 16:01:46 +08:00
data = msg
2023-10-31 12:28:27 +08:00
msg = STATUS_TEXT[code]
} else {
msg = msg || STATUS_TEXT[code]
2020-09-16 16:01:46 +08:00
}
output = { code, msg, data }
output = JSON.stringify(output)
if (callback) {
2023-10-31 12:28:27 +08:00
callback = callback.replace(/[^\w\.]/g, '')
2020-09-16 16:01:46 +08:00
output = callback + '(' + output + ')'
}
2023-10-31 12:28:27 +08:00
this.type = 'json'
2020-09-16 16:01:46 +08:00
this.set('Content-Length', Buffer.byteLength(output))
// 只设置200以上的值
2023-10-31 12:28:27 +08:00
if (code && code >= 200 && code <= 599) {
this.status = code
2020-09-16 16:01:46 +08:00
}
2023-10-31 12:28:27 +08:00
this.body = output
2020-09-16 16:01:46 +08:00
}
/**
* [get 读取已写入的头信息]
*/
get(key) {
2023-10-31 12:28:27 +08:00
return this.#res.getHeader(key)
2020-09-16 16:01:46 +08:00
}
/**
* [set 设置头信息]
*/
set(key, val) {
2023-10-31 12:28:27 +08:00
if (this.#ended) {
2020-09-16 16:01:46 +08:00
return this
}
2023-10-31 12:28:27 +08:00
let value = Array.isArray(val) ? val.map(String) : String(val)
this.#res.setHeader(key, value)
2020-09-16 16:01:46 +08:00
return this
}
2020-09-21 17:53:21 +08:00
/**
* [append 往header插入信息]
* @param {String} key [description]
* @param {String} val [description]
*/
append(key, val) {
2023-10-31 12:28:27 +08:00
if (this.#ended) {
2020-09-21 17:53:21 +08:00
return
}
2023-10-31 12:28:27 +08:00
let prev = this.get(key) || []
2020-09-21 17:53:21 +08:00
2023-10-31 12:28:27 +08:00
if (!Array.isArray(prev)) {
prev = [prev]
2020-09-21 17:53:21 +08:00
}
2023-10-31 12:28:27 +08:00
prev = prev.concat(val)
return this.set(key, prev)
2020-09-21 17:53:21 +08:00
}
/**
* [set 设置cookie]
* @param {[string]} key
* @param {[string/number]} val
* @param {[object]} opts [设置cookie的额外信息,如域,有效期等]
*/
cookie(key, val, opts = {}) {
//读取之前已经写过的cookie缓存
2023-10-31 12:28:27 +08:00
let cache = this.get('set-cookie')
2020-09-21 17:53:21 +08:00
if (cache) {
if (!Array.isArray(cache)) {
cache = [cache]
}
} else {
cache = []
}
if (cache.length > 0) {
// 如果之前已经写了一个相同的cookie, 则删除之前的
cache = cache.filter(it => {
let _key = it.split('=')[0].trim()
return key !== _key
})
}
cache.push(serialize(key, val, opts))
this.set('set-cookie', cache)
}
2020-09-16 16:01:46 +08:00
}