diff --git a/index.toffee b/index.toffee index 1d34e60..32e87d7 100644 --- a/index.toffee +++ b/index.toffee @@ -1 +1,2 @@ -module.exports = require './lib/curl' +CurlBuilder = require './lib/CurlBuilder' +module.exports = CurlBuilder.create() diff --git a/lib/curl.toffee b/lib/Curl.toffee similarity index 83% rename from lib/curl.toffee rename to lib/Curl.toffee index 4eb1d8c..3fc45ed 100644 --- a/lib/curl.toffee +++ b/lib/Curl.toffee @@ -3,8 +3,6 @@ try catch e {Curl} = require __dirname + '/../build/default/node-curl' -CurlBuilder = require __dirname + '/CurlBuilder' - Curl::setopt_user_ = (option_id, value) -> @options[option_id] = value @@ -41,6 +39,18 @@ Curl.user_options = RAW: 'RAW' DEBUG: 'DEBUG' +Curl::on = (event, callback) -> + switch event + when 'data' + @on_write = callback + when 'error' + @on_error = callback + when 'end' + @on_end = callback + else + throw new Error("invalid event type #{event}") + @ + Curl::perform = -> @perform_() Curl.process() @@ -56,5 +66,6 @@ Curl.process = -> else Curl.in_process = false -curl = CurlBuilder.create() -module.exports = curl + +module.exports = Curl +# vim: sw=2 ts=2 sts=2 expandtab : diff --git a/lib/CurlBuilder.js b/lib/CurlBuilder.js index 30a3a07..15b8b16 100644 --- a/lib/CurlBuilder.js +++ b/lib/CurlBuilder.js @@ -5,9 +5,9 @@ __slice = [].slice; try { - Curl = require(__dirname + '/../build/Release/node-curl').Curl; + Curl = require(__dirname + '/Curl'); } catch (e) { - Curl = require(__dirname + '/../build/default/node-curl').Curl; + Curl = require(__dirname + '/Curl'); } CurlBuilder = (function() { @@ -69,12 +69,13 @@ this.setOptions({ URL: this.url }); - c.on_write = function(chunk) { + c.on('data', function(chunk) { curl.log("receive " + chunk.length + " bytes"); c.chunks.push(chunk); - return length += chunk.length; - }; - c.on_end = function() { + length += chunk.length; + return chunk.length; + }); + c.on('end', function() { var chunk, data, position, _i, _len, _ref5, _this = this; curl.log("receive succeeded."); @@ -97,15 +98,15 @@ return process.nextTick(function() { return cb.call(curl, null, curl); }); - }; - c.on_error = function(err) { + }); + c.on('error', function(err) { var _this = this; curl.log("receive failed: " + err.message); curl.running = false; return process.nextTick(function() { return cb.call(curl, err, null); }); - }; + }); this.log('perform'); return c.perform(); }; diff --git a/lib/CurlBuilder.toffee b/lib/CurlBuilder.toffee index 54d1ebe..1ffbb7c 100644 --- a/lib/CurlBuilder.toffee +++ b/lib/CurlBuilder.toffee @@ -1,7 +1,7 @@ try - {Curl} = require __dirname + '/../build/Release/node-curl' + Curl = require __dirname + '/Curl' catch e - {Curl} = require __dirname + '/../build/default/node-curl' + Curl = require __dirname + '/Curl' class CurlBuilder @curls: {} @@ -47,12 +47,13 @@ class CurlBuilder @setOptions @effectiveOptions @setOptions {URL: @url} - c.on_write = (chunk) -> + c.on 'data', (chunk) -> curl.log "receive #{chunk.length} bytes" c.chunks.push chunk length += chunk.length + chunk.length - c.on_end = -> + c.on 'end', -> curl.log "receive succeeded." curl.running = false data = new Buffer(length) @@ -72,7 +73,7 @@ class CurlBuilder process.nextTick! cb.call curl, null, curl - c.on_error = (err)-> + c.on 'error', (err)-> curl.log "receive failed: #{err.message}" curl.running = false process.nextTick! @@ -146,3 +147,5 @@ process.on 'exit', -> CurlBuilder.close_all() module.exports = CurlBuilder + +# vim: sw=2 ts=2 sts=2 expandtab : diff --git a/lib/CurlInstance.js b/lib/CurlInstance.js new file mode 100644 index 0000000..9883a7c --- /dev/null +++ b/lib/CurlInstance.js @@ -0,0 +1,6 @@ +// Generated by ToffeeScript 1.4.0 +(function() { + + + +}).call(this); diff --git a/lib/curl.js b/lib/curl.js deleted file mode 100644 index a076296..0000000 --- a/lib/curl.js +++ /dev/null @@ -1,80 +0,0 @@ -// Generated by ToffeeScript 1.4.0 -(function() { - var Curl, CurlBuilder, curl; - - try { - Curl = require(__dirname + '/../build/Release/node-curl').Curl; - } catch (e) { - Curl = require(__dirname + '/../build/default/node-curl').Curl; - } - - CurlBuilder = require(__dirname + '/CurlBuilder'); - - Curl.prototype.setopt_user_ = function(option_id, value) { - return this.options[option_id] = value; - }; - - Curl.prototype.setopt = function(ooption, value) { - var option, option_id; - option = ooption.toUpperCase(); - if ((option_id = Curl.user_options[option]) != null) { - return this.setopt_user_(option_id, value); - } else if ((option_id = Curl.slist_options[option]) != null) { - return this.setopt_slist_(option_id, value); - } else if ((option_id = Curl.integer_options[option]) != null) { - return this.setopt_int_(option_id, value >> 0); - } else if ((option_id = Curl.string_options[option]) != null) { - return this.setopt_str_(option_id, value.toString()); - } else { - throw new Error("unsupported option " + option); - } - }; - - Curl.prototype.getinfo = function(oinfo) { - var info, info_id; - info = oinfo.toUpperCase(); - if ((info_id = Curl.slist_infos[info]) != null) { - return this.getinfo_slist_(info_id); - } else if ((info_id = Curl.integer_infos[info]) != null) { - return this.getinfo_int_(info_id); - } else if ((info_id = Curl.string_infos[info]) != null) { - return this.getinfo_str_(info_id); - } else if ((info_id = Curl.double_infos[info]) != null) { - return this.getinfo_double_(info_id); - } else { - throw new Error("unsupported info " + oinfo); - } - }; - - Curl.user_options = { - RAW: 'RAW', - DEBUG: 'DEBUG' - }; - - Curl.prototype.perform = function() { - this.perform_(); - return Curl.process(); - }; - - Curl.process = function() { - var once; - if (Curl.in_process) { - return; - } - return (once = function() { - var num; - num = Curl.process_(); - if (num > 0) { - Curl.in_process = true; - return setTimeout(once, 80); - } else { - return Curl.in_process = false; - } - })(); - }; - - curl = CurlBuilder.create(); - - module.exports = curl; - -}).call(this);