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 🙏

© 2026 – Pkg Stats / Ryan Hefner

access-gate

v0.1.3

Published

A powerful and flexible role-based access control (RBAC) library for modern JavaScript and TypeScript applications. Supports guards, policies, and advanced async capabilities for granular access control.

Readme

access-gate

A powerful and flexible role-based access control (RBAC) library for modern JavaScript and TypeScript applications.

Key Features

  • Policies: Define granular access control rules.
  • Guards: Apply global or contextual restrictions.
  • Async Guards: Handle asynchronous operations seamlessly.
  • Lightweight: Minimal dependencies, fast performance.
  • TypeScript Support: Fully typed API for improved developer experience.

For full Documentation.

Installation

Install the package via npm or yarn:

npm install access-gate
# or
yarn add access-gate

1. Import the Library

import { Gate, Policy } from "access-gate";

const gate = new Gate();

// Define a policy
const postPolicy = new Policy("post");

postPolicy.define("update", (user, post) => user.id === post.authorId);

gate.addPolicy(postPolicy);

// Access the policy
const representative = gate.build({ id: 1 });
const decision = representative.access("post", "update").can({ authorId: 1 });

console.log(decision); // true

Core Concepts

Policies

A Policy represents a set of actions that define access rules.

const postPolicy = new Policy("post");

postPolicy.define("view", (user, post) => true);
postPolicy.define("update", (user, post) => user.id === post.authorId);

gate.addPolicy(postPolicy);

Guards

Guards define global or contextual access rules.

gate.guard((user) => user.isAdmin);

Lazy Guards

Lazy guards are entity-specific restrictions.

gate.lazyGuard((user, post) => user.id === post.authorId);

Decisions

A Decision object represents the result of evaluating a policy or guard.

const decision = representative.access("post", "view");
console.log(decision.can()); // true

API Reference

Gate

The Gate class manages policies and guards.

Methods

  • addPolicy(policy: Policy): Adds a policy.
  • guard(fn: Guard): Adds a global guard.
  • lazyGuard(fn: LazyGuard): Adds a lazy guard.

Policy

The Policy class defines a set of rules.

Methods

  • define(action: string, fn: (user, entity) => boolean): Defines a policy action.
  • can(action: string): Evaluates if an action can be performed.

Representative

The Representative class evaluates access based on policies and guards.

Methods

  • access(policy: string, action: string): Accesses a policy decision.
  • can(entity?: object): Evaluates synchronous access.
  • could(entity?: object): Evaluates async access.

Advanced Usage

Async Guards

gate.asyncLazyGuard(async (user, resource) => {
  const hasAccess = await someAsyncCheck(user, resource);
  return hasAccess;
});

Dependency Injection

// provide
gate.guard((user, provide) => {
  const role = getRole(user);

  provide("role", role);
});

// inject
policy.define("update", (user, entity) => {
    const role = this.inject("role");

    return role === "user" && user.id === entity.authorId;
});