From 90cb7c7fdf3d029960b9d27823660593d9497b80 Mon Sep 17 00:00:00 2001 From: Dan VerWeire Date: Tue, 6 Oct 2020 20:32:02 -0400 Subject: [PATCH] add index.d.ts issue #44 --- index.d.ts | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +- 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..3ea4ec9 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,90 @@ +declare module 'node-discover' { + import { EventEmitter } from 'events'; + import { RemoteInfo } from 'dgram'; + + type Options = { + helloInterval?: number; + checkInterval?: number; + nodeTimeout?: number; + masterTimeout?: number; + address?: string; + port?: number; + broadcast?: string; + multicast?: string; + multicastTTL?: number; + unicast?: string | string[]; + key?: string; + mastersRequired?: number; + weight?: number; + client?: boolean; + server?: boolean; + reuseAddr?: boolean; + exclusive?: boolean; + ignoreProcess?: boolean; + ignoreInstance?: boolean; + start?: boolean; + hostname?: string; + }; + + type Message = { + event: string; + pid: string; + iid: string; + hostName: string; + data?: T; + }; + + type Node = { + isMaster: boolean; + isMasterEligible: boolean; + weight: number; + address: string; + lastSeen: number; + hostName: string; + port: number; + id: string; + advertisement?: A; + }; + + type ThisNode = { + isMaster: boolean; + isMasterEligible: boolean; + weight: number; + address: string; + advertisement?: A; + }; + + type ChannelListener = (data: D, obj: Message, rinfo: RemoteInfo) => void; + + class Discover=Record> extends EventEmitter { + nodes: Record; + constructor(callback?: (error: Error, something: boolean) => void); + constructor(options?: Options, callback?: (error: Error, success: boolean) => void); + + promote(): void; + demote(permanent: boolean): void; + join(channel: T, cb: ChannelListener): boolean; + leave(channel: T): boolean; + advertise(advertisement: A): void; + send(channel: T, obj: C[T]): boolean; + start(callback?: (error: Error, success: boolean) => void): false | boolean; + stop(): false | boolean; + eachNode(fn: (node: Node) => void): void; + + on(event: 'promotion', listener: (me: ThisNode) => void): this; + on(event: 'demotion', listener: (me: ThisNode) => void): this; + on(event: 'added', listener: ChannelListener>): this; + on(event: 'removed', listener: (node: Node) => void): this; + on(event: 'master', listener: ChannelListener>): this; + on(event: 'helloReceived', listener: (node: Node, obj: Message>, rinfo: RemoteInfo, isNew: boolean, wasMaster: null | boolean) => void): this; + on(event: 'helloEmitted', listener: () => void): this; + on(event: 'error', listener: (error: Error) => void): this; + on(event: 'check', listener: () => void): this; + on(event: 'started', listener: (self: Discover) => void): this; + on(event: 'stopped', listener: (self: Discover) => void): this; + on(channel: 'hello', listener: ChannelListener): this; + on(channel: T, listener: ChannelListener): this; + } + + export = Discover; + } \ No newline at end of file diff --git a/package.json b/package.json index 204c4f9..d6977a4 100644 --- a/package.json +++ b/package.json @@ -40,5 +40,6 @@ "split", "brain" ], - "license": "MIT" + "license": "MIT", + "types": "./index.d.ts" }