Skip to content

Commit

Permalink
Merge branch 'add-user-agent' of https://github.com/dorightdigital/fa…
Browse files Browse the repository at this point in the history
…stimage into dorightdigital-add-user-agent
  • Loading branch information
Shogun committed Mar 7, 2016
2 parents 9c54724 + 2699937 commit 5d7a9c9
Show file tree
Hide file tree
Showing 14 changed files with 379 additions and 66 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ <h3>fastimage.timeout([timeout])</h3><p>Gets or sets the maximum number of secon
<h3>fastimage.threshold([threshold])</h3><p>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.</p>
<p>If the value is present, it will also set the new value. If the value is <code>null</code>, it will restore the default value.</p>
<p>The default value is <code>4096</code>.</p>
<h3>fastimage.userAgent([userAgent])</h3><p>Gets or sets the User Agent String to send on outgoing requests.</p>
<p>If the value is present, it will also set the new value. If the value is <code>null</code>, it will restore the default value.</p>
<p>The default value is <code>4096</code>.</p>
<h3>fastimage.FastImageStream</h3><p>A image analysis stream.</p>
<p>Streams will emit the <code>size</code> and <code>type</code> if you only need those informations about the image.</p>
<h3>fastimage.FastImageError</h3><p>This error will be returned as the first argument of the callbacks if anything goes wrong.</p>
Expand Down Expand Up @@ -179,7 +182,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-fa
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-beta2</a> on Sat Mar 28 2015 14:49:38 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Feb 23 2016 12:36:13 GMT+0000 (GMT)
</footer>

<script> prettyPrint(); </script>
Expand Down
99 changes: 67 additions & 32 deletions docs/lib_core.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ <h1 class="page-title">Source: lib/core.js</h1>
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");

/**
* 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.
*
Expand All @@ -59,8 +66,9 @@ <h1 class="page-title">Source: lib/core.js</h1>
threshold = value;
else if(value === null)
threshold = originalThreshold;

return threshold;
}
};

/**
* Gets or sets the maximum number of seconds to wait to connect to a host.
Expand All @@ -74,8 +82,25 @@ <h1 class="page-title">Source: lib/core.js</h1>
timeout = value;
else if(value === null)
timeout = originalTimeout;

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;
Expand Down Expand Up @@ -171,37 +196,45 @@ <h1 class="page-title">Source: lib/core.js</h1>
var realUrl = null;
var unsupportedError = new errors.FastImageError("Unsupported image file.", {url: url, code: "UNSUPPORTED_TYPE"});

request({url: url, method: "GET", encoding: null, timeout: timeout}) // When a response is received, control the HTTP code. If is not in the 2xx range, return a error.
.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){
// When a response is received, control the HTTP code. If is not in the 2xx range, return a 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 &amp;&amp; !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 &amp;&amp; !info)
callback(unsupportedError);
});
};

var performAnalysis = function(subject, callback){
Expand All @@ -225,6 +258,8 @@ <h1 class="page-title">Source: lib/core.js</h1>

timeout: manageTimeout,

userAgent: manageUserAgent,

performAnalysis: performAnalysis,

/**
Expand Down Expand Up @@ -284,7 +319,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-fa
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-beta2</a> on Sat Mar 28 2015 14:49:38 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Feb 23 2016 12:36:13 GMT+0000 (GMT)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/lib_errors.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-fa
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-beta2</a> on Sat Mar 28 2015 14:49:38 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Feb 23 2016 12:36:13 GMT+0000 (GMT)
</footer>

<script> prettyPrint(); </script>
Expand Down
19 changes: 14 additions & 5 deletions docs/lib_streams.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,20 @@ <h1 class="page-title">Source: lib/streams.js</h1>
* @class module:fastimage.FastImageStream
*/
var FastImageStream = function(){
var legacyNode = process.version.match(/v0\.10/);
var options = arguments[arguments.length - 1] || {};
options.readableObjectMode = true;

// Enable ObjectMode for Node 0.12+ or io.js
if(!legacyNode)
options.readableObjectMode = true;

Duplex.call(this, options);
this.pause();

// Enable ObjectMode for Node 0.10 or pause for the rest
if(legacyNode)
this._readableState.objectMode = true;
else
this.pause();

if(arguments.length === 2)
this.source = arguments[0];
Expand Down Expand Up @@ -86,7 +95,7 @@ <h1 class="page-title">Source: lib/streams.js</h1>
// Push the data and close.
this.push(this.info);
}catch(e){
if(this.buffer.length > core.threshold)
if(this.buffer.length > core.threshold())
this.end();
}

Expand Down Expand Up @@ -129,7 +138,7 @@ <h1 class="page-title">Source: lib/streams.js</h1>

_failAnalysis: function(){
if(!this.info)
this.emit("error", this.error || new errors.FastImageError("Unsupported image.", {code: "UNSUPPORTED_TYPE"}));
this.emit("error", this.error || new errors.FastImageError("Unsupported image data.", {code: "UNSUPPORTED_TYPE"}));
}
});

Expand Down Expand Up @@ -173,7 +182,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-fa
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-beta2</a> on Sat Mar 28 2015 14:49:38 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Feb 23 2016 12:36:13 GMT+0000 (GMT)
</footer>

<script> prettyPrint(); </script>
Expand Down
4 changes: 3 additions & 1 deletion docs/main.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ <h1 class="page-title">Source: main.js</h1>

threshold: core.threshold,

userAgent: core.userAgent,

/**
* Analyzes a source (a local path, a remote URL or a Buffer) and return the image informations.
*
Expand Down Expand Up @@ -160,7 +162,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-fa
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-beta2</a> on Sat Mar 28 2015 14:49:38 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Feb 23 2016 12:36:13 GMT+0000 (GMT)
</footer>

<script> prettyPrint(); </script>
Expand Down
7 changes: 2 additions & 5 deletions docs/module-fastimage.FastImageError.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ <h1 class="page-title">Class: FastImageError</h1>
<header>

<h2>
<span class="ancestors"><a href="module-fastimage.html">fastimage</a>.</span>

FastImageError
</h2>
<span class="ancestors"><a href="module-fastimage.html">fastimage</a>.</span>FastImageError</h2>


</header>
Expand Down Expand Up @@ -232,7 +229,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-fa
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-beta2</a> on Sat Mar 28 2015 14:49:38 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Feb 23 2016 12:36:13 GMT+0000 (GMT)
</footer>

<script> prettyPrint(); </script>
Expand Down
7 changes: 2 additions & 5 deletions docs/module-fastimage.FastImageStream.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ <h1 class="page-title">Class: FastImageStream</h1>
<header>

<h2>
<span class="ancestors"><a href="module-fastimage.html">fastimage</a>.</span>

FastImageStream
</h2>
<span class="ancestors"><a href="module-fastimage.html">fastimage</a>.</span>FastImageStream</h2>


</header>
Expand Down Expand Up @@ -157,7 +154,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-fa
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-beta2</a> on Sat Mar 28 2015 14:49:38 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Feb 23 2016 12:36:13 GMT+0000 (GMT)
</footer>

<script> prettyPrint(); </script>
Expand Down
Loading

0 comments on commit 5d7a9c9

Please sign in to comment.