1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | 7x 4x 36x 36x 36x 36x 36x 36x 36x 33x 33x 33x 33x 33x 33x 33x 33x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 2x 2x 1x 1x 2x 7x | import ns from '../namespace'; import GenericError from '../error/GenericError'; ns.namespace('ima.router'); /** * Wrapper for the ExpressJS response, exposing only the necessary minimum. */ export default class Response { static get $dependencies() { return []; } /** * Initializes the response. */ constructor() { /** * The ExpressJS response object, or {@code null} if running at the * client side. * * @type {?Express.Response} */ this._response = null; /** * It is flag for sent response for request. * * @type {boolean} */ this._isSent = false; /** * HTTP Status code. * * @type {number} */ this._status = 500; /** * The content of response. * * @type {string} */ this._content = ''; /** * The rendered page state. * * @type {Object<string, *>} */ this._pageState = {}; /** * Internal cookie storage for Set-Cookie header. * * @type {Map<string, { * value: string, * options: {domain: string=, expires: (number|string)=} * }>} */ this._internalCookieStorage = new Map(); /** * Transform function for cookie value. * * @type {{encode: function, decode: function}} */ this._cookieTransformFunction = { encode: value => value, decode: value => value }; } /** * Initializes this response wrapper with the provided ExpressJS response * object. * * @param {?Express.Response} response The ExpressJS response, or * {@code null} if the code is running at the client side. * @param {{ * encode: function(string): string=, * decode: function(string): string * }=} cookieTransformFunction * @return {ima.router.Response} This response. */ init(response, cookieTransformFunction = {}) { this._cookieTransformFunction = Object.assign( this._cookieTransformFunction, cookieTransformFunction ); this._response = response; this._isSent = false; this._status = 500; this._content = ''; this._pageState = {}; this._internalCookieStorage = new Map(); return this; } /** * Redirects the client to the specified location, with the specified * redirect HTTP response code. * * For full list of HTTP response status codes see * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html * * Use this method only at the server side. * * @param {string} url The URL to which the client should be redirected. * @param {number=} [status=302] The HTTP status code to send to the * client. * @return {Response} This response. */ redirect(url, status = 302) { if ($Debug) { if (this._isSent === true) { let params = this.getResponseParams(); params.url = url; throw new GenericError( 'ima.router.Response:redirect The response has already ' + 'been sent. Check your workflow.', params ); } } this._isSent = true; this._status = status; this._setCookieHeaders(); this._response.redirect(status, url); return this; } /** * Sets the HTTP status code that will be sent to the client when the * response is sent. * * For full list of available response codes see * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html * * Use this method only at the server side. * * @param {number} httpStatus HTTP response status code to send to the * client. * @return {Response} This response. */ status(httpStatus) { Eif ($Debug) { Iif (this._isSent === true) { let params = this.getResponseParams(); throw new GenericError( 'ima.router.Response:status The response has already ' + 'been sent. Check your workflow.', params ); } } this._status = httpStatus; this._response.status(httpStatus); return this; } /** * Sends the response to the client with the provided content. Use this * method only at the server side. * * @param {string} content The response body. * @return {Response} This response. */ send(content) { Eif ($Debug) { Iif (this._isSent === true) { let params = this.getResponseParams(); params.content = content; throw new GenericError( 'ima.router.Response:send The response has already been ' + 'sent. Check your workflow.', params ); } } this._isSent = true; this._content = content; this._setCookieHeaders(); this._response.send(content); return this; } /** * Sets the rendered page state. * * @param {Object<string, *>} pageState The rendered page state. * @return {Response} This response. */ setPageState(pageState) { Eif ($Debug) { Iif (this._isSent === true) { let params = this.getResponseParams(); params.pageState = pageState; throw new GenericError( 'ima.router.Response:setState The response has already ' + 'been sent. Check your workflow.', params ); } } this._pageState = pageState; return this; } /** * Sets a cookie, which will be sent to the client with the response. * * @param {string} name The cookie name. * @param {(boolean|number|string)} value The cookie value, will be * converted to string. * @param {{domain: string=, expires: (number|string)=, maxAge: number=}} * options Cookie attributes. Only the attributes listed in the type * annotation of this field are supported. For documentation and full * list of cookie attributes * see http://tools.ietf.org/html/rfc2965#page-5 * @return {Response} This response. */ setCookie(name, value, options = {}) { if ($Debug) { if (this._isSent === true) { let params = this.getResponseParams(); params.name = name; params.value = value; params.options = options; throw new GenericError( 'ima.router.Response:setCookie The response has already ' + 'been sent. Check your workflow.', params ); } } let advancedOptions = Object.assign( {}, this._cookieTransformFunction, options ); this._internalCookieStorage.set(name, { value, options: advancedOptions }); return this; } /** * Return object which contains response status, content and rendered * page state. * * @return {{status: number, content: string, pageState: Object<string, *>}} */ getResponseParams() { return { status: this._status, content: this._content, pageState: this._pageState }; } /** * Return true if response is sent from server to client. * * @return {boolean} */ isResponseSent() { return this._isSent; } /** * Set cookie headers for response. */ _setCookieHeaders() { for (let [name, param] of this._internalCookieStorage) { let options = this._prepareCookieOptionsForExpress(param.options); this._response.cookie(name, param.value, options); } } /** * Prepares cookie options for Express. * * @param {{domain: string=, expires: (number|string)=, maxAge: number=}} * options Cookie attributes. Only the attributes listed in the type * annotation of this field are supported. For documentation and full * list of cookie attributes * see http://tools.ietf.org/html/rfc2965#page-5 * @return {Object} Cookie options prepared for Express. */ _prepareCookieOptionsForExpress(options) { let expressOptions = Object.assign({}, options); if (typeof expressOptions.maxAge === 'number') { expressOptions.maxAge *= 1000; } else { delete expressOptions.maxAge; } return expressOptions; } } ns.ima.router.Response = Response; |