0.0.0-development • Published 7 years ago

worker-tokens v0.0.0-development

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

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 yarn or npm
$ 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 createTokenInterface with 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 cooldown value and returns an observable of tokens
    • tokenInterface.tokenGenerator(1000) will emit a new token every 1s
  • checkToken: string -> Observable

    • Takes in a token and returns an observable of if that token is valid
    • checkToken('token_123') will emit true/false or throw error
  • useToken: string -> Observable

    • Takes in a token and returns an observable of the work to be performed
    • useToken('token_123') will emit 1 if successful