This repository was archived by the owner on Jan 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.js
More file actions
147 lines (118 loc) · 4.77 KB
/
module.js
File metadata and controls
147 lines (118 loc) · 4.77 KB
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
var util = require('util');
var domain = require('domain');
var uuid = require('node-uuid');
var directorRouter = require('director').http.Router;
function cleanUpError(err) {
// this is just a lot of data that domain use internally
// but bunyan shouldn't care about it and some of them
// can't be stringified
delete err.domain;
delete err.domain_emitter;
delete err.domain_thrown;
return err;
}
function Router(config) {
if (!(this instanceof Router)) return new Router(config);
directorRouter.call(this);
// check config object
if (!config) throw new TypeError('first argument must be an object');
if (!config.logger) throw new TypeError('a bunyan logger must be specified');
if (!config.error) throw new TypeError('an error handler must be specified');
if (!config.fatal) throw new TypeError('a fatal handler must be specified');
// request/response spefic logic
this.attach(function () {
var self = this;
// get or generate a request UUID
var requestId = this.req.headers['X-Request-Id'] || uuid.v1();
this.res.setHeader('X-Request-Id', requestId);
// setup bunyan logger
this.log = config.logger.child({
'req_id': requestId,
'req': this.req,
'res': this.res
});
// this attach functions are executed syncronously, the active domain
// should be the domain relevant to this request/response
this.domain = domain.active;
this.domain.on('error', function (err) {
var errorDomain = domain.create();
errorDomain.once('error', function (handleError) {
handleError = cleanUpError(handleError);
// no more protection, do something fail safe PLEASE!
config.fatal.call(self, err, handleError);
// stops all I/O related to this request/response
errorDomain.dispose();
self.domain.dispose();
});
errorDomain.run(function () {
// Since a domain error event is in a uncaughtException handler
// scope, a sync error won't catched by domain. Thats we a try
// catch is nessarry here.
try {
// stops all I/O related to this request/response
// TODO: this should actually happen when the HTTP end
// sequence has been send (or the socket closes), since
// the underlying socket can be "keep-alive" and might
// never close.
self.res.once('close', function () {
self.domain.dispose();
});
// remove domain specific properties from the error object
err = cleanUpError(err);
// if no statusCode was assigned to the error, use 500
err.statusCode = err.statusCode || 500;
config.error.call(self, err);
} catch (e) {
process.emit('uncaughtException', e);
}
});
});
//
// as much as possibol should be below ths line,
// otherwise if an error throws, there will be no domain.on('error')
// handler to catch it.
//
// simple error helper
this.error = function (statusCode, err) {
// statusCode is optional
if (statusCode instanceof Error) {
err = statusCode;
} else {
err.statusCode = statusCode;
}
self.domain.emit('error', err);
};
});
}
util.inherits(Router, directorRouter);
module.exports = Router;
Router.prototype.configure = function (options) {
options = options || {};
// use streams by default
if (options.hasOwnProperty('stream') === false) {
options.stream = true;
}
// apply configure object
return directorRouter.prototype.configure.call(this, options);
};
Router.prototype.dispatch = function (req, res, callback) {
var self = this, args = arguments;
// create request/response domain
var d = domain.create();
// catch request and response errors and dispose them in case of error
d.add(req);
d.add(res);
// run router in current domain
return d.run(function () {
return directorRouter.prototype.dispatch.call(self, req, res, function (errInfo) {
// relay errors to the domain
if (errInfo) {
var err = new Error(errInfo.message);
err.statusCode = errInfo.status;
d.emit('error', err);
}
// relay callback info
if (callback) callback.apply(this, arguments);
});
});
};