Adds an indirection layer to enhance the Metro middleware

Reviewed By: BYK

Differential Revision: D6436722

fbshipit-source-id: d864960e2eb7c8c2a96e9869c0b340cc6257808e
This commit is contained in:
Maël Nison 2017-12-11 10:10:54 -08:00 committed by Facebook Github Bot
parent fa08cb3c99
commit f66eb477ba
2 changed files with 20 additions and 4 deletions

View File

@ -25,10 +25,19 @@ import type {PostProcessModules} from './DeltaBundler';
import type {PostProcessModules as PostProcessModulesForBuck} from './ModuleGraph/types.flow.js';
import type {TransformVariants} from './ModuleGraph/types.flow';
import type {HasteImpl} from './node-haste/Module';
import type {IncomingMessage, ServerResponse} from 'http';
type Middleware = (IncomingMessage, ServerResponse, ?(?Error) => void) => void;
export type ConfigT = {
assetRegistryPath: string,
/**
* Called with the Metro middleware in parameter; can be used to wrap this
* middleware inside another one
*/
enhanceMiddleware: Middleware => Middleware,
extraNodeModules: {[id: string]: string},
/**
* Specify any additional asset file extensions to be used by the packager.
@ -148,6 +157,7 @@ export type ConfigT = {
const DEFAULT = ({
assetRegistryPath: 'missing-asset-registry-path',
enhanceMiddleware: middleware => middleware,
extraNodeModules: {},
assetTransforms: false,
getAssetExts: () => [],

View File

@ -69,7 +69,7 @@ async function runMetro({
maxWorkers = 1,
projectRoots = [],
watch = false,
}: PrivateMetroOptions) {
}: PrivateMetroOptions): Promise<MetroServer> {
const normalizedConfig = config ? Config.normalize(config) : Config.DEFAULT;
const assetExts = defaults.assetExts.concat(
@ -136,6 +136,7 @@ type CreateConnectMiddlewareOptions = {|
exports.createConnectMiddleware = async function(
options: CreateConnectMiddlewareOptions,
) {
// $FlowFixMe I don't know why Flow thinks there's an error here... runMetro IS async
const metroServer = await runMetro({
config: options.config,
maxWorkers: options.maxWorkers,
@ -144,10 +145,15 @@ exports.createConnectMiddleware = async function(
watch: true,
});
const normalizedConfig = options.config
? Config.normalize(options.config)
: Config.DEFAULT;
return {
middleware(req: IncomingMessage, res: ServerResponse) {
return metroServer.processRequest(req, res);
},
middleware: normalizedConfig.enhanceMiddleware(
(req: IncomingMessage, res: ServerResponse, next: ?(?Error) => void) =>
metroServer.processRequest(req, res, next),
),
end() {
metroServer.end();
},