UNPKG

alpha-one

Version:

ideas about recurring tasks in Web- and Backend-Application building

111 lines (86 loc) 3.51 kB
// Generated by CoffeeScript 1.7.1 (function() { var TRM, alert, badge, debug, echo, help, info, log, rpr, warn, whisper; TRM = require('coffeenode-trm'); rpr = TRM.rpr.bind(TRM); badge = 'α1/HTTP'; log = TRM.get_logger('plain', badge); info = TRM.get_logger('info', badge); whisper = TRM.get_logger('whisper', badge); alert = TRM.get_logger('alert', badge); debug = TRM.get_logger('debug', badge); warn = TRM.get_logger('warn', badge); help = TRM.get_logger('help', badge); echo = TRM.echo.bind(TRM); this._add_options = function(request, response) { request['A1']['HTTP'] = { 'status': 200, 'headers': {} }; return null; }; this._get_http_options = function(request) { var R, _ref; R = (_ref = request['A1']) != null ? _ref['HTTP'] : void 0; if (R == null) { throw new Error("unable to write headers with no `add_request_options` middleware in stack"); } return R; }; this.write_header = function(request, response) { var HTTP, headers, status, _ref; HTTP = this._get_http_options(request); status = (_ref = HTTP['status']) != null ? _ref : 200; headers = HTTP['headers'] != null ? HTTP['headers'] : HTTP['headers'] = {}; if (headers['Content-Type'] == null) { headers['Content-Type'] = 'text/html'; } return response.writeHeader(status, headers); }; this.set_status = function(request, response, status, location) { var HTTP, headers; HTTP = this._get_http_options(request); headers = HTTP['headers'] != null ? HTTP['headers'] : HTTP['headers'] = {}; HTTP['status'] = status; if (location != null) { headers['location'] = location; } return null; }; this.redirect = function(request, response, location) { log(TRM.blue('redirect')); return this.set_status(request, response, 302, location); }; this.bounce = function(request, response, location) { /* Issue an HTTP 302 response redirecting the client to the given location. A (plain-text) cookie by the name of `comes-from` will be set that contains the route of the request that caused this redirect; this may later be used by `HTTP.back_to` to steer the client back to their original location, and is useful for login / signup and logout / signout scenarios, where the user may hit upon a restricted address, gets redirected to a login page, and then back to the first location. */ log(TRM.blue('bounce')); response.cookie('comes-from', request['url'], { path: '/' }); return this.redirect(request, response, location); }; this.back_to = function(request, response, location) { /* Like `bounce`, but reads a `comes-from` cookie to find the redirect location. If no cookie `comes-from` is found, the `location` argument is used instead; if no `location` argument was given, `/` is used. */ var _ref, _ref1, _ref2; log(TRM.blue('back_to')); location = (_ref = (_ref1 = (_ref2 = request['cookies']) != null ? _ref2['comes-from'] : void 0) != null ? _ref1 : location) != null ? _ref : '/'; return this.redirect(request, response, location); }; this.ok = function(request, response) { return this.set_status(request, response, 200); }; this.server_error = function(request, response) { return this.set_status(request, response, 500); }; this.not_found = function(request, response) { return this.set_status(request, response, 404); }; }).call(this);