mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-22 11:57:33 +08:00
WebTorrent project typings & parse-torrent library
This commit is contained in:
97
parse-torrent/parse-torrent-tests.ts
Normal file
97
parse-torrent/parse-torrent-tests.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
/// <reference path="parse-torrent.d.ts" />
|
||||
|
||||
import parseTorrent = require('parse-torrent');
|
||||
import * as fs from 'fs';
|
||||
|
||||
// info hash (as a hex string)
|
||||
parseTorrent('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36')
|
||||
// { infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' }
|
||||
|
||||
// info hash (as a Buffer)
|
||||
parseTorrent(new Buffer('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36', 'hex'))
|
||||
// { infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' }
|
||||
|
||||
// magnet uri (as a utf8 string)
|
||||
parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36')
|
||||
// { xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
|
||||
// infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' }
|
||||
|
||||
// magnet uri with torrent name
|
||||
parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves%20of%20Grass%20by%20Walt%20Whitman.epub')
|
||||
// { xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
|
||||
// dn: 'Leaves of Grass by Walt Whitman.epub',
|
||||
// infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
|
||||
// name: 'Leaves of Grass by Walt Whitman.epub' }
|
||||
|
||||
// magnet uri with trackers
|
||||
parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&tr=http%3A%2F%2Ftracker.example.com%2Fannounce')
|
||||
// { xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
|
||||
// tr: 'http://tracker.example.com/announce',
|
||||
// infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
|
||||
// announce: [ 'http://tracker.example.com/announce' ] }
|
||||
|
||||
// .torrent file (as a Buffer)
|
||||
parseTorrent(fs.readFileSync(__dirname + '/torrents/leaves.torrent'))
|
||||
// { info:
|
||||
// { length: 362017,
|
||||
// name: <Buffer 4c 65 61 76 65 73 20 6f 66 20 47 72 61 73 73 20 62 79 20 57 61 6c 74 20 57 68 69 74 6d 61 6e 2e 65 70 75 62>,
|
||||
// 'piece length': 16384,
|
||||
// pieces: <Buffer 1f 9c 3f 59 be ec 07 97 15 ec 53 32 4b de 85 69 e4 a0 b4 eb ec 42 30 7d 4c e5 55 7b 5d 39 64 c5 ef 55 d3 54 cf 4a 6e cc 7b f1 bc af 79 d1 1f a5 e0 be 06 ...> },
|
||||
// infoBuffer: <Buffer 64 36 3a 6c 65 6e 67 74 68 69 33 36 32 30 31 37 65 34 3a 6e 61 6d 65 33 36 3a 4c 65 61 76 65 73 20 6f 66 20 47 72 61 73 73 20 62 79 20 57 61 6c 74 20 57 ...>,
|
||||
// infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
|
||||
// name: 'Leaves of Grass by Walt Whitman.epub',
|
||||
// private: false,
|
||||
// created: Thu Aug 01 2013 06:27:46 GMT-0700 (PDT),
|
||||
// comment: 'Downloaded from http://TheTorrent.org',
|
||||
// announce:
|
||||
// [ 'http://tracker.example.com/announce' ],
|
||||
// urlList: [],
|
||||
// files:
|
||||
// [ { path: 'Leaves of Grass by Walt Whitman.epub',
|
||||
// name: 'Leaves of Grass by Walt Whitman.epub',
|
||||
// length: 362017,
|
||||
// offset: 0 } ],
|
||||
// length: 362017,
|
||||
// pieceLength: 16384,
|
||||
// lastPieceLength: 1569,
|
||||
// pieces:
|
||||
// [ '1f9c3f59beec079715ec53324bde8569e4a0b4eb',
|
||||
// 'ec42307d4ce5557b5d3964c5ef55d354cf4a6ecc',
|
||||
// '7bf1bcaf79d11fa5e0be06593c8faafc0c2ba2cf',
|
||||
// '76d71c5b01526b23007f9e9929beafc5151e6511',
|
||||
// '0931a1b44c21bf1e68b9138f90495e690dbc55f5',
|
||||
// '72e4c2944cbacf26e6b3ae8a7229d88aafa05f61',
|
||||
// 'eaae6abf3f07cb6db9677cc6aded4dd3985e4586',
|
||||
// '27567fa7639f065f71b18954304aca6366729e0b',
|
||||
// '4773d77ae80caa96a524804dfe4b9bd3deaef999',
|
||||
// 'c9dd51027467519d5eb2561ae2cc01467de5f643',
|
||||
// '0a60bcba24797692efa8770d23df0a830d91cb35',
|
||||
// 'b3407a88baa0590dc8c9aa6a120f274367dcd867',
|
||||
// 'e88e8338c572a06e3c801b29f519df532b3e76f6',
|
||||
// '70cf6aee53107f3d39378483f69cf80fa568b1ea',
|
||||
// 'c53b506159e988d8bc16922d125d77d803d652c3',
|
||||
// 'ca3070c16eed9172ab506d20e522ea3f1ab674b3',
|
||||
// 'f923d76fe8f44ff32e372c3b376564c6fb5f0dbe',
|
||||
// '52164f03629fd1322636babb2c014b7dae582da4',
|
||||
// '1363965261e6ce12b43701f0a8c9ed1520a70eba',
|
||||
// '004400a267765f6d3dd5c7beb5bd3c75f3df2a54',
|
||||
// '560a61801147fa4ec7cf568e703acb04e5610a4d',
|
||||
// '56dcc242d03293e9446cf5e457d8eb3d9588fd90',
|
||||
// 'c698de9b0dad92980906c026d8c1408fa08fe4ec' ] }
|
||||
|
||||
var uri = parseTorrent.toMagnetURI({
|
||||
infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36'
|
||||
})
|
||||
|
||||
var buf = parseTorrent.toTorrentFile({
|
||||
info: {
|
||||
infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36'
|
||||
/* ... */
|
||||
}
|
||||
})
|
||||
|
||||
parseTorrent.remote('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36', function (err, parsedTorrent) {
|
||||
// if (err) throw err
|
||||
// console.log(parsedTorrent)
|
||||
})
|
||||
|
||||
42
parse-torrent/parse-torrent.d.ts
vendored
Normal file
42
parse-torrent/parse-torrent.d.ts
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
// Type definitions for parse-torrent
|
||||
// Project: https://github.com/feross/parse-torrent
|
||||
// Definitions by: Bazyli Brzóska <https://invent.life>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
/// <reference path='../node/node.d.ts' />
|
||||
|
||||
declare module ParseTorrent {
|
||||
export interface ParsedTorrent {
|
||||
infoHash:string;
|
||||
xt?:string;
|
||||
info?: { length:number, name:Buffer, 'piece length':number, pieces:Buffer };
|
||||
infoBuffer?:Buffer;
|
||||
name?:string;
|
||||
private?:boolean;
|
||||
created?:Date;
|
||||
comment?:string;
|
||||
announce?:Array<string>;
|
||||
urlList?:Array<string>;
|
||||
files?:Array<{path:string, name:string, length: number, offset:number}>;
|
||||
length?:number;
|
||||
pieceLength?:number;
|
||||
lastPieceLength?:number;
|
||||
pieces?:Array<string>;
|
||||
}
|
||||
|
||||
interface StaticInstance {
|
||||
(magnetUriOrInfoHash:string):ParsedTorrent;
|
||||
(torrentFileOrInfoHash:Buffer):{ info:ParsedTorrent };
|
||||
|
||||
toMagnetURI(parsedTorrent:ParsedTorrent):string;
|
||||
toTorrentFile(parsedTorrent:{ info:ParsedTorrent }):Buffer;
|
||||
|
||||
remote(remoteURLorLocalTorrentPath:string, onTorrentCallback?:(err:Error, parsedTorrent:ParsedTorrent)=>void):void;
|
||||
remote(torrentBlob:Blob, onTorrentCallback?:(err:Error, parsedTorrent:ParsedTorrent)=>void):void;
|
||||
}
|
||||
}
|
||||
|
||||
declare module "parse-torrent" {
|
||||
const parseTorrentStatic:ParseTorrent.StaticInstance;
|
||||
export = parseTorrentStatic;
|
||||
}
|
||||
78
webtorrent/webtorrent-tests.ts
Normal file
78
webtorrent/webtorrent-tests.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
/// <reference path="webtorrent.d.ts" />
|
||||
|
||||
import WebTorrent = require('webtorrent');
|
||||
import * as fs from 'fs';
|
||||
|
||||
var client = new WebTorrent()
|
||||
var magnetURI = '...'
|
||||
|
||||
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')
|
||||
|
||||
file.getBuffer(function (err, buffer) {
|
||||
if (err) throw err
|
||||
console.log(buffer) // <Buffer 00 98 00 01 01 00 00 00 50 ae 07 04 01 00 00 00 0a 00 00 00 00 00 00 00 78 ae 07 04 01 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
||||
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('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)
|
||||
})
|
||||
|
||||
var magnetURI = '...'
|
||||
|
||||
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:<port>/ to see a list of files
|
||||
|
||||
// access individual files at http://localhost:<port>/<index> where index is the index
|
||||
// in the torrent.files array
|
||||
|
||||
// later, cleanup...
|
||||
server.close()
|
||||
client.destroy()
|
||||
})
|
||||
329
webtorrent/webtorrent.d.ts
vendored
Normal file
329
webtorrent/webtorrent.d.ts
vendored
Normal file
@@ -0,0 +1,329 @@
|
||||
// Type definitions for WebTorrent
|
||||
// Project: https://webtorrent.io/
|
||||
// Definitions by: Bazyli Brzóska <https://invent.life>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
/// <reference path='../node/node.d.ts' />
|
||||
/// <reference path='../parse-torrent/parse-torrent.d.ts' />
|
||||
|
||||
declare module 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, // 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<string>, // 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): NodeJS.EventEmitter;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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): NodeJS.EventEmitter;
|
||||
|
||||
/**
|
||||
* 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 <input> 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?, onSeed?:(torrent:Torrent)=>void):void;
|
||||
seed(inputFile:File, opts?, onSeed?:(torrent:Torrent)=>void):void;
|
||||
seed(inputFileList:FileList, opts?, onSeed?:(torrent:Torrent)=>void):void;
|
||||
seed(inputBuffer:Buffer, opts?, onSeed?:(torrent:Torrent)=>void):void;
|
||||
seed(inputBuffersFilesOrPaths:Array<string|File|Buffer>, opts?, onSeed?:(torrent:Torrent)=>void):void;
|
||||
|
||||
/**
|
||||
* 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)=>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)=>void);
|
||||
|
||||
/**
|
||||
* An array of all torrents in the client.
|
||||
*/
|
||||
torrents:Array<Torrent>;
|
||||
|
||||
/**
|
||||
* 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): NodeJS.EventEmitter;
|
||||
|
||||
/**
|
||||
* 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<InTorrentFile>;
|
||||
|
||||
/**
|
||||
* The attached bittorrent-swarm instance.
|
||||
*/
|
||||
swarm; // 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);
|
||||
|
||||
/**
|
||||
* 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 /<index> where <index> is the index in the torrent.files array (e.g. /0, /1, etc.)
|
||||
*/
|
||||
createServer(opts?); // 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): NodeJS.EventEmitter;
|
||||
|
||||
/**
|
||||
* 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): NodeJS.EventEmitter;
|
||||
|
||||
/**
|
||||
* 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)=>void): NodeJS.EventEmitter;
|
||||
}
|
||||
|
||||
export interface InTorrentFile extends NodeJS.EventEmitter {
|
||||
on(event: string, listener: Function): NodeJS.EventEmitter;
|
||||
|
||||
/**
|
||||
* 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) // <Buffer 00 98 00 01 01 00 00 00 50 ae 07 04 01 00 00 00 0a 00 00 00 00 00 00 00 78 ae 07 04 01 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...>
|
||||
})
|
||||
|
||||
*/
|
||||
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, callback?:(err:Error, elem:Element) => void):void;
|
||||
|
||||
/**
|
||||
* Like file.appendTo but renders directly into given element (or CSS selector).
|
||||
*/
|
||||
renderTo(elem:Element, callback?:(err, elem) => 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): NodeJS.EventEmitter;
|
||||
}
|
||||
}
|
||||
|
||||
declare module "webtorrent" {
|
||||
const webTorrentStatic:WebTorrent.ClientConstructor;
|
||||
export = webTorrentStatic;
|
||||
}
|
||||
Reference in New Issue
Block a user