https://github.com/ziflex/resource-handler
A thin wrapper around asynchronous resources
https://github.com/ziflex/resource-handler
Last synced: 10 months ago
JSON representation
A thin wrapper around asynchronous resources
- Host: GitHub
- URL: https://github.com/ziflex/resource-handler
- Owner: ziflex
- License: mit
- Created: 2020-04-22T17:13:53.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T13:55:55.000Z (almost 3 years ago)
- Last Synced: 2025-02-24T04:16:35.271Z (10 months ago)
- Language: TypeScript
- Homepage: https://ziflex.github.io/resource-handler/
- Size: 623 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# resource-handler
A thin wrapper around asynchronous resources
[](https://www.npmjs.com/package/resource-handler)
[](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);
}
```