Skip to content

Commit 62ba0f4

Browse files
author
Brian Vaughn
committed
DevTools: Add Bridge protocol version backend/frontend
Frontend shows upgrade or downgrade instructions if the version does not match.
1 parent 0f5ebf3 commit 62ba0f4

File tree

11 files changed

+311
-3
lines changed

11 files changed

+311
-3
lines changed

packages/react-devtools-core/src/standalone.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ function initialize(socket: WebSocket) {
217217
socket.close();
218218
});
219219

220-
store = new Store(bridge, {supportsNativeInspection: false});
220+
store = new Store(bridge, {
221+
checkBridgeProtocolCompatibility: true,
222+
supportsNativeInspection: false,
223+
});
221224

222225
log('Connected');
223226
reload();

packages/react-devtools-inline/src/frontend.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
2121
import type {Props} from 'react-devtools-shared/src/devtools/views/DevTools';
2222

2323
export function createStore(bridge: FrontendBridge): Store {
24-
return new Store(bridge, {supportsTraceUpdates: true});
24+
return new Store(bridge, {
25+
checkBridgeProtocolCompatibility: true,
26+
supportsTraceUpdates: true,
27+
});
2528
}
2629

2730
export function createBridge(

packages/react-devtools-shared/src/backend/agent.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
toggleEnabled as setTraceUpdatesEnabled,
2727
} from './views/TraceUpdates';
2828
import {patch as patchConsole, unpatch as unpatchConsole} from './console';
29+
import {currentBridgeProtocol} from 'react-devtools-shared/src/bridge';
2930

3031
import type {BackendBridge} from 'react-devtools-shared/src/bridge';
3132
import type {
@@ -176,6 +177,7 @@ export default class Agent extends EventEmitter<{|
176177
bridge.addListener('clearWarningsForFiberID', this.clearWarningsForFiberID);
177178
bridge.addListener('copyElementPath', this.copyElementPath);
178179
bridge.addListener('deletePath', this.deletePath);
180+
bridge.addListener('getBridgeProtocol', this.getBridgeProtocol);
179181
bridge.addListener('getProfilingData', this.getProfilingData);
180182
bridge.addListener('getProfilingStatus', this.getProfilingStatus);
181183
bridge.addListener('getOwnersList', this.getOwnersList);
@@ -308,6 +310,10 @@ export default class Agent extends EventEmitter<{|
308310
return null;
309311
}
310312

313+
getBridgeProtocol = () => {
314+
this._bridge.send('bridgeProtocol', currentBridgeProtocol);
315+
};
316+
311317
getProfilingData = ({rendererID}: {|rendererID: RendererID|}) => {
312318
const renderer = this._rendererInterfaces[rendererID];
313319
if (renderer == null) {

packages/react-devtools-shared/src/bridge.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,41 @@ import type {StyleAndLayout as StyleAndLayoutPayload} from 'react-devtools-share
2020

2121
const BATCH_DURATION = 100;
2222

23+
// This message specifies the version of the DevTools protocol currently supported by the backend,
24+
// as well as the earliest NPM version (e.g. "4.13.0") that protocol is supported by on the frontend.
25+
// This enables an older frontend to display an upgrade message to users for a newer, unsupported backend.
26+
export type BridgeProtocol = {|
27+
// Version supported by the current frontend/backend.
28+
version: number,
29+
30+
// NPM version range that also supports this version.
31+
// Note that 'maxNpmVersion' is only set when the version is bumped.
32+
minNpmVersion: string,
33+
maxNpmVersion: string | null,
34+
|};
35+
36+
// Bump protocol version whenever a backwards breaking change is made
37+
// in the messages sent between BackendBridge and FrontendBridge.
38+
// This mapping is embedded in both frontend and backend builds.
39+
//
40+
// The backend protocol will always be the latest entry in the BRIDGE_PROTOCOL array.
41+
//
42+
// When an older frontend connects to a newer backend,
43+
// the backend can send the minNpmVersion and the frontend can display an NPM upgrade prompt.
44+
//
45+
// When a newer frontend connects with an older protocol version,
46+
// the frontend can use the embedded minNpmVersion/maxNpmVersion values to display a downgrade prompt.
47+
export const BRIDGE_PROTOCOL: Array<BridgeProtocol> = [
48+
{
49+
version: 1,
50+
minNpmVersion: '4.11.0',
51+
maxNpmVersion: null,
52+
},
53+
];
54+
55+
export const currentBridgeProtocol: BridgeProtocol =
56+
BRIDGE_PROTOCOL[BRIDGE_PROTOCOL.length - 1];
57+
2358
type ElementAndRendererID = {|id: number, rendererID: RendererID|};
2459

2560
type Message = {|
@@ -119,6 +154,7 @@ type UpdateConsolePatchSettingsParams = {|
119154
|};
120155

121156
export type BackendEvents = {|
157+
bridgeProtocol: [BridgeProtocol],
122158
extensionBackendInitialized: [],
123159
inspectedElement: [InspectedElementPayload],
124160
isBackendStorageAPISupported: [boolean],
@@ -150,6 +186,7 @@ type FrontendEvents = {|
150186
clearWarningsForFiberID: [ElementAndRendererID],
151187
copyElementPath: [CopyElementPathParams],
152188
deletePath: [DeletePath],
189+
getBridgeProtocol: [],
153190
getOwnersList: [ElementAndRendererID],
154191
getProfilingData: [{|rendererID: RendererID|}],
155192
getProfilingStatus: [],

packages/react-devtools-shared/src/devtools/store.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ import {localStorageGetItem, localStorageSetItem} from '../storage';
2929
import {__DEBUG__} from '../constants';
3030
import {printStore} from './utils';
3131
import ProfilerStore from './ProfilerStore';
32+
import {currentBridgeProtocol} from 'react-devtools-shared/src/bridge';
3233

3334
import type {Element} from './views/Components/types';
3435
import type {ComponentFilter, ElementType} from '../types';
35-
import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
36+
import type {
37+
FrontendBridge,
38+
BridgeProtocol,
39+
} from 'react-devtools-shared/src/bridge';
3640

3741
const debug = (methodName, ...args) => {
3842
if (__DEBUG__) {
@@ -51,6 +55,7 @@ const LOCAL_STORAGE_RECORD_CHANGE_DESCRIPTIONS_KEY =
5155
'React::DevTools::recordChangeDescriptions';
5256

5357
type Config = {|
58+
checkBridgeProtocolCompatibility?: boolean,
5459
isProfiling?: boolean,
5560
supportsNativeInspection?: boolean,
5661
supportsReloadAndProfile?: boolean,
@@ -76,6 +81,8 @@ export default class Store extends EventEmitter<{|
7681
supportsNativeStyleEditor: [],
7782
supportsProfiling: [],
7883
supportsReloadAndProfile: [],
84+
unsupportedBridgeProtocolDetected: [],
85+
unsupportedRendererVersionDetected: [],
7986
unsupportedRendererVersionDetected: [],
8087
|}> {
8188
_bridge: FrontendBridge;
@@ -119,6 +126,10 @@ export default class Store extends EventEmitter<{|
119126

120127
_nativeStyleEditorValidAttributes: $ReadOnlyArray<string> | null = null;
121128

129+
// Older backends don't support an explicit bridge protocol,
130+
// so we should timeout eventually and show a downgrade message.
131+
_onBridgeProtocolTimeoutID: TimeoutID | null = null;
132+
122133
// Map of element (id) to the set of elements (ids) it owns.
123134
// This map enables getOwnersListForElement() to avoid traversing the entire tree.
124135
_ownersMap: Map<number, Set<number>> = new Map();
@@ -147,6 +158,7 @@ export default class Store extends EventEmitter<{|
147158
_supportsReloadAndProfile: boolean = false;
148159
_supportsTraceUpdates: boolean = false;
149160

161+
_unsupportedBridgeProtocol: BridgeProtocol | null = null;
150162
_unsupportedRendererVersionDetected: boolean = false;
151163

152164
// Total number of visible elements (within all roots).
@@ -217,6 +229,20 @@ export default class Store extends EventEmitter<{|
217229
);
218230

219231
this._profilerStore = new ProfilerStore(bridge, this, isProfiling);
232+
233+
// Verify that the frontend version is compatible with the connected backend.
234+
// See github.com/facebook/react/issues/21326
235+
if (config != null && config.checkBridgeProtocolCompatibility) {
236+
// Older backends don't support an explicit bridge protocol,
237+
// so we should timeout eventually and show a downgrade message.
238+
this._onBridgeProtocolTimeoutID = setTimeout(
239+
this.onBridgeProtocolTimeout,
240+
10000,
241+
);
242+
243+
bridge.addListener('bridgeProtocol', this.onBridgeProtocol);
244+
bridge.send('getBridgeProtocol');
245+
}
220246
}
221247

222248
// This is only used in tests to avoid memory leaks.
@@ -385,6 +411,10 @@ export default class Store extends EventEmitter<{|
385411
return this._supportsTraceUpdates;
386412
}
387413

414+
get unsupportedBridgeProtocol(): BridgeProtocol | null {
415+
return this._unsupportedBridgeProtocol;
416+
}
417+
388418
get unsupportedRendererVersionDetected(): boolean {
389419
return this._unsupportedRendererVersionDetected;
390420
}
@@ -1168,6 +1198,12 @@ export default class Store extends EventEmitter<{|
11681198
'unsupportedRendererVersion',
11691199
this.onBridgeUnsupportedRendererVersion,
11701200
);
1201+
bridge.removeListener('bridgeProtocol', this.onBridgeProtocol);
1202+
1203+
if (this._onBridgeProtocolTimeoutID !== null) {
1204+
clearTimeout(this._onBridgeProtocolTimeoutID);
1205+
this._onBridgeProtocolTimeoutID = null;
1206+
}
11711207
};
11721208

11731209
onBackendStorageAPISupported = (isBackendStorageAPISupported: boolean) => {
@@ -1187,4 +1223,34 @@ export default class Store extends EventEmitter<{|
11871223

11881224
this.emit('unsupportedRendererVersionDetected');
11891225
};
1226+
1227+
onBridgeProtocol = (bridgeProtocol: BridgeProtocol) => {
1228+
if (this._onBridgeProtocolTimeoutID !== null) {
1229+
clearTimeout(this._onBridgeProtocolTimeoutID);
1230+
this._onBridgeProtocolTimeoutID = null;
1231+
}
1232+
1233+
this._onBridgeProtocolTimeoutID = null;
1234+
1235+
if (bridgeProtocol.version !== currentBridgeProtocol.version) {
1236+
this._unsupportedBridgeProtocol = bridgeProtocol;
1237+
1238+
this.emit('unsupportedBridgeProtocolDetected');
1239+
}
1240+
};
1241+
1242+
onBridgeProtocolTimeout = () => {
1243+
this._bridge.removeListener('bridgeProtocol', this.onBridgeProtocol);
1244+
1245+
// If we timed out, that indicates the backend predates the bridge protocol,
1246+
// so we can set a fake version (0) to trigger the downgrade message.
1247+
// We pin to the latest 4.10 release because 4.11 contained a breaking protocol change.
1248+
this._unsupportedBridgeProtocol = {
1249+
version: 0,
1250+
minNpmVersion: '<4.11.0',
1251+
maxNpmVersion: '<4.11.0',
1252+
};
1253+
1254+
this.emit('unsupportedBridgeProtocolDetected');
1255+
};
11901256
}

packages/react-devtools-shared/src/devtools/views/DevTools.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import ViewElementSourceContext from './Components/ViewElementSourceContext';
2525
import {ProfilerContextController} from './Profiler/ProfilerContext';
2626
import {ModalDialogContextController} from './ModalDialog';
2727
import ReactLogo from './ReactLogo';
28+
import UnsupportedBridgeProtocolDialog from './UnsupportedBridgeProtocolDialog';
2829
import UnsupportedVersionDialog from './UnsupportedVersionDialog';
2930
import WarnIfLegacyBackendDetected from './WarnIfLegacyBackendDetected';
3031
import {useLocalStorage} from './hooks';
@@ -226,6 +227,7 @@ export default function DevTools({
226227
</TreeContextController>
227228
</ViewElementSourceContext.Provider>
228229
</SettingsContextController>
230+
<UnsupportedBridgeProtocolDialog />
229231
{warnIfLegacyBackendDetected && <WarnIfLegacyBackendDetected />}
230232
{warnIfUnsupportedVersionDetected && <UnsupportedVersionDialog />}
231233
</ModalDialogContextController>

packages/react-devtools-shared/src/devtools/views/Settings/SettingsContext.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,13 @@ export function updateThemeVariables(
383383
updateStyleHelper(theme, 'color-expand-collapse-toggle', documentElements);
384384
updateStyleHelper(theme, 'color-link', documentElements);
385385
updateStyleHelper(theme, 'color-modal-background', documentElements);
386+
updateStyleHelper(
387+
theme,
388+
'color-bridge-version-npm-background',
389+
documentElements,
390+
);
391+
updateStyleHelper(theme, 'color-bridge-version-npm-text', documentElements);
392+
updateStyleHelper(theme, 'color-bridge-version-number', documentElements);
386393
updateStyleHelper(
387394
theme,
388395
'color-primitive-hook-badge-background',
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
.Column {
2+
display: flex;
3+
flex-direction: column;
4+
}
5+
6+
.Title {
7+
font-size: var(--font-size-sans-large);
8+
margin-bottom: 0.5rem;
9+
}
10+
11+
.ReleaseNotesLink {
12+
color: var(--color-button-active);
13+
}
14+
15+
.Version {
16+
color: var(--color-bridge-version-number);
17+
font-weight: bold;
18+
}
19+
20+
.NpmCommand {
21+
display: flex;
22+
justify-content: space-between;
23+
padding: 0.25rem 0.25rem 0.25rem 0.5rem;
24+
background-color: var(--color-bridge-version-npm-background);
25+
color: var(--color-bridge-version-npm-text);
26+
margin: 0;
27+
font-family: var(--font-family-monospace);
28+
font-size: var(--font-size-monospace-large);
29+
}
30+
31+
.Paragraph {
32+
margin: 0.5rem 0;
33+
}
34+
35+
.Link {
36+
color: var(--color-link);
37+
}

0 commit comments

Comments
 (0)