[bittorrent-protocol] new typings/package, version 2.1 (#14086)

* bittorrent-protocol 2.1

* tsc fix
This commit is contained in:
Tomasz Łaziuk
2017-01-18 10:03:17 -08:00
committed by Mohamed Hegazy
parent d775cf9ea3
commit d8b7c57534
4 changed files with 136 additions and 0 deletions
@@ -0,0 +1,20 @@
import * as Protocol from 'bittorrent-protocol';
import * as net from 'net';
net.createServer(function(socket) {
var wire = new Protocol();
// pipe to and from the protocol
socket.pipe(wire).pipe(socket);
wire.on('handshake', function(infoHash, peerId) {
// receive a handshake (infoHash and peerId are hex strings)
// lets emit a handshake of our own as well
wire.handshake('my info hash (hex)', 'my peer id (hex)');
});
wire.on('unchoke', function() {
console.log('peer is no longer choking us: ' + wire.peerChoking);
});
}).listen(6881);
+93
View File
@@ -0,0 +1,93 @@
// Type definitions for bittorrent-protocol 2.1
// Project: https://github.com/feross/bittorrent-protocol#readme
// Definitions by: Tomasz Łaziuk <https://github.com/tlaziuk>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import * as stream from 'stream';
declare const BittorrentProtocol: BittorrentProtocol.BittorrentProtocol;
declare namespace BittorrentProtocol {
export interface BittorrentProtocol {
new (): Wire;
(): Wire;
}
export interface Extension {
constructor(wire: Wire): this;
onHandshake?: () => void;
onExtendedHandshake?: () => void;
onMessage?: () => void;
name: string;
}
export interface Request {
constructor(piece: number, offset: number, length: number, callback: () => void): this;
piece: number;
offset: number;
length: number;
callback: () => void;
}
export interface Wire extends stream.Duplex {
readonly peerId: string; // remote peer id (hex string)
readonly peerIdBuffer: Buffer; // remote peer id (Buffer)
readonly type: 'webrtc' | 'tcpIncoming' | 'tcpOutgoing' | 'webSeed'; // connection type
readonly amChoking: boolean; // are we choking the peer?
readonly amInterested: boolean; // are we interested in the peer?
readonly peerChoking: boolean; // is the peer choking us?
readonly peerInterested: boolean; // is the peer interested in us?
readonly requests: Request[];
readonly peerRequests: Request[];
readonly extendedMapping: { [key: number]: string, };
readonly peerExtendedMapping: { [key: string]: number, };
setKeepAlive(enable: boolean): void;
setTimeot(ms: number, unref?: boolean): void;
destroy(): void;
use(ext: Extension): void;
handshake(infoHash: string | Buffer, peerId: string | Buffer, extensions?: any): void;
choke(): void;
unchoke(): void;
interested(): void;
uninterested(): void;
have(index: number): void;
// TODO: bitfield can be also a bitfield instance
bitfield(bitfield: Buffer | any): void;
request<T extends any>(index: number, offset: number, length: number, cb?: (err: Error) => T): T | void;
piece(index: number, offset: number, buffer: Buffer): void;
cancel(index: number, offset: number, length: number): void;
port(port: number): void;
extend(ext: number | string, obj: any): void;
// TODO: bitfield is a bitfield instance
on(event: 'bitfield', listener: (bitfield: any) => void): this;
on(event: string | 'keep-alive' | 'choke' | 'unchoke' | 'interested' | 'uninterested' | 'timeout', listener: () => void): this;
on(event: 'upload' | 'have' | 'download' | 'port', listener: (length: number) => void): this;
on(event: 'handshake', listener: (infoHash: string, peerId: string, extensions: Extension[]) => void): this;
on(event: 'request', listener: (index: number, offset: number, length: number, respond: () => void) => void): this;
on(event: 'piece', listener: (index: number, offset: number, buffer: Buffer) => void): this;
on(event: 'cancel', listener: (index: number, offset: number, length: number) => void): this;
on(event: 'extended', listener: (ext: 'handshake' | string, buf: any) => void): void;
on(event: 'unknownmessage', listener: (buffer: Buffer) => void): this;
}
}
export = BittorrentProtocol;
+20
View File
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"bittorrent-protocol-tests.ts"
]
}
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "../tslint.json"
}