Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript definitions #43

Merged
merged 3 commits into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion P2PT.d.ts

This file was deleted.

18 changes: 16 additions & 2 deletions api-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* [`setIdentifier(identifierString)`](#setidentifieridentifierstring)
* [`start()`](#start)
* [`requestMorePeers()`](#requestmorepeers)
* [`removePeer(peerId)`](#removepeerpeerid)
* [`send(peer, msg[, msgID = ''])`](#sendpeer-msg-msgid--)
* [`destroy()`](#destroy)

Expand All @@ -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.

Expand Down Expand Up @@ -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<Msg>(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:**
Expand Down Expand Up @@ -113,4 +127,4 @@ Request More Peers
### `destroy()`
Destroy the P2PT Object
* **Arguments:** None
* **Returns:** `void`
* **Returns:** `void`
36 changes: 36 additions & 0 deletions p2pt.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
declare module "p2pt" {
import type { EventEmitter } from "events";
export default class P2PT<SendableMessage = any> extends EventEmitter {
_peerId: string;
constructor(announceURLs: Array<string> = [], identifierString = "");
setIdentifier(identifierString: string): void;
start(): void;
requestMorePeers(): Promise<object>;
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;
}
}