Skip to content

Commit

Permalink
feat: handle error if cb does not return jimp
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Nov 18, 2020
1 parent f0d77f9 commit ba9bae5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
18 changes: 15 additions & 3 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const compare = (stream, fixtureName, expectedName, done, expectedErr) => {
}

describe('gulp-jimp-wrapper', () => {
it('works for me', done => {
it('should just work', done => {
compare(
jimp(img => img.invert()),
'original.jpg',
Expand All @@ -48,13 +48,25 @@ describe('gulp-jimp-wrapper', () => {
)
})

it('throws an error', done => {
it('should throw an error if argument is illegal', done => {
compare(
jimp('img => img.invert()'),
'original.jpg',
'invert.jpg',
done,
`Argument \`img => img.invert()\` is not a function`
"Argument 'img => img.invert()' is not a function."
)
})

it('throws an error if callback does not return a Jimp instance', done => {
compare(
jimp(img => {
img.invert()
}),
'original.jpg',
'invert.jpg',
done,
'Jimp instance must be returned from your callback.'
)
})
})
38 changes: 19 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,46 @@ const SUPPORTED_MIME_TYPES = {
gif: 'image/gif'
}

const processImg = (img, cb) =>
new Promise((resolve, reject) => {
const extension = path.extname(img.path).split('.').pop()

jimp
.read(img.contents)
.then(cb)
.then(img => img.getBufferAsync(SUPPORTED_MIME_TYPES[extension]))
.then(data => resolve(data))
.catch(err => reject(err))
})
const pluginError = msg => new PluginError(PLUGIN_NAME, msg)

const processImage = async (img, cb, callback) => {
const extension = path.extname(img.path).split('.').pop()

const res = await jimp.read(img.contents).then(cb)
if (!(res instanceof jimp)) {
callback(pluginError('Jimp instance must be returned from your callback.'))
return
}

return res.getBufferAsync(SUPPORTED_MIME_TYPES[extension])
}

const jimpWrapper = cb =>
const gulpJimp = cb =>
through.obj(async function (img, _, callback) {
if (img.isNull()) {
callback(null, img)
return
}

if (img.isStream()) {
callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'))
callback(pluginError('Streaming not supported.'))
return
}

if (typeof cb !== 'function') {
callback(
new PluginError(PLUGIN_NAME, `Argument \`${cb}\` is not a function`)
)
callback(pluginError(`Argument '${cb}' is not a function.`))
return
}

try {
const data = await processImg(img, cb)
const data = await processImage(img, cb, callback)
img.contents = Buffer.from(data)
this.push(img)
} catch (err) {
this.emit('error', new PluginError(PLUGIN_NAME, err))
this.emit('error', pluginError(err.message))
}

callback()
})

module.exports = jimpWrapper
module.exports = gulpJimp

0 comments on commit ba9bae5

Please sign in to comment.