Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions code/core/src/channels/parse-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="../typings.d.ts" />
import { global } from '@storybook/global';

import { isJSON, parse } from 'telejson';

/**
* Deserialize a raw channel message using telejson. Returns the parsed object when the message is
* a JSON string, otherwise returns it as-is. Uses `global.CHANNEL_OPTIONS` for parse options so
* all transports apply the same settings.
*/
export function parseEvent(data: unknown): any {
return typeof data === 'string' && isJSON(data)
? parse(data, global.CHANNEL_OPTIONS || {})
: data;
}
5 changes: 3 additions & 2 deletions code/core/src/channels/postmessage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as EVENTS from 'storybook/internal/core-events';

import { global } from '@storybook/global';

import { isJSON, parse, stringify } from 'telejson';
import { stringify } from 'telejson';
import invariant from 'tiny-invariant';

import type {
Expand All @@ -14,6 +14,7 @@ import type {
ChannelTransport,
Config,
} from '../types';
import { parseEvent } from '../parse-event';
import { getEventSourceUrl } from './getEventSourceUrl';

const { document, location } = global;
Expand Down Expand Up @@ -195,7 +196,7 @@ export class PostMessageTransport implements ChannelTransport {
try {
const { data } = rawEvent;
const { key, event, refId } =
typeof data === 'string' && isJSON(data) ? parse(data, global.CHANNEL_OPTIONS || {}) : data;
parseEvent(data);

if (key === KEY) {
const pageString =
Expand Down
5 changes: 3 additions & 2 deletions code/core/src/channels/websocket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import * as EVENTS from 'storybook/internal/core-events';

import { global } from '@storybook/global';

import { isJSON, parse, stringify } from 'telejson';
import { stringify } from 'telejson';
import invariant from 'tiny-invariant';

import type { ChannelHandler, ChannelTransport, Config } from '../types';
import { parseEvent } from '../parse-event';

const { WebSocket } = global;

Expand Down Expand Up @@ -49,7 +50,7 @@ export class WebsocketTransport implements ChannelTransport {
this.flush();
};
this.socket.onmessage = ({ data }) => {
const event = typeof data === 'string' && isJSON(data) ? parse(data) : data;
const event = parseEvent(data);
invariant(this.handler, 'WebsocketTransport handler should be set');
this.handler(event);
if (event.type === 'ping') {
Expand Down
5 changes: 3 additions & 2 deletions code/core/src/core-server/utils/get-server-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import type { IncomingMessage } from 'node:http';
import type { ChannelHandler } from 'storybook/internal/channels';
import { Channel, HEARTBEAT_INTERVAL } from 'storybook/internal/channels';

import { isJSON, parse, stringify } from 'telejson';
import { stringify } from 'telejson';
import WebSocket, { WebSocketServer } from 'ws';

import { parseEvent } from '../../channels/parse-event';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relative references to sub package exports like storybook/internal/channels are not allowed

import { logger } from '../../node-logger';
import { UniversalStore } from '../../shared/universal-store';
import { type HostValidationOptions, isValidHost } from './getHostValidationMiddleware';
Expand Down Expand Up @@ -64,7 +65,7 @@ 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 = parseEvent(data);
this.handler?.(event);
});
});
Expand Down
Loading