123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- 'use strict';
- const util = require('util');
- const createError = require('http-errors');
- const httpAssert = require('http-assert');
- const delegate = require('delegates');
- const statuses = require('statuses');
- const Cookies = require('cookies');
- const COOKIES = Symbol('context#cookies');
- const proto = module.exports = {
-
- inspect() {
- if (this === proto) return this;
- return this.toJSON();
- },
-
- toJSON() {
- return {
- request: this.request.toJSON(),
- response: this.response.toJSON(),
- app: this.app.toJSON(),
- originalUrl: this.originalUrl,
- req: '<original node req>',
- res: '<original node res>',
- socket: '<original node socket>'
- };
- },
-
- assert: httpAssert,
-
- throw(...args) {
- throw createError(...args);
- },
-
- onerror(err) {
-
-
-
- if (null == err) return;
-
-
-
- const isNativeError =
- Object.prototype.toString.call(err) === '[object Error]' ||
- err instanceof Error;
- if (!isNativeError) err = new Error(util.format('non-error thrown: %j', err));
- let headerSent = false;
- if (this.headerSent || !this.writable) {
- headerSent = err.headerSent = true;
- }
-
- this.app.emit('error', err, this);
-
-
-
- if (headerSent) {
- return;
- }
- const { res } = this;
-
-
- if (typeof res.getHeaderNames === 'function') {
- res.getHeaderNames().forEach(name => res.removeHeader(name));
- } else {
- res._headers = {};
- }
-
- this.set(err.headers);
-
- this.type = 'text';
- let statusCode = err.status || err.statusCode;
-
- if ('ENOENT' === err.code) statusCode = 404;
-
- if ('number' !== typeof statusCode || !statuses[statusCode]) statusCode = 500;
-
- const code = statuses[statusCode];
- const msg = err.expose ? err.message : code;
- this.status = err.status = statusCode;
- this.length = Buffer.byteLength(msg);
- res.end(msg);
- },
- get cookies() {
- if (!this[COOKIES]) {
- this[COOKIES] = new Cookies(this.req, this.res, {
- keys: this.app.keys,
- secure: this.request.secure
- });
- }
- return this[COOKIES];
- },
- set cookies(_cookies) {
- this[COOKIES] = _cookies;
- }
- };
- if (util.inspect.custom) {
- module.exports[util.inspect.custom] = module.exports.inspect;
- }
- delegate(proto, 'response')
- .method('attachment')
- .method('redirect')
- .method('remove')
- .method('vary')
- .method('has')
- .method('set')
- .method('append')
- .method('flushHeaders')
- .access('status')
- .access('message')
- .access('body')
- .access('length')
- .access('type')
- .access('lastModified')
- .access('etag')
- .getter('headerSent')
- .getter('writable');
- delegate(proto, 'request')
- .method('acceptsLanguages')
- .method('acceptsEncodings')
- .method('acceptsCharsets')
- .method('accepts')
- .method('get')
- .method('is')
- .access('querystring')
- .access('idempotent')
- .access('socket')
- .access('search')
- .access('method')
- .access('query')
- .access('path')
- .access('url')
- .access('accept')
- .getter('origin')
- .getter('href')
- .getter('subdomains')
- .getter('protocol')
- .getter('host')
- .getter('hostname')
- .getter('URL')
- .getter('header')
- .getter('headers')
- .getter('secure')
- .getter('stale')
- .getter('fresh')
- .getter('ips')
- .getter('ip');
|