Skip to content

Commit

Permalink
Merge 080734f into 89c8fac
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Oct 15, 2014
2 parents 89c8fac + 080734f commit 30e92e8
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ var zlib = require('zlib');
var ua = require('default-user-agent');
var digestAuthHeader = require('digest-header');
var typer = require('media-typer');

var util = require('util');
var EventEmitter = require('events').EventEmitter;

var _Promise;
var _iconv;

var pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '../', 'package.json')));
var pkg = require('../package.json');

var USER_AGENT = exports.USER_AGENT = ua('node-urllib', pkg.version);
var TIME_OUT = exports.TIME_OUT = 60000; // 60 seconds
Expand Down Expand Up @@ -344,8 +345,8 @@ exports.requestWithCallback = function (url, args, callback) {

cb(err, data, response);

if (args._events) {
args._events.emit('response', {
if (args.emitter) {
args.emitter.emit('response', {
error: err,
ctx: args.ctx,
req: {
Expand Down Expand Up @@ -626,47 +627,35 @@ function parseJSON(data) {
return result;
}


function HttpClient(options) {
EventEmitter.call(this);
options = options || {};
this.events = null;
this.agent = options.agent || exports.agent;
this.httpsAgent = options.httpsAgent || exports.httpsAgent;
}
util.inherits(HttpClient, EventEmitter);

HttpClient.prototype.request = function (url, args, callback) {
if (typeof args === 'function') {
callback = args;
args = null;
}
args = args || {};
args._events = this.events;
args.emitter = this;
args.agent = args.agent || this.agent;
args.httpsAgent = args.httpsAgent || this.httpsAgent;
return exports.request(url, args, callback);
};

HttpClient.prototype.requestThunk = function (url, args) {
args = args || {};
args._events = this.events;
args.emitter = this;
args.agent = args.agent || this.agent;
args.httpsAgent = args.httpsAgent || this.httpsAgent;
return exports.requestThunk(url, args);
};

HttpClient.prototype.on = function (event, listener) {
if (!this.events) {
this.events = new EventEmitter();
}
this.events.on(event, listener);
};

HttpClient.prototype.removeAllListeners = function (event) {
if (!this.events) {
return;
}
this.events.removeAllListeners(event);
};

exports.HttpClient = HttpClient;

exports.create = function (options) {
Expand Down

0 comments on commit 30e92e8

Please sign in to comment.