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

@timhall/opa

v0.1.1

Published

Open Policy Agent client with bundle loading

Downloads

6

Readme

@timhall/opa

@timhall/opa builds on @open-policy-agent/opa-wasm to add support for loading Open Policy Agent policy bundles and polling for changes.

Example

import { resolve } from "path";
import { OpaClient } from "@timhall/opa";

export const opa = new OpaClient({
  config: resolve(__dirname, "../config.yaml"),
});
await opa.load();

const input = {};
const result = opa.evaluate(input);

Building policies / bundles

Bundles are helpful for packaging a wasm policy with json data. Bundles must be generated as wasm with the -t wasm flag for the opa build command (run opa build --help for more information).

  • Top-level "example" entrypoint with policies directory: opa build -t wasm -e example ./policies
  • "example/allow" entrypoint with example.rego file: opa build -t wasm -e example/allow example.rego

To generate just a wasm policy, it can be extracted from the built bundle.

  • Extract policy.wasm from bundle: tar -xzf ./bundle.tar.gz /policy.wasm

API

new OpaClient(options)

Initialize a new OPA client for loading and evaluating policies and bundles.

Options:

  • [config]: JSON configuration or path to OPA configuration JSON or YAML (optional). Note: only some configuration fields are currently supported
  • [agent]: http agent to use for any requests (optional).
import { resolve } from "path";

// Load from OPA configuration at path
let opa = new OpaClient({
  config: resolve(__dirname, "../config.yaml"),
});

OpaClient.load([options]): Promise<void>

Load client from configuration, waiting for bundle to be ready.

import { resolve } from "path";
import { OpaClient } from "@timhall/opa";

const opa = new OpaClient({
  config: resolve(__dirname, "../config.yaml"),
});
await opa.load();

// Evaluate using bundle loaded from configuration
const input = {};
const result = await opa.evaluate(input);

Options:

  • [signal]: AbortContoller signal to stop bundle polling

OpaClient.loadBundle(bundle): Promise<void>

Load policy bundle (policy and data) for evaluation.

import { readFile } from "fs/promises";
import { OpaClient } from "@timhall/opa";

const opa = new OpaClient();

const bundle = await readFile("./bundle.tar.gz");
await opa.loadBundle(bundle);

const input = {};
result = await opa.evaluate(input);

OpaClient.loadPolicy(policy): Promise<void>

Load wasm policy for evaluation.

import { readFile } from "fs/promises";
import { OpaClient } from "@timhall/opa";

const opa = new OpaClient();

const policy = await readFile("./policy.wasm");
await opa.loadPolicy(policy);

const input = {};
const result = await opa.evaluate(input);

OpaClient.setData(data)

Set base document for use when evaluating the policy. Overrides any previously defined data, including data from bundles.

import { readFile } from "fs/promises";
import { OpaClient } from "@timhall/opa";

const opa = new OpaClient();

const policy = await readFile("./policy.wasm");
await opa.loadPolicy(policy);
opa.setData({ networks: [] });

const input = {};
const result = await opa.evaluate(input);

OpaClient.evaluate(input?, entrypoint?): object

Evaluate the given input data against the loaded policy or bundle, with an optional entrypoint to limit the evaluation.

import { readFile } from "fs/promises";
import { OpaClient } from "@timhall/opa";

const opa = new OpaClient();

let result = opa.evaluate();
// result = {} with no policy/bundle

const bundle = await readFile("./bundle.tar.gz");
await opa.loadBundle(bundle);

const input = {
  subject: { email: "..." },
  operation: "GET /",
};
result = opa.evaluate(input);

// Limit the evaluation to example/allow entrypoint
result = opa.evaluate(input, "example/allow");