bitperm
v1.1.0
Published
Lightweight bitwise permission manager for Node.js and Express. Simple, fast, and fully typed.
Downloads
13
Maintainers
Readme
What is Bitperm?
Bitperm is a lightweight and efficient permission management library based on bitwise operations. It allows you to assign and check user permissions using integers, making it extremely fast, compact, and easy to integrate into any Node.js or Express project.
Features
🧩 Bitmask-based permission system
⚡ Super-fast checks using bitwise operations
🧠 Full TypeScript support
🔐 Express middleware for route protection
🪶 Minimal and dependency-free
Installation
npm install bitpermUsage
- Define your permissions
import { BitPerm } from "bitperm";
export const PERMS = {
COMMENT_VIEW: 1 << 1, // 2
COMMENT_CREATE: 1 << 2, // 4
COMMENT_DELETE: 1 << 3, // 8
};- Combine permissions for a user
let userPermissions = BitPerm.combine(PERMS.COMMENT_VIEW);
// userPermissions = 2- Check permissions
BitPerm.has(userPermissions, PERMS.COMMENT_VIEW); // true- Add or remove permissions
userPermissions = BitPerm.add(userPermissions, PERMS.COMMENT_DELETE);
userPermissions = BitPerm.remove(userPermissions, PERMS.COMMENT_CREATE);- List all permissions
BitPerm.list(userPermissions, PERMS);
// Returns: ['COMMENT_VIEW', 'COMMENT_DELETE']Express Middleware
Protect routes with required permissions.
import express from "express";
import { BitPerm, requirePermission } from "bitperm";
const app = express();
const PERMS = {
COMMENT_VIEW: 1 << 1,
COMMENT_CREATE: 1 << 2,
COMMENT_DELETE: 1 << 3,
};
// Simulated auth middleware
app.use((req, res, next) => {
req.user = { id: 1, permission: BitPerm.combine(PERMS.COMMENT_VIEW, PERMS.COMMENT_CREATE) };
next();
});
app.get("/comments", requirePermission(PERMS.COMMENT_VIEW), (req, res) => {
res.json({ message: "You can view comments" });
});
app.post("/comments", requirePermission(PERMS.COMMENT_CREATE), (req, res) => {
res.json({ message: "You can create comments" });
});
app.delete("/comments", requirePermission(PERMS.COMMENT_DELETE), (req, res) => {
res.json({ message: "You can delete comments" });
});
app.listen(3000, () => console.log("Server running on port 3000"));Publishing & Maintenance
Increment versions with npm version patch|minor|major#
Build with npm run build
Publish with npm publish
