Install
npm i torrefy
Basic usage
import { create, encode, decode } 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 [torrentStream1, torrentStream2] = torrentStream.tee();
const torrentBinary = await new Response(torrentStream1).arrayBuffer();
const decodedMetaInfo = await decode(torrentStream2);
Features
Supports Creating V1, V2 or Hybrid Torrents
This package supports creating V1, V2 (introduction blog) or Hybrid (introduction blog) torrents.
Covers Various Web File APIs
This package can handle input files or directories acquired from File API, File and Directory Entries API or File System Access API.
Supports Comprehensive Options
TBD
Supports Handling Progress
TBD
Exposes Stream-Based APIs
The create
function consumes an iterable of input files as ReadableStream
s with options and populates a MetaInfo
object. This function internally uses several TransformStream
s to chop the files into pieces and hash them.
The encode
function consumes any bcodec friendly entity (e.g. MetaInfo
object) and bencodes it into a ReadableStream
.
The decode
function consumes any bcodec friendly ReadableStream
(e.g. torrent ReadableStream
) and bdecodes it into the corresponding entity. This function internally uses a TransformStream
called Tokenizer
to tokenize the input ReadableStream
and then calls parse
function to parse the Tokens
.
All TransformStream
s used in this package are also exported.
Supports a Comprehensive Set of Bcodec Friendly Javascript Types
Bcodec friendly Javascript types includes (for the time being):
encode
function supports all Loose
type inputs and decode
function always returns Strict
type results.
Supports Hooks in Bencoding
⚠ The terminology may change in the future.
You can register hooks when using the encode
function. A common use case is extracting the bencoded info
dictionary and calculating the infohash
. (This package doesn't provide an out-of-box function to calculate infohash
for now)
To use hooks, you will have to install the peer dependency @sec-ant/trie-map
, which allows you to register hook handlers with iterable paths as keys. You can learn more about this package in its README.
This package provides 3 helper functions to help you register hook handlers on hooks and consume their results as you please: useUint8ArrayStreamHook
, useArrayBufferPromiseHook
, useTextPromiseHook
.
Here is how you should use this feature:
import { encode, EncoderHooks, useArrayBufferPromiseHook } from "torrefy";
import { TrieMap } from "@sec-ant/trie-map";
const dummyObject = {
a: "b",
c: 1,
info: {
foo: "bar",
},
s: ["t"],
};
const hooks: EncoderHooks = new TrieMap();
const [infoArrayBufferPromise] = useArrayBufferPromiseHook(["info"], hooks);
const bencodedReadableStream = encode(dummyObject, hooks);
const infoArrayBuffer = await infoArrayBufferPromise;