Skip to content

Commit 17a2dd8

Browse files
authored
🤖 Merge PR DefinitelyTyped#74247 feat: update create-torrent types and tests for version 6.1.0 by @trim21
1 parent 6dc2213 commit 17a2dd8

File tree

3 files changed

+71
-58
lines changed

3 files changed

+71
-58
lines changed
Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
1-
import createTorrent = require("create-torrent");
2-
import fs = require("fs");
1+
/// <reference types="node" />
2+
import createTorrent, { announceList, isJunkPath } from "create-torrent";
33

4-
createTorrent("test", (err, torrent) => {
5-
if (err) {
6-
return;
7-
}
4+
const cb = (err: Error | null, torrent?: Buffer) => {};
85

9-
fs.writeFileSync("test.torrent", torrent);
6+
createTorrent("test", (err: Error | null, torrent?: Buffer) => {
7+
if (err) return;
8+
if (torrent) torrent.equals(Buffer.alloc(0));
109
});
1110

11+
createTorrent(["a.txt", "b.txt"], cb);
12+
1213
createTorrent(
13-
"test",
14+
[Buffer.from("a")],
1415
{
1516
name: "test",
16-
comment: "test",
17-
createdBy: "test",
18-
creationDate: Date.now(),
17+
comment: "comment",
18+
createdBy: "tester",
19+
creationDate: new Date(),
1920
private: true,
20-
pieceLength: 100,
21-
announceList: [["test"]],
22-
urlList: ["test"],
23-
info: {
24-
test: "test",
25-
},
26-
onProgress: (b1, b2) => {
27-
const percent = Math.round(b1 / b2 * 100);
28-
console.info(`${percent} % ${b1} B - ${b2} B`);
21+
pieceLength: 1024,
22+
maxPieceLength: 2 * 1024 * 1024,
23+
announceList: [["udp://tracker.example"]],
24+
urlList: ["https://example.com/file"],
25+
info: { custom: "value" },
26+
onProgress: (written: number, total: number) => {
27+
const percent: number = Math.round((written / total) * 100);
28+
console.log(percent);
2929
},
3030
},
31-
(err, torrent) => {
32-
if (err) {
33-
return;
34-
}
31+
cb,
32+
);
3533

36-
fs.writeFileSync("test.torrent", torrent);
34+
createTorrent(
35+
[Buffer.from("a")],
36+
{
37+
name: "test",
38+
comment: "comment",
39+
createdBy: "tester",
40+
creationDate: new Date(),
41+
private: false,
42+
pieceLength: 1024,
43+
maxPieceLength: 2 * 1024 * 1024,
44+
announceList: [["udp://tracker.example"]],
45+
urlList: ["https://example.com/file"],
46+
info: { custom: "value" },
47+
onProgress: (written: number, total: number) => {
48+
const percent: number = Math.round((written / total) * 100);
49+
console.log(percent);
50+
},
3751
},
52+
cb,
3853
);
54+
55+
const junk: boolean = isJunkPath("dir");
56+
const firstTracker: string | undefined = announceList[0]?.[0];
57+
console.log(junk, firstTracker);
Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,48 @@
11
/// <reference types="node" />
2+
import type { Readable as ReadableStream } from "node:stream";
23

3-
interface CreateTorrentOptions {
4+
export type CreateTorrentCallback = (err: Error | null, torrent?: Buffer) => void;
5+
6+
export interface CreateTorrentOptions {
47
// name of the torrent (default = basename of `path`, or 1st file's name)
58
name?: string | undefined;
69
// free-form textual comments of the author
710
comment?: string | undefined;
811
// name and version of program used to create torrent
912
createdBy?: string | undefined;
1013
// creation time in UNIX epoch format (default = now)
11-
creationDate?: number | undefined;
14+
creationDate?: number | Date | undefined;
1215
// is this a private .torrent? (default = false)
1316
private?: boolean | undefined;
1417
// force a custom piece length (number of bytes)
1518
pieceLength?: number | undefined;
19+
maxPieceLength?: number;
1620
// custom trackers (array of arrays of strings) (see [bep12](http://www.bittorrent.org/beps/bep_0012.html))
1721
announceList?: string[][] | undefined;
1822
// web seed urls (see [bep19](http://www.bittorrent.org/beps/bep_0019.html))
1923
urlList?: string[] | undefined;
2024
// add non-standard info dict entries, e.g. info.source, a convention for cross-seeding
21-
info?: Record<string, string> | undefined;
25+
info?: Record<string, unknown>;
2226
// called with the number of bytes hashed and estimated total size after every piece
23-
onProgress?(hashedLength: number, estimatedTorrentLength: number): void;
27+
onProgress?: (hashedLength: number, estimatedTorrentLength: number) => void;
2428
}
2529

26-
declare function createTorrent(
27-
input:
28-
| string
29-
| string[]
30-
| File
31-
| File[]
32-
| FileList
33-
| Buffer
34-
| Buffer[]
35-
| NodeJS.ReadableStream
36-
| NodeJS.ReadableStream[],
37-
cb: (err: Error | null, torrent: Buffer) => any,
38-
): void;
30+
export type TorrentInput =
31+
| string
32+
| File
33+
| FileList
34+
| Buffer
35+
| ReadableStream
36+
| string[]
37+
| File[]
38+
| Buffer[]
39+
| ReadableStream[];
40+
41+
declare function createTorrent(input: TorrentInput, opts: CreateTorrentOptions, cb: CreateTorrentCallback): void;
42+
declare function createTorrent(input: TorrentInput, cb: CreateTorrentCallback): void;
3943

40-
declare function createTorrent(
41-
input:
42-
| string
43-
| string[]
44-
| File
45-
| File[]
46-
| FileList
47-
| Buffer
48-
| Buffer[]
49-
| NodeJS.ReadableStream
50-
| NodeJS.ReadableStream[],
51-
opts: CreateTorrentOptions,
52-
cb: (err: Error | null, torrent: Buffer) => any,
53-
): void;
44+
declare const announceList: string[][];
45+
declare function isJunkPath(path: string): boolean;
5446

55-
export = createTorrent;
47+
export default createTorrent;
48+
export { announceList, isJunkPath };

‎types/create-torrent/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"private": true,
33
"name": "@types/create-torrent",
4-
"version": "5.0.9999",
4+
"type": "module",
5+
"version": "6.0.9999",
56
"projects": [
67
"https://github.com/webtorrent/create-torrent#readme"
78
],

0 commit comments

Comments
 (0)