Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions types/create-torrent/create-torrent-tests.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@
import createTorrent = require("create-torrent");
import fs = require("fs");
/// <reference types="node" />
import createTorrent, { announceList, isJunkPath } from "create-torrent";

createTorrent("test", (err, torrent) => {
if (err) {
return;
}
const cb = (err: Error | null, torrent?: Buffer) => {};

fs.writeFileSync("test.torrent", torrent);
createTorrent("test", (err: Error | null, torrent?: Buffer) => {
if (err) return;
if (torrent) torrent.equals(Buffer.alloc(0));
});

createTorrent(["a.txt", "b.txt"], cb);

createTorrent(
"test",
[Buffer.from("a")],
{
name: "test",
comment: "test",
createdBy: "test",
creationDate: Date.now(),
comment: "comment",
createdBy: "tester",
creationDate: new Date(),
private: true,
pieceLength: 100,
announceList: [["test"]],
urlList: ["test"],
info: {
test: "test",
},
onProgress: (b1, b2) => {
const percent = Math.round(b1 / b2 * 100);
console.info(`${percent} % ${b1} B - ${b2} B`);
pieceLength: 1024,
maxPieceLength: 2 * 1024 * 1024,
announceList: [["udp://tracker.example"]],
urlList: ["https://example.com/file"],
info: { custom: "value" },
onProgress: (written: number, total: number) => {
const percent: number = Math.round((written / total) * 100);
console.log(percent);
},
},
(err, torrent) => {
if (err) {
return;
}
cb,
);

fs.writeFileSync("test.torrent", torrent);
createTorrent(
[Buffer.from("a")],
{
name: "test",
comment: "comment",
createdBy: "tester",
creationDate: new Date(),
private: false,
pieceLength: 1024,
maxPieceLength: 2 * 1024 * 1024,
announceList: [["udp://tracker.example"]],
urlList: ["https://example.com/file"],
info: { custom: "value" },
onProgress: (written: number, total: number) => {
const percent: number = Math.round((written / total) * 100);
console.log(percent);
},
},
cb,
);

const junk: boolean = isJunkPath("dir");
const firstTracker: string | undefined = announceList[0]?.[0];
console.log(junk, firstTracker);
57 changes: 25 additions & 32 deletions types/create-torrent/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,48 @@
/// <reference types="node" />
import type { Readable as ReadableStream } from "node:stream";

interface CreateTorrentOptions {
export type CreateTorrentCallback = (err: Error | null, torrent?: Buffer) => void;

export interface CreateTorrentOptions {
// name of the torrent (default = basename of `path`, or 1st file's name)
name?: string | undefined;
// free-form textual comments of the author
comment?: string | undefined;
// name and version of program used to create torrent
createdBy?: string | undefined;
// creation time in UNIX epoch format (default = now)
creationDate?: number | undefined;
creationDate?: number | Date | undefined;
// is this a private .torrent? (default = false)
private?: boolean | undefined;
// force a custom piece length (number of bytes)
pieceLength?: number | undefined;
maxPieceLength?: number;
// custom trackers (array of arrays of strings) (see [bep12](http://www.bittorrent.org/beps/bep_0012.html))
announceList?: string[][] | undefined;
// web seed urls (see [bep19](http://www.bittorrent.org/beps/bep_0019.html))
urlList?: string[] | undefined;
// add non-standard info dict entries, e.g. info.source, a convention for cross-seeding
info?: Record<string, string> | undefined;
info?: Record<string, unknown>;
// called with the number of bytes hashed and estimated total size after every piece
onProgress?(hashedLength: number, estimatedTorrentLength: number): void;
onProgress?: (hashedLength: number, estimatedTorrentLength: number) => void;
}

declare function createTorrent(
input:
| string
| string[]
| File
| File[]
| FileList
| Buffer
| Buffer[]
| NodeJS.ReadableStream
| NodeJS.ReadableStream[],
cb: (err: Error | null, torrent: Buffer) => any,
): void;
export type TorrentInput =
| string
| File
| FileList
| Buffer
| ReadableStream
| string[]
| File[]
| Buffer[]
| ReadableStream[];

declare function createTorrent(input: TorrentInput, opts: CreateTorrentOptions, cb: CreateTorrentCallback): void;
declare function createTorrent(input: TorrentInput, cb: CreateTorrentCallback): void;

declare function createTorrent(
input:
| string
| string[]
| File
| File[]
| FileList
| Buffer
| Buffer[]
| NodeJS.ReadableStream
| NodeJS.ReadableStream[],
opts: CreateTorrentOptions,
cb: (err: Error | null, torrent: Buffer) => any,
): void;
declare const announceList: string[][];
declare function isJunkPath(path: string): boolean;

export = createTorrent;
export default createTorrent;
export { announceList, isJunkPath };
3 changes: 2 additions & 1 deletion types/create-torrent/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"private": true,
"name": "@types/create-torrent",
"version": "5.0.9999",
"type": "module",
"version": "6.0.9999",
"projects": [
"https://github.com/webtorrent/create-torrent#readme"
],
Expand Down