@ecomplus/storefront-renderer 2.10.6 → 2.10.8
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 +12 -0
- package/functions/index.js +65 -19
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
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.8](https://github.com/ecomplus/storefront/compare/@ecomplus/storefront-renderer@2.10.7...@ecomplus/storefront-renderer@2.10.8) (2023-05-20)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **renderer/proxy:** force cors header and properly handle req/res required headers ([072ed51](https://github.com/ecomplus/storefront/commit/072ed51ec5fa31693016e813b252ade2bf89d58c))
|
|
11
|
+
|
|
12
|
+
## [2.10.7](https://github.com/ecomplus/storefront/compare/@ecomplus/storefront-renderer@2.10.6...@ecomplus/storefront-renderer@2.10.7) (2023-05-20)
|
|
13
|
+
|
|
14
|
+
### Bug Fixes
|
|
15
|
+
|
|
16
|
+
- **renderer/proxy:** fix handling url on reverse proxy ([#904](https://github.com/ecomplus/storefront/issues/904)) ([15c4116](https://github.com/ecomplus/storefront/commit/15c411617b427fecaa618532d53cff3c245c4ae2))
|
|
17
|
+
|
|
6
18
|
## [2.10.6](https://github.com/ecomplus/storefront/compare/@ecomplus/storefront-renderer@2.10.5...@ecomplus/storefront-renderer@2.10.6) (2023-05-12)
|
|
7
19
|
|
|
8
20
|
### Bug Fixes
|
package/functions/index.js
CHANGED
|
@@ -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,19 +27,72 @@ exports.ssr = (req, res, getCacheControl) => {
|
|
|
28
27
|
)
|
|
29
28
|
}
|
|
30
29
|
|
|
31
|
-
const proxy =
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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]
|
|
63
|
+
}
|
|
41
64
|
})
|
|
65
|
+
console.log({ proxy: proxyUrl.href })
|
|
66
|
+
try {
|
|
67
|
+
const response = await axios.get(proxyUrl.href, {
|
|
68
|
+
headers,
|
|
69
|
+
timeout: 3000,
|
|
70
|
+
responseType: 'text',
|
|
71
|
+
validateStatus: (status) => {
|
|
72
|
+
return Boolean(status)
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
res.status(response.status)
|
|
76
|
+
Object.keys(response.headers).forEach((headerName) => {
|
|
77
|
+
switch (headerName) {
|
|
78
|
+
case 'transfer-encoding':
|
|
79
|
+
case 'connection':
|
|
80
|
+
case 'strict-transport-security':
|
|
81
|
+
case 'alt-svc':
|
|
82
|
+
case 'server':
|
|
83
|
+
break
|
|
84
|
+
default:
|
|
85
|
+
res.set(headerName, response.headers[headerName])
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
res.set('access-control-allow-origin', '*')
|
|
89
|
+
return res.send(response.data)
|
|
90
|
+
} catch (err) {
|
|
91
|
+
console.error(err)
|
|
92
|
+
return res.status(400).send(err.message)
|
|
93
|
+
}
|
|
42
94
|
}
|
|
43
|
-
|
|
95
|
+
res.sendStatus(400)
|
|
44
96
|
}
|
|
45
97
|
|
|
46
98
|
const redirect = (url, status = 302) => {
|
|
@@ -65,13 +117,7 @@ exports.ssr = (req, res, getCacheControl) => {
|
|
|
65
117
|
|
|
66
118
|
const fallback = () => {
|
|
67
119
|
if (url.startsWith('/reverse-proxy/')) {
|
|
68
|
-
proxy(
|
|
69
|
-
if (response) {
|
|
70
|
-
const { status, headers, data } = response
|
|
71
|
-
return res.writeHead(status, headers).send(data)
|
|
72
|
-
}
|
|
73
|
-
return res.sendStatus(400)
|
|
74
|
-
})
|
|
120
|
+
proxy()
|
|
75
121
|
} else if (url.slice(-1) === '/') {
|
|
76
122
|
redirect(url.slice(0, -1))
|
|
77
123
|
} else if (url !== '/404' && (/\/[^/.]+$/.test(url) || /\.x?html$/.test(url))) {
|