Socket
Socket
Sign inDemoInstall

fs-maybe-open

Package Overview
Dependencies
10
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    fs-maybe-open

Open a file unless already an fd


Version published
Weekly downloads
378
decreased by-31.4%
Maintainers
1
Install size
173 kB
Created
Weekly downloads
 

Readme

Source

fs-maybe-open

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

npm status node Travis build status AppVeyor build status Dependency status

usage

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). 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 with your error (if any).

install

With npm do:

npm install fs-maybe-open

license

MIT © Vincent Weevers

Keywords

FAQs

Last updated on 18 Dec 2016

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc