generated from streamich/tpl-ts-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
141 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,59 @@ | ||
import {MqttDecoder} from '../MqttDecoder'; | ||
import { connect } from './util'; | ||
import {connect, connectAck, subscribe, subscribeAck} from './util'; | ||
import {PACKET_TYPE} from '../enums'; | ||
|
||
it('can instantiate', () => { | ||
const decoder = new MqttDecoder(() => {}); | ||
const decoder = new MqttDecoder(); | ||
}); | ||
|
||
it('can parse header', () => { | ||
const decoder = new MqttDecoder(() => {}); | ||
it('can parse first byte packet type of CONNECT packet', () => { | ||
const decoder = new MqttDecoder(); | ||
decoder.push(connect); | ||
expect(decoder.packet.t).toBe(0); | ||
expect(decoder.packet.f).toBe(0); | ||
decoder.parseHeader(); | ||
expect(decoder.packet.t).toBe(1); | ||
expect(decoder.packet.f).toBe(0); | ||
expect(decoder.packet.b).toBe(0); | ||
expect(decoder.packet.l).toBe(0); | ||
decoder.parseFirstByte(); | ||
expect(decoder.packet.b).not.toBe(0); | ||
expect(decoder.packet.l).toBe(0); | ||
expect(decoder.packet.type()).toBe(PACKET_TYPE.CONNECT); | ||
}); | ||
|
||
it('can parse first byte packet type of CONNACK packet', () => { | ||
const decoder = new MqttDecoder(); | ||
decoder.push(connectAck); | ||
expect(decoder.packet.b).toBe(0); | ||
expect(decoder.packet.l).toBe(0); | ||
decoder.parseFirstByte(); | ||
expect(decoder.packet.b).not.toBe(0); | ||
expect(decoder.packet.l).toBe(0); | ||
expect(decoder.packet.type()).toBe(PACKET_TYPE.CONNACK); | ||
}); | ||
|
||
it('can parse CONNECT packet fixed header', () => { | ||
const decoder = new MqttDecoder(); | ||
decoder.push(connect); | ||
const packet = decoder.parse(); | ||
expect(packet!.type()).toBe(PACKET_TYPE.CONNECT); | ||
expect(packet!.dup()).toBe(false); | ||
expect(packet!.qos()).toBe(0); | ||
expect(packet!.retain()).toBe(false); | ||
}); | ||
|
||
it('can parse CONNACK packet fixed header', () => { | ||
const decoder = new MqttDecoder(); | ||
decoder.push(connectAck); | ||
const packet = decoder.parse(); | ||
expect(packet!.type()).toBe(PACKET_TYPE.CONNACK); | ||
expect(packet!.dup()).toBe(false); | ||
expect(packet!.qos()).toBe(0); | ||
expect(packet!.retain()).toBe(false); | ||
}); | ||
|
||
it('can parse SUBACK packet fixed header', () => { | ||
const decoder = new MqttDecoder(); | ||
decoder.push(subscribeAck); | ||
const packet = decoder.parse(); | ||
expect(packet!.type()).toBe(PACKET_TYPE.SUBACK); | ||
expect(packet!.dup()).toBe(false); | ||
expect(packet!.qos()).toBe(0); | ||
expect(packet!.retain()).toBe(false); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
import {PACKET_TYPE} from './enums'; | ||
|
||
export interface IPacket { | ||
/** Packet type. */ | ||
t: PACKET_TYPE; | ||
/** Fixed header flags. */ | ||
f: number; | ||
export interface MqttPacketHeaderData { | ||
/** Packet first byte. */ | ||
b: number; | ||
/** Variable length. */ | ||
l: number; | ||
key: string | null; | ||
dat: Buffer | null; | ||
} | ||
|
||
export class MqttPacket implements IPacket { | ||
public t: PACKET_TYPE = PACKET_TYPE.RESERVED; | ||
public f: number = 0; | ||
export class MqttPacket implements MqttPacketHeaderData { | ||
public b: number = 0; | ||
public l: number = 0; | ||
public key: string | null = null; | ||
public dat: Buffer | null = null; | ||
|
||
public type (): PACKET_TYPE { | ||
return this.b >> 4; | ||
} | ||
|
||
public dup (): boolean { | ||
return !!(this.b & 0b1000); | ||
} | ||
|
||
public qos (): 0 | 1 | 2 { | ||
return ((this.b >> 1) & 0b11) as 0 | 1 | 2; | ||
} | ||
|
||
public retain (): boolean { | ||
return !!(this.b & 0b1); | ||
} | ||
} |