Last active
December 21, 2015 05:28
-
-
Save refack/6256990 to your computer and use it in GitHub Desktop.
single file nodestrum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**package | |
{ | |
"name": "nodestrum", | |
"description": "Some cross cutting utils", | |
"version": "0.10.1", | |
"author": "Refael Ackermann<[email protected]>(http://node.org.il)", | |
"homepage": "https://gist.github.com/refack/6256990/", | |
"repository": "https://gist.githubusercontent.com/refack/6256990/raw/nodestrum.js", | |
"license": "MIT", | |
"readme": "Some crosscutting node.js utils" | |
} | |
**/ | |
'use strict'; | |
Error.stackTraceLimit = Infinity; | |
var util = require('util'); | |
var domain = require('domain'); | |
exports.isDebug = function (key) {return process.env.NODE_DEBUG && (~process.env.NODE_DEBUG.indexOf(key) || process.env.NODE_DEBUG[0] === '*');}, | |
exports.logFor = function (key) { | |
var logger = function () {arguments[0] = '\x1B[36m' + key + '\x1B[39m: ' + arguments[0]; console.log.apply(console, arguments)}; | |
logger.active = true; | |
return exports.isDebug(key) ? logger : function () {}; | |
}; | |
exports.catch_all_middleware = exports.CatchAllMiddleware = function CatchAllMiddleware(err, req, res, next) { | |
// I have to do something with "next", otherwise I don't get "err" | |
next = null; | |
console.error(err.stack); | |
res.send(500, '<pre>An unexpected error occurred! Please check logs.\n' + err.stack); | |
}; | |
exports.ConnectionCloser = function ConnectionCloser(req, res, next) { | |
res.set("Connection", "close"); | |
res.shouldKeepAlive = false; | |
return next(); | |
}; | |
exports.domain_wrapper_middleware = exports.DomainsMiddleware = function DomainsMiddleware(req, res, next) { | |
var d = exports.domain_wrapper([req, res]) | |
d.run(next); | |
}; | |
exports.domain_wrapper = exports.DomainWrapper = function DomainWrapper(objs) { | |
objs = objs || []; | |
var d = domain.create(); | |
d.on('error', function (err) { | |
var message = err.stack || err; | |
util.error('#### domain exception ####\n', message, '\n#### end ####'); | |
try { | |
var res = d.members.filter(function (o) {return o.constructor.name == 'ServerResponse';})[0]; | |
if (res._headerSent) return; | |
res.json(500, { | |
Status: -1, | |
Message: message | |
}); | |
} catch (e) { | |
util.error('#### no sure if sent 500 ####') | |
} | |
}); | |
objs.forEach(d.add.bind(d)) | |
return d; | |
}; | |
exports.register_process_catcher = exports.RegisterCatchAll = function RegisterCatchAll() { | |
process.on('uncaughtException', function (err) { | |
util.error('---- process exception ----\n', err.stack || err, '\n---- end ----'); | |
}); | |
}; | |
exports.escapeHTMLComponent = global.escapeHTMLComponent = util.deprecate(function (str) { return str == null ? '' : String(str).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g, '"').replace(/'/g, '''); }); | |
exports.encodeJSONHTML = global.encodeJSONHTML = util.deprecate(function (str) { return escapeHTMLComponent(JSON.stringify(str)); }); | |
exports.unescapeHTMLComponent = global.unescapeHTMLComponent = util.deprecate(function (str) { return str == null ? '' : String(str).replace(/\&\;/gi, '&').replace(/\<\;/gi, '<').replace(/\>\;/gi, '>').replace(/\"\;/gi, '"').replace(/\&\#039\;/gi, "'"); }); | |
exports.decodeJSONHTML = global.decodeJSONHTML = util.deprecate(function (str) { return JSON.parse(unescapeHTMLComponent(str)); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment