-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const fs = require('fs').promises; | ||
const path = require('path'); | ||
const zlib = require('zlib'); | ||
const util = require('util'); | ||
|
||
const gzip = util.promisify(zlib.gzip); | ||
const gunzip = util.promisify(zlib.gunzip); | ||
|
||
const epochDataDir = './epoch-data'; | ||
const indexFilePath = path.join(epochDataDir, 'index.json.gz'); | ||
|
||
async function readEpochSummaries() { | ||
try { | ||
const compressedIndexData = await fs.readFile(indexFilePath); | ||
const indexData = await gunzip(compressedIndexData); | ||
const summaries = JSON.parse(indexData.toString()); | ||
return summaries.sort((a, b) => a.epochHeight - b.epochHeight); | ||
} catch (error) { | ||
if (error.code !== 'ENOENT') { | ||
console.error('Error reading index.json.gz:', error); | ||
} | ||
|
||
return []; | ||
} | ||
} | ||
|
||
async function writeEpochSummaries(summaries) { | ||
const compressedSummaries = await gzip(JSON.stringify(summaries)); | ||
await fs.writeFile(indexFilePath, compressedSummaries); | ||
} | ||
|
||
async function writeEpochData(epochHeight, data) { | ||
await fs.mkdir(epochDataDir, { recursive: true }); | ||
const compressedData = await gzip(JSON.stringify(data)); | ||
await fs.writeFile( | ||
path.join(epochDataDir, `${epochHeight}.json.gz`), | ||
compressedData | ||
); | ||
} | ||
|
||
module.exports = { | ||
readEpochSummaries, | ||
writeEpochSummaries, | ||
writeEpochData | ||
}; |