mirror of https://github.com/status-im/metro.git
Adds an indirection layer to enhance the Metro middleware
Reviewed By: BYK Differential Revision: D6436722 fbshipit-source-id: d864960e2eb7c8c2a96e9869c0b340cc6257808e
This commit is contained in:
parent
fa08cb3c99
commit
f66eb477ba
|
@ -25,10 +25,19 @@ import type {PostProcessModules} from './DeltaBundler';
|
||||||
import type {PostProcessModules as PostProcessModulesForBuck} from './ModuleGraph/types.flow.js';
|
import type {PostProcessModules as PostProcessModulesForBuck} from './ModuleGraph/types.flow.js';
|
||||||
import type {TransformVariants} from './ModuleGraph/types.flow';
|
import type {TransformVariants} from './ModuleGraph/types.flow';
|
||||||
import type {HasteImpl} from './node-haste/Module';
|
import type {HasteImpl} from './node-haste/Module';
|
||||||
|
import type {IncomingMessage, ServerResponse} from 'http';
|
||||||
|
|
||||||
|
type Middleware = (IncomingMessage, ServerResponse, ?(?Error) => void) => void;
|
||||||
|
|
||||||
export type ConfigT = {
|
export type ConfigT = {
|
||||||
assetRegistryPath: string,
|
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},
|
extraNodeModules: {[id: string]: string},
|
||||||
/**
|
/**
|
||||||
* Specify any additional asset file extensions to be used by the packager.
|
* Specify any additional asset file extensions to be used by the packager.
|
||||||
|
@ -148,6 +157,7 @@ export type ConfigT = {
|
||||||
|
|
||||||
const DEFAULT = ({
|
const DEFAULT = ({
|
||||||
assetRegistryPath: 'missing-asset-registry-path',
|
assetRegistryPath: 'missing-asset-registry-path',
|
||||||
|
enhanceMiddleware: middleware => middleware,
|
||||||
extraNodeModules: {},
|
extraNodeModules: {},
|
||||||
assetTransforms: false,
|
assetTransforms: false,
|
||||||
getAssetExts: () => [],
|
getAssetExts: () => [],
|
||||||
|
|
|
@ -69,7 +69,7 @@ async function runMetro({
|
||||||
maxWorkers = 1,
|
maxWorkers = 1,
|
||||||
projectRoots = [],
|
projectRoots = [],
|
||||||
watch = false,
|
watch = false,
|
||||||
}: PrivateMetroOptions) {
|
}: PrivateMetroOptions): Promise<MetroServer> {
|
||||||
const normalizedConfig = config ? Config.normalize(config) : Config.DEFAULT;
|
const normalizedConfig = config ? Config.normalize(config) : Config.DEFAULT;
|
||||||
|
|
||||||
const assetExts = defaults.assetExts.concat(
|
const assetExts = defaults.assetExts.concat(
|
||||||
|
@ -136,6 +136,7 @@ type CreateConnectMiddlewareOptions = {|
|
||||||
exports.createConnectMiddleware = async function(
|
exports.createConnectMiddleware = async function(
|
||||||
options: CreateConnectMiddlewareOptions,
|
options: CreateConnectMiddlewareOptions,
|
||||||
) {
|
) {
|
||||||
|
// $FlowFixMe I don't know why Flow thinks there's an error here... runMetro IS async
|
||||||
const metroServer = await runMetro({
|
const metroServer = await runMetro({
|
||||||
config: options.config,
|
config: options.config,
|
||||||
maxWorkers: options.maxWorkers,
|
maxWorkers: options.maxWorkers,
|
||||||
|
@ -144,10 +145,15 @@ exports.createConnectMiddleware = async function(
|
||||||
watch: true,
|
watch: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const normalizedConfig = options.config
|
||||||
|
? Config.normalize(options.config)
|
||||||
|
: Config.DEFAULT;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
middleware(req: IncomingMessage, res: ServerResponse) {
|
middleware: normalizedConfig.enhanceMiddleware(
|
||||||
return metroServer.processRequest(req, res);
|
(req: IncomingMessage, res: ServerResponse, next: ?(?Error) => void) =>
|
||||||
},
|
metroServer.processRequest(req, res, next),
|
||||||
|
),
|
||||||
end() {
|
end() {
|
||||||
metroServer.end();
|
metroServer.end();
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue