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

resource-handler

v3.2.0

Published

Thin wrapper around async resource

Downloads

14

Readme

resource-handler

A thin wrapper around asynchronous resources

npm version Actions Status

Motivation

There are some scenarios when you need to monitor an async resource like a database connection that does not have auto reconnection functionality in order to recover it from a failure. This package provdes a lightwight wrapper around such resources that allows you to easily restore their state without any hussle.

Installation

npm i resource-handler

API

You can find API here.

Quick start

import * as amqp from 'amqplib';
import { create } from 'resource-handler';

const rh = create(async () => {
    return amqp.connect(opts);
});

await rh.open();

const connection = await rh.resource();

await rh.close();

With automatic opening:

import * as amqp from 'amqplib';
import { open } from 'resource-handler';

const rh = await open(async () => {
    return amqp.connect(opts);
});

const connection = await rh.resource();

await rh.close();

Retry options

By default, resource-handler uses default values for restoring a given resouce. You can tune it to meet your needs:

import * as amqp from 'amqplib';
import { create } from 'resource-handler';

const rh = create(
    async () => {
        return amqp.connect(opts);
    },
    {
        retry: {
            retries: 5,
            minTimeout: 2000,
            maxTimeout: 10000,
            factor: 1.5,
            randomize: true,
        },
    },
);

await rh.open();

const connection = await rh.resource();

await rh.close();

Custom closer

In case your resource has other than .close method for closing its operation, you can provide a custom closer function:

import { create } from 'resource-handler';

const rh = create(
    async () => {
        return connect();
    },
    {
        closer: (resource) => resource.destroy(),
    },
);

await rh.open();

const connection = await rh.resource();

await rh.close();

Events proxying

import { create } from 'resource-handler';

const rh = create(
    async () => {
        return connect();
    },
    {
        events: ['foo'],
    },
);

await rh.open();

rh.on('foo', () => console.log('bar'));

const connection = await rh.resource();

connection.emit('foo');

await rh.close();

Abrupt retries

import { create } from 'resource-handler';

const rh = create(
    async () => {
        return connect();
    },
    {
        retry: {
            onFailedAttempt(err) {
                if (err.message === 'some error') {
                    throw new Error('Stop it!');
                }
            },
        },
    },
);

try {
    await rh.open();
    const res = await rh.resource();

    console.log("Successfuly open a resource");
} catch (e) {
    console.log("Failed to open a resource", e);
}