{"id":18326484,"url":"https://github.com/ghosind/node-dynamic-buffer","last_synced_at":"2025-04-13T09:53:08.290Z","repository":{"id":63139966,"uuid":"559830884","full_name":"ghosind/node-dynamic-buffer","owner":"ghosind","description":"An automatically resizing storage size buffer type for Node.js that is based on Node.js builtin Buffer.","archived":false,"fork":false,"pushed_at":"2025-03-14T06:58:14.000Z","size":998,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-06T16:40:02.462Z","etag":null,"topics":["buffer","dynamic-buffer","node-package","nodejs","npm-package","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/dynamic-buffer","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/ghosind.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2022-10-31T07:24:38.000Z","updated_at":"2025-03-14T06:58:15.000Z","dependencies_parsed_at":"2025-03-14T07:28:46.625Z","dependency_job_id":"4f275aae-831a-40ee-ac92-aa2fea60ac59","html_url":"https://github.com/ghosind/node-dynamic-buffer","commit_stats":{"total_commits":102,"total_committers":3,"mean_commits":34.0,"dds":"0.019607843137254943","last_synced_commit":"471f7a734871e8033651960a7c0a0e855b53633b"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghosind%2Fnode-dynamic-buffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghosind%2Fnode-dynamic-buffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghosind%2Fnode-dynamic-buffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghosind%2Fnode-dynamic-buffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ghosind","download_url":"https://codeload.github.com/ghosind/node-dynamic-buffer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248695304,"owners_count":21146952,"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":["buffer","dynamic-buffer","node-package","nodejs","npm-package","typescript"],"created_at":"2024-11-05T19:07:03.311Z","updated_at":"2025-04-13T09:53:08.269Z","avatar_url":"https://github.com/ghosind.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dynamic Buffer for Node.js\n\n[![Latest version](https://img.shields.io/github/v/release/ghosind/node-dynamic-buffer?include_prereleases)](https://github.com/ghosind/node-dynamic-buffer)\n[![NPM Package](https://img.shields.io/npm/v/dynamic-buffer)](https://www.npmjs.com/package/dynamic-buffer)\n[![Github Actions build](https://img.shields.io/github/actions/workflow/status/ghosind/node-dynamic-buffer/test.yaml?branch=main)](https://github.com/ghosind/node-dynamic-buffer)\n[![codecov](https://codecov.io/gh/ghosind/node-dynamic-buffer/branch/main/graph/badge.svg)](https://codecov.io/gh/ghosind/node-dynamic-buffer)\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/210eeb335c0a441ea463872fcd4b4569)](https://www.codacy.com/gh/ghosind/node-dynamic-buffer/dashboard?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=ghosind/node-dynamic-buffer\u0026amp;utm_campaign=Badge_Grade)\n[![License](https://img.shields.io/github/license/ghosind/node-dynamic-buffer)](https://github.com/ghosind/node-dynamic-buffer)\n\nAn automatically resizing storage size buffer type for Node.js that is based on Node.js builtin [`Buffer`](https://nodejs.org/api/buffer.html).\n\n## Installation\n\nYou can install this library by npm with the following command:\n\n```shell\nnpm install dynamic-buffer\n```\n\nYou can also install by yarn or other npm alternative package managers:\n\n```shell\nyarn add dynamic-buffer\n```\n\n## Getting Started\n\n1. Imports `DynamicBuffer` from `dynamic-buffer` package:\n\n  ```ts\n  import { DynamicBuffer } from 'dynamic-buffer';\n  ```\n\n2. Creates `DynamicBuffer` instance with default initial size or the specific size:\n\n  ```ts\n  const buffer = new DynamicBuffer();\n  ```\n\n3. Appends data into buffer:\n\n  ```ts\n  buffer.append('Hello ');\n  buffer.append('world!');\n  ```\n\n4. Exports data to string or builtin buffer object without unused bytes:\n\n  ```ts\n  console.log(buffer.length);\n  // 12\n  const buf = buffer.toBuffer();\n  console.log(buf.toString());\n  // Hello world!\n  const str = buffer.toString();\n  console.log(str);\n  // Hello world!\n  ```\n\n## Table of Contents\n\n- [Installation](#installation)\n\n- [Getting Started](#getting-started)\n\n- [Usages](#usages)\n\n  - [Write Data](#write-data)\n\n  - [Read Data](#read-data)\n\n  - [Iteration](#iteration)\n\n  - [Search](#search)\n\n  - [Comparison](#comparison)\n\n  - [Export Data](#export-data)\n\n  - [Utils](#utils)\n\n- [Run Tests](#run-tests)\n\n- [License](#license)\n\n## Usages\n\n### Write Data\n\nYou can write a string into a buffer by `append` method, it'll add the string to the end of the buffer:\n\n```ts\nbuf.append('Hello');\nbuf.append(' ');\nbuf.append('world');\nconsole.log(buf.toString());\n// Hello world\n```\n\nAnd you can also set the string length to write. Like the second line of the following example, it'll write `'Script!'` into buffer and without the last two `'!'` symbols:\n\n```ts\nbuf.append('Java');\nbuf.append('Script!!!', 7);\nconsole.log(buf.toString());\n// JavaScript!\n```\n\nYou can also use `write` method to write data to the specified position in the buffer:\n\n```ts\nbuf.append('Hello world!');\nbuf.write('Node.js', 6);\nconsole.log(buf.toString());\n// Hello Node.js\n```\n\n### Read Data\n\nYou can access the byte at the specified position in the buffer by `read` or `at` methods:\n\n```ts\nconst buf = new DynamicBuffer('Hello world');\n\nbuf.at(0); // 72\nbuf.read(1); // 101\n```\n\n### Iteration\n\n`DynamicBuffer` provides three ways to iterate data from the specified buffer, you can use them with `for...of` statement.\n\n- `entries()` returns an iterator of key-value pair (index and byte) from the buffer.\n\n  ```ts\n  buf.append('Hello');\n  for (const pair of buf.entries()) {\n    console.log(pair);\n  }\n  // [ 0, 72 ]\n  // [ 1, 101 ]\n  // [ 2, 108 ]\n  // [ 3, 108 ]\n  // [ 4, 111 ]\n  ```\n\n- `values()` returns an iterator of data(byte) from the buffer.\n\n  ```ts\n  buf.append('Hello');\n  for (const value of buf.values()) {\n    console.log(value);\n  }\n  // 72\n  // 101\n  // 108\n  // 108\n  // 111\n  ```\n\n- `keys()` returns an iterator of buffer keys (indices).\n\n### Search\n\nYou can search a value in the buffer by `indexOf` or `lastIndexOf`, and get the position of the first/last occurrence in the buffer. The searching value can be a string, a number, a `Buffer`, an `Uint8Array`, or another `DynamicBuffer`.\n\n```ts\nbuf.append('ABCABCABC');\nbuf.indexOf('ABC'); // 0\nbuf.lastIndexOf('ABC'); // 6\nbuf.indexOf('abc'); // -1\n```\n\n### Comparison\n\nYou can compare `DynamicBuffer` object with another `DynamicBuffer` object, Node.js builtin `Buffer` object, or an `Uint8Array` by `compare` or `equals` methods.\n\nFor `compare` method, it returns a number to indicate whether the buffer comes before, after, or is the same as another buffer in sort order.\n\n```ts\nbuf.append('ABC');\nconsole.log(buf.compare(Buffer.from('ABC')));\n// 0\nconsole.log(buf.compare(Buffer.from('BCD')));\n// -1\n```\n\nFor `equals` method, it returns a boolean value to indicate whether the buffer is the same as the target buffer.\n\n```ts\nbuf.append('ABC');\nconsole.log(buf.equals(Buffer.from('ABC')));\n// true\nconsole.log(buf.equals(Buffer.from('BCD')));\n// false\n```\n\n### Export Data\n\nYou can export buffer content (without unused parts) to string, `Buffer` object, or JSON representation object.\n\n```ts\nbuf.append('Hello');\nconsole.log(buf.toString());\n// Hello\nconst dataBuffer = buf.toBuffer();\nconsole.log(buf.length, dataBuffer.toString());\n// 5 Hello\nconsole.log(JSON.stringify(buf)); // JSON.stringify implicitly calls toJSON method.\n// {\"type\":\"Buffer\",\"data\":[72,101,108,108,111]}\n```\n\nFor `toString` and `toBuffer` methods, you can also set the start and end offsets to export the subset of written data.\n\n```ts\nbuf.append('Hello world!!!');\nconsole.log(buf.toString('utf8', 6, 11));\n// world\n```\n\n### Utils\n\nWe provided `isDynamicBuffer` function to indicating an object is a DynamicBuffer object or not.\n\n```ts\nimport { isDynamicBuffer } from 'dynamic-buffer';\n\nconst buf1 = Buffer.alloc(8);\nconst buf2 = new DynamicBuffer();\n\nisDynamicBuffer(buf1); // false\nisDynamicBuffer(buf2); // true\n```\n\n## Run Tests\n\nAll of test cases are written with `mocha`, `assert`, and `nyc`. They can be run with the following commands:\n\n```sh\nnpm test\n# or\nyarn test\n```\n\n## License\n\nThis project was published under MIT license, you can see more detail in [LICENSE file](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghosind%2Fnode-dynamic-buffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fghosind%2Fnode-dynamic-buffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghosind%2Fnode-dynamic-buffer/lists"}