torrent-piece
![javascript style guide](https://camo.githubusercontent.com/bde227e3207c7143032c0feb73889ffbda8eb1ef234b820b915ccaf74f9c66d7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f64655f7374796c652d7374616e646172642d627269676874677265656e2e737667)
Torrent piece abstraction
Also works in the browser with browserify! This module is used by WebTorrent and torrent-stream.
install
npm install torrent-piece
usage
const Piece = require('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 and WebTorrent, LLC.