Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Replace request with got
Browse files Browse the repository at this point in the history
  • Loading branch information
avaly committed Oct 15, 2017
1 parent 63f11c6 commit 8f952ee
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ The following third-party libraries are used by this module:
* pngjs: https://github.com/niegowski/node-pngjs
* stream-buffers: https://github.com/samcday/node-stream-buffer
* underscore: http://underscorejs.org
* request: https://github.com/request/request
* got: https://github.com/sindresorhus/got

###Dev-Dependencies
* chai: http://chaijs.com
Expand Down
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var fs = require('fs'),
filters = require('./lib/filters'),
streamBuffers = require("stream-buffers"),
MemoryStream = require('./lib/memoryStream'),
request = require('request');
got = require('got');

var Decoder = require('./lib/png/decoder');
var Encoder = require('./lib/png/encoder');
Expand Down Expand Up @@ -136,29 +136,29 @@ PNGImage._readImageFromUrl = function (url, fn) {

var stream, req;

request.head(url, function (err, res) {
got.head(url).then(function (res) {

var contentType = (res.headers['content-type'] || '').toLowerCase();

if (contentType !== 'image/png') {
fn(new Error('Unsupported image format: ' + contentType));
throw new Error('Unsupported image format: ' + contentType);
}

} else {
stream = new MemoryStream({size: res.headers['content-length']});
req = got.stream(url);
req.pipe(stream);

stream = new MemoryStream({size: res.headers['content-length']});
req = request(url).pipe(stream);
req.on('error', function (err) {
fn(err);
});

req.on('error', function (err) {
fn(err);
});
req.on('end', function () {
var buffer = stream.getBuffer();

req.on('finish', function () {
var buffer = stream.getBuffer();
PNGImage.loadImage(buffer, fn);
});

PNGImage.loadImage(buffer, fn);
});
}
});
}).catch(fn);

return null; // This will be deprecated
};
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@
"docs": "yuidoc ."
},
"dependencies": {
"got": "^7.1.0",
"iconv-lite": "^0.4.8",
"pako": "^0.2.6",
"pngjs": "2.3.1",
"request": "^2.55.0",
"stream-buffers": "1.0.1",
"underscore": "1.7.0"
},
"devDependencies": {
"chai": "1.9.2",
"coveralls": "2.11.2",
"codeclimate-test-reporter": "0.0.4",
"coveralls": "2.11.2",
"istanbul": "0.3.2",
"mocha": "1.21.4",
"nock": "^9.0.22",
"sinon": "1.12.2",
"sinon-chai": "2.7.0",
"yuidocjs": "0.3.50"
Expand Down
39 changes: 38 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var PNG = require('pngjs').PNG;
var PNGImage = require('../index');
var expect = require('chai').expect;
var fs = require('fs');
var nock = require('nock');

/**
* Generates an image
Expand Down Expand Up @@ -346,6 +347,43 @@ describe('Instance', function () {
});
});

it('should read from an URL', function (done) {

var stat = fs.statSync(__dirname + '/test.png');

nock('http://mock.com')
.head('/test.png')
.reply(200, '', {
'Content-Type': 'image/png',
'Content-Length': stat.size
});
nock('http://mock.com')
.get('/test.png')
.reply(200, function(uri, requestBody) {
return fs.createReadStream(__dirname + '/test.png')
}, {
'Content-Type': 'image/png'
});

PNGImage.readImage('http://mock.com/test.png', function (err, image) {

if (err) {
done(err);
} else {

try {
expect(image.getBlob().length, this.instance.getBlob().length);
compareBuffers(image.getBlob(), this.instance.getBlob(), 0, 0, image.getBlob().length);

done();
} catch (err) {
done(err);
}
}

}.bind(this));
});

it('should load an image', function (done) {

var contents = fs.readFileSync(__dirname + '/test.png');
Expand Down Expand Up @@ -750,4 +788,3 @@ describe('Instance', function () {
});
});
});

0 comments on commit 8f952ee

Please sign in to comment.