forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmiddleware.js
More file actions
95 lines (87 loc) · 2.99 KB
/
middleware.js
File metadata and controls
95 lines (87 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/
import typeof * as CLIServerAPI from '@react-native-community/cli-server-api';
import type {Server} from 'connect';
import type {TerminalReportableEvent} from 'metro/src/lib/TerminalReporter';
const debug = require('debug')('ReactNative:CommunityCliPlugin');
type MiddlewareReturn = {
middleware: Server,
websocketEndpoints: {
[path: string]: ws$WebSocketServer,
},
messageSocketEndpoint: {
server: ws$WebSocketServer,
broadcast: (method: string, params?: Record<string, mixed> | null) => void,
},
eventsSocketEndpoint: {
server: ws$WebSocketServer,
reportEvent: (event: TerminalReportableEvent) => void,
},
...
};
// $FlowFixMe
const unusedStubWSServer: ws$WebSocketServer = {};
// $FlowFixMe
const unusedMiddlewareStub: Server = {};
const communityMiddlewareFallback = {
createDevServerMiddleware: (params: {
host?: string,
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: {
server: unusedStubWSServer,
broadcast: (
method: string,
_params?: Record<string, mixed> | null,
): void => {},
},
eventsSocketEndpoint: {
server: unusedStubWSServer,
reportEvent: (event: TerminalReportableEvent) => {},
},
}),
};
// Attempt to use the community middleware if it exists, but fallback to
// the stubs if it doesn't.
try {
// `@react-native-community/cli` is an optional peer dependency of this
// package, and should be a dev dependency of the host project (via the
// community template's package.json).
const communityCliPath = require.resolve('@react-native-community/cli');
// `@react-native-community/cli-server-api` is a dependency of
// `@react-native-community/cli`, but is not re-exported by it, so we need
// to resolve the former through the latter.
const communityCliServerApiPath = require.resolve(
'@react-native-community/cli-server-api',
{paths: [communityCliPath]},
);
// $FlowIgnore[unsupported-syntax] dynamic import
const communityCliServerApi: CLIServerAPI = require(
communityCliServerApiPath,
);
// $FlowIgnore[unsupported-syntax] dynamic import
communityMiddlewareFallback.createDevServerMiddleware =
communityCliServerApi.createDevServerMiddleware;
} catch {
debug(`⚠️ Unable to find @react-native-community/cli-server-api
Starting the server without the community middleware.`);
}
export const createDevServerMiddleware =
communityMiddlewareFallback.createDevServerMiddleware;