webtorrent: add missing fields to Torrent, create Piece interface (#37609)

* webtorrent: add missing fields to Torrent, create Piece interface

* chore: bump package version in comments

* update tests

* fix: use only major/minor

* fix: pieces can have null values

* fix: build
This commit is contained in:
Gabriel JUCHAULT 2019-08-16 20:41:42 +02:00 committed by Pranav Senthilnathan
parent ae10522911
commit 8f18d93086
2 changed files with 58 additions and 2 deletions

View File

@ -1,6 +1,8 @@
// Type definitions for WebTorrent 0.98
// Type definitions for WebTorrent 0.107
// Project: https://github.com/feross/webtorrent, https://webtorrent.io
// Definitions by: Bazyli Brzóska <https://github.com/niieani>, Tomasz Łaziuk <https://github.com/tlaziuk>
// Definitions by: Bazyli Brzóska <https://github.com/niieani>
// Tomasz Łaziuk <https://github.com/tlaziuk>
// Gabriel Juchault <https://github.com/gjuchault>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
@ -74,6 +76,10 @@ declare namespace WebTorrent {
readonly files: TorrentFile[];
readonly announce: string[];
readonly pieces: Array<TorrentPiece | null>;
readonly timeRemaining: number;
readonly received: number;
@ -90,14 +96,32 @@ declare namespace WebTorrent {
readonly ratio: number;
readonly length: number;
readonly pieceLength: number;
readonly lastPieceLength: number;
readonly numPeers: number;
readonly path: string;
readonly ready: boolean;
readonly paused: boolean;
readonly done: boolean;
readonly name: string;
readonly created: Date;
readonly createdBy: string;
readonly comment: string;
readonly maxWebConns: number;
destroy(cb?: (err: Error | string) => void): void;
addPeer(peer: string | SimplePeer): boolean;
@ -160,6 +184,12 @@ declare namespace WebTorrent {
getBlobURL(callback: (err: string | Error | undefined, blobURL?: string) => void): void;
}
interface TorrentPiece {
readonly length: number;
readonly missing: number;
}
}
export = WebTorrent;

View File

@ -8,6 +8,32 @@ client.add(magnetURI, {}, torrent => {
// Got torrent metadata!
console.log('Client is downloading:', torrent.infoHash);
console.log(
torrent.maxWebConns,
torrent.ready,
torrent.paused,
torrent.done,
torrent.created,
torrent.createdBy,
torrent.comment,
);
torrent.announce.forEach(announce => console.log(announce));
console.log(torrent.length, torrent.pieceLength, torrent.lastPieceLength);
console.log(
torrent.pieces.reduce(
(acc, piece) => acc + (piece ? piece.length : 0),
0,
),
);
console.log(
torrent.pieces.reduce(
(acc, piece) => acc + (piece ? piece.missing : 0),
0,
),
);
torrent.files.forEach(file => {
// Display the file by appending it to the DOM. Supports video, audio, images, and
// more. Specify a container element (CSS selector or reference to DOM node).