• Stars
    star
    119
  • Rank 288,650 (Top 6 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Swiss Army knife for Unix permissions
unix-permissions logo

Node Browsers TypeScript Codecov Minified size Mastodon Medium

Swiss Army knife for Unix permissions.

Unix file permissions can take many shapes: symbolic (ug+rw), octal (660) or a list of characters (drw-rw----). This library enables using any of these (instead of being limited to a single one) with any Node.js or CLI command.

This library can also perform operations on Unix permissions such as:

  • testing, setting and unsetting. Using bitwise operations (|, &, ^, ~) can be tedious and error-prone otherwise.
  • validating syntax.
  • normalizing. For example u+r,u+w can be shortened to u+rw.
  • inverting. For example a umask of 117 means new files will be created with 661 permissions.
  • checking the minimal or maximal permissions among a list of them. This can be useful to aggregate all the permissions of several files, e.g. during a directory recursion.

Permissions are manipulated as strings, not as file paths. This means you must use other utilities (such as chmod or stat) to get and set file permissions using those strings.

Examples

In JavaScript:

import { convert } from 'unix-permissions'

// Retrieve a file's permission as an object like
// `{ user: { write: false, read: true, ... }, ... }` instead of a number
convert.object(fs.statSync('/etc/passwd').mode)

// Set a file's permission using `symbolic` notation instead of a number
fs.chmod('/etc/passwd', convert.number('a=r'))

// Set a file's permission using `symbolic` notation instead of a number
fs.writeFile('/my/file', content, { mode: convert.number('a=r') })

// Disallow executing new files using `umask`
process.umask(convert.number(invert('a-x')))

// If your library takes Unix permissions as input, using
// `unix-permissions` under the hood lets your users choose their
// favorite Unix permissions type.
myLibrary.method({ mode: 'a-wx' })
myLibrary.method({ mode: '444' })

On the command line:

$ stat -c "%a" /etc/passwd
644

$ unix-permissions convert.symbolic "$(stat -c "%a" /etc/passwd)"
u=rw,go=r

Demo

You can try this library:

Install

npm install unix-permissions

This package works in both Node.js >=16.17.0 and browsers.

This is an ES module. It must be loaded using an import or import() statement, not require(). If TypeScript is used, it must be configured to output ES modules, not CommonJS.

Usage (JavaScript)

import { convert } from 'unix-permissions'

// `permission` will be set to `rw-rw----`
const permission = convert.stat('660')

Several methods other than convert are available but they mostly follow the same pattern. Permission strings are passed as input and returned as output.

Usage (CLI)

$ unix-permissions convert.stat 660
rw-rw----

The same methods as in JavaScript are available. Exit code will be 1 if an error occurred, e.g. if the permission syntax is invalid.

Permission types

You can use any of the following permission types as input. You can also convert() between them:

  • octal strings like "422"
  • decimal number like 274
  • stat like rw-rw-r--
  • symbolic like a+rw
  • object like { user: { read: true, write: false, execute: false }, group: { write: false }, others: { write: false } }

Special permissions (setuid, setgid, sticky) can be used.

Please see the types full documentation.

Methods

convert.octal|number|stat|symbolic|object(permission)

Converts permission to another type.
Full documentation.

type(permission)

Returns the permission's type or invalid.
Full documentation.

normalize(permission)

Normalizes a permission to its canonical shape. Throw if permission is invalid.
Full documentation.

positive(permission)

Removes all negative permissions.
Full documentation.

contain(permission, permissions...)

Tests whether permission includes permissions.
Full documentation.

equal(permission, permissions...)

Tests whether permission equals exactly permissions.
Full documentation.

set(permission, permissions...)

Sets permissions on permission. This is useful to avoid error-prone bitwise operations (|, &, ^, ~).
Full documentation.

not(permission)

Inverts permission including special permissions. This can be used in combination with set() to unset permissions instead of setting them.
Full documentation.

invert(permission)

Inverts permission and removes special permissions.
Full documentation.

min(permissions...)

Retrieves the lowest permissions among all arguments.
Full documentation.

max(permissions...)

Retrieves the highest permissions among all arguments.
Full documentation.

Support

For any question, don't hesitate to submit an issue on GitHub.

Everyone is welcome regardless of personal background. We enforce a Code of conduct in order to promote a positive and inclusive environment.

Contributing

This project was made with ❤️. The simplest way to give back is by starring and sharing it online.

If the documentation is unclear or has a typo, please click on the page's Edit button (pencil icon) and suggest a correction.

If you would like to help us fix a bug or add a new feature, please check our guidelines. Pull requests are welcome!

More Repositories

1

cross-platform-node-guide

📗 How to write cross-platform Node.js code
JavaScript
1,331
star
2

modern-errors

Handle errors in a simple, stable, consistent way
JavaScript
1,273
star
3

nve

Run any command on specific Node.js versions
JavaScript
614
star
4

wild-wild-path

🤠 Object property paths with wildcards and regexps 🌵
JavaScript
608
star
5

log-process-errors

Show some ❤️ to Node.js process errors
JavaScript
471
star
6

human-signals

Human-friendly process signals
JavaScript
261
star
7

autoserver

Create a full-featured REST/GraphQL API from a configuration file
JavaScript
200
star
8

safe-json-value

⛑️ JSON serialization should never fail
JavaScript
198
star
9

cross-platform-terminal-characters

All the characters that work on most terminals
JavaScript
196
star
10

test-each

🤖 Repeat tests. Repeat tests. Repeat tests.
JavaScript
103
star
11

Notes

Technologies I've learned
sed
58
star
12

gulp-execa

Gulp.js command execution for humans
JavaScript
55
star
13

fast-cartesian

Fast cartesian product
TypeScript
53
star
14

nvexeca

nvm + execa = nvexeca
JavaScript
34
star
15

get-bin-path

Get the current package's binary path
JavaScript
34
star
16

wild-wild-utils

🤠 Functional utilities using object property paths with wildcards and regexps 🌵
JavaScript
28
star
17

get-node

Download a specific version of Node.js
JavaScript
19
star
18

handle-cli-error

💣 Error handler for CLI applications 💥
JavaScript
15
star
19

keep-func-props

Wrap a function without changing its name and other properties
JavaScript
13
star
20

node-version-alias

Resolve Node.js version aliases like `latest`, `lts` or `erbium`
JavaScript
12
star
21

big-cartesian

Cartesian product for big inputs
TypeScript
12
star
22

eslint-config

ESLint configuration for my own projects
JavaScript
10
star
23

preferred-node-version

Get the preferred Node.js version of a project or user
JavaScript
9
star
24

all-node-versions

List all available Node.js versions
JavaScript
9
star
25

error-serializer

Convert errors to/from plain objects.
JavaScript
9
star
26

normalize-node-version

Normalize and validate Node.js versions
JavaScript
8
star
27

cv-website

Static page with my CV website
HTML
8
star
28

modern-errors-http

`modern-errors` plugin to create HTTP error responses.
TypeScript
8
star
29

truncate-json

Truncate a JSON string.
JavaScript
8
star
30

merge-error-cause

Merge an error with its `cause`
JavaScript
7
star
31

get-node-cli

Download a specific version of Node.js (CLI)
JavaScript
7
star
32

global-cache-dir

Get the global cache directory
JavaScript
6
star
33

dev-tasks

Automated development tasks for my own projects
Shell
5
star
34

portuguese-conjugation-cheat-sheet

Portuguese conjugation cheat sheet
CSS
5
star
35

modern-errors-serialize

`modern-errors` plugin to serialize/parse errors.
JavaScript
5
star
36

spyd

Complete yet simple benchmark runner
JavaScript
5
star
37

string-byte-length

Get the UTF-8 byte length of a string.
JavaScript
5
star
38

template-javascript

JavaScript library template
JavaScript
5
star
39

colors-option

Let users toggle colors
JavaScript
5
star
40

modern-errors-winston

`modern-errors` plugin for Winston.
JavaScript
5
star
41

normalize-exception

Normalize exceptions/errors
JavaScript
5
star
42

abstract-parser

Abstraction layer for JavaScript parsers
JavaScript
4
star
43

modern-errors-cli

`modern-errors` plugin to handle errors in CLI modules.
TypeScript
4
star
44

error-custom-class

Create custom error classes
JavaScript
4
star
45

fetch-node-website

Fetch releases on nodejs.org
JavaScript
4
star
46

winston-error-format

Log errors with Winston
JavaScript
4
star
47

is-json-value

Check if a value is valid JSON.
JavaScript
4
star
48

error-cause-polyfill

Polyfill `error.cause`
JavaScript
4
star
49

modern-errors-switch

`modern-errors` plugin to execute class-specific logic.
JavaScript
4
star
50

modern-errors-clean

`modern-errors` plugin to clean stack traces.
JavaScript
4
star
51

wild-wild-parser

🤠 Parser for object property paths with wildcards and regexps 🌵
JavaScript
4
star
52

precise-now

Like `performance.now()` but in nanoseconds
TypeScript
4
star
53

error-http-response

Create HTTP error responses.
JavaScript
3
star
54

test-api

[WIP] Automated API testing
JavaScript
3
star
55

modern-errors-process

`modern-errors` plugin to handle process errors.
JavaScript
3
star
56

guess-json-indent

Guess the indentation of a JSON string.
JavaScript
3
star
57

time-resolution

Find the process's time resolution
TypeScript
3
star
58

is-error-instance

Check if a value is an `Error` instance.
TypeScript
3
star
59

declarative-merge

Merge objects/arrays declaratively
JavaScript
3
star
60

template-typescript

TypeScript library template
TypeScript
3
star
61

modern-errors-bugs

`modern-errors` plugin to print where to report bugs.
JavaScript
3
star
62

set-error-class

Properly update an error's class.
JavaScript
2
star
63

dev-parser

Parse JavaScript using a terminal
JavaScript
2
star
64

ehmicky

Node.js back-end developer
2
star
65

set-error-stack

Properly update an error's stack.
JavaScript
2
star
66

set-array

Set array items declaratively
JavaScript
2
star
67

set-error-message

Properly update an error's message.
JavaScript
2
star
68

set-error-props

Properly update an error's properties
JavaScript
2
star
69

string-byte-slice

Like `string.slice()` but bytewise.
JavaScript
2
star
70

redefine-property

Better `Object.defineProperty()`
JavaScript
2
star
71

terminal-theme

🎨 Use a color theme for your code's terminal output
JavaScript
2
star
72

chalk-string

Chalk with style strings.
JavaScript
2
star
73

wrap-error-message

Properly wrap an error's message.
JavaScript
2
star
74

error-class-utils

Properly create error classes.
JavaScript
2
star
75

create-error-types

Create multiple error types.
JavaScript
1
star
76

oh-oh

This is an.
JavaScript
1
star
77

design

Logos of my projects
1
star