hoagie
Advanced tools
Comparing version 2.4.0 to 3.0.0
@@ -18,4 +18,4 @@ var Server = require('./lib/server'); | ||
app.request = { app: app, __proto__: hoagie.Request.prototype }; | ||
app.response = { app: app, __proto__: hoagie.Response.prototype }; | ||
app.request = { app: app, __proto__: hoagie.Request }; | ||
app.response = { app: app, __proto__: hoagie.Response }; | ||
app.init(); | ||
@@ -22,0 +22,0 @@ |
@@ -5,2 +5,3 @@ var Router = require('./router'); | ||
var fs = require('fs'); | ||
var debug = require('debug')('hoagie:application'); | ||
@@ -79,2 +80,5 @@ /** | ||
exports.handle = function(req, res, done) { | ||
done = done || function() { | ||
debug('end'); res.end(); | ||
}; | ||
this._router.handle(req, res, done); | ||
@@ -81,0 +85,0 @@ }; |
@@ -0,12 +1,27 @@ | ||
var merge = require('merge-descriptors'); | ||
var parse = require('minimist'); | ||
/** | ||
* Inserts this app's request and response prototypes | ||
* into the prototype chain for this request. | ||
* into the prototype chain for this request. Also provides | ||
* the params array/hash on the request. | ||
* @returns {Function} | ||
*/ | ||
module.exports = function init(app) { | ||
return function(req, res, next) { | ||
module.exports = function (app) { | ||
return function init(req, res, next) { | ||
req.__proto__ = app.request; | ||
res.__proto__ = app.response; | ||
if (!req.params) { | ||
req.params = extend(parse(req.argv)); | ||
} | ||
next(); | ||
}; | ||
function extend(argv) { | ||
return merge(argv._, argv); | ||
} | ||
}; |
@@ -1,32 +0,3 @@ | ||
var minimist = require('minimist'); | ||
var stream = require('stream'); | ||
var merge = require('merge-descriptors'); | ||
var util = require('util'); | ||
/** | ||
* Creates a new Request instance. Requests are readable streams | ||
* that delegate to `stdin` | ||
* @param {Array} argv The argv array | ||
* @param {stream.Readable} stdin The standard input stream | ||
* @constructor | ||
*/ | ||
function Request(argv, stdin) { | ||
stream.Readable.call(this); | ||
this.stdin = stdin; | ||
var opts = minimist(argv); | ||
this.params = merge(opts._, opts); | ||
} | ||
util.inherits(Request, stream.Readable); | ||
/** | ||
* @api private | ||
*/ | ||
Request.prototype._read = function(size) { | ||
this.push(this.stdin.read(size)); | ||
exports = module.exports = { | ||
__proto__: process.stdin.__proto__ | ||
}; | ||
@@ -40,3 +11,3 @@ | ||
Request.prototype.get = function(name) { | ||
exports.get = function(name) { | ||
return process.env[name]; | ||
@@ -50,3 +21,3 @@ }; | ||
Request.prototype.__defineGetter__('program', function() { | ||
exports.__defineGetter__('program', function() { | ||
return this.app.get('program'); | ||
@@ -61,6 +32,4 @@ }); | ||
Request.prototype.__defineGetter__('command', function() { | ||
exports.__defineGetter__('command', function() { | ||
return this.params[0]; | ||
}); | ||
module.exports = Request; |
@@ -1,28 +0,19 @@ | ||
var stream = require('stream'); | ||
var util = require('util'); | ||
/** | ||
* Creates a new Response instance. Responses are writable streams | ||
* that delegate to `stdout` | ||
* @param {stream.Writable} stdout The standard output stream | ||
* @constructor | ||
*/ | ||
exports = module.exports = { | ||
__proto__: process.stdout.__proto__ | ||
}; | ||
function Response(stdout) { | ||
stream.Writable.call(this); | ||
this.stdout = stdout; | ||
this.exitCode = 0; | ||
} | ||
util.inherits(Response, stream.Writable); | ||
/** | ||
* @api private | ||
* Since process.stdout can't end, use this to signal the end of | ||
* the program's output stream, optionally writing `chunk` to | ||
* the output before exiting. | ||
*/ | ||
Response.prototype._write = function(chunk, encoding, callback) { | ||
return this.stdout.write(chunk, encoding, callback); | ||
}; | ||
exports.end = format(function(chunk) { | ||
if (chunk) { | ||
this.write(chunk); | ||
} | ||
this.emit('exit'); | ||
}); | ||
@@ -35,8 +26,5 @@ /** | ||
Response.prototype.writeln = function(chunk) { | ||
if (arguments.length > 1) { | ||
return this.write(format(arguments) + '\n'); | ||
} | ||
exports.writeln = format(function(chunk) { | ||
return this.write(chunk + '\n'); | ||
}; | ||
}); | ||
@@ -49,8 +37,5 @@ /** | ||
Response.prototype.send = function(chunk) { | ||
if (arguments.length > 1) { | ||
return this.end(format(arguments) + '\n'); | ||
} | ||
exports.send = format(function(chunk) { | ||
return this.end(chunk + '\n'); | ||
}; | ||
}); | ||
@@ -63,3 +48,3 @@ /** | ||
Response.prototype.code = function(code) { | ||
exports.code = function(code) { | ||
this.exitCode = code; | ||
@@ -79,3 +64,3 @@ return this; | ||
Response.prototype.render = function(file, locals) { | ||
exports.render = function(file, locals) { | ||
return this.end(this.app.render(file, locals)); | ||
@@ -85,20 +70,13 @@ }; | ||
/** | ||
* Overwrite .isTTY to delegate to stdout. | ||
* @returns {true|undefined} | ||
* All hoagie output methods support util.format placeholder | ||
* formatting. | ||
*/ | ||
Response.prototype.__defineGetter__('isTTY', function() { | ||
return this.stdout.isTTY; | ||
}); | ||
module.exports = Response; | ||
/** | ||
* Passes `args` to util.format. | ||
* @param {Array} args The arguments to format | ||
* @returns {String} | ||
*/ | ||
function format(args) { | ||
return util.format.apply(null, args); | ||
function format(fn) { | ||
return function(chunk) { | ||
if (arguments.length > 1) { | ||
chunk = util.format.apply(null, arguments); | ||
} | ||
fn.call(this, chunk); | ||
}; | ||
} |
@@ -17,5 +17,3 @@ var debug = require('debug')('hoagie:router'); | ||
function handle(req, res, done) { | ||
if (!done) { | ||
done = function() { debug('done'); res.end(); }; | ||
} | ||
debug('handle'); | ||
@@ -22,0 +20,0 @@ function nextStack(err) { |
@@ -1,3 +0,1 @@ | ||
var Request = require('./request'); | ||
var Response = require('./response'); | ||
var events = require('events'); | ||
@@ -18,3 +16,3 @@ var util = require('util'); | ||
this.handler = handler; | ||
this.on('run', handler); | ||
} | ||
@@ -24,15 +22,11 @@ | ||
Server.prototype.run = function(argv, stdin, stdout) { | ||
var req = new Request(argv, stdin || process.stdin); | ||
var res = new Response(stdout || process.stdout); | ||
Server.prototype.run = function(argv, req, res) { | ||
var self = this; | ||
debug('run', req.params); | ||
req.argv = argv; | ||
res.on('finish', function() { | ||
res.on('exit', function() { | ||
process.exitCode = res.exitCode; | ||
}); | ||
res.on('finish', function() { | ||
process.nextTick(function() { | ||
self.emit('finish', process.exitCode); | ||
self.emit('exit', process.exitCode); | ||
}); | ||
@@ -42,4 +36,4 @@ }); | ||
process.nextTick(function() { | ||
self.emit('start'); | ||
self.handler(req, res); | ||
debug('run', argv); | ||
self.emit('run', req, res); | ||
}); | ||
@@ -46,0 +40,0 @@ |
{ | ||
"name": "hoagie", | ||
"version": "2.4.0", | ||
"version": "3.0.0", | ||
"description": "Express-like organization for command-line applications", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
17
1
20569
662