Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

torrefy

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

torrefy

torrefy An ES

  • 2.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
Maintainers
1
Weekly downloads
 
Created
Source

This package is still under heavy development. APIs are prone to change. Use with caution!!

torrefy

An ESM package that uses Web Streams API to create v1, v2 or hybrid torrents in your web browser

Install

npm i torrefy # or yarn add torrefy

Usage

Basic usage

import { create, encode, decode } from "torrefy";

// create a test file
const testFile = new File(
  ["Hello world. This is the test file content."],
  "testfile.txt"
);

// calculate (hash) the meta info of the test file
const metaInfo = await create([testFile]);

// bencode meta info into a readable stream
const torrentStream = encode(metaInfo);

// tee the readable stream into two readable streams
const [torrentStream1, torrentStream2] = torrentStream.tee();

// consume the first readable stream as an array buffer
const torrentBinary = await new Response(torrentStream).arrayBuffer();

// decode the second readable stream into meta info
const decodedMetaInfo = await decode(torrentStream2);

Advance usage

import {
  create,
  encode,
  decode,
  CommonPieceLength,
  TorrentType,
  TorrentOptions,
  OnProgress,
  ArrayKeyedMap,
  useArrayBufferPromiseHook,
  useTextPromiseHook,
} from "torrefy";

// create a test file
const testFile = new File(
  ["Hello world. This is the test file content."],
  "testfile.txt"
);

// v1 torrent options
const options: TorrentOptions<TorrentType.V1> = {
  type: TorrentType.V1,
  announceList: [
    ["udp://tracker.opentrackr.org:1337/announce"],
    ["udp://9.rarbg.com:2810/announce"],
  ],
  pieceLength: CommonPieceLength["16KB"],
};

// handle progress
const handleProgress: OnProgress = (current, total) => {
  console.log(((current / total) * 100).toFixed(2) + "%");
};

// calculate (hash) the meta info of the test file
const metaInfo = await create([testFile], options, handleProgress);

// use hooks when bencoding
const hooks = new ArrayKeyedMap();

// declare hook result as an array buffer promise
const [infoPromise, updateInfo] = useArrayBufferPromiseHook();
// register the above hook under "info" path
hooks.set(["info"], updateInfo);

// declare hook result as a text promise
const [piecesPromise, updatePieces] = useTextPromiseHook();
// register the above hook under "info.pieces" path
hooks.set(["info", "pieces"], updatePieces);

// bencode meta info into a readable stream with registered hooks
const torrentStream = encode(metaInfo, hooks);

// tee the readable stream into two readable streams
const [torrentStream1, torrentStream2] = torrentStream.tee();

// consume the first readable stream as an array buffer
const torrentBinary = await new Response(torrentStream1).arrayBuffer();

// get bencoded "info" as an array buffer
const info = await infoPromise;

// get bencoded "info.pieces" as a piece of text
const pieces = await piecesPromise;

// decode the second readable stream into meta info
const decodedMetaInfo = await decode(torrentStream2);

Todos

  • BDecode implementation
  • Magnet URI scheme (should be trivial)
  • Convert all makeXXXTransformStream functional closure states to transformer class states (should be trivial)
  • ArrayKeyedMap with ArrayBuffer keys (use proxy or drop suppport?)
  • Convert typescript Enums to Unions (need investigation)
  • Other type related issues (need investigation)
  • Bundleless entry (need investigation)
  • Support other common BEPs (need investigation)
  • Add tests
  • Add demo page

FAQs

Package last updated on 07 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc