From 3fff8d11d26feb7d8ea7cd77ef325dfacdbcf9bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=81aziuk?= Date: Wed, 18 Jan 2017 19:19:15 +0100 Subject: [PATCH] [webtorrent] update to v0.98 (#14045) * WebTorrent 0.98.1 * version * lint * authors * simple-peer --- webtorrent/index.d.ts | 480 ++++++++++----------------------- webtorrent/tsconfig.json | 4 +- webtorrent/tslint.json | 3 + webtorrent/webtorrent-tests.ts | 122 ++++----- 4 files changed, 213 insertions(+), 396 deletions(-) create mode 100644 webtorrent/tslint.json diff --git a/webtorrent/index.d.ts b/webtorrent/index.d.ts index 3760e043ea..413dee9133 100644 --- a/webtorrent/index.d.ts +++ b/webtorrent/index.d.ts @@ -1,341 +1,155 @@ -// Type definitions for WebTorrent -// Project: https://webtorrent.io/ -// Definitions by: Bazyli Brzóska +// Type definitions for WebTorrent 0.98 +// Project: https://github.com/feross/webtorrent +// Definitions by: Bazyli Brzóska , Tomasz Łaziuk // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// -/// -import ParseTorrent = require("parse-torrent"); +import * as ParseTorrent from 'parse-torrent'; +import * as SimplePeer from 'simple-peer'; +import * as http from 'http'; -declare var webTorrentStatic:WebTorrent.ClientConstructor; -export = webTorrentStatic; -export as namespace WebTorrent; +declare const WebTorrent: WebTorrent.WebTorrent; declare namespace WebTorrent { - export interface ClientOptions { - dht?: boolean|Object, // Enable DHT (default=true), or options object for DHT - maxConns?: number, // Max number of connections per torrent (default=55) - nodeId?: string|Buffer, // DHT protocol node ID (default=randomly generated) - peerId?: string|Buffer, // Wire protocol peer ID (default=randomly generated) - rtcConfig?: Object, // RTCPeerConnection configuration object (default=STUN only) - tracker?: boolean|Object, // Whether or not to enable trackers (default=true) - wrtc?: Object // Custom webrtc implementation (in node, specify the [wrtc](https://www.npmjs.com/package/wrtc) package) - } - - export interface TorrentOptions { - announce?: Array, // Torrent trackers to use (added to list in .torrent or magnet uri) - path?: string, // Folder to download files to (default=`/tmp/webtorrent/`) - store?: Function // Custom chunk store (must follow [abstract-chunk-store](https://www.npmjs.com/package/abstract-chunk-store) API) - } - - export interface ClientConstructor { - new (config?:ClientOptions):Client; - } - - export interface Client extends NodeJS.EventEmitter, ClientConstructor { - on(event: string, listener: Function): this; - - /** - * Start downloading a new torrent. Aliased as client.download. - - torrentId can be one of: - - * magnet uri (string) - * torrent file (buffer) - * info hash (hex string or buffer) - * parsed torrent (from parse-torrent) - * http/https url to a torrent file (string) - * filesystem path to a torrent file (string) - - If ontorrent is specified, then it will be called when this torrent is ready to be used (i.e. metadata is available). Note: this is distinct from the 'torrent' event which will fire for all torrents. - - If you want access to the torrent object immediately in order to listen to events as the metadata is fetched from the network, then use the return value of client.add. If you just want the file data, then use ontorrent or the 'torrent' event. - */ - // add(torrentFileBufferOrMagnetUriOrPathOrInfoHash:string|Buffer|ParsedTorrent, opts?:TorrentOptions, onTorrentCallback?:(torrent:Torrent)=>void):Torrent; - add(magnetUriOrPathOrInfoHash:string, opts?:TorrentOptions, onTorrentCallback?:(torrent:Torrent)=>void):Torrent; - add(torrentFileOrInfoHash:Buffer, opts?:TorrentOptions, onTorrentCallback?:(torrent:Torrent)=>void):Torrent; - add(parsedTorrent:ParseTorrent.ParsedTorrent, opts?:TorrentOptions, onTorrentCallback?:(torrent:Torrent)=>void):Torrent; - - add(magnetUriOrPathOrInfoHash:string, onTorrentCallback?:(torrent:Torrent)=>void):Torrent; - add(torrentFileOrInfoHash:Buffer, onTorrentCallback?:(torrent:Torrent)=>void):Torrent; - add(parsedTorrent:ParseTorrent.ParsedTorrent, onTorrentCallback?:(torrent:Torrent)=>void):Torrent; - - /** - * Emitted when a torrent is ready to be used (i.e. metadata is available and store is ready). See the torrent section for more info on what methods a torrent has. - */ - on(event:'torrent', callback:(torrent:Torrent)=>void): this; - - /** - * Start seeding a new torrent. - - input can be any of the following: - - path to the file or folder on filesystem (string) (Node.js only) - W3C File object (from an or drag and drop) - W3C FileList object (basically an array of File objects) - Node Buffer object (works in the browser) - Or, an array of string, File, or Buffer objects. - - If opts is specified, it should contain the following types of options: - - options for create-torrent (to allow configuration of the .torrent file that is created) - options for client.add (see above) - If onseed is specified, it will be called when the client has begun seeding the file. - */ - seed(inputFileOrFolderPath:string, opts?:TorrentOptions|any, onSeed?:(torrent:Torrent)=>void):void; - seed(inputFile:File, opts?:TorrentOptions|any, onSeed?:(torrent:Torrent)=>void):void; - seed(inputFileList:FileList, opts?:TorrentOptions|any, onSeed?:(torrent:Torrent)=>void):void; - seed(inputBuffer:Buffer, opts?:TorrentOptions|any, onSeed?:(torrent:Torrent)=>void):void; - seed(inputBuffersFilesOrPaths:Array, opts?:TorrentOptions|any, onSeed?:(torrent:Torrent)=>void):void; - - seed(inputFileOrFolderPath:string, onSeed?:(torrent:Torrent)=>void):void; - seed(inputFile:File, onSeed?:(torrent:Torrent)=>void):void; - seed(inputFileList:FileList, onSeed?:(torrent:Torrent)=>void):void; - seed(inputBuffer:Buffer, onSeed?:(torrent:Torrent)=>void):void; - seed(inputBuffersFilesOrPaths:Array, onSeed?:(torrent:Torrent)=>void):void; - //TODO: opts - - /** - * Remove a torrent from the client. Destroy all connections to peers and delete all saved file data. If callback is specified, it will be called when file data is removed. - */ - remove(torrentId:string, callback?:(err:Error)=>void):void; - - /** - * Destroy the client, including all torrents and connections to peers. If callback is specified, it will be called when the client has gracefully closed. - */ - destroy(callback?:(err:Error)=>void):void; - - /** - * An array of all torrents in the client. - */ - torrents:Array; - - /** - * Returns the torrent with the given torrentId. Convenience method. Easier than searching through the client.torrents array. Returns null if no matching torrent found. - */ - get(torrentId:string):Torrent; - - /** - * Seed ratio for all torrents in the client. - */ - ratio:number; - } - - export interface Torrent extends NodeJS.EventEmitter { - on(event: string, listener: Function): this; - - /** - * Get the info hash of the torrent. - */ - infoHash:string; - - /** - * Get the magnet URI of the torrent. - */ - magnetURI:string; - - /** - * An array of all files in the torrent. See the file section for more info on what methods the file has. - */ - files:Array; - - /** - * The attached bittorrent-swarm instance. - */ - swarm:any; // TODO: return type - - /** - * Get total bytes received from peers (including invalid data). - */ - recieved:number; - - /** - * Get total bytes received from peers (excluding invalid data). - */ - downloaded:number; - - /** - * Get the time remaining in millis if downloading. - */ - timeRemaining:number; - - /** - * Get the total progress from 0 to 1. - */ - progress:number; - - /** - * Get the torrent ratio (seeded/downloaded). - */ - ratio:number; - - /** - * Returns the download speed. - */ - downloadSpeed():number; - - /** - * Returns the current upload speed. - */ - uploadSpeed():number; - - /** - * Get the torrent download location. - */ - path:string; - - /** - * Alias for client.remove(torrent). - */ - destroy():void; - - /** - * Adds a peer to the underlying bittorrent-swarm instance. - - Returns true if peer was added, false if peer was blocked by the loaded blocklist. - */ - addPeer(addr:string):boolean; - - /** - * Selects a range of pieces to prioritize starting with start and ending with end (both inclusive) at the given priority. notify is an optional callback to be called when the selection is updated with new data. - */ - select(start:number, end:number, priority?:number, notifyCallback?:() => void):void; - - /** - * Deprioritizes a range of previously selected pieces. - */ - deselect(start:number, end:number, priority:number):void; - - /** - * Marks a range of pieces as critical priority to be downloaded ASAP. From start to end (both inclusive). - */ - critical(start:number, end:number):void; - - /** - * Create an http server to serve the contents of this torrent, dynamically fetching the needed torrent pieces to satisfy http requests. Range requests are supported. - - Returns an http.Server instance (got from calling http.createServer). If opts is specified, it is passed to the http.createServer function. - - Visiting the root of the server / will show a list of links to individual files. Access individual files at / where is the index in the torrent.files array (e.g. /0, /1, etc.) - */ - createServer(opts?:any):any; // TODO: :http.Server; - - /** - * Temporarily stop connecting to new peers. Note that this does not pause new incoming connections, nor does it pause the streams of existing connections or their wires. - */ - pause():void; - - /** - * Resume connecting to new peers. - */ - resume():void; - - /** - * Emitted when all the torrent's files have been downloaded. - */ - on(event: 'done', callback:()=>void): this; - - /** - * Emitted every time a new chunk of data arrives, it's useful for reporting the current torrent status. - */ - on(event: 'download', callback:(chunkSize:number)=>void): this; - - /** - * Emitted whenever a new peer is connected for this torrent. wire is an instance of bittorrent-protocol, which is a node.js-style duplex stream to the remote peer. This event can be used to specify custom BitTorrent protocol extensions. - */ - on(event: 'wire', callback:(wire: any, addr: any) => void): this; - } - - export interface InTorrentFile extends NodeJS.EventEmitter { - on(event: string, listener: Function): this; - - /** - * File name, as specified by the torrent. Example: 'some-filename.txt' - */ - name:string; - - /** - * File path, as specified by the torrent. Example: 'some-folder/some-filename.txt' - */ - path:string; - - /** - * File length (in bytes), as specified by the torrent. Example: 12345 - */ - length:number; - - /** - * Selects the file to be downloaded, but at a lower priority than files with streams. Useful if you know you need the file at a later stage. - */ - select():void; - - /** - * Deselects the file, which means it won't be downloaded unless someone creates a stream for it. - */ - deselect():void; - - /** - * Create a readable stream to the file. Pieces needed by the stream will be prioritized highly and fetched from the swarm first. - * You can pass opts to stream only a slice of a file. - * Both start and end bytes are inclusive. - */ - createReadStream(opts?:{start:number, end:number}):NodeJS.ReadableStream; - - /** - * Get the file contents as a Buffer. - - The file will be fetched from the network with highest priority, and callback will be called once the file is ready. callback must be specified, and will be called with a an Error (or null) and the file contents as a Buffer. - - file.getBuffer(function (err, buffer) { - if (err) throw err - console.log(buffer) // - }) - - */ - getBuffer(callback: (err:Error, buffer:Buffer) => void):void; - - /** - * Show the file in a the browser by appending it to the DOM. This is a powerful function that handles many file types like video (.mp4, .webm, .m4v, etc.), audio (.m4a, .mp3, .wav, etc.), images (.jpg, .gif, .png, etc.), and other file formats (.pdf, .md, .txt, etc.). - - The file will be fetched from the network with highest priority and streamed into the page (if it's video or audio). In some cases, video or audio files will not be streamable because they're not in a format that the browser can stream so the file will be fully downloaded before being played. For other non-streamable file types like images and PDFs, the file will be downloaded then displayed. - - rootElem is a container element (CSS selector or reference to DOM node) that the content will be shown in. A new DOM node will be created for the content and appended to rootElem. - - callback will be called once the file is visible to the user. callback must be specified, and will be called with a an Error (or null) and the new DOM node that is displaying the content. - - file.appendTo('#containerElement', function (err, elem) { - if (err) throw err // file failed to download or display in the DOM - console.log('New DOM node with the content', elem) - }) - - */ - appendTo(rootElem:Element|string, callback?:(err:Error, elem:Element) => void):void; - - /** - * Like file.appendTo but renders directly into given element (or CSS selector). - */ - renderTo(elem:Element|string, callback?:(err:Error, elem:Element) => void):void; - - /** - * Get a url which can be used in the browser to refer to the file. - - The file will be fetched from the network with highest priority, and callback will be called once the file is ready. callback must be specified, and will be called with a an Error (or null) and the Blob URL (String). - - file.getBlobURL(function (err, url) { - if (err) throw err - var a = document.createElement('a') - a.download = file.name - a.href = url - a.textContent = 'Download ' + file.name - document.body.appendChild(a) - }) - */ - getBlobURL(callback:(err:Error, url:string) => void):void; - - /** - * Emitted when the file have been downloaded. - */ - on(event: 'done', callback:()=>void): this; - } + export interface WebTorrent { + new (config?: Options): Instance; + (config?: Options): Instance; + WEBRTC_SUPPORT: boolean; + } + export interface Options { + maxConns?: number; + nodeId?: string | Buffer; + peerId?: string | Buffer; + tracker?: boolean | {}; + dht?: boolean | {}; + webSeeds?: boolean; + } + + export interface TorrentOptions { + announce?: any[]; + getAnnounceOpts?: () => void; + maxWebConns?: number; + path?: string; + store?: (chunkLength: number, storeOpts: { length: number, files: File[], torrent: Torrent, }) => any; + } + + export interface Instance extends NodeJS.EventEmitter { + on(event: 'torrent', callback: (torrent: Torrent) => void): this; + on(event: 'error', callback: (err: Error | string) => void): this; + + add(torrent: string | Buffer | ParseTorrent.ParsedTorrent, opts?: TorrentOptions, cb?: (torrent: Torrent) => any): Torrent; + add(torrent: string | Buffer | ParseTorrent.ParsedTorrent, cb?: (torrent: Torrent) => any): Torrent; + + seed(input: string | string[] | File | File[] | FileList | Buffer | Buffer[] | NodeJS.ReadableStream | NodeJS.ReadableStream[], opts?: TorrentOptions, cb?: (torrent: Torrent) => any): Torrent; + seed(input: string | string[] | File | File[] | FileList | Buffer | Buffer[] | NodeJS.ReadableStream | NodeJS.ReadableStream[], cb?: (torrent: Torrent) => any): Torrent; + + remove(torrentId: Torrent | string | Buffer, callback?: (err: Error | string) => void): void; + + destroy(callback?: (err: Error | string) => void): void; + + readonly torrents: Torrent[]; + + get(torrentId: Torrent | string | Buffer): Torrent | void; + + readonly downloadSpeed: number; + + readonly uploadSpeed: number; + + readonly progress: number; + + readonly ratio: number; + } + + export interface Torrent extends NodeJS.EventEmitter { + readonly infoHash: string; + + readonly magnetURI: string; + + readonly torrentFile: Buffer; + + readonly torrentFileBlobURL: string; + + readonly files: TorrentFile[]; + + readonly timeRemaining: number; + + readonly received: number; + + readonly downloaded: number; + + readonly uploaded: number; + + readonly downloadSpeed: number; + + readonly uploadSpeed: number; + + readonly progress: number; + + readonly ratio: number; + + readonly numPeers: number; + + readonly path: string; + + destroy(cb?: (err: Error | string) => void): void; + + addPeer(peer: string | SimplePeer.Instance): boolean; + + addWebSeed(url: string): void; + + removePeer(peer: string | SimplePeer.Instance): void; + + select(start: number, end: number, priority?: number, notify?: () => void): void; + + deselect(start: number, end: number, priority: number): void; + + createServer(opts?: http.RequestOptions): http.Server; + + pause(): void; + + resume(): void; + + on(event: 'infoHash' | 'metadata' | 'ready' | 'done', callback: () => void): this; + + + on(event: 'warning' | 'error', callback: (err: Error | string) => void): this; + + on(event: 'download' | 'upload', callback: (bytes: number) => void): this; + + // TODO: wire is a bittorrent-protocol instance + on(event: 'wire', callback: (wire: any, addr?: string) => void): this; + + on(event: 'noPeers', callback: (announceType: 'tracker' | 'dht') => void): this; + } + + export interface TorrentFile extends NodeJS.EventEmitter { + readonly name: string; + + readonly path: string; + + readonly length: number; + + readonly downloaded: number; + + select(): void; + + deselect(): void; + + createReadStream(opts?: { start: number, end: number, }): NodeJS.ReadableStream; + + getBuffer(callback: (err: string | Error | undefined, buffer?: Buffer) => void): void; + + appendTo(rootElement: HTMLElement | string, opts?: { autoplay?: boolean, controls?: boolean, maxBlobLength?: number }, callback?: (err: Error | undefined, element: HTMLMediaElement) => void): void; + appendTo(rootElement: HTMLElement | string, callback?: (err: Error | undefined, element: HTMLMediaElement) => void): void; + + renderTo(rootElement: HTMLMediaElement | string, opts?: { autoplay?: boolean, controls?: boolean, maxBlobLength?: number }, callback?: (err: Error | undefined, element: HTMLMediaElement) => void): void; + renderTo(rootElement: HTMLMediaElement | string, callback?: (err: Error | undefined, element: HTMLMediaElement) => void): void; + + getBlob(callback: (err: string | Error | undefined, blob?: Blob) => void): void; + + getBlobURL(callback: (err: string | Error | undefined, blobURL?: string) => void): void; + } } + +export = WebTorrent; diff --git a/webtorrent/tsconfig.json b/webtorrent/tsconfig.json index 25cf6a8aa6..92d678f0a5 100644 --- a/webtorrent/tsconfig.json +++ b/webtorrent/tsconfig.json @@ -7,7 +7,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -20,4 +20,4 @@ "index.d.ts", "webtorrent-tests.ts" ] -} \ No newline at end of file +} diff --git a/webtorrent/tslint.json b/webtorrent/tslint.json new file mode 100644 index 0000000000..ec365f164b --- /dev/null +++ b/webtorrent/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../tslint.json" +} diff --git a/webtorrent/webtorrent-tests.ts b/webtorrent/webtorrent-tests.ts index 96314de12d..895d734817 100644 --- a/webtorrent/webtorrent-tests.ts +++ b/webtorrent/webtorrent-tests.ts @@ -1,76 +1,76 @@ - - -import WebTorrent = require('webtorrent'); +import * as WebTorrent from 'webtorrent'; import * as fs from 'fs'; -var client = new WebTorrent() -var magnetURI = '...' +var client = new WebTorrent(); +var magnetURI = '...'; -client.add(magnetURI, {}, function (torrent) { - // Got torrent metadata! - console.log('Client is downloading:', torrent.infoHash) +client.add(magnetURI, {}, function(torrent) { + // Got torrent metadata! + console.log('Client is downloading:', torrent.infoHash); - torrent.files.forEach(function (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). - file.appendTo('body') + torrent.files.forEach(function(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). + file.appendTo('body'); - file.getBuffer(function (err, buffer) { - if (err) throw err - console.log(buffer) // - }) + file.getBuffer(function(err, buffer) { + if (err) throw err; + console.log(buffer); // + }); - file.appendTo('#containerElement', function (err, elem) { - if (err) throw err // file failed to download or display in the DOM - console.log('New DOM node with the content', elem) - }) + file.appendTo('#containerElement', function(err, elem) { + if (err) throw err; // file failed to download or display in the DOM + console.log('New DOM node with the content', elem); + }); - file.getBlobURL(function (err, url) { - if (err) throw err - var a = document.createElement('a') - // a.download = file.name - a.href = url - a.textContent = 'Download ' + file.name - document.body.appendChild(a) - }) - }) + file.getBlobURL(function(err, url) { + if (err) throw err; + var a = document.createElement('a'); + // a.download = file.name + if (url) { + a.href = url; + } + a.textContent = 'Download ' + file.name; + document.body.appendChild(a); + }); + }); - torrent.on('done', function(){ - console.log('torrent finished downloading'); - torrent.files.forEach(function(file){ - // do something with file - }) - }) + torrent.on('done', function() { + console.log('torrent finished downloading'); + torrent.files.forEach(function(file) { + // do something with file + }); + }); - torrent.on('download', function(chunkSize){ - console.log('chunk size: ' + chunkSize); - console.log('total downloaded: ' + torrent.downloaded); - console.log('download speed: ' + torrent.downloadSpeed); - console.log('progress: ' + torrent.progress); - console.log('======'); - }) + torrent.on('download', function(chunkSize) { + console.log('chunk size: ' + chunkSize); + console.log('total downloaded: ' + torrent.downloaded); + console.log('download speed: ' + torrent.downloadSpeed); + console.log('progress: ' + torrent.progress); + console.log('======'); + }); - torrent.on('wire', function (wire, addr) { - console.log('connected to peer with address ' + addr) - wire.use() - }) -}) + torrent.on('wire', function(wire, addr) { + console.log('connected to peer with address ' + addr); + wire.use(); + }); +}); -client.seed('./file.txt', {}, function (torrent) { - console.log('Client is seeding:', torrent.infoHash) -}) +client.seed('./file.txt', {}, function(torrent) { + console.log('Client is seeding:', torrent.infoHash); +}); -client.add(magnetURI, function (torrent) { - // create HTTP server for this torrent - var server = torrent.createServer() - server.listen(1234) // start the server listening to a port +client.add(magnetURI, function(torrent) { + // create HTTP server for this torrent + var server = torrent.createServer(); + server.listen(1234); // start the server listening to a port - // visit http://localhost:/ to see a list of files + // visit http://localhost:/ to see a list of files - // access individual files at http://localhost:/ where index is the index - // in the torrent.files array + // access individual files at http://localhost:/ where index is the index + // in the torrent.files array - // later, cleanup... - server.close() - client.destroy() -}) + // later, cleanup... + server.close(); + client.destroy(); +});