Skip to content

Commit

Permalink
fix: ficing double call for callbacks on error saving png to file
Browse files Browse the repository at this point in the history
  • Loading branch information
soldair committed Nov 23, 2021
1 parent 3d9f9ec commit 9aeacd8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
11 changes: 9 additions & 2 deletions lib/renderer/png.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ exports.renderToFile = function renderToFile (path, qrData, options, cb) {
options = undefined
}

let called = false
const done = (...args) => {
if (called) return
called = true
cb.apply(null, args)
}
const stream = fs.createWriteStream(path)
stream.on('error', cb)
stream.on('close', cb)

stream.on('error', done)
stream.on('close', done)

exports.renderToFileStream(stream, qrData, options)
}
Expand Down
4 changes: 3 additions & 1 deletion lib/renderer/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ exports.getOptions = function getOptions (options) {

const margin = typeof options.margin === 'undefined' ||
options.margin === null ||
options.margin < 0 ? 4 : options.margin
options.margin < 0
? 4
: options.margin

const width = options.width && options.width >= 21 ? options.width : undefined
const scale = options.scale || 4
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-terser": "^5.3.0",
"sinon": "^9.0.2",
"standard": "^14.3.3",
"tap": "^14.10.7"
"standard": "^16.0.4",
"tap": "^15.1.2"
},
"repository": {
"type": "git",
Expand All @@ -71,7 +71,8 @@
"standard": {
"ignore": [
"build/",
"examples/vendors/"
"examples/vendors/",
"lib/core/regex.js"
]
}
}
2 changes: 1 addition & 1 deletion test/e2e/toFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('toFile png', function (t) {
'qVO5LQqTxRrFGKNUqxRon/scYo1ijFGqVYoxRrlGKNUqxRijVKsUYp1ijFGqVYoxRrlGKN',
'UqxRijXKP0OHEepgrecVAAAAAElFTkSuQmCC'].join('')

t.plan(9)
t.plan(8)

QRCode.toFile(fileName, 'i am a pony!', {
errorCorrectionLevel: 'L'
Expand Down
2 changes: 1 addition & 1 deletion test/unit/renderer/png.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test('PNG renderToFile', function (t) {
let fsStub = sinon.stub(fs, 'createWriteStream')
fsStub.returns(new StreamMock())

t.plan(6)
t.plan(5)

PngRenderer.renderToFile(fileName, sampleQrData, function (err) {
t.ok(!err,
Expand Down

0 comments on commit 9aeacd8

Please sign in to comment.