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

Check protocol version #250

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/io/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type DecoderState = { state: number; header: Header | undefined; buf: Buffer };
const WAITING_FOR_HEADER = 0;
const WAITING_FOR_BODY = 1;

export const SUPPORTED_PROTOCOL_VERSION = 0;

function initalDecoderState(buf: Buffer): DecoderState {
return {
state: WAITING_FOR_HEADER,
Expand All @@ -57,8 +59,20 @@ function decodeMessages(decoderState: DecoderState, out: Output): DecoderState {
}
const h = buf.readBigUInt64BE();
buf = buf.subarray(8);
decoderState.header = Header.fromU64be(h);
const materializedHeader = Header.fromU64be(h);
decoderState.header = materializedHeader;
decoderState.state = WAITING_FOR_BODY;

// Check protocol version
if (
materializedHeader.protocolVersion !== undefined &&
materializedHeader.protocolVersion !== SUPPORTED_PROTOCOL_VERSION
) {
throw new Error(
`Unsupported protocol version ${materializedHeader.protocolVersion}, only version ${SUPPORTED_PROTOCOL_VERSION} is supported`
);
}

break;
}
case WAITING_FOR_BODY: {
Expand Down
29 changes: 28 additions & 1 deletion test/message_coders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
*/

import { describe, expect } from "@jest/globals";
import { streamDecoder } from "../src/io/decoder";
import {
decodeMessagesBuffer,
streamDecoder,
SUPPORTED_PROTOCOL_VERSION,
} from "../src/io/decoder";
import { Message } from "../src/types/types";
import { backgroundInvokeMessage } from "./protoutils";
import { encodeMessage } from "../src/io/encoder";
import { START_MESSAGE_TYPE, StartMessage } from "../src/types/protocol";

describe("The stream decoder", () => {
it("should handle decoding of messages across chunks", () => {
Expand Down Expand Up @@ -44,4 +49,26 @@ describe("The stream decoder", () => {
expect(result[0]).toStrictEqual(largeMessage);
expect(callbackCounter).toStrictEqual(2);
});

it("should fail when unsupported protocol version", () => {
const startMessage = encodeMessage(
new Message(
START_MESSAGE_TYPE,
StartMessage.create({
id: Buffer.from(
"f311f1fdcb9863f0018bd3400ecd7d69b547204e776218b2",
"hex"
),
debugId: "8xHx_cuYY_AAYvTQA7NfWm1RyBOd2IYsg",
}),
undefined,
SUPPORTED_PROTOCOL_VERSION + 1,
undefined
)
);

expect(() => decodeMessagesBuffer(Buffer.from(startMessage))).toThrow(
"Unsupported protocol version"
);
});
});
Loading