-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22940 from storybookjs/norbert/server-channel-for…
…-addons Core: Integrate serverChannel into channel, make serverChannel bi-directional and extendable
- Loading branch information
Showing
31 changed files
with
192 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,14 @@ | ||
export async function generateAddonSetupCode() { | ||
return ` | ||
import { createChannel as createPostMessageChannel } from '@storybook/channel-postmessage'; | ||
import { createChannel as createWebSocketChannel } from '@storybook/channel-websocket'; | ||
import { addons } from '@storybook/preview-api'; | ||
const channel = createPostMessageChannel({ page: 'preview' }); | ||
addons.setChannel(channel); | ||
window.__STORYBOOK_ADDONS_CHANNEL__ = channel; | ||
if (window.CONFIG_TYPE === 'DEVELOPMENT'){ | ||
const serverChannel = createWebSocketChannel({}); | ||
addons.setServerChannel(serverChannel); | ||
window.__STORYBOOK_SERVER_CHANNEL__ = serverChannel; | ||
window.__STORYBOOK_SERVER_CHANNEL__ = channel; | ||
} | ||
`.trim(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
declare var CHANNEL_OPTIONS: any; | ||
declare var CONFIG_TYPE: 'DEVELOPMENT' | 'PRODUCTION'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
declare module 'json-fn'; | ||
declare var CONFIG_TYPE: 'DEVELOPMENT' | 'PRODUCTION'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,56 @@ | ||
import WebSocket, { WebSocketServer } from 'ws'; | ||
import { stringify } from 'telejson'; | ||
import { isJSON, parse, stringify } from 'telejson'; | ||
import type { ChannelHandler } from '@storybook/channels'; | ||
import { Channel } from '@storybook/channels'; | ||
|
||
type Server = ConstructorParameters<typeof WebSocketServer>[0]['server']; | ||
|
||
export class ServerChannel { | ||
webSocketServer: WebSocketServer; | ||
/** | ||
* This class represents a channel transport that allows for a one-to-many relationship between the server and clients. | ||
* Unlike other channels such as the postmessage and websocket channel implementations, this channel will receive from many clients and any events emitted will be sent out to all connected clients. | ||
*/ | ||
export class ServerChannelTransport { | ||
private socket: WebSocketServer; | ||
|
||
private handler?: ChannelHandler; | ||
|
||
constructor(server: Server) { | ||
this.webSocketServer = new WebSocketServer({ noServer: true }); | ||
this.socket = new WebSocketServer({ noServer: true }); | ||
|
||
server.on('upgrade', (request, socket, head) => { | ||
if (request.url === '/storybook-server-channel') { | ||
this.webSocketServer.handleUpgrade(request, socket, head, (ws) => { | ||
this.webSocketServer.emit('connection', ws, request); | ||
this.socket.handleUpgrade(request, socket, head, (ws) => { | ||
this.socket.emit('connection', ws, request); | ||
}); | ||
} | ||
}); | ||
this.socket.on('connection', (wss) => { | ||
wss.on('message', (raw) => { | ||
const data = raw.toString(); | ||
const event = typeof data === 'string' && isJSON(data) ? parse(data) : data; | ||
this.handler(event); | ||
}); | ||
}); | ||
} | ||
|
||
setHandler(handler: ChannelHandler) { | ||
this.handler = handler; | ||
} | ||
|
||
emit(type: string, args: any = []) { | ||
const event = { type, args }; | ||
send(event: any) { | ||
const data = stringify(event, { maxDepth: 15, allowFunction: true }); | ||
Array.from(this.webSocketServer.clients) | ||
|
||
Array.from(this.socket.clients) | ||
.filter((c) => c.readyState === WebSocket.OPEN) | ||
.forEach((client) => client.send(data)); | ||
} | ||
} | ||
|
||
export function getServerChannel(server: Server) { | ||
return new ServerChannel(server); | ||
const transports = [new ServerChannelTransport(server)]; | ||
|
||
return new Channel({ transports, async: true }); | ||
} | ||
|
||
// for backwards compatibility | ||
export type ServerChannel = ReturnType<typeof getServerChannel>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.