Skip to content

Commit d47afe2

Browse files
huntiefacebook-github-bot
authored andcommitted
Create dev-middleware package (facebook#38194)
Summary: Pull Request resolved: facebook#38194 ## Context RFC: Decoupling Flipper from React Native core: react-native-community/discussions-and-proposals#641 ## Changes Inits the `react-native/dev-middleware` package. Contains an initial implementation of `/open-debugger`, migrated from react-native-community/cli@2535dbe. ## Attribution This implementation is greatly inspired by `expo/dev-server`: https://github.com/expo/expo/blob/1120c716f35cb28d88800e8f5d963d2b2ac94705/packages/%40expo/dev-server/src/JsInspector.ts#L18 Changelog: [Internal] Reviewed By: motiz88 Differential Revision: D46283818 fbshipit-source-id: 4eb8e3e09d0ce05418c98526d5d136e6aad0143e
1 parent 5846be0 commit d47afe2

18 files changed

+739
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
declare module 'chrome-launcher' {
13+
import typeof fs from 'fs';
14+
import typeof childProcess from 'child_process';
15+
import type {ChildProcess} from 'child_process';
16+
17+
declare export type Options = {
18+
startingUrl?: string,
19+
chromeFlags?: Array<string>,
20+
prefs?: mixed,
21+
port?: number,
22+
handleSIGINT?: boolean,
23+
chromePath?: string,
24+
userDataDir?: string | boolean,
25+
logLevel?: 'verbose' | 'info' | 'error' | 'warn' | 'silent',
26+
ignoreDefaultFlags?: boolean,
27+
connectionPollInterval?: number,
28+
maxConnectionRetries?: number,
29+
envVars?: {[key: string]: ?string},
30+
};
31+
32+
declare export type LaunchedChrome = {
33+
pid: number,
34+
port: number,
35+
process: ChildProcess,
36+
kill: () => void,
37+
};
38+
39+
declare export type ModuleOverrides = {
40+
fs?: fs,
41+
spawn?: childProcess['spawn'],
42+
};
43+
44+
declare class Launcher {
45+
launch(options: Options): Promise<LaunchedChrome>;
46+
}
47+
48+
declare module.exports: Launcher;
49+
}

flow-typed/npm/connect_v3.x.x.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
declare module 'connect' {
13+
import type http from 'http';
14+
15+
declare export type ServerHandle = HandleFunction | http.Server;
16+
17+
declare type NextFunction = (err?: mixed) => void;
18+
19+
declare export type NextHandleFunction = (
20+
req: IncomingMessage,
21+
res: http.ServerResponse,
22+
next: NextFunction,
23+
) => void | Promise<void>;
24+
declare export type HandleFunction = NextHandleFunction;
25+
26+
declare export interface IncomingMessage extends http.IncomingMessage {
27+
originalUrl?: http.IncomingMessage['url'];
28+
}
29+
30+
declare export interface Server extends events$EventEmitter {
31+
(req: IncomingMessage, res: http.ServerResponse): void;
32+
33+
use(fn: HandleFunction): Server;
34+
use(route: string, fn: HandleFunction): Server;
35+
36+
listen(
37+
port: number,
38+
hostname?: string,
39+
backlog?: number,
40+
callback?: Function,
41+
): http.Server;
42+
listen(port: number, hostname?: string, callback?: Function): http.Server;
43+
listen(path: string, callback?: Function): http.Server;
44+
listen(handle: any, listeningListener?: Function): http.Server;
45+
}
46+
47+
declare type createServer = () => Server;
48+
49+
declare module.exports: createServer;
50+
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
// Modified from flow-typed repo:
13+
// https://github.com/flow-typed/flow-typed/blob/master/definitions/npm/node-fetch_v2.x.x/flow_v0.104.x-/node-fetch_v2.x.x.js
14+
15+
declare module 'node-fetch' {
16+
import type http from 'http';
17+
import type https from 'https';
18+
import type {URL} from 'url';
19+
import type {Readable} from 'stream';
20+
21+
declare export type AbortSignal = {
22+
+aborted: boolean,
23+
+onabort: (event?: {...}) => void,
24+
25+
+addEventListener: (name: string, cb: () => mixed) => void,
26+
+removeEventListener: (name: string, cb: () => mixed) => void,
27+
+dispatchEvent: (event: {...}) => void,
28+
...
29+
};
30+
31+
declare export class Request mixins Body {
32+
constructor(
33+
input: string | {href: string, ...} | Request,
34+
init?: RequestInit,
35+
): this;
36+
context: RequestContext;
37+
headers: Headers;
38+
method: string;
39+
redirect: RequestRedirect;
40+
referrer: string;
41+
url: string;
42+
43+
// node-fetch extensions
44+
agent: http.Agent | https.Agent;
45+
compress: boolean;
46+
counter: number;
47+
follow: number;
48+
hostname: string;
49+
port: number;
50+
protocol: string;
51+
size: number;
52+
timeout: number;
53+
}
54+
55+
declare type HeaderObject = {[index: string]: string | number, ...};
56+
57+
declare export type RequestInit = {|
58+
body?: BodyInit,
59+
headers?: HeaderObject | null,
60+
method?: string,
61+
redirect?: RequestRedirect,
62+
signal?: AbortSignal | null,
63+
64+
// node-fetch extensions
65+
agent?: (URL => http.Agent | https.Agent) | http.Agent | https.Agent | null,
66+
compress?: boolean,
67+
follow?: number,
68+
size?: number,
69+
timeout?: number,
70+
|};
71+
72+
declare export interface FetchError extends Error {
73+
// cannot set name due to incompatible extend error
74+
// name: 'FetchError';
75+
type: string;
76+
code: ?number;
77+
errno: ?number;
78+
}
79+
80+
declare export interface AbortError extends Error {
81+
// cannot set name due to incompatible extend error
82+
// name: 'AbortError';
83+
type: 'aborted';
84+
}
85+
86+
declare type RequestContext =
87+
| 'audio'
88+
| 'beacon'
89+
| 'cspreport'
90+
| 'download'
91+
| 'embed'
92+
| 'eventsource'
93+
| 'favicon'
94+
| 'fetch'
95+
| 'font'
96+
| 'form'
97+
| 'frame'
98+
| 'hyperlink'
99+
| 'iframe'
100+
| 'image'
101+
| 'imageset'
102+
| 'import'
103+
| 'internal'
104+
| 'location'
105+
| 'manifest'
106+
| 'object'
107+
| 'ping'
108+
| 'plugin'
109+
| 'prefetch'
110+
| 'script'
111+
| 'serviceworker'
112+
| 'sharedworker'
113+
| 'subresource'
114+
| 'style'
115+
| 'track'
116+
| 'video'
117+
| 'worker'
118+
| 'xmlhttprequest'
119+
| 'xslt';
120+
declare type RequestRedirect = 'error' | 'follow' | 'manual';
121+
122+
declare export class Headers {
123+
append(name: string, value: string): void;
124+
delete(name: string): void;
125+
forEach(callback: (value: string, name: string) => void): void;
126+
get(name: string): string;
127+
getAll(name: string): Array<string>;
128+
has(name: string): boolean;
129+
raw(): {[k: string]: string[], ...};
130+
set(name: string, value: string): void;
131+
entries(): Iterator<[string, string]>;
132+
keys(): Iterator<string>;
133+
values(): Iterator<string>;
134+
@@iterator(): Iterator<[string, string]>;
135+
}
136+
137+
declare export class Body {
138+
buffer(): Promise<Buffer>;
139+
json(): Promise<any>;
140+
json<T>(): Promise<T>;
141+
text(): Promise<string>;
142+
body: stream$Readable;
143+
bodyUsed: boolean;
144+
}
145+
146+
declare export class Response mixins Body {
147+
constructor(body?: BodyInit, init?: ResponseInit): this;
148+
clone(): Response;
149+
error(): Response;
150+
redirect(url: string, status: number): Response;
151+
headers: Headers;
152+
ok: boolean;
153+
status: number;
154+
statusText: string;
155+
size: number;
156+
timeout: number;
157+
type: ResponseType;
158+
url: string;
159+
}
160+
161+
declare type ResponseType =
162+
| 'basic'
163+
| 'cors'
164+
| 'default'
165+
| 'error'
166+
| 'opaque'
167+
| 'opaqueredirect';
168+
169+
declare interface ResponseInit {
170+
headers?: HeaderInit;
171+
status: number;
172+
statusText?: string;
173+
}
174+
175+
declare type HeaderInit = Headers | Array<string>;
176+
declare type BodyInit =
177+
| string
178+
| null
179+
| Buffer
180+
| Blob
181+
| Readable
182+
| URLSearchParams;
183+
184+
declare export default function fetch(
185+
url: string | Request,
186+
init?: RequestInit,
187+
): Promise<Response>;
188+
}

flow-typed/npm/temp-dir_2.x.x.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
declare module 'temp-dir' {
13+
declare module.exports: string;
14+
}

packages/dev-middleware/.babelrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"presets": [
3+
"@babel/preset-flow",
4+
[
5+
"@babel/preset-env",
6+
{
7+
"targets": {
8+
"node": "16"
9+
}
10+
}
11+
]
12+
]
13+
}

packages/dev-middleware/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Dependencies
2+
/node_modules
3+
4+
# Build output
5+
/dist

packages/dev-middleware/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# @react-native/dev-middleware
2+
3+
![https://img.shields.io/npm/v/@react-native/dev-middleware?color=brightgreen&label=npm%20package](https://www.npmjs.com/package/@react-native/dev-middleware)
4+
5+
Dev server middleware supporting core React Native development features. This package is preconfigured in all React Native projects.
6+
7+
## Endpoints
8+
9+
### `/open-debugger`
10+
11+
Open the JavaScript debugger for a given CDP target (direct Hermes debugging).
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
export * from './src';
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "@react-native/dev-middleware",
3+
"version": "0.73.0",
4+
"description": "Dev server middleware for React Native",
5+
"keywords": [
6+
"react-native",
7+
"tools"
8+
],
9+
"homepage": "https://github.com/facebook/react-native/tree/HEAD/packages/dev-middleware#readme",
10+
"bugs": "https://github.com/facebook/react-native/issues",
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/facebook/react-native.git",
14+
"directory": "packages/dev-middleware"
15+
},
16+
"license": "MIT",
17+
"exports": "./dist/index.js",
18+
"files": [
19+
"dist"
20+
],
21+
"scripts": {
22+
"build": "yarn clean && babel src --out-dir dist",
23+
"dev": "babel src --out-dir dist --source-maps --watch",
24+
"clean": "rimraf dist",
25+
"prepare": "yarn build"
26+
},
27+
"dependencies": {
28+
"chrome-launcher": "^0.15.2",
29+
"connect": "^3.6.5",
30+
"node-fetch": "^2.6.0",
31+
"temp-dir": "^2.0.0"
32+
},
33+
"devDependencies": {
34+
"@babel/cli": "^7.20.0",
35+
"@babel/core": "^7.20.0",
36+
"@babel/preset-env": "^7.20.0",
37+
"@babel/preset-flow": "^7.20.0",
38+
"rimraf": "^3.0.2"
39+
},
40+
"engines": {
41+
"node": ">=18"
42+
}
43+
}

0 commit comments

Comments
 (0)