├── .editorconfig ├── .gitattributes ├── .github └── security.md ├── .gitignore ├── .jshintrc ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "curly": true, 6 | "immed": true, 7 | "newcap": true, 8 | "noarg": true, 9 | "undef": true, 10 | "unused": "vars", 11 | "strict": true 12 | } 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - 'iojs' 5 | - '0.12' 6 | - '0.10' 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var fs = require('fs'); 3 | var nullCheck = require('null-check'); 4 | 5 | var access = module.exports = function (pth, mode, cb) { 6 | if (typeof pth !== 'string') { 7 | throw new TypeError('path must be a string'); 8 | } 9 | 10 | if (typeof mode === 'function') { 11 | cb = mode; 12 | mode = access.F_OK; 13 | } else if (typeof cb !== 'function') { 14 | throw new TypeError('callback must be a function'); 15 | } 16 | 17 | if (!nullCheck(pth, cb)) { 18 | return; 19 | } 20 | 21 | mode = mode | 0; 22 | 23 | if (mode === access.F_OK) { 24 | fs.stat(pth, cb); 25 | } 26 | }; 27 | 28 | access.sync = function (pth, mode) { 29 | nullCheck(pth); 30 | 31 | mode = mode === undefined ? access.F_OK : mode | 0; 32 | 33 | if (mode === access.F_OK) { 34 | fs.statSync(pth); 35 | } 36 | }; 37 | 38 | access.F_OK = 0; 39 | access.R_OK = 4; 40 | access.W_OK = 2; 41 | access.X_OK = 1; 42 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Sindre Sorhus (sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fs-access", 3 | "version": "2.0.0", 4 | "description": "Node.js 0.12 fs.access() & fs.accessSync() ponyfill", 5 | "license": "MIT", 6 | "repository": "sindresorhus/fs-access", 7 | "author": { 8 | "name": "Sindre Sorhus", 9 | "email": "sindresorhus@gmail.com", 10 | "url": "sindresorhus.com" 11 | }, 12 | "engines": { 13 | "node": ">=0.10.0" 14 | }, 15 | "scripts": { 16 | "test": "node test.js" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "built-in", 23 | "core", 24 | "ponyfill", 25 | "polyfill", 26 | "shim", 27 | "fs", 28 | "access", 29 | "stat", 30 | "mode", 31 | "permission", 32 | "user", 33 | "process", 34 | "check" 35 | ], 36 | "dependencies": { 37 | "null-check": "^1.0.0" 38 | }, 39 | "devDependencies": { 40 | "os-tmpdir": "^1.0.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Deprecated 2 | 3 | This package is no longer relevant as Node.js 0.12 is unmaintained. 4 | 5 | --- 6 | 7 | # fs-access [![Build Status](https://travis-ci.org/sindresorhus/fs-access.svg?branch=master)](https://travis-ci.org/sindresorhus/fs-access) 8 | 9 | > Node.js 0.12 [`fs.access()`](https://nodejs.org/api/fs.html#fs_fs_access_path_mode_callback) & [`fs.accessSync()`](https://nodejs.org/api/fs.html#fs_fs_accesssync_path_mode) [ponyfill](https://ponyfill.com) 10 | 11 | 12 | ## Install 13 | 14 | ``` 15 | $ npm install --save fs-access 16 | ``` 17 | 18 | 19 | ## Usage 20 | 21 | ```js 22 | var fsAccess = require('fs-access'); 23 | 24 | fsAccess('unicorn.txt', function (err) { 25 | if (err) { 26 | console.error('no access'); 27 | return; 28 | } 29 | 30 | console.log('access'); 31 | }); 32 | ``` 33 | 34 | ```js 35 | var fsAccess = require('fs-access'); 36 | 37 | try { 38 | fsAccess.sync('unicorn.txt'); 39 | console.log('access'); 40 | } catch (err) { 41 | console.error('no access'); 42 | } 43 | ``` 44 | 45 | 46 | ## API 47 | 48 | See the [`fs.access()` & `fs.accessSync()` docs](https://nodejs.org/api/fs.html#fs_fs_access_path_mode_callback). 49 | 50 | Mode flags are on the `fsAccess` instance instead of `fs`. Only the `F_OK` mode is supported. 51 | 52 | 53 | ## License 54 | 55 | MIT © [Sindre Sorhus](http://sindresorhus.com) 56 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var fs = require('fs'); 3 | var path = require('path'); 4 | var osTmpdir = require('os-tmpdir'); 5 | 6 | var fsAccess = require('./'); 7 | fs.access = fsAccess; 8 | fs.accessSync = fsAccess.sync; 9 | fs.F_OK = fs.access.F_OK; 10 | fs.R_OK = fs.access.R_OK; 11 | fs.W_OK = fs.access.W_OK; 12 | fs.X_OK = fs.access.X_OK; 13 | 14 | 15 | // https://github.com/iojs/io.js/blob/18d457bd3408557a48b453f13b2b99e1ab5e7159/test/parallel/test-fs-access.js 16 | var doesNotExist = __filename + '__this_should_not_exist'; 17 | var readOnlyFile = path.join(osTmpdir(), 'read_only_file'); 18 | var readWriteFile = path.join(osTmpdir(), 'read_write_file'); 19 | 20 | var removeFile = function (file) { 21 | try { 22 | fs.unlinkSync(file); 23 | } catch (err) {} 24 | }; 25 | 26 | var createFileWithPerms = function (file, mode) { 27 | removeFile(file); 28 | fs.writeFileSync(file, ''); 29 | fs.chmodSync(file, mode); 30 | }; 31 | 32 | createFileWithPerms(readOnlyFile, 0444); 33 | createFileWithPerms(readWriteFile, 0666); 34 | 35 | /* 36 | * On non-Windows supported platforms, fs.access(readOnlyFile, W_OK, ...) 37 | * always succeeds if node runs as the super user, which is sometimes the 38 | * case for tests running on our continuous testing platform agents. 39 | * 40 | * In this case, this test tries to change its process user id to a 41 | * non-superuser user so that the test that checks for write access to a 42 | * read-only file can be more meaningful. 43 | * 44 | * The change of user id is done after creating the fixtures files for the same 45 | * reason: the test may be run as the superuser within a directory in which 46 | * only the superuser can create files, and thus it may need superuser 47 | * priviledges to create them. 48 | * 49 | * There's not really any point in resetting the process' user id to 0 after 50 | * changing it to 'nobody', since in the case that the test runs without 51 | * superuser priviledge, it is not possible to change its process user id to 52 | * superuser. 53 | * 54 | * It can prevent the test from removing files created before the change of user 55 | * id, but that's fine. In this case, it is the responsability of the 56 | * continuous integration platform to take care of that. 57 | */ 58 | var hasWriteAccessForReadonlyFile = false; 59 | if (process.platform !== 'win32' && process.getuid() === 0) { 60 | hasWriteAccessForReadonlyFile = true; 61 | try { 62 | process.setuid('nobody'); 63 | hasWriteAccessForReadonlyFile = false; 64 | } catch (err) {} 65 | } 66 | 67 | assert(typeof fs.F_OK === 'number'); 68 | assert(typeof fs.R_OK === 'number'); 69 | assert(typeof fs.W_OK === 'number'); 70 | assert(typeof fs.X_OK === 'number'); 71 | 72 | fs.access(__filename, function (err) { 73 | assert.strictEqual(err, null, 'error should not exist'); 74 | }); 75 | 76 | fs.access(__filename, fs.R_OK, function (err) { 77 | assert.strictEqual(err, null, 'error should not exist'); 78 | }); 79 | 80 | fs.access(doesNotExist, function (err) { 81 | assert.notEqual(err, null, 'error should exist'); 82 | assert.strictEqual(err.code, 'ENOENT'); 83 | assert.strictEqual(err.path, doesNotExist); 84 | }); 85 | 86 | fs.access(readOnlyFile, fs.F_OK | fs.R_OK, function (err) { 87 | assert.strictEqual(err, null, 'error should not exist'); 88 | }); 89 | 90 | fs.access(readOnlyFile, fs.W_OK, function (err) { 91 | if (hasWriteAccessForReadonlyFile) { 92 | assert.equal(err, null, 'error should not exist'); 93 | } else { 94 | assert.notEqual(err, null, 'error should exist'); 95 | assert.strictEqual(err.path, readOnlyFile); 96 | } 97 | }); 98 | 99 | assert.throws(function () { 100 | fs.access(100, fs.F_OK, function (err) {}); 101 | }, /path must be a string/); 102 | 103 | assert.throws(function () { 104 | fs.access(__filename, fs.F_OK); 105 | }, /callback must be a function/); 106 | 107 | assert.throws(function () { 108 | fs.access(__filename, fs.F_OK, {}); 109 | }, /callback must be a function/); 110 | 111 | assert.doesNotThrow(function () { 112 | fs.accessSync(__filename); 113 | }); 114 | 115 | assert.doesNotThrow(function () { 116 | var mode = fs.F_OK | fs.R_OK | fs.W_OK; 117 | fs.accessSync(readWriteFile, mode); 118 | }); 119 | 120 | assert.throws(function () { 121 | fs.accessSync(doesNotExist); 122 | }, function (err) { 123 | return err.code === 'ENOENT' && err.path === doesNotExist; 124 | }); 125 | 126 | process.on('exit', function () { 127 | removeFile(readOnlyFile); 128 | removeFile(readWriteFile); 129 | }); 130 | --------------------------------------------------------------------------------