@maukode/siesvi
Version:
siesvi is CSV library that use typescript and provide CSV common functions from parsing, validation, to transformation.
93 lines (92 loc) • 3.5 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileLoader = void 0;
const stream_1 = require("stream");
const promises_1 = require("fs/promises");
class FileLoader extends stream_1.Readable {
constructor(filePath, chunkSize = 64 * 1024) {
super();
this.fileHandle = null;
this.position = 0;
this.fileSize = 0;
this.filePath = filePath;
this.chunkSize = chunkSize;
}
_construct(callback) {
return __awaiter(this, void 0, void 0, function* () {
try {
this.fileHandle = yield (0, promises_1.open)(this.filePath, 'r');
const stats = yield this.fileHandle.stat();
this.fileSize = stats.size;
callback(null);
}
catch (error) {
callback(error);
}
});
}
_read(_) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.fileHandle) {
this.destroy(new Error('File handle not initialized.'));
return;
}
if (this.position >= this.fileSize) {
this.push(null); // End of stream
if (this.fileHandle) {
this.fileHandle.close();
this.fileHandle = null;
}
return;
}
const buffer = Buffer.alloc(Math.min(this.chunkSize, this.fileSize - this.position));
try {
const { bytesRead } = yield this.fileHandle.read(buffer, 0, buffer.length, this.position);
if (bytesRead === 0) {
this.push(null); // End of stream
if (this.fileHandle) {
this.fileHandle.close();
this.fileHandle = null;
}
return;
}
this.position += bytesRead;
this.push(buffer.subarray(0, bytesRead));
}
catch (error) {
this.destroy(error);
if (this.fileHandle) {
this.fileHandle.close();
this.fileHandle = null;
}
}
});
}
_destroy(error, callback) {
return __awaiter(this, void 0, void 0, function* () {
if (this.fileHandle) {
try {
yield this.fileHandle.close();
this.fileHandle = null;
callback(error);
}
catch (closeError) {
callback(error || closeError);
}
}
else {
callback(error);
}
});
}
}
exports.FileLoader = FileLoader;