MCPcopy
hub / github.com/facebook/react / startServer

Function startServer

packages/react-devtools-core/src/standalone.js:309–402  ·  view source on GitHub ↗
(
  port: number = 8097,
  host: string = 'localhost',
  httpsOptions?: ServerOptions,
  loggerOptions?: LoggerOptions,
)

Source from the content-addressed store, hash-verified

307};
308
309function startServer(
310 port: number = 8097,
311 host: string = 'localhost',
312 httpsOptions?: ServerOptions,
313 loggerOptions?: LoggerOptions,
314): {close(): void} {
315 registerDevToolsEventLogger(loggerOptions?.surface ?? 'standalone');
316
317 const useHttps = !!httpsOptions;
318 const httpServer = useHttps
319 ? require('https').createServer(httpsOptions)
320 : require('http').createServer();
321 const server = new Server({server: httpServer, maxPayload: 1e9});
322 let connected: WebSocket | null = null;
323 server.on('connection', (socket: WebSocket) => {
324 if (connected !== null) {
325 connected.close();
326 log.warn(
327 'Only one connection allowed at a time.',
328 'Closing the previous connection',
329 );
330 }
331 connected = socket;
332 socket.onerror = error => {
333 connected = null;
334 onDisconnected();
335 log.error('Error with websocket connection', error);
336 };
337 socket.onclose = () => {
338 connected = null;
339 onDisconnected();
340 log('Connection to RN closed');
341 };
342 initialize(socket);
343 });
344
345 server.on('error', (event: $FlowFixMe) => {
346 onError(event);
347 log.error('Failed to start the DevTools server', event);
348 startServerTimeoutID = setTimeout(() => startServer(port), 1000);
349 });
350
351 httpServer.on('request', (request: $FlowFixMe, response: $FlowFixMe) => {
352 // Serve a file that immediately sets up the connection.
353 const backendFile = readFileSync(join(__dirname, 'backend.js'));
354
355 // The renderer interface doesn't read saved component filters directly,
356 // because they are generally stored in localStorage within the context of the extension.
357 // Because of this it relies on the extension to pass filters, so include them wth the response here.
358 // This will ensure that saved filters are shared across different web pages.
359 const savedPreferencesString = `
360 window.__REACT_DEVTOOLS_COMPONENT_FILTERS__ = ${JSON.stringify(
361 getSavedComponentFilters(),
362 )};`;
363
364 response.end(
365 savedPreferencesString +
366 '\n;' +

Callers

nothing calls this directly

Calls 11

getSavedComponentFiltersFunction · 0.90
onDisconnectedFunction · 0.85
statusListenerFunction · 0.85
onMethod · 0.80
logFunction · 0.70
initializeFunction · 0.70
onErrorFunction · 0.70
closeMethod · 0.65
errorMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected