Skip to content

Commit

Permalink
fix(server): handleProtocols accepts arrays too and gracefully reje…
Browse files Browse the repository at this point in the history
…cts other types

Closes #318
  • Loading branch information
enisdenjo committed Feb 23, 2022
1 parent 96058eb commit 98dec1a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/modules/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ to handle the connection afterwards.

| Name | Type |
| :------ | :------ |
| `protocols` | `string` \| `Set`<`string`\> |
| `protocols` | `string` \| `Set`<`string`\> \| `string`[] |

#### Returns

Expand Down
66 changes: 65 additions & 1 deletion src/__tests__/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ExecutionResult,
GraphQLSchema,
} from 'graphql';
import { makeServer } from '../server';
import { handleProtocols, makeServer } from '../server';
import {
GRAPHQL_TRANSPORT_WS_PROTOCOL,
CloseCode,
Expand Down Expand Up @@ -1705,3 +1705,67 @@ describe('Disconnect/close', () => {
client.ws.close(4321, 'Byebye');
});
});

it('should only accept a Set, Array or string in handleProtocols', () => {
for (const test of [
{
in: new Set(['not', 'me']),
out: false,
},
{
in: new Set(['maybe', 'me', GRAPHQL_TRANSPORT_WS_PROTOCOL + 'nah']),
out: false,
},
{
in: new Set(['almost', 'next', GRAPHQL_TRANSPORT_WS_PROTOCOL, 'one']),
out: GRAPHQL_TRANSPORT_WS_PROTOCOL,
},
{
in: [''],
out: false,
},
{
in: ['123', GRAPHQL_TRANSPORT_WS_PROTOCOL],
out: GRAPHQL_TRANSPORT_WS_PROTOCOL,
},
{
in: [GRAPHQL_TRANSPORT_WS_PROTOCOL, GRAPHQL_TRANSPORT_WS_PROTOCOL],
out: GRAPHQL_TRANSPORT_WS_PROTOCOL,
},
{
in: `some, ${GRAPHQL_TRANSPORT_WS_PROTOCOL} , other-one,third`,
out: GRAPHQL_TRANSPORT_WS_PROTOCOL,
},
{
in: `no, graphql-TransPort-ws`,
out: false,
},
{
in: { iAm: 'unacceptable' },
out: false,
},
{
in: 123,
out: false,
},
{
in: null,
out: false,
},
{
in: undefined,
out: false,
},
{
in: () => {
// void
},
out: false,
},
]) {
expect(
// @ts-expect-error for test purposes, in can be different from type
handleProtocols(test.in),
).toBe(test.out);
}
});
26 changes: 15 additions & 11 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,16 +862,20 @@ export function makeServer<
* to handle the connection afterwards.
*/
export function handleProtocols(
protocols: Set<string> | string,
protocols: Set<string> | string[] | string,
): typeof GRAPHQL_TRANSPORT_WS_PROTOCOL | false {
return (
typeof protocols === 'string'
? protocols
.split(',')
.map((p) => p.trim())
.includes(GRAPHQL_TRANSPORT_WS_PROTOCOL)
: protocols.has(GRAPHQL_TRANSPORT_WS_PROTOCOL)
)
? GRAPHQL_TRANSPORT_WS_PROTOCOL
: false;
switch (true) {
case protocols instanceof Set &&
protocols.has(GRAPHQL_TRANSPORT_WS_PROTOCOL):
case Array.isArray(protocols) &&
protocols.includes(GRAPHQL_TRANSPORT_WS_PROTOCOL):
case typeof protocols === 'string' &&
protocols
.split(',')
.map((p) => p.trim())
.includes(GRAPHQL_TRANSPORT_WS_PROTOCOL):
return GRAPHQL_TRANSPORT_WS_PROTOCOL;
default:
return false;
}
}

0 comments on commit 98dec1a

Please sign in to comment.