IntrospectiveSystems / xGraph

Introspective Systems, developer of xGraph, collaborative AI platform for data systems
https://www.introspectivesystems.com/
GNU Affero General Public License v3.0
14 stars 3 forks source link

externalize xgraph message parsing to a separate repo #71

Open valyrie97 opened 6 years ago

valyrie97 commented 6 years ago

or at least a separate file, that can be required within a module. This would help unify the code of the many different Proxy types currently available, and remove about 100 lines of code from each.

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

module.exports.StreamParser = class StreamParser extends EventEmitter{
    constructor(stream) {
        super();
        this._stream = stream;
        let buffer = '';
        this._stream.on('data', async (data) => {
            let parts = data.toString().split(/([\x02\x03])/g);
            for (let part of parts) {
                if (part === '\x02') {
                    buffer = '';
                } else if (part === '\x03') {
                    try {
                        let cmd = JSON.parse(buffer);
                        this.emit('message', cmd)
                    } catch(e) {
                        console.log('Command Parse Error:\n', buffer);
                    }
                } else {
                    buffer += part;
                }
            }
        });
    }
}
valyrie97 commented 6 years ago

marcus13345/xgmp

npm/xgmp