node-curl/lib/Curl.js
2013-04-10 03:09:25 +08:00

169 lines
4.3 KiB
JavaScript

// Generated by ToffeeScript 1.6.2
(function() {
var Curl, curls, e, id, m, p,
__hasProp = {}.hasOwnProperty;
try {
Curl = require(__dirname + '/../build/Release/node-curl').Curl;
} catch (_error) {
e = _error;
Curl = require(__dirname + '/../build/default/node-curl').Curl;
}
Curl.prototype.setopt_user_ = function(option_id, value) {
return this.options[option_id] = value;
};
Curl.prototype.setopt_httppost = function(rows) {
var cols, k, option_id, row, v;
this.httppost = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = rows.length; _i < _len; _i++) {
row = rows[_i];
cols = [];
for (k in row) {
if (!__hasProp.call(row, k)) continue;
v = row[k];
k = k.toUpperCase();
if ((option_id = Curl.httppost_options[k]) != null) {
cols.push(option_id);
if (!(v instanceof Buffer)) {
v = new Buffer(v.toString());
}
cols.push(v);
} else {
throw new Error("invalid http post option " + k);
}
}
_results.push(cols);
}
return _results;
})();
this.setopt_httppost_(this.httppost);
return this;
};
Curl.prototype.setopt = function(option_name, value) {
var option, option_id;
option = option_name.toUpperCase();
if ((option_id = Curl.user_options[option]) != null) {
if (option === 'MULTIPART') {
this.setopt_httppost(value);
} else {
this.setopt_user_(option_id, value);
}
} else if ((option_id = Curl.slist_options[option]) != null) {
this.setopt_slist_(option_id, value);
} else if ((option_id = Curl.integer_options[option]) != null) {
this.setopt_int_(option_id, value >> 0);
} else if ((option_id = Curl.string_options[option]) != null) {
if (value == null) {
throw new Error("Cannot set option " + option_name + " to " + value + ".");
}
this.setopt_str_(option_id, value.toString());
} else {
throw new Error("unsupported option " + option);
}
return this;
};
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',
MULTIPART: 'MULTIPART'
};
id = 0;
curls = {};
Curl.prototype.on = function(event, callback) {
var _this = this;
switch (event) {
case 'data':
this.on_write = function(chunk) {
return callback.call(_this, chunk);
};
break;
case 'error':
this.on_error = function(e) {
delete curls[_this.id];
return callback.call(_this, e);
};
break;
case 'end':
this.on_end = function() {
delete curls[_this.id];
return callback.call(_this);
};
break;
default:
throw new Error("invalid event type " + event);
}
return this;
};
Curl.prototype.close = function() {
delete curls[this.id];
return this.close_();
};
Curl.prototype.perform = function() {
this.id = ++id;
curls[this.id] = this;
this.perform_();
Curl.process();
return this;
};
m = 0;
p = console.log;
Curl.process = function() {
var once;
if (Curl.in_process) {
return;
}
return (once = function() {
var n, w;
n = Curl.process_();
if (n > 0) {
Curl.in_process = true;
if (n > 8192 && m < 10) {
++m;
return process.nextTick(once);
} else {
m = 0;
w = (8192 - n) * 80 / 8192 >> 0;
if (w < 0) {
w = 0;
}
return setTimeout(once, w);
}
} else {
return Curl.in_process = false;
}
})();
};
module.exports = Curl;
}).call(this);