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

1 line
13 KiB
Plaintext
Raw Normal View History

2023-10-07 19:42:30 +08:00
{"version":3,"names":["PROTOCOL_VERSION","parseMessage","data","binary","logger","error","undefined","message","JSON","parse","version","e","isBroadcast","method","id","target","isRequest","isResponse","requestId","clientId","result","createMessageSocketEndpoint","wss","WebSocketServer","noServer","clients","Map","nextClientId","getClientWs","clientWs","get","Error","handleSendBroadcast","broadcasterId","forwarded","params","size","warn","otherId","otherWs","send","stringify","toString","on","handleCaughtError","errorMessage","handleServerRequest","forEach","url","upgradeReq","query","forwardRequest","forwardResponse","set","onCloseHandler","onmessage","delete","onclose","onerror","event","server","broadcast"],"sources":["../../src/websocket/createMessageSocketEndpoint.ts"],"sourcesContent":["/**\n * Copyright (c) Facebook, Inc. and its affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n\nimport url from 'url';\nimport {Server as WebSocketServer} from 'ws';\nimport {logger} from '@react-native-community/cli-tools';\n\nconst PROTOCOL_VERSION = 2;\n\ntype IdObject = {\n requestId: string;\n clientId: string;\n};\n\ntype Message = {\n version?: string;\n id?: IdObject;\n method?: string;\n target: string;\n result?: any;\n error?: Error;\n params?: Record<string, any>;\n};\n\nfunction parseMessage(data: string, binary: any) {\n if (binary) {\n logger.error('Expected text message, got binary!');\n return undefined;\n }\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 (e) {\n logger.error(`Failed to parse the message as JSON:\\n${data}`);\n }\n return undefined;\n}\n\nfunction isBroadcast(message: Message) {\n return (\n typeof message.method === 'string' &&\n message.id === undefined &&\n message.target === undefined\n );\n}\n\nfunction isRequest(message: Message) {\n return (\n typeof message.method === 'string' && typeof message.target === 'string'\n );\n}\n\nfunction isResponse(message: Message) {\n return (\n typeof message.id === 'object' &&\n typeof message.id.requestId !== 'undefined' &&\n typeof message.id.clientId === 'string' &&\n (message.result !== undefined || message.error !== undefined)\n );\n}\n\nexport default function createMessageSocketEndpoint(): {\n server: WebSocketServer;\n broadcast: (method: string, params?: Record<string, any>) => void;\n} {\n const wss = new WebSocketServer({\n noServer: true,\n });\n const clients = new Map();\n let nextClientId = 0;\n\n function getClientWs(clientId: string) {\n const clientWs = clients.get(clientId);\n if (clientWs === undefined) {\n throw new Error(\n `could not find id \"${clientId}\" while forwarding request`,\n );\n }\n return clientWs;\n }\n\n function handleSendBroadcast(\n broadcasterId: string | null,\n message: Partial<Message>,\n ) {\n const forwarded = {\n version: PROTOCOL_VERSION,\n method: message.method,\n params: message.params,\n };\n if (clients.size === 0) {\n logger.warn(\n `No apps connected. Sending \"${message.method}\" to all React Native apps failed. Make sure your app is running in the simulator or on a phone connected via USB.`,\n );\n }\n for (const [otherId, otherWs] of clients) {\n if (otherId !== broadcasterId) {\n try {\n otherWs.send(JSON.stringify(forwarded));\n } catch (e) {\n logger.error(\n `Failed to send broadcast to client: '${otherId}' ` +\n `due to:\\n ${(e as any).toString()}`,\n );\n }\n }\n }\n }\n\n wss.on('connection', (clientWs) => {\n const clientId = `client#${nextClientId++}`;\n\n function handleCaughtError(message: Message, error: Error) {\n const errorMessage = {\n id: message.i