Skip to content

Commit

Permalink
Specify minimal interface for websocketEndpoints, obviate `@types/w…
Browse files Browse the repository at this point in the history
…s` 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
  • Loading branch information
robhogan authored and facebook-github-bot committed Apr 13, 2023
1 parent 41cdc03 commit 7deb525
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
1 change: 0 additions & 1 deletion packages/metro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
},
"devDependencies": {
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
"@types/ws": "^8.5.4",
"babel-jest": "^29.2.1",
"dedent": "^0.7.0",
"jest-snapshot": "^26.5.2",
Expand Down
16 changes: 13 additions & 3 deletions packages/metro/src/index.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import type {CustomResolverOptions} from 'metro-resolver';
import type {ReadOnlyGraph} from './DeltaBundler';
import type {ServerOptions} from './Server';
import type {OutputOptions, RequestOptions} from './shared/types.flow.js';
import type {Server as HttpServer} from 'http';
import type EventEmitter from 'events';
import type {IncomingMessage, Server as HttpServer} from 'http';
import type {Server as HttpsServer} from 'https';
import type {
ConfigT,
Expand All @@ -24,6 +25,7 @@ import type {
Middleware,
} from 'metro-config/src/configTypes.flow';
import type {CustomTransformOptions} from 'metro-transform-worker';
import type {Duplex} from 'stream';
import typeof Yargs from 'yargs';

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

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

interface WebsocketServer extends EventEmitter {
handleUpgrade<T = WebsocketServer>(
request: IncomingMessage,
socket: Duplex,
upgradeHead: Buffer,
callback: (client: T, request: IncomingMessage) => void,
): void;
}

export type RunServerOptions = $ReadOnly<{
hasReducedPerformance?: boolean,
host?: string,
Expand All @@ -75,7 +85,7 @@ export type RunServerOptions = $ReadOnly<{
waitForBundler?: boolean,
watch?: boolean,
websocketEndpoints?: $ReadOnly<{
[path: string]: typeof ws.Server,
[path: string]: WebsocketServer,
}>,
}>;

Expand Down
16 changes: 13 additions & 3 deletions packages/metro/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export * from './ModuleGraph/worker/collectDependencies';
export * from './Server';
export * from './lib/reporting';

import type {Server as HttpServer} from 'http';
import type {EventEmitter} from 'events';
import type {IncomingMessage, Server as HttpServer} from 'http';
import type {Server as HttpsServer} from 'https';
import type {
ConfigT,
Expand All @@ -24,7 +25,7 @@ import type {
} from 'metro-config';
import type {CustomTransformOptions} from 'metro-babel-transformer';
import type {ReadOnlyGraph} from './DeltaBundler/types';
import type {Server} from 'ws';
import type {Duplex} from 'stream';
import Yargs = require('yargs');
import type {default as MetroServer, ServerOptions} from './Server';
import type {OutputOptions, RequestOptions} from './shared/types';
Expand All @@ -45,6 +46,15 @@ export interface RunMetroOptions extends ServerOptions {
waitForBundler?: boolean;
}

interface WebsocketServer extends EventEmitter {
handleUpgrade<T = WebsocketServer>(
request: IncomingMessage,
socket: Duplex,
upgradeHead: Buffer,
callback: (client: T, request: IncomingMessage) => void,
): void;
}

export interface RunServerOptions {
hasReducedPerformance?: boolean;
host?: string;
Expand All @@ -65,7 +75,7 @@ export interface RunServerOptions {
waitForBundler?: boolean;
watch?: boolean;
websocketEndpoints?: {
[path: string]: Server;
[path: string]: WebsocketServer;
};
}

Expand Down
7 changes: 0 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1296,13 +1296,6 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff"
integrity sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw==

"@types/ws@^8.5.4":
version "8.5.4"
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.4.tgz#bb10e36116d6e570dd943735f86c933c1587b8a5"
integrity sha512-zdQDHKUgcX/zBc4GrwsE/7dVdAD8JR4EuiAXiiUhhfyIJXXb2+PrGshFyeXWQPMmmZ2XxgaqclgpIC7eTXc1mg==
dependencies:
"@types/node" "*"

"@types/yargs-parser@*":
version "15.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d"
Expand Down

0 comments on commit 7deb525

Please sign in to comment.