tovarish-relayer/lib/services/data.js

152 lines
4.7 KiB
JavaScript
Raw Normal View History

2024-11-13 04:42:13 +03:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.existsAsync = existsAsync;
exports.zipAsync = zipAsync;
exports.unzipAsync = unzipAsync;
exports.saveUserFile = saveUserFile;
exports.saveLastBlock = saveLastBlock;
exports.loadLastBlock = loadLastBlock;
exports.loadSavedEvents = loadSavedEvents;
exports.download = download;
exports.loadCachedEvents = loadCachedEvents;
const path_1 = __importDefault(require("path"));
const promises_1 = require("fs/promises");
const fflate_1 = require("fflate");
async function existsAsync(fileOrDir) {
try {
await (0, promises_1.stat)(fileOrDir);
return true;
}
catch {
return false;
}
}
function zipAsync(file) {
return new Promise((res, rej) => {
(0, fflate_1.zip)(file, { mtime: new Date('1/1/1980') }, (err, data) => {
if (err) {
rej(err);
return;
}
res(data);
});
});
}
function unzipAsync(data) {
return new Promise((res, rej) => {
(0, fflate_1.unzip)(data, {}, (err, data) => {
if (err) {
rej(err);
return;
}
res(data);
});
});
}
async function saveUserFile({ fileName, userDirectory, dataString, lastBlock, }) {
fileName = fileName.toLowerCase();
const filePath = path_1.default.join(userDirectory, fileName);
const payload = await zipAsync({
[fileName]: new TextEncoder().encode(dataString),
});
if (!(await existsAsync(userDirectory))) {
await (0, promises_1.mkdir)(userDirectory, { recursive: true });
}
await (0, promises_1.writeFile)(filePath + '.zip', payload);
await (0, promises_1.writeFile)(filePath, dataString);
if (lastBlock) {
await saveLastBlock({
fileName: fileName.replace('.json', ''),
userDirectory,
lastBlock,
});
}
}
async function saveLastBlock({ fileName, userDirectory, lastBlock, }) {
const filePath = path_1.default.join(userDirectory, fileName);
if (lastBlock) {
await (0, promises_1.writeFile)(filePath + '.lastblock.txt', String(lastBlock));
}
}
async function loadLastBlock({ name, directory }) {
const filePath = path_1.default.join(directory, `${name}.lastblock.txt`);
if (!(await existsAsync(filePath))) {
return;
}
try {
const lastBlock = Number(await (0, promises_1.readFile)(filePath, { encoding: 'utf8' }));
if (lastBlock) {
return lastBlock;
}
// eslint-disable-next-line no-empty
}
catch { }
}
async function loadSavedEvents({ name, userDirectory, }) {
const filePath = path_1.default.join(userDirectory, `${name}.json`.toLowerCase());
2024-11-20 09:02:43 +03:00
const loadedBlock = await loadLastBlock({
name,
directory: userDirectory,
});
2024-11-13 04:42:13 +03:00
if (!(await existsAsync(filePath))) {
return {
events: [],
2024-11-20 09:02:43 +03:00
lastBlock: loadedBlock || 0,
2024-11-13 04:42:13 +03:00
};
}
try {
const events = JSON.parse(await (0, promises_1.readFile)(filePath, { encoding: 'utf8' }));
return {
events,
lastBlock: loadedBlock || events[events.length - 1]?.blockNumber || 0,
};
}
catch (err) {
console.log('Method loadSavedEvents has error');
console.log(err);
return {
events: [],
lastBlock: 0,
};
}
}
async function download({ name, cacheDirectory }) {
const fileName = `${name}.json`.toLowerCase();
const zipName = `${fileName}.zip`;
const zipPath = path_1.default.join(cacheDirectory, zipName);
const data = await (0, promises_1.readFile)(zipPath);
const { [fileName]: content } = await unzipAsync(data);
return new TextDecoder().decode(content);
}
async function loadCachedEvents({ name, cacheDirectory, deployedBlock, }) {
try {
const module = await download({ cacheDirectory, name });
if (module) {
const events = JSON.parse(module);
const lastBlock = events && events.length ? events[events.length - 1].blockNumber : deployedBlock;
return {
events,
lastBlock,
fromCache: true,
};
}
return {
events: [],
lastBlock: deployedBlock,
fromCache: true,
};
}
catch (err) {
console.log('Method loadCachedEvents has error');
console.log(err);
return {
events: [],
lastBlock: deployedBlock,
fromCache: true,
};
}
}