Skip to content

Commit 7e100ac

Browse files
franticFacebook Github Bot 2
authored andcommitted
Extract getDevServerURL into reusable module
Summary: Many RN devtools (inspector, source maps, etc.) rely on packager. This refactors individual SourceCode.scriptURL parsing into one function that will be easier to change in the future. Reviewed By: javache Differential Revision: D3348465 fbshipit-source-id: 5a55939ea59e1517cb63bcbe4963f57f02ab15f3
1 parent bbc6139 commit 7e100ac

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

Libraries/Inspector/ElementProperties.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ var Text = require('Text');
2020
var TouchableHighlight = require('TouchableHighlight');
2121
var TouchableWithoutFeedback = require('TouchableWithoutFeedback');
2222
var View = require('View');
23-
var {SourceCode} = require('NativeModules');
2423
var {fetch} = require('fetch');
2524

2625
var flattenStyle = require('flattenStyle');
2726
var mapWithSeparator = require('mapWithSeparator');
27+
var getDevServer = require('getDevServer');
2828

2929
var ElementProperties = React.createClass({
3030
propTypes: {
@@ -97,10 +97,7 @@ var ElementProperties = React.createClass({
9797
},
9898

9999
_openFile: function(fileName: string, lineNumber: number) {
100-
var match = SourceCode.scriptURL && SourceCode.scriptURL.match(/^https?:\/\/.*?\//);
101-
var baseURL = match ? match[0] : 'http://localhost:8081/';
102-
103-
fetch(baseURL + 'open-stack-frame', {
100+
fetch(getDevServer().url + 'open-stack-frame', {
104101
method: 'POST',
105102
body: JSON.stringify({file: fileName, lineNumber}),
106103
});

Libraries/JavaScriptAppEngine/Initialization/ExceptionsManager.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ function reportException(e: Error, isFatal: bool) {
3535
}
3636

3737
function symbolicateAndUpdateStack(id, message, stack) {
38+
const getDevServer = require('getDevServer');
3839
const {fetch} = require('fetch');
39-
const {SourceCode, ExceptionsManager} = require('NativeModules');
40-
const match = SourceCode.scriptURL && SourceCode.scriptURL.match(/^https?:\/\/.*?\//);
41-
const endpoint = (match && match[0] : 'http://localhost:8081/') + 'symbolicate';
40+
const {ExceptionsManager} = require('NativeModules');
41+
const devServer = getDevServer();
42+
if (!devServer.bundleLoadedFromServer) {
43+
return;
44+
}
4245

43-
fetch(endpoint, { method: 'POST', body: JSON.stringify({stack}) })
46+
fetch(devServer.url + 'symbolicate', { method: 'POST', body: JSON.stringify({stack}) })
4447
.then(response => response.json())
4548
.then(response =>
4649
ExceptionsManager.updateExceptionMessage(message, response.stack, id))
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule getDevServer
10+
* @flow
11+
*/
12+
'use strict';
13+
14+
const {SourceCode} = require('NativeModules');
15+
16+
let _cachedDevServerURL: ?string;
17+
const FALLBACK = 'http://localhost:8081/';
18+
19+
type DevServerInfo = {
20+
url: string;
21+
bundleLoadedFromServer: boolean;
22+
};
23+
24+
/**
25+
* Many RN development tools rely on the development server (packager) running
26+
* @return URL to packager with trailing slash
27+
*/
28+
function getDevServer(): DevServerInfo {
29+
if (_cachedDevServerURL === undefined) {
30+
const match = SourceCode.scriptURL && SourceCode.scriptURL.match(/^https?:\/\/.*?\//);
31+
_cachedDevServerURL = match ? match[0] : null;
32+
}
33+
34+
return {
35+
url: _cachedDevServerURL || FALLBACK,
36+
bundleLoadedFromServer: _cachedDevServerURL !== null,
37+
};
38+
}
39+
40+
module.exports = getDevServer;

0 commit comments

Comments
 (0)