{"id":18831423,"url":"https://github.com/kingstinct/utils","last_synced_at":"2025-07-13T14:32:28.858Z","repository":{"id":114881049,"uuid":"513828854","full_name":"kingstinct/utils","owner":"kingstinct","description":null,"archived":false,"fork":false,"pushed_at":"2024-02-06T06:28:55.000Z","size":671,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-29T16:23:00.203Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kingstinct.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-07-14T08:57:58.000Z","updated_at":"2022-07-14T08:58:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"f180ca4c-fb0a-41e3-bc44-3020fc7d355a","html_url":"https://github.com/kingstinct/utils","commit_stats":null,"previous_names":[],"tags_count":47,"template":false,"template_full_name":null,"purl":"pkg:github/kingstinct/utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingstinct%2Futils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingstinct%2Futils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingstinct%2Futils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingstinct%2Futils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kingstinct","download_url":"https://codeload.github.com/kingstinct/utils/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingstinct%2Futils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265155759,"owners_count":23719577,"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-11-08T01:54:11.061Z","updated_at":"2025-07-13T14:32:28.817Z","avatar_url":"https://github.com/kingstinct.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"⚠️ This repo has been moved to [Kingstinct/zemble](https://github.com/kingstinct/zemble/tree/main/packages/utils), and has been renamed to [`@zemble/utils`](https://www.npmjs.com/package/@zemble/utils).\n\n# @kingstinct/utils\n\n[![npm (scoped)](https://img.shields.io/npm/v/@kingstinct/utils?style=for-the-badge)](https://www.npmjs.com/package/@kingstinct/utils)\n\nThis is a generic utility library that we use across our projects at Kingstinct (still early days for this lib).\n\nThere are two main imports, one generic and one for some node-specific stuff:\n`import { wait, times, sample, logPrettyData } from '@kingstinct/utils'`\n\n`import { gravatarUrlForEmail } from '@kingstinct/utils/lib/node'`\n\nYou can also import utilities directly:\n`import wait from '@kingstinct/utils/lib/wait'`\n\nThe goal of this library (and the related [@kingstinct/react](https://github.com/Kingstinct/react)) is to:\n- Keep the number of dependencies in projects down\n- Have a common place to put useful utilities, so they're easier to maintain and find\n- Quickly get up and running with new projects\n\nWe believe this is a better approach than the alternatives:\n- Using one single utility library for everything, which would introduce unnecessary dependencies\n- Using lots of micro-libs. Micro-libs does have it's advantages, but is harder to maintain and means loosing oversight of the dependencies in a project.\n- Copy pasting between projects :)\n\n## Timeoutify\n\nTimeoutify is a utility to handle timeouts making it easy to clean up resources when a timeout occurs, and it can also be aborted for other reasons (client disconnects for example). \n\nThe easiest way to use it is through the Fastify plugin, and access it on from your request object:\n  \n  ```ts\n  import fastify from 'fastify'\n  import { timeoutifyPlugin } from '@kingstinct/utils/fastify/timeoutifyPlugin'\n  import mongodb from 'mongodb'\n\n  const fastifyServer = fastify()\n  fastifyServer.register(timeoutifyPlugin, { timeoutMS: 30000 }) // \u003c- time out your request after 30 seconds\n\n  const db = await mongodb.connect('mongodb://localhost:27017', { timeout: req.timeoutify.timeout })\n\n  fastifyServer.get('/callSomeOtherApi', async (req, res) =\u003e {    \n    const result = await fetch('https://api.slow.app', { signal: req.timeoutify.abortSignal });\n    return result;\n  })\n\n  fastifyServer.get('/callMongoDb', async (req, res) =\u003e {    \n    const result = await req.timeoutify.runMongoOpWithTimeout(\n      db.collection('users').find({})\n    );\n    return result;\n  })\n  ```\nThis will take care of the following:\n- If the timeout (of 30s in this example) is hit a 504 response will be sent to the client.\n- If the client disconnects (ex: closes browser tab) a 499 response will be sent to the client.\n- If the request is aborted (by either a timeout or client disconnect) the fetch request will be aborted. Works with any calls supporting AbortSignal.\n- If the request times out the MongoDB query will time out at the same time. This ensures that MongoDB queries are not left hogging resources.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkingstinct%2Futils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkingstinct%2Futils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkingstinct%2Futils/lists"}