{"id":22382419,"url":"https://github.com/jcoreio/safe-iterate-readable-stream","last_synced_at":"2025-03-26T19:40:51.307Z","repository":{"id":262356781,"uuid":"886984920","full_name":"jcoreio/safe-iterate-readable-stream","owner":"jcoreio","description":null,"archived":false,"fork":false,"pushed_at":"2024-12-02T19:41:36.000Z","size":58,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-02-01T01:41:35.290Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jcoreio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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}},"created_at":"2024-11-12T00:58:54.000Z","updated_at":"2024-12-02T19:40:28.000Z","dependencies_parsed_at":"2025-02-01T01:41:31.743Z","dependency_job_id":"aaee71a1-6347-4d0c-892e-ee0aedb33ff1","html_url":"https://github.com/jcoreio/safe-iterate-readable-stream","commit_stats":null,"previous_names":["jcoreio/safe-iterate-readable-stream"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsafe-iterate-readable-stream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsafe-iterate-readable-stream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsafe-iterate-readable-stream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsafe-iterate-readable-stream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcoreio","download_url":"https://codeload.github.com/jcoreio/safe-iterate-readable-stream/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245726746,"owners_count":20662544,"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-12-05T00:12:57.218Z","updated_at":"2025-03-26T19:40:51.300Z","avatar_url":"https://github.com/jcoreio.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @jcoreio/safe-iterate-readable-stream\n\navoid pitfalls when async iterating a ReadableStream\n\n[![CircleCI](https://circleci.com/gh/jcoreio/safe-iterate-readable-stream.svg?style=svg)](https://circleci.com/gh/jcoreio/safe-iterate-readable-stream)\n[![Coverage Status](https://codecov.io/gh/jcoreio/safe-iterate-readable-stream/branch/master/graph/badge.svg)](https://codecov.io/gh/jcoreio/safe-iterate-readable-stream)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![npm version](https://badge.fury.io/js/%40jcoreio%2Fsafe-iterate-readable-stream.svg)](https://badge.fury.io/js/%40jcoreio%2Fsafe-iterate-readable-stream)\n\nLong-lived promises are dangerous. At JCore Systems, we have a lot of places we wrap pubsub in async iterables or `ReadableStream`s. This means we're creating\npromises that can be indefinitely pending until the next pubsub event comes in.\n\nIt's extremely easy to cause memory and resource leaks with long-lived promises like this. Even if you use `Promise.race([resultPromise, abortPromise(signal)])`\nto make sure a next event promise rejects right away if you abort a signal, [it still leaks memory](https://github.com/nodejs/node/issues/17469). The handlers\nthat were waiting on the promise are still retained as long as `resultPromise` is pending, even after the other promise rejects.\n\nTo complicate matters with `ReadableStream`s, the builtin `ReadableStream[Symbol.asyncIterator]` implementation doesn't cancel the stream right away when the iterator\nis `return()`ed if the last `next()` promise is still pending because the underlying read is waiting. If the read doesn't resolve until a pubsub event comes in,\nthe stream could be stuck open for an unbounded amount of time.\n\nTo avoid this danger, you have to either write the underlying operations to time out after a maximum amount of time (which still delays cleanup temporarily) or\nuse use carefully-written logic like `@jcoreio/safe-iterate-readable-stream` provides to guarantee timely cleanup when async operations are aborted.\n\n## `safeIterateReadableStream(stream, signal)`\n\nReturns an `AsyncIterable` that iterates over the given stream. If the iterator is `return()`ed, `throw()`n, or `signal` is aborted, it will close and reject\nany outstanding `next()` promises immediately.\n\n```ts\nimport { safeIterateReadableStream } from '@jcoreio/safe-iterate-readable-stream'\n\nconst stream = new ReadableStream({ ... })\n\nconst abortController = new AbortController()\nconst { signal } = abortController\n\nfor await (const chunk of safeIterateReadableStream(stream, signal)) {\n  console.log(chunk)\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Fsafe-iterate-readable-stream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcoreio%2Fsafe-iterate-readable-stream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Fsafe-iterate-readable-stream/lists"}