<commit_before>/* See license.txt for terms of usage */

/**
 * @ignore
 */
var EXPORTED_SYMBOLS = ["EventPacket", "RequestPacket", "ResponsePacket"];

/**
 * @name Packet
 * @description The prototype object for all other packet types.
 * Handles parsing packet strings and JSON, and generating sequence numbers.
 * @public
 * @type Packet
 */
var Packet = {
    seq: 0,
    length: 0,
    data: null,

    /**
     * @name toJSON
     * @description Converts the given object to a JSON {@link String}.
     * @function
     * @public
     * @memberOf Packet
     * @param obj the object to convert to a JSON {@link String}
     * @type String
     * @returns the given object as a JSON {@link String}
     */
    toJSON: function( obj) {
        //FIXME: only works for FF3.5 native JSON
        return JSON.stringify(obj);
    },

    /**
     * @name parseJSON
     * @description Parses the {@link Object} from the given JSON {@link String}.
     * @function
     * @public
     * @memberOf Packet
     * @param str the JSON {@link String} to parse
     * @type Object
     * @returns the {@link Object} value from the JSON {@link String}
     */
    parseJSON: function( str) {
        if (str) {
            //return eval('(' + str + ')'); //FIXME: dangerous
            return JSON.parse(str);
        }
        return null;
    },

    /**
     * @name toPacketString
     * @description Converts the given string to over-the-wire form, with the <code>Content-Length</code> header and lines feed added.
     * @function
     * @public
     * @memberOf Packet
     * @param str the {@link String} to add to the {@link Packet} {@link String}
     * @type String
     * @returns the given {@link String} with the <code>Content-Length</code> header and lines feed added
     */
    toPacketString: function( body, headers) {
        if (!headers) {
            headers = [];
        }
        // insert content-length header at beginning of headers
        headers.splice(0,0,"Content-Length:" + body.length);
        headers = headers.join("\r\n"); // only adds \r\n if more than one header
        return headers + "\r\n\r\n" + body; // HTTP-ish style
    }
};

/**
 * @name EventPacket
 * @description Creates a new {@link EventPacket} object.
 * @constructor
 * @public
 * @param event name of the event.
 * @param data JSON object containing additional arguments for the event.
 * @type EventPacket
 * @returns a new {@link EventPacket}
 */
function EventPacket( event, data, headers) {
    if (data) {
        var sequence = Packet.seq++;
        var packet = {
                "seq": sequence,
                "type": "event",
                "event": event
        };
        for (var prop in data) {
            packet[prop] = data[prop];
        }
        var json = this.toJSON(packet);
        this.data = this.toPacketString(json, headers);
        this.length = this.data.length;
    } else {
        //FIXME: incoming event hack
        var json = this.parseJSON(event);
        for (var prop in json) {
            this[prop] = json[prop];
        }
        if (json && json.seq)
            Packet.seq = json.seq+1;
    }
}

EventPacket.prototype = Packet;

/**
 * @name RequestPacket
 * @description Creates a {@link RequestPacket} object.
 * @constructor
 * @public
 * @param packetString The unprocessed UTF-8 packet string.
 * @type RequestPacket
 * @returns a new {@link RequestPacket}
 */
function RequestPacket( packetString) {
    var json,command,args,headers,packet;
    if (arguments.length == 1) {
        json = this.parseJSON(packetString);
        for (var prop in json) {
            this[prop] = json[prop];
        }
        if (json && json.seq)
            Packet.seq = json.seq+1;
    } else if (arguments.length > 2){
        command = arguments[0];
        args = arguments[1];
        headers = arguments[2];
        packet = {
            "seq": Packet.seq++,
            "type": "command",
            "command": command
        };

        if (args) {
            if (args["contextId"]) {
                packet["contextId"] = args["contextId"];
                delete args["contextId"];
            }

            for (var arg in args) {
                if (!packet["arguments"]) packet["arguments"] = {};
                packet["arguments"][arg] = args[arg];
            }
        }

        json = this.toJSON(packet);
        this.data = this.toPacketString(json, headers);
        this.length = this.data.length;
    }
}

RequestPacket.prototype = Packet;

/**
 * @name ResponsePacket
 * @description Creates a new {@link ResponsePacket} object.
 * @constructor
 * @public
 * @param command The name of the command that requested the response.
 * @param requestSeq The sequence number of the request that initiated this response.
 * @param contextid the Crossfire id for the context from the request. Can be <code>null</code>
 * @param body The JSON body of the response.
 * @param status The status object.
 * @type ResponsePacket
 * @returns a new {@link ResponsePacket}
 */
function ResponsePacket( command, requestSeq, contextid, body, status, headers) {
    var sequence = Packet.seq++;
    var packet = {
            "type": "response",
            "command": command,
            "seq": sequence,
            "requestSeq": requestSeq,
            "body": body,
            "status": status
    };
    if(contextid) {
        packet["contextId"] = contextid;
    }
    var json = this.toJSON(packet);
    this.data = this.toPacketString(json, headers);
    this.length = this.data.length;
}

ResponsePacket.prototype = Packet;<commit_msg>Test of jazzhub and convert Packet code to Orion code conventions<commit_after>/* See license.txt for terms of usage */

/**
 * @name Packet
 * @description The prototype object for all other packet types.
 * Handles parsing packet strings and JSON, and generating sequence numbers.
 * @public
 * @type Packet
 */
var Packet = {
    seq: 0,
    length: 0,
    data: null,

    /**
     * @name toJSON
     * @description Converts the given object to a JSON {@link String}.
     * @function
     * @public
     * @memberOf Packet
     * @param obj the object to convert to a JSON {@link String}
     * @type String
     * @returns the given object as a JSON {@link String}
     */
    toJSON: function( obj) {
        return JSON.stringify(obj);
    },

    /**
     * @name parseJSON
     * @description Parses the {@link Object} from the given JSON {@link String}.
     * @function
     * @public
     * @memberOf Packet
     * @param str the JSON {@link String} to parse
     * @type Object
     * @returns the {@link Object} value from the JSON {@link String}
     */
    parseJSON: function( str) {
        if (str) {
            return JSON.parse(str);
        }
        return null;
    },

    /**
     * @name toPacketString
     * @description Converts the given string to over-the-wire form, with the <code>Content-Length</code> header and lines feed added.
     * @function
     * @public
     * @memberOf Packet
     * @param str the {@link String} to add to the {@link Packet} {@link String}
     * @type String
     * @returns the given {@link String} with the <code>Content-Length</code> header and lines feed added
     */
    toPacketString: function( body, headers) {
        if (!headers) {
            headers = [];
        }
        // insert content-length header at beginning of headers
        headers.splice(0,0,"Content-Length:" + body.length);
        headers = headers.join("\r\n"); // only adds \r\n if more than one header
        return headers + "\r\n\r\n" + body; // HTTP-ish style
    }
};

/**
 * @name EventPacket
 * @description Creates a new {@link EventPacket} object.
 * @constructor
 * @public
 * @param event name of the event.
 * @param data JSON object containing additional arguments for the event.
 * @type EventPacket
 * @returns a new {@link EventPacket}
 */
function EventPacket( event, data, headers) {
    if (data) {
        var sequence = Packet.seq++;
        var packet = {
                "seq": sequence,
                "type": "event",
                "event": event
        };
        Object.keys(data).forEach(function(property) {
		    packet[property] = data[property];
		});
        this.data = this.toPacketString(this.toJSON(packet), headers);
        this.length = this.data.length;
    } else {
        //FIXME: incoming event hack
        var json = this.parseJSON(event);
        Object.keys(json).forEach(function(property) {
		    this[property] = json[property];
		});
        if (json && json.seq) {
            Packet.seq = json.seq+1;
        }
    }
}

EventPacket.prototype = Packet;

/**
 * @name RequestPacket
 * @description Creates a {@link RequestPacket} object.
 * @constructor
 * @public
 * @param packetString The unprocessed UTF-8 packet string.
 * @type RequestPacket
 * @returns a new {@link RequestPacket}
 */
function RequestPacket( packetString) {
    var json,command,args,headers,packet;
    if (arguments.length === 1) {
        json = this.parseJSON(packetString);
        Object.keys(json).forEach(function(property) {
		    this[property] = json[property];
		});
        if (json && json.seq) {
            Packet.seq = json.seq+1;
        }
    } else if (arguments.length > 2){
        command = arguments[0];
        args = arguments[1];
        headers = arguments[2];
        packet = {
            "seq": Packet.seq++,
            "type": "command",
            "command": command
        };

        if (args) {
            if (args.contextId) {
                packet.contextId = args.contextId;
                delete args.contextId;
            }

			if (!packet.arguments) {
				packet.arguments = {};
			}
			Object.keys(args).forEach(function(property) {
				packet.arguments[property] = args[property];
			});
        }

        json = this.toJSON(packet);
        this.data = this.toPacketString(json, headers);
        this.length = this.data.length;
    }
}

RequestPacket.prototype = Packet;

/**
 * @name ResponsePacket
 * @description Creates a new {@link ResponsePacket} object.
 * @constructor
 * @public
 * @param command The name of the command that requested the response.
 * @param requestSeq The sequence number of the request that initiated this response.
 * @param contextid the Crossfire id for the context from the request. Can be <code>null</code>
 * @param body The JSON body of the response.
 * @param status The status object.
 * @type ResponsePacket
 * @returns a new {@link ResponsePacket}
 */
function ResponsePacket( command, requestSeq, contextid, body, status, headers) {
    var sequence = Packet.seq++;
    var packet = {
            "type": "response",
            "command": command,
            "seq": sequence,
            "requestSeq": requestSeq,
            "body": body,
            "status": status
    };
    if(contextid) {
        packet.contextId = contextid;
    }
    var json = this.toJSON(packet);
    this.data = this.toPacketString(json, headers);
    this.length = this.data.length;
}

ResponsePacket.prototype = Packet;
{'fraction_non_alphanumeric': 0.12239371784457082, 'fraction_numerical': 0.0023467822005596175, 'mean_word_length': 2.251173708920188, 'pattern_counts': {'":': 24, '<': 15, '<?xml version=': 0, '>': 17, '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': '5373629', 'n_tokens_mistral': 3151, 'n_tokens_neox': 3054, 'n_words': 998}