Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
synced-append
Advanced tools
Append text files without leaving corrupted states between process crashes. A set of files can be grouped together so that appends to all those files can be committed at once making sure files are in sync.
A library that can be used to append multiple text files and do commits so that all files are in sync and doesn't leave in corrupted states between process crashes and restarts. In case you only have just one file to append still this library can be useful as it ensures files will not stuck in corrupted states.
This supports Atomicity, Consistency and Durability in ACID. Isolation is not supported.
Note: It's assumed fs.fsyncSync() ensures data written to the hardware.
If strings are appended to files using write() it's not guaranteed data written to the disk in an atomic manner. This makes sense as if it initiates a write to disk for each small string append, OS will be spending lot more time doing disk IO. But the problem with this is if the process crashes in an unexpected manner (e.g. a SIGKILL), it's possible that all the appends you did might not reach the disk.
Please go through following links to understand basic concepts on how to solve these types of problems.
npm install synced-append
SyncedAppend = require 'synced-append'
files =
country: './data/country.csv'
city: './data/city.csv'
# - First parameters is the file used to store states so that commits can be
# rolledback.
# - Second parameter is the set of files that need to be in sync
# - At the time of object creatation any previous uncomiited work will be
# rolled back.
sync = new SyncedAppend './data/rollback.log', files
# starts the sync for appends. This creates the rollback log
sync.start()
countryWriter = sync.getFile 'country'
cityWriter = sync.getFile 'city'
# append strigns to files
countryWriter.append 'code,name\n'
countryWriter.append 'LKA,Sri Lanka\n'
cityWriter.append 'id,name,countryCode\n'
cityWriter.append '0,Colombo,LKA\n'
cityWriter.append '1,Kandy,LKA\n'
# Commit above appends to files. This doesn't leave corrupted states in case of
# crash
sync.syncStop()
newFiles =
language:
path: './data/language.csv'
encoding: 'utf8'
# Since sync was stopped previously can add new files when starting again
sync.start newFiles
langWriter = sync.getFile 'language'
countryWriter.append 'USA,United States of America\n'
langWriter.append 'countryCode,lanague\n'
langWriter.append 'USA,English\n'
# Commits above changes and auto start.
# This is euqauvlent to sync.syncStop and sync.start()
sync.sync()
# Run this program multiple times. Every time it runs it writes to a
# new log file.
SyncedAppend = require 'synced-append'
# recovers last written file
sync = new SyncedAppend "./data/example2_rollback.log"
logWriter = sync.getFile 'log'
if logWriter?
console.log "Recovered the file at #{logWriter.getPath()}"
# Every time we write to a new file
files =
log: "./data/log-#{(new Date).getTime()}.txt"
# starting write to the new file
sync.start files
if not logWriter?
logWriter = sync.getFile 'log'
logWriter.append "File written at #{(new Date).getTime()}"
# Let's commit the append
sync.syncStop()
# File paths can be changed on the file when sync is stopped
files.log += ".more"
sync.start files
# Try removing these two lines. If no bytes are committed to a file
# the file will be removed when recovered.
logWriter.append 'first line\n'
sync.sync()
# Force stop the program with uncommitted appends
exit = (code) ->
console.log """
exit #{code}\n
Process is force stopped. There can be uncommited appends in the file
#{logWriter.getPath()}
"""
process.exit code
setTimeout exit, 100, 1
# Append the file without commiting so that file left with uncommited
# appends when the program exits
MAXN = 1<<20
next = ->
# Make sure appends are large enough to fill the file buffer so that
# actual file write will happen.
for i in [0...MAXN]
logWriter.append "#{i}"
setTimeout next, 0
next()
MIT
FAQs
Append text files without leaving corrupted states between process crashes. A set of files can be grouped together so that appends to all those files can be committed at once making sure files are in sync.
The npm package synced-append receives a total of 6 weekly downloads. As such, synced-append popularity was classified as not popular.
We found that synced-append demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.