All files ProcessFork.ts

100% Statements 78/78
86.67% Branches 26/30
100% Functions 15/15
100% Lines 73/73

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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 1411x 1x     1x       1x   1x 37x 37x   37x 37x 37x     1x 37x 37x     37x 37x 37x 37x   37x 37x 37x     37x   1x 37x 37x 1x         37x 3x         37x   37x 37x       37x 145x 37x 108x 36x 2x   72x 36x 4x   36x   36x       37x   2886x 2886x     37x   1x       1x   121x 121x   121x   1x 2886x 2886x 2886x 2886x 2886x 3x   2883x     2886x 13554x 13554x 2883x   10671x 10671x   10671x 10671x       85x 85x   85x   85x   85x       1x                       1x
import { Processor, ProcessLineResult } from "./Processor";
import P from "bluebird"
import { Converter } from "./Converter";
import { ChildProcess } from "child_process";
import { CSVParseParam, mergeParams } from "./Parameters";
import { ParseRuntime } from "./ParseRuntime";
import { Readable, Writable } from "stream";
import { bufFromString, emptyBuffer } from "./util";
import CSVError from "./CSVError";
 
export class ProcessorFork extends Processor {
  flush(): P<ProcessLineResult[]> {
    return new P((resolve, reject) => {
      // console.log("flush");
      this.finalChunk = true;
      this.next = resolve;
      this.childProcess.stdin.end();
    });
  }
  destroy(): P<void> {
    this.childProcess.kill();
    return P.resolve();
  }
  childProcess: ChildProcess;
  inited: boolean = false;
  private resultBuf: ProcessLineResult[] = [];
  private leftChunk: string = "";
  private finalChunk: boolean = false;
  private next?: (result: ProcessLineResult[]) => any;
  constructor(protected converter: Converter) {
    super(converter);
    this.childProcess = require("child_process").spawn(process.execPath, [__dirname + "/../v2/worker.js"], {
      stdio: ["pipe", "pipe", "pipe", "ipc"]
    });
    this.initWorker();
  }
  private prepareParam(param:CSVParseParam):any{
    const clone:any=mergeParams(param);
    if (clone.ignoreColumns){
      clone.ignoreColumns={
        source:clone.ignoreColumns.source,
        flags:clone.ignoreColumns.flags
      }
    }
    if (clone.includeColumns){
      clone.includeColumns={
        source:clone.includeColumns.source,
        flags:clone.includeColumns.flags
      }
    }
    return clone;
  }
  private initWorker() {
    this.childProcess.send({
      cmd: "init",
      params: this.prepareParam(this.converter.parseParam)
    } as InitMessage);
    this.childProcess.on("message", (msg: Message) => {
      if (msg.cmd === "inited") {
        this.inited = true;
      } else if (msg.cmd === "eol") {
        if (this.converter.listeners("eol").length > 0){
          this.converter.emit("eol",(msg as StringMessage).value);
        }
      }else if (msg.cmd === "header") {
        if (this.converter.listeners("header").length > 0){
          this.converter.emit("header",(msg as StringMessage).value);
        }
      }else Eif (msg.cmd === "done"){
 
        this.flushResult();
      }
 
    });
    this.childProcess.stdout.on("data", (data) => {
      // console.log("stdout", data.toString());
      const res = data.toString();
      this.appendBuf(res);
 
    });
    this.childProcess.stderr.on("data", (data) => {
      // console.log("stderr", data.toString());
      this.converter.emit("error", CSVError.fromJSON(JSON.parse(data.toString())));
    });
 
  }
  private flushResult() {
    // console.log("flush result", this.resultBuf.length);
    Eif (this.next) {
      this.next(this.resultBuf);
    }
    this.resultBuf = [];
  }
  private appendBuf(data: string) {
    const res = this.leftChunk + data;
    const list = res.split("\n");
    let counter = 0;
    const lastBit = list[list.length - 1];
    if (lastBit !== "") {
      this.leftChunk = list.pop() || "";
    } else {
      this.leftChunk = "";
    }
 
    while (list.length) {
      let item = list.shift() || "";
      if (item.length === 0 ) {
        continue;
      }
      Eif (this.params.output !== "line") {
        item = JSON.parse(item);
      }
      this.resultBuf.push(item);
      counter++;
    }
  }
 
  process(chunk: Buffer): P<ProcessLineResult[]> {
    return new P((resolve, reject) => {
      // console.log("chunk", chunk.length);
      this.next = resolve;
      // this.appendReadBuf(chunk);
      this.childProcess.stdin.write(chunk, () => {
        // console.log("chunk callback");
        this.flushResult();
      });
    });
  }
}
 
export interface Message {
  cmd: string
}
 
export interface InitMessage extends Message {
  params: any;
}
export interface StringMessage extends Message {
  value: string
}
export const EOM = "\x03";