This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
-
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.
Add support for binary decoder in addition to json decoder
- Loading branch information
Showing
11 changed files
with
257 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* The Binary serializer for encoding and decoding messages */ | ||
|
||
|
||
import {ChannelMessage} from "./channel-message"; | ||
import {MessageDecoder} from "./serializer"; | ||
|
||
export class BinaryDecoder implements MessageDecoder { | ||
|
||
private textDecoder : TextDecoder; | ||
|
||
constructor() { | ||
this.textDecoder = new TextDecoder(); | ||
} | ||
|
||
public decode(messageEvent: MessageEvent): ChannelMessage { | ||
const buffer: ArrayBuffer = messageEvent.data; | ||
const view = new DataView(buffer) | ||
|
||
const control = view.getUint8(0); | ||
if (control != 255){ | ||
throw new Error('Invalid binary data; no control byte match') | ||
} | ||
|
||
const extractor: Extractor = new Extractor(this.textDecoder, buffer, 4); | ||
const message_id = extractor.decodeChunk(view.getUint8(1)); | ||
const correlation_id = extractor.decodeChunk(view.getUint8(2)); | ||
const event = extractor.decodeChunk(view.getUint8(3)); | ||
const payload = extractor.decodeChunk(null); | ||
|
||
return new ChannelMessage(message_id, event, correlation_id, payload); | ||
} | ||
} | ||
|
||
class Extractor { | ||
constructor(private readonly textDecoder : TextDecoder, | ||
private readonly buffer: ArrayBuffer, | ||
private offset: number) { | ||
} | ||
|
||
public decodeChunk(size: number) : string { | ||
const endIndex = size ? this.offset + size : this.buffer.byteLength + 1; | ||
const data = this.textDecoder.decode(this.buffer.slice(this.offset, endIndex)); | ||
this.offset = endIndex; | ||
return data; | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* The Json serializer for encoding and decoding messages */ | ||
|
||
|
||
import {ChannelMessage} from "./channel-message"; | ||
import {MessageDecoder} from "./serializer"; | ||
|
||
export class JsonDecoder implements MessageDecoder { | ||
|
||
public decode(messageEvent: MessageEvent): ChannelMessage { | ||
const [message_id, correlation_id, event, payload] = JSON.parse(messageEvent.data); | ||
return new ChannelMessage(message_id, event, correlation_id, payload); | ||
} | ||
} | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as chai from 'chai'; | ||
|
||
import {ChannelMessage} from "../src/channel-message"; | ||
import {MessageDecoder} from "../src/serializer"; | ||
import {MessageEvent} from "./support/event" | ||
import {BinaryDecoder} from "../src/binary-decoder"; | ||
import "fast-text-encoding" | ||
|
||
const assert = chai.assert; | ||
describe('Binary Serializer Tests', function() { | ||
|
||
const serializer: MessageDecoder = new BinaryDecoder(); | ||
|
||
it('Should decode basic binary payload' , () => { | ||
|
||
const data: ArrayBuffer = binaryData(); | ||
const event = new MessageEvent('test', { | ||
data : data | ||
}); | ||
|
||
const message = serializer.decode(event); | ||
|
||
assert.deepEqual(message, new ChannelMessage("message_id2", "event_name2", "correlation_id2", "message_data1")); | ||
}); | ||
|
||
it('Should decode UTF-8 with special characters binary payload' , () => { | ||
const plainPayload = "{\"strange_message: \"áéíóú@ñ&%$#!especíalç\", \"strange_message: \"áéíóú@ñ&%$#!especíal2ç\"}"; | ||
const data: ArrayBuffer = specialBinaryData(); | ||
const event = new MessageEvent('test', { | ||
data : data | ||
}); | ||
|
||
const message = serializer.decode(event); | ||
|
||
assert.deepEqual(message, new ChannelMessage("message_id2", "event_name2", "correlation_id2", plainPayload)); | ||
}); | ||
|
||
}); | ||
|
||
/* | ||
message_id: "message_id2", | ||
correlation_id: "correlation_id2", | ||
message_data: "message_data1", | ||
event_name: "event_name2" | ||
*/ | ||
function binaryData() : ArrayBuffer { | ||
const rawData = [255, 11, 15, 11, 109, 101, 115, 115, 97, 103, 101, 95, 105, 100, 50, 99, 111, | ||
114, 114, 101, 108, 97, 116, 105, 111, 110, 95, 105, 100, 50, 101, 118, 101, | ||
110, 116, 95, 110, 97, 109, 101, 50, 109, 101, 115, 115, 97, 103, 101, 95, | ||
100, 97, 116, 97, 49]; | ||
const byteArray = new Uint8Array(rawData); | ||
return byteArray.buffer; | ||
} | ||
|
||
function specialBinaryData() : ArrayBuffer { | ||
const rawData = [255, 11, 15, 11, 109, 101, 115, 115, 97, 103, 101, 95, 105, 100, 50, 99, 111, | ||
114, 114, 101, 108, 97, 116, 105, 111, 110, 95, 105, 100, 50, 101, 118, 101, | ||
110, 116, 95, 110, 97, 109, 101, 50, 123, 34, 115, 116, 114, 97, 110, 103, | ||
101, 95, 109, 101, 115, 115, 97, 103, 101, 58, 32, 34, 195, 161, 195, 169, | ||
195, 173, 195, 179, 195, 186, 64, 195, 177, 38, 37, 36, 35, 33, 101, 115, 112, | ||
101, 99, 195, 173, 97, 108, 195, 167, 34, 44, 32, 34, 115, 116, 114, 97, 110, | ||
103, 101, 95, 109, 101, 115, 115, 97, 103, 101, 58, 32, 34, 195, 161, 195, | ||
169, 195, 173, 195, 179, 195, 186, 64, 195, 177, 38, 37, 36, 35, 33, 101, 115, | ||
112, 101, 99, 195, 173, 97, 108, 50, 195, 167, 34, 125]; | ||
const byteArray = new Uint8Array(rawData); | ||
return byteArray.buffer; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as chai from 'chai'; | ||
|
||
import {JsonDecoder} from "../src/json-decoder"; | ||
import {ChannelMessage} from "../src/channel-message"; | ||
import {MessageDecoder} from "../src/serializer"; | ||
import {MessageEvent} from "./support/event" | ||
|
||
const assert = chai.assert; | ||
describe('Json Serializer Tests', function() { | ||
|
||
const serializer: MessageDecoder = new JsonDecoder() | ||
|
||
it('Should decode basic string payload' , () => { | ||
let payload = "[\"ids332msg1\", \"\", \"person.registered\", \"someData\"]"; | ||
// @ts-ignore | ||
const event = new MessageEvent('test', { | ||
data : payload | ||
}); | ||
const message = serializer.decode(event); | ||
assert.deepEqual(message, new ChannelMessage("ids332msg1", "person.registered", "", "someData")); | ||
}); | ||
|
||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.