node-curl/lib/Curl.toffee
2013-02-19 21:09:39 +08:00

89 lines
2.1 KiB
Text

try
{Curl} = require __dirname + '/../build/Release/node-curl'
catch e
{Curl} = require __dirname + '/../build/default/node-curl'
Curl::setopt_user_ = (option_id, value) ->
@options[option_id] = value
Curl::setopt = (option_name, value) ->
option = option_name.toUpperCase()
# slist must be at the top of condition
# the option exists in string_options too
if (option_id = Curl.user_options[option])?
@setopt_user_ option_id, value
else if (option_id = Curl.slist_options[option])?
@setopt_slist_ option_id, value
else if (option_id = Curl.integer_options[option])?
@setopt_int_ option_id, value >> 0
else if (option_id = Curl.string_options[option])?
if !value?
throw new Error("Cannot set option #{option_name} to #{value}.")
@setopt_str_ option_id, value.toString()
else
throw new Error("unsupported option #{option}")
@
Curl::getinfo = (oinfo) ->
info = oinfo.toUpperCase()
if (info_id = Curl.slist_infos[info])?
@getinfo_slist_(info_id)
else 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("unsupported info #{oinfo}")
Curl.user_options =
RAW: 'RAW'
DEBUG: 'DEBUG'
# on 'data' must be returns the chunk length
Curl::on = (event, callback) ->
switch event
when 'data'
# (Buffer chunk) ->
@on_write = callback
when 'error'
# (Error error) ->
@on_error = callback
when 'end'
# () ->
@on_end = callback
else
throw new Error("invalid event type #{event}")
@
Curl::perform = ->
@perform_()
Curl.process()
@
m = 0
p = console.log
Curl.process = ->
if Curl.in_process
return
do once = ->
n = Curl.process_()
if n > 0
Curl.in_process = true
if n > 8192 && m < 10
++m
process.nextTick once
else
m = 0
w = (8192 - n) * 80 / 8192
if w < 0
w = 0
setTimeout once, w
else
Curl.in_process = false
module.exports = Curl
# vim: sw=2 ts=2 sts=2 expandtab :