From 7b722eeb49355ac3776b0570555509b6ebcdbc35 Mon Sep 17 00:00:00 2001 From: Mat Carey Date: Tue, 23 Feb 2016 12:55:26 +0000 Subject: [PATCH 1/2] Adding a configurable user agent string to outgoing requests. --- README.md | 8 +++++ lib/core.js | 86 ++++++++++++++++++++++++++++++++---------------- main.js | 2 ++ package.json | 1 + test/general.js | 11 +++++++ test/promises.js | 60 ++++++++++++++++++++++++++++++++- 6 files changed, 138 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 3ff6732..7a8e881 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,14 @@ If the value is present, it will also set the new value. If the value is `null`, The default value is `4096`. +### fastimage.userAgent([userAgent]) + +Gets or sets the User Agent String to send on outgoing requests. + +If the value is present, it will also set the new value. If the value is `null`, it will restore the default value. + +The default value is `4096`. + ### fastimage.FastImageStream A image analysis stream. diff --git a/lib/core.js b/lib/core.js index 8c7b066..4bc4746 100644 --- a/lib/core.js +++ b/lib/core.js @@ -19,6 +19,9 @@ var originalTimeout = 30000; var timeout = originalTimeout; + var originalUserAgent = "Fast Image - Node Image Lookup - https://www.npmjs.com/package/fastimage"; + var userAgent = originalUserAgent; + // Polyfill for Node 0.10 if(typeof Promise === "undefined") // eslint-disable-line no-use-before-define var Promise = require("promise"); @@ -55,6 +58,22 @@ return timeout; }; + /** + Gets or sets the user agent string to send when connecting to a host. + * + * @alias module:fastimage.userAget + * @param {string} [value] - The new value to set. Passing `null` will restore the default value (Fast Image - Node Image Lookup). + * @returns {number} The current value + */ + var manageUserAgent = function(value){ + if(typeof value === "string") + userAgent = value; + else if(value === null) + userAgent = originalUserAgent; + + return userAgent; + }; + var identify = function(buffer, url, realUrl, size, time){ var info = null; var elapsed = null; @@ -150,37 +169,44 @@ var unsupportedError = new errors.FastImageError("Unsupported image file.", {url: url, code: "UNSUPPORTED_TYPE"}); // When a response is received, control the HTTP code. If is not in the 2xx range, return a error. - request({url: url, method: "GET", encoding: null, timeout: timeout}) - .on("response", function(response){ - realUrl = response.request.uri.href; - size = parseFloat(response.headers["content-length"]); - - if(response.statusCode / 100 !== 2){ - this.abort(); - callback(errors.handleNetworkError({code: "HTTP_ERROR", status: response.statusCode}, url)); - } - }).on("data", function(chunk){ - // Append the new data and try the identification again. - buffer = Buffer.concat([buffer, chunk]); - info = identify(buffer, url, realUrl, size, time); - - // Informations found or threshold reached. End the transfer and call the callback. - if(info || buffer.length >= threshold){ - this.abort(); - - if(info) - callback(null, info); - else - callback(unsupportedError); - } - }).on("error", function(error){ + request.get({ + url: url, + encoding: null, + timeout: timeout, + headers: { + "user-agent": userAgent + } + }) + .on("response", function(response){ + realUrl = response.request.uri.href; + size = parseFloat(response.headers["content-length"]); + + if(response.statusCode / 100 !== 2){ + this.abort(); + callback(errors.handleNetworkError({code: "HTTP_ERROR", status: response.statusCode}, url)); + } + }).on("data", function(chunk){ + // Append the new data and try the identification again. + buffer = Buffer.concat([buffer, chunk]); + info = identify(buffer, url, realUrl, size, time); + + // Informations found or threshold reached. End the transfer and call the callback. + if(info || buffer.length >= threshold){ this.abort(); - callback(errors.handleNetworkError(error, url)); - }).on("end", function(){ - // The end of the transfer has been reached with no results. Return an error. - if(!this._aborted && !info) + + if(info) + callback(null, info); + else callback(unsupportedError); - }); + } + }).on("error", function(error){ + this.abort(); + callback(errors.handleNetworkError(error, url)); + }).on("end", function(){ + // The end of the transfer has been reached with no results. Return an error. + if(!this._aborted && !info) + callback(unsupportedError); + }); }; var performAnalysis = function(subject, callback){ @@ -204,6 +230,8 @@ timeout: manageTimeout, + userAgent: manageUserAgent, + performAnalysis: performAnalysis, /** diff --git a/main.js b/main.js index 24bb402..02b7b1f 100644 --- a/main.js +++ b/main.js @@ -20,6 +20,8 @@ threshold: core.threshold, + userAgent: core.userAgent, + /** * Analyzes a source (a local path, a remote URL or a Buffer) and return the image informations. * diff --git a/package.json b/package.json index 6abcac6..76770ee 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "mocha": "~2.1.0", "mocha-lcov-reporter": "0.0.2", "sepia": "^2.0.1", + "sinon": "^1.17.3", "through": "^2.3.6" }, "config": { diff --git a/test/general.js b/test/general.js index 10b6f16..4474983 100644 --- a/test/general.js +++ b/test/general.js @@ -51,6 +51,17 @@ }); }); + describe("user agent", function(){ + it("should set and get the value", function(){ + var defaultValue = "Fast Image - Node Image Lookup - https://www.npmjs.com/package/fastimage"; + expect(fastimage.userAgent()).to.equal(defaultValue); + expect(fastimage.userAgent("new value")).to.equal("new value"); + expect(fastimage.userAgent()).to.equal("new value"); + expect(fastimage.userAgent(null)).to.equal(defaultValue); + expect(fastimage.userAgent()).to.equal(defaultValue); + }); + }); + describe("using callbacks", function(){ this.timeout(5000); diff --git a/test/promises.js b/test/promises.js index 31e05d5..e1fe584 100644 --- a/test/promises.js +++ b/test/promises.js @@ -8,6 +8,8 @@ var expect = require("expect.js"); var path = require("path"); + var sinon = require("sinon"); + var request = require("request"); var fastimage = require("../main"); @@ -120,5 +122,61 @@ }); }); }); + + describe("outgoing request", function(){ + var waitForTestToCallRequest, defaultUserAgent; + + beforeEach(function(){ + defaultUserAgent = "Fast Image - Node Image Lookup - https://www.npmjs.com/package/fastimage"; + waitForTestToCallRequest = new Promise(function (res) { + sinon.stub(request, "get", function (outgoingRequest) { + res(outgoingRequest); + var ret = { + on: function () { + return ret; + } + }; + return ret; + }); + }); + }); + afterEach(function(){ + request.get.restore(); + }); + + it("should contain a default user agent string", function (done) { + waitForTestToCallRequest.then(function (outgoingRequest) { + expect(outgoingRequest.headers["user-agent"]).to.be(defaultUserAgent); + done(); + }).catch(done); + + fastimage.info("http://example.com/"); + }); + + it("should contain a default user agent string", function (done) { + var newUserAgentString = "Hello World!"; + + waitForTestToCallRequest.then(function (outgoingRequest) { + expect(outgoingRequest.headers["user-agent"]).to.be(newUserAgentString); + done(); + }).catch(done); + + fastimage.userAgent(newUserAgentString); + fastimage.info("http://example.com/"); + }); + + it("should have a standard setter/getter for user agent string", function (done) { + waitForTestToCallRequest.then(function (outgoingRequest) { + expect(outgoingRequest.headers["user-agent"]).to.be(defaultUserAgent); + done(); + }).catch(done); + + fastimage.userAgent("this will never actually be used"); + expect(fastimage.userAgent()).to.eql("this will never actually be used"); + fastimage.userAgent(null); + expect(fastimage.userAgent()).to.eql(defaultUserAgent); + fastimage.info("http://example.com/"); + }); + }); }); -})(); \ No newline at end of file +})(); From 2699937adbb1ddbc1baf793f960f99a5b03e6bf7 Mon Sep 17 00:00:00 2001 From: Mat Carey Date: Tue, 23 Feb 2016 12:55:35 +0000 Subject: [PATCH 2/2] Updated docs. --- docs/index.html | 5 +- docs/lib_core.js.html | 99 +++++++---- docs/lib_errors.js.html | 2 +- docs/lib_streams.js.html | 19 ++- docs/main.js.html | 4 +- docs/module-fastimage.FastImageError.html | 7 +- docs/module-fastimage.FastImageStream.html | 7 +- docs/module-fastimage.html | 181 ++++++++++++++++++++- 8 files changed, 266 insertions(+), 58 deletions(-) diff --git a/docs/index.html b/docs/index.html index 576b390..bcc13a8 100644 --- a/docs/index.html +++ b/docs/index.html @@ -149,6 +149,9 @@

fastimage.timeout([timeout])

Gets or sets the maximum number of secon

fastimage.threshold([threshold])

Gets or sets the maximum number of bytes to read to attempt an identification before giving up and state that the source is not an image.

If the value is present, it will also set the new value. If the value is null, it will restore the default value.

The default value is 4096.

+

fastimage.userAgent([userAgent])

Gets or sets the User Agent String to send on outgoing requests.

+

If the value is present, it will also set the new value. If the value is null, it will restore the default value.

+

The default value is 4096.

fastimage.FastImageStream

A image analysis stream.

Streams will emit the size and type if you only need those informations about the image.

fastimage.FastImageError

This error will be returned as the first argument of the callbacks if anything goes wrong.

@@ -179,7 +182,7 @@

Home

Modules