fix(gateway): validate event/response frames
This commit is contained in:
@@ -8,7 +8,9 @@ import {
|
|||||||
type HelloOk,
|
type HelloOk,
|
||||||
PROTOCOL_VERSION,
|
PROTOCOL_VERSION,
|
||||||
type RequestFrame,
|
type RequestFrame,
|
||||||
|
validateEventFrame,
|
||||||
validateRequestFrame,
|
validateRequestFrame,
|
||||||
|
validateResponseFrame,
|
||||||
} from "./protocol/index.js";
|
} from "./protocol/index.js";
|
||||||
|
|
||||||
type Pending = {
|
type Pending = {
|
||||||
@@ -125,7 +127,7 @@ export class GatewayClient {
|
|||||||
private handleMessage(raw: string) {
|
private handleMessage(raw: string) {
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(raw);
|
const parsed = JSON.parse(raw);
|
||||||
if (parsed?.type === "event") {
|
if (validateEventFrame(parsed)) {
|
||||||
const evt = parsed as EventFrame;
|
const evt = parsed as EventFrame;
|
||||||
const seq = typeof evt.seq === "number" ? evt.seq : null;
|
const seq = typeof evt.seq === "number" ? evt.seq : null;
|
||||||
if (seq !== null) {
|
if (seq !== null) {
|
||||||
@@ -140,7 +142,7 @@ export class GatewayClient {
|
|||||||
this.opts.onEvent?.(evt);
|
this.opts.onEvent?.(evt);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (parsed?.type === "res") {
|
if (validateResponseFrame(parsed)) {
|
||||||
const pending = this.pending.get(parsed.id);
|
const pending = this.pending.get(parsed.id);
|
||||||
if (!pending) return;
|
if (!pending) return;
|
||||||
// If the payload is an ack with status accepted, keep waiting for final.
|
// If the payload is an ack with status accepted, keep waiting for final.
|
||||||
|
|||||||
@@ -121,6 +121,9 @@ export const validateConnectParams =
|
|||||||
ajv.compile<ConnectParams>(ConnectParamsSchema);
|
ajv.compile<ConnectParams>(ConnectParamsSchema);
|
||||||
export const validateRequestFrame =
|
export const validateRequestFrame =
|
||||||
ajv.compile<RequestFrame>(RequestFrameSchema);
|
ajv.compile<RequestFrame>(RequestFrameSchema);
|
||||||
|
export const validateResponseFrame =
|
||||||
|
ajv.compile<ResponseFrame>(ResponseFrameSchema);
|
||||||
|
export const validateEventFrame = ajv.compile<EventFrame>(EventFrameSchema);
|
||||||
export const validateSendParams = ajv.compile(SendParamsSchema);
|
export const validateSendParams = ajv.compile(SendParamsSchema);
|
||||||
export const validateAgentParams = ajv.compile(AgentParamsSchema);
|
export const validateAgentParams = ajv.compile(AgentParamsSchema);
|
||||||
export const validateWakeParams = ajv.compile<WakeParams>(WakeParamsSchema);
|
export const validateWakeParams = ajv.compile<WakeParams>(WakeParamsSchema);
|
||||||
|
|||||||
Reference in New Issue
Block a user