Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
set body(val) {
const original = this._body;
this._body = val;
if (this.res.headersSent) return;
// no content
if (null == val) {
if (!statuses.empty[this.status]) this.status = 204;
this.remove('Content-Type');
this.remove('Content-Length');
this.remove('Transfer-Encoding');
return;
}
// set the status
if (!this._explicitStatus) this.status = 200;
// set the content-type only if not yet set
const setType = !this.header['content-type'];
// string
if ('string' == typeof val) {
if (setType) this.type = /^\s*
return async (ctx, next) => {
ctx.vary('Accept-Encoding')
await next()
let { body } = ctx
if (!body) return
if (ctx.res.headersSent || !ctx.writable) return
if (ctx.compress === false) return
if (ctx.request.method === 'HEAD') return
if (status.empty[ctx.response.status]) return
if (ctx.response.get('Content-Encoding')) return
// forced compression or implied
if (!(ctx.compress === true || filter(ctx.response.type))) return
// identity
const encoding = ctx.acceptsEncodings('gzip', 'deflate', 'identity')
if (!encoding) ctx.throw(406, 'supported encodings: gzip, deflate, identity')
if (encoding === 'identity') return
// json
if (isJSON(body)) body = ctx.body = JSON.stringify(body)
// threshold
if (threshold && ctx.response.length < threshold) return
function respond(ctx) {
// allow bypassing koa
if (false === ctx.respond) return;
const res = ctx.res;
if (!ctx.writable) return;
let body = ctx.body;
const code = ctx.status;
// ignore body
if (statuses.empty[code]) {
// strip headers
ctx.body = null;
return res.end();
}
if ('HEAD' == ctx.method) {
if (!res.headersSent && isJSON(body)) {
ctx.length = Buffer.byteLength(JSON.stringify(body));
}
return res.end();
}
// status body
if (null == body) {
body = ctx.message || String(code);
if (!res.headersSent) {
const headers = response.headers || {}
res.statusCode = response.status
res.statusMessage = statuses[response.status] || res.statusMessage
// Using for..in here, since it's faster than Object.keys()
for (const headerName in headers) {
const headerValue = headers[headerName]
if (typeof headerValue !== "undefined") {
res.setHeader(headerName, headerValue)
}
}
if (body && statuses.empty[response.status]) {
debug(Error(`Warning: Response body defined on a ${response.status} response.`))
} else if (!body && !statuses.empty[response.status]) {
debug(Error(`Warning: No response body defined on a ${response.status} response.`))
}
if (Buffer.isBuffer(body)) {
return res.end(body)
} else if (isStream(body)) {
return body.pipe(res)
} else if (body) {
throw Error(`Unexpected Response.body: ${body}`)
} else {
return res.end()
}
}
export function applyResponseTo(response: Response, res: http.ServerResponse) {
const body = response.body
const headers = response.headers || {}
res.statusCode = response.status
res.statusMessage = statuses[response.status] || res.statusMessage
// Using for..in here, since it's faster than Object.keys()
for (const headerName in headers) {
const headerValue = headers[headerName]
if (typeof headerValue !== "undefined") {
res.setHeader(headerName, headerValue)
}
}
if (body && statuses.empty[response.status]) {
debug(Error(`Warning: Response body defined on a ${response.status} response.`))
} else if (!body && !statuses.empty[response.status]) {
debug(Error(`Warning: No response body defined on a ${response.status} response.`))
}
if (Buffer.isBuffer(body)) {
return res.end(body)
} else if (isStream(body)) {
return body.pipe(res)
} else if (body) {
throw Error(`Unexpected Response.body: ${body}`)
} else {
return res.end()
}
}
// Skip request ended externally.
if (original.headersSent) return
// Apply default statuses.
if (Number(res.status) === 404) {
// Ensure redirect status.
if (res.get('Location')) res.status = 302
// Default the status to 200 if there is substance to the response.
else if (body) res.status = 200
}
// Default status message based on status code.
res.message = res.message || statuses[res.status]
// Ensure no content-type for empty responses.
if (req.method === 'HEAD' || statuses.empty[res.status] || !body) {
body = null
res.remove('Content-Type')
res.remove('Content-Length')
} else {
// Attempt to guess content type.
if (!res.get('Content-Type')) res.set('Content-Type', checkType(body))
// Stringify objects that are not buffers.
if (typeof body === 'object' && !isStream && !isBuffer) body = JSON.stringify(body)
// Attempt to guess content-length.
if (!res.get('Content-Length') && !isStream) res.set('Content-Length', byteLength(body))
}
// Send off headers.
original.writeHead(res.status, res.message, removeEmptyHeaders(res.headers))
// Allow for requests to stay open.
if (res.end === false) return
set status(code) {
if (this.headerSent) return;
assert(Number.isInteger(code), 'status code must be a number');
assert(code >= 100 && code <= 999, `invalid status code: ${code}`);
this._explicitStatus = true;
this.res.statusCode = code;
if (this.req.httpVersionMajor < 2) this.res.statusMessage = statuses[code];
if (this.body && statuses.empty[code]) this.body = null;
},
set status(code) {
assert('number' == typeof code, 'status code must be a number');
assert(statuses[code], `invalid status code: ${code}`);
assert(!this.res.headersSent, 'headers have already been sent');
this._explicitStatus = true;
this.res.statusCode = code;
this.res.statusMessage = statuses[code];
if (this.body && statuses.empty[code]) this.body = null;
},
set status(code) {
assert('number' == typeof code, 'status code must be a number');
assert(statuses[code], `invalid status code: ${code}`);
assert(!this.res.headersSent, 'headers have already been sent');
this._explicitStatus = true;
this.res.statusCode = code;
this.res.statusMessage = statuses[code];
if (this.body && statuses.empty[code]) this.body = null;
},
return function* compressor(next) {
yield* next
var body = this.response.body
if (!body) return
if (this.request.method === 'HEAD') return
if (status.empty[this.response.status]) return
if (this.response.get('Content-Encoding')) return
if (!filter(this.response.type)) return
if (isJSON(body)) body = this.body = JSON.stringify(body)
if (this.response.length < threshold) return
this.response.set('Content-Encoding', 'gzip')
this.response.remove('Content-Length')
this.body = typeof body.pipe === 'function'
? body.pipe(zlib.Gzip(options))
: (yield zlib.gzip(body))
}
}