Dir exists separate fn

This commit is contained in:
Wietse Wind
2023-10-02 00:44:26 +02:00
parent dcac9e3c61
commit 06c8099df4
2 changed files with 11 additions and 5 deletions

7
lib/dirExists.mjs Normal file
View File

@@ -0,0 +1,7 @@
import { stat } from 'fs'
const dirExists = async storeDir => await new Promise(resolve => stat(storeDir, staterr => resolve(!staterr)))
export {
dirExists,
}

View File

@@ -1,7 +1,7 @@
import { writeFile } from 'fs'
import { stat } from 'fs'
import { ledgerIndexToFolders } from './ledgerIndexToFolders.mjs'
import { computeBinaryTransactionHash } from './computeBinaryTransactionHash.mjs'
import { dirExists } from './dirExists.mjs'
import 'dotenv/config'
const obtainedHumanReadableLedgers = []
@@ -17,9 +17,7 @@ const onLedger = async ({
const relativeStoreDir = 'store/' + networkId + '/' + ledgerIndexToFolders(ledger.ledger_index)
const storeDir = new URL('../' + relativeStoreDir, import.meta.url).pathname
const dirExists = await new Promise(resolve => stat(storeDir, staterr => resolve(!staterr)))
if (dirExists) {
if (await dirExists(storeDir)) {
;[
...(
obtainedBinaryTxLedgers.indexOf(ledger.ledger_index) < 0
@@ -66,7 +64,7 @@ const onLedger = async ({
/**
* Merge transaction hashes with the raw tx data
*/
results.ledger.transactions.map(t => {
;(results?.ledger?.transactions || []).map(t => {
return Object.assign(t, { tx_id: computeBinaryTransactionHash(t.tx_blob), })
})
@@ -79,6 +77,7 @@ const onLedger = async ({
if (
results?.ledger?.parent_hash
&& obtainedHumanReadableLedgers.indexOf(ledger.ledger_index) < 0
&& results?.ledger?.ledger_hash === ledger?.ledger_hash
) {
/**
* See comment above "Store in array" ... - first one will be stored