Skip to content

Commit

Permalink
feat: Allow null payloads in messages (enisdenjo#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Mar 6, 2023
1 parent a464491 commit eeb0265
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
8 changes: 4 additions & 4 deletions PROTOCOL.md
Expand Up @@ -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<string, unknown>;
payload?: Record<string, unknown> | null;
}
```

Expand All @@ -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<string, unknown>;
payload?: Record<string, unknown> | null;
}
```

Expand All @@ -73,7 +73,7 @@ The optional `payload` field can be used to transfer additional details about th
```typescript
interface PingMessage {
type: 'ping';
payload?: Record<string, unknown>;
payload?: Record<string, unknown> | null;
}
```

Expand All @@ -90,7 +90,7 @@ The optional `payload` field can be used to transfer additional details about th
```typescript
interface PongMessage {
type: 'pong';
payload?: Record<string, unknown>;
payload?: Record<string, unknown> | null;
}
```

Expand Down
12 changes: 4 additions & 8 deletions src/__tests__/__snapshots__/common.ts.snap
Expand Up @@ -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"`;

Expand All @@ -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"`;

Expand Down
24 changes: 16 additions & 8 deletions src/__tests__/common.ts
Expand Up @@ -36,10 +36,6 @@ it.each([
type: MessageType.ConnectionInit,
payload: 0,
},
{
type: MessageType.ConnectionInit,
payload: undefined,
},
{
type: MessageType.ConnectionAck,
payload: '',
Expand All @@ -48,10 +44,6 @@ it.each([
type: MessageType.Ping,
payload: 0,
},
{
type: MessageType.Pong,
payload: undefined,
},

// invalid subscribe message
{
Expand Down Expand Up @@ -219,27 +211,43 @@ it.each([
type: MessageType.ConnectionInit,
payload: {},
},
{
type: MessageType.ConnectionInit,
payload: null,
},
{
type: MessageType.ConnectionAck,
},
{
type: MessageType.ConnectionAck,
payload: {},
},
{
type: MessageType.ConnectionAck,
payload: null,
},
{
type: MessageType.Ping,
},
{
type: MessageType.Ping,
payload: {},
},
{
type: MessageType.Ping,
payload: null,
},
{
type: MessageType.Pong,
},
{
type: MessageType.Pong,
payload: {},
},
{
type: MessageType.Pong,
payload: null,
},

// valid subscribe message
{
Expand Down
4 changes: 2 additions & 2 deletions src/common.ts
Expand Up @@ -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}"`,
);
}

Expand Down

0 comments on commit eeb0265

Please sign in to comment.