npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fluxninja/aperture-js

v2.12.0

Published

Flow control SDK that interfaces with FluxNinja Aperture Agents

Downloads

2,119

Readme

Rate Limiting for JavaScript or Node.js Applications

The aperture-js SDK provides an easy way to integrate your JavaScript applications with FluxNinja Aperture. It allows flow control functionality on fine-grained features inside service code.

Refer documentation for more details.

Use Cases and Features

Aperture provides rate limiting, caching, and api quota management to effectively manage the load on your application. With its unique approach that separates application code from rate limiting code/infra, it can effectively handle various use cases of rate limiting such as:

  • Enforcing or complying with
    • Rate-limits based on no. of requests per second
    • Per-user rate-limits based on consumed tokens (e.g. quota mangement for GPT APIs)
    • Rate-limits based on user subscription plans
    • Rate-limits based on token-bucket algorithm
    • Fine-grained rate-limits configured via policies UI or yaml
  • Prioritizing requests based on custom labels (e.g. free user vs paid user)
  • Enforcing adaptive rate-limits based on concurrency and available system capacity
  • Managing load on your databases or self-hosted services such as Mistral, CodeLlama, etc.
  • Caching API responses to avoid high API or Cloud cost and improve time to response
  • Updating rate-limiting policies from UI without changing the code
  • Monitoring workload for your internal or external services

Are we missing your use case for FluxNinja Aperture? Share with us on Discord

Usage

Install SDK

Run the command below to install the SDK:

npm install @fluxninja/aperture-js

Create Aperture Client

The next step is to create an Aperture Client instance, for which, the address of the organization created in Aperture Cloud and API key are needed. You can locate both these details by clicking on the Aperture tab in the sidebar menu of Aperture Cloud.

import { ApertureClient } from "@fluxninja/aperture-js";

// Create aperture client
export const apertureClient = new ApertureClient({
  address: "ORGANIZATION.app.fluxninja.com:443",
  apiKey: "API_KEY",
});

Flow Functionality

The created instance can then be used to start a flow:

async function handleRequestRateLimit(req: Request, res: Response) {
  // Start a flow by passing control point and business labels
  const flow = await apertureClient.startFlow("awesomeFeature", {
    labels: {
      limit_key: "some_user_id",
    },
    grpcCallOptions: {
      deadline: Date.now() + 300, // ms
    },
  });

  if (flow.shouldRun()) {
    // Add business logic to process incoming request
    console.log("Request accepted. Processing...");
    const resString = "foo";
    res.send({ message: resString });
  } else {
    console.log("Request rate-limited. Try again later.");
    // Handle flow rejection
    flow.setStatus(FlowStatus.Error);
    res.status(429).send({ message: "Too many requests" });
  }

  flow.end();
}

The above code snippet is making startFlow calls to Aperture. For this call, it is important to specify the control point (awesomeFeature in the example) and business labels that will be aligned with the policy created in Aperture Cloud. For request prioritization use cases, it's important to set a higher gRPC deadline. This parameter specifies the maximum duration a request can remain in the queue. For each flow that is started, a shouldRun decision is made, determining whether to allow the request into the system or to rate limit it. In this example, we only see log returns, but in a production environment, actual business logic can be executed when a request is allowed. It is important to make the end call made after processing each request, to send telemetry data that would provide granular visibility for each flow.

For more context on using the Aperture JavaScript SDK to set feature control points, refer to the example app available in the repository.

API Reference

@fluxninja/aperture-js

Table of contents

Enumerations

Classes

Interfaces