ant-http
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -80,3 +80,20 @@ const http = require('http'); | ||
this.get = function(url, callback, encoding='utf8') { | ||
/* | ||
options = { | ||
encoding, | ||
onData | ||
} | ||
*/ | ||
this.get = function(url, options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = { | ||
encoding : 'utf8' | ||
}; | ||
} | ||
else if (options.encoding === undefined) { | ||
options.encoding = 'utf8'; | ||
} | ||
var opts = this.parseUrl(url); | ||
@@ -101,10 +118,23 @@ opts.method = 'GET'; | ||
res.setEncoding(encoding); | ||
res.setEncoding(options.encoding); | ||
var get_data = ''; | ||
res.on('data', (data) => { | ||
get_data += data.toString(encoding); | ||
if (options.onData !== undefined && typeof options.onData === 'function') { | ||
options.onData(data); | ||
} | ||
if (options.toStream !== undefined) { | ||
options.toStream.write(data, {encoding : options.encoding}); | ||
} | ||
if (options.merge_data === undefined || options.merge_data !== false) { | ||
res_data += data.toString(options.encoding); | ||
} | ||
}); | ||
res.on('end', () => { | ||
if (options.toStream !== undefined) { | ||
options.toStream.end(); | ||
} | ||
return callback(null, get_data); | ||
@@ -128,3 +158,6 @@ }); | ||
headers, | ||
encoding | ||
encoding, | ||
onData, | ||
toStream, | ||
merge_data | default true | ||
} | ||
@@ -168,6 +201,19 @@ */ | ||
res.on('data', (data) => { | ||
res_data += data.toString(options.encoding); | ||
if (options.onData !== undefined && typeof options.onData === 'function') { | ||
options.onData(data); | ||
} | ||
if (options.toStream !== undefined) { | ||
options.toStream.write(data, {encoding : options.encoding}); | ||
} | ||
if (options.merge_data === undefined || options.merge_data !== false) { | ||
res_data += data.toString(options.encoding); | ||
} | ||
}); | ||
res.on('end', () => { | ||
if (options.toStream !== undefined) { | ||
options.toStream.end(); | ||
} | ||
return callback(null, res_data); | ||
@@ -311,3 +357,5 @@ }); | ||
if (options.method == 'GET') { | ||
return this.get(url, callback, 'binary'); | ||
return this.get(url, { | ||
encoding : 'binary' | ||
}, callback); | ||
} else if (options.method == 'POST') { | ||
@@ -321,2 +369,39 @@ return this.post(url, { | ||
/* | ||
method : GET | POST, | ||
data : Object if method == POST, | ||
target : FILE_PATH | ||
*/ | ||
this.downStream = function(url, options, callback) { | ||
var data_stream = fs.createWriteStream(options.target, {encoding : 'binary'}); | ||
data_stream.on('error', (err) => { | ||
return callback(err, null); | ||
}); | ||
data_stream.on('end', () => { | ||
return callback(null, true); | ||
}); | ||
if (options.method == 'GET') { | ||
return this.get(url, { | ||
encoding : 'binary', | ||
toStream : data_stream, | ||
merge_data : false | ||
}, (err, data) => { | ||
}); | ||
} else if (options.method == 'POST') { | ||
return this.post(url, { | ||
encoding : 'binary', | ||
data : options.data, | ||
toStream : data_stream, | ||
merge_data : false | ||
}, (err, data) => { | ||
}); | ||
} | ||
}; | ||
}; | ||
@@ -323,0 +408,0 @@ |
{ | ||
"name": "ant-http", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "simple http/https request lib", | ||
@@ -5,0 +5,0 @@ "main": "anthttp.js", |
12568
333