Skip to content

Commit

Permalink
Change how debug works. No longer accepts a boolean. Use debug lib in…
Browse files Browse the repository at this point in the history
…stead by default.

Also handle double quotes and backslashes on flag values
  • Loading branch information
Juan José committed Nov 22, 2016
1 parent f5ddc50 commit 1b948cb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dafuq allows you to create an api that executes files on the os command line (vi
### Options
* **commands**: Path where to look for commands to run on requests.
* **shebang** (optional): If specified, this will be the command line interpreter to be used when running the file. If it is not specified we will check the file for execution permisions and run it by itself. Defaults to `''`.
* **debug** (optional): Show debug info. If true, `console.log` will be used as loggin function. If a function it will used as loggin function instead of the default . Defaults to `false`.
* **debug** (optional): Log debug function. A function that will used as loggin function instead of the default . Defaults to `debug('dafuq')`.
* **bearer** (optional): Add bearer token authorization method to the api. The acces token is provided as the value of this config. Defaults to ''
* **timeout** (optional): Time to wait before killing an spawned command. Defaults to `0` which means infinite.
* **middlewares** (optional): Array of middlewares that will be executed after the command has run but before sending the api respnse. The response object has a `dafuq` property containing `result` and `type` properties with the result of the execution.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "MIT",
"dependencies": {
"body-parser": "^1.15.2",
"debug": "^2.3.3",
"express": "^4.14.0",
"glob": "^7.0.5",
"multer": "^1.2.0",
Expand Down
23 changes: 11 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ const fs = require('fs')
, path = require('path')
, assert = require('assert')
, child_process = require('child_process')
, debug = require('debug')
, glob = require('glob')
, express = require('express')
, multer = require('multer')
, bodyParser = require('body-parser')

const IS_TEST = process.env['NODE_ENV'] === 'test'
const LOG = debug('dafuq')

const RESPONSE_RESULT_CONTAINER = 'dafuq'
const RESPONSE_RESULT_TYPE = 'type'
Expand Down Expand Up @@ -132,10 +133,14 @@ function buildCommandFlags(req) {

const flags = Object.assign({}, headerFlags, req.query, req.params, req.body, uploadFlags)
Object.keys(flags).forEach(function(flagName) {
const flagValue = flags[flagName]
let flagValue = flags[flagName]
cmdFlags += ` --${flagName}`
if (flagValue)
if (flagValue) {
flagValue = flagValue
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
cmdFlags += ` "${flagValue}"`
}
})

return cmdFlags
Expand Down Expand Up @@ -275,7 +280,7 @@ export default function dafuq(config) {
// Assign default values
const opts = Object.assign({
shebang: '',
debug: false,
debug: LOG,
brearer: '',
timeout: 0,
middlewares: [],
Expand Down Expand Up @@ -308,14 +313,8 @@ export default function dafuq(config) {
if (opts.env !== undefined && (Array.isArray(opts.env) || typeof opts.env !== 'object'))
throw new TypeError('env must be a an object')

if (opts.debug !== undefined) {
if (opts.debug === true)
opts.debug = IS_TEST ? (() => {}) : console.log
else if (opts.debug === false)
opts.debug = (() => {})
else if (typeof opts.debug !== 'function')
throw new TypeError('debug must be a boolean or a logging function')
}
if (opts.debug !== undefined && typeof opts.debug !== 'function')
throw new TypeError('debug must be a logging function')

opts.debug('Building dafuq instance with %j', config)

Expand Down
54 changes: 47 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Constructor', function() {

const build = function(opts) {
return function() {
dafuq(opts)
return dafuq(opts)
}
}

Expand Down Expand Up @@ -109,19 +109,19 @@ describe('Constructor', function() {
}).should.throw(/timeout/);
})

it('should throw if debug is not a boolean nor a function', function() {
it('should throw if debug is not a function', function() {
build({
path: './commands',
debug: false
debug: function() {}
}).should.not.throw();
build({
path: './commands',
debug: true
}).should.not.throw();
debug: false
}).should.throw();
build({
path: './commands',
debug: function() {}
}).should.not.throw();
debug: true
}).should.throw();
build({
path: './commands',
debug: ''
Expand Down Expand Up @@ -578,4 +578,44 @@ describe('Arguments', () => {
.expect(res => res.body.result.should.be.equal("Hello Jhon"))
.end(done)
})

it('should pass arguments containing blank spaces', (done) => {
request(app)
.get('/hello')
.query({ name: 'Sarah Connor'})
.expect(200)
.expect('Content-Type', /json/)
.expect(res => res.body.result.should.be.equal("Hello Sarah Connor"))
.end(done)
})

it('should pass arguments containing double quotes', (done) => {
request(app)
.get('/hello')
.query({ name: 'Sarah "Connor"'})
.expect(200)
.expect('Content-Type', /json/)
.expect(res => res.body.result.should.be.equal("Hello Sarah \"Connor\""))
.end(done)
})

it('should pass arguments containing backslashes', (done) => {
request(app)
.get('/hello')
.query({ name: 'Sarah\\ Connor'})
.expect(200)
.expect('Content-Type', /json/)
.expect(res => res.body.result.should.be.equal("Hello Sarah\\ Connor"))
.end(done)
})

it('should pass arguments containing backslashes and double quotes', (done) => {
request(app)
.get('/hello')
.query({ name: 'Sarah \\"Connor\\"'})
.expect(200)
.expect('Content-Type', /json/)
.expect(res => res.body.result.should.be.equal('Hello Sarah \\"Connor\\"'))
.end(done)
})
})

0 comments on commit 1b948cb

Please sign in to comment.