-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathConverter.js
193 lines (193 loc) · 6.44 KB
/
Converter.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var stream_1 = require("stream");
var Parameters_1 = require("./Parameters");
var ParseRuntime_1 = require("./ParseRuntime");
var bluebird_1 = __importDefault(require("bluebird"));
// import { ProcessorFork } from "./ProcessFork";
var ProcessorLocal_1 = require("./ProcessorLocal");
var Result_1 = require("./Result");
var Converter = /** @class */ (function (_super) {
__extends(Converter, _super);
function Converter(param, options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this, options) || this;
_this.options = options;
_this.params = Parameters_1.mergeParams(param);
_this.runtime = ParseRuntime_1.initParseRuntime(_this);
_this.result = new Result_1.Result(_this);
// if (this.params.fork) {
// this.processor = new ProcessorFork(this);
// } else {
_this.processor = new ProcessorLocal_1.ProcessorLocal(_this);
// }
_this.once("error", function (err) {
// console.log("BBB");
//wait for next cycle to emit the errors.
setImmediate(function () {
_this.result.processError(err);
_this.emit("done", err);
});
});
_this.once("done", function () {
_this.processor.destroy();
});
return _this;
}
Converter.prototype.preRawData = function (onRawData) {
this.runtime.preRawDataHook = onRawData;
return this;
};
Converter.prototype.preFileLine = function (onFileLine) {
this.runtime.preFileLineHook = onFileLine;
return this;
};
Converter.prototype.subscribe = function (onNext, onError, onCompleted) {
this.parseRuntime.subscribe = {
onNext: onNext,
onError: onError,
onCompleted: onCompleted
};
return this;
};
Converter.prototype.fromFile = function (filePath, options) {
var _this = this;
var fs = require("fs");
// var rs = null;
// this.wrapCallback(cb, function () {
// if (rs && rs.destroy) {
// rs.destroy();
// }
// });
fs.exists(filePath, function (exist) {
if (exist) {
var rs = fs.createReadStream(filePath, options);
rs.pipe(_this);
}
else {
_this.emit('error', new Error("File does not exist. Check to make sure the file path to your csv is correct."));
}
});
return this;
};
Converter.prototype.fromStream = function (readStream) {
readStream.pipe(this);
return this;
};
Converter.prototype.fromString = function (csvString) {
var csv = csvString.toString();
var read = new stream_1.Readable();
var idx = 0;
read._read = function (size) {
if (idx >= csvString.length) {
this.push(null);
}
else {
var str = csvString.substr(idx, size);
this.push(str);
idx += size;
}
};
return this.fromStream(read);
};
Converter.prototype.then = function (onfulfilled, onrejected) {
var _this = this;
return new bluebird_1.default(function (resolve, reject) {
_this.parseRuntime.then = {
onfulfilled: function (value) {
if (onfulfilled) {
resolve(onfulfilled(value));
}
else {
resolve(value);
}
},
onrejected: function (err) {
if (onrejected) {
resolve(onrejected(err));
}
else {
reject(err);
}
}
};
});
};
Object.defineProperty(Converter.prototype, "parseParam", {
get: function () {
return this.params;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Converter.prototype, "parseRuntime", {
get: function () {
return this.runtime;
},
enumerable: true,
configurable: true
});
Converter.prototype._transform = function (chunk, encoding, cb) {
var _this = this;
this.processor.process(chunk)
.then(function (result) {
// console.log(result);
if (result.length > 0) {
_this.runtime.started = true;
return _this.result.processResult(result);
}
})
.then(function () {
_this.emit("drained");
cb();
}, function (error) {
_this.runtime.hasError = true;
_this.runtime.error = error;
_this.emit("error", error);
cb();
});
};
Converter.prototype._flush = function (cb) {
var _this = this;
this.processor.flush()
.then(function (data) {
if (data.length > 0) {
return _this.result.processResult(data);
}
})
.then(function () {
_this.processEnd(cb);
}, function (err) {
_this.emit("error", err);
cb();
});
};
Converter.prototype.processEnd = function (cb) {
this.result.endProcess();
this.emit("done");
cb();
};
Object.defineProperty(Converter.prototype, "parsedLineNumber", {
get: function () {
return this.runtime.parsedLineNumber;
},
enumerable: true,
configurable: true
});
return Converter;
}(stream_1.Transform));
exports.Converter = Converter;
//# sourceMappingURL=Converter.js.map