domain-context
Version:
Globally accessible domain-bound contexts
150 lines (136 loc) • 3.83 kB
JavaScript
// Generated by CoffeeScript 1.6.2
var domain;
domain = require('domain');
exports.context = function(context, currentDomain) {
if (currentDomain == null) {
currentDomain = domain.active;
}
if (currentDomain == null) {
throw new Error('no active domain');
}
return currentDomain.__context__ = context != null ? context() : {};
};
exports.cleanup = function(cleanup, context, currentDomain) {
if (context == null) {
context = null;
}
if (currentDomain == null) {
currentDomain = domain.active;
}
context = context || currentDomain.__context__;
if ((cleanup != null) && (context != null)) {
cleanup(context);
}
if (currentDomain != null) {
return currentDomain.__context__ = null;
}
};
exports.onError = function(err, onError, context, currentDomain) {
if (context == null) {
context = null;
}
if (currentDomain == null) {
currentDomain = domain.active;
}
context = context || currentDomain.__context__;
if (onError != null) {
onError(err, context);
}
return currentDomain.__context__ = null;
};
exports.get = function(key, currentDomain) {
if (currentDomain == null) {
currentDomain = domain.active;
}
if (currentDomain == null) {
throw new Error('no active domain');
}
return currentDomain.__context__[key];
};
exports.set = function(key, value, currentDomain) {
if (currentDomain == null) {
currentDomain = domain.active;
}
if (currentDomain == null) {
throw new Error('no active domain');
}
return currentDomain.__context__[key] = value;
};
exports.run = function(options, func) {
var cleanup, context, currentDomain, err, onError;
if (!func) {
func = options;
options = {};
}
context = options.context, cleanup = options.cleanup, onError = options.onError;
currentDomain = options.domain || domain.active;
if (!currentDomain) {
throw new Error('no active domain');
}
currentDomain.on('dispose', function() {
return exports.cleanup(cleanup, null, currentDomain);
});
currentDomain.on('error', function(err) {
if (onError != null) {
return exports.onError(err, onError, null, currentDomain);
} else {
return exports.cleanup(cleanup, null, currentDomain);
}
});
exports.context(context, currentDomain);
try {
currentDomain.bind(func, true)();
} catch (_error) {
err = _error;
currentDomain.emit('error', err);
}
return currentDomain;
};
exports.runInNewDomain = function(options, func) {
var currentDomain;
if (!func) {
func = options;
options = {};
}
currentDomain = domain.active;
options.domain = domain.create();
if (!options.detach && currentDomain) {
currentDomain.add(options.domain);
options.domain.on('error', function(err) {
return currentDomain.emit('error', err);
});
currentDomain.on('dispose', function() {
return options.domain.dispose();
});
}
return exports.run(options, func);
};
exports.middleware = function(context, cleanup) {
return function(req, res, next) {
var currentDomain, _ref;
if (typeof context !== 'function') {
_ref = context, context = _ref.context, cleanup = _ref.cleanup;
}
currentDomain = domain.active;
exports.context(context, currentDomain);
res.on('finish', function() {
return exports.cleanup(cleanup, null, currentDomain);
});
req.__context__ = currentDomain.__context__;
return next();
};
};
exports.middlewareOnError = function(onError) {
return function(err, req, res, next) {
if (typeof onError !== 'function') {
onError = onError.onError;
}
if (onError != null) {
exports.onError(err, onError, req.__context__);
} else {
exports.cleanup(onError, req.__context__);
}
req.__context__ = null;
return next(err);
};
};