{"id":16773827,"url":"https://github.com/ziflex/resource-handler","last_synced_at":"2025-03-16T17:21:29.985Z","repository":{"id":42871455,"uuid":"257968768","full_name":"ziflex/resource-handler","owner":"ziflex","description":"A thin wrapper around asynchronous resources","archived":false,"fork":false,"pushed_at":"2023-03-04T13:55:55.000Z","size":638,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-24T04:16:35.271Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://ziflex.github.io/resource-handler/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ziflex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-22T17:13:53.000Z","updated_at":"2021-09-23T15:16:01.000Z","dependencies_parsed_at":"2023-02-08T01:30:54.152Z","dependency_job_id":null,"html_url":"https://github.com/ziflex/resource-handler","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fresource-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fresource-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fresource-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fresource-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ziflex","download_url":"https://codeload.github.com/ziflex/resource-handler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243903166,"owners_count":20366434,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T06:47:09.030Z","updated_at":"2025-03-16T17:21:29.959Z","avatar_url":"https://github.com/ziflex.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# resource-handler\n\nA thin wrapper around asynchronous resources\n\n[![npm version](https://badge.fury.io/js/resource-handler.svg)](https://www.npmjs.com/package/resource-handler)\n[![Actions Status](https://github.com/ziflex/resource-handler/workflows/Node%20CI/badge.svg)](https://github.com/ziflex/resource-handler/workflows/Node%20CI/badge.svg)\n\n## Motivation\n\nThere 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.\n\n## Installation\n\n```bash\nnpm i resource-handler\n```\n\n## API\n\nYou can find API [here](https://ziflex.github.io/resource-handler).\n\n## Quick start\n\n```typescript\nimport * as amqp from 'amqplib';\nimport { create } from 'resource-handler';\n\nconst rh = create(async () =\u003e {\n    return amqp.connect(opts);\n});\n\nawait rh.open();\n\nconst connection = await rh.resource();\n\nawait rh.close();\n```\n\nWith automatic opening:\n\n```typescript\nimport * as amqp from 'amqplib';\nimport { open } from 'resource-handler';\n\nconst rh = await open(async () =\u003e {\n    return amqp.connect(opts);\n});\n\nconst connection = await rh.resource();\n\nawait rh.close();\n```\n\n### Retry options\n\nBy default, `resource-handler` uses default values for restoring a given resouce. You can tune it to meet your needs:\n\n```typescript\nimport * as amqp from 'amqplib';\nimport { create } from 'resource-handler';\n\nconst rh = create(\n    async () =\u003e {\n        return amqp.connect(opts);\n    },\n    {\n        retry: {\n            retries: 5,\n            minTimeout: 2000,\n            maxTimeout: 10000,\n            factor: 1.5,\n            randomize: true,\n        },\n    },\n);\n\nawait rh.open();\n\nconst connection = await rh.resource();\n\nawait rh.close();\n```\n\n### Custom closer\n\nIn case your resource has other than `.close` method for closing its operation, you can provide a custom closer function:\n\n```typescript\nimport { create } from 'resource-handler';\n\nconst rh = create(\n    async () =\u003e {\n        return connect();\n    },\n    {\n        closer: (resource) =\u003e resource.destroy(),\n    },\n);\n\nawait rh.open();\n\nconst connection = await rh.resource();\n\nawait rh.close();\n```\n\n### Events proxying\n\n```typescript\nimport { create } from 'resource-handler';\n\nconst rh = create(\n    async () =\u003e {\n        return connect();\n    },\n    {\n        events: ['foo'],\n    },\n);\n\nawait rh.open();\n\nrh.on('foo', () =\u003e console.log('bar'));\n\nconst connection = await rh.resource();\n\nconnection.emit('foo');\n\nawait rh.close();\n```\n\n### Abrupt retries\n\n```typescript\nimport { create } from 'resource-handler';\n\nconst rh = create(\n    async () =\u003e {\n        return connect();\n    },\n    {\n        retry: {\n            onFailedAttempt(err) {\n                if (err.message === 'some error') {\n                    throw new Error('Stop it!');\n                }\n            },\n        },\n    },\n);\n\ntry {\n    await rh.open();\n    const res = await rh.resource();\n\n    console.log(\"Successfuly open a resource\");\n} catch (e) {\n    console.log(\"Failed to open a resource\", e);\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziflex%2Fresource-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fziflex%2Fresource-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziflex%2Fresource-handler/lists"}