first commit

This commit is contained in:
jiangfriend@gmail.com 2012-02-14 14:56:03 +08:00
commit f8f934a119
18 changed files with 901 additions and 0 deletions

120
lib/curl.js Normal file
View file

@ -0,0 +1,120 @@
// Generated by CoffeeScript 1.1.4-3
(function() {
var Curl, curl, curl_id,
__slice = [].slice,
__hasProp = {}.hasOwnProperty;
try {
Curl = require(__dirname + '/../build/Release/node-curl').Curl;
} catch (e) {
Curl = require(__dirname + '/../build/default/node-curl').Curl;
}
Curl.prototype.setopt = function(ooption, value) {
var option, option_id;
option = ooption.toUpperCase();
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.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("unsupproted info " + oinfo);
}
};
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_id = 0;
curl = function() {
var args, c, cb, chunks, k, length, options, res, url, v;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
cb = args.pop();
url = args[0], options = args[1];
if (options == null) options = {};
c = new Curl();
c.id = ++curl_id;
c.setopt('FOLLOWLOCATION', 1);
c.setopt('ACCEPT_ENCODING', 'gzip');
chunks = [];
length = 0;
res = {};
for (k in options) {
if (!__hasProp.call(options, k)) continue;
v = options[k];
c.setopt(k, v);
}
c.on_write = function(chunk) {
chunks.push(chunk);
length += chunk.length;
return console.info("on_write " + c.id + " " + chunk.length);
};
c.on_end = function() {
var chunk, data, i, position, st, _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;
}
st = Date.now();
i = 0;
while (Date.now() - st < 500) {
++i;
}
res.body = data;
res.status = res.code = c.getinfo('RESPONSE_CODE');
res.info = function(info) {
return c.getinfo(info);
};
console.info("id: " + c.id);
return cb(null, res);
};
c.on_error = function(err) {
var _this = this;
return process.nextTick(function() {
return cb(err, null);
});
};
c.setopt('URL', url);
return c.perform();
};
curl.Curl = Curl;
module.exports = curl;
}).call(this);

95
lib/curl.toffee Normal file
View file

@ -0,0 +1,95 @@
try
{Curl} = require __dirname + '/../build/Release/node-curl'
catch e
{Curl} = require __dirname + '/../build/default/node-curl'
Curl::setopt = (ooption, value) ->
option = ooption.toUpperCase()
if (option_id = Curl.integer_options[option])?
@setopt_int_ option_id, value >> 0
else if (option_id = Curl.string_options[option])?
@setopt_str_ option_id, value.toString()
else
throw new Error("unsupported option #{option}")
Curl::getinfo = (oinfo) ->
info = oinfo.toUpperCase()
if (info_id = Curl.integer_infos[info])?
@getinfo_int_(info_id)
else if (info_id = Curl.string_infos[info])?
@getinfo_str_(info_id)
else if (info_id = Curl.double_infos[info])?
@getinfo_double_(info_id)
else
throw new Error("unsupproted info #{oinfo}")
Curl::perform = ->
@perform_()
Curl.process()
Curl.process = ->
if Curl.in_process
return
do once = ->
num = Curl.process_()
if num > 0
Curl.in_process = true
setTimeout once, 80
else
Curl.in_process = false
# url, [options], cb
curl_id = 0
curl = (args...) ->
cb = args.pop()
[url, options] = args
options ?= {}
c = new Curl()
c.id = ++curl_id
c.setopt 'FOLLOWLOCATION', 1
c.setopt 'ACCEPT_ENCODING', 'gzip'
chunks = []
length = 0
res = {}
for own k, v of options
c.setopt k, v
c.on_write = (chunk) ->
chunks.push chunk
length += chunk.length
console.info "on_write #{c.id} #{chunk.length}"
c.on_end = ->
data = new Buffer(length)
position = 0
for chunk in chunks
chunk.copy data, position
position += chunk.length
# Strange Issue
# use data.toString() will cause parallel http request terminated? eg yahoo.com
st = Date.now()
i = 0
while Date.now() - st < 500
++i
res.body = data #.toString()
res.status = res.code = c.getinfo('RESPONSE_CODE')
res.info = (info)->
c.getinfo(info)
# 当curl返回过快且cb循环调用回导致堆栈溢出
# process.nextTick!
console.info "id: #{c.id}"
cb null, res
c.on_error = (err)->
process.nextTick!
cb err, null
c.setopt('URL', url)
c.perform()
curl.Curl = Curl
module.exports = curl