Skip to content

Commit

Permalink
Update cli-server-api middleware imports (#48997)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #48997

Follows react-native-community/cli#2584.

- Also add FIXME comment flagging potential core APIs gap without CLI.

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D68766565

fbshipit-source-id: 60747715f76c4323e306c39ab0613fb4818b4914
  • Loading branch information
huntie authored and facebook-github-bot committed Jan 28, 2025
1 parent 037504c commit bb1e3cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
17 changes: 7 additions & 10 deletions packages/community-cli-plugin/src/commands/start/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @oncall react_native
*/

import type {NextHandleFunction, Server} from 'connect';
import type {Server} from 'connect';
import type {TerminalReportableEvent} from 'metro/src/lib/TerminalReporter';

const debug = require('debug')('ReactNative:CommunityCliPlugin');
Expand All @@ -30,10 +30,6 @@ type MiddlewareReturn = {
...
};

const noopNextHandle: NextHandleFunction = (req, res, next) => {
next();
};

// $FlowFixMe
const unusedStubWSServer: ws$WebSocketServer = {};
// $FlowFixMe
Expand All @@ -45,6 +41,12 @@ const communityMiddlewareFallback = {
port: number,
watchFolders: $ReadOnlyArray<string>,
}): MiddlewareReturn => ({
// FIXME: Several features will break without community middleware and
// should be migrated into core.
// e.g. used by Libraries/Core/Devtools:
// - /open-stack-frame
// - /open-url
// - /symbolicate
middleware: unusedMiddlewareStub,
websocketEndpoints: {},
messageSocketEndpoint: {
Expand All @@ -59,15 +61,12 @@ const communityMiddlewareFallback = {
reportEvent: (event: TerminalReportableEvent) => {},
},
}),
indexPageMiddleware: noopNextHandle,
};

// Attempt to use the community middleware if it exists, but fallback to
// the stubs if it doesn't.
try {
const community = require('@react-native-community/cli-server-api');
communityMiddlewareFallback.indexPageMiddleware =
community.indexPageMiddleware;
communityMiddlewareFallback.createDevServerMiddleware =
community.createDevServerMiddleware;
} catch {
Expand All @@ -77,5 +76,3 @@ Starting the server without the community middleware.`);

export const createDevServerMiddleware =
communityMiddlewareFallback.createDevServerMiddleware;
export const indexPageMiddleware =
communityMiddlewareFallback.indexPageMiddleware;
8 changes: 2 additions & 6 deletions packages/community-cli-plugin/src/commands/start/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import isDevServerRunning from '../../utils/isDevServerRunning';
import loadMetroConfig from '../../utils/loadMetroConfig';
import * as version from '../../utils/version';
import attachKeyHandlers from './attachKeyHandlers';
import {createDevServerMiddleware, indexPageMiddleware} from './middleware';
import {createDevServerMiddleware} from './middleware';
import {createDevMiddleware} from '@react-native/dev-middleware';
import chalk from 'chalk';
import Metro from 'metro';
Expand Down Expand Up @@ -146,11 +146,7 @@ async function runServer(
secure: args.https,
secureCert: args.cert,
secureKey: args.key,
unstable_extraMiddleware: [
communityMiddleware,
indexPageMiddleware,
middleware,
],
unstable_extraMiddleware: [communityMiddleware, middleware],
websocketEndpoints: {
...communityWebsocketEndpoints,
...websocketEndpoints,
Expand Down

0 comments on commit bb1e3cd

Please sign in to comment.