# resource-handler

> Thin wrapper around async resource

Latest version **3.2.0** (published 2021-09-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install resource-handler
pnpm add resource-handler
yarn add resource-handler
bun add resource-handler
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2021-09-23 |
| First published | 2020-08-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 149.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Tim Voronov |
| Maintainers | ziflex |
| Keywords | async, await, connection, wrapper, handler, auto, reconnect |

## Links

- npm: https://www.npmjs.com/package/resource-handler
- Repository: https://github.com/ziflex/resource-handler
- Homepage: https://github.com/ziflex/resource-handler#readme
- Issues: https://github.com/ziflex/resource-handler/issues
- npm.io page: https://npm.io/package/resource-handler

## Dependencies (2)

- [lock](https://npm.io/package/lock.md) ^1.1.0
- [p-retry](https://npm.io/package/p-retry.md) ^4.6.1

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 3.2.0 (latest) — 2021-09-23
- 3.1.1 — 2021-09-23
- 3.1.0 — 2021-09-22
- 3.0.0 — 2021-09-22
- 2.3.0 — 2021-09-21
- 2.2.0 — 2021-09-21
- 2.1.0 — 2021-01-08
- 2.0.0 — 2021-01-07
- 1.1.0 — 2020-08-17
- 1.0.1 — 2020-08-13
- 1.0.0 — 2020-08-12

## README

# resource-handler

A thin wrapper around asynchronous resources

[![npm version](https://badge.fury.io/js/resource-handler.svg)](https://www.npmjs.com/package/resource-handler)
[![Actions Status](https://github.com/ziflex/resource-handler/workflows/Node%20CI/badge.svg)](https://github.com/ziflex/resource-handler/workflows/Node%20CI/badge.svg)

## 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

```bash
npm i resource-handler
```

## API

You can find API [here](https://ziflex.github.io/resource-handler).

## Quick start

```typescript
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:

```typescript
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:

```typescript
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:

```typescript
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

```typescript
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

```typescript
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);
}

```

---
_Source: https://npm.io/package/resource-handler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
