Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

EspressJS to Fastify migration #785

Merged
merged 31 commits into from
Aug 10, 2022
Merged

Conversation

giulianok
Copy link
Member

@giulianok giulianok commented Jul 20, 2022

Express to Fastify migration that will improve overall performance on requests/responses including SSR

Description

The goal with this PR is to run all One App servers under Fastify while supporting existing ExpressJS code to slowly migrate to Fastify. This is done through a library called @fastify/express that allows us run ExpressJS through Fastify.

Motivation and Context

Fastify handles more requests per second than ExpressJS. It is also better maintained, provides a cleaner syntax, encapsulation, and more. To minimize risk, in this PR, we are introducing Fastify + Express as described in the Description section above.

Benchmark from Fastify website

Screen Shot 2022-07-26 at 1 18 22 PM

Benchmark local with fastify-express

Screen Shot 2022-06-21 at 1 12 11 PM (1)

The metrics still show that fastify handles 4x the amount of requests compared to express, and fastify-express is slower than fastify but still 3x faster than express

How Has This Been Tested?

Tests were performed via manual tests, unit testing, and integration tests with v5 and v6 versions.

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (adding or updating documentation)
  • Dependency update
  • Security update

Checklist:

  • My change requires a change to the documentation and I have updated the documentation accordingly.
  • These changes should be applied to a maintenance branch.
  • This change requires cross browser checks.
  • Performance tests should be ran against the server prior to merging.
  • This change impacts caching for client browsers.
  • This change impacts HTTP headers.
  • This change adds additional environment variable requirements for One App users.
  • I have added the Apache 2.0 license header to any new files created.

What is the Impact to Developers Using One App?

There's no impact in DX at the moment

10xLaCroixDrinker and others added 17 commits June 28, 2022 15:49
BREAKING CHANGE: Upgrade from React 16 to 17
BREAKING CHANGE: minimum supported node version is 16
* chore(babel): update packages

* chore(commitlint): update

* chore(rollup-plugins): update

* chore(acorn): uninstall

* chore(babel-preset-amex): update to 4

* chore(body-parser): update

* chore(dev-deps): update

* chore(holocron): update 1.3.0

* chore(redux): update 4.2.0

* chore(core-js): update 3.23.3

* chore(deps): run npm update

* chore(husky): update to 8.x

* chore(chalk): downgrade to non esm version

* chore(webdriverio): update 7.x

* feat(dockerfile): update node version to 16.15.1

* chore(deps): update supertest

* fix(node): set min version 16.15.1

* chore(deps): dedupe

* test(createRequestHtmlFragment): more reliable error message

* chore(jest): upgrade 28.1.2
BREAKING CHANGE: Upgrade from React 16 to 17
BREAKING CHANGE: minimum supported node version is 16
* chore(babel): update packages

* chore(commitlint): update

* chore(rollup-plugins): update

* chore(acorn): uninstall

* chore(babel-preset-amex): update to 4

* chore(body-parser): update

* chore(dev-deps): update

* chore(holocron): update 1.3.0

* chore(redux): update 4.2.0

* chore(core-js): update 3.23.3

* chore(deps): run npm update

* chore(husky): update to 8.x

* chore(chalk): downgrade to non esm version

* chore(webdriverio): update 7.x

* feat(dockerfile): update node version to 16.15.1

* chore(deps): update supertest

* fix(node): set min version 16.15.1

* chore(deps): dedupe

* test(createRequestHtmlFragment): more reliable error message

* chore(jest): upgrade 28.1.2
@github-actions
Copy link
Contributor

github-actions bot commented Jul 20, 2022

Size Change: 0 B

Total Size: 681 kB

ℹ️ View Unchanged
Filename Size
./build/app/app.js 165 kB
./build/app/app~vendors.js 386 kB
./build/app/runtime.js 7.07 kB
./build/app/service-worker-client.js 7.26 kB
./build/app/vendors.js 115 kB

compressed-size-action

Base automatically changed from feature/v6 to main July 20, 2022 20:30
@giulianok giulianok marked this pull request as ready for review July 26, 2022 17:40
@giulianok giulianok requested a review from a team as a code owner July 26, 2022 17:40
@giulianok giulianok requested a review from a team as a code owner July 26, 2022 17:40
CHANGELOG.md Outdated
Comment on lines 59 to 60
* **deps:** upgrade to react 17 ([9ba5c39](https://github.com/americanexpress/one-app/commit/9ba5c3926f603a8bf3e8a4ed32930766568621eb))
* **server:** drop node 12 support ([5154d65](https://github.com/americanexpress/one-app/commit/5154d65924dec28f63ad5fdf9192c4ecb972d440))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be included

@@ -75,7 +75,7 @@ const setUpTestRunner = async ({
dockerComposeUpProcess.stderr.pipe(logWatcherDuplex);

// uncomment this line in order to view full logs for debugging
// logWatcherDuplex.pipe(process.stdout);
logWatcherDuplex.pipe(process.stdout);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logWatcherDuplex.pipe(process.stdout);
// logWatcherDuplex.pipe(process.stdout);

@@ -327,6 +327,7 @@ export default function sendHtml(req, res) {

console.info(`sendHtml, have store? ${!!store}, have appHtml? ${!!appHtml}`);
if (appHtml && typeof appHtml !== 'string') {
console.log('--test 1', 'before throw');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove console.logs

Suggested change
console.log('--test 1', 'before throw');

@@ -345,10 +346,12 @@ export default function sendHtml(req, res) {
const allowedHtmlTags = clientInitialState.getIn(['rendering', 'renderTextOnlyOptions', 'allowedHtmlTags']);

if (renderPartialOnly) {
console.log('--test 2', 'renderPartialOnly');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log('--test 2', 'renderPartialOnly');

return safeSend(res, renderPartial({ html: req.appHtml, store, disableStyles }));
}

if (renderTextOnly) {
console.log('--test 3', 'renderTextOnly');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log('--test 3', 'renderTextOnly');

@@ -390,9 +393,11 @@ export default function sendHtml(req, res) {
</html>
`;
} catch (err) {
console.log('--test 4', 'ERROR');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log('--test 4', 'ERROR');

console.error('sendHtml had an error, sending static error page', err);
return renderStaticErrorPage(res);
}

console.log('--test 5', 'safeSend');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log('--test 5', 'safeSend');

: {};
};

export const listen = async ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you planning on moving back to ./listen

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not since this is a fn only consumed in index.js

@giulianok giulianok requested review from JAdshead and a team July 28, 2022 14:05
__tests__/server/ssrServer.spec.js Outdated Show resolved Hide resolved
@@ -0,0 +1,107 @@
/*
* Copyright 2019 American Express Travel Related Services Company, Inc.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright 2019 American Express Travel Related Services Company, Inc.
* Copyright 2022 American Express Travel Related Services Company, Inc.


app.use(serverError);
// TODO: Needs refactoring (priority)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this TODO for this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this is for when we migrate the express routes to fastify. Server errors are being handled from the express router but we still need a 404 handler on the top level

Co-authored-by: Jamie King <jamie.king@aexp.com>
@JAdshead JAdshead requested review from 10xLaCroixDrinker and a team August 9, 2022 21:14
@@ -1,3 +1,4 @@
/* eslint-disable jest/no-disabled-tests */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* eslint-disable jest/no-disabled-tests */

.get('/route');
expect(addFrameOptionsHeader).toBeCalled();
});

it('should return the static error page when an error was encountered', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still being tested elsewhere ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's covered by fastify but I added it back

@10xLaCroixDrinker 10xLaCroixDrinker merged commit e3da397 into main Aug 10, 2022
@10xLaCroixDrinker 10xLaCroixDrinker deleted the feature/v6-express-to-fastify branch August 10, 2022 23:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants