Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
The stat-mode npm package is used to interpret and manipulate the file mode (permissions) of a file system's stat object. It provides a simple interface to read and modify the permissions of files and directories.
Reading File Permissions
This feature allows you to read the permissions of a file. The code sample demonstrates how to use the stat-mode package to check if the owner can read, the group can write, and others can execute the file 'example.txt'.
const fs = require('fs');
const Mode = require('stat-mode');
fs.stat('example.txt', (err, stats) => {
if (err) throw err;
const mode = new Mode(stats);
console.log(`Owner can read: ${mode.owner.read}`);
console.log(`Group can write: ${mode.group.write}`);
console.log(`Others can execute: ${mode.others.execute}`);
});
Modifying File Permissions
This feature allows you to modify the permissions of a file. The code sample shows how to use the stat-mode package to set the read permission for the owner, write permission for the group, and execute permission for others on the file 'example.txt'.
const fs = require('fs');
const Mode = require('stat-mode');
fs.stat('example.txt', (err, stats) => {
if (err) throw err;
const mode = new Mode(stats);
mode.owner.read = true;
mode.group.write = true;
mode.others.execute = true;
fs.chmod('example.txt', mode.stat.mode, (err) => {
if (err) throw err;
console.log('Permissions updated');
});
});
fs-extra is a package that extends the native Node.js fs module with additional methods and promises. It includes methods for reading and writing file permissions, similar to stat-mode, but also provides a broader range of file system operations such as copying, moving, and removing files and directories.
chmodr is a package specifically designed for recursively changing file permissions. While stat-mode focuses on interpreting and modifying file modes, chmodr provides a simple interface for changing permissions of entire directory trees, making it more suitable for bulk permission changes.
mode
You know that mode
property on the fs.Stat
object that you probably
usually just ignore? Well there's acutally a lot of information packed
into that number.
The specific information includes:
setuid
and setgid
bits are setsticky
bit is setThis module helps you extract that information.
All the getters are also setters, which change the mode
property
appropriately. This is useful for when you have to build up your
own fs.Stat
object for whatever reason (like when implementing a
FUSE filesystem.
$ npm install stat-mode
So given some arbitrary file (let's say /bin/echo
):
$ ls -l /bin/echo
-rwxr-xr-x 1 root wheel 14128 Aug 11 2013 /bin/echo
We can inspect it using the fs.stat()
call and creating a Mode
instance
on top of it.
var fs = require('fs');
var Mode = require('stat-mode');
fs.stat('/bin/echo', function (err, stat) {
if (err) throw err;
// create a "Mode" instance on top of the `stat` object
var mode = new Mode(stat);
// you can check what kind of file it is:
mode.isDirectory();
// false
mode.isFIFO();
// false
mode.isFile();
// true
// and you can also check individual owner, group and others permissions
mode.owner.read;
// true
mode.owner.write;
// true
mode.owner.execute;
// true
mode.group.read;
// true
mode.group.write;
// false
mode.group.execute;
// true
mode.others.read;
// true
mode.others.write;
// false
mode.others.execute;
// true
// the `toString()` output resembes the `ls -l` output:
mode.toString();
// '-rwxr-xr-x'
});
You must pass in "stat" object to the Mode
constructor. The "stat"
object can be a real fs.Stat
instance, or really any Object with a
mode
property.
Returns true
if the mode's file type is "directory", false
otherwise.
If you pass true
to the function, then the mode will be set to "directory".
Returns true
if the mode's file type is "file", false
otherwise.
If you pass true
to the function, then the mode will be set to "file".
Returns true
if the mode's file type is "block device", false
otherwise.
If you pass true
to the function, then the mode will be set to "block device".
Returns true
if the mode's file type is "character device", false
otherwise.
If you pass true
to the function, then the mode will be set to "character
device".
Returns true
if the mode's file type is "symbolic link", false
otherwise.
If you pass true
to the function, then the mode will be set to "symbolic link".
Returns true
if the mode's file type is "FIFO", false
otherwise.
If you pass true
to the function, then the mode will be set to "FIFO".
Returns true
if the mode's file type is "socket", false
otherwise.
If you pass true
to the function, then the mode will be set to "socket".
true
if the mode is "owner read" rights, false
otherwise.
true
if the mode is "owner write" rights, false
otherwise.
true
if the mode is "owner execute" rights, false
otherwise.
true
if the mode is "group read" rights, false
otherwise.
true
if the mode is "group write" rights, false
otherwise.
true
if the mode is "group execute" rights, false
otherwise.
true
if the mode is "others read" rights, false
otherwise.
true
if the mode is "others write" rights, false
otherwise.
true
if the mode is "others execute" rights, false
otherwise.
FAQs
Offers convenient getters and setters for the stat `mode`
The npm package stat-mode receives a total of 816,450 weekly downloads. As such, stat-mode popularity was classified as popular.
We found that stat-mode demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.