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

@logfire-sh/tools

v1.0.1

Published

JavaScript logging tools

Downloads

8

Readme

logfire JavaScript client(https://logfire.ai) - Helper tools

ISC License

Looking for a logging solution?
Check out logfire and [logfire clients for JavaScript and Node.js](DOC URL).

@logfire-sh/tools

This library provides helper tools used by the JavaScript logger.

Queue<T>

Generic FIFO queue. Used by makeThrottle to store pipeline functions to be executed as concurrent 'slots' become available. Provides fast retrieval for any primitive or object that needs ordered, first-in, first-out retrieval.

Used to store .log() Promises that are being batched/throttled.

Usage example

import { Queue } from '@logfire-sh/tools';

// Interface representing a person
interface IPerson {
  name: string;
  age: number;
}

// Create a queue to store `IPerson` objects
const q = new Queue<IPerson>();

// Add a couple of records...
q.push({ name: 'Jeff', age: 50 });
q.push({ name: 'Sally', age: 39 });

// Pull values from the queue...
while (q.length) {
  console.log(q.shift().name); // <-- first Jeff, then Sally...
}

makeThrottle<T>(max: number)

Returns a throttle higher-order function, which wraps an async function, and limits the number of active Promises to max: number

The throttle function has this signature:

throttle(fn: T): (...args: InferArgs<T>[]) => Promise<InferArgs<T>>

Usage example

import logfire from '@logfire-sh/logger';
import { makeThrottle } from '@logfire-sh/tools';

// Create a new logfire instance
const logfire = new logfire('sourceToken');

// Guarantee a pipeline will run a max of 2x at once
const throttle = makeThrottle(2);

// Create a basic pipeline function which resolves after 2 seconds
const pipeline = async (log) =>
  new Promise((resolve) => {
    setTimeout(() => resolve(log), 2000);
  });

// Add a pipeline which has been throttled
logfire.addPipeline(throttle(pipeline));

// Add 10 logs, and store the Promises
const promises = [];
for (let i = 0; i < 10; i++) {
  promises.push(logfire.log({ message: `Hello ${i}` }));
}

void (async () => {
  void (await promises); // <-- will take 10 seconds total!
})();

makeBatch(size: number, flushTimeout: number)

Creates a higher-order batch function aggregates logfire logs and resolves when either size # of logs have been collected, or when flushTimeout (in ms) has elapsed -- whichever occurs first.

This is used alongside the throttler to provide an array of IlogfireLog to the function set in the .setSync() method, to be synced with logfire.ai

Used internally by the @logfire-sh/core Base class to implicitly batch logs:

// Create a throttler, for sync operations
const throttle = makeThrottle(this._options.syncMax);

// Sync after throttling
const throttler = throttle((logs: any) => {
  return this._sync!(logs);
});

// Create a batcher, for aggregating logs by buffer size/interval
const batcher = makeBatch(this._options.batchSize, this._options.batchInterval);

this._batch = batcher((logs: any) => {
  return throttler(logs);
});

LICENSE

ISC