amis-rpc-design/node_modules/@react-native-community/cli-server-api/build/websocket/createEventsSocketEndpoint.js.map

1 line
9.8 KiB
Plaintext
Raw Normal View History

2023-10-07 19:42:30 +08:00
{"version":3,"names":["PROTOCOL_VERSION","parseMessage","data","message","JSON","parse","version","logger","error","undefined","serializeMessage","toSerialize","Error","prettyFormat","escapeString","highlight","maxDepth","min","type","map","item","plugins","ReactElement","stringify","e","createEventsSocketEndpoint","broadcast","wss","WebSocketServer","noServer","verifyClient","origin","startsWith","clients","Map","nextClientId","broadCastEvent","size","serialized","ws","values","send","toString","on","clientWs","clientId","set","onclose","onerror","delete","onmessage","event","command","params","server","reportEvent"],"sources":["../../src/websocket/createEventsSocketEndpoint.ts"],"sourcesContent":["import {Server as WebSocketServer} from 'ws';\nimport {logger} from '@react-native-community/cli-tools';\nimport prettyFormat from 'pretty-format';\n\n/**\n * The eventsSocket websocket listens at the 'events/` for websocket\n * connections, on which all Metro reports will be emitted.\n *\n * This is mostly useful for developer tools (clients) that wants to monitor Metro,\n * and the apps connected to Metro.\n *\n * The eventsSocket provides the following features:\n * - it reports any Metro event (that is reported through a reporter) to all clients\n * - it reports any console.log's (and friends) from the connected app to all clients\n * (as client_log event)\n * - it allows connected clients to send commands through Metro to the connected app.\n * This reuses the generic command mechanism.\n * Two useful commands are 'reload' and 'devmenu'.\n */\n\ntype Command = {\n version: number;\n type: 'command';\n command: string;\n params?: any;\n};\n\n/**\n * This number is used to version the communication protocol between\n * Dev tooling like Flipper and Metro, so that in the future we can recognize\n * messages coming from old clients, so that it will be simpler to implement\n * backward compatibility.\n *\n * We start at 2 as the protocol is currently the same as used internally at FB,\n * which happens to be at version 2 as well.\n */\nconst PROTOCOL_VERSION = 2;\n\nfunction parseMessage<T extends Object>(data: string): T | undefined {\n try {\n const message = JSON.parse(data);\n if (message.version === PROTOCOL_VERSION) {\n return message;\n }\n logger.error(\n 'Received message had wrong protocol version: ' + message.version,\n );\n } catch {\n logger.error('Failed to parse the message as JSON:\\n' + data);\n }\n return undefined;\n}\n\n/**\n * Two types of messages will arrive in this function,\n * 1) messages generated by Metro itself (through the reporter abstraction)\n * those are yet to be serialized, and can contain any kind of data structure\n * 2) a specific event generated by Metro is `client_log`, which describes\n * console.* calls in the app.\n * The arguments send to the console are pretty printed so that they can be\n * displayed in a nicer way in dev tools\n *\n * @param message\n */\nfunction serializeMessage(message: any) {\n // We do want to send Metro report messages, but their contents is not guaranteed to be serializable.\n // For some known types we will pretty print otherwise not serializable parts first:\n let toSerialize = message;\n if (message && message.error && message.error instanceof Error) {\n toSerialize = {\n ...message,\n error: prettyFormat(message.error, {\n escapeString: true,\n highlight: true,\n maxDepth: 3,\n min: true,\n }),\n };\n } else if (message && message.type === 'client_log') {\n toSerialize = {\n ...message,\n data: message.data.map((item: any) =>\n typeof item === 'string'\n ? item\n : prettyFormat(item, {\n escapeString: true,\n highlight: true,\n maxDepth: 3,\n min: true,\n plugins: [prettyFormat.plugins.ReactElement],\n }),\n ),\n };\n }\n try {\n return JSON.stringify(toSerialize);\n } catch (e) {\n logger.error('Failed to serialize: