Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

bucket-decorator

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bucket-decorator

Decorators for limiting usage of a class method

latest
Source
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

Bucket Decorator

Bucket Decorator is a npm module for help you to use safe web services or other stuff without getting api limit error (Too many requests)

  • Intended to use with webServices with traffic policy with Leaky Bucket algorithm
  • No need to change your current implementation, just decorate used methods
  • Magic

Requirements

  • reflect-metadata module

Installation

  • npm install bucket-decorator

Usage

  • Import the module:
import { Bucket } from 'bucket-decorator';
  • Decorate your method:
class MyAwesomeClass {
    ...
    @Bucket.Limiter({
        limit: 40,
        leakRate: 2,
        limitKey: 'my namespace',
    })
    myAwesomeMethod(@Bucket.LimitKey() token){...}
    ...
}

API Reference

@Bucket.Limiter(options: BucketOptionsInterface)

You can decorate any method

PropertytypeDefault valueDescription
limitnumber100Maxim 'bucket' size. Method calls what 'get' into bucket are executed instantly. Other ones are in queue.
leakRatenumber10Leak rate for your bucket. Every second 'bucket' will have place for more leakRate calls.
limitKeystring''Is optional. You can have multiple 'buckets' with different 'names'.

@Bucket.LimitKey()

You can decorate any string propery of your method. This will override BucketOptionsInterface.limitKey option

Example

import 'reflect-metadata';

import {
    Bucket
} from 'bucket-decorator';


class Test {

    @Bucket.Limiter({
        leakRate: 2,
        limit: 40
    })
    doMyWork( @Bucket.LimitKey() myToken) {

        console.log(`Working for ${myToken}`);
    }
}


(async () => {

    let testInstance = new Test();
    for (let i = 0; i < 100; i++) {

        await testInstance.doMyWork('my-secret-token');
    }
})();

Building

  • npm install or yarn
  • npm run start or yarn start

Changelog

1.0.2

  • Add default values for BucketOptionsInterface
  • Fix queue for function calls

1.0.1 - Initial Release

Keywords

nodejs

FAQs

Package last updated on 08 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts