aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/request-image-size.js
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2022-10-07 15:03:22 -0400
committerbobzel <zzzman@gmail.com>2022-10-07 15:03:22 -0400
commita90bce69bcb5dfe09d8f2605e0dc244cf9fcc7ad (patch)
tree74d08977a4c4913647b57313acb6baf0e1901880 /src/client/util/request-image-size.js
parent0e41cb323c486b23c70e53d14a563adaf0eeef9e (diff)
parenteb5f75785fd28acb50f1b30434e89223fff00185 (diff)
Merge branch 'master' into pres-pin-fixes
Diffstat (limited to 'src/client/util/request-image-size.js')
-rw-r--r--src/client/util/request-image-size.js26
1 files changed, 14 insertions, 12 deletions
diff --git a/src/client/util/request-image-size.js b/src/client/util/request-image-size.js
index beb030635..502e0fbac 100644
--- a/src/client/util/request-image-size.js
+++ b/src/client/util/request-image-size.js
@@ -15,15 +15,18 @@ const HttpError = require('standard-http-error');
module.exports = function requestImageSize(options) {
let opts = {
- encoding: null
+ encoding: null,
};
if (options && typeof options === 'object') {
opts = Object.assign(options, opts);
} else if (options && typeof options === 'string') {
- opts = Object.assign({
- uri: options
- }, opts);
+ opts = Object.assign(
+ {
+ uri: options,
+ },
+ opts
+ );
} else {
return Promise.reject(new Error('You should provide an URI string or a "request" options object.'));
}
@@ -38,9 +41,8 @@ module.exports = function requestImageSize(options) {
return reject(new HttpError(res.statusCode, res.statusMessage));
}
- let buffer = new Buffer.from([]);
+ let buffer = Buffer.from([]);
let size;
- let imageSizeError;
res.on('data', chunk => {
buffer = Buffer.concat([buffer, chunk]);
@@ -48,8 +50,8 @@ module.exports = function requestImageSize(options) {
try {
size = imageSize(buffer);
} catch (err) {
- imageSizeError = err;
- return;
+ reject(err);
+ return req.abort();
}
if (size) {
@@ -58,11 +60,11 @@ module.exports = function requestImageSize(options) {
}
});
- res.on('error', err => reject(err));
+ res.on('error', reject);
res.on('end', () => {
if (!size) {
- return reject(imageSizeError);
+ return reject(new Error('Image has no size'));
}
size.downloaded = buffer.length;
@@ -70,6 +72,6 @@ module.exports = function requestImageSize(options) {
});
});
- req.on('error', err => reject(err));
+ req.on('error', reject);
});
-}; \ No newline at end of file
+};