From c4e3c4400aa0b220d5d51c2198c8a69dd4a67880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20=C5=81aziuk?= Date: Wed, 22 Mar 2017 09:01:29 +0100 Subject: [PATCH] improved Extension typings --- .../bittorrent-protocol-tests.ts | 7 +++++++ bittorrent-protocol/index.d.ts | 21 ++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/bittorrent-protocol/bittorrent-protocol-tests.ts b/bittorrent-protocol/bittorrent-protocol-tests.ts index 1d0d17793a..227e4c3606 100644 --- a/bittorrent-protocol/bittorrent-protocol-tests.ts +++ b/bittorrent-protocol/bittorrent-protocol-tests.ts @@ -1,9 +1,16 @@ import * as Protocol from 'bittorrent-protocol'; import * as net from 'net'; +class TestExtension implements Protocol.Extension { + constructor(protected wire: Protocol.Wire) { } + name: 'extname'; +} + net.createServer(socket => { var wire = new Protocol(); + wire.use(TestExtension); + // pipe to and from the protocol socket.pipe(wire).pipe(socket); diff --git a/bittorrent-protocol/index.d.ts b/bittorrent-protocol/index.d.ts index 2570dd7132..81d2426d62 100644 --- a/bittorrent-protocol/index.d.ts +++ b/bittorrent-protocol/index.d.ts @@ -1,6 +1,6 @@ -// Type definitions for bittorrent-protocol 2.1 +// Type definitions for bittorrent-protocol 2.2 // Project: https://github.com/feross/bittorrent-protocol#readme -// Definitions by: Tomasz Łaziuk +// Definitions by: Feross Aboukhadijeh , Tomasz Łaziuk // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// @@ -15,18 +15,19 @@ declare namespace BittorrentProtocol { (): Wire; } + export interface ExtensionConstructor { + new (wire: Wire): Extension; + } + export interface Extension { - // tslint:disable-next-line:no-misused-new - could use class instead of interface but class is not extendible - constructor(wire: Wire): this; - onHandshake?: () => void; - onExtendedHandshake?: () => void; - onMessage?: () => void; + onHandshake?: (infoHash: string, peerId: string, extensions: { [name: string]: boolean }) => void; + onExtendedHandshake?: (handshake: { [key: string]: any }) => void; + onMessage?: (buf: Buffer) => void; name: string; } export interface Request { - //tslint:disable-next-line:no-misused-new - could use class instead of interface but class is not extendible - constructor(piece: number, offset: number, length: number, callback: () => void): this; + new (piece: number, offset: number, length: number, callback: () => void): this; piece: number; offset: number; length: number; @@ -52,7 +53,7 @@ declare namespace BittorrentProtocol { destroy(): void; - use(ext: Extension): void; + use(ext: ExtensionConstructor): void; handshake(infoHash: string | Buffer, peerId: string | Buffer, extensions?: any): void;