From 785ddc7140d4de216675f30d266669ee83cab268 Mon Sep 17 00:00:00 2001 From: Toshiya Nakakura Date: Wed, 10 Sep 2014 21:24:12 +0900 Subject: [PATCH] add peerjs/Peer.d.ts --- CONTRIBUTORS.md | 1 + peerjs/Peer-tests.ts | 44 +++++++++++ peerjs/Peer.d.ts | 175 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+) create mode 100644 peerjs/Peer-tests.ts create mode 100644 peerjs/Peer.d.ts diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index ef83ff093c..58b79da84e 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -291,6 +291,7 @@ All definitions files include a header with the author and editors, so at some p * [Parallel.js](https://github.com/adambom/parallel.js) (by [Josh Baldwin](https://github.com/jbaldwin)) * [Parsimmon](https://github.com/jayferd/parsimmon) (by [Bart van der Schoor](https://github.com/Bartvds)) * [PDF.js](https://github.com/mozilla/pdf.js) (by [Josh Baldwin](https://github.com/jbaldwin)) +* [PeerJS](http://peerjs.com/) (by [Toshiya Nakakura](https://github.com/nakakura)) * [PEG.js](http://pegjs.majda.cz/) (by [vvakame](https://github.com/vvakame)) * [Persona](http://www.mozilla.org/en-US/persona) (by [James Frasca](https://github.com/Nycto)) * [PhantomJS](http://phantomjs.org) (by [Jed Hunsaker](https://github.com/jedhunsaker)) diff --git a/peerjs/Peer-tests.ts b/peerjs/Peer-tests.ts new file mode 100644 index 0000000000..5299c84bc5 --- /dev/null +++ b/peerjs/Peer-tests.ts @@ -0,0 +1,44 @@ +/// +/// + +var peerByOption: Peer = new Peer({ + key: 'peerKey', + debug: 3, + logFunction: ()=>{ + } +}); + +var peerById: Peer = new Peer("peerid"); + +var peerByIdAndOption: Peer = new Peer( + "peerId", + { + key: 'peerKey', + debug: 3, + logFunction: ()=>{ + } + }); + +var id = peerByOption.id; +var connections = peerByOption.connections; +var flag = peerByOption.disconnected; +flag = peerByOption.destroyed; + +peerByOption.disconnect(); +peerByOption.reconnect(); +peerByOption.destroy(); + +var connection = peerById.connect("id", { + label: 'chat', + serialization: 'none', + metadata: {message: 'hi i want to chat with you!'} +}); + +var call = peerById.call('callto-id', (window).localStream); + +peerById.on("open", ()=> console.log("open")); +peerById.on("connection", (c)=> console.log("connection")); +peerById.on("call", (media)=> console.log("call")); +peerById.on("close", ()=> console.log("close")); +peerById.on("disconnected", ()=> console.log("disconnected")); +peerById.on("error", (err)=> console.log(err)); diff --git a/peerjs/Peer.d.ts b/peerjs/Peer.d.ts new file mode 100644 index 0000000000..2d57877993 --- /dev/null +++ b/peerjs/Peer.d.ts @@ -0,0 +1,175 @@ +// Type definitions for PeerJS +// Project: http://peerjs.com/ +// Definitions by: Toshiya Nakakura +// Definitions: https://github.com/borisyankov/DefinitelyTyped + + +/// + +declare module PeerJs{ + interface PeerJSOption{ + key?: string; + host?: string; + port?: number; + path?: string; + secure?: boolean; + config?: RTCPeerConnectionConfig; + debug?: number; + } + + interface PeerConnectOption{ + label?: string; + metadata?: any; + serialization?: string; + reliable?: boolean; + + } + + interface DataConnection{ + send(data: any): void; + close(): void; + on(event: string, cb: ()=>void): void; + on(event: 'data', cb: (data: any)=>void): void; + on(event: 'open', cb: ()=>void): void; + on(event: 'close', cb: ()=>void): void; + on(event: 'error', cb: (err: any)=>void): void; + dataChannel: RTCDataChannel; + label: string; + metadata: any; + open: boolean; + peerConnection: any; + peer: string; + reliable: boolean; + serialization: string; + type: string; + buffSize: number; + } + + interface MediaConnection{ + answer(stream?: any): void; + close(): void; + on(event: string, cb: ()=>void): void; + on(event: 'stream', cb: (stream: any)=>void): void; + on(event: 'close', cb: ()=>void): void; + on(event: 'error', cb: (err: any)=>void): void; + open: boolean; + metadata: any; + peer: string; + type: string; + } + + interface utilSupportsObj { + audioVideo: boolean; + data: boolean; + binary: boolean; + reliable: boolean; + } + + interface util{ + browser: string; + supports: utilSupportsObj; + } +} + +interface Peer{ + /** + * + * @param id The brokering ID of the remote peer (their peer.id). + * @param options for specifying details about Peer Connection + */ + connect(id: string, options?: PeerJs.PeerJSOption): PeerJs.DataConnection; + /** + * Connects to the remote peer specified by id and returns a data connection. + * @param id The brokering ID of the remote peer (their peer.id). + * @param stream The caller's media stream + * @param options Metadata associated with the connection, passed in by whoever initiated the connection. + */ + call(id: string, stream: any, options?: any): PeerJs.MediaConnection; + /** + * Calls the remote peer specified by id and returns a media connection. + * @param event Event name + * @param cb Callback Function + */ + on(event: string, cb: ()=>void): void; + /** + * Emitted when a connection to the PeerServer is established. + * @param event Event name + * @param cb id is the brokering ID of the peer + */ + on(event: 'open', cb: (id: string)=>void): void; + /** + * Emitted when a new data connection is established from a remote peer. + * @param event Event name + * @param cb Callback Function + */ + on(event: 'connection', cb: (dataConnection: PeerJs.DataConnection)=>void): void; + /** + * Emitted when a remote peer attempts to call you. + * @param event Event name + * @param cb Callback Function + */ + on(event: 'call', cb: (mediaConnection: PeerJs.MediaConnection)=>void): void; + /** + * Emitted when the peer is destroyed and can no longer accept or create any new connections. + * @param event Event name + * @param cb Callback Function + */ + on(event: 'close', cb: ()=>void): void; + /** + * Emitted when the peer is disconnected from the signalling server + * @param event Event name + * @param cb Callback Function + */ + on(event: 'disconnected', cb: ()=>void): void; + /** + * Errors on the peer are almost always fatal and will destroy the peer. + * @param event Event name + * @param cb Callback Function + */ + on(event: 'error', cb: (err: any)=>void): void; + /** + * Close the connection to the server, leaving all existing data and media connections intact. + */ + disconnect(): void; + /** + * Attempt to reconnect to the server with the peer's old ID + */ + reconnect(): void; + /** + * Close the connection to the server and terminate all existing connections. + */ + destroy(): void; + /** + * The brokering ID of this peer + */ + id: string; + /** + * A hash of all connections associated with this peer, keyed by the remote peer's ID. + */ + connections: any; + /** + * false if there is an active connection to the PeerServer. + */ + disconnected: boolean; + /** + * true if this peer and all of its connections can no longer be used. + */ + destroyed: boolean; +} + +declare var Peer: { + prototype: RTCIceServer; + /** + * A peer can connect to other peers and listen for connections. + * @param id Other peers can connect to this peer using the provided ID. + * If no ID is given, one will be generated by the brokering server. + * @param options for specifying details about PeerServer + */ + new (id: string, options?: PeerJs.PeerJSOption): Peer; + + /** + * A peer can connect to other peers and listen for connections. + * @param options for specifying details about PeerServer + */ + new (options: PeerJs.PeerJSOption): Peer; +};