{"id":19178058,"url":"https://github.com/cmdruid/buff","last_synced_at":"2026-03-10T15:03:57.821Z","repository":{"id":62239671,"uuid":"558502322","full_name":"cmdruid/buff","owner":"cmdruid","description":"The swiss-army-knife of byte manipulation.","archived":false,"fork":false,"pushed_at":"2024-01-30T00:41:34.000Z","size":2152,"stargazers_count":6,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-24T07:53:17.510Z","etag":null,"topics":["base58","base64","bech32","binary","buffer","convert","format","ripemd160","sha256"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cmdruid.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":"2022-10-27T17:15:08.000Z","updated_at":"2024-11-12T04:15:30.000Z","dependencies_parsed_at":"2023-02-05T16:47:03.210Z","dependency_job_id":"76918a38-846c-452a-b7b6-0781da6bdc28","html_url":"https://github.com/cmdruid/buff","commit_stats":null,"previous_names":["cmdruid/bytes-utils","cmdruid/buff","cmdruid/buff-utils"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cmdruid/buff","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmdruid%2Fbuff","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmdruid%2Fbuff/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmdruid%2Fbuff/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmdruid%2Fbuff/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cmdruid","download_url":"https://codeload.github.com/cmdruid/buff/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmdruid%2Fbuff/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30338599,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T15:02:55.010Z","status":"ssl_error","status_checked_at":"2026-03-10T15:02:36.911Z","response_time":106,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["base58","base64","bech32","binary","buffer","convert","format","ripemd160","sha256"],"created_at":"2024-11-09T10:36:43.805Z","updated_at":"2026-03-10T15:03:57.795Z","avatar_url":"https://github.com/cmdruid.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# buff\n\nThe swiss-army-knife of byte manipulation.\n\nFeatures:\n * Move between data formats with ease!\n * Encode / decode between Base58, Base64, Bech32 and more.\n * `Buff` object recognized as native `Uint8Array`.\n * Prepend, append, sort, split and join arrays of multiple formats.\n * Read and prefix varints.\n * Convert blobs of data into consumable streams.\n * Uses `DataView.setUint8` for ultra-fast performace.\n * Supports endianess for all the things!\n\n## How to Import\n\nThis library is designed to support classic and modern ESM imports, in both a nodejs and browser environment.\n\nExample install via NPM or yarn:\n\n```bash\nnpm install @cmdcode/buff || yarn add @cmdcode/buff\n```\n\nClassic import into a nodejs project:\n\n```ts\nconst { Buff, Bytes } = require('@cmdcode/buff')\n```\n\nModern import into an nodejs project:\n\n```ts\nimport { Buff, Bytes } from '@cmdcode/buff'\n```\n\nClassic import into a browser-based project:\n\n```html\n\u003cscript src=\"https://unpkg.com/@cmdcode/buff/dist/browser.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  const { Buff, Bytes } = window.buff\n\u003c/script\u003e\n```\n\nModern import into a browser-based project:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import { Buff, Bytes } from \"https://unpkg.com/@cmdcode/buff/dist/module.mjs\" \n\u003c/script\u003e\n```\n\n## How to Use\n\nThe `Buff` class is an extention of the base `Uint8Array` class. It provides the same default functionality of a Uint8Array, and can be used as a drop-in replacement for Uint8Array. Typescript will treat Buff as a Uint8Array object.\n\n```ts\nimport { Buff, Bytes } from '@cmdcode/buff'\n\n// Bytes covers value types that are convertable to Uint8Array.\ntype Bytes  = string | number | bigint | Uint8Array | Buff\n// You can optionally specify the endianess of data.\ntype Endian = 'le' | 'be'\n\nconst bytes = new Buff (\n  data    : Bytes | Bytes[] | ArrayBuffer,  \n  size   ?: number, // Specify the size of the array (for padding)\n  endian ?: Endian  // Specify the endianess of the array.\n)\n\n```\nYou can convert from many different types and formats into a `Buff` object.\n\n```ts\nBuff\n  .any     = (data : any, size ?: number)        =\u003e Buff,\n  .raw     = (data : Uint8Array, size ?: number) =\u003e Buff,\n  .str     = (data : string, size ?: number)     =\u003e Buff,\n  .hex     = (data : string, size ?: number)     =\u003e Buff,\n  .bin     = (data : string, size ?: number)     =\u003e Buff,\n  .num     = (data : number, size ?: number)     =\u003e Buff,\n  .big     = (data : bigint, size ?: number)     =\u003e Buff,\n  .bytes   = (data : Bytes,  size ?: number)     =\u003e Buff,\n  .json    = (data : T,  replacer ?: Replacer)   =\u003e Buff,\n  .b58chk  = (data : string)                     =\u003e Buff,\n  .base64  = (data : string)                     =\u003e Buff,\n  .b64url  = (data : string)                     =\u003e Buff,\n\n  .bech32  = (\n    data        : string,  // Data to be encoded\n    limit      ?: number,  // Enforce a character limit.\n    chk_prefix ?: string   // Enforce a certain prefix.\n  ) =\u003e Buff,\n\n  .bech32m = (\n    data        : string,  // Data to be encoded\n    limit      ?: number,  // Enforce a character limit.\n    chk_prefix ?: string   // Enforce a certain prefix.\n  ) =\u003e Buff\n}\n```\n\nWith `Buff`, you have access to an extensive API for converting between formats.\n\n```ts\nconst bytes = new Buff(data)\n\n/* Quicky convert into many formats using getters. */\n\nbytes\n  .arr     =\u003e number[]    // Convert to a number array.\n  .num     =\u003e number      // Convert to a Number.\n  .big     =\u003e bigint      // Convert to a BigInt.\n  .str     =\u003e string      // Convert to a UTF8 string.\n  .hex     =\u003e string      // Convert to a hex string.\n  .raw     =\u003e Uint8Array  // Convert to a pure Uint8Array.\n  .bin     =\u003e string      // Convert to a binary string.\n  .b58chk  =\u003e string      // Convert to base58 with checksum.\n  .base64  =\u003e string      // Convert to base64 string.\n  .b64url  =\u003e string      // Convert to base64url string.\n  .digest  =\u003e Buff        // Convert to a sha256 digest.\n  .id      =\u003e string      // Convert to a digest (hex string).\n  .stream  =\u003e Stream      // Convert to a Stream object.\n\n/* There are a few export methods that support extra params. */\n\nbytes\n  .toNum     : (endian ?: Endian)                  =\u003e number\n  .toBig     : (endian ?: Endian)                  =\u003e bigint\n  .toBin     : ()                                  =\u003e string\n  .toHash    : ()                                  =\u003e Buff\n  .toJson    : (reviver ?: Reviver)                =\u003e T\n  .toBech32  : (prefix : string, limit ?: number)  =\u003e string\n  .toBech32m : (prefix : string, limit ?: number)  =\u003e string\n```\n\nIn addition to format conversion, you can perform many other convenient tasks.\n\n```ts\nBuff = {\n  // Standard UTF-8 string-to-bytes encoding.\n  encode : (str : string) =\u003e Uint8Array,\n  // Standard UTF-8 bytes-to-string decoding.\n  decode : (bytes : Uint8Array) =\u003e string,\n  // Same as Uint8Array.from(), but returns a Buff object.\n  from (data : Uint8Array | number[]) =\u003e Buff\n  // Same as Uint8Array.of(), but returns a Buff object.\n  of (...data : number[]) =\u003e Buff,\n  // Join together multiple arrays of bytes.\n  join : (array : Bytes[]) =\u003e Buff,\n  // Sort multiple arrays of bytes in lexicographic order.\n  sort (arr : Bytes[], size ?: number) =\u003e Buff[],\n  // Return a buffer object with random data (uses webcrypto).\n  random (size : number) =\u003e Buff,\n  // Converts a number into a 'varint' for byte streams.\n  varInt : (num : number, endian ?: Endian) =\u003e Buff\n}\n\nconst bytes = new Buff(data)\n\nbytes\n  // Append data to your ubber object\n  .append (data : Bytes) =\u003e Buff\n  // Prepend data to your buffer object.\n  .prepend (data : Bytes) =\u003e Buff\n  // Encode the size of your buffer as a varint and prepend it.\n  .prefixSize (endian ?: Endian) =\u003e Buff\n  // Same as Uint8Array.reverse(), but returns a Buff object.\n  .reverse () =\u003e Buff\n  // Identical to Uint8Array.set() method.\n  .set (array : ArrayLike\u003cnumber\u003e, offset ?: number) =\u003e void\n  // Same as Uint8Array.slice(), but returns a Buff object.\n  .slice (start ?: number, end ?: number) =\u003e Buff\n  // Same as Uint8Array.subarray(), but returns a Buff object.\n  .subarray (begin ?: number, end ?: number) =\u003e Buff\n  // Same as Uint8Array.set(), but allows Bytes as input.\n  .write (data : Bytes, offset ?: number) =\u003e void\n```\n\nThe `Stream` tool will take a blob of data and allow you to consume it byte-per-byte.\n\n```ts\nimport { Stream } from '@cmdcode/buff'\n\n// Convert data into a stream object.\nconst stream = new Stream(data)\n\n// You can convert a buff object into a stream.\nconst stream = new Buff(data).stream\n\nstream\n  // Reads x number of bytes, does not consume the stream.\n  .peek(size: number) =\u003e Buff\n  // Reads x number of bytes, consumes the stream.\n  .read(size: number) =\u003e bytes\n  // Reads the next bytes(s) as a varint, returns the number value.\n  .readSize (endian ?: Endian) =\u003e number\n```\n\n## Dependencies\n\nThis library uses minimal dependences.\n\n**@scure/base**  \nFor performing encoding and decoding of many formats.  \nhttps://github.com/paulmillr/scure-base\n\n**@noble/hashes**\nFor creating hashes using `sha256`.  \nhttps://github.com/paulmillr/noble-hashes\n\nSpecial thanks to Paul Miller for his wonderful work.\n\n## Bugs / Issues\n\nPlease feel free to post any questions or bug reports on the issues page!\n\n## Development / Testing\n\nThis project uses `eslint` and `typescript` for development, `tape` for unit tests, and `rollup` for bundling releases.\n\n```bash\nyarn test    || npm run test\nyarn release || npm run release\n```\n\n## Contributions\n\nAll contributions are welcome!\n\n## License\n\nUse this code however you like! No warranty!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcmdruid%2Fbuff","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcmdruid%2Fbuff","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcmdruid%2Fbuff/lists"}