Permalink
Browse files

Add https options to RN CLI server

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 2c32acb755730ac20775aec4aad50d853d0577bf
@@ -26,8 +26,10 @@ const defaultSourceExts = require('metro-bundler/build/defaults').sourceExts;
const defaultPlatforms = require('metro-bundler/build/defaults').platforms; const defaultPlatforms = require('metro-bundler/build/defaults').platforms;
const defaultProvidesModuleNodeModules = require('metro-bundler/build/defaults') const defaultProvidesModuleNodeModules = require('metro-bundler/build/defaults')
.providesModuleNodeModules; .providesModuleNodeModules;
const fs = require('fs');
const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware'); const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware');
const http = require('http'); const http = require('http');
const https = require('https');
const indexPageMiddleware = require('./middleware/indexPage'); const indexPageMiddleware = require('./middleware/indexPage');
const loadRawBodyMiddleware = require('./middleware/loadRawBodyMiddleware'); const loadRawBodyMiddleware = require('./middleware/loadRawBodyMiddleware');
const messageSocket = require('./util/messageSocket.js'); const messageSocket = require('./util/messageSocket.js');
@@ -89,23 +91,32 @@ function runServer(
app.use(connect.logger()).use(connect.errorHandler()); app.use(connect.logger()).use(connect.errorHandler());
const serverInstance = http if (args.https && (!args.key || !args.cert)) {
.createServer(app) throw new Error('Cannot use https without specifying key and cert options');
.listen(args.port, args.host, 511, function() { }
attachHMRServer({
httpServer: serverInstance, const serverInstance = args.https
path: '/hot', ? https.createServer(
packagerServer, {
}); key: fs.readFileSync(args.key),
cert: fs.readFileSync(args.cert),
wsProxy = webSocketProxy.attachToServer( },
serverInstance, app,
'/debugger-proxy', )
); : http.createServer(app);
ms = messageSocket.attachToServer(serverInstance, '/message');
inspectorProxy.attachToServer(serverInstance, '/inspector'); serverInstance.listen(args.port, args.host, 511, function() {
readyCallback(packagerServer._reporter); 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 // Disable any kind of automatic timeout behavior for incoming
// requests in case it takes the packager more than the default // requests in case it takes the packager more than the default
// timeout of 120 seconds to respond to a request. // timeout of 120 seconds to respond to a request.
@@ -121,5 +121,14 @@ module.exports = {
}, { }, {
command: '--verbose', command: '--verbose',
description: 'Enables logging', 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',
}], }],
}; };
@@ -17,6 +17,7 @@ const url = require('url');
import type {ResolutionResponse} from './getInverseDependencies'; import type {ResolutionResponse} from './getInverseDependencies';
import type {Server as HTTPServer} from 'http'; import type {Server as HTTPServer} from 'http';
import type {Server as HTTPSServer} from 'https';
const blacklist = [ const blacklist = [
'Libraries/Utilities/HMRClient.js', 'Libraries/Utilities/HMRClient.js',
@@ -57,7 +58,7 @@ type PackagerServer<TModule> = {
}; };
type HMROptions<TModule> = { type HMROptions<TModule> = {
httpServer: HTTPServer, httpServer: HTTPServer | HTTPSServer,
packagerServer: PackagerServer<TModule>, packagerServer: PackagerServer<TModule>,
path: string, path: string,
}; };
@@ -48,6 +48,8 @@ const WebSocket = require('ws');
const debug = require('debug')('RNP:InspectorProxy'); const debug = require('debug')('RNP:InspectorProxy');
const launchChrome = require('./launchChrome'); const launchChrome = require('./launchChrome');
import type {Server as HTTPSServer} from 'https';
type DevicePage = { type DevicePage = {
id: string, id: string,
title: string, title: string,
@@ -94,6 +96,8 @@ type Address = {
port: number, port: number,
}; };
type Server = http.Server | HTTPSServer;
const DEVICE_TIMEOUT = 30000; const DEVICE_TIMEOUT = 30000;
// FIXME: This is the url we want to use as it more closely matches the actual protocol we use. // FIXME: This is the url we want to use as it more closely matches the actual protocol we use.
@@ -280,7 +284,7 @@ class InspectorProxy {
this._devicesCounter = 0; this._devicesCounter = 0;
} }
attachToServer(server: http.Server, pathPrefix: string) { attachToServer(server: Server, pathPrefix: string) {
this._createPageHandler(server, pathPrefix + '/page'); this._createPageHandler(server, pathPrefix + '/page');
this._createDeviceHandler(server, pathPrefix + '/device'); this._createDeviceHandler(server, pathPrefix + '/device');
this._createPagesListHandler(server, pathPrefix + '/'); this._createPagesListHandler(server, pathPrefix + '/');
@@ -324,7 +328,7 @@ class InspectorProxy {
} }
} }
_createDeviceHandler(server: http.Server, path: string) { _createDeviceHandler(server: Server, path: string) {
const wss = new WebSocket.Server({ const wss = new WebSocket.Server({
server, server,
path, path,
@@ -347,7 +351,7 @@ class InspectorProxy {
}); });
} }
_createPageHandler(server: http.Server, path: string) { _createPageHandler(server: Server, path: string) {
const wss = new WebSocket.Server({ const wss = new WebSocket.Server({
server, server,
path, path,
@@ -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) => { server.on('request', (request: http.IncomingMessage, response: http.ServerResponse) => {
if (request.url === path) { if (request.url === path) {
this._getPages(server.address()).then((result: Array<Page>) => { this._getPages(server.address()).then((result: Array<Page>) => {
@@ -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) => { server.on('request', (request: http.IncomingMessage, response: http.ServerResponse) => {
if (request.url === path) { if (request.url === path) {
this._getPages(server.address()).then((result: Array<Page>) => { this._getPages(server.address()).then((result: Array<Page>) => {

0 comments on commit 2c32acb

Please sign in to comment.