{"id":24009520,"url":"https://github.com/harrydehix/easy-buffer","last_synced_at":"2025-07-31T14:38:46.328Z","repository":{"id":197763991,"uuid":"699289267","full_name":"harrydehix/easy-buffer","owner":"harrydehix","description":"Provides an intuitive (and typesafe) way to convert a buffer to any kind of data structure","archived":false,"fork":false,"pushed_at":"2024-07-18T15:52:13.000Z","size":143,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-08T03:58:55.785Z","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/harrydehix.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":"2023-10-02T10:37:09.000Z","updated_at":"2024-07-18T15:52:17.000Z","dependencies_parsed_at":null,"dependency_job_id":"ad2da0cb-50bf-4d46-88a8-09c319ecc06c","html_url":"https://github.com/harrydehix/easy-buffer","commit_stats":null,"previous_names":["harrydehix/smart-buffer-parser"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harrydehix%2Feasy-buffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harrydehix%2Feasy-buffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harrydehix%2Feasy-buffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harrydehix%2Feasy-buffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/harrydehix","download_url":"https://codeload.github.com/harrydehix/easy-buffer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240675942,"owners_count":19839441,"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-01-08T03:59:26.860Z","updated_at":"2025-02-25T13:30:01.491Z","avatar_url":"https://github.com/harrydehix.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# easy-buffer\n\nProvides a powerful interface to read primitives, arrays and tuples from a buffer and transform them after that.\n\n# Examples\n\nLet's get started quickly and learn from simple examples!\n\nEvery example expects an easy buffer instance. You can create one like this:\n\n```ts\nimport { EasyBuffer, Type } from \"easy-buffer\";\n\n// the nodejs buffer you want to read from\nconst buffer: Buffer = ...;\n\n// just pass the nodejs buffer to the easy buffer constructor\nconst easy = new Easybuffer(buffer);\n```\n\n### Example 1: Primitives\n\nTo read a primitive `INT32_BE` which is located at byte `16` write:\n\n```ts\nconst number = easy.read({ type: Type.INT32_BE, offset: 16 }).end();\n```\n\nTo read a `ascii` string which is located at byte `20` and is `15` bytes long write:\n\n```ts\nconst primitive = easy\n    .read({ type: Type.STRING(15, \"ascii\"), offset: 20 })\n    .end();\n```\n\n### Example 2: Arrays\n\nImagine your buffer is structured like this:\n\n![](./screenshots/2.jpg)\n\nAssume you want to convert this buffer into a simple `number[]`:\n\n```ts\nconst array = easy.read({ type: Type.ARRAY(Type.INT32_LE, 4) }).end();\n```\n\nTo parse an array we simply utilized the `Type.ARRAY(...)` method and passed our item's type.\n\n### Example 3: Arrays with gaps\n\nImagine your buffer is structured like this:\n\n![](./screenshots/3.jpg)\n\nAgain your buffer has an arrayish structure but there are some nasty gaps in there.\n\nI don't see any problem, just specify the gap's size in bytes🙃.\n\n```ts\nconst array = easy\n    .read({ type: Type.ARRAY(Type.INT32_LE, 4, 4), offset: 0 })\n    .end();\n```\n\n_Tip: It is possible to nest multiple Type.ARRAY(type, size, gap) calls to read arrays of higher dimensions!_\n\n### Example 4: Tuples\n\nIt is also possible to read tuples.\n\n```ts\nconst tuple = easy\n    .read({\n        type: Type.TUPLE_3(\n            Type.INT32_LE,\n            Type.STRING(3, \"ascii\"),\n            Type.FLOAT_LE\n        ),\n        offset: 5,\n    })\n    .end(); // this returns a tuple of type [number, string, number]\n```\n\n### Example 5: Transforming your data after it got parsed\n\nIt is also possible to transform your data after it got parsed to the specified `type`.\n\nTo do so call `.transform(val =\u003e ...)` before calling `.end()`. You can chain multiple transform calls.\n\n```ts\nconst primitive = easy\n    .read({ type: Type.STRING(15, \"ascii\"), offset: 20 })\n    .transform((str) =\u003e str.toUpperCase()) // this makes the string uppercase\n    .end();\n```\n\nIf you want to transform each item of an array individually you can call `.transformItem((val, index) =\u003e ...)`.\n\n```ts\nconst array = easy\n    .read({ type: Type.ARRAY(Type.INT32_LE), size: 4, gap: 4, offset: 0 })\n    .transformItem((val, index) =\u003e val.toFixed(2)) // converts each number to string\n    .end();\n```\n\nIf you want to transform your tuple (but still want it to be a tuple) call `.transformTuple(tuple =\u003e ...)`.\n\n```ts\nconst tuple = easy\n    .read({\n        type: Type.TUPLE_3(\n            Type.INT32_LE,\n            Type.STRING(3, \"ascii\"),\n            Type.FLOAT_LE\n        ),\n        offset: 5,\n    })\n    .transformTuple((tuple) =\u003e [\n        tuple[0] * 2,\n        tuple[1].toUpperCase(),\n        tuple[2].toFixed(2),\n    ])\n    .end(); // this returns a tuple of type [number, string, string]\n```\n\n### Example 6: Nulling values\n\nIn some of my projects specific values of an integer have a special meaning, e.g. `0x12A3` or `0x32A4` meant `'no signal'`. In these cases I wanted to return `null` instead. That's why this library offers multiple functions to automatically null the parsed result if certain conditions are met.\n\n`.nullIfEquals(...nullables)` nulls result if it matches one of the passed nullable.\n`.nullIfItemEquals(...nullables)` does the same but for arrays.\n`.nullIfTupleItemEquals([...nullables, ...nullables, ...])` does the same but for tuples.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharrydehix%2Feasy-buffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fharrydehix%2Feasy-buffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharrydehix%2Feasy-buffer/lists"}