Skip to content

Commit 7deb525

Browse files
robhoganfacebook-github-bot
authored andcommitted
Specify minimal interface for websocketEndpoints, obviate @types/ws dependency
Summary: Define the minimal interface we need for `runServer`'s `websocketEndpoints` option - the expectations is a `Websocket` implementation will be supplied, but since there are several of those for Node JS (`ws`, `websocket`) that broadly aim to implement the same web specification, I think it's best to be unopinionated in the type we require and just specify the parts of the interface Metro needs - this has been stable for some time. The starting point for this type is from `types/ws`, here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a785fc80eb9e588cdef1e4c008225e167a8d925f/types/ws/index.d.ts#L349-L354 I've mirrored this change in Flow to ensure that our source is checked against the same rules we're exposing in TS. Changelog: ``` * **[Types]** Remove dependency on `types/ws` ``` Reviewed By: huntie Differential Revision: D44930635 fbshipit-source-id: b5b27fe4875aa39bf74d82b62463a9bd7c3846b9
1 parent 41cdc03 commit 7deb525

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

packages/metro/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
},
6464
"devDependencies": {
6565
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
66-
"@types/ws": "^8.5.4",
6766
"babel-jest": "^29.2.1",
6867
"dedent": "^0.7.0",
6968
"jest-snapshot": "^26.5.2",

packages/metro/src/index.flow.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import type {CustomResolverOptions} from 'metro-resolver';
1515
import type {ReadOnlyGraph} from './DeltaBundler';
1616
import type {ServerOptions} from './Server';
1717
import type {OutputOptions, RequestOptions} from './shared/types.flow.js';
18-
import type {Server as HttpServer} from 'http';
18+
import type EventEmitter from 'events';
19+
import type {IncomingMessage, Server as HttpServer} from 'http';
1920
import type {Server as HttpsServer} from 'https';
2021
import type {
2122
ConfigT,
@@ -24,6 +25,7 @@ import type {
2425
Middleware,
2526
} from 'metro-config/src/configTypes.flow';
2627
import type {CustomTransformOptions} from 'metro-transform-worker';
28+
import type {Duplex} from 'stream';
2729
import typeof Yargs from 'yargs';
2830

2931
const makeBuildCommand = require('./commands/build');
@@ -48,7 +50,6 @@ const {Terminal} = require('metro-core');
4850
const {InspectorProxy} = require('metro-inspector-proxy');
4951
const net = require('net');
5052
const {parse} = require('url');
51-
const ws = require('ws');
5253

5354
type MetroMiddleWare = {
5455
attachHmrServer: (httpServer: HttpServer | HttpsServer) => void,
@@ -62,6 +63,15 @@ export type RunMetroOptions = {
6263
waitForBundler?: boolean,
6364
};
6465

66+
interface WebsocketServer extends EventEmitter {
67+
handleUpgrade<T = WebsocketServer>(
68+
request: IncomingMessage,
69+
socket: Duplex,
70+
upgradeHead: Buffer,
71+
callback: (client: T, request: IncomingMessage) => void,
72+
): void;
73+
}
74+
6575
export type RunServerOptions = $ReadOnly<{
6676
hasReducedPerformance?: boolean,
6777
host?: string,
@@ -75,7 +85,7 @@ export type RunServerOptions = $ReadOnly<{
7585
waitForBundler?: boolean,
7686
watch?: boolean,
7787
websocketEndpoints?: $ReadOnly<{
78-
[path: string]: typeof ws.Server,
88+
[path: string]: WebsocketServer,
7989
}>,
8090
}>;
8191

packages/metro/types/index.d.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export * from './ModuleGraph/worker/collectDependencies';
1414
export * from './Server';
1515
export * from './lib/reporting';
1616

17-
import type {Server as HttpServer} from 'http';
17+
import type {EventEmitter} from 'events';
18+
import type {IncomingMessage, Server as HttpServer} from 'http';
1819
import type {Server as HttpsServer} from 'https';
1920
import type {
2021
ConfigT,
@@ -24,7 +25,7 @@ import type {
2425
} from 'metro-config';
2526
import type {CustomTransformOptions} from 'metro-babel-transformer';
2627
import type {ReadOnlyGraph} from './DeltaBundler/types';
27-
import type {Server} from 'ws';
28+
import type {Duplex} from 'stream';
2829
import Yargs = require('yargs');
2930
import type {default as MetroServer, ServerOptions} from './Server';
3031
import type {OutputOptions, RequestOptions} from './shared/types';
@@ -45,6 +46,15 @@ export interface RunMetroOptions extends ServerOptions {
4546
waitForBundler?: boolean;
4647
}
4748

49+
interface WebsocketServer extends EventEmitter {
50+
handleUpgrade<T = WebsocketServer>(
51+
request: IncomingMessage,
52+
socket: Duplex,
53+
upgradeHead: Buffer,
54+
callback: (client: T, request: IncomingMessage) => void,
55+
): void;
56+
}
57+
4858
export interface RunServerOptions {
4959
hasReducedPerformance?: boolean;
5060
host?: string;
@@ -65,7 +75,7 @@ export interface RunServerOptions {
6575
waitForBundler?: boolean;
6676
watch?: boolean;
6777
websocketEndpoints?: {
68-
[path: string]: Server;
78+
[path: string]: WebsocketServer;
6979
};
7080
}
7181

yarn.lock

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,13 +1296,6 @@
12961296
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff"
12971297
integrity sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw==
12981298

1299-
"@types/ws@^8.5.4":
1300-
version "8.5.4"
1301-
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.4.tgz#bb10e36116d6e570dd943735f86c933c1587b8a5"
1302-
integrity sha512-zdQDHKUgcX/zBc4GrwsE/7dVdAD8JR4EuiAXiiUhhfyIJXXb2+PrGshFyeXWQPMmmZ2XxgaqclgpIC7eTXc1mg==
1303-
dependencies:
1304-
"@types/node" "*"
1305-
13061299
"@types/yargs-parser@*":
13071300
version "15.0.0"
13081301
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d"

0 commit comments

Comments
 (0)