forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.js
174 lines (131 loc) · 4.15 KB
/
load.js
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
module.exports = function (sails) {
/**
* Module dependencies.
*/
var async = require('async'),
util = require('sails-util'),
Configuration = require('../configuration')(sails),
MiddlewareRegistry = require('../middleware')(sails),
Router = require('../router')(sails),
initializeHooks = require('./loadHooks')(sails);
/**
* Expose loader start point.
* (idempotent)
*
* @api public
*/
return function load (configOverride, cb) {
// configOverride is optional
if (util.isFunction(configOverride)) {
cb = configOverride;
configOverride = {};
}
// Ensure override is an object and clone it (or make an empty object if it's not)
configOverride = configOverride || {};
sails.config = util.cloneDeep(configOverride);
// If host is explicitly specified, set `explicitHost`
// (otherwise when host is omitted, Express will accept all connections via INADDR_ANY)
if (configOverride.host) {
configOverride.explicitHost = configOverride.host;
}
async.auto({
// Get configuration modules into memory
config: [ Configuration.load ],
// Load hooks into memory, with their middleware and routes
hooks: [ 'config', loadHooks ],
// Populate the middleware registry
middleware: [ 'hooks', MiddlewareRegistry.load ],
// Build and configure the router using config + hooks
router: [ 'hooks', 'middleware', Router.load ]
}, ready__(cb) );
};
/**
* Load hooks in parallel
* let them work out dependencies themselves,
* taking advantage of events fired from the sails object
*
* @api private
*/
function loadHooks (cb) {
sails.hooks = {};
// If config.hooks is disabled, skip hook loading altogether
if ( !sails.config.hooks ) {
return cb();
}
async.series([
function (cb) { loadHookDefinitions(sails.hooks, cb); },
function (cb) { initializeHooks(sails.hooks, cb); }
], function (err) {
if (err) return cb(err);
// Inform any listeners that the initial, built-in hooks
// are finished loading
sails.emit('hooks:builtIn:ready');
sails.log.verbose('Built-in hooks are ready.');
return cb();
});
}
/**
* Load built-in hook definitions from `sails.config.hooks`
* and put them back into `hooks` (probably `sails.hooks`)
*
* @api private
*/
function loadHookDefinitions (hooks, cb) {
// Mix in user-configured hook definitions
util.extend(hooks, sails.config.hooks);
// If user configured `loadHooks`, only include those.
if ( sails.config.loadHooks ) {
if ( ! util.isArray(sails.config.loadHooks) ) {
return cb('Invalid `loadHooks` config. '+
'Please specify an array of string hook names.\n' +
'You specified ::' + util.inspect(sails.config.loadHooks) );
}
util.each(hooks, function (def, hookName) {
if ( !util.contains(sails.config.loadHooks, hookName) ){
hooks[hookName] = false;
}
});
sails.log.verbose('Deliberate partial load-- will only initialize hooks ::', sails.config.loadHooks);
}
return cb();
}
/**
* Returns function which is fired when Sails is ready to go
*
* @api private
*/
function ready__ (cb) {
return function (err) {
if (err) {
sails.log.error('Sails encountered the following internal error:');
sails.log.error(err);
return cb(err);
}
// Wait until all hooks are ready
sails.log.verbose('Waiting for all hooks to declare that they\'re ready...');
var hookTimeout = setTimeout(function tooLong (){
var hooksTookTooLongErr = 'Hooks are taking way too long to get ready... ' +
'Something is amiss.\nAre you using any custom hooks?\nIf so, make sure the hook\'s ' +
'`initialize()` method is triggering it\'s callback.';
sails.log.error(hooksTookTooLongErr);
process.exit(1);
}, 10000);
async.whilst(
function checkIfAllHooksAreReady () {
return util.any(sails.hooks, function (hook) {
return !hook.ready;
});
},
function waitABit (cb) {
setTimeout(cb, 150);
},
function hooksLoaded (err) {
clearTimeout(hookTimeout);
if (err) return cb('Error loading hooks.');
sails.log.verbose('All hooks were loaded successfully.');
cb(null, sails);
}
);
};
}
};