Skip to content

Commit d5cdb11

Browse files
NordskogBjornskjald
authored andcommitted
Added Payload class to encode/decode Thrift CompactProtocol payloads
1 parent b804175 commit d5cdb11

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/mqtt/payloads/Payload.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Thrift, TCompactProtocol, TBufferedTransport } from 'thrift'
2+
3+
export default abstract class Payload {
4+
/**
5+
* Write this payload into the specified stream
6+
*/
7+
encode (proto: TCompactProtocol): Promise<void> {
8+
return null
9+
}
10+
11+
/**
12+
* Decodes thrift buffer into this payload
13+
*/
14+
decode (data: Buffer): Promise<void> {
15+
return null
16+
}
17+
18+
/**
19+
* Get topic this payload should published to
20+
* Only single-level topics have been observed,
21+
* but mqtt works with multiple, possibly dynamically
22+
* generated topics.
23+
*/
24+
getTopic (): string {
25+
// Maybe thrown?
26+
return null
27+
}
28+
29+
/**
30+
* Static convenience method for writing a payload to a
31+
* buffered transport and returning it as a buffer
32+
*/
33+
static encodePayload (payload: Payload): Promise<Buffer> {
34+
return new Promise<Buffer>((resolve, reject) => {
35+
const trans = new TBufferedTransport(null, (msg, seqid) => {
36+
resolve(msg)
37+
})
38+
39+
const proto = new TCompactProtocol(trans)
40+
payload.encode(proto)
41+
42+
proto.flush()
43+
})
44+
}
45+
}

src/mqtt/payloads/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Payload from './Payload'
2+
3+
export { Payload }
4+
5+
export const encodePayload = Payload.encodePayload

0 commit comments

Comments
 (0)