torrefy
create v1, v2 or hybrid torrents in your browser
Usage
Basic usage
import { create, encode } from "torrefy";
const testFile = new File(
["Hello world. This is the test file content."],
"testfile.txt"
);
const metaInfo = await create([testFile]);
const torrentStream = encode(metaInfo);
const torrentBinary = await new Response(torrentStream).arrayBuffer();
Advance usage
import {
create,
encode,
CommonPieceLength,
TorrentType,
TorrentOptions,
OnProgress,
ArrayKeyedMap,
useArrayBufferPromiseHook,
useTextPromiseHook,
} from "torrefy";
const testFile = new File(
["Hello world. This is the test file content."],
"testfile.txt"
);
const options: TorrentOptions<TorrentType.V1> = {
type: TorrentType.V1,
announceList: [
["udp://tracker.opentrackr.org:1337/announce"],
["udp://9.rarbg.com:2810/announce"],
],
pieceLength: CommonPieceLength["16KB"],
};
const handleProgress: OnProgress = (current, total) => {
console.log(((current / total) * 100).toFixed(2) + "%");
};
const metaInfo = await create([testFile], options, handleProgress);
const hooks = new ArrayKeyedMap();
const [infoPromise, updateInfo] = useArrayBufferPromiseHook();
hooks.set(["info"], updateInfo);
const [piecesPromise, updatePieces] = useTextPromiseHook();
hooks.set(["info", "pieces"], updatePieces);
const torrentStream = encode(metaInfo, hooks);
const torrentBinary = await new Response(torrentStream).arrayBuffer();
const info = await infoPromise;
const pieces = await piecesPromise;