{"id":15493472,"url":"https://github.com/sinclairzx81/corsa","last_synced_at":"2025-08-01T15:02:54.295Z","repository":{"id":57209583,"uuid":"152276944","full_name":"sinclairzx81/corsa","owner":"sinclairzx81","description":"Asynchronous uni-directional channels in node using async iteration.","archived":false,"fork":false,"pushed_at":"2020-03-01T18:22:37.000Z","size":104,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-22T19:54:44.165Z","etag":null,"topics":["async-iterators","backpressure","channels","streams"],"latest_commit_sha":null,"homepage":"","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/sinclairzx81.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":"2018-10-09T15:36:53.000Z","updated_at":"2025-01-26T23:34:18.000Z","dependencies_parsed_at":"2022-09-01T07:51:11.862Z","dependency_job_id":null,"html_url":"https://github.com/sinclairzx81/corsa","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sinclairzx81/corsa","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinclairzx81%2Fcorsa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinclairzx81%2Fcorsa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinclairzx81%2Fcorsa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinclairzx81%2Fcorsa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sinclairzx81","download_url":"https://codeload.github.com/sinclairzx81/corsa/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinclairzx81%2Fcorsa/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268248281,"owners_count":24219547,"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","status":"online","status_checked_at":"2025-08-01T02:00:08.611Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["async-iterators","backpressure","channels","streams"],"created_at":"2024-10-02T08:07:01.309Z","updated_at":"2025-08-01T15:02:54.270Z","avatar_url":"https://github.com/sinclairzx81.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Corsa\n\nAsync iteration channels in JavaScript.\n\n[![NPM package](https://badge.fury.io/js/corsa.svg)](https://www.npmjs.com/package/corsa) \n [![Build Status](https://travis-ci.org/sinclairzx81/corsa.svg?branch=master)](https://travis-ci.org/sinclairzx81/corsa)\n\n```\n$ npm install corsa --save\n```\n```typescript\nimport { channel } from 'corsa'\n\nconst { readable, writable } = channel()\nwritable.write(3)\nwritable.write(2)\nwritable.write(1)\nwritable.end()\n\nfor await (const value of readable) {\n  console.log(value)\n}\nconsole.log('done')\n```\n```\noutput:\n\u003e 3\n\u003e 2\n\u003e 1\n\u003e done\n```\n\n## Overview\n\nCorsa is a library for creating buffered readable / writable channels in JavaScript. This library was specifically written to help solve backpressure issues that can occur when dealing with high frequency messaging using traditional event listeners in JavaScript.\n\nCorsa approaches this problem by making the channels sender await and suspend at buffer capacity. This helps to ensure the senders send rate is locked to the throughput allowed by a receiver.\n\nRequires async/await and AsyncIteration support. Tested natively on Node v10.\n\n## channel\u0026lt;T\u0026gt;\n\nA channel is a uni-directional pipe for which data can flow. The following code creates an `unbounded` channel which allows for near infinite buffering of messages between `writable` and `readable`. The call to channel returns a `channel` object, which we destructure into the readable and writable pairs.\n\n```typescript\nconst { readable, writable } = channel()\n```\nThe following creates a bounded channel which allows for sending `5` values before suspending (see bounded vs unbounded)\n\n```typescript\nconst { readable, writable }  = channel(5)\n```\n\n## Writer\u0026lt;T\u0026gt;\n\nThe following code creates an unbounded channel and sends the values `1, 2, 3` followed by a call to `end()` to signal `EOF` to a receiver.\n\n```typescript\nconst { readable, writable } = channel\u003cnumber\u003e()\n\nwritable.write(1)\nwritable.write(2)\nwritable.write(3)\nwritable.end()\n\n```\n\n## Reader\u0026lt;T\u0026gt;\n\nThe `Reader\u003cT\u003e` is the receiving side of a channel and supports `for-await-of` for general iteration. \n\n```typescript\nconst { readable, writable } = channel()\nwritable.write(1)\nwritable.write(2)\nwritable.write(3)\nwritable.end()\n\nfor await (const value of readable) {\n  console.log(value)\n}\n\n```\n\n## bounded vs unbounded\n\nBy default all channels are `unbounded` but it is possible to set a fixed buffering size when creating a `channel()`. When setting a channel size, this will cause a writable to pause at `await` when sending values. The `await` at the writable will only occur once the channels buffer has filled with values. The writable will remained suspended until such time a receiver starts pulling values from the channel.\n\nThe following code demostrates this behavior with channel bound to a buffer of 5.\n\n```typescript\nconst { readable, writable } = channel(5)\nawait writable.write(1) \nawait writable.write(2) \nawait writable.write(3) \nawait writable.write(4) \nawait writable.write(5) // - at capacity, the readable will need to read something.\n\nawait writable.write(6) // suspend  \u003c-----+\n                        //                | - readable.read() dequeues one element from the\n...                     //                |   stream which will cause the writable to resume.\n                        //                |  \nawait readable.read()   // resume   ------+\n```\n\n## select\n\nThis library provides a simple channel `select` function similar to multi channel select found in the Go programming language. It allows multiple `Reader\u003cT\u003e` types to be combined into a singular stream.\n\n```typescript\nimport { channel, select } from 'corsa'\n\nfunction strings() {\n  const { readable, writable } = channel\u003cstring\u003e()\n  setInterval(() =\u003e writable.write('hello world'), 100)\n  return readable\n}\n\nfunction numbers() {\n  const { readable, writable } = channel\u003cnumber\u003e()\n  setInterval(() =\u003e writable.write(Math.random()), 200)\n  return readable\n}\n\nasync function start() {\n  const readable = select(strings(), numbers())\n  for await (const value of readable) {\n    console.log(value)\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinclairzx81%2Fcorsa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsinclairzx81%2Fcorsa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinclairzx81%2Fcorsa/lists"}