{"id":22792161,"url":"https://github.com/craiggleso/binary-bitfield","last_synced_at":"2026-04-12T23:39:16.075Z","repository":{"id":72610465,"uuid":"79209115","full_name":"CraigglesO/binary-bitfield","owner":"CraigglesO","description":"Binary bitfield system to easily keep track of torrent pieces","archived":false,"fork":false,"pushed_at":"2019-08-15T01:13:34.000Z","size":133,"stargazers_count":0,"open_issues_count":7,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-05T23:41:12.813Z","etag":null,"topics":["binary","binary-bitfield","binary-data","bitfield","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CraigglesO.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":"2017-01-17T09:08:19.000Z","updated_at":"2017-02-19T00:25:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"7f09af54-8700-4b17-852a-8edb94f48ef2","html_url":"https://github.com/CraigglesO/binary-bitfield","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CraigglesO%2Fbinary-bitfield","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CraigglesO%2Fbinary-bitfield/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CraigglesO%2Fbinary-bitfield/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CraigglesO%2Fbinary-bitfield/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CraigglesO","download_url":"https://codeload.github.com/CraigglesO/binary-bitfield/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246351016,"owners_count":20763232,"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":["binary","binary-bitfield","binary-data","bitfield","typescript"],"created_at":"2024-12-12T03:09:57.003Z","updated_at":"2025-10-28T03:32:21.738Z","avatar_url":"https://github.com/CraigglesO.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# binary-bitfield [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url]\n\n[![Greenkeeper badge](https://badges.greenkeeper.io/CraigglesO/binary-bitfield.svg)](https://greenkeeper.io/)\n\n[travis-image]: https://travis-ci.org/CraigglesO/binary-bitfield.svg?branch=master\n[travis-url]: https://travis-ci.org/CraigglesO/binary-bitfield\n[npm-image]: https://img.shields.io/npm/v/binary-bitfield.svg\n[npm-url]: https://npmjs.org/package/binary-bitfield\n[downloads-image]: https://img.shields.io/npm/dm/binary-bitfield.svg\n[downloads-url]: https://npmjs.org/package/binary-bitfield\n\n### Binary bitfield system to easily keep track of torrent pieces\n\nMost bitfield systems are very simplistic. This one allows you to:\n\n- Read the hex code or binary code (binary is more useful IMO)\n- Know how much you have downloaded\n- Keep track of how many pieces you have access to\n  - Know where 'rare' pieces exist easily (ensure you can get all data)\n  - Know peers piece you need closes to index 0 (streaming)\n  - Get quick access to the total amount of each piece\n- Handles Buffers or Hex-strings as input\n- Truly Async to not bog down your application.\n\n* NEW : onHave function. Updates a peers hash with piece index\n\n\nbinaryBitfield(pieces [, downloaded])\n  - pieces: number of pieces total to download (number or Buffer or Hex String)\n  - downloaded: which pieces have already been downloaded. (Buffer or Hex String)\n\n## Install\n\n``` typescript\nnpm install binary-bitfield\n```\n\n## Usage\n``` typescript\nimport binaryBitfield from 'binary-bitfield';\n\n// Create bitfield with just a piece size:\nlet bitfield1 = new binaryBitfield(9);\n// binaryBitfield {\n//   pieces: 9,\n//   bitfield: '1111111110000000',\n//   totalBitfield: '0000000000000000',\n//   downloaded: '0000000000000000',\n//   percent: 0 }\n\n// Create bitfield with a hex input for both piece size and downloaded\nlet bitfield2 = new binaryBitfield('c0', '40');\n//binaryBitfield {\n//  pieces: 2,\n//  bitfield: '11000000',\n//  totalBitfield: '01000000',\n//  downloaded: '01000000',\n//  percent: 0.5 }\n\n\n// Keep track of the total bitfield as peers come in:\n\nlet buffer = Buffer.from('40', 'hex');\n\nbitfield2.findNewPieces(buffer, (result, total) =\u003e {\n  console.log('hex: ', result);\n  console.log('total: ', total);\n  console.log(bitfield);\n});\n\nhex: 00          // buffer or 'peer' had no new pieces\ntotal:  02000000 // a new totalBitfield count\n// binaryBitfield {\n//  pieces: 2,\n//  bitfield: '11000000',\n//  totalBitfield: '02000000',\n//  downloaded: '01000000',\n//  percent: 0.5 }\n\nbitfield.set(0)  // returns downloaded: '11000000'\n// binaryBitfield {\n//   pieces: 2,\n//   bitfield: '11000000',\n//   totalBitfield: '02000000',\n//   downloaded: '11000000',\n//   percent: 1 }\n\nbitfield.get(0) // true\n\n```\n\n\n\n## Variables and Functions:\n\n### Variables\n\npieces:        number\nbitfield:      string\ntotalBitfield: string\ndownloaded:    string\npercent:       number\n\n### Functions:\n\n`isSeeder(string | Buffer): Boolean`\nThis compares the bitfield to input. This quickly ensures the peer is a seeder.\nThe return is `true` if peer is seeder\n\n`getBitfield() : string`\nGet the bitfield.\n\n`binary2hex(binary: string): string`\nConvert a binary string to hex. This system reads the input at half byte (4 bit) interval.\n\n`hex2binary(hex: string): string`\nConvert a hex string to binary. This system reads the input at half byte (4 bit) interval.\n\n`getPercentage(): number`\nReturns the current percentage downloaded.\n\n**NEW** `onHave(piece: number, bitfield: string | Buffer): string`\nUpdates a peers hash with piece index and returns the new bitfield.\n\n\n## ISC License (Open Source Initiative)\n\nISC License (ISC)\nCopyright \u003c2017\u003e \u003cCraig OConnor\u003e\n\nPermission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcraiggleso%2Fbinary-bitfield","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcraiggleso%2Fbinary-bitfield","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcraiggleso%2Fbinary-bitfield/lists"}