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: add types definitions #76

Merged
merged 11 commits into from
Mar 5, 2021
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"version": "8.1.2",
"description": "The spec for an Aedes persistence, with abstract tests and a fast in-memory implementation.",
"main": "persistence.js",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard --verbose | snazzy",
"lint-fix": "standard --fix",
"unit": "tape test.js | faucet",
"test": "npm run lint && npm run unit",
"test:types": "tsd",
"test": "npm run lint && npm run unit && tsd",
"coverage": "nyc --reporter=lcov tape test.js",
"test:ci": "npm run lint && npm run coverage",
"license-checker": "license-checker --production --onlyAllow='MIT;ISC;BSD-3-Clause;BSD-2-Clause'",
Expand Down Expand Up @@ -58,6 +60,7 @@
"node": ">=10"
},
"devDependencies": {
"aedes": "^0.44.2",
robertsLando marked this conversation as resolved.
Show resolved Hide resolved
"concat-stream": "^2.0.0",
"faucet": "0.0.1",
"license-checker": "^25.0.1",
Expand All @@ -69,7 +72,8 @@
"snazzy": "^9.0.0",
"standard": "^15.0.1",
"tape": "^4.13.2",
"through2": "^4.0.2"
"through2": "^4.0.2",
"tsd": "^0.14.0"
},
"dependencies": {
"aedes-packet": "^2.3.1",
Expand Down
153 changes: 153 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import type { Client } from 'aedes';
import type { AedesPacket } from 'aedes-packet';
import type { Readable } from 'stream';

export type { AedesPacket as Packet } from 'aedes-packet';

export interface AedesPersistenceSubscription {
clientId?: string;
topic: string;
qos: number;
}
getlarge marked this conversation as resolved.
Show resolved Hide resolved
getlarge marked this conversation as resolved.
Show resolved Hide resolved

type LastHearthbeatTimestamp = Date;

export interface Brokers {
[brokerId: string]: LastHearthbeatTimestamp;
}
getlarge marked this conversation as resolved.
Show resolved Hide resolved

export type CallbackError = Error | null | undefined;

export type WillPacket = AedesPacket & { [key: string]: any };

interface Incoming {
[clientId: string]: { [messageId: string]: AedesPacket };
}

export class AedesMemoryPersistence {
getlarge marked this conversation as resolved.
Show resolved Hide resolved
private _retained: AedesPacket[];
private _subscriptions: Map<string, Map<string, number>>;
getlarge marked this conversation as resolved.
Show resolved Hide resolved
private _clientsCount: number;
private _trie: any;
private _outgoing: Record<string, AedesPacket[]>;
private _incoming: Incoming;
private _wills: Record<string, WillPacket>;
getlarge marked this conversation as resolved.
Show resolved Hide resolved

constructor();

storeRetained: (
packet: AedesPacket,
cb: (error: CallbackError) => void
) => void;

createRetainedStream: (pattern: string) => Readable;

createRetainedStreamCombi: (patterns: string[]) => Readable;

addSubscriptions: (
client: Client,
subs: AedesPersistenceSubscription[],
cb: (error: CallbackError, client: Client) => void
) => void;

removeSubscriptions: (
client: Client,
subs: AedesPersistenceSubscription[],
cb: (error: CallbackError, client: Client) => void
) => void;

subscriptionsByClient: (
client: Client,
cb: (
error: CallbackError,
subs: AedesPersistenceSubscription[],
client: Client
) => void
) => void;

countOffline: (
cb: (
error: CallbackError,
subscriptionsCount: number,
clientsCount: number
) => void
) => void;

subscriptionsByTopic: (
pattern: string,
cb: (error: CallbackError, subs: AedesPersistenceSubscription[]) => void
) => void;

cleanSubscriptions: (
client: Client,
cb: (error: CallbackError, client: Client) => void
) => void;

outgoingEnqueue: (
sub: AedesPersistenceSubscription,
packet: AedesPacket,
cb: (error: CallbackError) => void
) => void;

outgoingEnqueueCombi: (
subs: AedesPersistenceSubscription[],
packet: AedesPacket,
cb: (error: CallbackError) => void
) => void;

outgoingUpdate: (
client: Client,
packet: AedesPacket,
cb: (error: CallbackError, client: Client, packet: AedesPacket) => void
) => void;

outgoingClearMessageId: (
client: Client,
packet: AedesPacket,
cb: (error?: CallbackError, packet?: AedesPacket) => void
) => void;

outgoingStream: (client: Client) => Readable;

incomingStorePacket: (
client: Client,
packet: AedesPacket,
cb: (error: CallbackError) => void
) => void;

incomingGetPacket: (
client: Client,
packet: AedesPacket,
cb: (error: CallbackError, packet: AedesPacket) => void
) => void;

incomingDelPacket: (
client: Client,
packet: AedesPacket,
cb: (error: CallbackError) => void
) => void;

putWill: (
client: Client,
packet: AedesPacket,
cb: (error: CallbackError, client: Client) => void
) => void;

getWill: (
client: Client,
cb: (error: CallbackError, will: WillPacket, client: Client) => void
) => void;

delWill: (
client: Client,
cb: (error: CallbackError, will: WillPacket, client: Client) => void
) => void;

streamWill: (brokers: Brokers) => Readable;

getClientList: (topic: string) => Readable;

destroy: (cb?: (error: CallbackError) => void) => void;
}

export default function aedesMemoryPersistence(): AedesMemoryPersistence;
168 changes: 168 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import type { Client } from 'aedes';
import type { AedesPacket } from 'aedes-packet';
import type { Readable } from 'stream';
import { expectType } from 'tsd';
import aedesMemoryPersistence, {
AedesMemoryPersistence,
AedesPersistenceSubscription,
Brokers,
CallbackError,
WillPacket,
} from '.';

expectType<AedesMemoryPersistence>(aedesMemoryPersistence());

expectType<void>(
aedesMemoryPersistence().storeRetained(
{
brokerId: '',
brokerCounter: 1,
cmd: 'publish',
qos: 0,
dup: false,
retain: false,
topic: 'test',
payload: 'test',
},
(err: CallbackError) => {}
)
);

expectType<void>(
aedesMemoryPersistence().addSubscriptions(
{} as Client,
[] as AedesPersistenceSubscription[],
(err: CallbackError) => {}
)
);

expectType<void>(
aedesMemoryPersistence().removeSubscriptions(
{} as Client,
[] as AedesPersistenceSubscription[],
(err: CallbackError) => {}
)
);

expectType<void>(
aedesMemoryPersistence().subscriptionsByClient(
{} as Client,
(
error: CallbackError,
subs: AedesPersistenceSubscription[],
client: Client
) => {}
)
);

expectType<void>(
aedesMemoryPersistence().countOffline(
(
error: CallbackError,
subscriptionsCount: number,
clientsCount: number
) => {}
)
);

expectType<void>(
aedesMemoryPersistence().subscriptionsByTopic(
'pattern',
(error: CallbackError, subs: AedesPersistenceSubscription[]) => {}
)
);

expectType<void>(
aedesMemoryPersistence().cleanSubscriptions(
{} as Client,
(error: CallbackError, client: Client) => {}
)
);

expectType<void>(
aedesMemoryPersistence().outgoingEnqueue(
{} as AedesPersistenceSubscription,
{} as AedesPacket,
(error: CallbackError) => {}
)
);

expectType<void>(
aedesMemoryPersistence().outgoingEnqueueCombi(
[] as AedesPersistenceSubscription[],
{} as AedesPacket,
(error: CallbackError) => {}
)
);

expectType<void>(
aedesMemoryPersistence().outgoingUpdate(
{} as Client,
{} as AedesPacket,
(error: CallbackError, client: Client, packet: AedesPacket) => {}
)
);

expectType<void>(
aedesMemoryPersistence().outgoingClearMessageId(
{} as Client,
{} as AedesPacket,
(error: CallbackError, packet?: AedesPacket) => {}
)
);

expectType<Readable>(aedesMemoryPersistence().outgoingStream({} as Client));

expectType<void>(
aedesMemoryPersistence().incomingStorePacket(
{} as Client,
{} as AedesPacket,
(error: CallbackError) => {}
)
);

expectType<void>(
aedesMemoryPersistence().incomingGetPacket(
{} as Client,
{} as AedesPacket,
(error: CallbackError, packet: AedesPacket) => {}
)
);

expectType<void>(
aedesMemoryPersistence().incomingDelPacket(
{} as Client,
{} as AedesPacket,
(error: CallbackError) => {}
)
);

expectType<void>(
aedesMemoryPersistence().putWill(
{} as Client,
{} as AedesPacket,
(error: CallbackError, client: Client) => {}
)
);

expectType<void>(
aedesMemoryPersistence().getWill(
{} as Client,
(error: CallbackError, will: WillPacket, client: Client) => {}
)
);

expectType<void>(
aedesMemoryPersistence().delWill(
{} as Client,
(error: CallbackError, will: WillPacket, client: Client) => {}
)
);

expectType<Readable>(aedesMemoryPersistence().streamWill({} as Brokers));

expectType<Readable>(aedesMemoryPersistence().getClientList('topic'));

expectType<void>(aedesMemoryPersistence().destroy());

expectType<void>(aedesMemoryPersistence().destroy(() => {}));