0.0.6 • Published 5 years ago

fs-lockfile v0.0.6

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

fs-lockfile

Managing the reading and writing of files in the application.

Only one action can manipulate file in the mean time.

No Dependencies.

Installation

npm i --save fs-lockfile

Method of fs-lockfile

  • read(filePath): Promise

  • write(filePath, content): Promise

  • obtainReadLock(filePath): Object

  • obtainWriteLock(filePath): Object

  • releaseReadLock(lockMeta): undefined

  • releaseWriteLock(lockMeta): undefined

Usage

Example:

const path = require('path')
const { read, write } = require('fs-lockfile')

async function demo(filePath) {

    await write(filePath, 'test data')

    const content = await read(filePath)

    console.log('File content:', content.toString())
}
 
const filePath = path.join(__dirname, './test.txt')
demo(filePath)

Another example:

async function read(fp) {
    if (!path.isAbsolute(fp)) throw new Error('Must be an absolute path.')

    const lockMeta = await obtainReadLock(fp)

    try {
        return await fsp.readFile(fp)
    } catch (readError) {
        throw readError
    } finally {
        await releaseReadLock(lockMeta)
    }
}

LISENCE

MIT

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago