/
bogart.js
637 lines (556 loc) · 16.5 KB
/
bogart.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
var
path = require('path'),
Q = require('promised-io/lib/promise'),
util = require('./util'),
Router = require('./router').Router,
middleware = require('./middleware'),
when = Q.when,
merge = require('./util').merge,
fs = require('fs'),
view = require('./view');
exports.version = [0,3,10];
exports.middleware = middleware;
/**
* A request to a bogart router
*/
function Request() {}
/**
* Creates a request object given a router and a jsgi request.
* This function is primarily intended to be used internally by bogart; however, it could be
* used by a third party library to compose bogart routers with its own handling mechanisms.
*
* @type Request
*/
exports.request = function(router, jsgiReq) {
var
search = util.extractSearch(jsgiReq),
requestedWith = jsgiReq.headers['x-requested-with'],
isxhr = !util.no(requestedWith), parsedBody,
contentType = jsgiReq.headers['content-type'],
req = Object.create(new Request(), {
router: { value: router, enumerable: true, readonly: true },
search: { value: search, enumerable: true, readonly: true },
isXMLHttpRequest: { value: isxhr, enumerable: true, readonly: true }
});
for (var k in jsgiReq) {
Object.defineProperty(req, k, { value: jsgiReq[k], readonly: true, enumerable: true });
}
return req;
};
/**
* Creates a @see ViewEngine
*
* Example:
* bogart.viewEngine("mustache", require("path").join(__dirname, "/templates"))
*
* @param {String} engineName the name of the engine, available: ["mustache", "haml"]
* @param {String} viewsPath Path where the views are located. Defaults to /views
* @member bogart
*/
exports.viewEngine = view.viewEngine;
/**
* Deprecated, use bogart#router instead
* @ignore
*/
exports.app = function(config, notFoundApp) {
console.log("app is deprecated, please use bogart.router");
return exports.router(config, notFoundApp);
};
/**
* Creates a bogart router. A router is responsible for routing requests to appropriate handlers.
*
* Example:
* bogart.router(function(get, post, put, del) {
* get('/', function() { return bogart.html('Hello World'); });
* });
*
* Alternative:
* var router = bogart.router();
* router.get('/', function() { return bogart.html('Hello World'); });
*
* @param {Function} config DSL configuration of routes.
* @param {Function} notFoundApp JSGI application to execute when no route from config matches the request.
*/
exports.router = function(config, notFoundApp) {
var
defaultNotFoundApp = function(req) {
var body = 'Not Found';
if (req.pathInfo) {
body += ': ' + req.pathInfo;
}
return {
status: 404,
body: [body],
headers: { 'Content-Length': body.length, 'Content-Type': 'text/html' }
};
},
router = new Router(config),
fn;
fn = function(req) {
var resp;
try {
respPromise = router.respond(exports.request(this, req));
if(util.no(respPromise) && req.pathInfo !== "/routes") {
if (notFoundApp) {
return notFoundApp(req);
}
else {
return defaultNotFoundApp(req);
}
}
if (util.no(respPromise) && req.pathInfo === '/routes') {
var str = 'GET<br />';
router.routes['get'].forEach(function(r) {
str += '<p>';
str += 'path: ' + r.path + '<br />' + 'paramNames: ' + r.paramNames;
str += '</p>';
});
return { status: 200, headers: { 'Content-Length': str.length, "Content-Type":"text/html" }, body: [ str ] };
}
return when(respPromise, function(resp) {
if (util.no(resp.status)) {
throw new Error('Response must have "status" property');
}
if (util.no(resp.body)) {
throw new Error('Response must have "body" property');
}
if (typeof resp.body.forEach !== 'function') {
throw new Error('Response "body" property must have a forEach method');
}
return resp;
});
} catch (err) {
var str = 'Error';
if (err && err.toString) {
str += '<br />'+err.toString();
}
if (err && err.stack) {
str += '<br />'+JSON.stringify(err.stack);
}
return exports.html(str, { status: 500 });
}
};
['get','post','put','del'].forEach(function(x) {
fn[x] = router[x].bind(router);
});
return fn;
};
/**
* Deprecated, use bogart#build instead
* @ignore
*/
exports.server = function(config) {
console.log("'bogart.server' is deprecated, please switch to 'bogart.build'");
return exports.build(config);
}
/**
* Utility class to help in creating stacks of JSGI applications.
* Allows the removal of nesting.
*
* @param {Function} config A configuration function that will be called by exports.build. The function will be
* be provided via its 'this' reference two functions: use, clear
*
* @returns {Function} A JSGI application that can be started using @see bogart#start
*/
exports.build = function(config) {
var
self = this,
app;
this.middleware = [];
this.use = function() {
this.middleware.push(Array.prototype.slice.call(arguments));
};
this.clear = function() {
this.middleware = [];
};
this.use(middleware.ParseForm);
this.use(middleware.ParseJson);
config.call(this);
this.middleware = this.middleware.reverse();
this.middleware.forEach(function(middle) {
var callable = middle.shift();
middle.push(app);
app = callable.apply(self, middle);
});
return function(req) {
return app(req);
};
};
/**
* Starts a server
*
* @param {Function} jsgiApp JSGI application to run
* @param {Object} options Options hash. Supports 'port' property which allows specification of port for server.
* Port defaults to 8080. More options are planned for the future.
*/
exports.start = function(jsgiApp, options) {
return require("jsgi").start(jsgiApp, options);
};
/**
* Text response. Bogart helper method to create a JSGI response.
* Returns a default JSGI response with body containing the specified text, a status of 200,
* and headers. The headers included are "content-type" of "text" and "content-length" set
* appropriately based upon the length of 'txt' parameter.
*
* @param {String} txt Text for the body of the response.
*/
exports.text = function(txt) {
return {
status: 200,
body: [txt],
headers: { "content-type": "text", "content-length": txt.length }
};
};
/**
* HTML response. Bogart helper method to create a JSGI response.
* Returns a default JSGI response with body containing the specified html, a status of 200,
* and headers. The headers included are "content-type" of "text/html" and "content-length" set
* appropriately based upon the length of the 'html' parameter.
*
* @param {String} html HTML for the body of the response
* @param {Object} opts Options to override JSGI response defaults. For example, passing { status: 404 } would
* cause the resulting JSGI response's status to be 404.
*
* @returns JSGI Response
* @type Object
*/
exports.html = function(html, opts) {
opts = opts || {};
html = html || "";
return {
status: opts.status || 200,
body: [html],
headers: { "content-type": "text/html", "content-length": html.length }
};
};
/**
* Bogart helper function to create a JSGI response.
* Returns a default JSGI response with body containing the specified object represented as JSON, a status of 200,
* and headers. The headers included are "content-type" of "application/json" and "content-length" set
* appropriately based upon the length of the JSON representation of @paramref(obj)
*
* var resp = bogart.json({ a: 1});
* sys.inspect(resp)
*
* Assuming node-style sys.inspect, evalutes to:
* {
* status: 200,
* headers: { "content-type": "application/json", "content-length": 5 },
* body: [ "{a:1}" ]
* }
*
*
* @param {Object} obj Object to be represented as JSON
* @param {Object} opts Options to override JSGI response defaults. For example, passing {status: 404 } would
* cause the resulting JSGI response's status to be 404.
*/
exports.json = function(obj, opts) {
opts = opts || {};
var str = JSON.stringify(obj);
return {
status: opts.status || 200,
body: [str],
headers: { "content-type": "application/json", "content-length": str.length }
};
};
exports.error = function(msg, opts) {
opts = opts || {};
msg = msg || "Server Error";
return {
status: opts.status || 500,
body: [msg],
headers: { "content-type": "text/html", "content-length": msg.length }
};
};
/**
* Bogart helper function to create a JSGI response.
* Returns a default JSGI response the redirects to the url provided by the 'url' parameter.
*
* var resp = bogart.redirect("http://google.com");
* sys.inspect(resp)
*
* Assuming node-style sys.inspect, evalutes to:
* {
* status: 302,
* headers: { "location": "http://google.com" },
* body: []
* }
*
*
* @param {String} url URL to which the JSGI response will redirect
* @returns JSGI response for a 302 redirect
* @type Object
*/
exports.redirect = function(url) {
return {
status: 302,
headers: { "location": url },
body: []
};
};
/**
* Bogart helper function to create a JSGI response.
* Returns a default JSGI response the redirects to the url provided by the 'url' parameter.
*
* var resp = bogart.permanentRedirect("http://google.com");
* sys.inspect(resp)
*
* Assuming node-style sys.inspect, evalutes to:
* {
* status: 301,
* headers: { "location": "http://google.com" },
* body: []
* }
*
*
* @param {String} url URL to which the JSGI response will redirect
* @returns JSGI response for a permanent (301) redirect
* @type Object
*/
exports.permanentRedirect = function(url){
return {
status:301,
headers: {"location": url},
body: []
};
};
/**
* Bogart helper function to create a JSGI response.
* Returns a default JSGI response with a status of 304 (not modified).
*
* var resp = bogart.notModified();
* sys.inspect(resp)
*
* Assuming node-style sys.inspect, evalutes to:
* {
* status: 304,
* body: []
* }
*
* @returns JSGI response for a not modified response (304).
* @type Object
*/
exports.notModified = function(){
return {
status: 304,
body:[]
};
};
/*
* @deprecated
*/
exports.stream = function() {
console.log('the stream method is deprecated and will be removed in Bogart 0.4.0');
var
deferred = Q.defer(),
buffer = [],
streamer = function(progress) {
deferred.progress(progress);
};
streamer.end = function() {
deferred.resolve();
};
streamer.respond = function(opts) {
opts = opts || {};
opts.status = opts.status || 200;
opts.headers = opts.headers || { "Content-Type": "text/plain" };
return {
status: opts.status,
body: {
forEach: function(cb) {
when(deferred, function success() {
}, function error() {
}, function(update) {
cb(update);
});
return deferred;
}
},
headers: opts.headers
};
};
return streamer;
};
var ResponseBuilder = exports.ResponseBuilder = function(viewEngine) {
var responseBuilder = Object.create(Q.defer())
, forEachCallback
, forEachDeferred = Q.defer()
, response = { headers: {}, status: 200, body: {} }
, waiting = [];
var send = function(data) {
if (typeof data === 'string') {
forEachCallback(data);
} else if (typeof data.forEach === 'function') {
data.forEach(forEachCallback);
} else {
forEachCallback(data);
}
};
/**
* Send response data
*
* Examples:
*
* res.send('Hello World');
* res.send([ 'Hello', 'World' ]);
* res.send(new Buffer('Hello World'));
*
* @param {String | ForEachable | Buffer} data Data to send
* @api public
*/
responseBuilder.send = function(data) {
if (typeof forEachCallback === 'function') {
send(data);
} else {
waiting.push(data);
}
};
/**
* Render a `view` to the response stream.
*
* Example:
*
* res.render('index.html', { locals: { title: 'Hello World' } });
*
* @param {String} view The view to render
* @param {Object} opts Options for the ViewEngine.
*/
responseBuilder.render = function(view, opts) {
if (!viewEngine) {
throw "No viewEngine specified"
}
viewEngine.render(view, opts).then(function(content) {
responseBuilder.send(content);
responseBuilder.end();
})
};
/**
* End the response.
*/
responseBuilder.end = function() {
this.resolve(response);
forEachDeferred.resolve();
};
responseBuilder.headers = function(headers) {
response.headers = headers;
};
responseBuilder.status = function(status) {
response.status = status;
};
response.body.forEach = function(callback) {
forEachCallback = callback;
if (waiting.length > 0) {
waiting.forEach(send);
waiting = [];
}
return forEachDeferred.promise;
};
return responseBuilder;
};
/**
* Retrieve a ResponseBuilder to build a JSGI response imperatively.
*
* var viewEngine = bogart.viewEngine('mustache');
* app.get('/', function(req) {
* var resp = bogart.response(viewEngine);
*
* doSomethingAsync(function(err, str) {
* if (err) {
* resp.status(500);
* resp.send('Error: '+err.reason);
* } else {
* resp.send(str);
* }
* resp.end();
* });
*
* return resp;
* });
*
* @param {ViewEngine} viewEngine The ViewEngine to be used by response helpers for rendering views.
* @returns {ResponseBuilder} An object with methods to help build a response.
*/
exports.response = function(viewEngine) {
return new ResponseBuilder(viewEngine);
};
function pipeStream(stream, opts) {
var response = exports.response();
opts = opts || {};
if (!stream.readable) {
throw "Streams passed to pipe must be readable streams."
}
if (opts.status) {
response.status(opts.status);
}
if (opts.headers) {
response.headers(opts.headers);
}
stream.on('data', function(data) {
response.send(data);
});
stream.on('end', function() {
response.end();
});
stream.on('error', function(err) {
response.reject(err);
});
return response.promise;
};
/**
* Pipe a response to a JSGI stream
*
* @param {ReadableStream} stream A readable stream.
* @returns {Promise} A promise for a JSGI stream.
*/
exports.pipe = function(stream, opts) {
var deferred = Q.defer();
if (typeof stream.forEach === 'function') {
deferred.resolve(merge({}, opts, { body: stream }));
return deferred.promise;
} else {
return pipeStream(stream, opts);
}
};
/**
* Get MIME type for a file extension
*
* @param {String} ext File extension
* @returns {String} MIME type of file extension.
*/
exports.mimeType = function(ext) {
return require("./mimetypes").mimeType(ext);
};
/**
* Creates a JSGI response that streams a file
*
* @param {String} filePath The path to the file to be streamed
* @param {Object} opts JSGI options
*
* @returns {Promise} A promise for a JSGI response
*/
exports.file = function(filePath, opts) {
opts = opts || {};
opts.headers = opts.headers || {};
opts.headers['Content-Type'] = opts.headers['Content-Type'] || exports.mimeType(path.extname(filePath));
return exports.pipe(fs.createReadStream(filePath), opts);
};
/**
* Helper function to determine the main directory of the application
* @returns {String} Directory of the script that was executed
*/
exports.maindir = function() {
if (typeof require.main === 'undefined') {
return __dirname;
}
return path.dirname(require.main.filename).replace("file://","");
};
view.setting('template directory', path.join(exports.maindir(), 'views'));
/**
* Pipes data from source to dest.
*
* @param {ForEachable | ReadableStream} src Source of data
* @param {WriteableStream} dest Write data from src to dest
*
* @returns {Promise} A promise that will be resolved when the pumping is completed
*/
exports.pump = require('./stream').pump;