From 8f952ee226b64b7e5dfc3963e3eaf978a2872c72 Mon Sep 17 00:00:00 2001 From: Valentin Agachi Date: Sun, 15 Oct 2017 11:42:29 +0200 Subject: [PATCH] Replace request with got --- README.md | 2 +- index.js | 30 +++++++++++++++--------------- package.json | 5 +++-- test/tests.js | 39 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 18c3b71..c4e2baa 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 4115c26..e9d178a 100755 --- a/index.js +++ b/index.js @@ -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'); @@ -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 }; diff --git a/package.json b/package.json index ecc2167..97a94d4 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/tests.js b/test/tests.js index 50234f4..a18a245 100644 --- a/test/tests.js +++ b/test/tests.js @@ -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 @@ -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'); @@ -750,4 +788,3 @@ describe('Instance', function () { }); }); }); -