Skip to content

Commit

Permalink
Added checks for missing options, buffer test and binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
martinheidegger committed Mar 15, 2016
1 parent 073a4e0 commit 3d5ae1e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"markdown-it": "^6.0.0",
"minimist": "^1.2.0",
"slug": "^0.9.1",
"through2": "^2.0.1"
"through2": "^2.0.1",
"istextorbinary": "^1.0.2"
},
"directories": {
"test": "test"
Expand Down
18 changes: 13 additions & 5 deletions processData.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ function preparePathForSlug (pth) {
}

function gatherDefaults (data, options, callback) {
if (!options) {
options = {}
}
if (options.data) {
Object.keys(options.data).forEach(function (key) {
if (data[key] === undefined) {
Expand Down Expand Up @@ -116,6 +113,12 @@ module.exports = function processData (raw, options, callback) {
callback = options
options = {}
}
if (!options) {
options = {}
}
if (raw === null || raw === undefined) {
return setImmediate(callback.bind(null, new Error('No data given to process.')))
}
if (typeof raw === 'function') {
raw = raw(options)
}
Expand All @@ -133,8 +136,13 @@ module.exports = function processData (raw, options, callback) {
stream.on('error', once)
return
}
if (raw === null || raw === undefined) {
return setImmediate(callback.bind(null, new Error('No data given to process.')))
if (raw instanceof Buffer) {
if (options.isText !== true && options.isText !== false) {
options.isText = require('istextorbinary').isTextSync(options.path, raw)
}
if (!options.isText) {
return setImmediate(callback.bind(null, new Error('binary-file')))
}
}
if (typeof raw !== 'string') {
raw = raw.toString()
Expand Down
16 changes: 15 additions & 1 deletion test/processData.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,22 @@ function describe (prefix, handler) {
})
}
describe('processing simple data', function (it) {
it('should thrown an error on invalid data', function (t) {
processData(new Buffer([400, 800, 20]), function (error) {
t.notEqual(error, null)
t.equal(error.message, 'binary-file')
t.end()
})
})
it('should thrown an error if the input is marked as not-text', function (t) {
processData(new Buffer(''), {isText: false}, function (error) {
t.notEqual(error, null)
t.equal(error.message, 'binary-file')
t.end()
})
})
it('should have at least the basic fields', function (t) {
processData('', function (ignore, data) {
processData('', null, function (ignore, data) {
t.equal(data.published, true)
t.equal(data.categories.length, 0)
t.equal(data.date, null)
Expand Down

0 comments on commit 3d5ae1e

Please sign in to comment.