Skip to content

Commit

Permalink
Merge pull request #23615 from storybookjs/norbert/fix-server-channel…
Browse files Browse the repository at this point in the history
…-options

Core: Fix channelOptions for serverChannel
  • Loading branch information
ndelangen authored Jul 27, 2023
2 parents a1c9ea6 + ff13ab9 commit 46a96b3
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
86 changes: 86 additions & 0 deletions code/lib/core-server/src/utils/__tests__/server-channel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import type { Server } from 'http';
import { Channel } from '@storybook/channels';

import { EventEmitter } from 'events';
import { stringify } from 'telejson';
import { getServerChannel, ServerChannelTransport } from '../get-server-channel';

describe('getServerChannel', () => {
test('should return a channel', () => {
const server = { on: jest.fn() } as any as Server;
const result = getServerChannel(server);
expect(result).toBeInstanceOf(Channel);
});

test('should attach to the http server', () => {
const server = { on: jest.fn() } as any as Server;
getServerChannel(server);
expect(server.on).toHaveBeenCalledWith('upgrade', expect.any(Function));
});
});

describe('ServerChannelTransport', () => {
test('parses simple JSON', () => {
const server = new EventEmitter() as any as Server;
const socket = new EventEmitter();
const transport = new ServerChannelTransport(server);
const handler = jest.fn();
transport.setHandler(handler);

// @ts-expect-error (an internal API)
transport.socket.emit('connection', socket);
socket.emit('message', '"hello"');

expect(handler).toHaveBeenCalledWith('hello');
});
test('parses object JSON', () => {
const server = new EventEmitter() as any as Server;
const socket = new EventEmitter();
const transport = new ServerChannelTransport(server);
const handler = jest.fn();
transport.setHandler(handler);

// @ts-expect-error (an internal API)
transport.socket.emit('connection', socket);
socket.emit('message', JSON.stringify({ type: 'hello' }));

expect(handler).toHaveBeenCalledWith({ type: 'hello' });
});
test('supports telejson cyclical data', () => {
const server = new EventEmitter() as any as Server;
const socket = new EventEmitter();
const transport = new ServerChannelTransport(server);
const handler = jest.fn();
transport.setHandler(handler);

// @ts-expect-error (an internal API)
transport.socket.emit('connection', socket);

const input: any = { a: 1 };
input.b = input;
socket.emit('message', stringify(input));

expect(handler.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"a": 1,
"b": [Circular],
}
`);
});
test('skips telejson classes and functions in data', () => {
const server = new EventEmitter() as any as Server;
const socket = new EventEmitter();
const transport = new ServerChannelTransport(server);
const handler = jest.fn();
transport.setHandler(handler);

// @ts-expect-error (an internal API)
transport.socket.emit('connection', socket);

const input = { a() {}, b: class {} };
socket.emit('message', stringify(input));

expect(handler.mock.calls[0][0].a).toEqual(expect.any(String));
expect(handler.mock.calls[0][0].b).toEqual(expect.any(String));
});
});
7 changes: 5 additions & 2 deletions code/lib/core-server/src/utils/get-server-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export class ServerChannelTransport {
this.socket.on('connection', (wss) => {
wss.on('message', (raw) => {
const data = raw.toString();
const event = typeof data === 'string' && isJSON(data) ? parse(data) : data;
const event =
typeof data === 'string' && isJSON(data)
? parse(data, { allowFunction: false, allowClass: false })
: data;
this.handler?.(event);
});
});
Expand All @@ -38,7 +41,7 @@ export class ServerChannelTransport {
}

send(event: any) {
const data = stringify(event, { maxDepth: 15, allowFunction: true });
const data = stringify(event, { maxDepth: 15, allowFunction: false, allowClass: false });

Array.from(this.socket.clients)
.filter((c) => c.readyState === WebSocket.OPEN)
Expand Down

0 comments on commit 46a96b3

Please sign in to comment.