Skip to content

Commit

Permalink
Add https options to RN CLI server
Browse files Browse the repository at this point in the history
Reviewed By: davidaurelio

Differential Revision: D5223852

fbshipit-source-id: 91025751ddf1f4ec4adcc768b69b936ee1a68edf
  • Loading branch information
andrewimm authored and facebook-github-bot committed Jun 12, 2017
1 parent 50b11aa commit 2c32acb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
43 changes: 27 additions & 16 deletions local-cli/server/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ const defaultSourceExts = require('metro-bundler/build/defaults').sourceExts;
const defaultPlatforms = require('metro-bundler/build/defaults').platforms;
const defaultProvidesModuleNodeModules = require('metro-bundler/build/defaults')
.providesModuleNodeModules;
const fs = require('fs');
const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware');
const http = require('http');
const https = require('https');
const indexPageMiddleware = require('./middleware/indexPage');
const loadRawBodyMiddleware = require('./middleware/loadRawBodyMiddleware');
const messageSocket = require('./util/messageSocket.js');
Expand Down Expand Up @@ -89,23 +91,32 @@ function runServer(

app.use(connect.logger()).use(connect.errorHandler());

const serverInstance = http
.createServer(app)
.listen(args.port, args.host, 511, function() {
attachHMRServer({
httpServer: serverInstance,
path: '/hot',
packagerServer,
});

wsProxy = webSocketProxy.attachToServer(
serverInstance,
'/debugger-proxy',
);
ms = messageSocket.attachToServer(serverInstance, '/message');
inspectorProxy.attachToServer(serverInstance, '/inspector');
readyCallback(packagerServer._reporter);
if (args.https && (!args.key || !args.cert)) {
throw new Error('Cannot use https without specifying key and cert options');
}

const serverInstance = args.https
? https.createServer(
{
key: fs.readFileSync(args.key),
cert: fs.readFileSync(args.cert),
},
app,
)
: http.createServer(app);

serverInstance.listen(args.port, args.host, 511, function() {
attachHMRServer({
httpServer: serverInstance,
path: '/hot',
packagerServer,
});

wsProxy = webSocketProxy.attachToServer(serverInstance, '/debugger-proxy');
ms = messageSocket.attachToServer(serverInstance, '/message');
inspectorProxy.attachToServer(serverInstance, '/inspector');
readyCallback(packagerServer._reporter);
});
// Disable any kind of automatic timeout behavior for incoming
// requests in case it takes the packager more than the default
// timeout of 120 seconds to respond to a request.
Expand Down
9 changes: 9 additions & 0 deletions local-cli/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,14 @@ module.exports = {
}, {
command: '--verbose',
description: 'Enables logging',
}, {
command: '--https',
description: 'Enables https connections to the server',
}, {
command: '--key [path]',
description: 'Path to custom SSL key',
}, {
command: '--cert [path]',
description: 'Path to custom SSL cert',
}],
};
3 changes: 2 additions & 1 deletion local-cli/server/util/attachHMRServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const url = require('url');

import type {ResolutionResponse} from './getInverseDependencies';
import type {Server as HTTPServer} from 'http';
import type {Server as HTTPSServer} from 'https';

const blacklist = [
'Libraries/Utilities/HMRClient.js',
Expand Down Expand Up @@ -57,7 +58,7 @@ type PackagerServer<TModule> = {
};

type HMROptions<TModule> = {
httpServer: HTTPServer,
httpServer: HTTPServer | HTTPSServer,
packagerServer: PackagerServer<TModule>,
path: string,
};
Expand Down
14 changes: 9 additions & 5 deletions local-cli/server/util/inspectorProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const WebSocket = require('ws');
const debug = require('debug')('RNP:InspectorProxy');
const launchChrome = require('./launchChrome');

import type {Server as HTTPSServer} from 'https';

type DevicePage = {
id: string,
title: string,
Expand Down Expand Up @@ -94,6 +96,8 @@ type Address = {
port: number,
};

type Server = http.Server | HTTPSServer;

const DEVICE_TIMEOUT = 30000;

// FIXME: This is the url we want to use as it more closely matches the actual protocol we use.
Expand Down Expand Up @@ -280,7 +284,7 @@ class InspectorProxy {
this._devicesCounter = 0;
}

attachToServer(server: http.Server, pathPrefix: string) {
attachToServer(server: Server, pathPrefix: string) {
this._createPageHandler(server, pathPrefix + '/page');
this._createDeviceHandler(server, pathPrefix + '/device');
this._createPagesListHandler(server, pathPrefix + '/');
Expand Down Expand Up @@ -324,7 +328,7 @@ class InspectorProxy {
}
}
_createDeviceHandler(server: http.Server, path: string) {
_createDeviceHandler(server: Server, path: string) {
const wss = new WebSocket.Server({
server,
path,
Expand All @@ -347,7 +351,7 @@ class InspectorProxy {
});
}
_createPageHandler(server: http.Server, path: string) {
_createPageHandler(server: Server, path: string) {
const wss = new WebSocket.Server({
server,
path,
Expand All @@ -373,7 +377,7 @@ class InspectorProxy {
});
}
_createPagesJsonHandler(server: http.Server, path: string) {
_createPagesJsonHandler(server: Server, path: string) {
server.on('request', (request: http.IncomingMessage, response: http.ServerResponse) => {
if (request.url === path) {
this._getPages(server.address()).then((result: Array<Page>) => {
Expand All @@ -387,7 +391,7 @@ class InspectorProxy {
});
}
_createPagesListHandler(server: http.Server, path: string) {
_createPagesListHandler(server: Server, path: string) {
server.on('request', (request: http.IncomingMessage, response: http.ServerResponse) => {
if (request.url === path) {
this._getPages(server.address()).then((result: Array<Page>) => {
Expand Down

0 comments on commit 2c32acb

Please sign in to comment.