Skip to content

Commit

Permalink
fix(types): add brokers interface (#585)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Lando <[email protected]>
  • Loading branch information
getlarge and robertsLando authored Feb 24, 2021
1 parent 3e4f7a5 commit 234f49a
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 1 deletion.
7 changes: 7 additions & 0 deletions aedes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ declare namespace aedes {

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

type LastHearthbeatTimestamp = Date;

interface Brokers {
[brokerId: string]: LastHearthbeatTimestamp;
}

interface AedesOptions {
mq?: any
id?: string
Expand All @@ -101,6 +107,7 @@ declare namespace aedes {
id: string
connectedClients: number
closed: boolean
brokers: Brokers

handle: (stream: Connection) => Client

Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"unit:report": "tap --no-esm -J test/*.js --cov --coverage-report=html --coverage-report=cobertura | tee out.tap",
"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": "npm run lint && npm run unit && npm run typescript && tsd",
"test:types": "tsd",
"test:ci": "npm run lint && npm run unit -- --cov --coverage-report=lcovonly && npm run typescript",
"license-checker": "license-checker --production --onlyAllow=\"MIT;ISC;BSD-3-Clause;BSD-2-Clause\"",
"release": "read -p 'GITHUB_TOKEN: ' GITHUB_TOKEN && export GITHUB_TOKEN=$GITHUB_TOKEN && release-it --disable-metrics"
Expand All @@ -38,6 +39,9 @@
"pre-commit": [
"test"
],
"tsd": {
"directory": "test/types"
},
"repository": {
"type": "git",
"url": "https://github.com/moscajs/aedes.git"
Expand Down Expand Up @@ -103,6 +107,7 @@
"snazzy": "^9.0.0",
"standard": "^16.0.3",
"tap": "^14.11.0",
"tsd": "^0.14.0",
"typescript": "^4.1.2",
"websocket-stream": "^5.5.2"
},
Expand Down
163 changes: 163 additions & 0 deletions test/types/aedes.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* eslint no-unused-vars: 0 */
/* eslint no-undef: 0 */

import { expectType } from 'tsd'
import type {
Aedes,
Brokers,
Client,
Connection,
ConnackPacket,
AuthenticateError,
AedesPublishPacket,
PublishPacket,
Subscription,
SubscribePacket,
UnsubscribePacket
} from '../../aedes'
import { Server } from '../../aedes'
import type { Packet } from 'mqtt-packet'
import { Socket } from 'net'

// Aedes server
const broker = Server({
concurrency: 100,
heartbeatInterval: 60000,
connectTimeout: 30000,
id: 'aedes',
preConnect: (client: Client, packet: Packet, callback) => {
if (client.req) {
callback(new Error('not websocket stream'), false)
}
if (client.conn instanceof Socket && client.conn.remoteAddress === '::1') {
callback(null, true)
} else {
callback(new Error('connection error'), false)
}
},
authenticate: (client: Client, username: string, password: Buffer, callback) => {
if (username === 'test' && password === Buffer.from('test') && client.version === 4) {
callback(null, true)
} else {
const error = new Error() as AuthenticateError
error.returnCode = 1

callback(error, false)
}
},
authorizePublish: (client: Client, packet: PublishPacket, callback) => {
if (packet.topic === 'aaaa') {
return callback(new Error('wrong topic'))
}

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

callback(null)
},
authorizeSubscribe: (client: Client, sub: Subscription, callback) => {
if (sub.topic === 'aaaa') {
return callback(new Error('wrong topic'))
}

if (sub.topic === 'bbb') {
// overwrites subscription
sub.qos = 2
}

callback(null, sub)
},
authorizeForward: (client: Client, packet: AedesPublishPacket) => {
if (packet.topic === 'aaaa' && client.id === 'I should not see this') {
return null
// also works with return undefined
} else if (packet.topic === 'aaaa' && client.id === 'I should not see this either') {
return
}

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

return packet
}
})

expectType<Aedes>(broker)

expectType<Brokers>(broker.brokers)

expectType<Aedes>(broker.on('closed', () => {}))
expectType<Aedes>(broker.on('client', (client: Client) => {}))
expectType<Aedes>(broker.on('clientError', (client: Client, error: Error) => {}))
expectType<Aedes>(broker.on('connackSent', (packet: ConnackPacket, client: Client) => {}))

expectType<void>(broker.publish(
{} as PublishPacket,
(error?: Error) => {
if (error) {
console.error(error)
}
}
))

expectType<void>(broker.subscribe(
'topic',
(packet: AedesPublishPacket, callback: () => void) => {},
() => {}
))

expectType<void>(broker.unsubscribe(
'topic',
(packet: AedesPublishPacket, callback: () => void) => {},
() => {}
))

expectType<void>(broker.close())
expectType<void>(broker.close(() => {}))

// Aedes client
const client = broker.handle({} as Connection)

expectType<Client>(client)

expectType<Connection>(client.conn)

expectType<Client>(client.on('connected', () => {}))
expectType<Client>(client.on('error', (error: Error) => {
if (error) {
console.error(error)
}
}))

expectType<void>(client.publish({} as PublishPacket, (error?: Error) => {
if (error) {
console.error(error)
}
}))
expectType<void>(client.publish({} as PublishPacket))

expectType<void>(client.subscribe({} as Subscription, (error?: Error) => {
if (error) {
console.error(error)
}
}))
expectType<void>(client.subscribe({} as Subscription))
expectType<void>(client.subscribe([] as Subscription[]))
expectType<void>(client.subscribe({} as SubscribePacket))

expectType<void>(client.unsubscribe({} as Subscription, (error?: Error) => {
if (error) {
console.error(error)
}
}))
expectType<void>(client.unsubscribe({} as Subscription))
expectType<void>(client.unsubscribe([] as Subscription[]))
expectType<void>(client.unsubscribe({} as UnsubscribePacket))

expectType<void>(client.emptyOutgoingQueue())
expectType<void>(client.emptyOutgoingQueue(() => {}))

expectType<void>(client.close())
expectType<void>(client.close(() => {}))
2 changes: 2 additions & 0 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ const broker = Server({
}
})

const { brokers } = broker

const server = createServer(broker.handle)

broker.on('closed', () => {
Expand Down

0 comments on commit 234f49a

Please sign in to comment.