Plugs the metro runServer function into the internal CLI

Reviewed By: rafeca

Differential Revision: D6797531

fbshipit-source-id: 52dab36749c84274d7b70666ee9359afb8cac31e
This commit is contained in:
Maël Nison 2018-02-02 04:35:08 -08:00 committed by Facebook Github Bot
parent d23c3ca10b
commit a620cf05d1
1 changed files with 21 additions and 3 deletions

View File

@ -32,16 +32,18 @@ import type {ConfigT} from './Config';
import type {Reporter} from './lib/reporting'; import type {Reporter} from './lib/reporting';
import type {RequestOptions, OutputOptions} from './shared/types.flow.js'; import type {RequestOptions, OutputOptions} from './shared/types.flow.js';
import type {Options as ServerOptions} from './shared/types.flow'; import type {Options as ServerOptions} from './shared/types.flow';
import type {Server as HttpServer} from 'http'; import type {Server as HttpServer, IncomingMessage, ServerResponse} from 'http';
import type {Server as HttpsServer} from 'https'; import type {Server as HttpsServer} from 'https';
export type {ConfigT} from './Config'; export type {ConfigT} from './Config';
type Middleware = (IncomingMessage, ServerResponse, ?() => mixed) => mixed;
type PublicMetroOptions = {| type PublicMetroOptions = {|
config?: ConfigT, config?: ConfigT,
maxWorkers?: number, maxWorkers?: number,
port?: ?number, port?: ?number,
projectRoots: Array<string>, projectRoots?: Array<string>,
reporter?: Reporter, reporter?: Reporter,
// deprecated // deprecated
resetCache?: boolean, resetCache?: boolean,
@ -147,6 +149,7 @@ async function runMetro({
type CreateConnectMiddlewareOptions = {| type CreateConnectMiddlewareOptions = {|
...PublicMetroOptions, ...PublicMetroOptions,
enhanceMiddleware?: Middleware => Middleware,
|}; |};
exports.createConnectMiddleware = async function( exports.createConnectMiddleware = async function(
@ -157,6 +160,7 @@ exports.createConnectMiddleware = async function(
maxWorkers: options.maxWorkers, maxWorkers: options.maxWorkers,
port: options.port, port: options.port,
projectRoots: options.projectRoots, projectRoots: options.projectRoots,
reporter: options.reporter,
resetCache: options.resetCache, resetCache: options.resetCache,
watch: true, watch: true,
}); });
@ -165,6 +169,18 @@ exports.createConnectMiddleware = async function(
? Config.normalize(options.config) ? Config.normalize(options.config)
: Config.DEFAULT; : Config.DEFAULT;
let enhancedMiddleware = metroServer.processRequest;
// Enhance the middleware using the parameter option
if (options.enhanceMiddleware) {
enhancedMiddleware = options.enhanceMiddleware(enhancedMiddleware);
}
// Enhance the resulting middleware using the config options
if (normalizedConfig.enhanceMiddleware) {
enhancedMiddleware = normalizedConfig.enhanceMiddleware(enhancedMiddleware);
}
return { return {
attachHmrServer(httpServer: HttpServer | HttpsServer) { attachHmrServer(httpServer: HttpServer | HttpsServer) {
attachWebsocketServer({ attachWebsocketServer({
@ -174,7 +190,7 @@ exports.createConnectMiddleware = async function(
}); });
}, },
metroServer, metroServer,
middleware: normalizedConfig.enhanceMiddleware(metroServer.processRequest), middleware: enhancedMiddleware,
end() { end() {
metroServer.end(); metroServer.end();
}, },
@ -190,6 +206,7 @@ type RunServerOptions = {|
secureKey?: string, secureKey?: string,
secureCert?: string, secureCert?: string,
hmrEnabled?: boolean, hmrEnabled?: boolean,
enhanceMiddleware?: Middleware => Middleware,
|}; |};
exports.runServer = async (options: RunServerOptions) => { exports.runServer = async (options: RunServerOptions) => {
@ -213,6 +230,7 @@ exports.runServer = async (options: RunServerOptions) => {
projectRoots: options.projectRoots, projectRoots: options.projectRoots,
reporter, reporter,
resetCache: options.resetCache, resetCache: options.resetCache,
enhanceMiddleware: options.enhanceMiddleware,
}); });
serverApp.use(middleware); serverApp.use(middleware);