diff --git a/types/create-torrent/create-torrent-tests.ts b/types/create-torrent/create-torrent-tests.ts index 5dc5f62d4a7dc7..004015b0090e67 100644 --- a/types/create-torrent/create-torrent-tests.ts +++ b/types/create-torrent/create-torrent-tests.ts @@ -1,38 +1,57 @@ -import createTorrent = require("create-torrent"); -import fs = require("fs"); +/// +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); diff --git a/types/create-torrent/index.d.ts b/types/create-torrent/index.d.ts index 00298d90f57d58..5813cec9121ffb 100644 --- a/types/create-torrent/index.d.ts +++ b/types/create-torrent/index.d.ts @@ -1,6 +1,9 @@ /// +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 @@ -8,48 +11,38 @@ interface CreateTorrentOptions { // 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 | undefined; + info?: Record; // 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 }; diff --git a/types/create-torrent/package.json b/types/create-torrent/package.json index 2b2c92efb975b6..6f7586341d7097 100644 --- a/types/create-torrent/package.json +++ b/types/create-torrent/package.json @@ -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" ],