diff --git a/src/node.js b/src/node.js index 7e09af0f..9a0d6143 100644 --- a/src/node.js +++ b/src/node.js @@ -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); } };