@satellite-earth/torrent v2.0.0
View full documentation at https://docs.satellite.earth/
Usage
const Torrent = require('@satellite-earth/torrent');
// Create a torrent model
const torrent = new Torrent({
name: 'mytorrent.png',
foo: 'bar'
});
// Compute torrent "meta" data
await torrent.data(file);
console.log(torrent.payload);
// {
// _signed_: {
// name: 'mytorrent',
// foo: 'bar',
// meta: '9437.nTrcKwmq8R39tFoR0DNvYhcLsq4=.QmaXEemWbXdysM3E9pj6sdM2LBcoW2NUUp1GzgAsNgrFCp'
// },
// _params_: {}
// }Constructor
Object|String- Message payload. Can be object or uri-encoded.Object- OptionsderiveName-Function- If provided, called withthisand expected to returnnameproperty. Useful for creating torrent models in which the name is derived from other properties instead of being explicitly contained in the signed data.
Properties
name
torrent.name; // 'mytorrent'String - Torrent's name, as set in constructor. Default Untitled.
extension
torrent.extension; // 'png'String - File extension of torrent data, if any (based on name)
length
torrent.length; // 255124Number - Byte length of torrent data
pieces
torrent.pieces; // [ '77412139e061b76cbe626d806bc748b350b390ef', ... ]Array - SHA1 hashes of each piece of torrent data
numPieces
torrent.numPieces; // 16Number - Number of pieces torrent data was split into
pieceLength
torrent.pieceLength; // 16384Number - Byte length of torrent pieces
lastPieceLength
torrent.lastPieceLength; // 9364Number - Byte length of last torrent piece
info
torrent.infoObject - BitTorrent info dict
name-Uint8Array- Torrent's namelength-Number- Byte length of torrent datapiece length-Number- Byte length of torrent piecespieces-Uint8Array- SHA1 hashes of each piece of torrent data
infoHash
torrent.infoHash; // 'd92cc55bd412a34f3bb3caf2878707963954fd52'String - Torrent's infohash
ipfsHash
torrent.ipfsHash; // 'QmaXEemWbXdysM3E9pj6sdM2LBcoW2NUUp1GzgAsNgrFCp'String - IPFS multihash of torrent's data
Methods
data
await torrent.data(file);
console.log(torrent._signed_.meta);
// 9437.nTrcKwmq8R39tFoR0DNvYhcLsq4=.QmaXEemWbXdysM3E9pj6sdM2LBcoW2NUUp1GzgAsNgrFCpPopulate torrent model with computed torrent meta data, concatenated as <length>.<SHA1>.<IPFS> and saved in the torrent's _signed_ object.
Parameters
Uint8Array|Blob- Torrent data
Returns
Promise returns this - Reference to self allows method chaining
clearTorrentParams
torrent.clearTorrentParams();
console.log(torrent._signed_.meta); // undefinedRemove existing torrent meta data from model
getParsedTorrent
const parsed = getParsedTorrent();
console.log(parsed);
// {
// length,
// info,
// infoHash,
// infoBuffer,
// name,
// infoHashBuffer,
// announce,
// urlList,
// lastPieceLength,
// pieceLength,
// pieces,
// files: [ ... ]
// }Get object representing the torrent in the format expected by WebTorrent
Parameters
Object- Optionsannounce-Array- Array of torrent trackersurlList-Array- Array of http webseeds
Returns
Object - The torrent object
getTorrentFile
const torrentFile = torrent.getTorrentFile();Get standard bencoded .torrent file in the format expected by any torrent client
Parameters
Object- Optionsannounce-Array- Array of torrent trackersurlList-Array- Array of http webseeds
Returns
Uint8Array - The .torrent file