1. torrent-piece
Torrent piece abstraction
torrent-piece
Package: torrent-piece
Created by: webtorrent
Last modified: Sun, 23 Jul 2023 11:36:59 GMT
Version: 3.0.0
License: MIT
Downloads: 14,178
Repository: https://github.com/webtorrent/torrent-piece

Install

npm install torrent-piece
yarn add torrent-piece

torrent-piece ci npm downloads javascript style guide

Torrent piece abstraction

Sauce Test Status

Also works in the browser with browserify! This module is used by WebTorrent and torrent-stream.

install

npm install torrent-piece

usage

 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 and WebTorrent, LLC.

Dependencies

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code