# fs-maybe-open

> Open a file unless already an fd

Latest version **1.0.0** (published 2016-12-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install fs-maybe-open
pnpm add fs-maybe-open
yarn add fs-maybe-open
bun add fs-maybe-open
```

## 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.0 |
| Published | 2016-12-18 |
| First published | 2016-12-18 |
| 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 | fd, fs, open |

## Links

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

## Dependencies (1)

- [fs-lotus](https://npm.io/package/fs-lotus.md) ~1.0.1

## 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.0 (latest) — 2016-12-18

## README

# fs-maybe-open

**Open a file unless it's already a file descriptor.**

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

## usage

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

function readExactly(fdOrFile, pos, length, done) {
  maybeOpen(fdOrFile, 'r', function (err, fd, maybeClose) {
    if (err) return done(err)

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

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

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

The `maybeOpen` 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:

- If `path` is a file descriptor, opening is a noop
- The open `callback` also receives a `maybeClose(callback, err, ...args)` function, which calls `fs.close` for you if `path` was a filename. An error from `fs.close` (if any) will be [combined](https://github.com/matthewmueller/combine-errors) with your error (if any).

## install

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

```
npm install fs-maybe-open
```

## license

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

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