<commit_before>const utils = require('./utils.js');

const url = require('url');
const EventEmitter = require('events');

const WebSocket = require('ws');

exports.connect = function (href) {
	return new Promise((resolve, reject) => {
		var storeURL = url.parse(href);
		utils.requestToken(storeURL.hostname, '/ws', 'GET')
		.then((token) => {
			var ws = new WebSocket('wss://' + storeURL.host + '/ws', null, {
					headers: { 'x-api-key': token },
					agent: utils.httpsAgent
					//ca: [ CM_HTTPS_CA_ROOT_CERT ],
					//rejectUnauthorized: !!CM_HTTPS_CA_ROOT_CERT
				});
			var notifications = new EventEmitter();
			ws.on('open', () => notifications.emit('open'));
			ws.on('message', (data) => {
				// TODO: Handle exception
				data = JSON.parse(data);
				notifications.emit('data', storeURL.hostname, data.datasource_id, data.data);
			});
			ws.on('error',(error)=>{
				notifications.emit('error',error);
			});
			resolve(notifications);
		}).catch((err) => reject(err));<commit_msg>needs the ca cert to be set as well as the https agent <commit_after>const utils = require('./utils.js');

const url = require('url');
const EventEmitter = require('events');

const WebSocket = require('ws');

const CM_HTTPS_CA_ROOT_CERT = process.env.CM_HTTPS_CA_ROOT_CERT;

exports.connect = function (href) {
	return new Promise((resolve, reject) => {
		var storeURL = url.parse(href);
		utils.requestToken(storeURL.hostname, '/ws', 'GET')
		.then((token) => {
			var ws = new WebSocket('wss://' + storeURL.host + '/ws', null, {
					headers: { 'x-api-key': token },
					agent: utils.httpsAgent,
					ca: [ CM_HTTPS_CA_ROOT_CERT ],
					//rejectUnauthorized: !!CM_HTTPS_CA_ROOT_CERT
				});
			var notifications = new EventEmitter();
			ws.on('open', () => notifications.emit('open'));
			ws.on('message', (data) => {
				// TODO: Handle exception
				data = JSON.parse(data);
				notifications.emit('data', storeURL.hostname, data.datasource_id, data.data);
			});
			ws.on('error',(error)=>{
				notifications.emit('error',error);
			});
			resolve(notifications);
		}).catch((err) => reject(err));
{'fraction_non_alphanumeric': 0.1861018562589243, 'fraction_numerical': 0.0, 'mean_word_length': 3.9692671394799053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4970366', 'n_tokens_mistral': 801, 'n_tokens_neox': 699, 'n_words': 143}