# All files / csv2json/libs/core worker.js

0% Statements 0/45
0% Branches 0/13
0% Functions 0/6
0% Lines 0/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85

     var param = null;
    var fileLine = require("./fileline");
    var csvline = require("./csvline");
    var linesToJson = require("./linesToJson");
    var CSVError = require('./CSVError');
    var eom = "\x03";
    var eom1 = "\x0e";
    var eom2 = "\x0f";
    /**
     * message is like :
     * 0{"a":"b"}
     * 13345|a,b,c
     * <cmd><data>
     * <cmd> is 0-9
     */
    var buffer="";
    process.stdin.on("data", function(d) {
      var str = d.toString("utf8");
      var all = buffer + str;
      var cmdArr = all.split(eom);
      while (cmdArr.length > 1) {
        processMsg(cmdArr.shift());
      }
      buffer = cmdArr[0];
    });

    process.on("message", processMsg);
    function processMsg(msg) {
      if (msg) {
        var cmd = msg[0];
        var data = msg.substr(1);
        switch (cmd) {
          case "0":
            initParams(data);
            break;
          case "1":
            processData(data);
            break;
          default:
            console.error("Unknown command: " + msg);
        }
      }
    }

    function initParams(data) {
       param = JSON.parse(data);
    }

    /**
     * e.g.
     * 1023|a,b,c,d\ne,f,g,h\n
     * <start line number>|<raw csv data>
     */
    function processData(data) {
      if (!param){
        console.error("Parameter not initialised when processing data.");
        process.exit(1);
      }
      var sepIdx = data.indexOf("|");
      var startIdx = parseInt(data.substr(0, sepIdx));
      var csvData = data.substr(sepIdx + 1);
      var lines = fileLine(csvData,param); //convert to file lines.
      // process.send("0"+lines.lines.length+"|"+lines.partial);
      var csvLines = csvline(lines.lines,param);
      var res = linesToJson(csvLines.lines,param,startIdx);
      if (csvLines.partial) {
        var lastIdx = res.length > 0 ? res[res.length - 1].index + 1 : startIdx;
        res.push({
          err:CSVError.unclosed_quote(lastIdx, csvLines.partial)
        });
      }
      // console.error(res)
      //1<line num>|^<row>|^err|^data|&<line num>|^<row>|^err|^data
      var str="1";
      res.forEach(function(item) {
        var errStr = item.err ? item.err.toString() : "";
        str += item.index + eom2 + JSON.stringify(item.row) + eom2 + errStr + eom2 + JSON.stringify(item.json) + eom1;
      });
      sendData(str);
    }

    function sendData(str) {
      process.stdout.write(str + eom);
    }