From 43c008f49b77581a64d1a7f09ebde827fa3e31a7 Mon Sep 17 00:00:00 2001 From: Ari Lotter Date: Sun, 5 Dec 2021 13:29:40 -0500 Subject: [PATCH 1/3] add typescript definitions, with ability to constrain the type that's sent :) --- P2PT.d.ts | 1 - p2pt.d.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) delete mode 100644 P2PT.d.ts create mode 100644 p2pt.d.ts 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/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; + } +} From 794dee30e044c414cac042e700a10ca11e1d76a4 Mon Sep 17 00:00:00 2001 From: Ari Lotter Date: Sun, 5 Dec 2021 13:30:01 -0500 Subject: [PATCH 2/3] remove `removePeer` from docs, because it's undocumented. --- api-docs.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api-docs.md b/api-docs.md index 9a0d9c7..7eb2e72 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) @@ -113,4 +112,4 @@ Request More Peers ### `destroy()` Destroy the P2PT Object * **Arguments:** None -* **Returns:** `void` \ No newline at end of file +* **Returns:** `void` From 441cd635454c74c78cc0c41bd4a01804c6cb7675 Mon Sep 17 00:00:00 2001 From: Ari Lotter Date: Sun, 5 Dec 2021 13:36:22 -0500 Subject: [PATCH 3/3] add documentation for Typescript types --- api-docs.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/api-docs.md b/api-docs.md index 7eb2e72..12c682a 100644 --- a/api-docs.md +++ b/api-docs.md @@ -19,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. @@ -75,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:**