Skip to content

Commit

Permalink
chore: convert server files to TS (react-native-community#783)
Browse files Browse the repository at this point in the history
* chore: convert server files to TS

* remove a bunch of flowfixmes

* cast rawbody
  • Loading branch information
thymikee authored and grabbou committed Oct 13, 2019
1 parent dd1fb89 commit d7b78f4
Show file tree
Hide file tree
Showing 24 changed files with 226 additions and 133 deletions.
5 changes: 4 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"@react-native-community/cli-tools": "^2.8.3",
"@react-native-community/cli-types": "^3.0.0-alpha.2",
"@types/mkdirp": "^0.5.2",
"@types/node-notifier": "^5.4.0",
"@types/semver": "^6.0.2",
"@types/ws": "^6.0.3",
"@types/node-notifier": "^5.4.0",
"chalk": "^2.4.2",
"command-exists": "^1.2.8",
"commander": "^2.19.0",
Expand Down Expand Up @@ -72,11 +72,14 @@
},
"devDependencies": {
"@types/command-exists": "^1.2.0",
"@types/compression": "^1.0.1",
"@types/cosmiconfig": "^5.0.3",
"@types/errorhandler": "^0.0.32",
"@types/graceful-fs": "^4.1.3",
"@types/hapi__joi": "^15.0.4",
"@types/minimist": "^1.2.0",
"@types/mkdirp": "^0.5.2",
"@types/morgan": "^1.7.37",
"@types/semver": "^6.0.2",
"@types/wcwidth": "^1.0.0",
"slash": "^3.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export default {
// See the PR: https://github.com/tabrindle/envinfo/pull/119
if (sdks === 'Not Found' && process.platform !== 'darwin') {
try {
// $FlowFixMe bad execa types
const {stdout} = await execa(
process.env.ANDROID_HOME
? `${process.env.ANDROID_HOME}/tools/bin/sdkmanager`
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands/server/messageSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ function attachToServer(server: Server, path: string) {
});

return {
broadcast: (method: string, params: Record<string, any>) => {
isDebuggerConnected: () => true,
broadcast: (method: string, params?: Record<string, any>) => {
handleSendBroadcast(null, {method, params});
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @strict
* @flow
*/

import compression from 'compression';
Expand All @@ -26,19 +23,18 @@ import systraceProfileMiddleware from './systraceProfileMiddleware';
import getDevToolsMiddleware from './getDevToolsMiddleware';

type Options = {
+watchFolders: $ReadOnlyArray<string>,
+host?: string,
host?: string;
watchFolders: Array<string>;
port: number;
};

type WebSocketProxy = {
server: WebSocketServer,
isDebuggerConnected: () => boolean,
server?: WebSocketServer;
isDebuggerConnected: () => boolean;
};

type Connect = $Call<connect>;

export default class MiddlewareManager {
app: Connect;
app: connect.Server;

options: Options;

Expand All @@ -49,6 +45,7 @@ export default class MiddlewareManager {
this.app = connect()
.use(getSecurityHeadersMiddleware)
.use(loadRawBodyMiddleware)
// @ts-ignore compression and connect types mismatch
.use(compression())
.use('/debugger-ui', serveStatic(debuggerUIFolder))
.use(openStackFrameInEditorMiddleware(this.options))
Expand All @@ -61,6 +58,7 @@ export default class MiddlewareManager {
}

serveStatic(folder: string) {
// @ts-ignore serveStatic and connect types mismatch
this.app.use(serveStatic(folder));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

import http from 'http';
import copyToClipBoard from '../copyToClipBoard';
import {logger} from '@react-native-community/cli-tools';

/**
* Handle the request from JS to copy contents onto host system clipboard.
* This is only supported on Mac for now.
*/
export default function copyMiddleware(req, res, next) {
export default function copyMiddleware(
req: http.IncomingMessage,
res: http.ServerResponse,
next: (err?: any) => void,
) {
if (req.url === '/copy-to-clipboard') {
const ret = copyToClipBoard(req.rawBody);
const ret = copyToClipBoard(
(req as http.IncomingMessage & {rawBody: string}).rawBody,
);
if (!ret) {
logger.warn('Copy button is not supported on this platform!');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import http from 'http';
import {logger} from '@react-native-community/cli-tools';
import {exec} from 'child_process';
import launchDebugger from '../launchDebugger';

function launchDefaultDebugger(port, args = '') {
function launchDefaultDebugger(port: number, args = '') {
const debuggerURL = `http://localhost:${port}/debugger-ui${args}`;
logger.info('Launching Dev Tools...');
launchDebugger(debuggerURL);
}

function escapePath(pathname) {
function escapePath(pathname: string) {
// " Can escape paths with spaces in OS X, Windows, and *nix
return `"${pathname}"`;
}

function launchDevTools({port, watchFolders}, isDebuggerConnected) {
type LaunchDevToolsOptions = {port: number; watchFolders: Array<string>};

function launchDevTools(
{port, watchFolders}: LaunchDevToolsOptions,
isDebuggerConnected: () => boolean,
) {
// Explicit config always wins
const customDebugger = process.env.REACT_DEBUGGER;
if (customDebugger) {
Expand All @@ -32,19 +36,32 @@ function launchDevTools({port, watchFolders}, isDebuggerConnected) {
}
}

function startCustomDebugger({watchFolders, customDebugger}) {
function startCustomDebugger({
watchFolders,
customDebugger,
}: {
watchFolders: Array<string>;
customDebugger: string;
}) {
const folders = watchFolders.map(escapePath).join(' ');
const command = `${customDebugger} ${folders}`;
logger.info('Starting custom debugger by executing:', command);
exec(command, function(error, stdout, stderr) {
exec(command, function(error) {
if (error !== null) {
logger.error('Error while starting custom debugger:', error);
logger.error('Error while starting custom debugger:', error.stack || '');
}
});
}

export default function getDevToolsMiddleware(options, isDebuggerConnected) {
return function devToolsMiddleware(req, res, next) {
export default function getDevToolsMiddleware(
options: LaunchDevToolsOptions,
isDebuggerConnected: () => boolean,
) {
return function devToolsMiddleware(
req: http.IncomingMessage,
res: http.ServerResponse,
next: (err?: any) => void,
) {
if (req.url === '/launch-js-devtools') {
launchDevTools(options, isDebuggerConnected);
res.end('OK');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @strict
* @format
*/
import http from 'http';

export default function getSecurityHeadersMiddleware(req, res, next) {
export default function getSecurityHeadersMiddleware(
req: http.IncomingMessage,
res: http.ServerResponse,
next: (err?: any) => void,
) {
// @ts-ignore Property 'client' does not exist on type 'IncomingMessage', verify
const address = req.client.server.address();

// Block any cross origin request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

import http from 'http';
import fs from 'fs';
import path from 'path';

export default function indexPageMiddleware(req, res, next) {
export default function indexPageMiddleware(
req: http.IncomingMessage,
res: http.ServerResponse,
next: (err?: any) => void,
) {
if (req.url === '/') {
res.setHeader('Content-Type', 'text/html');
res.end(fs.readFileSync(path.join(__dirname, 'index.html')));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import http from 'http';

export default function rawBodyMiddleware(
req: http.IncomingMessage,
_res: http.ServerResponse,
next: (err?: any) => void,
) {
(req as http.IncomingMessage & {rawBody: string}).rawBody = '';
req.setEncoding('utf8');

req.on('data', (chunk: string) => {
(req as http.IncomingMessage & {rawBody: string}).rawBody += chunk;
});

req.on('end', () => {
next();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

import http from 'http';
import launchEditor from '../launchEditor';

export default function getOpenStackFrameInEditorMiddleware({watchFolders}) {
return (req, res, next) => {
export default function getOpenStackFrameInEditorMiddleware({
watchFolders,
}: {
watchFolders: Array<string>;
}) {
return (
req: http.IncomingMessage & {rawBody: string},
res: http.ServerResponse,
next: (err?: any) => void,
) => {
if (req.url === '/open-stack-frame') {
const frame = JSON.parse(req.rawBody);
launchEditor(frame.file, frame.lineNumber, watchFolders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

import http from 'http';
import launchDefaultBrowser from '../launchDefaultBrowser';
import {logger} from '@react-native-community/cli-tools';

/**
* Handle request from JS to open an arbitrary URL in Chrome
*/
export default function openURLMiddleware(req, res, next) {
export default function openURLMiddleware(
req: http.IncomingMessage & {rawBody: string},
res: http.ServerResponse,
next: (err?: any) => void,
) {
if (req.url === '/open-url') {
const {url} = JSON.parse(req.rawBody);
logger.info(`Opening ${url}...`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import http from 'http';

/**
* Status page so that anyone who needs to can verify that the packager is
* running on 8081 and not another program / service.
*/
export default function statusPageMiddleware(req, res, next) {
export default function statusPageMiddleware(
req: http.IncomingMessage,
res: http.ServerResponse,
next: (err?: any) => void,
) {
if (req.url === '/status') {
res.end('packager-status:running');
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

import http from 'http';
import fs from 'fs';
import {logger} from '@react-native-community/cli-tools';

export default function systraceProfileMiddleware(req, res, next) {
export default function systraceProfileMiddleware(
req: http.IncomingMessage & {rawBody: string},
res: http.ServerResponse,
next: (err?: any) => void,
) {
if (req.url !== '/systrace') {
next();
return;
Expand Down
16 changes: 0 additions & 16 deletions packages/cli/src/commands/server/middleware/unless.js

This file was deleted.

Loading

0 comments on commit d7b78f4

Please sign in to comment.