Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: typescript types issues #812

Merged
merged 7 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions aedes.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/// <reference path="./types/instance.d.ts" />
/// <reference path="./types/client.d.ts" />
/// <reference path="./types/packet.d.ts" />
import Aedes, { AedesOptions } from './types/instance'

export declare function createBroker (options?: AedesOptions): Aedes

export * from './types/instance'
export * from './types/packet'
export * from './types/client'
export default Aedes
2 changes: 1 addition & 1 deletion aedes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const mqemitter = require('mqemitter')
const Client = require('./lib/client')
const { $SYS_PREFIX, bulk } = require('./lib/utils')

module.exports = Aedes.Server = Aedes
module.exports = Aedes.createBroker = Aedes

const defaultOptions = {
concurrency: 100,
Expand Down
16 changes: 16 additions & 0 deletions docs/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ server.listen(port, function () {
})
```

## Typescript

```ts
import Aedes from 'aedes'
import { createServer } from 'net'

const port = 1883

const aedes = new Aedes()
const server = createServer(aedes.handle)

server.listen(port, function () {
console.log('server started and listening on port ', port)
})
```

## Simple plain MQTT server using server-factory

```js
Expand Down
4 changes: 2 additions & 2 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const { setup, connect, subscribe, subscribeMultiple, noError } = require('./hel
const aedes = require('../')
const proxyquire = require('proxyquire')

test('test aedes.Server', function (t) {
test('test aedes.createBroker', function (t) {
t.plan(1)

const broker = new aedes.Server()
const broker = aedes.createBroker()
t.teardown(broker.close.bind(broker))

connect(setup(broker), {}, function () {
Expand Down
164 changes: 107 additions & 57 deletions test/types/aedes.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/// <reference path="../../aedes.d.ts" />

import type { AedesPublishPacket, ConnackPacket, ConnectPacket, PingreqPacket, PublishPacket, PubrelPacket, SubscribePacket, Subscription, UnsubscribePacket } from 'aedes:packet'
import type { AuthenticateError, Brokers, Connection } from 'aedes:server'
import Server, { Aedes } from 'aedes:server'

import { IncomingMessage } from 'node:http'
import type { Client } from 'aedes:client'
import { Socket } from 'node:net'
import type {
Brokers,
AuthenticateError,
Client,
Connection
} from '../../aedes'
import Aedes, { createBroker } from '../../aedes'
import type { AedesPublishPacket, ConnackPacket, ConnectPacket, PingreqPacket, PublishPacket, PubrelPacket, Subscription, SubscribePacket, UnsubscribePacket } from '../../types/packet'
import { expectType } from 'tsd'

// Aedes server
let broker = new Server()
let broker = createBroker()
expectType<Aedes>(broker)

broker = new Aedes({
Expand All @@ -29,8 +31,17 @@ broker = new Aedes({
callback(new Error('connection error'), false)
}
},
authenticate: (client: Client, username: Readonly<string | undefined>, password: Readonly<Buffer | undefined>, callback) => {
if (username === 'test' && password === Buffer.from('test') && client.version === 4) {
authenticate: (
client: Client,
username: Readonly<string | undefined>,
password: Readonly<Buffer | undefined>,
callback
) => {
if (
username === 'test' &&
password === Buffer.from('test') &&
client.version === 4
) {
callback(null, true)
} else {
const error = new Error() as AuthenticateError
Expand All @@ -39,7 +50,11 @@ broker = new Aedes({
callback(error, false)
}
},
authorizePublish: (client: Client | null, packet: PublishPacket, callback) => {
authorizePublish: (
client: Client | null,
packet: PublishPacket,
callback
) => {
if (packet.topic === 'aaaa') {
return callback(new Error('wrong topic'))
}
Expand All @@ -66,7 +81,10 @@ broker = new Aedes({
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') {
} else if (
packet.topic === 'aaaa' &&
client.id === 'I should not see this either'
) {
return
}

Expand All @@ -91,75 +109,107 @@ expectType<Aedes>(broker.on('client', (client: Client) => {}))
expectType<Aedes>(broker.on('clientReady', (client: Client) => {}))
expectType<Aedes>(broker.on('clientDisconnect', (client: Client) => {}))
expectType<Aedes>(broker.on('keepaliveTimeout', (client: Client) => {}))
expectType<Aedes>(broker.on('clientError', (client: Client, error: Error) => {}))
expectType<Aedes>(broker.on('connectionError', (client: Client, error: Error) => {}))
expectType<Aedes>(broker.on('connackSent', (packet: ConnackPacket, client: Client) => {}))
expectType<Aedes>(broker.on('ping', (packet: PingreqPacket, client: Client) => {}))
expectType<Aedes>(broker.on('publish', (packet: AedesPublishPacket, client: Client | null) => {}))
expectType<Aedes>(broker.on('ack', (packet: PublishPacket | PubrelPacket, client: Client) => {}))
expectType<Aedes>(broker.on('subscribe', (subscriptions: Subscription[], client: Client) => {}))
expectType<Aedes>(broker.on('unsubscribe', (unsubscriptions: string[], client: Client) => {}))

expectType<void>(broker.publish(
{} as PublishPacket,
(error?: Error) => {
expectType<Aedes>(
broker.on('clientError', (client: Client, error: Error) => {})
)
expectType<Aedes>(
broker.on('connectionError', (client: Client, error: Error) => {})
)
expectType<Aedes>(
broker.on('connackSent', (packet: ConnackPacket, client: Client) => {})
)
expectType<Aedes>(
broker.on('ping', (packet: PingreqPacket, client: Client) => {})
)
expectType<Aedes>(
broker.on(
'publish',
(packet: AedesPublishPacket, client: Client | null) => {}
)
)
expectType<Aedes>(
broker.on('ack', (packet: PublishPacket | PubrelPacket, client: Client) => {})
)
expectType<Aedes>(
broker.on('subscribe', (subscriptions: Subscription[], client: Client) => {})
)
expectType<Aedes>(
broker.on('unsubscribe', (unsubscriptions: string[], 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.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, {} as IncomingMessage)
const client2 = broker.handle({} as Connection)

expectType<Client>(client)
expectType<Client>(client2)

expectType<Connection>(client.conn)
expectType<IncomingMessage>(client.req!)

expectType<Client>(client.on('connected', () => {}))
expectType<Client>(client.on('error', (error: Error) => {
if (error) {
console.error(error)
}
}))
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, (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, (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, (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))
Expand Down
68 changes: 40 additions & 28 deletions types/client.d.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
declare module 'client' {
import { IncomingMessage } from 'node:http'
import { PublishPacket, SubscribePacket, Subscription, Subscriptions, UnsubscribePacket } from 'aedes:packet'
import { Connection } from 'aedes:server'
import { EventEmitter } from 'node:events'
import { IncomingMessage } from 'node:http'
import {
PublishPacket,
SubscribePacket,
Subscription,
Subscriptions,
UnsubscribePacket
} from './packet'
import { Connection } from './instance'
import { EventEmitter } from 'node:events'

export interface Client extends EventEmitter {
id: Readonly<string>
clean: Readonly<boolean>
version: Readonly<number>
conn: Connection
req?: IncomingMessage
connecting: Readonly<boolean>
connected: Readonly<boolean>
closed: Readonly<boolean>
export interface Client extends EventEmitter {
id: Readonly<string>;
clean: Readonly<boolean>;
version: Readonly<number>;
conn: Connection;
req?: IncomingMessage;
connecting: Readonly<boolean>;
connected: Readonly<boolean>;
closed: Readonly<boolean>;

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

publish (message: PublishPacket, callback?: (error?: Error) => void): void
subscribe (
subscriptions: Subscriptions | Subscription | Subscription[] | SubscribePacket,
callback?: (error?: Error) => void
): void
unsubscribe (topicObjects: Subscriptions | Subscription | Subscription[] | UnsubscribePacket, callback?: (error?: Error) => void): void
close (callback?: () => void): void
emptyOutgoingQueue (callback?: () => void): void
}
}
declare module 'aedes:client' {
export * from 'client'
publish(message: PublishPacket, callback?: (error?: Error) => void): void;
subscribe(
subscriptions:
| Subscriptions
| Subscription
| Subscription[]
| SubscribePacket,
callback?: (error?: Error) => void
): void;
unsubscribe(
topicObjects:
| Subscriptions
| Subscription
| Subscription[]
| UnsubscribePacket,
callback?: (error?: Error) => void
): void;
close(callback?: () => void): void;
emptyOutgoingQueue(callback?: () => void): void;
}
Loading