Skip to content

Commit

Permalink
Support for express server compression middleware. (magento#2980)
Browse files Browse the repository at this point in the history
* Added compression middleware controlled by ENABLE_UPWARD_TEXT_COMPRESSION env variable.

* ENABLE_UPWARD_TEXT_COMPRESSION -> ENABLE_EXPRESS_SERVER_COMPRESSION

* Added express middleware to validate gzip compression.

* Removing Gzip validation middleware.

* Minor.

* Moving the compression logic to buildpack.

* Updated PWADevServer tests.

* Added serve module tests.

* Mocking env for snapshot tests.

* Minor.

* Checking for undefined PORT.

* Minor

* Enabling compression in prod mode.

Co-authored-by: Devagouda <[email protected]>
  • Loading branch information
revanth0212 and dpatil-magento authored Feb 11, 2021
1 parent 23c72da commit 5e38b33
Show file tree
Hide file tree
Showing 15 changed files with 707 additions and 8 deletions.
1 change: 1 addition & 0 deletions docker/.env.docker.dev
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ UPWARD_JS_LOG_URL=1
CHECKOUT_BRAINTREE_TOKEN=sandbox_8yrzsvtm_s2bg8fs563crhqzk
GOOGLE_MAPS_API_KEY=redacted
MAGENTO_BACKEND_EDITION=EE
ENABLE_EXPRESS_SERVER_COMPRESSION=false
1 change: 1 addition & 0 deletions docker/.env.docker.prod
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ UPWARD_JS_LOG_URL=1
CHECKOUT_BRAINTREE_TOKEN=redacted
GOOGLE_MAPS_API_KEY=redacted
MAGENTO_BACKEND_EDITION=EE
ENABLE_EXPRESS_SERVER_COMPRESSION=true
17 changes: 12 additions & 5 deletions packages/pwa-buildpack/envVarDefinitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@
{
"name": "IMAGE_OPTIMIZING_ORIGIN",
"type": "str",
"choices": [
"backend",
"onboard",
"auto"
],
"choices": ["backend", "onboard", "auto"],
"desc": "Specify the origin to use for images in the UI. Set to `backend` when Fastly or another CDN is optimizing images, and the frontend will load images directly from the Magento instance at MAGENTO_BACKEND_URL. To force usage of the onboard image optimizer, set to `onboard`. Set to `auto` to autodetect whether the backend is using FastlyIO and optimize onboard only if it needs to.",
"default": "auto"
}
Expand Down Expand Up @@ -199,6 +195,17 @@
"default": ""
}
]
},
{
"name": "Express compression settings",
"variables": [
{
"name": "ENABLE_EXPRESS_SERVER_COMPRESSION",
"type": "bool",
"desc": "Specify if express server compression needs to be enabled. Defaults to false if not provided.",
"default": false
}
]
}
],
"changes": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,13 @@ CHECKOUT_BRAINTREE_TOKEN=sandbox_8yrzsvtm_s2bg8fs563crhqzk
BUILDBUS_DEPS_ADDITIONAL=
#
################################################################################
#### Express compression settings ##############################################
#
# Specify if express server compression needs to be enabled. Defaults to false if not provided.
# - Default when not set: false
ENABLE_EXPRESS_SERVER_COMPRESSION=false
#
################################################################################
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should add compression middleware if ENABLE_EXPRESS_SERVER_COMPRESSION is true 1`] = `
Object {
"error": Array [],
"info": Array [
"PORT is set in environment: 1234",
"NODE_ENV=production, will not attempt to use custom host or port",
"Launching UPWARD server
",
],
"success": Array [
"
UPWARD server running.",
],
"warn": Array [],
}
`;

exports[`should add the Image Opt middleware 1`] = `
Array [
Array [
Object {},
Object {},
],
]
`;

exports[`should add the Image Opt middleware 2`] = `
Object {
"error": Array [],
"info": Array [
"PORT is set in environment: 1234",
"NODE_ENV=production, will not attempt to use custom host or port",
"Launching UPWARD server
",
],
"success": Array [
"
UPWARD server running.",
],
"warn": Array [],
}
`;

exports[`should create upward server 1`] = `
Array [
Array [
Object {
"before": [Function],
"bindLocal": true,
"env": Object {
"ENABLE_EXPRESS_SERVER_COMPRESSION": "false",
"PORT": "1234",
},
"logUrl": true,
"port": "1234",
},
],
]
`;

exports[`should create upward server 2`] = `"Server Object"`;

exports[`should create upward server 3`] = `
Object {
"error": Array [],
"info": Array [
"PORT is set in environment: 1234",
"NODE_ENV=production, will not attempt to use custom host or port",
"Launching UPWARD server
",
],
"success": Array [
"
UPWARD server running.",
],
"warn": Array [],
}
`;

exports[`should log error if configureHost throws error 1`] = `
Object {
"error": Array [
"Could not configure or access custom host. Using loopback...",
"unable to configure host",
],
"info": Array [
"PORT is set in environment: 7899",
"Launching UPWARD server
",
],
"success": Array [
"
UPWARD server running.",
],
"warn": Array [],
}
`;

exports[`should not add compression middleware if ENABLE_EXPRESS_SERVER_COMPRESSION is false 1`] = `
Object {
"error": Array [],
"info": Array [
"PORT is set in environment: 1234",
"NODE_ENV=production, will not attempt to use custom host or port",
"Launching UPWARD server
",
],
"success": Array [
"
UPWARD server running.",
],
"warn": Array [],
}
`;

exports[`should throw error if unable to load env 1`] = `[Error: Can not load environment config!]`;

exports[`should throw error if unable to load env 2`] = `
Object {
"error": Array [],
"info": Array [],
"success": Array [],
"warn": Array [],
}
`;

exports[`should use configureHost config if custom origin is enabled 1`] = `
Array [
Array [
Object {
"dir": "pwa-buildpack",
"enabled": true,
"interactive": false,
},
],
]
`;

exports[`should use configureHost config if custom origin is enabled 2`] = `
Object {
"error": Array [],
"info": Array [
"PORT is set in environment: 7899",
"Launching UPWARD server
",
],
"success": Array [
"
UPWARD server running.",
],
"warn": Array [],
}
`;

exports[`should use env.PORT if provided 1`] = `
Object {
"error": Array [],
"info": Array [
"PORT is set in environment: 7899",
"NODE_ENV=production, will not attempt to use custom host or port",
"Launching UPWARD server
",
],
"success": Array [
"
UPWARD server running.",
],
"warn": Array [],
}
`;

exports[`should use port 0 if both env.PORT and staging config port are falsy 1`] = `
Object {
"error": Array [],
"info": Array [
"NODE_ENV=production, will not attempt to use custom host or port",
"Launching UPWARD server
",
],
"success": Array [
"
UPWARD server running.",
],
"warn": Array [
"No port set. Binding to OS-assigned port",
],
}
`;

exports[`should use staging server port if env.PORT is not defined 1`] = `
Object {
"error": Array [],
"info": Array [
"STAGING_SERVER_PORT is configured: 5678",
"NODE_ENV=production, will not attempt to use custom host or port",
"Launching UPWARD server
",
],
"success": Array [
"
UPWARD server running.",
],
"warn": Array [],
}
`;

exports[`should use staging server port if env.PORT is null 1`] = `
Object {
"error": Array [],
"info": Array [
"STAGING_SERVER_PORT is configured: 5678",
"NODE_ENV=production, will not attempt to use custom host or port",
"Launching UPWARD server
",
],
"success": Array [
"
UPWARD server running.",
],
"warn": Array [],
}
`;

exports[`should use staging server port if env.PORT is undefined 1`] = `
Object {
"error": Array [],
"info": Array [
"STAGING_SERVER_PORT is configured: 5678",
"NODE_ENV=production, will not attempt to use custom host or port",
"Launching UPWARD server
",
],
"success": Array [
"
UPWARD server running.",
],
"warn": Array [],
}
`;
Loading

0 comments on commit 5e38b33

Please sign in to comment.