1

I have some code that parses a pcap file and writes the parsed data into an array data = [] and then writes that data to a JSON file:

var fs = require("fs");
var pcapp = require('pcap-parser');
var hex = "";
var data = [];
var i = 0;

function hex2a(hexx) {
    var hex = hexx.toString();//force conversion
    var str = '';
    for (var i = 0; i < hex.length; i += 2)
        str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
        return str;
}

var parser = pcapp.parse('C:/Users/Brandt Winkler Prins/Desktop/data.pcap');
parser.on('packet', function(packet) {
    hex = [...packet.data].map(_ => ('0' +_.toString(16)).slice(-2)).join();
    var ticker = hex2a(hex.substring(282, 306).replace(/,/g,""));
    var bidPrice = parseInt(hex.substring(318,342).split(",").reverse().join(""), 16);
    var timestamp = parseInt(hex.substring(258, 282).split(",").reverse().join(""), 16);
    if(packet.header.capturedLength == 126 && ticker == "AAPL    ") {
       data[2*i] = bidPrice;
       data[2*i-1] = timestamp;
       i++
    }
 });
 data = JSON.stringify(data);
fs.writeFile("C:/Users/Brandt Winkler Prins/Desktop/Project/data.json",data);

All of the data gets written to its respective array in parser.on(...) however node.js executes my .writeFile command before it executes parser.on(...). In sum, my question is this: how can I force node to execute my code before writing my file?

Any help is greatly appreciated.

3 Answers 3

1

You need to write to file after finishing parsing

parser.on('end', function(){
  data = JSON.stringify(data);
  fs.writeFile("C:/Users/Brandt Winkler Prins/Desktop/Project/data.json",data);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Just wanted to say thanks and get that commentator badge
1

Your parser will read one packet at a time from the file, and raise the on() method for each packet.

How do you know that the quantity of packets have been reached before writing to your file?

You could do:

  1. Make a simple methode, like check_finished_and_save() which you add at the end of your on() and which controls that there is no more data to read. If yes, it writes to file.

  2. You could simply append your results to your file (mode a for append). Each time on() is called you append the results at the end of your file.

Comments

1

Capture end event for your parser and do your stuff like stringify and write file operations

parser.on('end', function (session) {
    data = JSON.stringify(data);
    fs.writeFile("C:/Users/Brandt Winkler Prins/Desktop/Project/data.json",data);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.