192 lines
20 KiB
JavaScript
192 lines
20 KiB
JavaScript
|
"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");
|
||
|
setTimeout(function () {
|
||
|
_this.result.processError(err);
|
||
|
_this.emit("done", err);
|
||
|
}, 0);
|
||
|
});
|
||
|
_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=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiL1VzZXJzL2t4aWFuZy93b3JrL3Byb2plY3RzL2NzdjJqc29uL3NyYy9Db252ZXJ0ZXIudHMiLCJzb3VyY2VzIjpbIi9Vc2Vycy9reGlhbmcvd29yay9wcm9qZWN0cy9jc3YyanNvbi9zcmMvQ29udmVydGVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7OztBQUFBLGlDQUErRDtBQUMvRCwyQ0FBMEQ7QUFDMUQsK0NBQWdFO0FBQ2hFLHNEQUF5QjtBQU96QixpREFBaUQ7QUFDakQsbURBQWtEO0FBQ2xELG1DQUFrQztBQU1sQztJQUErQiw2QkFBUztJQXVGdEMsbUJBQVksS0FBOEIsRUFBUyxPQUE4QjtRQUE5Qix3QkFBQSxFQUFBLFlBQThCO1FBQWpGLFlBQ0Usa0JBQU0sT0FBTyxDQUFDLFNBdUJmO1FBeEJrRCxhQUFPLEdBQVAsT0FBTyxDQUF1QjtRQUUvRSxLQUFJLENBQUMsTUFBTSxHQUFHLHdCQUFXLENBQUMsS0FBSyxDQUFDLENBQUM7UUFDakMsS0FBSSxDQUFDLE9BQU8sR0FBRywrQkFBZ0IsQ0FBQyxLQUFJLENBQUMsQ0FBQztRQUN0QyxLQUFJLENBQUMsTUFBTSxHQUFHLElBQUksZUFBTSxDQUFDLEtBQUksQ0FBQyxDQUFDO1FBQy9CLDBCQUEwQjtRQUMxQiw4Q0FBOEM7UUFDOUMsV0FBVztRQUNYLEtBQUksQ0FBQyxTQUFTLEdBQUcsSUFBSSwrQkFBYyxDQUFDLEtBQUksQ0FBQyxDQUFDO1FBQzFDL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