You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

fs-lotus

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fs-lotus

Sugar to open and close a file descriptor

1.0.1
latest
Source
npmnpm
Version published
Weekly downloads
1.5K
-0.46%
Maintainers
1
Weekly downloads
 
Created
Source

fs-lotus

Sugar to open and close a file descriptor.

npm status node Travis build status AppVeyor build status Dependency status

usage

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

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

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 do:

npm install fs-lotus

license

MIT © Vincent Weevers

Keywords

close

FAQs

Package last updated on 11 Dec 2016

Did you know?

Socket

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.