node-curl/lib/CurlBuilder.js
2013-02-19 20:54:47 +08:00

207 lines
5.7 KiB
JavaScript

// Generated by ToffeeScript 1.4.0
(function() {
var Curl, CurlBuilder,
__hasProp = {}.hasOwnProperty,
__slice = [].slice;
try {
Curl = require(__dirname + '/Curl');
} catch (e) {
Curl = require(__dirname + '/Curl');
}
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) {
var curl;
curl = function() {
return curl.perform.apply(curl, arguments);
};
curl.Curl = Curl;
curl.perform = function() {
var args, c, cb, 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 = {};
}
c = this.curl_;
c.chunks = [];
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.on('data', function(chunk) {
curl.log("receive " + chunk.length + " bytes");
c.chunks.push(chunk);
length += chunk.length;
return chunk.length;
});
c.on('end', function() {
var chunk, data, position, _i, _len, _ref5,
_this = this;
curl.log("receive succeeded.");
curl.running = false;
data = new Buffer(length);
position = 0;
_ref5 = c.chunks;
for (_i = 0, _len = _ref5.length; _i < _len; _i++) {
chunk = _ref5[_i];
chunk.copy(data, position);
position += chunk.length;
}
c.chunks = [];
if (c.options.RAW) {
curl.body = data;
} else {
curl.body = data.toString();
}
curl.status = curl.code = c.getinfo('RESPONSE_CODE');
return 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;
return 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);