Skip to content

Commit

Permalink
[express] express request compat with https (#4325)
Browse files Browse the repository at this point in the history
* express request compat with https

* add http vs https test

* add missing semi
  • Loading branch information
Brianzchen committed Jun 7, 2022
1 parent 69e5ee6 commit 1c80d82
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
14 changes: 9 additions & 5 deletions definitions/npm/express_v4.x.x/flow_v0.104.x-/express_v4.x.x.js
Expand Up @@ -121,6 +121,7 @@ declare class express$Response extends http$ServerResponse mixins express$Reques
redirect(url: string, ...args: Array<void>): this;
redirect(status: number, url: string, ...args: Array<void>): this;
render(view: string, locals?: { [name: string]: mixed, ... }, callback?: express$RenderCallback): this;
render(view: string, callback?: express$RenderCallback): this;
send(body?: mixed): this;
sendFile(path: string, options?: express$SendFileOptions, callback?: (err?: ?Error) => mixed): this;
sendStatus(statusCode: number): this;
Expand Down Expand Up @@ -190,6 +191,8 @@ declare class express$Route<
connect: express$RouteMethodType<this, Req, Res>;
}

declare type express$IncomingMessage = http$IncomingMessage<tls$TLSSocket> | http$IncomingMessage<>;

declare class express$Router<
Req: express$Request = express$Request,
Res: express$Response = express$Response,
Expand All @@ -206,7 +209,7 @@ declare class express$Router<
...middleware: Array<express$Middleware<Req, Res>>
): this;
use(path: string, router: express$Router<Req, Res>): this;
handle(req: http$IncomingMessage<>, res: http$ServerResponse, next: express$NextFunction): void;
handle(req: express$IncomingMessage, res: http$ServerResponse, next: express$NextFunction): void;
param(
param: string,
callback: (
Expand All @@ -217,7 +220,7 @@ declare class express$Router<
paramName: string,
) => mixed
): void;
(req: http$IncomingMessage<>, res: http$ServerResponse, next?: ?express$NextFunction): void;
(req: express$IncomingMessage, res: http$ServerResponse, next?: ?express$NextFunction): void;
}

declare class express$Application<
Expand All @@ -242,10 +245,11 @@ declare class express$Application<
*/
// get(name: string): mixed;
set(name: string, value: mixed): mixed;
render(name: string, optionsOrFunction: { [name: string]: mixed, ... }, callback: express$RenderCallback): void;
handle(req: http$IncomingMessage<>, res: http$ServerResponse, next?: ?express$NextFunction): void;
render(name: string, options: { [name: string]: mixed, ... }, callback: express$RenderCallback): void;
render(name: string, callback: express$RenderCallback): void;
handle(req: express$IncomingMessage, res: http$ServerResponse, next?: ?express$NextFunction): void;
// callable signature is not inherited
(req: http$IncomingMessage<>, res: http$ServerResponse, next?: ?express$NextFunction): void;
(req: express$IncomingMessage, res: http$ServerResponse, next?: ?express$NextFunction): void;
}

declare module 'express' {
Expand Down
Expand Up @@ -20,7 +20,7 @@ declare class test_express$CustomApplication extends express$Application<
constructor(expressConstructor: expressConstructor): this;
}

// Class Extensions: Test Functions
// $FlowExpectedError[name-already-bound] Class Extensions: Test Functions
function test_express$CustomApplication(
expressConstructor: expressConstructor,
) {
Expand All @@ -41,15 +41,15 @@ function test_express$CustomApplication(
// Class Extensions: Test
const customApp = new test_express$CustomApplication(express);

// $FlowExpectedError
// $FlowExpectedError[incompatible-call]
const customApp_error = new test_express$CustomApplication();

customApp.use(
"/something",
(req: express$Request, res: express$Response, next: express$NextFunction) => {
// $FlowExpectedError
// $FlowExpectedError[prop-missing]
req.foo;
// $FlowExpectedError
// $FlowExpectedError[prop-missing]
res.bar;
next();
}
Expand All @@ -63,14 +63,14 @@ customApp.use(
) => {
req.foo;
res.bar;
// $FlowExpectedError
// $FlowExpectedError[prop-missing]
req.notHere;
// $FlowExpectedError
// $FlowExpectedError[prop-missing]
res.notHere;
}
);

// $FlowExpectedError
// $FlowExpectedError[incompatible-call]
customApp.use("/something", (req: string, res: string, next: Function) => {
next();
});
Expand Up @@ -44,7 +44,7 @@ const httpServer = http.createServer(app);
httpServer.listen(9000);

const badHttpServer = null;
// $FlowExpectedError
// $FlowExpectedError[incompatible-use]
badHttpServer.listen();

// Can manually invoke app.handle() to handle a request
Expand Down
55 changes: 39 additions & 16 deletions definitions/npm/express_v4.x.x/test_express_v4.x.x.js
@@ -1,17 +1,22 @@
/* @flow */
// @flow
import { describe, test } from 'flow-typed-test'
import express, { Router } from 'express';

const http = require('http');
const https = require('https');
const fs = require('fs');

const app = express();

// $FlowExpectedError property `foo` Property not found in Application:
// $FlowExpectedError[prop-missing] property `foo` Property not found in Application:
app.foo();

app.locals.title = 'My Express App';

// $FlowExpectedError Symbol: This type is incompatible with string
// $FlowExpectedError[incompatible-type] Symbol: This type is incompatible with string
app.locals[Symbol('bad')] = 'Should not work';

// $FlowExpectedError
// $FlowExpectedError[incompatible-type]
const num: number = app.mountpath;

const myRouter = new express.Router();
Expand All @@ -24,11 +29,11 @@ myRouter.use('/dang', (req, res: express$Response, next: express$NextFunction) =
res.status(200);
res.render('someTemplate', {}, (err, html: ?string) => null);
res.render('someTemplate', (err, html: ?string) => null);
// $FlowExpectedError String: This type is incompatible with Function | {[name: string]: mixed}
// $FlowExpectedError[incompatible-call] String: This type is incompatible with Function | {[name: string]: mixed}
res.render('someTemplate', 'Error');
// $FlowExpectedError
// $FlowExpectedError[incompatible-call]
res.sendFile('/myfile.txt', { dotfiles: 'none' })
// $FlowExpectedError
// $FlowExpectedError[incompatible-call]
next('Error');
});

Expand All @@ -51,12 +56,12 @@ myRouter.use(handleRequest, (err: Error, req: express$Request, res: express$Resp

app.on('mount', (parent: express$Application<>) => {
console.log('Parent Loaded', parent);
// $FlowExpectedError
// $FlowExpectedError[prop-missing]
parent.fail();
})

app.use('/foo', (req: express$Request, res: express$Response, next) => {
// $FlowExpectedError
// $FlowExpectedError[incompatible-call]
res.status('400');
res.send('should work')
.status(300);
Expand All @@ -65,10 +70,10 @@ app.use('/foo', (req: express$Request, res: express$Response, next) => {
const bar: express$Router<> = new Router();

bar.get('/', (req: express$Request, res: express$Response): void => {
// $FlowExpectedError should be of type object
// $FlowExpectedError[incompatible-type] should be of type object
const locals: Array<any> = res.locals;
res.locals.title = 'Home Page';
// $FlowExpectedError should not allow to set keys to non string value.
// $FlowExpectedError[incompatible-type] should not allow to set keys to non string value.
res.locals[0] = 'Fail';
res.send('bar')
.status(200);
Expand All @@ -90,17 +95,17 @@ app.listen(9004, () => {
console.log('Example app listening on port 9004!');
});

// $FlowExpectedError backlog should be number
// $FlowExpectedError[incompatible-call] backlog should be number
app.listen(9005, '127.0.0.1', '256', () => {
console.log('Example app listening on port 9005!');
})

app.set('foo');

app.get('foo');
// $FlowExpectedError
// $FlowExpectedError[incompatible-call]
app.enable(100);
// $FlowExpectedError
// $FlowExpectedError[incompatible-type]
const f: number = app.enabled('100');

const g: express$Application<> = app.enable('foo');
Expand All @@ -119,7 +124,7 @@ app.use('/again', (req: express$Request, res: express$Response) => {
});

app.use('/something', (req: express$Request, res: express$Response) => {
// $FlowExpectedError
// $FlowExpectedError[incompatible-call]
res.redirect('/different', 200);
});

Expand All @@ -144,7 +149,7 @@ app.use((err: Error, req: express$Request, res: express$Response, next: express$
next(err);
});

// $FlowExpectedError path could not be an Object
// $FlowExpectedError[incompatible-type] path could not be an Object
const invalidPath: express$Path = {};

let validPath: express$Path = 'string_path';
Expand All @@ -156,3 +161,21 @@ const validPaths = ['string', 'pattern?', /a[b-f]+g/];
app.get(validPaths, (req: express$Request, res: express$Response) => {
res.end();
});

describe('express', () => {
test('http vs https server usage', () => {
const app = express();

// start http server
const port = 15000;
let server = http.createServer(app).listen(port);

// start https server
const sslOptions = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};

let serverHttps = https.createServer(sslOptions, app).listen(443);
});
});

0 comments on commit 1c80d82

Please sign in to comment.