@forzalabs/remora 0.2.6 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Constants.js +10 -2
- package/actions/debug.js +1 -0
- package/actions/deploy.js +1 -0
- package/actions/run.js +17 -13
- package/actions/sample.js +1 -1
- package/core/Algo.js +8 -4
- package/definitions/ExecutorDefinitions.js +2 -0
- package/definitions/json_schemas/consumer-schema.json +1 -1
- package/definitions/json_schemas/producer-schema.json +1 -1
- package/definitions/temp.js +2 -0
- package/drivers/DeltaShareDriver.js +4 -0
- package/drivers/DriverFactory.js +10 -10
- package/drivers/DriverHelper.js +33 -10
- package/drivers/HttpApiDriver.js +4 -0
- package/drivers/LocalDriver.js +73 -6
- package/drivers/RedshiftDriver.js +4 -0
- package/drivers/S3Driver.js +36 -52
- package/drivers/files/LocalDestinationDriver.js +200 -0
- package/drivers/files/LocalSourceDriver.js +394 -0
- package/drivers/s3/S3DestinationDriver.js +159 -0
- package/drivers/s3/S3SourceDriver.js +455 -0
- package/engines/ai/LLM.js +0 -11
- package/engines/consumer/ConsumerEngine.js +0 -77
- package/engines/consumer/ConsumerManager.js +61 -36
- package/engines/consumer/ConsumerOnFinishManager.js +14 -0
- package/engines/consumer/PostProcessor.js +1 -7
- package/engines/dataset/Dataset.js +0 -61
- package/engines/dataset/DatasetManager.js +16 -76
- package/engines/dataset/DatasetRecord.js +4 -3
- package/engines/deployment/DeploymentPlanner.js +0 -7
- package/engines/execution/ExecutionPlanner.js +2 -2
- package/engines/execution/RequestExecutor.js +4 -45
- package/engines/file/FileExporter.js +7 -32
- package/engines/parsing/CSVParser.js +27 -26
- package/engines/parsing/LineParser.js +52 -0
- package/engines/parsing/XMLParser.js +1 -1
- package/engines/producer/ProducerEngine.js +0 -45
- package/engines/scheduler/CronScheduler.js +12 -4
- package/engines/scheduler/QueueManager.js +11 -4
- package/engines/sql/SQLCompiler.js +4 -4
- package/engines/transform/JoinEngine.js +3 -3
- package/engines/transform/TransformationEngine.js +3 -89
- package/engines/usage/UsageManager.js +8 -6
- package/engines/validation/Validator.js +12 -18
- package/executors/ConsumerExecutor.js +152 -0
- package/executors/Executor.js +168 -0
- package/executors/ExecutorOrchestrator.js +315 -0
- package/executors/ExecutorPerformance.js +17 -0
- package/executors/ExecutorProgress.js +52 -0
- package/executors/OutputExecutor.js +118 -0
- package/executors/ProducerExecutor.js +108 -0
- package/package.json +3 -3
- package/workers/ExecutorWorker.js +48 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
45
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
46
|
+
var m = o[Symbol.asyncIterator], i;
|
|
47
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
48
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
49
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
50
|
+
};
|
|
51
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
52
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
53
|
+
};
|
|
54
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
|
+
const fs = __importStar(require("fs"));
|
|
56
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
57
|
+
const readline_1 = __importDefault(require("readline"));
|
|
58
|
+
const path_1 = __importDefault(require("path"));
|
|
59
|
+
const Affirm_1 = __importDefault(require("../../core/Affirm"));
|
|
60
|
+
const FileExporter_1 = __importDefault(require("../../engines/file/FileExporter"));
|
|
61
|
+
const Logger_1 = __importDefault(require("../../helper/Logger"));
|
|
62
|
+
class LocalDestinationDriver {
|
|
63
|
+
constructor() {
|
|
64
|
+
this.init = (source) => __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
(0, Affirm_1.default)(source, `Invalid source`);
|
|
66
|
+
const fileURL = source.authentication['path'];
|
|
67
|
+
(0, Affirm_1.default)(fileURL, `Missing file path in the authentication of source "${source.name}"`);
|
|
68
|
+
const exist = fs.existsSync(fileURL);
|
|
69
|
+
(0, Affirm_1.default)(exist, `The path (${fileURL}) for source "${source.name}" does NOT exist.`);
|
|
70
|
+
this._path = source.authentication['path'];
|
|
71
|
+
return this;
|
|
72
|
+
});
|
|
73
|
+
this.uploadFile = (options) => __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
(0, Affirm_1.default)(this._path, 'Path not initialized');
|
|
75
|
+
(0, Affirm_1.default)(options, 'Invalid upload options');
|
|
76
|
+
(0, Affirm_1.default)(options.name, 'File name is required');
|
|
77
|
+
(0, Affirm_1.default)(options.content != null, 'File content is required');
|
|
78
|
+
const folder = this._path;
|
|
79
|
+
try {
|
|
80
|
+
if (!fs.existsSync(folder))
|
|
81
|
+
fs.mkdirSync(folder, { recursive: true });
|
|
82
|
+
const filePath = path_1.default.join(folder, options.name);
|
|
83
|
+
fs.writeFileSync(filePath, options.content);
|
|
84
|
+
return { bucket: folder, key: filePath, res: true };
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
throw new Error(`Failed to upload local file "${options.name}": ${error.message}`);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
this.uploadStream = (options) => __awaiter(this, void 0, void 0, function* () {
|
|
91
|
+
(0, Affirm_1.default)(options, `Invalid upload options`);
|
|
92
|
+
const { dataset, name, recordProjection } = options;
|
|
93
|
+
(0, Affirm_1.default)(dataset, 'No streaming dataset');
|
|
94
|
+
(0, Affirm_1.default)(name, 'No filename provided for upload stream');
|
|
95
|
+
(0, Affirm_1.default)(recordProjection, 'No recordProjection for upload stream');
|
|
96
|
+
const folder = this._path;
|
|
97
|
+
try {
|
|
98
|
+
if (!fs.existsSync(folder))
|
|
99
|
+
fs.mkdirSync(folder, { recursive: true });
|
|
100
|
+
const filePath = path_1.default.join(folder, options.name);
|
|
101
|
+
fs.writeFileSync(filePath, '');
|
|
102
|
+
yield dataset.streamBatches((batch) => __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
const chunks = FileExporter_1.default.prepareBatch(batch, options);
|
|
104
|
+
for (const chunk of chunks)
|
|
105
|
+
fs.appendFileSync(filePath, chunk);
|
|
106
|
+
}));
|
|
107
|
+
return { bucket: folder, key: filePath, res: true };
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
// Clean up the partial file if it exists
|
|
111
|
+
const filePath = path_1.default.join(folder, options.name);
|
|
112
|
+
if (fs.existsSync(filePath)) {
|
|
113
|
+
try {
|
|
114
|
+
fs.unlinkSync(filePath);
|
|
115
|
+
}
|
|
116
|
+
catch (cleanupError) {
|
|
117
|
+
console.error(`Failed to clean up partial file after error: ${cleanupError.message}`);
|
|
118
|
+
throw cleanupError;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
throw new Error(`Failed to complete local multipart upload for "${options.name}": ${error.message}`);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
this.saveFile = (fileKey, content) => {
|
|
125
|
+
(0, Affirm_1.default)(this._path, 'Path not initialized');
|
|
126
|
+
(0, Affirm_1.default)(fileKey, 'Invalid file key');
|
|
127
|
+
(0, Affirm_1.default)(content, 'Invalid content');
|
|
128
|
+
const filePath = path_1.default.join(this._path, fileKey);
|
|
129
|
+
const directory = path_1.default.dirname(filePath);
|
|
130
|
+
// Create directory if it doesn't exist
|
|
131
|
+
if (!fs.existsSync(directory)) {
|
|
132
|
+
fs.mkdirSync(directory, { recursive: true });
|
|
133
|
+
}
|
|
134
|
+
fs.writeFileSync(filePath, content);
|
|
135
|
+
return Promise.resolve();
|
|
136
|
+
};
|
|
137
|
+
this.copyFromLocal = (sourceFilePath, destinationFileKey) => {
|
|
138
|
+
(0, Affirm_1.default)(this._path, 'Path not initialized');
|
|
139
|
+
(0, Affirm_1.default)(sourceFilePath, 'Invalid source file path');
|
|
140
|
+
(0, Affirm_1.default)(destinationFileKey, 'Invalid destination file key');
|
|
141
|
+
const destinationFilePath = path_1.default.join(this._path, destinationFileKey);
|
|
142
|
+
const destinationDir = path_1.default.dirname(destinationFilePath);
|
|
143
|
+
// Ensure destination directory exists
|
|
144
|
+
if (!fs.existsSync(destinationDir)) {
|
|
145
|
+
fs.mkdirSync(destinationDir, { recursive: true });
|
|
146
|
+
}
|
|
147
|
+
fs.copyFileSync(sourceFilePath, destinationFilePath);
|
|
148
|
+
};
|
|
149
|
+
this.copyFromS3 = (s3Driver, sourceFileKey, destinationFileKey) => __awaiter(this, void 0, void 0, function* () {
|
|
150
|
+
const fileContent = yield s3Driver.downloadFile(sourceFileKey);
|
|
151
|
+
yield this.saveFile(destinationFileKey, fileContent);
|
|
152
|
+
});
|
|
153
|
+
this.ready = (destinationPath) => __awaiter(this, void 0, void 0, function* () {
|
|
154
|
+
return fs.createWriteStream(destinationPath);
|
|
155
|
+
});
|
|
156
|
+
this.move = (fromPath, toName) => __awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
try {
|
|
158
|
+
const toFilePath = path_1.default.join(this._path, toName);
|
|
159
|
+
yield promises_1.default.rename(fromPath, toFilePath);
|
|
160
|
+
return { bucket: '', key: toFilePath, res: true };
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
Logger_1.default.error(error);
|
|
164
|
+
return { bucket: '', key: '', res: false };
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
this.transformAndMove = (fromPath, transform, toName) => __awaiter(this, void 0, void 0, function* () {
|
|
168
|
+
var _a, e_1, _b, _c;
|
|
169
|
+
try {
|
|
170
|
+
const toFilePath = path_1.default.join(this._path, toName);
|
|
171
|
+
const reader = fs.createReadStream(fromPath);
|
|
172
|
+
const lineReader = readline_1.default.createInterface({ input: reader, crlfDelay: Infinity });
|
|
173
|
+
const writer = fs.createWriteStream(toFilePath);
|
|
174
|
+
try {
|
|
175
|
+
for (var _d = true, lineReader_1 = __asyncValues(lineReader), lineReader_1_1; lineReader_1_1 = yield lineReader_1.next(), _a = lineReader_1_1.done, !_a; _d = true) {
|
|
176
|
+
_c = lineReader_1_1.value;
|
|
177
|
+
_d = false;
|
|
178
|
+
const line = _c;
|
|
179
|
+
writer.write(transform(line) + '\n');
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
183
|
+
finally {
|
|
184
|
+
try {
|
|
185
|
+
if (!_d && !_a && (_b = lineReader_1.return)) yield _b.call(lineReader_1);
|
|
186
|
+
}
|
|
187
|
+
finally { if (e_1) throw e_1.error; }
|
|
188
|
+
}
|
|
189
|
+
writer.close();
|
|
190
|
+
reader.close();
|
|
191
|
+
return { bucket: '', key: toFilePath, res: true };
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
Logger_1.default.error(error);
|
|
195
|
+
return { bucket: '', key: '', res: false };
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
exports.default = LocalDestinationDriver;
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
45
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
46
|
+
var m = o[Symbol.asyncIterator], i;
|
|
47
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
48
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
49
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
50
|
+
};
|
|
51
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
52
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
53
|
+
};
|
|
54
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
|
+
const fs = __importStar(require("fs"));
|
|
56
|
+
const path_1 = __importDefault(require("path"));
|
|
57
|
+
const readline_1 = __importDefault(require("readline"));
|
|
58
|
+
const Affirm_1 = __importDefault(require("../../core/Affirm"));
|
|
59
|
+
const Algo_1 = __importDefault(require("../../core/Algo"));
|
|
60
|
+
const xlsx_1 = __importDefault(require("xlsx"));
|
|
61
|
+
const XMLParser_1 = __importDefault(require("../../engines/parsing/XMLParser")); // Added XMLParser import
|
|
62
|
+
const XLSParser_1 = __importDefault(require("../../engines/parsing/XLSParser"));
|
|
63
|
+
const Helper_1 = __importDefault(require("../../helper/Helper"));
|
|
64
|
+
const ParseHelper_1 = __importDefault(require("../../engines/parsing/ParseHelper"));
|
|
65
|
+
const Logger_1 = __importDefault(require("../../helper/Logger"));
|
|
66
|
+
const DriverHelper_1 = __importDefault(require("../DriverHelper"));
|
|
67
|
+
class LocalSourceDriver {
|
|
68
|
+
constructor() {
|
|
69
|
+
this.init = (source) => __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
const fileURL = source.authentication['path'];
|
|
71
|
+
(0, Affirm_1.default)(fileURL, `Missing file path in the authentication of source "${source.name}"`);
|
|
72
|
+
const exist = fs.existsSync(fileURL);
|
|
73
|
+
(0, Affirm_1.default)(exist, `The path (${fileURL}) for source "${source.name}" does NOT exist.`);
|
|
74
|
+
this._path = source.authentication['path'];
|
|
75
|
+
return this;
|
|
76
|
+
});
|
|
77
|
+
this.readAll = (request) => __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
(0, Affirm_1.default)(this._path, `Invalid path`);
|
|
79
|
+
(0, Affirm_1.default)(request, `Invalid download request`);
|
|
80
|
+
(0, Affirm_1.default)(request.fileKey, `Invalid file key for download request`);
|
|
81
|
+
(0, Affirm_1.default)(request.fileType, `Invalid file type for download request`);
|
|
82
|
+
const { fileKey } = request;
|
|
83
|
+
if (fileKey.includes('%')) {
|
|
84
|
+
const allFileKeys = this.listFiles(fileKey);
|
|
85
|
+
Logger_1.default.log(`Matched ${allFileKeys.length} files, copying to locally and creating unified dataset.`);
|
|
86
|
+
const firstPath = path_1.default.join(this._path, allFileKeys[0]);
|
|
87
|
+
const headerLine = (yield DriverHelper_1.default.quickReadFile(firstPath, 1))[0];
|
|
88
|
+
const promises = allFileKeys.map((x, i) => this._get(Object.assign(Object.assign({}, request), { fileKey: x }), headerLine, i));
|
|
89
|
+
const results = yield Promise.all(promises);
|
|
90
|
+
return results.flat();
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
return yield this._get(request, '');
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
this.readLinesInRange = (request) => __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
(0, Affirm_1.default)(this._path, `Invalid path`);
|
|
98
|
+
(0, Affirm_1.default)(request, 'Invalid read options');
|
|
99
|
+
(0, Affirm_1.default)(request.fileKey, 'Invalid file key');
|
|
100
|
+
(0, Affirm_1.default)(request.fileType, `Invalid file type`);
|
|
101
|
+
(0, Affirm_1.default)(request.options, `Invalid request options`);
|
|
102
|
+
Affirm_1.default.hasValue(request.options.lineFrom, `Invalid request options line from`);
|
|
103
|
+
Affirm_1.default.hasValue(request.options.lineTo, `Invalid request options line to`);
|
|
104
|
+
const { fileKey } = request;
|
|
105
|
+
if (fileKey.includes('%')) {
|
|
106
|
+
const allFileKeys = this.listFiles(fileKey);
|
|
107
|
+
Logger_1.default.log(`Matched ${allFileKeys.length} files, copying to locally and creating unified dataset.`);
|
|
108
|
+
const firstPath = path_1.default.join(this._path, allFileKeys[0]);
|
|
109
|
+
const headerLine = (yield DriverHelper_1.default.quickReadFile(firstPath, 1))[0];
|
|
110
|
+
const promises = allFileKeys.map((x, i) => this._get(Object.assign(Object.assign({}, request), { fileKey: x }), headerLine, i));
|
|
111
|
+
const results = yield Promise.all(promises);
|
|
112
|
+
return results.flat();
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
return yield this._get(request, '');
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
this.download = (dataset) => __awaiter(this, void 0, void 0, function* () {
|
|
119
|
+
(0, Affirm_1.default)(this._path, `Invalid path`);
|
|
120
|
+
(0, Affirm_1.default)(dataset, `Invalid dataset`);
|
|
121
|
+
const file = dataset.getFile();
|
|
122
|
+
(0, Affirm_1.default)(file, 'Invalid dataset file');
|
|
123
|
+
(0, Affirm_1.default)(file.fileKey, 'Invalid file key');
|
|
124
|
+
(0, Affirm_1.default)(file.fileType, `Invalid file type`);
|
|
125
|
+
const includeSourceFilename = file.includeSourceFilename === true;
|
|
126
|
+
const copyLocally = (fileKey_1, headerLine_1, ...args_1) => __awaiter(this, [fileKey_1, headerLine_1, ...args_1], void 0, function* (fileKey, headerLine, appendMode = false, sourceFilename, stream) {
|
|
127
|
+
const sourceFilePath = path_1.default.join(this._path, fileKey);
|
|
128
|
+
(0, Affirm_1.default)(fs.existsSync(sourceFilePath), `Source file does not exist: ${sourceFilePath}`);
|
|
129
|
+
// Copy and validate header in a single stream pass
|
|
130
|
+
const readStream = fs.createReadStream(sourceFilePath);
|
|
131
|
+
let streamToUse = readStream;
|
|
132
|
+
if (['XLS', 'XLSX'].includes(file.fileType))
|
|
133
|
+
streamToUse = stream;
|
|
134
|
+
else
|
|
135
|
+
streamToUse = readStream;
|
|
136
|
+
return DriverHelper_1.default.appendToUnifiedFile({
|
|
137
|
+
stream: streamToUse,
|
|
138
|
+
fileKey,
|
|
139
|
+
destinationPath: dataset.getPath(),
|
|
140
|
+
append: appendMode,
|
|
141
|
+
headerLine,
|
|
142
|
+
fileType: file.fileType,
|
|
143
|
+
hasHeaderRow: file.hasHeaderRow,
|
|
144
|
+
delimiter: dataset.getDelimiter(),
|
|
145
|
+
sourceFilename
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
// this function copy the local file on the temporary file and retrive the number of line.
|
|
149
|
+
const handleFileAndGetLineCount = (fileKey, appendMode, fileType, sourceFilename) => __awaiter(this, void 0, void 0, function* () {
|
|
150
|
+
let totalLineCount;
|
|
151
|
+
let streamXLS;
|
|
152
|
+
switch (fileType) {
|
|
153
|
+
case 'XLS':
|
|
154
|
+
case 'XLSX':
|
|
155
|
+
streamXLS = (yield XLSParser_1.default.getStreamXls(path_1.default.join(this._path, fileKey), file.sheetName));
|
|
156
|
+
totalLineCount = yield copyLocally(fileKey, dataset.getFirstLine(), appendMode, sourceFilename, streamXLS);
|
|
157
|
+
break;
|
|
158
|
+
default:
|
|
159
|
+
totalLineCount = yield copyLocally(fileKey, dataset.getFirstLine(), appendMode, sourceFilename);
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
return totalLineCount;
|
|
163
|
+
});
|
|
164
|
+
const { fileKey } = file;
|
|
165
|
+
let totalLineCount = 0;
|
|
166
|
+
let sourceFilename;
|
|
167
|
+
if (fileKey.includes('%')) {
|
|
168
|
+
const allFileKeys = this.listFiles(fileKey);
|
|
169
|
+
yield DriverHelper_1.default.setHeaderFromFile(allFileKeys[0], file, this._path, dataset);
|
|
170
|
+
Logger_1.default.log(`Matched ${allFileKeys.length} files, copying locally and creating unified dataset.`);
|
|
171
|
+
Affirm_1.default.hasItems(allFileKeys, `The file key "${fileKey}" doesn't have any matches in path "${this._path}".`);
|
|
172
|
+
totalLineCount = 0;
|
|
173
|
+
// Copy files sequentially to avoid file conflicts
|
|
174
|
+
for (let i = 0; i < allFileKeys.length; i++) {
|
|
175
|
+
const currentFileKey = allFileKeys[i];
|
|
176
|
+
// Pass the filename (just the basename) if includeSourceFilename is enabled
|
|
177
|
+
const sourceFilename = includeSourceFilename ? path_1.default.basename(currentFileKey) : undefined;
|
|
178
|
+
totalLineCount += yield handleFileAndGetLineCount(currentFileKey, i > 0, file.fileType, sourceFilename); // Append mode for subsequent files
|
|
179
|
+
}
|
|
180
|
+
dataset.setCount(totalLineCount);
|
|
181
|
+
return dataset;
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
sourceFilename = includeSourceFilename ? path_1.default.basename(fileKey) : undefined;
|
|
185
|
+
yield DriverHelper_1.default.setHeaderFromFile(fileKey, file, this._path, dataset);
|
|
186
|
+
totalLineCount = (yield handleFileAndGetLineCount(fileKey, false, file.fileType, sourceFilename));
|
|
187
|
+
dataset.setCount(totalLineCount);
|
|
188
|
+
return dataset;
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
this.exist = (producer) => __awaiter(this, void 0, void 0, function* () {
|
|
192
|
+
(0, Affirm_1.default)(this._path, `Invalid path`);
|
|
193
|
+
(0, Affirm_1.default)(producer, `Invalid producer`);
|
|
194
|
+
const fileKey = producer.settings.fileKey;
|
|
195
|
+
(0, Affirm_1.default)(fileKey, `Invalid file key for download request`);
|
|
196
|
+
if (fileKey.includes('%')) {
|
|
197
|
+
const allFileKeys = this.listFiles(fileKey);
|
|
198
|
+
return allFileKeys.length > 0;
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
const fileUrl = path_1.default.join(this._path, fileKey);
|
|
202
|
+
return fs.existsSync(fileUrl);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
this._readLines = (fileUri, lineFrom, lineTo) => __awaiter(this, void 0, void 0, function* () {
|
|
206
|
+
var _a, e_1, _b, _c;
|
|
207
|
+
const stream = fs.createReadStream(fileUri);
|
|
208
|
+
const reader = readline_1.default.createInterface({ input: stream, crlfDelay: Infinity });
|
|
209
|
+
const lines = [];
|
|
210
|
+
let lineCounter = 0;
|
|
211
|
+
try {
|
|
212
|
+
for (var _d = true, reader_1 = __asyncValues(reader), reader_1_1; reader_1_1 = yield reader_1.next(), _a = reader_1_1.done, !_a; _d = true) {
|
|
213
|
+
_c = reader_1_1.value;
|
|
214
|
+
_d = false;
|
|
215
|
+
const line = _c;
|
|
216
|
+
if (Algo_1.default.hasVal(lineFrom) && Algo_1.default.hasVal(lineTo)) {
|
|
217
|
+
if (lineCounter >= lineFrom && lineCounter < lineTo) {
|
|
218
|
+
if (line && line.length > 0)
|
|
219
|
+
lines.push(line);
|
|
220
|
+
}
|
|
221
|
+
lineCounter++;
|
|
222
|
+
if (lineCounter >= lineTo)
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
if (line && line.length > 0)
|
|
227
|
+
lines.push(line);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
232
|
+
finally {
|
|
233
|
+
try {
|
|
234
|
+
if (!_d && !_a && (_b = reader_1.return)) yield _b.call(reader_1);
|
|
235
|
+
}
|
|
236
|
+
finally { if (e_1) throw e_1.error; }
|
|
237
|
+
}
|
|
238
|
+
reader.close();
|
|
239
|
+
stream.close();
|
|
240
|
+
return lines;
|
|
241
|
+
});
|
|
242
|
+
this._readExcelLines = (fileUri, sheetName, lineFrom, lineTo) => __awaiter(this, void 0, void 0, function* () {
|
|
243
|
+
const excel = xlsx_1.default.readFile(fileUri);
|
|
244
|
+
let targetSheetName = sheetName;
|
|
245
|
+
if (!targetSheetName) {
|
|
246
|
+
(0, Affirm_1.default)(excel.SheetNames.length > 0, 'The Excel file has no sheets.');
|
|
247
|
+
targetSheetName = excel.SheetNames[0];
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
(0, Affirm_1.default)(excel.SheetNames.includes(targetSheetName), `The sheet "${targetSheetName}" doesn't exist in the excel (available: ${excel.SheetNames.join(', ')})`);
|
|
251
|
+
}
|
|
252
|
+
const sheet = excel.Sheets[targetSheetName];
|
|
253
|
+
const csv = xlsx_1.default.utils.sheet_to_csv(sheet);
|
|
254
|
+
const lines = csv.split('\n');
|
|
255
|
+
if (Algo_1.default.hasVal(lineFrom) && Algo_1.default.hasVal(lineTo))
|
|
256
|
+
return lines.slice(lineFrom, lineTo + 1);
|
|
257
|
+
else
|
|
258
|
+
return lines;
|
|
259
|
+
});
|
|
260
|
+
this._readXmlLines = (fileUri, lineFrom, lineTo) => __awaiter(this, void 0, void 0, function* () {
|
|
261
|
+
const fileContent = fs.readFileSync(fileUri, 'utf-8');
|
|
262
|
+
const jsonData = XMLParser_1.default.xmlToJson(fileContent);
|
|
263
|
+
// Convert JSON data to string lines. This might need adjustment based on XML structure.
|
|
264
|
+
// Assuming jsonData is an array of objects, where each object is a record.
|
|
265
|
+
let lines = Array.isArray(jsonData) ? jsonData.map(item => JSON.stringify(item)) : [JSON.stringify(jsonData)];
|
|
266
|
+
if (Algo_1.default.hasVal(lineFrom) && Algo_1.default.hasVal(lineTo)) {
|
|
267
|
+
lines = lines.slice(lineFrom, lineTo + 1);
|
|
268
|
+
}
|
|
269
|
+
return lines;
|
|
270
|
+
});
|
|
271
|
+
this._get = (request, headerLine, index) => __awaiter(this, void 0, void 0, function* () {
|
|
272
|
+
const { fileKey, fileType, options } = request;
|
|
273
|
+
let lineFrom, lineTo, sheetName, hasHeaderRow;
|
|
274
|
+
if (options) {
|
|
275
|
+
lineFrom = options.lineFrom;
|
|
276
|
+
lineTo = options.lineTo;
|
|
277
|
+
sheetName = options.sheetName;
|
|
278
|
+
hasHeaderRow = options.hasHeaderRow;
|
|
279
|
+
}
|
|
280
|
+
const fileUrl = path_1.default.join(this._path, fileKey);
|
|
281
|
+
let lines = [];
|
|
282
|
+
switch (fileType) {
|
|
283
|
+
case 'CSV':
|
|
284
|
+
case 'JSON':
|
|
285
|
+
case 'JSONL':
|
|
286
|
+
case 'TXT':
|
|
287
|
+
if (Algo_1.default.hasVal(lineFrom) && Algo_1.default.hasVal(lineTo))
|
|
288
|
+
lines = yield this._readLines(fileUrl, lineFrom, lineTo);
|
|
289
|
+
else
|
|
290
|
+
lines = yield this._readLines(fileUrl);
|
|
291
|
+
break;
|
|
292
|
+
case 'XLS':
|
|
293
|
+
case 'XLSX':
|
|
294
|
+
if (Algo_1.default.hasVal(lineFrom) && Algo_1.default.hasVal(lineTo))
|
|
295
|
+
lines = yield this._readExcelLines(fileUrl, sheetName, lineFrom, lineTo);
|
|
296
|
+
else
|
|
297
|
+
lines = yield this._readExcelLines(fileUrl, sheetName);
|
|
298
|
+
break;
|
|
299
|
+
case 'XML':
|
|
300
|
+
if (Algo_1.default.hasVal(lineFrom) && Algo_1.default.hasVal(lineTo))
|
|
301
|
+
lines = yield this._readXmlLines(fileUrl, lineFrom, lineTo);
|
|
302
|
+
else
|
|
303
|
+
lines = yield this._readXmlLines(fileUrl);
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
const firstLine = lines[0];
|
|
307
|
+
if (headerLine && headerLine.trim() !== '' && firstLine.trim() !== headerLine.trim()) {
|
|
308
|
+
const msg = `Error creating unified dataset: file "${fileKey}" has a different header line than the other files in this dataset\n\t-${fileKey}: ${firstLine}\n\t-main: ${headerLine}`;
|
|
309
|
+
Logger_1.default.log(msg);
|
|
310
|
+
throw new Error(msg);
|
|
311
|
+
}
|
|
312
|
+
// If this is not the first file read in a pattern match AND the file type has an header,
|
|
313
|
+
// then I need to remove the header from the resulting lines or the header will be duplicated
|
|
314
|
+
if (index > 0 && ParseHelper_1.default.shouldHaveHeader(fileType, hasHeaderRow)) {
|
|
315
|
+
lines = lines.slice(1);
|
|
316
|
+
}
|
|
317
|
+
return lines;
|
|
318
|
+
});
|
|
319
|
+
this.listFiles = (filekeyPattern) => {
|
|
320
|
+
(0, Affirm_1.default)(this._path, 'Path not initialized');
|
|
321
|
+
try {
|
|
322
|
+
// Get all files in the directory (recursively if needed)
|
|
323
|
+
const getAllFiles = (dirPath, basePath = '') => {
|
|
324
|
+
const files = [];
|
|
325
|
+
const items = fs.readdirSync(dirPath);
|
|
326
|
+
for (const item of items) {
|
|
327
|
+
const fullPath = path_1.default.join(dirPath, item);
|
|
328
|
+
const relativePath = basePath ? path_1.default.join(basePath, item) : item;
|
|
329
|
+
const stats = fs.statSync(fullPath);
|
|
330
|
+
if (stats.isDirectory()) {
|
|
331
|
+
// Recursively get files from subdirectories
|
|
332
|
+
files.push(...getAllFiles(fullPath, relativePath));
|
|
333
|
+
}
|
|
334
|
+
else if (stats.isFile()) {
|
|
335
|
+
files.push(relativePath);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return files;
|
|
339
|
+
};
|
|
340
|
+
const allFiles = getAllFiles(this._path);
|
|
341
|
+
return Helper_1.default.matchPattern(filekeyPattern, allFiles);
|
|
342
|
+
}
|
|
343
|
+
catch (error) {
|
|
344
|
+
throw new Error(`Failed to list files in directory "${this._path}": ${error.message}`);
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
this.readFile = (fileKey) => {
|
|
348
|
+
(0, Affirm_1.default)(this._path, 'Path not initialized');
|
|
349
|
+
(0, Affirm_1.default)(fileKey, 'Invalid file key');
|
|
350
|
+
const filePath = path_1.default.join(this._path, fileKey);
|
|
351
|
+
(0, Affirm_1.default)(fs.existsSync(filePath), `Source file does not exist: ${filePath}`);
|
|
352
|
+
return fs.readFileSync(filePath);
|
|
353
|
+
};
|
|
354
|
+
this.deleteFile = (fileKey) => {
|
|
355
|
+
(0, Affirm_1.default)(this._path, 'Path not initialized');
|
|
356
|
+
(0, Affirm_1.default)(fileKey, 'Invalid file key');
|
|
357
|
+
const filePath = path_1.default.join(this._path, fileKey);
|
|
358
|
+
if (fs.existsSync(filePath)) {
|
|
359
|
+
fs.unlinkSync(filePath);
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
this.moveFile = (sourceFileKey, destinationPath, destinationFileKey) => {
|
|
363
|
+
(0, Affirm_1.default)(this._path, 'Path not initialized');
|
|
364
|
+
(0, Affirm_1.default)(sourceFileKey, 'Invalid source file key');
|
|
365
|
+
(0, Affirm_1.default)(destinationPath, 'Invalid destination path');
|
|
366
|
+
(0, Affirm_1.default)(destinationFileKey, 'Invalid destination file key');
|
|
367
|
+
const sourceFilePath = path_1.default.join(this._path, sourceFileKey);
|
|
368
|
+
const destinationFilePath = path_1.default.join(destinationPath, destinationFileKey);
|
|
369
|
+
(0, Affirm_1.default)(fs.existsSync(sourceFilePath), `Source file does not exist: ${sourceFilePath}`);
|
|
370
|
+
// Ensure destination directory exists
|
|
371
|
+
const destinationDir = path_1.default.dirname(destinationFilePath);
|
|
372
|
+
if (!fs.existsSync(destinationDir)) {
|
|
373
|
+
fs.mkdirSync(destinationDir, { recursive: true });
|
|
374
|
+
}
|
|
375
|
+
fs.renameSync(sourceFilePath, destinationFilePath);
|
|
376
|
+
};
|
|
377
|
+
this.ready = (producer) => __awaiter(this, void 0, void 0, function* () {
|
|
378
|
+
(0, Affirm_1.default)(producer, 'Invalid producer');
|
|
379
|
+
// TODO: extra logic for encoded files (xml, xls, ...) to be decoded and prepared locally as a plain CSV
|
|
380
|
+
// then return the uri to this new temporary file
|
|
381
|
+
const { fileKey } = producer.settings;
|
|
382
|
+
if (fileKey.includes('%')) {
|
|
383
|
+
const allFileKeys = this.listFiles(fileKey);
|
|
384
|
+
const allFilePaths = allFileKeys.map(x => path_1.default.join(this._path, x));
|
|
385
|
+
return { files: allFilePaths.map(x => ({ fullUri: x })) };
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
const sourceFilePath = path_1.default.join(this._path, fileKey);
|
|
389
|
+
return { files: [{ fullUri: sourceFilePath }] };
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
exports.default = LocalSourceDriver;
|