Skip to content

Commit

Permalink
Fixed coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shogun committed Mar 24, 2016
1 parent 841c279 commit 17b9c50
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 2016-03-23 / 2.0.0

* **Dropped support for Node < 5.**
* `fastimage.info` always includes `realPath` and `realUrl` instead of omitting them if equals to `path` and `url`.

### 2016-03-08 / 1.2.0

Expand Down
6 changes: 3 additions & 3 deletions docs/module-fastimage.html
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ <h4 class="name" id=".info"><span class="type-signature">&lt;static> </span>info
&quot;width&quot;: 1000, // The width of the image in pixels.
&quot;height&quot;: 1000, // The height of the image in pixels.
&quot;type&quot;: &quot;gif&quot;, // The type of the image. Can be `bmp`, `gif`, `jpeg`, `png`, `psd`, `tif`, `webp` or `svg`.
&quot;subject&quot;: &quot;http://placehold.it/1000x1000.gif&quot;, // The original URL of the image.
&quot;realUrl&quot;: &quot;http://placehold.it/1000x1000.gif&quot;, // The real URL of the image after all the redirects. It will be omitted if equals to the URL.
&quot;url&quot;: &quot;http://placehold.it/1000x1000.gif&quot;, // The original URL of the image.
&quot;realUrl&quot;: &quot;http://placehold.it/1000x1000.gif&quot;, // The real URL of the image after all the redirects.
&quot;size&quot;: 24090, // The size of the image (in bytes). Present only if the server returned the Content-Length HTTP header.
&quot;transferred&quot;: 979, // The amount of data transferred (in bytes) to identify the image.
&quot;time&quot;: 171.43721 // The time required for the operation, in milliseconds.
Expand All @@ -184,7 +184,7 @@ <h4 class="name" id=".info"><span class="type-signature">&lt;static> </span>info
&quot;height&quot;: 150, // The height of the image in pixels.
&quot;type&quot;: &quot;png&quot;, // The type of the image. Can be `bmp`, `gif`, `jpeg`, `png`, `psd`, `tif`, `webp` or `svg`.
&quot;path&quot;: &quot;1.png&quot;, // The original path of the image.
&quot;realPath&quot;: &quot;/home/user/1.png&quot;, // The absolute path of the image. It will be omitted if equals to the path.
&quot;realPath&quot;: &quot;/home/user/1.png&quot;, // The absolute path of the image.
&quot;size&quot;: 24090, // The size (in bytes) of the image.
&quot;time&quot;: 14.00558 // The time required for the operation, in milliseconds.
}</code></pre><p>When the source is a Buffer the object will be similar to this:</p>
Expand Down
2 changes: 1 addition & 1 deletion docs/quicksearch.html

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,11 @@ const identify = function(buffer, url, realUrl, size, time){

// Set URL informations.
info.url = url;
if(realUrl !== url)
info.realUrl = realUrl;
info.realUrl = realUrl;

// Set file size informations.
info.transferred = buffer.length;
if(size)
info.size = size;
info.size = size;

// Set time informations.
elapsed = process.hrtime(time);
Expand Down Expand Up @@ -154,8 +152,7 @@ const analyzeFile = function(relativePath, callback){

// Set path informations.
info.path = relativePath;
if(path !== realPath)
info.realPath = realPath;
info.realPath = realPath;

// Set size and time informations.
info.size = stats.size;
Expand Down Expand Up @@ -185,7 +182,9 @@ const analyzeRemote = function(url, callback){
request.get(params)
.on("response", function(response){
realUrl = response.request.uri.href;
size = parseFloat(response.headers["content-length"]);

if(response.headers["content-length"])
size = parseInt(response.headers["content-length"], 0);

if(response.statusCode / HTTP_RESPONSE_CLASS_FACTOR !== 2){
this.abort();
Expand Down
4 changes: 2 additions & 2 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const FastImageError = function(message, attributes){

// Setup inspecting
this.name = "FastImageError";
this.message = message || "";
this.message = message;

// Copy other properties
Object.keys(attributes).forEach(key => {
Expand Down Expand Up @@ -55,7 +55,7 @@ module.exports = {

if(message)
finalError = new FastImageError(message, {url, code: "NETWORK_ERROR", originalError: error});
else if(error.code === "HTTP_ERROR")
else
finalError = new FastImageError("Unexpected response from the remote host.", {url, code: "SERVER_ERROR", httpCode: error.status});

return finalError;
Expand Down
36 changes: 5 additions & 31 deletions lib/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,10 @@ const FastImageStream = function(){
const options = arguments[arguments.length - 1] || {};

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

Duplex.call(this, options);

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

if(arguments.length === 2)
this.source = arguments[0];
Expand All @@ -41,7 +35,7 @@ const FastImageStream = function(){

this.on("finish", function(){
if(!this.info)
this._failAnalysis();
this.emit("error", this.error);
});
};
util.inherits(FastImageStream, Duplex);
Expand All @@ -58,23 +52,8 @@ FastImageStream.prototype = util._extend(FastImageStream.prototype, {
_write(chunk, encoding, callback){
"use strict";

let info = null;

try{
// Attemp identification.
this.buffer = Buffer.concat([this.buffer, chunk]);
info = imageSize(this.buffer);

// Setup informations.
this._finalizeAnalysis(info);

// Push the data and close.
this.push(this.info);
}catch(e){
if(this.buffer.length > core.manageThreshold())
this.end();
}

this.buffer = Buffer.concat([this.buffer, chunk]);
this._analyze(this.buffer);
callback();
},

Expand Down Expand Up @@ -108,11 +87,6 @@ FastImageStream.prototype = util._extend(FastImageStream.prototype, {
this.emit("type", info.type);

this.info = info;
},

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

Expand Down
6 changes: 3 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ module.exports = {
* "width": 1000, // The width of the image in pixels.
* "height": 1000, // The height of the image in pixels.
* "type": "gif", // The type of the image. Can be `bmp`, `gif`, `jpeg`, `png`, `psd`, `tif`, `webp` or `svg`.
* "subject": "http://placehold.it/1000x1000.gif", // The original URL of the image.
* "realUrl": "http://placehold.it/1000x1000.gif", // The real URL of the image after all the redirects. It will be omitted if equals to the URL.
* "url": "http://placehold.it/1000x1000.gif", // The original URL of the image.
* "realUrl": "http://placehold.it/1000x1000.gif", // The real URL of the image after all the redirects.
* "size": 24090, // The size of the image (in bytes). Present only if the server returned the Content-Length HTTP header.
* "transferred": 979, // The amount of data transferred (in bytes) to identify the image.
* "time": 171.43721 // The time required for the operation, in milliseconds.
Expand All @@ -47,7 +47,7 @@ module.exports = {
* "height": 150, // The height of the image in pixels.
* "type": "png", // The type of the image. Can be `bmp`, `gif`, `jpeg`, `png`, `psd`, `tif`, `webp` or `svg`.
* "path": "1.png", // The original path of the image.
* "realPath": "/home/user/1.png", // The absolute path of the image. It will be omitted if equals to the path.
* "realPath": "/home/user/1.png", // The absolute path of the image.
* "size": 24090, // The size (in bytes) of the image.
* "time": 14.00558 // The time required for the operation, in milliseconds.
* }
Expand Down
14 changes: 14 additions & 0 deletions test/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ describe("using callbacks", function(){
});
});

it("should return a error when the URL is not a image when downloading the entire file", done => {
fastimage.threshold(-1);
fastimage.info("http://www.google.com/robots.txt", (error, info) => {
verify(done, () => {
expect(info).not.to.exist;
expect(error).to.be.instanceof(fastimage.FastImageError);

expect(error.code).to.equal("UNSUPPORTED_TYPE");
expect(error.message).to.equal("Unsupported image file.");
fastimage.threshold(null);
});
});
});

it("should handle connection timeouts", done => {
fastimage.timeout(1);

Expand Down

0 comments on commit 17b9c50

Please sign in to comment.