ProcessFork.ts
4.29 KB
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
141
142
143
144
145
146
147
148
149
150
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();
// this.childProcess.stdout.on("end",()=>{
// // console.log("!!!!");
// this.flushResult();
// })
});
}
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.on("exit",()=>{
this.flushResult();
})
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 if (msg.cmd === "done"){
// this.flushResult();
}
});
this.childProcess.stdout.on("data", (data) => {
// console.log("stdout", data.toString());
const res = data.toString();
// console.log(res);
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);
if (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 = "";
}
this.resultBuf=this.resultBuf.concat(list);
// while (list.length) {
// let item = list.shift() || "";
// if (item.length === 0 ) {
// continue;
// }
// // if (this.params.output !== "line") {
// // item = JSON.parse(item);
// // }
// this.resultBuf.push(item);
// counter++;
// }
// console.log("buf length",this.resultBuf.length);
}
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";