response/index.js

311 lines
6.4 KiB
JavaScript
Raw Permalink 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)
}
2023-10-31 14:23:48 +08:00
set length(val) {
this.set('Content-Length', val)
}
2023-10-31 12:28:27 +08:00
set body(buf = null) {
if (this.#ended) {
2023-10-31 14:23:48 +08:00
return
2023-10-31 12:28:27 +08:00
}
this.#ended = true
this.#res.writeHead(this.status, STATUS_TEXT[this.status])
this.#res.end(buf)
2020-09-16 16:01:46 +08:00
}
/**
* 设置缓存时长, 单位秒
*/
set expires(time = 3600) {
let t = new Date(Date.now() + time * 1000)
this.set('Expires', t.toGMTString())
this.set('Cache-Control', 'max-age=' + time)
}
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
}
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
if (this.#ended) {
2020-09-16 16:01:46 +08:00
return
}
2023-10-31 14:23:48 +08:00
let html = `<html><head><meta http-equiv="refresh" content="0;url=${url}"></head></html>`
2020-09-16 16:01:46 +08:00
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
}
if (code) {
2023-10-31 12:28:27 +08:00
this.status = code
2020-09-16 16:01:46 +08:00
}
2023-10-31 14:23:48 +08:00
data += ''
data = data || STATUS_TEXT[this.status]
this.type = 'html'
this.length = Buffer.byteLength(data)
2023-10-31 14:23:48 +08:00
2023-10-31 12:28:27 +08:00
this.body = data
2020-09-16 16:01:46 +08:00
}
// 文件下载
2024-07-23 12:00:16 +08:00
sendfile(target, filename = 'untitled', expires = 3600) {
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
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
2024-07-23 12:00:16 +08:00
this.expires = expires
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.length = size
2023-10-31 12:28:27 +08:00
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)
}
this.length = data.length
2023-10-31 12:28:27 +08:00
this.body = data
2020-09-16 16:01:46 +08:00
}
2024-07-23 12:00:16 +08:00
load(file, type, expires = 24 * 3600) {
let stat = fs.stat(file)
if (stat.isFile()) {
let size = stat.size
let _type = type || file.split('.').pop() || 'stream'
2024-07-23 12:00:16 +08:00
this.expires = expires
this.type = _type
this.length = stat.size
return fs.origin.createReadStream(file).pipe(this.#res)
} else {
this.status = 404
this.body = null
}
}
2020-09-16 16:01:46 +08:00
/**
* [send json格式输出]
* @param {Num} code [返回码]
* @param {Str} msg [提示信息]
* @param {Str/Obj} data [额外数据]
*/
2023-10-31 14:23:48 +08:00
send(code = 200, msg = '', data) {
2023-10-31 12:28:27 +08:00
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') {
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
}
2023-10-31 14:23:48 +08:00
output = JSON.stringify({ code, msg, data })
2020-09-16 16:01:46 +08:00
2023-10-31 12:28:27 +08:00
this.type = 'json'
this.length = Buffer.byteLength(output)
2020-09-16 16:01:46 +08:00
// 只设置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
2023-10-31 14:23:48 +08:00
delete(key) {
this.#res.removeHeader(key)
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
}