diff --git a/P2PT.d.ts b/P2PT.d.ts deleted file mode 100644 index f62353a..0000000 --- a/P2PT.d.ts +++ /dev/null @@ -1 +0,0 @@ -declare module 'P2PT'; \ No newline at end of file diff --git a/api-docs.md b/api-docs.md index 9a0d9c7..12c682a 100644 --- a/api-docs.md +++ b/api-docs.md @@ -11,7 +11,6 @@ * [`setIdentifier(identifierString)`](#setidentifieridentifierstring) * [`start()`](#start) * [`requestMorePeers()`](#requestmorepeers) - * [`removePeer(peerId)`](#removepeerpeerid) * [`send(peer, msg[, msgID = ''])`](#sendpeer-msg-msgid--) * [`destroy()`](#destroy) @@ -20,6 +19,11 @@ The P2PT class is defined and exposed by the `p2pt` module : ```javascript const P2PT = require('p2pt') ``` +In Typescript, you can use +```typescript +import P2PT from "p2pt"; +``` + This is the base class that needs to be instantiated to use this library. It provides the API to implement P2P connections, communicate messages (even large content!) using WebTorrent WebSocket Trackers as the signalling server. @@ -76,6 +80,16 @@ var trackersAnnounceURLs = [ var p2pt = new P2PT(trackersAnnounceURLs, 'myApp') ``` +In Typescript, the `P2PT` class accepts an optional type parameter to constrain the type of messages you can pass to the `send` function. It doesn't constrain the type of messages you recieve, since any peer *could* send anything. +```typescript +type Msg = 'hello' | { goodbye: boolean } +const p2pt = new P2PT(trackersAnnounceURLs, 'myApp') +// ... find a peer ... +p2pt.send(peer, 'some_message') // TS typecheck error: Argument of type 'string' is not assignable to parameter of type 'Msg'. +p2pt.send(peer, 'hello') // ok! +p2pt.send(peer, { goodbye: true }) // ok! +``` + ### `setIdentifier(identifierString)` Sets the identifier string used to discover peers in the network * **Arguments:** @@ -113,4 +127,4 @@ Request More Peers ### `destroy()` Destroy the P2PT Object * **Arguments:** None -* **Returns:** `void` \ No newline at end of file +* **Returns:** `void` diff --git a/p2pt.d.ts b/p2pt.d.ts new file mode 100644 index 0000000..a5a7c30 --- /dev/null +++ b/p2pt.d.ts @@ -0,0 +1,36 @@ +declare module "p2pt" { + import type { EventEmitter } from "events"; + export default class P2PT extends EventEmitter { + _peerId: string; + constructor(announceURLs: Array = [], identifierString = ""); + setIdentifier(identifierString: string): void; + start(): void; + requestMorePeers(): Promise; + send( + peer: Peer, + msg: SendableMessage, + msgID: number = "" + ): Promise<[peer: Peer, msg: object]>; + destroy(): void; + + on(event: "peerconnect", callback: (peer: Peer) => void); + on(event: "data", callback: (peer: Peer, data: any) => void); + on(event: "msg", callback: (peer: Peer, msg: any) => void); + on(event: "peerclose", callback: (peer: Peer) => void); + on( + event: "trackerconnect", + callback: (tracker: Tracker, stats: object) => void + ); + on( + event: "trackerwarning", + callback: (error: object, stats: object) => void + ); + } + export interface Peer { + id: string; + respond(msg: SendableMessage): Promise<[peer: Peer, msg: any]>; + } + export interface Tracker { + announceUrl: string; + } +}