{"id":25094602,"url":"https://github.com/dlenroc/binary-decoder.js","last_synced_at":"2026-02-08T12:38:30.693Z","repository":{"id":255446983,"uuid":"852439518","full_name":"dlenroc/binary-decoder.js","owner":"dlenroc","description":"Incremental binary data parser powered by generators.","archived":false,"fork":false,"pushed_at":"2025-01-02T10:21:46.000Z","size":25,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T10:01:56.083Z","etag":null,"topics":[],"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/dlenroc.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-04T20:04:46.000Z","updated_at":"2025-01-02T10:20:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"74c17ccc-1587-4b37-9c73-21185a9edf42","html_url":"https://github.com/dlenroc/binary-decoder.js","commit_stats":null,"previous_names":["dlenroc/binary-decoder.js"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlenroc%2Fbinary-decoder.js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlenroc%2Fbinary-decoder.js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlenroc%2Fbinary-decoder.js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlenroc%2Fbinary-decoder.js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dlenroc","download_url":"https://codeload.github.com/dlenroc/binary-decoder.js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249189570,"owners_count":21227210,"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":"2025-02-07T15:28:21.868Z","updated_at":"2026-02-08T12:38:25.655Z","avatar_url":"https://github.com/dlenroc.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @dlenroc/binary-decoder · [![NPM Version](https://img.shields.io/npm/v/@dlenroc/binary-decoder)](https://www.npmjs.com/package/@dlenroc/binary-decoder)\n\nA library for implementing incremental binary data parsers using generators.\n\n## Installation\n\n```sh\nnpm install @dlenroc/binary-decoder\n```\n\n## Usage\n\nThe `BinaryDecoder` class enables streaming binary decoding using a generator\nfunction. It leverages `yield` to read bytes, return unused data, and produce\nthe decoded result.\n\n```ts\nimport { BinaryDecoder } from '@dlenroc/binary-decoder';\n\nconst decoder = new BinaryDecoder(function* () {\n  // ✨ N ≥ 0 | Read exactly N bytes.\n  const fixedLengthBytes: Uint8Array = yield 2;\n\n  // ✨ N \u003c 0 | Read at most |N| bytes, at least 1 byte.\n  const variableLengthBytes: Uint8Array = yield -2;\n\n  // ✨ ArrayBufferView | Pushback bytes to the internal buffer.\n  yield Uint8Array.of(40, 50);\n\n  // ✨ Other | Enqueue parsed data.\n  yield { fixedLengthBytes, variableLengthBytes, extraBytes: yield -Infinity };\n});\n\nconsole.log([...decoder.decode(Uint8Array.of(10, 20, 30))]);\n// [\n//   {\n//     fixedLengthBytes: Uint8Array(2) [ 10, 20 ],\n//     variableLengthBytes: Uint8Array(1) [ 30 ],\n//     extraBytes: Uint8Array(2) [ 40, 50 ]\n//   }\n// ]\n```\n\n## Examples\n\n### Stateful Parsing\n\nSuppose we need to parse a simple protocol defined as follows:\n\n| No. of bytes | Type [Value] | Description |\n| ------------ | ------------ | ----------- |\n| 4            | U32          | length      |\n| length       | U8 array     | text        |\n\n```ts\nimport type { Decoder } from '@dlenroc/binary-decoder';\nimport { BinaryDecoder, getUint32 } from '@dlenroc/binary-decoder';\n\nfunction* parse(): Decoder\u003cstring\u003e {\n  while (true) {\n    const length = yield* getUint32();\n    const bytes = yield length;\n\n    yield new TextDecoder().decode(bytes);\n  }\n}\n\nconst decoder = new BinaryDecoder(parse);\n\n// create a message\nconst textBytes = new TextEncoder().encode('Hello, World!');\nconst chunk = new Uint8Array([...new Uint8Array(4), ...textBytes]);\nnew DataView(chunk.buffer).setUint32(0, textBytes.byteLength);\n\n// [1] Parse a message\nconsole.log([...decoder.decode(chunk)]);\n// [ 'Hello, World!' ]\n\n// [2] Parse multiple messages\nconsole.log([...decoder.decode(Uint8Array.of(...chunk, ...chunk))]);\n// [ 'Hello, World!', 'Hello, World!' ]\n\n// [3] Parse a message split across chunks\nconsole.log([...decoder.decode(chunk.subarray(0, 3))]);\n// []\nconsole.log([...decoder.decode(chunk.subarray(3, 5))]);\n// []\nconsole.log([...decoder.decode(chunk.subarray(5))]);\n// [ 'Hello, World!' ]\n```\n\n### Handling Large Messages\n\nUpdate the decoder to stream decoded chunks directly, avoiding buffering.\n\n```ts\nimport { getUint32, streamBytes } from '@dlenroc/binary-decoder';\n\nfunction* parse(): Decoder\u003cTextDecoderStream\u003e {\n  while (true) {\n    const length = yield* getUint32();\n    const stream = new TextDecoderStream();\n    const writer = stream.writable.getWriter();\n\n    try {\n      yield stream;\n      yield* streamBytes(length, (chunk) =\u003e writer.write(chunk));\n    } finally {\n      writer.close();\n    }\n  }\n}\n\n// ... 👀 See previous example\n\n// [3] Parse a message split across chunks (streaming output)\nconsole.log([...decoder.decode(chunk.subarray(0, 3))]);\n// []\nconsole.log([...decoder.decode(chunk.subarray(3, 5))]);\n// [ TextDecoderStream { ... } ]\nconsole.log([...decoder.decode(chunk.subarray(5))]);\n// []\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdlenroc%2Fbinary-decoder.js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdlenroc%2Fbinary-decoder.js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdlenroc%2Fbinary-decoder.js/lists"}