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

feat: Allow null payloads in messages #456

Merged
merged 2 commits into from
Mar 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions PROTOCOL.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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