Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function preloadDisk( opt, disk ) {
if( fileState.loaded )
return; // already have the disk image in memory
if( fileState.flushPending || fileState.writing ) {
_debug && console.log( "wait for pending flush on another connection?" );
setTimeout( ()=>preloadDisk( opt, disk ), fileDelay/3 );
return;
}
fileState.reading = true;
_debug && console.log( new Date(), "preload happened." );
const stream = fs.createReadStream( fileName, { flags:"r", encoding:"utf8"} );
const parser = json6.begin( function(val) {
//_json_debug && console.log( "Recover:", val[0] );
disk[val[0]] = val[1];
} );
stream.on('open', function() {
_debug && console.log( new Date(), "Read stream opened.." );
} );
stream.on('error', function( err ){
if( err.code !== 'ENOENT' )
console.log( "READ STREAM ERROR:", err );
loaded = true;
fileState.reading = false;
handlePending();
} );
stream.on('data', function(chunk) {
//_json_debug && console.log( "got stream data",chunk );
parser.write( chunk );