diff --git a/lib/bind.js b/lib/bind.js new file mode 100644 index 0000000..bdd1062 --- /dev/null +++ b/lib/bind.js @@ -0,0 +1,5 @@ +module.exports = function bind(fn, context) { + return function __bind__ () { + return fn.apply(context, arguments) + } +} diff --git a/lib/client.js b/lib/client.js index b875461..3690a78 100644 --- a/lib/client.js +++ b/lib/client.js @@ -4,6 +4,7 @@ var http = require('http') , Serializer = require('./serializer') , Deserializer = require('./deserializer') , Cookies = require('./cookies') + , bind = require('./bind') /** * Creates a Client object for making XML-RPC method calls. @@ -103,7 +104,7 @@ Client.prototype.methodCall = function methodCall(method, params, callback) { options.headers['Content-Length'] = Buffer.byteLength(xml, 'utf8') this.headersProcessors.composeRequest(options.headers) - var request = transport.request(options, function(response) { + var request = transport.request(options, bind(function(response) { var body = [] response.on('data', function (chunk) { body.push(chunk) }) @@ -130,7 +131,7 @@ Client.prototype.methodCall = function methodCall(method, params, callback) { callback(err, result) }) } - }.bind(this)) + }, this)) request.on('error', callback) request.write(xml, 'utf8') diff --git a/lib/cookies.js b/lib/cookies.js index 0d495bc..aab63c0 100644 --- a/lib/cookies.js +++ b/lib/cookies.js @@ -1,3 +1,5 @@ +var bind = require('./bind') + /** * Creates object for cookies manipulation on client side. * Allows to parse server's response in order to get cookies and compose http request to transfer cookies to the server @@ -66,7 +68,7 @@ Cookies.prototype = { parseResponse: function(headers) { var cookies = headers['set-cookie'] if (cookies) { - cookies.forEach(function(c) { + cookies.forEach(bind(function(c) { var cookiesParams = c.split(';') var cookiePair = cookiesParams.shift().split('=') var options = {} @@ -78,7 +80,7 @@ Cookies.prototype = { } }) this.set(cookiePair[0].trim(), cookiePair[1].trim(), options) - }.bind(this)) + }, this)) } }, @@ -101,10 +103,10 @@ Cookies.prototype = { */ toString: function() { return Object.keys(this.cookies) - .filter(this.checkNotExpired.bind(this)) - .map(function(name) { - return name + '=' + this.cookies[name].value - }.bind(this)).join(';') + .filter(bind(this.checkNotExpired, this)) + .map(bind(function(name) { + return name + '=' + this.cookies[name].value + }, this)).join(';') } } diff --git a/lib/deserializer.js b/lib/deserializer.js index 819356a..5edf0cc 100644 --- a/lib/deserializer.js +++ b/lib/deserializer.js @@ -1,5 +1,6 @@ var sax = require('sax') , dateFormatter = require('./date_formatter') + , bind = require('./bind') var Deserializer = function(encoding) { this.type = null @@ -14,12 +15,12 @@ var Deserializer = function(encoding) { this.error = null this.parser = sax.createStream() - this.parser.on('opentag', this.onOpentag.bind(this)) - this.parser.on('closetag', this.onClosetag.bind(this)) - this.parser.on('text', this.onText.bind(this)) - this.parser.on('cdata', this.onCDATA.bind(this)) - this.parser.on('end', this.onDone.bind(this)) - this.parser.on('error', this.onError.bind(this)) + this.parser.on('opentag', bind(this.onOpentag, this)) + this.parser.on('closetag', bind(this.onClosetag, this)) + this.parser.on('text', bind(this.onText, this)) + this.parser.on('cdata', bind(this.onCDATA, this)) + this.parser.on('end', bind(this.onDone, this)) + this.parser.on('error', bind(this.onError, this)) } Deserializer.prototype.deserializeMethodResponse = function(stream, callback) { @@ -44,7 +45,7 @@ Deserializer.prototype.deserializeMethodResponse = function(stream, callback) { } stream.setEncoding(this.encoding) - stream.on('error', this.onError.bind(this)) + stream.on('error', bind(this.onError, this)) stream.pipe(this.parser) } @@ -67,7 +68,7 @@ Deserializer.prototype.deserializeMethodCall = function(stream, callback) { } stream.setEncoding(this.encoding) - stream.on('error', this.onError.bind(this)) + stream.on('error', bind(this.onError, this)) stream.pipe(this.parser) } diff --git a/lib/server.js b/lib/server.js index baef3b6..b0ef25c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -4,6 +4,7 @@ var http = require('http') , EventEmitter = require('events').EventEmitter , Serializer = require('./serializer') , Deserializer = require('./deserializer') + , bind = require('./bind') /** * Creates a new Server object. Also creates an HTTP server to start listening @@ -62,13 +63,13 @@ function Server(options, isSecure, onListening) { this.httpServer = isSecure ? https.createServer(options, handleMethodCall) : http.createServer(handleMethodCall) - process.nextTick(function() { + process.nextTick(bind(function() { this.httpServer.listen(options.port, options.host, onListening) - }.bind(this)) - this.close = function(callback) { + }, this)) + this.close = bind(function(callback) { this.httpServer.once('close', callback) this.httpServer.close() - }.bind(this) + }, this) } // Inherit from EventEmitter to emit and listen