From 2e9a643c0f6991490d9aa3d66a2e156c973e7a13 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 18 Oct 2021 00:52:09 -0600 Subject: [PATCH] feature: fail faster on bad createWriteStream --- index.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 0b993d2..83717b9 100644 --- a/index.js +++ b/index.js @@ -76,6 +76,19 @@ function setDefaults(defs) { var req; var finalOpts = {}; + // allow specifying a file + if ('string' === typeof opts.stream) { + if (opts.debug) { + console.debug('[@root/request] creating file write stream'); + } + try { + opts.stream = fs.createWriteStream(opts.stream); + } catch (e) { + cb(e); + return; + } + } + function onResponse(resp) { var followRedirect; @@ -151,20 +164,6 @@ function setDefaults(defs) { reject = _reject; }); - // allow specifying a file - if ('string' === typeof opts.stream) { - try { - if (opts.debug) { - console.debug( - '[@root/request] file write stream created' - ); - } - opts.stream = fs.createWriteStream(opts.stream); - } catch (e) { - cb(e); - return; - } - } // or an existing write stream if ('function' === typeof opts.stream.pipe) { if (opts.debug) {