{"id":15447918,"url":"https://github.com/hazae41/cascade","last_synced_at":"2025-07-27T20:06:49.825Z","repository":{"id":136123465,"uuid":"607219349","full_name":"hazae41/cascade","owner":"hazae41","description":"Never let streams give you a headache again","archived":false,"fork":false,"pushed_at":"2024-03-13T16:06:31.000Z","size":236,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-10T14:47:23.088Z","etag":null,"topics":["esmodules","javascript","streams","streams-api","typescript"],"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-02-27T15:01:02.000Z","updated_at":"2024-03-09T19:30:52.000Z","dependencies_parsed_at":"2023-11-23T13:40:27.344Z","dependency_job_id":"88a4182b-2139-46fd-9fe3-976e7dc0651e","html_url":"https://github.com/hazae41/cascade","commit_stats":null,"previous_names":[],"tags_count":49,"template":false,"template_full_name":null,"purl":"pkg:github/hazae41/cascade","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazae41%2Fcascade","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazae41%2Fcascade/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazae41%2Fcascade/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazae41%2Fcascade/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hazae41","download_url":"https://codeload.github.com/hazae41/cascade/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazae41%2Fcascade/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267417664,"owners_count":24083839,"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-07-27T02:00:11.917Z","response_time":82,"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":["esmodules","javascript","streams","streams-api","typescript"],"created_at":"2024-10-01T20:21:02.611Z","updated_at":"2025-07-27T20:06:49.802Z","avatar_url":"https://github.com/hazae41.png","language":"TypeScript","readme":"# Cascade\n\nNever let streams give you a headache again\n\n```bash\nnpm i @hazae41/cascade\n```\n\n[**Node Package 📦**](https://www.npmjs.com/package/@hazae41/cascade)\n\n## Why\n\nIt took me 1 year to manage JavaScript streams in such a way as to avoid headaches, fortunately you can skip this struggle and never get any headache (and even avoid Safari bugs!)\n\n## Features\n\n### Current features\n- 100% TypeScript and ESM\n- No external dependencies\n- No need for controller access\n- Simplex, Half-Duplex, Full-Duplex\n- Use Symbol.dispose to close streams\n\n## Usage\n\n### Streams\n\nFor basic scenarios, Cascade provides streams but with an accessible controller\n\n```typescript\nconst encoder = new SuperTransformStream\u003cUint8Array, Uint8Array\u003e({ write: onWrite })\n\nasync function onWrite(chunk: Uint8Array) {\n  /** \n   * No need to get the controller \n   */\n  await stream.enqueue(encode(chunk))\n}\n\nexample.readable\n  .pipeTo(encoder.substream.writable)\n  .then(() =\u003e console.log(\"Closed\"))\n  .catch(e =\u003e console.error(\"Errored\", e))\n\nencoder.substream.readable\n  .pipeTo(example.writable)\n  .then(() =\u003e console.log(\"Closed\"))\n  .catch(e =\u003e console.error(\"Errored\", e))\n\n/**\n * You can close it at any time\n */\nencoder.terminate()\n```\n\n\n### Plexes\n\nFor more complex scenarios, Cascade provides plexes, which are streams with events, and you can associate them to build complex and reliable structures\n\n#### Simplex\n\nA basic in-out stream with events\n\n```tsx\nconst simplex = new Simplex\u003cUint8Array\u003e()\n\n/**\n * You can use pipes\n */\nexample.readable\n  .pipeTo(simplex.writable)\n  .then(() =\u003e console.log(\"Closed\"))\n  .catch(e =\u003e console.error(\"Errored\", e))\n\nsimplex.readable\n  .pipeTo(example.writable)\n  .then(() =\u003e console.log(\"Closed\"))\n  .catch(e =\u003e console.error(\"Errored\", e))\n\n/**\n * You can also use events\n */\nconst simplex = new Simplex\u003cUint8Array\u003e({\n  /**\n   * When the simplex starts\n   */\n  async start() {\n    this.enqueue(new Uint8Array([0, 1, 2]))\n  },\n  /**\n   * When the simplex is closing\n   */\n  async close() {\n    this.enqueue(new Uint8Array([7, 8, 9]))\n  },\n  /**\n   * When the simplex is erroring\n   */\n  async error(error) {\n    console.log(\"Errored\", error)\n  },\n  /**\n   * When the simplex receives chunks\n   */\n  async write(chunk) {\n    /**\n     * Pass the chunk to the readable side\n     */\n    this.enqueue(chunk)\n  },\n})\n\n/**\n * You can use enqueue at any time\n */\nsimplex.enqueue(new Uint8Array([4, 5, 6]))\n\n/**\n * You can use error at any time\n */\nsimplex.error(new Error())\n\n/**\n * You can use close at any time\n */\nsimplex.close()\n\n/**\n * You can check if it's closed\n */\nsimplex.closed\n\n/**\n * You can check if it's errored\n */\nsimplex.errored\n\n/**\n * You can check if it's closed or errored\n */\nsimplex.stopped\n```\n\n#### Full-Duplex\n\nA pair of simplexes that are closed independently\n\n- When one side is errored, the other is automatically errored\n- When one side is closed, the other is NOT automatically closed\n\nEvents\n- error — called ONCE when input OR output are errored\n- close — called ONCE when input AND output are closed\n\n```tsx\nclass Crypter extends FullDuplex\u003cUint8Array, Uint8Array\u003e {\n\n  constructor() {\n    super({\n      input: { message: m =\u003e this.#onInputMessage(m) },\n      output: { message: m =\u003e this.#onOutputMessage(m) },\n      close: () =\u003e this.#onClose(),\n      error: e =\u003e this.#onError(e)\n    })\n  }\n\n  async #onInputMessage(data: Uint8Array) {\n    this.input.enqueue(await encrypt(data))\n  }\n\n  async #onOutputMessage(data: Uint8Array) {\n    this.output.enqueue(await decrypt(data))\n  }\n\n  async #onError(reason?: unknown) {\n    console.error(\"Errored\", reason)\n  }\n\n  async #onClose() {\n    console.log(\"Closed\")\n  }\n\n}\n\nfunction crypt(subprotocol: FullDuplex\u003cUint8Array, Uint8Array\u003e) {\n  const crypter = new Crypter()\n\n  subprotocol.outer.readable.pipeTo(crypter.inner.writable).catch(() =\u003e { })\n  crypter.inner.readable.pipeTo(subprotocol.outer.writable).catch(() =\u003e { })\n\n  return crypter\n}\n```\n\n\n#### Half-Duplex\n\nA pair of simplexes that are closed together\n\n- When one side is errored, the other is automatically errored\n- When one side is closed, the other is automatically closed\n\nEvents\n- error — called ONCE when input OR output are errored\n- close — called ONCE when input OR output are closed\n\n```tsx\nclass MySocket extends EventTarget {\n\n  readonly #duplex = new HalfDuplex\u003cstring, string\u003e({\n    input: { message: m =\u003e this.#onMessage(m) },\n    error: e =\u003e this.#onError(e),\n    close: () =\u003e this.#onClose(),\n  })\n\n  get inner() {\n    return this.#duplex.inner\n  }\n\n  send(message: string) {\n    this.#duplex.output.enqueue(message)\n  }\n\n  error(reason?: unknown) {\n    this.#duplex.output.error(reason)\n  }\n\n  close() {\n    this.#duplex.output.close()\n  }\n\n  async #onMessage(data: string) {\n    this.dispatchEvent(new MessageEvent(\"message\", { data }))\n  }\n\n  async #onError(reason?: unknown) {\n    this.dispatchEvent(new ErrorEvent(\"error\", { error: reason }))\n  }\n\n  async #onClose() {\n    this.dispatchEvent(new Event(\"close\"))\n  }\n\n}\n```\n\n","funding_links":["https://github.com/sponsors/hazae41","https://patreon.com/hazae41"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhazae41%2Fcascade","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhazae41%2Fcascade","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhazae41%2Fcascade/lists"}