node-casbin / pubsub-watcher

Google Cloud Pub/Sub watcher for Node-Casbin

Home Page:https://github.com/casbin/node-casbin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pubsub-watcher

NPM version CI

Google Cloud Pub/Sub watcher for node-casbin

Installation

# NPM
npm install --save @casbin/pubsub-watcher

# Yarn
yarn add @casbin/pubsub-watcher

Simple Example

import { PubsubWatcher } from '@casbin/pubsub-watcher';
import { newEnforcer } from 'casbin';

// Initialize the watcher.
// You need to create Pubsub topic and subscription(per casbin instance) first
const watcher = await PubsubWatcher.newWatcher();

// Initialize the enforcer.
const enforcer = await newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv');

enforcer.setWatcher(watcher);

// By default, the watcher's callback is automatically set to the
// enforcer's loadPolicy() in the setWatcher() call.
// We can change it by explicitly setting a callback.
watcher.setUpdateCallback(() => console.log('Casbin need update'));

Test

Set GOOGLE_APPLICATION_CREDENTIALS in environment variable and npm run test

Test uses Pub/Sub emulator

gcloud beta emulators pubsub start --project=casbin
PUBSUB_EMULATOR_HOST=localhost:8085 PUBSUB_PROJECT_ID=casbin npm run test

About

Google Cloud Pub/Sub watcher for Node-Casbin

https://github.com/casbin/node-casbin

License:Apache License 2.0


Languages

Language:JavaScript 54.5%Language:TypeScript 45.5%