From 8c4a7cf13b9a8aa6f1b85cbb501515b025115030 Mon Sep 17 00:00:00 2001 From: Jonas Kruckenberg Date: Sat, 4 Apr 2020 00:59:32 +0200 Subject: [PATCH] Add Typings --- index.d.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 55 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..8836380 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,54 @@ +import Nanoresource from 'nanoresource' +import UtpNative from 'utp-native' +import { Server, Socket } from 'net' +import { Discovery, Peer, Topic, AnnounceOptions } from '@hyperswarm/discovery' + +declare function _exports(opts?: NetworkResourceOptions): NetworkResource; +export default _exports +export { NetworkResource }; + +export interface NetworkResourceOptions { + bind?: () => void + close?: () => void + socket?: () => void + bootstrap?: Array + ephemeral?:boolean +} + +declare class NetworkResource extends Nanoresource { + constructor(opts?: NetworkResourceOptions); + preferredPort: number; + tcp: Server; + utp: UtpNative; + discovery: Discovery; + options: NetworkResourceOptions; + sockets: Set; + private _announceLocalAddress: boolean; + private _onbind: () => void; + private _onclose: () => void; + private _onsocket: () => void; + private _bootstrap: Array; + private _ephemeral: boolean; + private _onincoming(isTCP: boolean, socket: Socket): void; + address(): { + host: string; + port: number; + }; + /** Connect to a peer. Will do UDP holepunching. If the underlying socket is a TCP socket isTCP will be true, if it is a UTP socket it will be false. */ + connect(peer: Peer, cb: ( err:Error, socket:Socket, isTCP:boolean ) => void ): void; + /** Start announcing the network on the Hyperswarm discovery network. */ + announce(topic:Buffer, options?: AnnounceOptions): Topic; + /** Start doing a lookup on the Hyperswarm discovery network. */ + lookupOne(topic:Buffer, cd?: () => void ): void; + /** Lookup a single peer on the Hyperswarm discovery network. */ + lookup(topic:Buffer, opts?: boolean): Topic; + /** Bind to a preferred port. Must be called before connecting. Safe to call multiple times. If already bound or binding it will call the callback when fully bound. */ + bind(preferredPort?: number, cb?: () => void ): void; + private _localAddress(): { + host: string; + port: number; + }; + private _open(cb: any): void; + private _removeSocket(socket: any): void; + private _close(cb: any): void; +} diff --git a/package.json b/package.json index e8ccd75..ba744dc 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ }, "devDependencies": { "@hyperswarm/dht": "0.0.1", + "@types/node": "^13.11.0", "events.once": "^2.0.2", "get-port": "^5.0.0", "standard": "^12.0.1",