fs-lotus

2019-02-13 admin

fs-lotus是什么

什么是fs-lotus,Sugar to open and close a file descriptor

fs-lotus使用教程帮助文档

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

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-53665.html

文章标题:fs-lotus

回到顶部