0.0.0-development • Published 8 years ago
worker-tokens v0.0.0-development
Worker Tokens
Like proof-of-work but without all the hassle
Overview
This project gives an interface for creating, using, and checking tokens stored in a redis instance. The overall goal of this is to create a way to for workers/instances to have a limit on the amount of work they are allowed to perform inside of a given system.
Usage
- Add it via
yarnornpm
$ yarn add worker-tokens
// Or
$ npm i -s worker-tokens- Create token interface
// via es modules
import { createTokenInterface } from 'worker-tokens'
// or via cjs modules
const { createTokenInterface } = require('worker-tokens')- Call
createTokenInterfacewith an object that has none or all of the below values:
const tokenInterface = createTokenInterface({
cooldown: 1000, // Create a token every x milliseconds
redisPort: 9876, // Connect to Redis port
redisHost: 'localhost', // Connect to Redis host
tokenLimit: 60, // The max life of a token
prefix: 'DEMO_TOKEN_' // The prefix for all tokens created by this interface
})Interface
tokenGenerator: number -> Observable- Takes in a
cooldownvalue and returns an observable of tokens tokenInterface.tokenGenerator(1000)will emit a new token every 1s
- Takes in a
checkToken: string -> Observable- Takes in a
tokenand returns an observable of if that token is valid checkToken('token_123')will emittrue/falseor throw error
- Takes in a
useToken: string -> Observable- Takes in a
tokenand returns an observable of the work to be performed useToken('token_123')will emit1if successful
- Takes in a
0.0.0-development
8 years ago