JAVASCRIPT   62

client.js

Guest on 21st August 2021 08:50:27 PM

  1. var util   = require('util'),
  2.     net    = require('net'),
  3.     tls    = require('tls'),
  4.     url    = require('url'),
  5.     driver = require('websocket-driver'),
  6.     API    = require('./api'),
  7.     Event  = require('./api/event');
  8.  
  9. var DEFAULT_PORTS    = {'http:': 80, 'https:': 443, 'ws:':80, 'wss:': 443},
  10.     SECURE_PROTOCOLS = ['https:', 'wss:'];
  11.  
  12. var Client = function(_url, protocols, options) {
  13.   options = options || {};
  14.  
  15.   this.url     = _url;
  16.   this._driver = driver.client(this.url, {maxLength: options.maxLength, protocols: protocols});
  17.  
  18.   ['open', 'error'].forEach(function(event) {
  19.     this._driver.on(event, function() {
  20.       self.headers    = self._driver.headers;
  21.       self.statusCode = self._driver.statusCode;
  22.     });
  23.   }, this);
  24.  
  25.   var proxy      = options.proxy || {},
  26.       endpoint   = url.parse(proxy.origin || this.url),
  27.       port       = endpoint.port || DEFAULT_PORTS[endpoint.protocol],
  28.       secure     = SECURE_PROTOCOLS.indexOf(endpoint.protocol) >= 0,
  29.       onConnect  = function() { self._onConnect() },
  30.       netOptions = options.net || {},
  31.       originTLS  = options.tls || {},
  32.       socketTLS  = proxy.origin ? (proxy.tls || {}) : originTLS,
  33.       self       = this;
  34.  
  35.   netOptions.host = socketTLS.host = endpoint.hostname;
  36.   netOptions.port = socketTLS.port = port;
  37.  
  38.   originTLS.ca = originTLS.ca || options.ca;
  39.   socketTLS.servername = socketTLS.servername || endpoint.hostname;
  40.  
  41.   this._stream = secure
  42.                ? tls.connect(socketTLS, onConnect)
  43.                : net.connect(netOptions, onConnect);
  44.  
  45.   if (proxy.origin) this._configureProxy(proxy, originTLS);
  46.  
  47.   API.call(this, options);
  48. };
  49. util.inherits(Client, API);
  50.  
  51. Client.prototype._onConnect = function() {
  52.   var worker = this._proxy || this._driver;
  53.   worker.start();
  54. };
  55.  
  56. Client.prototype._configureProxy = function(proxy, originTLS) {
  57.   var uri    = url.parse(this.url),
  58.       secure = SECURE_PROTOCOLS.indexOf(uri.protocol) >= 0,
  59.       self   = this,
  60.       name;
  61.  
  62.   this._proxy = this._driver.proxy(proxy.origin);
  63.  
  64.   if (proxy.headers) {
  65.     for (name in proxy.headers) this._proxy.setHeader(name, proxy.headers[name]);
  66.   }
  67.  
  68.   this._proxy.pipe(this._stream, {end: false});
  69.   this._stream.pipe(this._proxy);
  70.  
  71.   this._proxy.on('connect', function() {
  72.     if (secure) {
  73.       var options = {socket: self._stream, servername: uri.hostname};
  74.       for (name in originTLS) options[name] = originTLS[name];
  75.       self._stream = tls.connect(options);
  76.       self._configureStream();
  77.     }
  78.     self._driver.io.pipe(self._stream);
  79.     self._stream.pipe(self._driver.io);
  80.     self._driver.start();
  81.   });
  82.  
  83.   this._proxy.on('error', function(error) {
  84.     self._driver.emit('error', error);
  85.   });
  86. };
  87.  
  88. module.exports = Client;

Raw Paste


Login or Register to edit or fork this paste. It's free.