diff --git a/PROTOCOL.md b/PROTOCOL.md index c0196761..2b012fbb 100644 --- a/PROTOCOL.md +++ b/PROTOCOL.md @@ -37,7 +37,7 @@ If the server receives more than one `ConnectionInit` message at any given time, ```typescript interface ConnectionInitMessage { type: 'connection_init'; - payload?: Record; + payload?: Record | null; } ``` @@ -52,7 +52,7 @@ The server can use the optional `payload` field to transfer additional details a ```typescript interface ConnectionAckMessage { type: 'connection_ack'; - payload?: Record; + payload?: Record | null; } ``` @@ -73,7 +73,7 @@ The optional `payload` field can be used to transfer additional details about th ```typescript interface PingMessage { type: 'ping'; - payload?: Record; + payload?: Record | null; } ``` @@ -90,7 +90,7 @@ The optional `payload` field can be used to transfer additional details about th ```typescript interface PongMessage { type: 'pong'; - payload?: Record; + payload?: Record | null; } ``` diff --git a/src/__tests__/__snapshots__/common.ts.snap b/src/__tests__/__snapshots__/common.ts.snap index 65834e7c..5047ae7a 100644 --- a/src/__tests__/__snapshots__/common.ts.snap +++ b/src/__tests__/__snapshots__/common.ts.snap @@ -56,13 +56,11 @@ exports[`should report invalid message {"type":""} with descriptive error 1`] = exports[`should report invalid message {"type":"complete"} with descriptive error 1`] = `""complete" message expects the 'id' property to be a string, but got undefined"`; -exports[`should report invalid message {"type":"connection_ack","payload":""} with descriptive error 1`] = `""connection_ack" message expects the 'payload' property to be an object or missing, but got """`; +exports[`should report invalid message {"type":"connection_ack","payload":""} with descriptive error 1`] = `""connection_ack" message expects the 'payload' property to be an object or nullish or missing, but got """`; -exports[`should report invalid message {"type":"connection_init","payload":""} with descriptive error 1`] = `""connection_init" message expects the 'payload' property to be an object or missing, but got """`; +exports[`should report invalid message {"type":"connection_init","payload":""} with descriptive error 1`] = `""connection_init" message expects the 'payload' property to be an object or nullish or missing, but got """`; -exports[`should report invalid message {"type":"connection_init","payload":0} with descriptive error 1`] = `""connection_init" message expects the 'payload' property to be an object or missing, but got "0""`; - -exports[`should report invalid message {"type":"connection_init"} with descriptive error 1`] = `""connection_init" message expects the 'payload' property to be an object or missing, but got "undefined""`; +exports[`should report invalid message {"type":"connection_init","payload":0} with descriptive error 1`] = `""connection_init" message expects the 'payload' property to be an object or nullish or missing, but got "0""`; exports[`should report invalid message {"type":"error"} with descriptive error 1`] = `""error" message expects the 'id' property to be a string, but got undefined"`; @@ -72,9 +70,7 @@ exports[`should report invalid message {"type":"next"} with descriptive error 2` exports[`should report invalid message {"type":"nuxt"} with descriptive error 1`] = `"Invalid message 'type' property "nuxt""`; -exports[`should report invalid message {"type":"ping","payload":0} with descriptive error 1`] = `""ping" message expects the 'payload' property to be an object or missing, but got "0""`; - -exports[`should report invalid message {"type":"pong"} with descriptive error 1`] = `""pong" message expects the 'payload' property to be an object or missing, but got "undefined""`; +exports[`should report invalid message {"type":"ping","payload":0} with descriptive error 1`] = `""ping" message expects the 'payload' property to be an object or nullish or missing, but got "0""`; exports[`should report invalid message {"type":"subscribe"} with descriptive error 1`] = `""subscribe" message expects the 'id' property to be a string, but got undefined"`; diff --git a/src/__tests__/common.ts b/src/__tests__/common.ts index 222ef54f..6da157fc 100644 --- a/src/__tests__/common.ts +++ b/src/__tests__/common.ts @@ -36,10 +36,6 @@ it.each([ type: MessageType.ConnectionInit, payload: 0, }, - { - type: MessageType.ConnectionInit, - payload: undefined, - }, { type: MessageType.ConnectionAck, payload: '', @@ -48,10 +44,6 @@ it.each([ type: MessageType.Ping, payload: 0, }, - { - type: MessageType.Pong, - payload: undefined, - }, // invalid subscribe message { @@ -219,6 +211,10 @@ it.each([ type: MessageType.ConnectionInit, payload: {}, }, + { + type: MessageType.ConnectionInit, + payload: null, + }, { type: MessageType.ConnectionAck, }, @@ -226,6 +222,10 @@ it.each([ type: MessageType.ConnectionAck, payload: {}, }, + { + type: MessageType.ConnectionAck, + payload: null, + }, { type: MessageType.Ping, }, @@ -233,6 +233,10 @@ it.each([ type: MessageType.Ping, payload: {}, }, + { + type: MessageType.Ping, + payload: null, + }, { type: MessageType.Pong, }, @@ -240,6 +244,10 @@ it.each([ type: MessageType.Pong, payload: {}, }, + { + type: MessageType.Pong, + payload: null, + }, // valid subscribe message { diff --git a/src/common.ts b/src/common.ts index 7a2076a0..8987f6aa 100644 --- a/src/common.ts +++ b/src/common.ts @@ -228,9 +228,9 @@ export function validateMessage(val: unknown): Message { case MessageType.ConnectionAck: case MessageType.Ping: case MessageType.Pong: { - if ('payload' in val && !isObject(val.payload)) { + if (val.payload != null && !isObject(val.payload)) { throw new Error( - `"${val.type}" message expects the 'payload' property to be an object or missing, but got "${val.payload}"`, + `"${val.type}" message expects the 'payload' property to be an object or nullish or missing, but got "${val.payload}"`, ); }