{"id":18401379,"url":"https://github.com/dozyio/js-buffered-channel","last_synced_at":"2025-04-12T17:33:58.209Z","repository":{"id":259334822,"uuid":"875847213","full_name":"dozyio/js-buffered-channel","owner":"dozyio","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-24T00:13:56.000Z","size":446,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-24T14:14:35.544Z","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/dozyio.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,"publiccode":null,"codemeta":null}},"created_at":"2024-10-21T00:47:11.000Z","updated_at":"2024-10-24T00:13:59.000Z","dependencies_parsed_at":"2024-10-24T14:34:09.153Z","dependency_job_id":"f9b372e3-6221-4353-80c6-c7aa9c66960d","html_url":"https://github.com/dozyio/js-buffered-channel","commit_stats":null,"previous_names":["dozyio/js-buffered-channel"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dozyio%2Fjs-buffered-channel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dozyio%2Fjs-buffered-channel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dozyio%2Fjs-buffered-channel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dozyio%2Fjs-buffered-channel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dozyio","download_url":"https://codeload.github.com/dozyio/js-buffered-channel/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248605132,"owners_count":21132118,"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-06T02:38:40.639Z","updated_at":"2025-04-12T17:33:58.185Z","avatar_url":"https://github.com/dozyio.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BufferedChannel\n\nBufferedChannel is a robust TypeScript library designed to facilitate reliable and efficient message passing between the main thread and worker threads in both **Node.js** and **browser** environments. Leveraging **semaphores** for concurrency control, BufferedChannel ensures backpressure management, acknowledgment handling, and error management, making it ideal for high-throughput applications.\n\n## Architecture\n\nBufferedChannel operates by establishing a communication channel between the main thread and worker threads using `MessagePort`. It leverages semaphores to manage the number of concurrent messages being sent, ensuring that the system can handle backpressure gracefully.\n\n## Installation\n\nInstall BufferedChannel via npm:\n\n```bash\nnpm install buffered-channel\n```\n\nOr using Yarn:\n\n```bash\nyarn add buffered-channel\n```\n\n## Usage\n\nBufferedChannel can be used in both **Node.js** and **browser** environments. See the [example](example) folder for a browser example.\n\n## API Reference\n\n### BufferedChannel\n\nA class that manages the sending and receiving of messages between the main thread and worker threads with concurrency control and acknowledgment handling.\n\n#### Constructor\n\n```typescript\nconstructor(\n  port: MessagePortLike,\n  bufferSize: number = 1,\n  opts: BufferedChannelOpts = {}\n)\n```\n\n- **port**: The `MessagePort` to use for communication.\n- **bufferSize**: The maximum number of concurrent send operations.\n- **opts**: Optional configuration options.\n  - **debug**: Enable debug logging.\n  - **name**: Name of the channel for logging purposes.\n  - **throwOnError**: Whether to throw errors or log them.\n\n#### Methods\n\n- **sendData**\n\n  Sends a data message through the channel with backpressure control.\n\n  ```typescript\n  async sendData(\n    message: DataMessage\u003cT\u003e,\n    transfer: Transferable[] = [],\n    timeout?: number | null\n  ): Promise\u003cvoid\u003e\n  ```\n\n  - **message**: The data message to send.\n  - **transfer**: Transferable objects (e.g., `ArrayBuffer`) to transfer ownership.\n  - **timeout**: Optional timeout in milliseconds for the send operation.\n\n- **sendAck**\n\n  Sends an acknowledgment message through the channel.\n\n  ```typescript\n  async sendAck(\n    ack: AckMessage,\n    transfer: Transferable[] = []\n  ): Promise\u003cvoid\u003e\n  ```\n\n  - **ack**: The acknowledgment message to send.\n  - **transfer**: Transferable objects to transfer ownership.\n\n- **receive**\n\n  An asynchronous generator that yields incoming data messages.\n\n  ```typescript\n  get receive(): AsyncIterableIterator\u003cDataMessage\u003cT\u003e\u003e\n  ```\n\n- **logPerformanceMetrics**\n\n  Logs performance metrics related to message sending and acknowledgment handling.\n\n  ```typescript\n  logPerformanceMetrics(): void\n  ```\n\n#### Properties (Public Getters)\n\n- **sentMessagesCountPublic**: `number`  \n  Number of messages sent.\n\n- **receivedAcksCountPublic**: `number`  \n  Number of acknowledgments received.\n\n- **totalLatencyPublic**: `number`  \n  Total latency accumulated from message sending to acknowledgment receipt.\n\n- **errorCountPublic**: `number`  \n  Number of errors encountered during message processing.\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdozyio%2Fjs-buffered-channel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdozyio%2Fjs-buffered-channel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdozyio%2Fjs-buffered-channel/lists"}