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

@mapbox/locking

v4.0.0

Published

Read I/O locking using LRU cache

Downloads

4,649

Readme

@mapbox/locking :lock: Build Status

npm install --save @mapbox/locking

Wrap any callback or async function to use an LRU cache and prevent (lock) asynchronous operations across concurrent calls.

Usage

const { Locking, LockingAsync } = require('@mapbox/locking');

The options objects are passed directly to lru-cache allowing you to set the max age, max items, and other behaviors of the LRU cache. When options.allowStale is set to true the locking cache implements additional behavior to continue serving a stale item until the item has been refreshed in the background.

Locking(function, options)

For locking callback functions in the form function(id, callback). Returns a callable function

const { Locking } = require('@mapbox/locking');
const fs = require('fs');
const readFile = Locking(fs.readFile, options);

// Reads file once, calls callback 10x.
for (let i = 0; i < 10; i++) {
  readFile('./sample.txt', function(err, data) {
    console.log(data);
  });
}

LockingAsync(function, options)

Constructor for locking promise or async functions in the form of function(...arguments). This can manage functions of arbitrary arguments and types. Returns a class with two functions, which includes a callable method get() for calling the original function.

const { LockingAsync } = require('@mapbox/locking');
const got = require('got');

const jsonApi = new Locking((url) => {
  return got(url).json();
}, options);

// call function
await jsonApi.get('https://api.mapbox.com');
await jsonApi.get('https://api.mapbox.com');
await jsonApi.get('https://api.mapbox.com');

// get stats
console.log(jsonApi.stats); // { total: 3, hit: 2, miss: 1, locks: 2, refreshHit: 0, currentLocks: 0, size: 1 }