fs-maybe-open

2019-02-13 admin

fs-maybe-open是什么

什么是fs-maybe-open,Open a file unless already an fd

fs-maybe-open使用教程帮助文档

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

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

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

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

文章标题:fs-maybe-open

回到顶部