fix: ficing double call for callbacks on error saving png to file

This commit is contained in:
Ryan Day 2021-11-23 11:49:20 -08:00
parent 3d9f9ecd35
commit 9aeacd8b5d
5 changed files with 18 additions and 8 deletions

View file

@ -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)
}

View file

@ -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

View file

@ -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",
@ -71,7 +71,8 @@
"standard": {
"ignore": [
"build/",
"examples/vendors/"
"examples/vendors/",
"lib/core/regex.js"
]
}
}

View file

@ -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'

View file

@ -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,