From a620cf05d1d31d325c6c31f0c77f008d34ea42a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Fri, 2 Feb 2018 04:35:08 -0800 Subject: [PATCH] Plugs the metro runServer function into the internal CLI Reviewed By: rafeca Differential Revision: D6797531 fbshipit-source-id: 52dab36749c84274d7b70666ee9359afb8cac31e --- packages/metro/src/index.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/metro/src/index.js b/packages/metro/src/index.js index f15aabdb..e9360549 100644 --- a/packages/metro/src/index.js +++ b/packages/metro/src/index.js @@ -32,16 +32,18 @@ import type {ConfigT} from './Config'; import type {Reporter} from './lib/reporting'; import type {RequestOptions, OutputOptions} from './shared/types.flow.js'; 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'; export type {ConfigT} from './Config'; +type Middleware = (IncomingMessage, ServerResponse, ?() => mixed) => mixed; + type PublicMetroOptions = {| config?: ConfigT, maxWorkers?: number, port?: ?number, - projectRoots: Array, + projectRoots?: Array, reporter?: Reporter, // deprecated resetCache?: boolean, @@ -147,6 +149,7 @@ async function runMetro({ type CreateConnectMiddlewareOptions = {| ...PublicMetroOptions, + enhanceMiddleware?: Middleware => Middleware, |}; exports.createConnectMiddleware = async function( @@ -157,6 +160,7 @@ exports.createConnectMiddleware = async function( maxWorkers: options.maxWorkers, port: options.port, projectRoots: options.projectRoots, + reporter: options.reporter, resetCache: options.resetCache, watch: true, }); @@ -165,6 +169,18 @@ exports.createConnectMiddleware = async function( ? Config.normalize(options.config) : 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 { attachHmrServer(httpServer: HttpServer | HttpsServer) { attachWebsocketServer({ @@ -174,7 +190,7 @@ exports.createConnectMiddleware = async function( }); }, metroServer, - middleware: normalizedConfig.enhanceMiddleware(metroServer.processRequest), + middleware: enhancedMiddleware, end() { metroServer.end(); }, @@ -190,6 +206,7 @@ type RunServerOptions = {| secureKey?: string, secureCert?: string, hmrEnabled?: boolean, + enhanceMiddleware?: Middleware => Middleware, |}; exports.runServer = async (options: RunServerOptions) => { @@ -213,6 +230,7 @@ exports.runServer = async (options: RunServerOptions) => { projectRoots: options.projectRoots, reporter, resetCache: options.resetCache, + enhanceMiddleware: options.enhanceMiddleware, }); serverApp.use(middleware);