+ doRequestAndSaveToFile,
+ downloadImage
+}
+
+// ---------------------------------------------------------------------------
+
+// Thanks to https://github.com/request/request/issues/2470#issuecomment-268929907 <3
+function onRequestDataLengthCheck (bodyKBLimit: number) {
+ let bufferLength = 0
+ const bytesLimit = bodyKBLimit * 1000
+
+ return function (chunk) {
+ bufferLength += chunk.length
+ if (bufferLength > bytesLimit) {
+ this.abort()
+
+ const error = new Error(`Response was too large - aborted after ${bytesLimit} bytes.`)
+ this.emit('error', error)
+ }
+ }