From d8b7c575345a50bc1002b672b8540af236b32719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=81aziuk?= Date: Wed, 18 Jan 2017 19:03:17 +0100 Subject: [PATCH] [bittorrent-protocol] new typings/package, version 2.1 (#14086) * bittorrent-protocol 2.1 * tsc fix --- .../bittorrent-protocol-tests.ts | 20 ++++ bittorrent-protocol/index.d.ts | 93 +++++++++++++++++++ bittorrent-protocol/tsconfig.json | 20 ++++ bittorrent-protocol/tslint.json | 3 + 4 files changed, 136 insertions(+) create mode 100644 bittorrent-protocol/bittorrent-protocol-tests.ts create mode 100644 bittorrent-protocol/index.d.ts create mode 100644 bittorrent-protocol/tsconfig.json create mode 100644 bittorrent-protocol/tslint.json diff --git a/bittorrent-protocol/bittorrent-protocol-tests.ts b/bittorrent-protocol/bittorrent-protocol-tests.ts new file mode 100644 index 0000000000..1d73154565 --- /dev/null +++ b/bittorrent-protocol/bittorrent-protocol-tests.ts @@ -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); diff --git a/bittorrent-protocol/index.d.ts b/bittorrent-protocol/index.d.ts new file mode 100644 index 0000000000..688527e71f --- /dev/null +++ b/bittorrent-protocol/index.d.ts @@ -0,0 +1,93 @@ +// Type definitions for bittorrent-protocol 2.1 +// Project: https://github.com/feross/bittorrent-protocol#readme +// Definitions by: Tomasz Łaziuk +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +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(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; diff --git a/bittorrent-protocol/tsconfig.json b/bittorrent-protocol/tsconfig.json new file mode 100644 index 0000000000..65222bf056 --- /dev/null +++ b/bittorrent-protocol/tsconfig.json @@ -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" + ] +} diff --git a/bittorrent-protocol/tslint.json b/bittorrent-protocol/tslint.json new file mode 100644 index 0000000000..ec365f164b --- /dev/null +++ b/bittorrent-protocol/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../tslint.json" +}