UNPKG

deepify

Version:
107 lines (89 loc) 2.1 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.ResponseEvent = undefined; var _os = require('os'); var _os2 = _interopRequireDefault(_os); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class ResponseEvent { /** * * @param {Http.IncomingMessage} request * @param {Http.ServerResponse} response */ constructor(request, response) { this._propagationStopped = false; this._request = request; this._response = response; } /** * * @returns {Http.ServerResponse} */ get response() { return this._response; } /** * * @returns {Http.IncomingMessage} */ get request() { return this._request; } /** * * @param {Http.ServerResponse} response */ set response(response) { this._response = response; } /** * Stop event propagation * * @returns {ResponseEvent} */ stopPropagation() { this._propagationStopped = true; return this; } /** * * @returns {Boolean} */ get isPropagationStopped() { return this._propagationStopped; } /** * @param {Error|String} error */ send500(error) { this.send(`${error}${_os2.default.EOL}`, 500); } /** * @param {String} message */ send404(message = null) { this.send(message || `404 Not Found${_os2.default.EOL}`, 404); } /** * @param {String} content * @param {Number} code * @param {String} contentType * @param {Boolean} isBinary */ send(content, code = 200, contentType = 'text/plain', isBinary = false) { // setup CORS headers this.response.setHeader('Access-Control-Allow-Origin', '*'); this.response.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); this.response.writeHead(code, { 'Content-Type': contentType }); if (isBinary) { this.response.write(content, 'binary'); } else { this.response.write(content); } this.response.end(); this.stopPropagation(); } } exports.ResponseEvent = ResponseEvent;