jenkins-bot has submitted this change and it was merged.

Change subject: T128659: Handle async createSocket
......................................................................


T128659: Handle async createSocket

 * Apparently this method isn't public and my claim in ffaeda52 was
   bogus all along. In any case, in node v5.7 it changes.

Change-Id: I90783bc1fe6b37370591390c9a9aec1a56cba3d2
---
M lib/mw/ApiRequest.js
1 file changed, 44 insertions(+), 17 deletions(-)

Approvals:
  Subramanya Sastry: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/mw/ApiRequest.js b/lib/mw/ApiRequest.js
index 5b5ade3..5d8de3c 100644
--- a/lib/mw/ApiRequest.js
+++ b/lib/mw/ApiRequest.js
@@ -5,6 +5,7 @@
 var events = require('events');
 var request = require('request');
 var util = require('util');
+var semver = require('semver');
 
 var Promise = require('../utils/promise.js');
 
@@ -19,31 +20,57 @@
        var http = require(protocol);
        var Agent = http.Agent;
 
-       // Many concurrent connections to the same host
        function ConnectTimeoutAgent() {
                Agent.apply(this, arguments);
        }
        util.inherits(ConnectTimeoutAgent, Agent);
 
        ConnectTimeoutAgent.prototype.createSocket = function() {
-               var s = Agent.prototype.createSocket.apply(this, arguments);
-               // Set up a connect timeout if connectTimeout option is set
-               if (this.options.connectTimeout && !s.connectTimeoutTimer) {
-                       s.connectTimeoutTimer = setTimeout(function() {
-                               var e = new Error('ETIMEDOUT');
-                               e.code = 'ETIMEDOUT';
-                               s.end();
-                               s.emit('error', e);
-                               s.destroy();
-                       }, this.options.connectTimeout);
-                       s.once('connect',  function() {
-                               if (this.connectTimeoutTimer) {
-                                       clearTimeout(this.connectTimeoutTimer);
-                                       this.connectTimeoutTimer = undefined;
+               var args = Array.from(arguments);
+               var options = this.options;
+               var cb = null;
+               function setup(err, s) {
+                       if (err) {
+                               if (typeof cb === 'function') {
+                                       cb(err, s);
                                }
-                       });
+                               return;
+                       }
+                       // Set up a connect timeout if connectTimeout option is 
set
+                       if (options.connectTimeout && !s.connectTimeoutTimer) {
+                               s.connectTimeoutTimer = setTimeout(function() {
+                                       var e = new Error('ETIMEDOUT');
+                                       e.code = 'ETIMEDOUT';
+                                       s.end();
+                                       s.emit('error', e);
+                                       s.destroy();
+                               }, options.connectTimeout);
+                               s.once('connect',  function() {
+                                       if (s.connectTimeoutTimer) {
+                                               
clearTimeout(s.connectTimeoutTimer);
+                                               s.connectTimeoutTimer = 
undefined;
+                                       }
+                               });
+                       }
+                       if (typeof cb === 'function') {
+                               cb(null, s);
+                       }
                }
-               return s;
+               // Unfortunately, `createSocket` is not a public method of Agent
+               // and, in v5.7 of node, it switched to being an asynchronous 
method.
+               // `setup` is passed in to remain compatible going forward, 
while
+               // we continue to return the socket if the synchronous method is
+               // feature detected.
+               var sufficientNodeVersion = semver.gte(process.version, 
'5.7.0');
+               if (sufficientNodeVersion) {
+                       cb = args[2];
+                       args[2] = setup;
+               }
+               var sock = Agent.prototype.createSocket.apply(this, args);
+               if (!sufficientNodeVersion) {
+                       setup(null, sock);
+                       return sock;
+               }
        };
 
        http.globalAgent = new ConnectTimeoutAgent({

-- 
To view, visit https://gerrit.wikimedia.org/r/273523
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I90783bc1fe6b37370591390c9a9aec1a56cba3d2
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Arlolra <[email protected]>
Gerrit-Reviewer: Arlolra <[email protected]>
Gerrit-Reviewer: Cscott <[email protected]>
Gerrit-Reviewer: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to