# fs-lotus

> Sugar to open and close a file descriptor

Latest version **1.0.1** (published 2016-12-11) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2016-12-11 |
| First published | 2016-12-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4.0.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Vincent Weevers |
| Maintainers | vweevers |
| Keywords | close, fd, fs, open |

## Links

- npm: https://www.npmjs.com/package/fs-lotus
- Repository: https://github.com/vweevers/fs-lotus
- Issues: https://github.com/vweevers/fs-lotus/issues
- npm.io page: https://npm.io/package/fs-lotus

## Dependencies (1)

- [combine-errors](https://npm.io/package/combine-errors.md) ~3.0.3

## 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

- 1.0.1 (latest) — 2016-12-11

## README

# fs-lotus

**Sugar to open and close a file descriptor.**

[![npm status](http://img.shields.io/npm/v/fs-lotus.svg?style=flat-square)](https://www.npmjs.org/package/fs-lotus) [![node](https://img.shields.io/node/v/fs-lotus.svg?style=flat-square)](https://www.npmjs.org/package/fs-lotus) [![Travis build status](https://img.shields.io/travis/vweevers/fs-lotus.svg?style=flat-square&label=travis)](http://travis-ci.org/vweevers/fs-lotus) [![AppVeyor build status](https://img.shields.io/appveyor/ci/vweevers/fs-lotus.svg?style=flat-square&label=appveyor)](https://ci.appveyor.com/project/vweevers/fs-lotus) [![Dependency status](https://img.shields.io/david/vweevers/fs-lotus.svg?style=flat-square)](https://david-dm.org/vweevers/fs-lotus)

## usage

```js
const open = require('fs-lotus')
    , fs = require('fs')

const readExactly = function (filename, pos, length, done) {
  open(filename, 'r', function (err, fd, close) {
    if (err) return done(err)

    fs.read(fd, Buffer(length), 0, length, pos, function (err, bytesRead, buf) {
      if (err) return close(done, err)

      if (bytesRead !== length) {
        return close(done, new Error('End of file'))
      }

      close(done, null, buf)
    })
  })
}
```

The `open` function has the same signature as [`fs.open(path, flags[, mode], callback)`](https://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback). Except that `callback` also receives a `close(callback, err, ...args)` function, which calls `fs.close` for you. An error from `fs.close` (if any) will be [combined](https://github.com/matthewmueller/combine-errors) with your error (if any).

This pattern ensures that our `readExactly` function doesn't leak fd's.

```js
readExactly('readme.md', 0, 10, function (err, buf) {
  console.log(buf.toString()) // '# fs-lotus'
})

readExactly('readme.md', 1e5, 10, function (err, buf) {
  console.log(err.toString()) // 'Error: End of file'
})
```

## install

With [npm](https://npmjs.org) do:

```
npm install fs-lotus
```

## license

[MIT](http://opensource.org/licenses/MIT) © Vincent Weevers

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