Skip to content

Commit

Permalink
Refactored typescript defintion (#394)
Browse files Browse the repository at this point in the history
* Refactored typescript defintion

* Relocated typescript files & tests
  • Loading branch information
gnought committed Feb 2, 2020
1 parent 9c84676 commit 26473cd
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 126 deletions.
104 changes: 104 additions & 0 deletions aedes.d.ts
@@ -0,0 +1,104 @@
/* eslint no-unused-vars: 0 */
/* eslint no-undef: 0 */
/* eslint space-infix-ops: 0 */

/// <reference types="node" />

import { IPublishPacket, ISubscribePacket, ISubscription, IUnsubscribePacket } from 'mqtt-packet'
import { Duplex } from 'stream'
import { Socket } from 'net'
import { IncomingMessage } from 'http'
import EventEmitter = NodeJS.EventEmitter

declare function aedes (options?: aedes.AedesOptions): aedes.Aedes

// eslint-disable-next-line no-redeclare
declare namespace aedes {
enum AuthErrorCode {
UNNACCEPTABLE_PROTOCOL = 1,
IDENTIFIER_REJECTED = 2,
SERVER_UNAVAILABLE = 3,
BAD_USERNAME_OR_PASSWORD = 4
}

type PreConnectHandler = (client: Client, callback: (err: Error | null, success: boolean) => void) => void

type AuthenticateError = Error & { returnCode: AuthErrorCode }

type AuthenticateHandler = (
client: Client,
username: string,
password: string,
done: (err: AuthenticateError | null, success: boolean | null) => void
) => void

type AuthorizePublishHandler = (client: Client, packet: IPublishPacket, callback: (err?: Error | null) => void) => void

type AuthorizeSubscribeHandler = (client: Client, subscription: ISubscription, callback: (err: Error | null, subscription?: ISubscription | null) => void) => void

type AuthorizeForwardHandler = (client: Client, packet: IPublishPacket) => IPublishPacket | null | void

type PublishedHandler = (packet: IPublishPacket, client: Client, callback: (err?: Error | null) => void) => void

interface AedesOptions {
mq?: any
persistence?: any
concurrency?: number
heartbeatInterval?: number
connectTimeout?: number
preConnect?: PreConnectHandler
authenticate?: AuthenticateHandler
authorizePublish?: AuthorizePublishHandler
authorizeSubscribe?: AuthorizeSubscribeHandler
authorizeForward?: AuthorizeForwardHandler
published?: PublishedHandler
queueLimit?: number
}
interface Client extends EventEmitter {
id: string
clean: boolean
conn: Socket
req?: IncomingMessage

on (event: 'connected', callback: () => void): this
on (event: 'error', callback: (error: Error) => void): this

publish (message: IPublishPacket, callback?: (error?: Error) => void): void
subscribe (
subscriptions: ISubscription | ISubscription[] | ISubscribePacket,
callback?: (error?: Error) => void
): void
unsubscribe (topicObjects: ISubscription | ISubscription[], callback?: (error?: Error) => void): void
close (callback?: () => void): void
}

interface Aedes extends EventEmitter {
handle: (stream: Duplex) => void

on (event: 'closed', callback: () => void): this
on (event: 'client' | 'clientReady' | 'clientDisconnect' | 'keepaliveTimeout' | 'connackSent', callback: (client: Client) => void): this
on (event: 'clientError' | 'connectionError', callback: (client: Client, error: Error) => void): this
on (event: 'ping' | 'publish' | 'ack', callback: (packet: any, client: Client) => void): this
on (event: 'subscribe' | 'unsubscribe', callback: (subscriptions: ISubscription | ISubscription[] | ISubscribePacket, client: Client) => void): this

publish (
packet: IPublishPacket & { topic: string | Buffer },
callback: () => void
): void
subscribe (
topic: string,
deliverfunc: (packet: ISubscribePacket, callback: () => void) => void,
callback: () => void
): void
unsubscribe (
topic: string,
deliverfunc: (packet: IUnsubscribePacket, callback: () => void) => void,
callback: () => void
): void
close (callback?: () => void): void
}

function Server (options?: aedes.AedesOptions): aedes.Aedes
}

export = aedes
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -3,14 +3,14 @@
"version": "0.40.1",
"description": "Stream-based MQTT broker",
"main": "aedes.js",
"types": "types/index.d.ts",
"types": "aedes.d.ts",
"scripts": {
"lint": "npm run lint:standard && npm run lint:typescript",
"lint:standard": "standard --verbose | snazzy",
"lint:typescript": "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin types/**/*.d.ts",
"lint:typescript": "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin test/types/*.ts aedes.d.ts",
"unit": "tap --no-esm -J test/*.js",
"unit:report": "tap --no-esm -J test/*.js --cov --coverage-report=html --coverage-report=cobertura | tee out.tap",
"typescript": "tsc --project ./test/typescript/tsconfig.json",
"typescript": "tsc --project ./test/types/tsconfig.json",
"test:report": "npm run lint && npm run unit:report && npm run typescript",
"test": "npm run lint && npm run unit && npm run typescript",
"test:ci": "npm run lint && npm run unit -- --cov --coverage-report=lcovonly && npm run typescript",
Expand Down
11 changes: 6 additions & 5 deletions test/typescript/typings.ts → test/types/index.ts
@@ -1,6 +1,7 @@
// relative path uses package.json {"types":"types/index.d.ts", ...}
/* eslint no-unused-vars: 0 */
/* eslint no-undef: 0 */

import { Server, Client, AuthenticateError } from '../..'
import { Server, Client, AuthenticateError } from '../../aedes'
import { IPublishPacket, ISubscribePacket, ISubscription, IUnsubscribePacket } from 'mqtt-packet'
import { createServer } from 'net'

Expand Down Expand Up @@ -34,7 +35,7 @@ const broker = Server({
}

if (packet.topic === 'bbb') {
packet.payload = new Buffer('overwrite packet payload')
packet.payload = Buffer.from('overwrite packet payload')
}

callback(null)
Expand All @@ -60,7 +61,7 @@ const broker = Server({
}

if (packet.topic === 'bbb') {
packet.payload = new Buffer('overwrite packet payload')
packet.payload = Buffer.from('overwrite packet payload')
}

return packet
Expand All @@ -70,7 +71,7 @@ const broker = Server({
const server = createServer(broker.handle)

broker.on('closed', () => {
console.log(`closed`)
console.log('closed')
})

broker.on('client', client => {
Expand Down
11 changes: 11 additions & 0 deletions test/types/tsconfig.json
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noEmit": true,
"strict": true
},
"files": [
"./index.ts"
]
}
14 changes: 0 additions & 14 deletions test/typescript/tsconfig.json

This file was deleted.

104 changes: 0 additions & 104 deletions types/index.d.ts

This file was deleted.

0 comments on commit 26473cd

Please sign in to comment.