<commit_before>import * as io from 'socket.io-client';
import RTCPeer from '@lib/RTCPeer';
import Bash from './components/Bash';
import './index.scss';

var socket = io.connect();
var peer;
var videoElement = document.getElementById('video') as HTMLVideoElement;
var bash = new Bash(document.getElementById('bash'));

bash.on('stdin', function (command) {
    log(command);
    let args = command.split(' ');

    if (args[0] === 'connect') {
        if (!args[1]) {
            return log('Peer socket id is missing.');
        }

        connect(args[1]);
    }
    else if (args[0] === 'enumerate') {
        socket.emit('enumerate', {}, function ({ ids }) {
            ids.forEach(id => log(id));
        });
    }
    else if (args[0] === 'help') {
        log('Type `connect <id>` to connect to peer.');
        log('Type `enumerate` to list all connected sockets.');
    }
    else {
        log('Unknown command.');
    }
});

function log(...args) {
    let texts = args.map(arg => {
        if (typeof arg === 'string') {
            return arg;
        }

        if (arg.stack) {
            return arg.stack;
        }

        if (arg.name) {
            return arg.name;
        }
    }).join('\n');

    bash.write(texts + '\n');
    bash.write('> ');
}

if (location.hostname !== 'localhost' && location.protocol !== 'https:') {
    log('You must use secure protocol (https) to access webRTC features.');
}

let getUserMedia = navigator.mediaDevices.getUserMedia ? navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices) :
    function (constraints) {
        return new Promise((resolve, reject) => {
            navigator.getUserMedia.call(navigator, constraints, function (stream) {
                resolve(stream);
            }, function (err) {
                reject(err);
            });
        })
    };

var constraints = {
    audio: false,
    video: true
};

socket.on('connect', function () {
    peer = new RTCPeer({}, socket);
    log(`Welcome, your id is: "${socket.id}".\nType \`connect <id>\` to connecto to peer.\nType \`enumerate\` to list connected socket ids.`);

    peer.on('offer', function (data) {
        getUserMedia(constraints).then(function (stream) {
            peer.accept(data, [stream], function (err, con) {
                con.on('streams', function (e) {
                    var video = document.createElement('video');
                    video.src = URL.createObjectURL(e.streams[0]);
                    video.autoplay = true;
                    bash.write(video);
                }).on('channel', function () {
                    con.on('data', function (e) {
                        log('data received', e.data);
                    });
                });
            });
        }, function () {
            peer.reject(data, function () {
                log('you rejected connection');
            });
        });
    });
});

async function connect(to) {
    log('Connecting to peer with id: ' + to);

    try {
        let stream = await getUserMedia(constraints);

        peer.offer(to, [stream], function (err, con) {
            if (err) {
                log(err.message);
            }
            else {
                con.on('streams', function (e) {
                    var video = document.createElement('video');
                    video.src = URL.createObjectURL(e.streams[0]);
                    video.autoplay = true;
                    bash.write(video);
                }).on('rejected', function () {
                    log('peer rejected connection');
                }).on('accepted', function () {
                    log('peer accepted connection');
                }).on('channel', function (channel) {
                    channel.send('hello friend');
                    log('data sent');
                }).on('offerfailed', function (e) {
                    log(e.err);
                });
            }<commit_msg>code quality fix<commit_after>import * as io from 'socket.io-client';
import RTCPeer from '@lib/RTCPeer';
import Bash from './components/Bash';
import './index.scss';

var socket = io.connect();
var peer;
var videoElement = document.getElementById('video') as HTMLVideoElement;
var bash = new Bash(document.getElementById('bash'));

bash.on('stdin', (command) => {
    log(command);
    let args = command.split(' ');

    if (args[0] === 'connect') {
        if (!args[1]) {
            return log('Peer socket id is missing.');
        }

        connect(args[1]);
    }
    else if (args[0] === 'enumerate') {
        socket.emit('enumerate', {}, ({ ids }) => {
            ids.forEach(id => log(id));
        });
    }
    else if (args[0] === 'help') {
        log('Type `connect <id>` to connect to peer.');
        log('Type `enumerate` to list all connected sockets.');
    }
    else {
        log('Unknown command.');
    }
});

function log(...args) {
    let items = args.map(item => {
        if (item instanceof Error) {
            return item.stack || item.name;
        }

        return item;
    });

    items.forEach(item => {
        bash.write(item);
        bash.write('\n');
    });
    
    bash.write('> ');
}

if (location.hostname !== 'localhost' && location.protocol !== 'https:') {
    log('You must use secure protocol (https) to access webRTC features.');
}

let getUserMedia = navigator.mediaDevices.getUserMedia ? navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices) :
    function (constraints) {
        return new Promise((resolve, reject) => {
            navigator.getUserMedia.call(navigator, constraints, function (stream) {
                resolve(stream);
            }, function (err) {
                reject(err);
            });
        })
    };

var constraints = {
    audio: false,
    video: true
};

socket.on('connect', () => {
    peer = new RTCPeer({}, socket);
    log(`Welcome, your id is: "${socket.id}".\nType \`connect <id>\` to connecto to peer.\nType \`enumerate\` to list connected socket ids.`);

    peer.on('offer', (data) => {
        getUserMedia(constraints).then((stream) => {
            peer.accept(data, [stream], (err, con) => {
                con.on('streams', (e) => {
                    let video = document.createElement('video');
                    video.src = URL.createObjectURL(e.streams[0]);
                    video.autoplay = true;
                    log(video);
                }).on('channel', () => {
                    con.on('data', (e) => {
                        log('data received', e.data);
                    });
                });
            });
        }, () => {
            peer.reject(data, () => {
                log('you rejected connection');
            });
        });
    });
});

async function connect(to) {
    log('Connecting to peer with id: ' + to);

    try {
        let stream = await getUserMedia(constraints);

        peer.offer(to, [stream], (err, con) => {
            if (err) {
                log(err.message);
            }
            else {
                con.on('streams', (e) => {
                    var video = document.createElement('video');
                    video.src = URL.createObjectURL(e.streams[0]);
                    video.autoplay = true;
                    log(video);
                }).on('rejected', () => {
                    log('peer rejected connection');
                }).on('accepted', () => {
                    log('peer accepted connection');
                }).on('channel', (channel) => {
                    channel.send('hello friend');
                    log('data sent');
                }).on('offerfailed', (e) => {
                    log(e.err);
                });
            }
{'fraction_non_alphanumeric': 0.16048261546445253, 'fraction_numerical': 0.0018162947586922678, 'mean_word_length': 1.6851271334029956, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, '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': '4073377', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2103, 'n_words': 486}