Google Cloud Pub/Sub watcher for Node-Casbin

Clone this repo:
  1. f0c592b fix: fix broken links (#3) by YunShu · 2 years, 7 months ago master
  2. ee4b01c Merge pull request #2 from node-casbin/cicd by Zixuan Liu · 6 years ago
  3. cd24848 Use github actions for test and build by hjl · 6 years ago cicd
  4. ef0d3c8 Merge pull request #1 from elaijuh/fix-package-main by Zixuan Liu · 6 years ago
  5. 271dd1b fix package.main by hjl · 6 years ago

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