Skip to content

Commit

Permalink
Add unstable_extraMiddleware option to runServer
Browse files Browse the repository at this point in the history
Summary:
This adds a new option to Metro's  `runServer` API providing a convenient hook to mount custom [`connect`](https://www.npmjs.com/package/connect) middleware on Metro's HTTP server. This replaces the `server.enhanceMiddleware` Metro config option (deprecated in D46356577).

This option is `unstable_` as we work through using this option in integrating projects.

Changelog: **[Experimental]** Add `unstable_extraMiddleware` option to `runServer` API

Reviewed By: motiz88

Differential Revision: D46359829

fbshipit-source-id: 58aafcf1f33aed24a5163f8f4277976bff3717f4
  • Loading branch information
huntie authored and facebook-github-bot committed Jun 2, 2023
1 parent 22e85fd commit d0d5543
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ Type: `(Middleware, MetroServer) => Middleware`
A function that allows attaching custom [`connect`](https://www.npmjs.com/package/connect) middleware to Metro. For example:
:::tip
You can use [`connect()`](https://www.npmjs.com/package/connect#mount-middleware) as a util to extend the base `metroMiddleware` and to mount additional middleware handlers.
You can use [`connect()`](https://www.npmjs.com/package/connect#mount-middleware) as a utility to extend the base `metroMiddleware` and to mount additional middleware handlers.
:::
```ts
Expand Down
7 changes: 7 additions & 0 deletions packages/metro/src/index.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

'use strict';

import type {HandleFunction} from 'connect';
import type {CustomResolverOptions} from 'metro-resolver';
import type {ReadOnlyGraph} from './DeltaBundler';
import type {ServerOptions} from './Server';
Expand Down Expand Up @@ -82,6 +83,7 @@ export type RunServerOptions = $ReadOnly<{
secure?: boolean, // deprecated
secureCert?: string, // deprecated
secureKey?: string, // deprecated
unstable_extraMiddleware?: $ReadOnlyArray<HandleFunction>,
waitForBundler?: boolean,
watch?: boolean,
websocketEndpoints?: $ReadOnly<{
Expand Down Expand Up @@ -254,6 +256,7 @@ exports.runServer = async (
secure, //deprecated
secureCert, // deprecated
secureKey, // deprecated
unstable_extraMiddleware,
waitForBundler = false,
websocketEndpoints = {},
watch,
Expand Down Expand Up @@ -283,6 +286,10 @@ exports.runServer = async (

serverApp.use(middleware);

for (const handler of unstable_extraMiddleware ?? []) {
serverApp.use(handler);
}

let inspectorProxy: ?InspectorProxy = null;
if (config.server.runInspectorProxy) {
inspectorProxy = new InspectorProxy(config.projectRoot);
Expand Down
2 changes: 2 additions & 0 deletions packages/metro/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './ModuleGraph/worker/collectDependencies';
export * from './Server';
export * from './lib/reporting';

import type {HandleFunction} from 'connect';
import type {EventEmitter} from 'events';
import type {IncomingMessage, Server as HttpServer} from 'http';
import type {Server as HttpsServer} from 'https';
Expand Down Expand Up @@ -72,6 +73,7 @@ export interface RunServerOptions {
/** @deprecated since version 0.61 */
secureKey?: string;

unstable_extraMiddleware?: ReadonlyArray<HandleFunction>;
waitForBundler?: boolean;
watch?: boolean;
websocketEndpoints?: {
Expand Down

0 comments on commit d0d5543

Please sign in to comment.