Skip to content

Commit

Permalink
Merge pull request #2 from Shorthand/axios-and-security-bump
Browse files Browse the repository at this point in the history
🩹 Replace request with axios. Bump libs.
  • Loading branch information
ckortekaas authored Apr 13, 2023
2 parents 3e89b86 + 3f2dc4e commit 0afb65e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 60 deletions.
27 changes: 11 additions & 16 deletions lib/amperize.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var EventEmitter = require('events').EventEmitter,
uuid = require('uuid'),
async = require('async'),
url = require('url'),
request = require('request-promise'),
axios = require('axios'),
probeImageSize = require('probe-image-size'),
_ = require('lodash'),
sizeOf = require('image-size'),
Expand Down Expand Up @@ -126,7 +126,7 @@ Amperize.prototype.traverse = async function traverse(data, html, done) {
'User-Agent': 'Mozilla/5.0 Safari/537.36'
},
timeout: self.config['request_timeout'],
encoding: null
responseEncoding: null
};

// check if element.width is smaller than 300 px. In that case, we shouldn't use
Expand Down Expand Up @@ -157,24 +157,19 @@ Amperize.prototype.traverse = async function traverse(data, html, done) {

// probe will fetch the minimal amount of data needed to determine
// the image dimensions so it's more performant than a full fetch
function _probeImageSize(url) {
return probeImageSize(
url,
requestOptions
).then(function (result) {
imageSizeCache[url] = result;
return result;
});
}
async function _probeImageSize(url) {
const result = await probeImageSize(url);
imageSizeCache[url] = result;
return result;
}

// fetch the full image before reading dimensions using image-size,
// it's slower but has better format support
function _fetchImageSize(url) {
return request(
url,
requestOptions
).then(function (response) {
var result = sizeOf(response);
return axios
.get(url, { responseType: "arraybuffer", ...requestOptions })
.then(function (response) {
const result = sizeOf(response.data);
imageSizeCache[url] = result;
return result;
});
Expand Down
37 changes: 18 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,27 @@
},
"homepage": "https://github.com/jbhannah/amperize#readme",
"dependencies": {
"async": "^3.0.1",
"domutils": "^2.0.0",
"async": "^3.2.4",
"axios": "^1.3.4",
"domutils": "^3.0.1",
"emits": "^3.0.0",
"htmlparser2": "^4.0.0",
"image-size": "^0.8.1",
"lodash": "^4.17.11",
"probe-image-size": "^5.0.0",
"request": "^2.83.0",
"request-promise": "^4.2.4",
"uuid": "^8.1.0",
"validator": "^13.0.0"
"htmlparser2": "^8.0.2",
"image-size": "^1.0.2",
"lodash": "^4.17.21",
"probe-image-size": "^7.2.3",
"uuid": "^9.0.0",
"validator": "^13.9.0"
},
"devDependencies": {
"chai": "^4.1.2",
"cz-conventional-changelog": "3.2.0",
"mocha": "^7.0.0",
"nock": "^12.0.0",
"nyc": "^15.0.0",
"rewire": "^5.0.0",
"semantic-release": "17.0.8",
"sinon": "^9.0.0",
"sinon-chai": "^3.3.0"
"chai": "^4.3.7",
"cz-conventional-changelog": "3.3.0",
"mocha": "^10.2.0",
"nock": "^13.3.0",
"nyc": "^15.1.0",
"rewire": "^6.0.0",
"semantic-release": "21.0.0",
"sinon": "^15.0.3",
"sinon-chai": "^3.7.0"
},
"config": {
"commitizen": {
Expand Down
26 changes: 1 addition & 25 deletions test/amperize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ describe('Amperize', function () {
});

it('can handle <img> tag without src and does not transform it', function (done) {
amperize.parse('<img><//img><p>some text here</p>', function (error, result) {
amperize.parse('<img><p>some text here</p>', function (error, result) {
expect(result).to.exist;
expect(result).to.be.equal('<img><p>some text here</p>');
done();
Expand Down Expand Up @@ -676,30 +676,6 @@ describe('Amperize', function () {
done();
});
});

it('can handle timeout errors', function (done) {
var GIF1x1 = Buffer.from('R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==', 'base64');

this.timeout(300);
amperize = new Amperize({request_timeout: 100});

// NOTE: nock will compare the delay value with the timeout value used in the underlying `request`
// call and immediately fire an ETIMEDOUT event so don't expect test times to match the delay
//
// Unfortunately nock.cleanAll() does not stop delayed requests so there can be a delay once all tests
// finish running whilst waiting for timeouts to finish so it's best to keep delays as short as possible
// https://github.com/nock/nock/issues/1118
imageSizeMock = nock('http://example.com')
.get('/images/IMG_xyz.jpg')
.delay(200)
.reply(200, GIF1x1);

amperize.parse('<img src="http://example.com/images/IMG_xyz.jpg">', function (error, result) {
expect(error).to.be.null;
expect(result).to.contain('<img src="http://example.com/images/IMG_xyz.jpg">');
done();
});
});
});

describe('#amperizer', function () {
Expand Down

0 comments on commit 0afb65e

Please sign in to comment.