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

@simpleclub/firebase-rules-generator

v1.0.0

Published

Helper package for building more complex Firebase rules with support for imports.

Downloads

20

Readme

image


Firebase rules generator Cloud Firestore (4- Icon, Use in Firebase Contexts Only) Cloud Storage for Firebase (4- Icon, Use in Firebase Contexts Only)

In 2020, we faced with the increasing need for splitting rules across different files to improve the readability of our security rules.
At that point we wrote a helper script to split rules across files and in a build step combine them into one file.

You can read our article about that here: Imports for Firestore Security rules

Now, we've abstracted it into a package and generate sourcemaps that can be used to match the built-file back to the source files.

See also Firebase rules coverage if you want to generate a test coverage report of your unit tests.


Installation

The @simpleclub/firebase-rules-generator package is available on NPM (also GitHub Packages):

$ npm install --save @simpleclub/firebase-rules-generator

Via Yarn:

$ yarn add @simpleclub/firebase-rules-generator

Usage

When installing the package the command firebase-build-rules will be added to your project.

You can use this command in the package.json-scripts or by executing ./node_modules/.bin/firebase-build-rules.

The command takes an index rules files and may contain imports to other rules files:

$ firebase-build-rules <input>

Options

You can configure the behaviour of the command with the following options:

| Option | Description | | ---------------- | ------------------------------------------------------- | | --output, -o | Where the final rules file should be saved. | | --source-map, -s | Whether to generate source maps or not (default: true). |

Examples

$ firebase-build-rules rules/index.rules --output firestore.rules
$ firebase-build-rules rules/index.rules --no-source-map --output storage.rules

Import syntax

To get the most out of this package you need to understand how the imports work.

Let's take a simple example:

index.rules

rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {
    include "matcher.rules";
  }
}

matcher.rules

match /articles/{article} {
  allow read: if request.auth != null;
  allow create, update: if request.auth.token.admin == true;
}

In this example we would use a command like this to generate the final rules file:

$ firebase-build-rules index.rules -o firestore.rules 

Because we have this include "matcher.rules"; statement in there the rules-generator will pick this up and include the contents from matcher.rules at the exact same position as the include-statement.

The final firestore.rules file will then look like:

rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {
match /articles/{article} {
  allow read: if request.auth != null;
  allow create, update: if request.auth.token.admin == true;
}
  }
}

Indentation will not be perfect, but that does not matter for the Firebase rules engine.

Include vs import

Why is it called include and not import?

It's a relatively straight forward answer:
Because the position where the include-statement is placed matters a lot in the built rules file. This behavior is vastly different from imports you know from other programming languages where the order of imports does not matter too much.