iterparse
Version:
Delightful data parsing
163 lines (162 loc) • 5.94 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WriteProgress = exports.Progress = exports.formatBytes = exports.DownloadProgress = exports.getFileType = void 0;
const tslib_1 = require("tslib");
const path_1 = require("path");
const pretty_ms_1 = tslib_1.__importDefault(require("pretty-ms"));
const url_1 = require("url");
function getFileType(contentType) {
if (contentType.includes('application/json'))
return '.json';
if (contentType.includes('text/csv'))
return '.csv';
if (contentType.includes('application/xml'))
return '.xml';
if (contentType.includes('text/xml'))
return '.xml';
return;
}
exports.getFileType = getFileType;
class DownloadProgress {
constructor(url, startTime, totalSize) {
this.url = url;
this.startTime = startTime;
this.totalSize = totalSize;
this.downloaded = 0;
}
add(chunk) {
this.downloaded += chunk;
}
toString() {
const json = this.toJSON();
if (json.etaMs) {
return `URL: "${new url_1.URL(this.url).href}", Progress: ${(json.progress * 100).toFixed(2)}%, Downloaded: ${formatBytes(this.downloaded)}, Speed: ${json.speed}, ETA: ${json.etaMs}, Memory: ${formatBytes(process.memoryUsage().heapUsed)}`;
}
return `URL: "${new url_1.URL(this.url).href}", Downloaded: ${formatBytes(this.downloaded)}, Speed: ${json.speed}, Memory: ${formatBytes(process.memoryUsage().heapUsed)}`;
}
toJSON() {
const diff = Math.floor(Date.now() - this.startTime);
const bytesPerMs = Math.floor(this.downloaded / diff) || 0;
if (this.totalSize) {
const eta = (this.totalSize - this.downloaded) / bytesPerMs || 1 || 0;
const progress = this.downloaded / this.totalSize;
return {
eta,
progress,
url: this.url,
bytesPerSec: bytesPerMs * 1000,
speed: `${formatBytes(bytesPerMs * 1000)}/s`,
etaMs: pretty_ms_1.default(eta === Infinity ? 0 : eta),
startTime: this.startTime,
totalSize: this.totalSize,
downloaded: this.downloaded,
};
}
return {
url: this.url,
bytesPerSec: bytesPerMs * 1000,
speed: `${formatBytes(bytesPerMs * 1000)}/s`,
startTime: this.startTime,
totalSize: this.totalSize,
downloaded: this.downloaded,
};
}
}
exports.DownloadProgress = DownloadProgress;
function formatBytes(bytes, decimals = 2) {
if (bytes === 0)
return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + sizes[i];
}
exports.formatBytes = formatBytes;
class Progress {
constructor(filePath, totalSize, startTime) {
this.filePath = filePath;
this.totalSize = totalSize;
this.startTime = startTime;
this.parsedBytes = 0;
this.items = 0;
}
add(chunk) {
this.parsedBytes += chunk;
}
set(data) {
if (data.currentSize) {
this.parsedBytes = data.currentSize;
}
if (data.items) {
this.items = data.items;
}
}
addItem(count = 1) {
this.items += count;
}
toString() {
const json = this.toJSON();
if (json.progress === 0) {
return `File: "${path_1.relative(process.cwd(), this.filePath)}", Progress: 0%`;
}
return `File: "${path_1.relative(process.cwd(), this.filePath)}", Progress: ${(json.progress * 100).toFixed(2)}%, Items: ${this.items.toLocaleString()}, Speed: ${json.speed}, ETA: ${json.etaMs}, Memory: ${formatBytes(process.memoryUsage().heapUsed)}`;
}
toJSON() {
const diff = Math.floor(Date.now() - this.startTime);
const bytesPerMs = Math.floor(this.parsedBytes / diff) || 0;
const eta = (this.totalSize - this.parsedBytes) / bytesPerMs || 1 || 0;
return {
eta,
filePath: this.filePath,
items: this.items,
progress: this.parsedBytes / this.totalSize,
etaMs: pretty_ms_1.default(eta === Infinity ? 0 : eta),
bytesPerSec: bytesPerMs * 1000,
speed: `${formatBytes(bytesPerMs * 1000)}/s`,
startTime: this.startTime,
totalSize: this.totalSize,
parsedBytes: this.parsedBytes,
};
}
}
exports.Progress = Progress;
class WriteProgress {
constructor(filePath, startTime) {
this.filePath = filePath;
this.startTime = startTime;
this.writedBytes = 0;
this.items = 0;
}
add(chunk) {
this.writedBytes += chunk;
}
set(data) {
if (data.currentSize) {
this.writedBytes = data.currentSize;
}
if (data.items) {
this.items = data.items;
}
}
addItem(count = 1) {
this.items += count;
}
toString() {
const json = this.toJSON();
return `Writing. File: "${path_1.relative(process.cwd(), this.filePath)}", Items: ${this.items.toLocaleString()}, Speed: ${json.speed}, Memory: ${formatBytes(process.memoryUsage().heapUsed)}`;
}
toJSON() {
const diff = Math.floor(Date.now() - this.startTime);
const bytesPerMs = Math.floor(this.writedBytes / diff) || 0;
return {
filePath: this.filePath,
items: this.items,
bytesPerSec: bytesPerMs * 1000,
speed: `${formatBytes(bytesPerMs * 1000)}/s`,
startTime: this.startTime,
writedBytes: this.writedBytes,
};
}
}
exports.WriteProgress = WriteProgress;