npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rbac-ts

v2.1.0

Published

Blazing Fast, Zero dependency, Hierarchical Role-Based Access Control for Node.js

Downloads

31

Readme

This is a typescript port of @rbac/rbac

npm version

  • ⏱ Lightweight
  • 🔥 Blazing Fast
  • ⚡️️ Zero dependency

Features

  • Focused on operations
  • Scalable
  • Each role is given specific access rights for every operation
  • High granularity in assigning rights

Thanks

Thanks to Karl Düüna (DeadAlready) and his awesome post on medium

Getting Started

Install

pnpm: pnpm add rbac-ts yarn: yarn add rbac-ts npm: npm i rbac-ts

RBAC is a curried function thats initially takes an object with configurations, then returns another function that takes an object with roles, finally returns an object that holds "can" property that is a function.

You can use it in many ways, below is one of them:

Setup RBAC config

step 01

| Property | Type | Params | Default | Description | |-------------- |--------------- |------------------------------------------------------------- |--------------- |----------------------------------------- | | logger | Function | role: Stringoperation: Stringresult: Boolean | defaultLogger | Function that logs operations to console | | enableLogger | Boolean | | true | Enable or disable logger |

Creating some roles

step 02

RBAC expects an object with roles as property names.

| Property | Type | Example | Description | |---------- |-------------- |------------------------------------------------ |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | can | Array | ['products:*'] | Array of strings, list of operations that user can do, since 1.1.0 also support glob | | when | Function or Promise | (params , done ) => done (null , true ) | Optional Promise that should resolve in Truthy or Falsy or Callback function that receives params and done as properties, should return done passing errors, and result | | inherits | Array | ['user'] | Optional Array of strings, list of roles inherited by this role |

IMPORTANT! "when" property should be either a Callback function that receives params and done or a Promise that should resolve in Truthy or Falsy values. Example:

const roles = {
  supervisor: {
    can: [{ name: 'products:find', when: (params, done) => {
      // done receives error as first argument and Truthy or Falsy value as second argument
      done(error, false);
    }}]
  },
  admin: {
    can: [{name: 'products:*', when: new Promise((resolve) => {
      resolve(true);
    })}]
  }
};

Check if user can do some operation

step 03

| Param | Type | Example | Description | |-------- |----------------------------------------------- |-------------------------- |---------------------------------------------------------------- | | First | String | 'admin' | Array of strings, list of operations that user can do | | Second | String, Glob (Wildcard), Regex | 'products:find' | Operation to validate | | Third | Any | {registered: true} | Optional Params that will flow to "when" callback Function |

Want more? Check out the examples folder.

Roadmap

  • [X] Wildcard support
  • [X] Regex support
  • [ ] Update roles in runtime

Contributing

Contributions are welcome!

  1. Build RBAC
  • Run yarn install to get RBAC's dependencies
  • Run yarn build to produce minified version of RBAC.
  1. Development mode
  • Having all the dependencies installed run yarn dev. This command will generate a non-minified version of your library and will run a watcher so you get the compilation on file change.
  1. Running the tests
  • Run yarn test
  1. Scripts
  • yarn build - produces production version of your library under the lib folder
  • yarn dev - produces development version of your library and runs a watcher
  • yarn test - well ... it runs the tests :)
  • yarn test:watch - same as above but in a watch mode

License

This project is under MIT License [https://opensource.org/licenses/MIT]