# fs-lockfile

> Managing the reading and writing of files in the application.

Latest version **0.0.6** (published 2019-10-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install fs-lockfile
pnpm add fs-lockfile
yarn add fs-lockfile
bun add fs-lockfile
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2019-10-15 |
| First published | 2019-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Yantze |
| Maintainers | yantze |
| Keywords | fs, lock, mutex, javascript, nodejs |

## Links

- npm: https://www.npmjs.com/package/fs-lockfile
- Repository: https://github.com/yantze/fs-lockfile
- Homepage: https://github.com/yantze/fs-lockfile#readme
- Issues: https://github.com/yantze/fs-lockfile/issues
- npm.io page: https://npm.io/package/fs-lockfile

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 0.0.6 (latest) — 2019-10-15
- 0.0.5 — 2019-04-09
- 0.0.4 — 2019-04-03
- 0.0.3 — 2019-04-03
- 0.0.2 — 2019-04-03
- 0.0.1 — 2019-04-03

## README

# 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:**
```js
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:
```js
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

---
_Source: https://npm.io/package/fs-lockfile · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
