-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.ts
More file actions
106 lines (91 loc) · 2.67 KB
/
Copy pathmain.ts
File metadata and controls
106 lines (91 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* @boringnode/bus
*
* @license MIT
* @copyright BoringNode
*/
import type { RedisOptions } from 'ioredis'
import type { IClientOptions } from 'mqtt'
export type { Redis, Cluster } from 'ioredis'
export type TransportFactory = () => Transport
/**
* A Duration can be a number in milliseconds or a string formatted as a duration
*
* Formats accepted are :
* - Simple number in milliseconds
* - String formatted as a duration. Uses https://github.com/lukeed/ms under the hood
*/
export type Duration = number | string
export interface ManagerConfig<KnownTransports extends Record<string, TransportConfig>> {
default?: keyof KnownTransports
transports: KnownTransports
}
export interface TransportConfig {
transport: TransportFactory
retryQueue?: RetryQueueOptions
}
export interface RedisTransportConfig extends RedisOptions {
/**
* If true, we will use `messageBuffer` event instead of `message` event
* that is emitted by ioredis. `messageBuffer` will returns a buffer instead
* of a string and this is useful when you are dealing with binary data.
*/
useMessageBuffer?: boolean
}
export interface RedisTransportOptions {
/**
* If true, we will use `messageBuffer` event instead of `message` event
* that is emitted by ioredis. `messageBuffer` will returns a buffer instead
* of a string and this is useful when you are dealing with binary data.
*/
useMessageBuffer?: boolean
}
export enum MqttProtocol {
MQTT = 'mqtt',
MQTTS = 'mqtts',
TCP = 'tcp',
TLS = 'tls',
WS = 'ws',
WSS = 'wss',
WXS = 'wxs',
ALIS = 'alis',
}
export interface MqttTransportConfig {
host: string
port?: number
protocol?: MqttProtocol
options?: IClientOptions
}
export interface Transport {
setId: (id: string) => Transport
onReconnect: (callback: () => void) => void
publish: (channel: string, message: Serializable) => Promise<number>
subscribe: <T extends Serializable>(
channel: string,
handler: SubscribeHandler<T>
) => Promise<void>
unsubscribe: (channel: string) => Promise<void>
disconnect: () => Promise<void>
}
export interface TransportMessage<T extends Serializable = any> {
busId: string
payload: T
}
export interface TransportEncoder {
encode: (message: TransportMessage) => string | Buffer
decode: <T>(data: string | Buffer) => { busId: string; payload: T }
}
export interface RetryQueueOptions {
enabled?: boolean
removeDuplicates?: boolean
maxSize?: number | null
retryInterval?: Duration | false
}
export type SubscribeHandler<T extends Serializable> = (payload: T) => void | Promise<void>
export type Serializable =
| string
| number
| boolean
| null
| Serializable[]
| { [key: string]: Serializable }