{"id":17398441,"url":"https://github.com/vweevers/fs-maybe-open","last_synced_at":"2025-03-29T20:15:39.953Z","repository":{"id":55828465,"uuid":"76775426","full_name":"vweevers/fs-maybe-open","owner":"vweevers","description":"Open a file unless it's already a file descriptor.","archived":false,"fork":false,"pushed_at":"2020-12-11T17:46:40.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T21:39:05.679Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vweevers.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-18T10:02:40.000Z","updated_at":"2016-12-18T10:43:57.000Z","dependencies_parsed_at":"2022-08-15T07:31:19.811Z","dependency_job_id":null,"html_url":"https://github.com/vweevers/fs-maybe-open","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vweevers%2Ffs-maybe-open","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vweevers%2Ffs-maybe-open/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vweevers%2Ffs-maybe-open/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vweevers%2Ffs-maybe-open/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vweevers","download_url":"https://codeload.github.com/vweevers/fs-maybe-open/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246237436,"owners_count":20745348,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-16T14:56:30.485Z","updated_at":"2025-03-29T20:15:39.923Z","avatar_url":"https://github.com/vweevers.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fs-maybe-open\n\n**Open a file unless it's already a file descriptor.**\n\n[![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\u0026label=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\u0026label=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)\n\n## usage\n\n```js\nconst maybeOpen = require('fs-maybe-open')\n    , fs = require('fs')\n\nfunction readExactly(fdOrFile, pos, length, done) {\n  maybeOpen(fdOrFile, 'r', function (err, fd, maybeClose) {\n    if (err) return done(err)\n\n    fs.read(fd, Buffer(length), 0, length, pos, function (err, bytesRead, buf) {\n      if (err) return maybeClose(done, err)\n\n      if (bytesRead !== length) {\n        return maybeClose(done, new Error('End of file'))\n      }\n\n      maybeClose(done, null, buf)\n    })\n  })\n}\n```\n\nThe `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:\n\n- If `path` is a file descriptor, opening is a noop\n- 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).\n\n## install\n\nWith [npm](https://npmjs.org) do:\n\n```\nnpm install fs-maybe-open\n```\n\n## license\n\n[MIT](http://opensource.org/licenses/MIT) © Vincent Weevers\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvweevers%2Ffs-maybe-open","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvweevers%2Ffs-maybe-open","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvweevers%2Ffs-maybe-open/lists"}