{"id":23128203,"url":"https://github.com/victorqueiroz/ringbud","last_synced_at":"2026-05-04T08:38:31.522Z","repository":{"id":200415900,"uuid":"705447887","full_name":"VictorQueiroz/ringbud","owner":"VictorQueiroz","description":"A solid implementation of a general-purpose RingBuffer, featuring support for any kind of JavaScript `TypedArray` derivative. 💍","archived":false,"fork":false,"pushed_at":"2023-10-16T05:18:28.000Z","size":143,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T19:05:19.411Z","etag":null,"topics":["browser","memory-efficient","nodejs","ring-buffer","ringbuffer","typedarray","typescript"],"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/VictorQueiroz.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,"governance":null}},"created_at":"2023-10-16T02:51:30.000Z","updated_at":"2025-01-03T00:04:46.000Z","dependencies_parsed_at":"2023-10-16T22:23:52.256Z","dependency_job_id":null,"html_url":"https://github.com/VictorQueiroz/ringbud","commit_stats":null,"previous_names":["victorqueiroz/marriage"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VictorQueiroz%2Fringbud","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VictorQueiroz%2Fringbud/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VictorQueiroz%2Fringbud/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VictorQueiroz%2Fringbud/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VictorQueiroz","download_url":"https://codeload.github.com/VictorQueiroz/ringbud/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247128738,"owners_count":20888234,"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":["browser","memory-efficient","nodejs","ring-buffer","ringbuffer","typedarray","typescript"],"created_at":"2024-12-17T09:17:43.497Z","updated_at":"2025-10-16T21:17:41.684Z","avatar_url":"https://github.com/VictorQueiroz.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RingBud\n\n\u003e A lightweight, high-performance Ring Buffer for streaming data using JavaScript `TypedArray`s.\n\n## Features\n\n- 🧠 **Frame-based buffering** (configurable frame size)\n- ⚡ **Zero dependencies**\n- 🧵 **Supports all major TypedArrays** (e.g. `Float32Array`, `Uint8Array`, etc.)\n- 📦 **Memory efficient** with optional frame trimming\n- 🔁 **Sync \u0026 async iteration support**\n- ✅ **Fully tested** and predictable behavior\n- 🧰 **Customizable preallocation and cache options**\n\n---\n\n## Installation\n\n```bash\nnpm install ringbud\n```\n\n---\n\n## Quick Start\n\n```ts\nimport { RingBufferU8 } from \"ringbud\";\n\n// Create a ring buffer with frame size of 100\nconst rb = new RingBufferU8(100);\n\n// Write 50 bytes (not enough for a full frame)\nrb.write(new Uint8Array(50).fill(1));\nconsole.log(rb.empty()); // true\n\n// Write 50 more bytes (now we have a complete frame)\nrb.write(new Uint8Array(50).fill(1));\nconsole.log(rb.empty()); // false\n\n// Read one frame of 100 bytes\nconst frame = rb.read();\nconsole.log(frame); // Uint8Array(100)\n\n// After reading, it becomes empty again\nconsole.log(rb.empty()); // true\n```\n\n---\n\n## Supported Types\n\nYou can instantiate ring buffers for:\n\n- `Uint8Array` → `RingBufferU8`\n- `Uint16Array` → `RingBufferU16`\n- `Float32Array` → `RingBufferF32`\n\nEach subclass wraps the base `RingBufferBase` with preconfigured types.\n\n---\n\n## Configuration Options\n\nAll constructors accept:\n\n```ts\n{\n  frameSize: number,                // Number of elements per frame (required)\n  preallocateFrameCount?: number,  // Default: 10\n  frameCacheSize?: number          // Default: 0 (no trim)\n}\n```\n\n### Frame Cache Size (Clamping)\n\nWhen `frameCacheSize \u003e 0`, the ring buffer trims memory usage by shifting unread bytes after every `.read()`. This reduces buffer growth at the cost of additional memory copying.\n\n---\n\n## API Reference\n\n### Constructor\n\n```ts\nnew RingBufferU8(frameSize: number, options?: {\n  preallocateFrameCount?: number;\n  frameCacheSize?: number;\n});\n```\n\n### Methods\n\n| Method              | Description |\n|---------------------|-------------|\n| `write(data)`       | Appends a `TypedArray` to the buffer |\n| `read()`            | Returns the next full frame, or `null` |\n| `drain()`           | Returns remaining **incomplete** data |\n| `peek()`            | Returns the entire buffer content (not a copy) |\n| `empty()`           | `true` if no full frame is available |\n| `remainingFrames()` | Number of full frames available to read |\n| `rewind()`          | Resets read offset so frames can be re-read |\n| `Symbol.iterator()` | Enables `for (const frame of buffer)` |\n| `Symbol.asyncIterator()` | Enables `for await (const frame of buffer)` |\n\n---\n\n## Example: Iteration\n\n```ts\nfor (const frame of rb) {\n  console.log(frame); // each is a complete frame\n}\n\n// or async\nfor await (const frame of rb) {\n  await process(frame);\n}\n```\n\n---\n\n## Example: Auto-Trimming\n\n```ts\nconst rb = new RingBufferU8(100, { frameCacheSize: 1 });\n\nrb.write(new Uint8Array(300)); // 3 frames\nrb.read();                     // returns 1st frame\n\n// Buffer automatically shifts remaining frames to the front\nrb.peek().subarray(0, 200);    // contains frame 2 and 3\n```\n\n---\n\n## Validations \u0026 Safety\n\n- `frameSize` must be an integer ≥ 1\n- `preallocateFrameCount` must be ≥ 1 (if set)\n- Partial frames are never returned from `.read()` or iterators\n- Trimming only occurs **after** reads when `frameCacheSize \u003e 0`\n- If iteration is used, all frames are consumed as if `.read()` was called repeatedly\n- Frames can be shared or copied depending on cache config\n\n---\n\n## TypedArray Support\n\nInternally, the base class accepts any `TypedArray` constructor:\n\n```ts\nnew RingBufferBase({\n  frameSize: 256,\n  TypedArrayConstructor: Uint16Array\n});\n```\n\nBuilt-in classes like `RingBufferF32` are wrappers over this API.\n\n---\n\n## Examples\n\n### Draining Partial Data\n\n```ts\nconst rb = new RingBufferU8(100);\n\nrb.write(new Uint8Array(230));\nrb.read();           // reads 1 frame (100 bytes)\nrb.read();           // reads 1 more frame (100 bytes)\nrb.read();           // null (30 bytes left)\n\nrb.drain();          // returns 30 bytes\n```\n\n### Rewind\n\n```ts\nrb.rewind();         // enables re-reading all written frames\nfor (const frame of rb) {\n  console.log(frame);\n}\n```\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvictorqueiroz%2Fringbud","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvictorqueiroz%2Fringbud","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvictorqueiroz%2Fringbud/lists"}