@ecomplus/storefront-renderer 2.10.7 → 2.10.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,16 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [2.10.9](https://github.com/ecomplus/storefront/compare/@ecomplus/storefront-renderer@2.10.8...@ecomplus/storefront-renderer@2.10.9) (2023-05-31)
7
+
8
+ **Note:** Version bump only for package @ecomplus/storefront-renderer
9
+
10
+ ## [2.10.8](https://github.com/ecomplus/storefront/compare/@ecomplus/storefront-renderer@2.10.7...@ecomplus/storefront-renderer@2.10.8) (2023-05-20)
11
+
12
+ ### Bug Fixes
13
+
14
+ - **renderer/proxy:** force cors header and properly handle req/res required headers ([072ed51](https://github.com/ecomplus/storefront/commit/072ed51ec5fa31693016e813b252ade2bf89d58c))
15
+
6
16
  ## [2.10.7](https://github.com/ecomplus/storefront/compare/@ecomplus/storefront-renderer@2.10.6...@ecomplus/storefront-renderer@2.10.7) (2023-05-20)
7
17
 
8
18
  ### Bug Fixes
@@ -17,7 +17,6 @@ exports.ssr = (req, res, getCacheControl) => {
17
17
 
18
18
  const isLongCache = String(STOREFRONT_LONG_CACHE).toLowerCase() === 'true'
19
19
  const url = req.url.replace(/\?.*$/, '').replace(/\.html$/, '')
20
- const { headers } = req
21
20
 
22
21
  const setStatusAndCache = (status, defaultCache) => {
23
22
  return res.status(status)
@@ -28,17 +27,74 @@ exports.ssr = (req, res, getCacheControl) => {
28
27
  )
29
28
  }
30
29
 
31
- const proxy = requestUrl => {
32
- if (requestUrl) {
33
- return axios.get(requestUrl, {
34
- headers,
35
- timeout: 3000,
36
- validateStatus: (status) => {
37
- return Boolean(status)
30
+ const proxy = async () => {
31
+ let proxyUrl
32
+ try {
33
+ proxyUrl = new URL(req.query.url)
34
+ } catch {
35
+ }
36
+ if (proxyUrl) {
37
+ const { headers } = req
38
+ /* eslint-disable dot-notation */
39
+ headers['origin'] = headers['x-forwarded-host']
40
+ headers['host'] = proxyUrl.hostname
41
+ if (!headers['accept']) {
42
+ headers['accept'] = 'text/plain,text/html,application/javascript,application/x-javascript'
43
+ }
44
+ headers['accept-encoding'] = 'gzip'
45
+ delete headers['forwarded']
46
+ delete headers['via']
47
+ delete headers['traceparent']
48
+ delete headers['upgrade-insecure-requests']
49
+ delete headers['x-timer']
50
+ delete headers['x-varnish']
51
+ delete headers['x-orig-accept-language']
52
+ Object.keys(headers).forEach((headerName) => {
53
+ if (
54
+ headerName.startsWith('x-forwarded-') ||
55
+ headerName.startsWith('cdn-') ||
56
+ headerName.startsWith('fastly-') ||
57
+ headerName.startsWith('x-firebase-') ||
58
+ headerName.startsWith('x-cloud-') ||
59
+ headerName.startsWith('x-appengine-') ||
60
+ headerName.startsWith('function-')
61
+ ) {
62
+ delete headers[headerName]
38
63
  }
39
64
  })
65
+ if (process.env.STOREFRONT_PROXY_DEBUG) {
66
+ console.log({ proxy: proxyUrl.href })
67
+ }
68
+ try {
69
+ const response = await axios.get(proxyUrl.href, {
70
+ headers,
71
+ timeout: 3000,
72
+ responseType: 'text',
73
+ validateStatus: (status) => {
74
+ return Boolean(status)
75
+ }
76
+ })
77
+ res.status(response.status)
78
+ Object.keys(response.headers).forEach((headerName) => {
79
+ switch (headerName) {
80
+ case 'transfer-encoding':
81
+ case 'connection':
82
+ case 'strict-transport-security':
83
+ case 'alt-svc':
84
+ case 'server':
85
+ break
86
+ default:
87
+ res.set(headerName, response.headers[headerName])
88
+ }
89
+ })
90
+ res.set('access-control-allow-origin', '*')
91
+ return res.send(response.data)
92
+ } catch (err) {
93
+ console.error(err)
94
+ return res.status(400).send(err.message)
95
+ }
40
96
  }
41
- return null
97
+ res.sendStatus(400)
42
98
  }
43
99
 
44
100
  const redirect = (url, status = 302) => {
@@ -63,13 +119,7 @@ exports.ssr = (req, res, getCacheControl) => {
63
119
 
64
120
  const fallback = () => {
65
121
  if (url.startsWith('/reverse-proxy/')) {
66
- proxy(req.query.url).then((response) => {
67
- if (response) {
68
- const { status, headers, data } = response
69
- return res.writeHead(status, headers).send(data)
70
- }
71
- return res.sendStatus(400)
72
- })
122
+ proxy()
73
123
  } else if (url.slice(-1) === '/') {
74
124
  redirect(url.slice(0, -1))
75
125
  } else if (url !== '/404' && (/\/[^/.]+$/.test(url) || /\.x?html$/.test(url))) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecomplus/storefront-renderer",
3
- "version": "2.10.7",
3
+ "version": "2.10.9",
4
4
  "description": "SSR for Storefront EJS views with E-Com Plus APIs",
5
5
  "main": "src/index.js",
6
6
  "repository": {