Add https options to RN CLI server

Reviewed By: davidaurelio

Differential Revision: D5223852

fbshipit-source-id: 91025751ddf1f4ec4adcc768b69b936ee1a68edf
This commit is contained in:
Andrew Imm 2017-06-12 16:06:36 -07:00 committed by Facebook Github Bot
parent 50b11aa09b
commit 2c32acb755
4 changed files with 46 additions and 21 deletions

View File

@ -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,
path: '/hot',
packagerServer,
});
wsProxy = webSocketProxy.attachToServer( const serverInstance = args.https
serverInstance, ? https.createServer(
'/debugger-proxy', {
); key: fs.readFileSync(args.key),
ms = messageSocket.attachToServer(serverInstance, '/message'); cert: fs.readFileSync(args.cert),
inspectorProxy.attachToServer(serverInstance, '/inspector'); },
readyCallback(packagerServer._reporter); 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 // 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.

View File

@ -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',
}], }],
}; };

View File

@ -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,
}; };

View File

@ -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>) => {