Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/webtorrent/torrent-piece
Torrent piece abstraction
https://github.com/webtorrent/torrent-piece
bittorrent browser javascript nodejs piece torrent webtorrent
Last synced: 9 days ago
JSON representation
Torrent piece abstraction
- Host: GitHub
- URL: https://github.com/webtorrent/torrent-piece
- Owner: webtorrent
- License: mit
- Created: 2015-08-19T13:54:52.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-10-26T16:31:36.000Z (13 days ago)
- Last Synced: 2024-10-30T00:27:31.328Z (10 days ago)
- Topics: bittorrent, browser, javascript, nodejs, piece, torrent, webtorrent
- Language: JavaScript
- Homepage: https://webtorrent.io
- Size: 875 KB
- Stars: 23
- Watchers: 5
- Forks: 9
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-peer-to-peer - torrent-piece
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/workflow/status/webtorrent/torrent-piece/ci/master
[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 // 81920piece.reserve() // 0
piece.set(0, someBuffer0)piece.reserve() // 1
piece.reserve() // 2
piece.reserve() // 3piece.set(1, someBuffer1)
piece.set(2, someBuffer2)
piece.set(3, someBuffer3)piece.reserve() // 4
piece.cancel(4) // cancel the reservation of a chunkpiece.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 // 0const pieceBuffer = piece.flush()
console.log(pieceBuffer)
```## license
MIT. Copyright (c) [Feross Aboukhadijeh](https://feross.org) and [WebTorrent, LLC](https://webtorrent.io).