# all files / node-ipc/dao/ client.js

71.72% Statements 71/99
51.16% Branches 22/43
100% Functions 9/9
71.72% Lines 71/99

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238

    1×

    1×
    1×
    1×

    7×
    7×

    1×
    7×

    7×
    7×
    7×

    7×

    7×

    7×
    1×
    1×

    6×

    1×
    6×
    6×

    1×

    13×

    13×
    13×

    13×
    7×

    7×

    7×

    7×

    6×
    6×
    6×

    13×

    13×

    10×
    10×

    13×

    3×
    3×
    3×

    13×

    13×

    13×

    7×
    7×

    7×
    7×
    7×

    7×

    6×

    6×
    6×

    6×

    13×

    7×
    7×

    7×
    7×

    7×

    7×

    7×

    7×
    7×
    7×
    7×
    7×

    7×
    7×

    7×

    7×

    1×
     'use strict';

    const net = require('net'),
        tls = require('tls'),
        eventParser = require('./eventParser.js'),
        Message = require('js-message'),
        fs = require('fs'),
        Queue = require('js-queue');

    let Events = require('event-pubsub/es5');
    Eif(process.version[1]>4){
        Events = require('event-pubsub');
    }

    class Client extends Events{
        constructor(config,log){
            super();
            Object.assign(
                this,
                {
                    Client  : Client,
                    config  : config,
                    queue   : new Queue,
                    socket  : false,
                    connect : connect,
                    emit    : emit,
                    log     : log,
                    retriesRemaining:config.maxRetries||0,
                    explicitlyDisconnected: false
                }
            );
        }
    }

    function emit(type,data){
        this.log('dispatching event to ', this.id, this.path, ' : ', type, ',', data);

        let message=new Message;
        message.type=type;
        message.data=data;

        Iif(this.config.rawBuffer){
            message=new Buffer(type,this.config.encoding);
        }else{
            message=eventParser.format(message);
        }

        if(!this.config.sync){
            this.socket.write(message);
            return;
        }

        this.queue.add(
            syncEmit.bind(this,message)
        );
    }

    function syncEmit(message){
        this.log('dispatching event to ', this.id, this.path, ' : ', message);
        this.socket.write(message);
    }

    function connect(){
        //init client object for scope persistance especially inside of socket events.
        let client=this;

        client.log('requested connection to ', client.id, client.path);
        Iif(!this.path){
            client.log('\n\n######\nerror: ', client.id ,' client has not specified socket path it wishes to connect to.');
            return;
        }

        if(!client.port){
            client.log('Connecting client on Unix Socket :', client.path);

            let path = client.path;

            Iif (process.platform ==='win32' && !client.path.startsWith('\\\\.\\pipe\\')){
                path = path.replace(/^\//, '');
                path = path.replace(/\//g, '-');
                path= `\\\\.\\pipe\\${path}`;
            }
            client.socket = net.connect(
                {
                    path: path
                }
            );
        }else{
            Eif(!client.config.tls){
                client.log('Connecting client via TCP to', client.path ,client.port);
                client.socket = net.connect(
                    {
                        port:client.port,
                        host:client.path
                    }
                );
            }else{
                client.log('Connecting client via TLS to', client.path ,client.port,client.config.tls);
                if(client.config.tls.private){
                    client.config.tls.key=fs.readFileSync(client.config.tls.private);
                }
                if(client.config.tls.public){
                    client.config.tls.cert=fs.readFileSync(client.config.tls.public);
                }
                if(client.config.tls.trustedConnections){
                    if(typeof client.config.tls.trustedConnections === 'string'){
                        client.config.tls.trustedConnections=[client.config.tls.trustedConnections];
                    }
                    client.config.tls.ca=[];
                    for(let i=0; i<client.config.tls.trustedConnections.length; i++){
                        client.config.tls.ca.push(
                            fs.readFileSync(client.config.tls.trustedConnections[i])
                        );
                    }
                }

                client.config.tls.host=client.path;
                client.config.tls.port=client.port;

                client.socket = tls.connect(
                    client.config.tls
                );
            }
        }

        client.socket.setEncoding(this.config.encoding);

        client.socket.on(
            'error',
            function(err){
                client.log('\n\n######\nerror: ', err);
                client.publish('error', err);

            }
        );

        client.socket.on(
            'connect',
            function connectionMade(){
                client.publish('connect');
                client.retriesRemaining=client.config.maxRetries;
                client.log('retrying reset');
            }
        );

        client.socket.on(
            'close',
            function connectionClosed(){
                client.log('connection closed' ,client.id , client.path,
                client.retriesRemaining, 'tries remaining of', client.config.maxRetries
            );

                if(
                    client.config.stopRetrying ||
                    client.retriesRemaining<1 ||
                    client.explicitlyDisconnected

                ){
                    client.publish('disconnect');
                    client.log(
                        (client.config.id),
                        'exceeded connection rety amount of',
                        ' or stopRetrying flag set.'
                    );

                    client.socket.destroy();
                    client.publish('destroy');
                    client=undefined;

                    return;
                }

                setTimeout(
                    function retryTimeout(){
                        client.retriesRemaining--;
                        client.connect();
                    }.bind(null,client),
                    client.config.retry
                );

                client.publish('disconnect');
            }
        );

        client.socket.on(
            'data',
            function(data) {
                client.log('## received events ##');
                Iif(client.config.rawBuffer){
                    client.publish(
                       'data',
                       new Buffer(data,client.config.encoding)
                    );
                    if(!client.config.sync){
                        return;
                    }

                    client.queue.next();
                    return;
                }

                Eif(!this.ipcBuffer){
                    this.ipcBuffer='';
                }

                data=(this.ipcBuffer+=data);

                Iif(data.slice(-1)!=eventParser.delimiter || data.indexOf(eventParser.delimiter) == -1){
                    client.log('Messages are large, You may want to consider smaller messages.');
                    return;
                }

                this.ipcBuffer='';

                const events = eventParser.parse(data);
                const eCount = events.length;
                for(let i=0; i<eCount; i++){
                    let message=new Message;
                    message.load(events[i]);

                    client.log('detected event', message.type, message.data);
                    client.publish(
                       message.type,
                       message.data
                    );
                }

                Iif(!client.config.sync){
                    return;
                }

                client.queue.next();
            }
        );
    }

    module.exports=Client;