<commit_before>var net = require('net');
var concat = require('concat-stream');
var RPCTransform = require('./RPCTransform');

function RPCConnection (host, port) {
    this.host = host;
    this.port = port;
}

RPCConnection.prototype.call = function (name, args, callback) {
    if (args.length > 254) {
        throw new Error('uSWGI has a 254 arguments per function maximum.');
    }

    args = args.map(function (value) {
        return '' + value;
    });

    var dataSize = (2 + name.length);
    for (var i = 0; i < args.length; i++) {
        if (args[i].length > 65535) {
            throw new Error('Each RPC function argument has a 16 bit size limit (65535 bytes) in uWSGI.');
        }
        dataSize += 2 + args[i].length;
    }

    // Create uwsgi packet header (http://uwsgi-docs.readthedocs.org/en/latest/Protocol.html)
    var header = new Buffer(4);
    header.writeUInt8(173, 0);
    header.writeUInt16LE(dataSize, 1);
    header.writeUInt8(0, 3);

    // Create data packet
    var body = new Buffer(dataSize);
    // Write function name to buffer
    body.writeUInt16LE(name.length, 0);
    body.write(name, 2, name.length, 'utf8');

    // Write arguments to buffer
    var pos = name.length + 2;
    for (i = 0; i < args.length; i++) {
        body.writeUInt16LE(args[i].length, pos, 1);
        body.write(args[i], pos + 2, 'utf8');
        pos = pos + args[i].length + 2;
    }<commit_msg>Fix argument truncation for strings with unicode characters<commit_after>var net = require('net');
var concat = require('concat-stream');
var RPCTransform = require('./RPCTransform');

function RPCConnection (host, port) {
    this.host = host;
    this.port = port;
}

RPCConnection.prototype.call = function (name, args, callback) {
    if (args.length > 254) {
        throw new Error('uSWGI has a 254 arguments per function maximum.');
    }

    args = args.map(function (value) {
        return '' + value;
    });

    var dataSize = (2 + name.length);
    for (var i = 0; i < args.length; i++) {
        var argLength = Buffer.byteLength(args[i], 'utf8');
        if (argLength > 65535) {
            throw new Error('Each RPC function argument has a 16 bit size limit (65535 bytes) in uWSGI.');
        }
        dataSize += 2 + argLength;
    }

    // Create uwsgi packet header (http://uwsgi-docs.readthedocs.org/en/latest/Protocol.html)
    var header = new Buffer(4);
    header.writeUInt8(173, 0);
    header.writeUInt16LE(dataSize, 1);
    header.writeUInt8(0, 3);

    // Create data packet
    var body = new Buffer(dataSize);
    // Write function name to buffer
    body.writeUInt16LE(name.length, 0);
    body.write(name, 2, name.length, 'utf8');

    // Write arguments to buffer
    var pos = name.length + 2;
    for (i = 0; i < args.length; i++) {
        var argLength = Buffer.byteLength(args[i], 'utf8');
        body.writeUInt16LE(argLength, pos, 1);
        body.write(args[i], pos + 2, 'utf8');
        pos = pos + argLength + 2;
    }
{'fraction_non_alphanumeric': 0.14266577361018085, 'fraction_numerical': 0.03148024112525117, 'mean_word_length': 2.642682926829268, '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': '1161826', 'n_tokens_mistral': 1040, 'n_tokens_neox': 958, 'n_words': 277}