Merge pull request #413 from uruz/feature/fix-failing-test

Fix failing test: fabric.util.loadImage
This commit is contained in:
Juriy Zaytsev 2013-02-22 08:22:09 -08:00
commit 4651d6ecd9

View file

@ -14,20 +14,12 @@
/** @private */
function request(url, encoding, callback) {
var oURL = URL.parse(url),
client = HTTP.createClient(oURL.port, oURL.hostname),
req = client.request('GET', oURL.pathname, { 'host': oURL.hostname });
client.addListener('error', function(err) {
if (err.errno === process.ECONNREFUSED) {
fabric.log('ECONNREFUSED: connection refused to ' + client.host + ':' + client.port);
}
else {
fabric.log(err.message);
}
});
req.end();
req.on('response', function (response) {
req = HTTP.request({
hostname: oURL.hostname,
port: oURL.port,
path: oURL.pathname,
method: 'GET'
}, function(res){
var body = "";
if (encoding) {
response.setEncoding(encoding);
@ -41,21 +33,46 @@
}
});
});
req.on('error', function(err) {
if (err.errno === process.ECONNREFUSED) {
fabric.log('ECONNREFUSED: connection refused to ' + oURL.hostname + ':' + oURL.port);
}
else {
fabric.log(err.message);
}
});
}
function request_fs(url, callback){
var fs = require('fs'),
stream = fs.createReadStream(url),
body = '';
stream.on('data', function(chunk){
body += chunk;
});
stream.on('end', function(){
callback(body);
});
};
fabric.util.loadImage = function(url, callback, context) {
var createImageAndCallBack = function(data){
img.src = new Buffer(data, 'binary');
// preserving original url, which seems to be lost in node-canvas
img._src = url;
callback && callback.call(context, img);
};
var img = new Image();
if (url && url.indexOf('data') === 0) {
img.src = img._src = url;
callback && callback.call(context, img);
}
else if (url && url.indexOf('http') !== 0) {
request_fs(url, createImageAndCallBack);
}
else if (url) {
request(url, 'binary', function(body) {
img.src = new Buffer(body, 'binary');
// preserving original url, which seems to be lost in node-canvas
img._src = url;
callback && callback.call(context, img);
});
request(url, 'binary', createImageAndCallBack);
}
};