1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
13 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
14 | };
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | var Processor_1 = require("./Processor");
|
17 | var bluebird_1 = __importDefault(require("bluebird"));
|
18 | var Parameters_1 = require("./Parameters");
|
19 | var CSVError_1 = __importDefault(require("./CSVError"));
|
20 | var ProcessorFork = (function (_super) {
|
21 | __extends(ProcessorFork, _super);
|
22 | function ProcessorFork(converter) {
|
23 | var _this = _super.call(this, converter) || this;
|
24 | _this.converter = converter;
|
25 | _this.inited = false;
|
26 | _this.resultBuf = [];
|
27 | _this.leftChunk = "";
|
28 | _this.finalChunk = false;
|
29 | _this.childProcess = require("child_process").spawn(process.execPath, [__dirname + "/../v2/worker.js"], {
|
30 | stdio: ["pipe", "pipe", "pipe", "ipc"]
|
31 | });
|
32 | _this.initWorker();
|
33 | return _this;
|
34 | }
|
35 | ProcessorFork.prototype.flush = function () {
|
36 | var _this = this;
|
37 | return new bluebird_1.default(function (resolve, reject) {
|
38 |
|
39 | _this.finalChunk = true;
|
40 | _this.next = resolve;
|
41 | _this.childProcess.stdin.end();
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | });
|
47 | };
|
48 | ProcessorFork.prototype.destroy = function () {
|
49 | this.childProcess.kill();
|
50 | return bluebird_1.default.resolve();
|
51 | };
|
52 | ProcessorFork.prototype.prepareParam = function (param) {
|
53 | var clone = Parameters_1.mergeParams(param);
|
54 | if (clone.ignoreColumns) {
|
55 | clone.ignoreColumns = {
|
56 | source: clone.ignoreColumns.source,
|
57 | flags: clone.ignoreColumns.flags
|
58 | };
|
59 | }
|
60 | if (clone.includeColumns) {
|
61 | clone.includeColumns = {
|
62 | source: clone.includeColumns.source,
|
63 | flags: clone.includeColumns.flags
|
64 | };
|
65 | }
|
66 | return clone;
|
67 | };
|
68 | ProcessorFork.prototype.initWorker = function () {
|
69 | var _this = this;
|
70 | this.childProcess.on("exit", function () {
|
71 | _this.flushResult();
|
72 | });
|
73 | this.childProcess.send({
|
74 | cmd: "init",
|
75 | params: this.prepareParam(this.converter.parseParam)
|
76 | });
|
77 | this.childProcess.on("message", function (msg) {
|
78 | if (msg.cmd === "inited") {
|
79 | _this.inited = true;
|
80 | }
|
81 | else if (msg.cmd === "eol") {
|
82 | if (_this.converter.listeners("eol").length > 0) {
|
83 | _this.converter.emit("eol", msg.value);
|
84 | }
|
85 | }
|
86 | else if (msg.cmd === "header") {
|
87 | if (_this.converter.listeners("header").length > 0) {
|
88 | _this.converter.emit("header", msg.value);
|
89 | }
|
90 | }
|
91 | else if (msg.cmd === "done") {
|
92 |
|
93 | }
|
94 | });
|
95 | this.childProcess.stdout.on("data", function (data) {
|
96 |
|
97 | var res = data.toString();
|
98 |
|
99 | _this.appendBuf(res);
|
100 | });
|
101 | this.childProcess.stderr.on("data", function (data) {
|
102 |
|
103 | _this.converter.emit("error", CSVError_1.default.fromJSON(JSON.parse(data.toString())));
|
104 | });
|
105 | };
|
106 | ProcessorFork.prototype.flushResult = function () {
|
107 |
|
108 | if (this.next) {
|
109 | this.next(this.resultBuf);
|
110 | }
|
111 | this.resultBuf = [];
|
112 | };
|
113 | ProcessorFork.prototype.appendBuf = function (data) {
|
114 | var res = this.leftChunk + data;
|
115 | var list = res.split("\n");
|
116 | var counter = 0;
|
117 | var lastBit = list[list.length - 1];
|
118 | if (lastBit !== "") {
|
119 | this.leftChunk = list.pop() || "";
|
120 | }
|
121 | else {
|
122 | this.leftChunk = "";
|
123 | }
|
124 | this.resultBuf = this.resultBuf.concat(list);
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | };
|
138 | ProcessorFork.prototype.process = function (chunk) {
|
139 | var _this = this;
|
140 | return new bluebird_1.default(function (resolve, reject) {
|
141 |
|
142 | _this.next = resolve;
|
143 |
|
144 | _this.childProcess.stdin.write(chunk, function () {
|
145 |
|
146 | _this.flushResult();
|
147 | });
|
148 | });
|
149 | };
|
150 | return ProcessorFork;
|
151 | }(Processor_1.Processor));
|
152 | exports.ProcessorFork = ProcessorFork;
|
153 | exports.EOM = "\x03";
|
154 |
|
\ | No newline at end of file |