{"id":14256965,"url":"https://github.com/hazae41/piscine","last_synced_at":"2025-09-01T12:35:53.604Z","repository":{"id":142716820,"uuid":"613945359","full_name":"hazae41/piscine","owner":"hazae41","description":"Create async pools with automatic retry","archived":false,"fork":false,"pushed_at":"2025-04-01T15:25:55.000Z","size":445,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-01T15:30:09.746Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/hazae41.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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,"publiccode":null,"codemeta":null},"funding":{"github":["hazae41"],"patreon":"hazae41"}},"created_at":"2023-03-14T15:25:23.000Z","updated_at":"2025-04-01T15:25:59.000Z","dependencies_parsed_at":"2024-01-16T19:05:04.805Z","dependency_job_id":"10611325-8f5d-4104-b39d-f7aeac5826d0","html_url":"https://github.com/hazae41/piscine","commit_stats":null,"previous_names":[],"tags_count":109,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazae41%2Fpiscine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazae41%2Fpiscine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazae41%2Fpiscine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazae41%2Fpiscine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hazae41","download_url":"https://codeload.github.com/hazae41/piscine/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249810997,"owners_count":21328730,"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-08-22T07:01:39.645Z","updated_at":"2025-04-19T21:35:51.021Z","avatar_url":"https://github.com/hazae41.png","language":"TypeScript","funding_links":["https://github.com/sponsors/hazae41","https://patreon.com/hazae41"],"categories":["TypeScript"],"sub_categories":[],"readme":"![piscine](https://user-images.githubusercontent.com/4405263/225078829-f9cbc271-1740-44b8-929c-802d0929fa5c.png)\n\nCreate async pools with automatic retry\n\n```bash\nnpm i @hazae41/piscine\n```\n\n[**Node Package 📦**](https://www.npmjs.com/package/@hazae41/piscine)\n\n## Features\n\n### Current features\n- 100% TypeScript and ESM\n- No external dependency\n- Simple API\n- Automatic retry\n- Get a fast random value using Math's PRNG\n- Get a secure random value using WebCrypto's CSPRNG\n- Rust-like resource borrowing and moving\n\n### Usage\n\nCreate an automatic pool of 10 WebSockets\n\n```tsx\nimport \"@hazae41/symbol-dispose-polyfill\"\n\nimport { Box, Deferred, Disposer, Stack } from \"@hazae41/box\"\nimport { Future } from \"@hazae41/future\"\nimport { test } from \"@hazae41/phobos\"\nimport { AutoPool } from \"@hazae41/piscine\"\n\nasync function openOrThrow(socket: WebSocket, signal: AbortSignal) {\n  using stack = new Stack()\n\n  const future = new Future\u003cvoid\u003e()\n\n  const onOpen = () =\u003e future.resolve()\n  const onError = () =\u003e future.reject(new Error(\"Errored\"))\n  const onAbort = () =\u003e future.reject(new Error(\"Aborted\"))\n\n  socket.addEventListener(\"open\", onOpen, { passive: true })\n  stack.push(new Deferred(() =\u003e socket.removeEventListener(\"open\", onOpen)))\n\n  socket.addEventListener(\"error\", onError, { passive: true })\n  stack.push(new Deferred(() =\u003e socket.removeEventListener(\"error\", onError)))\n\n  signal.addEventListener(\"abort\", onAbort, { passive: true })\n  stack.push(new Deferred(() =\u003e signal.removeEventListener(\"abort\", onAbort)))\n\n  return await future.promise\n}\n\nasync function createOrThrow(index: number, signal: AbortSignal) {\n  const socket = new WebSocket(\"wss://echo.websocket.org/\")\n  await openOrThrow(socket, signal)\n\n  const resource = Disposer.wrap(socket, () =\u003e socket.close())\n\n  using entry = new Box(resource)\n  using stack = new Box(new Stack())\n\n  const onClose = () =\u003e pool.delete(index)\n\n  socket.addEventListener(\"close\", onClose, { passive: true })\n  stack.get().push(new Deferred(() =\u003e socket.removeEventListener(\"close\", onClose)))\n\n  const unentry = entry.unwrapOrThrow()\n  const unstack = stack.unwrapOrThrow()\n\n  return Disposer.wrap(unentry, () =\u003e unstack[Symbol.dispose]())\n}\n\n// Launch a pool of 10 sockets\nusing pool = new AutoPool\u003cDisposer\u003cWebSocket\u003e\u003e(createOrThrow, 10)\n\n{\n  // Borrow socket 0 when it's available\n  using borrow = await pool.waitOrThrow(0, x =\u003e x?.getOrNull()?.borrowOrNull())\n  const socket = borrow.get().get().get()\n\n  socket.send(\"hello\")\n\n  await new Promise(ok =\u003e socket.addEventListener(\"message\", ok))\n\n  // Return socket 0 into the pool\n}\n\n{\n  // Take a random available socket and automatically start creating a new one\n  using taken = await pool.waitRandomOrThrow(x =\u003e x?.getOrNull()?.unwrapOrNull())\n  const socket = taken.get().get()\n\n  socket.send(\"hello\")\n\n  await new Promise(ok =\u003e socket.addEventListener(\"message\", ok))\n\n  // Close the socket\n}\n\n// Use all stale sockets to send a message\nfor (const entry of pool) {\n  if (entry.isErr())\n    continue\n  const item = entry.get()\n\n  if (item.borrowed)\n    continue\n  const socket = item.get().get().get()\n\n  socket.send(\"hello\")\n}\n\n// Get all entries\nconst entries = [...pool]\n\n// Destroy the pool\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhazae41%2Fpiscine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhazae41%2Fpiscine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhazae41%2Fpiscine/lists"}