@@ -4,6 +4,14 @@ var async = require('async'),
4
4
_ = require ( 'lodash' ) ,
5
5
logger = require ( './lib/logger' ) ;
6
6
7
+ /**
8
+ * Callback levels:
9
+ * - next: Top scope.
10
+ * - callback: Second scope.
11
+ * - cb: Third scope.
12
+ * ... After that, you're on your own.
13
+ */
14
+
7
15
async . auto ( {
8
16
environment : environment ,
9
17
validators : validators ,
@@ -15,7 +23,7 @@ async.auto({
15
23
routes : [ 'validators' , 'models' , 'httpd' , routes ]
16
24
} , complete ) ;
17
25
18
- function environment ( callback ) {
26
+ function environment ( next ) {
19
27
if ( ! process . env . SECRET ) {
20
28
logger . warn ( 'No $SECRET present. Generating a temporary random value.' ) ;
21
29
process . env . SECRET = require ( 'crypto' ) . randomBytes ( 256 ) ;
@@ -32,7 +40,7 @@ function environment(callback) {
32
40
logger . warn ( 'No PROVIDER_URL present. Defaulting to `https://localhost:8080`.' ) ;
33
41
process . env . PROVIDER_URL = 'https://localhost:8080' ;
34
42
}
35
- return callback ( null ) ;
43
+ return next ( null ) ;
36
44
}
37
45
38
46
/**
@@ -43,8 +51,8 @@ function certificate(next) {
43
51
var fs = require ( 'fs' ) ;
44
52
// Get the certificates.
45
53
async . auto ( {
46
- key : function ( next ) { fs . readFile ( 'cert/server.key' , 'utf8' , next ) ; } ,
47
- cert : function ( next ) { fs . readFile ( 'cert/server.crt' , 'utf8' , next ) ; }
54
+ key : function ( callback ) { fs . readFile ( 'cert/server.key' , 'utf8' , callback ) ; } ,
55
+ cert : function ( callback ) { fs . readFile ( 'cert/server.crt' , 'utf8' , callback ) ; }
48
56
} , function ( error , results ) {
49
57
if ( error ) { generateCertificate ( error , results , next ) ; }
50
58
else { return next ( error , results ) ; }
@@ -56,63 +64,62 @@ function certificate(next) {
56
64
* @param {Object|null } results - Passed to `next`.
57
65
* @param {Function } next - The callback. Is passed `error` (if not a certificate error) and `results`.
58
66
*/
59
- function generateCertificate ( error , results , next ) {
67
+ function generateCertificate ( error , results , callback ) {
60
68
process . env . NODE_TLS_REJECT_UNAUTHORIZED = "0" ; // Tell Node it's okay.
61
69
if ( error && error . code === 'ENOENT' ) {
62
70
logger . warn ( 'No certificates present in `cert/{server.key, server.crt}`. Generating a temporary certificate.' ) ;
63
71
require ( 'pem' ) . createCertificate ( { days : 1 , selfSigned : true } , function formatKey ( error , keys ) {
64
72
if ( error ) { return next ( error , null ) ; }
65
- return next ( null , { key : keys . serviceKey , cert : keys . certificate } ) ;
73
+ return callback ( null , { key : keys . serviceKey , cert : keys . certificate } ) ;
66
74
} ) ;
67
75
} else {
68
- return next ( error , results ) ;
76
+ return callback ( error , results ) ;
69
77
}
70
78
}
71
79
}
72
80
73
- function middleware ( callback , data ) {
81
+ function middleware ( next , data ) {
74
82
function providerRequest ( req , path , callback ) {
75
83
require ( 'request' ) . get ( { url : process . env . PROVIDER_URL + path , json : true } , function ( error , request , body ) {
76
84
callback ( error , request ) ;
77
85
} ) ;
78
86
// Callback should be (error, request)
79
87
}
80
- function populateVisualizationList ( req , res , next ) {
88
+ function populateVisualizationList ( req , res , callback ) {
81
89
// TODO: Cache this per user.
82
90
providerRequest ( req , '/api' , function validation ( error , request ) {
83
91
if ( error ) { return next ( error ) ; }
84
92
var validated = data . validators . list ( request . body ) ;
85
93
console . log ( validated . valid ) ;
86
94
if ( validated . valid === true ) {
87
95
req . visualizations = request . body . visualizations ;
88
- next ( ) ;
96
+ callback ( ) ;
89
97
} else {
90
- next ( new Error ( JSON . stringify ( validated , 2 ) ) ) ;
98
+ callback ( new Error ( JSON . stringify ( validated , 2 ) ) ) ;
91
99
}
92
100
} ) ;
93
101
}
94
- function populateVisualization ( req , res , next ) {
102
+ function populateVisualization ( req , res , callback ) {
95
103
if ( ! req . params . title ) { return res . redirect ( '/' ) ; }
96
104
providerRequest ( req , '/api/' + req . params . title , function validation ( error , request ) {
97
105
if ( error ) { return next ( error ) ; }
98
106
var validated = data . validators . item ( request . body ) ;
99
- console . log ( validated . valid ) ;
100
107
if ( validated . valid === true ) {
101
108
req . visualization = request . body ;
102
- next ( ) ;
109
+ callback ( ) ;
103
110
} else {
104
- next ( new Error ( JSON . stringify ( validated , 2 ) ) ) ;
111
+ callback ( new Error ( JSON . stringify ( validated , 2 ) ) ) ;
105
112
}
106
113
} ) ;
107
114
}
108
- return callback ( null , {
115
+ return next ( null , {
109
116
populateVisualizationList : populateVisualizationList ,
110
117
populateVisualization : populateVisualization ,
111
118
providerRequest : providerRequest
112
119
} ) ;
113
120
}
114
121
115
- function httpd ( callback , data ) {
122
+ function httpd ( next , data ) {
116
123
var server = require ( 'express' ) ( ) ;
117
124
// Set the server engine.
118
125
server . set ( 'view engine' , 'hbs' ) ;
@@ -141,34 +148,39 @@ function httpd(callback, data) {
141
148
// server.use(require('csurf')());
142
149
// Compresses responses.
143
150
server . use ( require ( 'compression' ) ( ) ) ;
144
- return callback ( null , server ) ;
151
+ return next ( null , server ) ;
145
152
}
146
153
147
- function database ( callback , data ) {
154
+ /**
155
+ * This task sets up MongoDB and will not invoke next until it has either connected or errored.
156
+ */
157
+ function database ( next , data ) {
148
158
var connection = require ( 'mongoose' ) . connect ( process . env . MONGO_URI ) . connection ;
159
+ // This is an event handler.
160
+ // The mongoose library emits events (open or error) when it connects (or fails to)
149
161
connection . on ( 'open' , function ( ) {
150
162
logger . log ( 'Connected to database on ' + process . env . MONGO_URI ) ;
151
- return callback ( null ) ;
163
+ return next ( null ) ;
152
164
} ) ;
153
165
connection . on ( 'error' , function ( error ) {
154
- return callback ( error , connection ) ;
166
+ return next ( error , connection ) ;
155
167
} ) ;
156
168
}
157
169
158
- function models ( callback , data ) {
159
- return callback ( null ) ;
170
+ function models ( next , data ) {
171
+ return next ( null ) ;
160
172
}
161
173
162
- function validators ( callback , data ) {
163
- var tv4 = require ( 'tv4' ) ,
174
+ function validators ( next , data ) {
175
+ var tv4 = require ( 'tv4' ) , // Node module: https://github.com/geraintluff/tv4
164
176
fs = require ( 'fs' ) ;
165
177
/**
166
178
* Creates validator functions for input.
167
179
* @param {String } file - The file path.
168
180
* @param {Function } callback - The callback.
169
181
*/
170
- function validatorFactory ( file , callback ) {
171
- fs . readFile ( './schema/list.json' , 'utf8' , function ( err , file ) {
182
+ function validatorFactory ( filePath , callback ) {
183
+ fs . readFile ( filePath , 'utf8' , function ( err , file ) {
172
184
if ( err ) { callback ( err , null ) ; }
173
185
/**
174
186
* Validates the data based on the schema.
@@ -182,18 +194,20 @@ function validators(callback, data) {
182
194
} ) ;
183
195
}
184
196
async . parallel ( {
197
+ // callbacks are implictly here
185
198
item : _ . partial ( validatorFactory , './schema/item.json' ) ,
186
199
list : _ . partial ( validatorFactory , './schema/list.json' )
187
200
} , function finish ( error , results ) {
188
- callback ( error , results ) ;
201
+ next ( error , results ) ;
202
+ // Results is [function validateItem(data), function validateList(data)]
189
203
} ) ;
190
204
}
191
205
192
- function routes ( callback , data ) {
206
+ function routes ( next , data ) {
193
207
var router = new require ( 'express' ) . Router ( ) ;
194
- router . get ( '/auth' , function ( req , res , next ) {
208
+ router . get ( '/auth' , function ( req , res , callback ) {
195
209
console . error ( "Not implemented yet" ) ;
196
- next ( ) ;
210
+ callback ( ) ;
197
211
} ) ;
198
212
router . get ( '/logout' , function ( req , res ) {
199
213
console . error ( "Logout called, but no auth implemented" ) ;
@@ -210,7 +224,7 @@ function routes(callback, data) {
210
224
}
211
225
) ;
212
226
router . get ( '/visualization/:title' ,
213
- function ( req , res , next ) { console . error ( "Auth not implemented yet." ) ; next ( ) ; } ,
227
+ function ( req , res , callback ) { console . error ( "Auth not implemented yet." ) ; callback ( ) ; } ,
214
228
data . middleware . populateVisualization ,
215
229
data . middleware . populateVisualizationList ,
216
230
function render ( req , res ) {
@@ -225,7 +239,7 @@ function routes(callback, data) {
225
239
) ;
226
240
// Attach the router.
227
241
data . httpd . use ( router ) ;
228
- callback ( null , router ) ;
242
+ next ( null , router ) ;
229
243
}
230
244
231
245
function complete ( error , data ) {
0 commit comments