<commit_before>'use strict';
/**
 * Copyright 2013, 2015 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

import WebSocket from 'ws';
import urllib from 'url';

const TEST_SERVER_PING_TIMEOUT = false;

export default function(RED) {

  class WebSocketListener {
    constructor(accountConfig, account, path, webSocketListeners, options) {
      this.accountConfig = accountConfig;
      this.account = account;
      this.path = path;
      this.webSocketListeners = webSocketListeners;
      this.server = null; // socket for server connection

      this._inputNodes = [];  // collection of input nodes want to receive events
      this._outputNodes = []; // node status event listeners
      this.closing = false;
      this.options = options || {};
      this.redirect = 0;
      this.authRetry = 0;
      this.startconn(); // start outbound connection
    }

    startconn(url) {  // Connect to remote endpoint
      let conf = this.accountConfig;
      let prefix = 'ws' + (conf.secure ? 's' : '') + '://';
      prefix += conf.loginUser + ':' + conf.loginPassword + '@';
      let accountId = conf.accountFqn.split('@');
      prefix += accountId[1];
      let path = this.path;
      if (url) {
        let urlobj = urllib.parse(url);
        if (!urlobj.host) {
          path = urlobj.href;
        } else {
          prefix = urlobj.href;
          path = null;
        }
      } else {
        prefix += '/' + accountId[0] + '/api';
      }

      if (path && path.length > 0 && path.charAt(0) !== '/') {
        prefix += '/';
      }
      if (path) {
        prefix += path;
      }
      if (this.server) {
        this.server.close();
      }
      let socket = new WebSocket(prefix, this.options);
      this.server = socket; // keep for closing
      this.handleConnection(socket);
    }

    handleConnection(/*socket*/socket) {
      let id = (1+Math.random()*4294967295).toString(16);
      socket.on('open', () => {
        this.emit2all('opened');
        this.redirect = 0;
        this.authRetry = 0;
        socket.skipCloseEventHandler = false;
      });
      socket.on('close', () => {
        if (socket.skipCloseEventHandler) {
          return;
        }
        this.emit2all('closed');<commit_msg>Fix an issue where ws complained of the credentials containing unescaped special characters like '%'<commit_after>'use strict';
/**
 * Copyright 2013, 2015 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

import WebSocket from 'ws';
import urllib from 'url';

const TEST_SERVER_PING_TIMEOUT = false;

export default function(RED) {

  class WebSocketListener {
    constructor(accountConfig, account, path, webSocketListeners, options) {
      this.accountConfig = accountConfig;
      this.account = account;
      this.path = path;
      this.webSocketListeners = webSocketListeners;
      this.server = null; // socket for server connection

      this._inputNodes = [];  // collection of input nodes want to receive events
      this._outputNodes = []; // node status event listeners
      this.closing = false;
      this.options = options || {};
      this.redirect = 0;
      this.authRetry = 0;
      this.startconn(); // start outbound connection
    }

    startconn(url) {  // Connect to remote endpoint
      let conf = this.accountConfig;
      let prefix = 'ws' + (conf.secure ? 's' : '') + '://';
      prefix += encodeURIComponent(conf.loginUser) + ':' + encodeURIComponent(conf.loginPassword) + '@';
      let accountId = conf.accountFqn.split('@');
      prefix += encodeURIComponent(accountId[1]);
      let path = this.path;
      if (url) {
        let urlobj = urllib.parse(url);
        if (!urlobj.host) {
          path = urlobj.href;
        } else {
          prefix = urlobj.href;
          path = null;
        }
      } else {
        prefix += '/' + encodeURIComponent(accountId[0]) + '/api';
      }

      if (path && path.length > 0 && path.charAt(0) !== '/') {
        prefix += '/';
      }
      if (path) {
        prefix += path;
      }
      if (this.server) {
        this.server.close();
      }
      let socket = new WebSocket(prefix, this.options);
      this.server = socket; // keep for closing
      this.handleConnection(socket);
    }

    handleConnection(/*socket*/socket) {
      let id = (1+Math.random()*4294967295).toString(16);
      socket.on('open', () => {
        this.emit2all('opened');
        this.redirect = 0;
        this.authRetry = 0;
        socket.skipCloseEventHandler = false;
      });
      socket.on('close', () => {
        if (socket.skipCloseEventHandler) {
          return;
        }
        this.emit2all('closed');
{'fraction_non_alphanumeric': 0.1253305129561079, 'fraction_numerical': 0.012339150361360832, 'mean_word_length': 2.4283987915407854, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2268842', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1595, 'n_words': 471}