// Generated by ToffeeScript 1.6.2-5 (function() { var Curl, CurlBuilder, e, __hasProp = {}.hasOwnProperty, __slice = [].slice; try { Curl = require(__dirname + '/Curl'); } catch (_error) { e = _error; Curl = require(__dirname + '/Curl'); } function merge_chunks(chunks, length) { var chunk, data, position, _i, _len; data = new Buffer(length); position = 0; for (_i = 0, _len = chunks.length; _i < _len; _i++) { chunk = chunks[_i]; chunk.copy(data, position); position += chunk.length; } return data; }; CurlBuilder = (function() { function CurlBuilder() {} CurlBuilder.curls = {}; CurlBuilder.id = 0; CurlBuilder.close_all = function() { var curl, id, _ref; _ref = CurlBuilder.curls; for (id in _ref) { if (!__hasProp.call(_ref, id)) continue; curl = _ref[id]; curl.end(); delete CurlBuilder.curls[id]; } return CurlBuilder; }; CurlBuilder.create = function(defaultOptions) { function curl() { return curl.perform.apply(curl, arguments); }; curl.perform = function() { var args, c, cb, header_length, k, length, v, _ref, _ref1, _ref2, _ref3, _ref4; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; if (this.running) { throw new Error('the cURL session is busy, use curl.create to create another cURL Session'); } if (!this.curl_) { throw new Error('the cURL is closed.'); } this.running = true; cb = args.pop(); this.url = args[0], this.options = args[1]; if ((_ref = this.options) == null) { this.options = {}; } length = 0; header_length = 0; this.debug = (_ref1 = (_ref2 = this.defaultOptions.DEBUG) != null ? _ref2 : this.options.DEBUG) != null ? _ref1 : this.debug; this.effectiveOptions = {}; _ref3 = this.defaultOptions; for (k in _ref3) { v = _ref3[k]; this.effectiveOptions[k] = v; } _ref4 = this.options; for (k in _ref4) { v = _ref4[k]; this.effectiveOptions[k] = v; } this.setOptions(this.effectiveOptions); this.setOptions({ URL: this.url }); c = this.curl_; c.chunks = []; c.header_chunks = []; c.on('data', function(chunk) { curl.log("receive " + chunk.length + " bytes"); c.chunks.push(chunk); length += chunk.length; return chunk.length; }); c.on('header', function(chunk) { curl.log("receive " + chunk.length + " header"); c.header_chunks.push(chunk); header_length += chunk.length; return chunk.length; }); c.on('end', function() { var data, header, _this = this; curl.log("receive succeeded."); curl.running = false; data = merge_chunks(c.chunks, length); header = merge_chunks(c.header_chunks, header_length); c.chunks = []; c.header_chunks = []; if (c.options.RAW) { curl.body = data; curl.header = header; } else { curl.body = data.toString(); curl.header = header.toString(); } curl.status = curl.code = c.getinfo('RESPONSE_CODE'); process.nextTick(function() { return cb.call(curl, null, curl); }); }); c.on('error', function(err) { var _this = this; curl.log("receive failed: " + err.message); curl.running = false; process.nextTick(function() { return cb.call(curl, err, null); }); }); this.log('perform'); return c.perform(); }; curl.setDefaultOptions = function(options, reset) { if (options == null) { options = {}; } if (reset == null) { reset = true; } defaultOptions = options; if (reset) { this.log('Set default options and reset cURL'); return this.reset(); } }; curl.log = function(text) { if (this.debug) { return console.info(("[cURL " + this.id + "] ") + text); } }; curl.setOptions = function(options) { var k, v; if (options == null) { options = {}; } for (k in options) { if (!__hasProp.call(options, k)) continue; v = options[k]; this.log("Set option '" + k + "' to '" + v + "'"); this.curl_.setopt(k, v); } return this; }; curl.setopts = function(options) { if (options == null) { options = {}; } return this.setOptions(options); }; curl.info = function(info) { if (this.curl_ == null) { throw new Error('curl is closed'); } return this.curl_.getinfo(info); }; curl.end = function() { if (this.curl_ != null) { this.curl_.close(); } this.curl_ = null; this.body = null; delete CurlBuilder.curls[this.id]; return this.log("closed."); }; curl.close = function() { return this.end(); }; curl.open = function() { if (curl.id == null) { curl.id = ++CurlBuilder.id; } this.log("opening."); this.curl_ = new Curl(); this.curl_.options = {}; this.defaultOptions = defaultOptions != null ? defaultOptions : {}; CurlBuilder.curls[curl.id] = curl; return this.log("opened."); }; curl.reset = function() { this.log('reset'); if (this.curl_) { this.end(); } return this.open(); }; curl.create = function(defaultOptions) { return CurlBuilder.create(defaultOptions); }; curl.get_count = function() { return Curl.get_count(); }; curl.open(); return curl; }; return CurlBuilder; }).call(this); process.on('exit', function() { return CurlBuilder.close_all(); }); module.exports = CurlBuilder; }).call(this);