<commit_before>/*
    stomp.js - STOMP Protocol in node.js

    Copyright (c) 2010, <NAME>
    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or
      other materials provided with the distribution.
    * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software
      without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

var net = require('net'),
    sys = require('sys'),
    frame = require('./frame'),
    stomp_utils = require('./stomp-utils'),
    exceptions = require('./stomp-exceptions'),
    utils = new stomp_utils.StompUtils(),
    log = null;

function parse_command(data) {
    var command,
        this_string = data.toString('utf8', start=0, end=data.length);
    command = this_string.split('\n');
    return command[0];
};

function parse_headers(headers_str) {
    var these_headers = [],
        one_header = [],
        header_key = null,
        header_val = null,
        headers_split = headers_str.split('\n');

    for (var i = 0; i < headers_split.length; i++) {
        one_header = headers_split[i].split(':');
        if (one_header.length > 1) {
            header_key = one_header.shift();
            header_val = one_header.join(':');
            these_headers[header_key] = header_val;
        }
        else {
            these_headers[one_header[0]] = one_header[1];
        }
    }
    return these_headers;
};

function parse_frame(chunk) {
    var args = {},
        data = null,
        command = null,
        headers = null,
        body = null,
        headers_str = null;

    if (!utils.really_defined(chunk))
        return null;

    command = parse_command(chunk);
    data = chunk.slice(command.length + 1, chunk.length);
    data = data.toString('utf8', start=0, end=data.length);

    the_rest = data.split('\n\n');
    headers = parse_headers(the_rest[0]);
    body = the_rest[1];

    if ('content-length' in headers)
       headers['bytes_message'] = true;

    args = {
        command: command,
        headers: headers,
        body: body
    }

    var this_frame = new frame.Frame();
    var return_frame = this_frame.build_frame(args);

    return return_frame;
};

function _connect(stomp) {
    var socket = stomp.socket = net.Stream();
    log = stomp.log;
    log.debug('Connecting to ' + stomp.host + ':' + stomp.port);
    socket.connect(stomp.port, stomp.host);

    socket.on('connect', function() {
        log.debug('Connected to socket');
        if (utils.really_defined(stomp.login) &&
            utils.really_defined(stomp.passcode)) {
            stomp_connect(stomp, {login: stomp.login, passcode: stomp.passcode});
        }
        else {
            stomp_connect(stomp);
        }
    });

    socket.on('drain', function(data) {
        log.debug('draining');
    });

    var buffer = '';
    socket.on('data', function(chunk) {
        buffer += chunk;
        var frames = buffer.split('\0\n');

        if (frames.length == 1) return;
        buffer = frames.pop();

        var parsed_frame = null;
        var _frame = null;
        while (_frame = frames.shift()) {
            parsed_frame = parse_frame(_frame + '\0\n')
            stomp.handle_new_frame(parsed_frame);
        }

    });

    socket.on('end', function() {
        log.debug("end");
    });

    socket.on('error', function(error) {
        log.error(error.stack + 'error name: ' + error.name);
    });
<commit_msg>Possible fix for issue #6 from @joshuarubin<commit_after>/*
    stomp.js - STOMP Protocol in node.js

    Copyright (c) 2010, <NAME>
    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or
      other materials provided with the distribution.
    * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software
      without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

var net = require('net'),
    sys = require('sys'),
    frame = require('./frame'),
    stomp_utils = require('./stomp-utils'),
    exceptions = require('./stomp-exceptions'),
    utils = new stomp_utils.StompUtils(),
    log = null;

function parse_command(data) {
    var command,
        this_string = data.toString('utf8', start=0, end=data.length);
    command = this_string.split('\n');
    return command[0];
};

function parse_headers(headers_str) {
    var these_headers = [],
        one_header = [],
        header_key = null,
        header_val = null,
        headers_split = headers_str.split('\n');

    for (var i = 0; i < headers_split.length; i++) {
        one_header = headers_split[i].split(':');
        if (one_header.length > 1) {
            header_key = one_header.shift();
            header_val = one_header.join(':');
            these_headers[header_key] = header_val;
        }
        else {
            these_headers[one_header[0]] = one_header[1];
        }
    }
    return these_headers;
};

function parse_frame(chunk) {
    var args = {},
        data = null,
        command = null,
        headers = null,
        body = null,
        headers_str = null;

    if (!utils.really_defined(chunk))
        return null;

    command = parse_command(chunk);
    data = chunk.slice(command.length + 1, chunk.length);
    data = data.toString('utf8', start=0, end=data.length);

    the_rest = data.split('\n\n');
    headers = parse_headers(the_rest[0]);
    body = the_rest[1];

    if ('content-length' in headers)
       headers['bytes_message'] = true;

    args = {
        command: command,
        headers: headers,
        body: body
    }

    var this_frame = new frame.Frame();
    var return_frame = this_frame.build_frame(args);

    return return_frame;
};

function _connect(stomp) {
    var socket = stomp.socket = net.Stream();
    log = stomp.log;
    log.debug('Connecting to ' + stomp.host + ':' + stomp.port);
    socket.connect(stomp.port, stomp.host);

    socket.on('connect', function() {
        log.debug('Connected to socket');
        if (utils.really_defined(stomp.login) &&
            utils.really_defined(stomp.passcode)) {
            stomp_connect(stomp, {login: stomp.login, passcode: stomp.passcode});
        }
        else {
            stomp_connect(stomp);
        }
    });

    socket.on('drain', function(data) {
        log.debug('draining');
    });

    var buffer = '';
    socket.on('data', function(chunk) {
        buffer += chunk;
        var frames = buffer.split('\0\n');

        if (frames.length == 1) return;
        buffer = frames.pop();

        var parsed_frame = null;
        var _frame = null;
        while (_frame = frames.shift()) {
            parsed_frame = parse_frame(_frame)
            stomp.handle_new_frame(parsed_frame);
        }

    });

    socket.on('end', function() {
        log.debug("end");
    });

    socket.on('error', function(error) {
        log.error(error.stack + 'error name: ' + error.name);
    });

{'fraction_non_alphanumeric': 0.10876815492200108, 'fraction_numerical': 0.004088219472834858, 'mean_word_length': 2.731834604576475, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '48437', 'n_tokens_mistral': 2714, 'n_tokens_neox': 2575, 'n_words': 829}