# torrent-piece

> Torrent piece abstraction

Latest version **4.0.1** (published 2026-05-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install torrent-piece
pnpm add torrent-piece
yarn add torrent-piece
bun add torrent-piece
```

## Health

**Score 60/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; has provenance; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 4.0.1 |
| Published | 2026-05-26 |
| First published | 2015-08-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | >=12.20.0 |
| Dependencies | 1 |
| Unpacked size | 7.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 28 |
| Author | WebTorrent LLC |
| Maintainers | feross, mafintosh, flet, watson, diegorbaquero, hicom150, jhiesey, webtorrent-bot, alxhotel |
| Keywords | torrent, piece, chunk, webtorrent, torrent-stream, peerflix, bittorrent |

## Links

- npm: https://www.npmjs.com/package/torrent-piece
- Repository: https://github.com/webtorrent/torrent-piece
- Homepage: https://github.com/webtorrent/torrent-piece#readme
- Issues: https://github.com/webtorrent/torrent-piece/issues
- Funding: https://github.com/sponsors/feross
- npm.io page: https://npm.io/package/torrent-piece

## Dependencies (1)

- [uint8-util](https://npm.io/package/uint8-util.md) ^2.1.9

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 4.0.1 (latest) — 2026-05-26
- 4.0.0 — 2026-05-25
- 3.0.2 — 2025-08-03
- 3.0.1 — 2025-01-29
- 3.0.0 — 2023-07-23
- 2.0.1 — 2021-04-01
- 2.0.0 — 2018-07-26
- 1.1.2 — 2018-06-09
- 1.1.1 — 2017-04-14
- 1.1.0 — 2016-03-01
- 1.0.0 — 2015-08-19

## README

# torrent-piece [![ci][ci-image]][ci-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]

[ci-image]: https://img.shields.io/github/actions/workflow/status/webtorrent/torrent-piece/ci.yml
[ci-url]: https://github.com/webtorrent/torrent-piece/actions
[npm-image]: https://img.shields.io/npm/v/torrent-piece.svg
[npm-url]: https://npmjs.org/package/torrent-piece
[downloads-image]: https://img.shields.io/npm/dm/torrent-piece.svg
[downloads-url]: https://npmjs.org/package/torrent-piece
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
[standard-url]: https://standardjs.com

### Torrent piece abstraction

[![Sauce Test Status](https://saucelabs.com/browser-matrix/torrent-piece.svg)](https://saucelabs.com/u/torrent-piece)

Also works in the browser with [browserify](http://browserify.org/)! This module is used by [WebTorrent](http://webtorrent.io) and [torrent-stream](https://npmjs.com/package/torrent-stream).

## install

```
npm install torrent-piece
```

## usage

```js
import Piece from 'torrent-piece'

Piece.BLOCK_LENGTH // 16384

const pieceLength = Piece.BLOCK_LENGTH * 5
const piece = new Piece(pieceLength)
piece.missing // 81920

piece.reserve() // 0
piece.set(0, someBuffer0)

piece.reserve() // 1
piece.reserve() // 2
piece.reserve() // 3

piece.set(1, someBuffer1)
piece.set(2, someBuffer2)
piece.set(3, someBuffer3)

piece.reserve() // 4
piece.cancel(4) // cancel the reservation of a chunk

piece.reserve() // 4 (given out again)
piece.set(4, someBuffer4)

// handy functions
piece.chunkLength(0) // 16384
piece.chunkOffset(0) // 0

// eventually, when no more chunks left...

piece.reserve() // -1 (signal that all chunks are reserved)
piece.missing // 0

const pieceBuffer = piece.flush()
console.log(pieceBuffer)
```

## license

MIT. Copyright (c) [Feross Aboukhadijeh](https://feross.org) and [WebTorrent, LLC](https://webtorrent.io).

---
_Source: https://npm.io/package/torrent-piece · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
