UNPKG

6.59 kBJavaScriptView Raw
1"use strict";
2var __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})();
12var __importDefault = (this && this.__importDefault) || function (mod) {
13 return (mod && mod.__esModule) ? mod : { "default": mod };
14};
15Object.defineProperty(exports, "__esModule", { value: true });
16var stream_1 = require("stream");
17var Parameters_1 = require("./Parameters");
18var ParseRuntime_1 = require("./ParseRuntime");
19var bluebird_1 = __importDefault(require("bluebird"));
20// import { ProcessorFork } from "./ProcessFork";
21var ProcessorLocal_1 = require("./ProcessorLocal");
22var Result_1 = require("./Result");
23var Converter = /** @class */ (function (_super) {
24 __extends(Converter, _super);
25 function Converter(param, options) {
26 if (options === void 0) { options = {}; }
27 var _this = _super.call(this, options) || this;
28 _this.options = options;
29 _this.params = Parameters_1.mergeParams(param);
30 _this.runtime = ParseRuntime_1.initParseRuntime(_this);
31 _this.result = new Result_1.Result(_this);
32 // if (this.params.fork) {
33 // this.processor = new ProcessorFork(this);
34 // } else {
35 _this.processor = new ProcessorLocal_1.ProcessorLocal(_this);
36 // }
37 _this.once("error", function (err) {
38 // console.log("BBB");
39 //wait for next cycle to emit the errors.
40 setImmediate(function () {
41 _this.result.processError(err);
42 _this.emit("done", err);
43 });
44 });
45 _this.once("done", function () {
46 _this.processor.destroy();
47 });
48 return _this;
49 }
50 Converter.prototype.preRawData = function (onRawData) {
51 this.runtime.preRawDataHook = onRawData;
52 return this;
53 };
54 Converter.prototype.preFileLine = function (onFileLine) {
55 this.runtime.preFileLineHook = onFileLine;
56 return this;
57 };
58 Converter.prototype.subscribe = function (onNext, onError, onCompleted) {
59 this.parseRuntime.subscribe = {
60 onNext: onNext,
61 onError: onError,
62 onCompleted: onCompleted
63 };
64 return this;
65 };
66 Converter.prototype.fromFile = function (filePath, options) {
67 var _this = this;
68 var fs = require("fs");
69 // var rs = null;
70 // this.wrapCallback(cb, function () {
71 // if (rs && rs.destroy) {
72 // rs.destroy();
73 // }
74 // });
75 fs.exists(filePath, function (exist) {
76 if (exist) {
77 var rs = fs.createReadStream(filePath, options);
78 rs.pipe(_this);
79 }
80 else {
81 _this.emit('error', new Error("File does not exist. Check to make sure the file path to your csv is correct."));
82 }
83 });
84 return this;
85 };
86 Converter.prototype.fromStream = function (readStream) {
87 readStream.pipe(this);
88 return this;
89 };
90 Converter.prototype.fromString = function (csvString) {
91 var csv = csvString.toString();
92 var read = new stream_1.Readable();
93 var idx = 0;
94 read._read = function (size) {
95 if (idx >= csvString.length) {
96 this.push(null);
97 }
98 else {
99 var str = csvString.substr(idx, size);
100 this.push(str);
101 idx += size;
102 }
103 };
104 return this.fromStream(read);
105 };
106 Converter.prototype.then = function (onfulfilled, onrejected) {
107 var _this = this;
108 return new bluebird_1.default(function (resolve, reject) {
109 _this.parseRuntime.then = {
110 onfulfilled: function (value) {
111 if (onfulfilled) {
112 resolve(onfulfilled(value));
113 }
114 else {
115 resolve(value);
116 }
117 },
118 onrejected: function (err) {
119 if (onrejected) {
120 resolve(onrejected(err));
121 }
122 else {
123 reject(err);
124 }
125 }
126 };
127 });
128 };
129 Object.defineProperty(Converter.prototype, "parseParam", {
130 get: function () {
131 return this.params;
132 },
133 enumerable: true,
134 configurable: true
135 });
136 Object.defineProperty(Converter.prototype, "parseRuntime", {
137 get: function () {
138 return this.runtime;
139 },
140 enumerable: true,
141 configurable: true
142 });
143 Converter.prototype._transform = function (chunk, encoding, cb) {
144 var _this = this;
145 this.processor.process(chunk)
146 .then(function (result) {
147 // console.log(result);
148 if (result.length > 0) {
149 _this.runtime.started = true;
150 return _this.result.processResult(result);
151 }
152 })
153 .then(function () {
154 _this.emit("drained");
155 cb();
156 }, function (error) {
157 _this.runtime.hasError = true;
158 _this.runtime.error = error;
159 _this.emit("error", error);
160 cb();
161 });
162 };
163 Converter.prototype._flush = function (cb) {
164 var _this = this;
165 this.processor.flush()
166 .then(function (data) {
167 if (data.length > 0) {
168 return _this.result.processResult(data);
169 }
170 })
171 .then(function () {
172 _this.processEnd(cb);
173 }, function (err) {
174 _this.emit("error", err);
175 cb();
176 });
177 };
178 Converter.prototype.processEnd = function (cb) {
179 this.result.endProcess();
180 this.emit("done");
181 cb();
182 };
183 Object.defineProperty(Converter.prototype, "parsedLineNumber", {
184 get: function () {
185 return this.runtime.parsedLineNumber;
186 },
187 enumerable: true,
188 configurable: true
189 });
190 return Converter;
191}(stream_1.Transform));
192exports.Converter = Converter;
193//# sourceMappingURL=Converter.js.map
\No newline at end of file