-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): instance proxy server (#635)
* feat(cli): instance proxy server * docs: add proxy docs
- Loading branch information
Showing
9 changed files
with
301 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
const url = require('url') | ||
const { reporter } = require('@dhis2/cli-helpers-engine') | ||
const httpProxy = require('http-proxy') | ||
const _ = require('lodash') | ||
const transformProxyResponse = require('node-http-proxy-json') | ||
|
||
const stripCookieSecure = cookie => { | ||
return cookie | ||
.split(';') | ||
.filter(v => v.trim().toLowerCase() !== 'secure') | ||
.join('; ') | ||
} | ||
|
||
const rewriteLocation = ({ location, target, baseUrl }) => { | ||
const parsedLocation = url.parse(location) | ||
const parsedTarget = url.parse(target) | ||
const parsedBaseUrl = url.parse(baseUrl) | ||
|
||
if ( | ||
parsedLocation.host === parsedTarget.host && | ||
parsedLocation.pathname.startsWith(parsedTarget.pathname) | ||
) { | ||
return url.format({ | ||
...parsedBaseUrl, | ||
pathname: parsedLocation.pathname.replace( | ||
parsedTarget.pathname, | ||
'' | ||
), | ||
search: parsedLocation.search, | ||
}) | ||
} | ||
return location | ||
} | ||
|
||
const isUrl = string => { | ||
try { | ||
const { protocol } = new URL(string) | ||
return protocol === 'http:' || protocol === 'https:' | ||
} catch (error) { | ||
return false | ||
} | ||
} | ||
|
||
const transformJsonResponse = (res, { target, baseUrl }) => { | ||
switch (typeof res) { | ||
case 'string': | ||
if (isUrl(res)) { | ||
return rewriteLocation({ location: res, target, baseUrl }) | ||
} | ||
return res | ||
case 'object': | ||
if (Array.isArray(res)) { | ||
return res.map(r => | ||
transformJsonResponse(r, { target, baseUrl }) | ||
) | ||
} | ||
return _.transform( | ||
res, | ||
(result, value, key) => { | ||
result[key] = transformJsonResponse(value, { | ||
target, | ||
baseUrl, | ||
}) | ||
}, | ||
{} | ||
) | ||
default: | ||
return res | ||
} | ||
} | ||
|
||
exports = module.exports = ({ target, baseUrl, port, shellPort }) => { | ||
const proxyServer = httpProxy.createProxyServer({ | ||
target, | ||
changeOrigin: true, | ||
secure: false, | ||
protocolRewrite: 'http', | ||
cookieDomainRewrite: '', | ||
cookiePathRewrite: '/', | ||
}) | ||
|
||
proxyServer.on('proxyRes', (proxyRes, req, res) => { | ||
if (proxyRes.headers['access-control-allow-origin']) { | ||
res.setHeader( | ||
'access-control-allow-origin', | ||
`http://localhost:${shellPort}` | ||
) | ||
} | ||
|
||
if (proxyRes.headers.location) { | ||
proxyRes.headers.location = rewriteLocation({ | ||
location: proxyRes.headers.location, | ||
target, | ||
baseUrl, | ||
}) | ||
} | ||
|
||
const sc = proxyRes.headers['set-cookie'] | ||
if (Array.isArray(sc)) { | ||
proxyRes.headers['set-cookie'] = sc.map(stripCookieSecure) | ||
} | ||
|
||
if ( | ||
proxyRes.headers['content-type'] && | ||
proxyRes.headers['content-type'].includes('application/json') | ||
) { | ||
transformProxyResponse(res, proxyRes, body => { | ||
if (body) { | ||
return transformJsonResponse(body, { | ||
target, | ||
baseUrl, | ||
}) | ||
} | ||
return body | ||
}) | ||
} | ||
}) | ||
|
||
proxyServer.on('error', error => { | ||
reporter.warn(error) | ||
}) | ||
|
||
proxyServer.listen(port) | ||
} | ||
|
||
exports.rewriteLocation = rewriteLocation | ||
exports.transformJsonResponse = transformJsonResponse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const { rewriteLocation, transformJsonResponse } = require('./proxy') | ||
|
||
describe('transformJsonResponse', () => { | ||
it('rewrites URLs in responses if they match the proxy target', () => { | ||
const transformedResponse = transformJsonResponse( | ||
{ | ||
a: { | ||
b: { | ||
c: 'https://play.dhis2.org/dev/api/endpoint', | ||
}, | ||
}, | ||
}, | ||
{ | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl: 'http://localhost:8080', | ||
} | ||
) | ||
|
||
expect(transformedResponse.a.b.c).toBe( | ||
'http://localhost:8080/api/endpoint' | ||
) | ||
}) | ||
}) | ||
|
||
describe('rewriteLocation', () => { | ||
it('rewrites locations if they match the proxy target', () => { | ||
const baseUrl = 'http://localhost:8080' | ||
|
||
expect( | ||
rewriteLocation({ | ||
location: 'https://play.dhis2.org/dev/login.action', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl, | ||
}) | ||
).toBe(`${baseUrl}/login.action`) | ||
|
||
expect( | ||
rewriteLocation({ | ||
location: 'https://play.dhis2.org/dev/page?param=value', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl, | ||
}) | ||
).toBe(`${baseUrl}/page?param=value`) | ||
|
||
expect( | ||
rewriteLocation({ | ||
location: 'https://server.com:1234', | ||
target: 'https://server.com:5678', | ||
baseUrl, | ||
}) | ||
).toBe('https://server.com:1234') | ||
|
||
expect( | ||
rewriteLocation({ | ||
location: 'https://server.com', | ||
target: 'http://server.com', | ||
baseUrl, | ||
}) | ||
).toBe(baseUrl) | ||
|
||
expect( | ||
rewriteLocation({ | ||
location: 'https://play.dhis2.org/dev/api/dev', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl, | ||
}) | ||
).toBe(`${baseUrl}/api/dev`) | ||
}) | ||
|
||
it('does not rewrite locations if they do not match the proxy target', () => { | ||
;[ | ||
{ | ||
location: 'https://example.com/path', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl: 'http://localhost:8080', | ||
}, | ||
{ | ||
location: 'https://play.dhis2.org/2.35dev', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl: 'http://localhost:8080', | ||
}, | ||
{ | ||
location: 'http://server.com:1234', | ||
target: 'http://server.com:5678', | ||
baseUrl: 'http://localhost:8080', | ||
}, | ||
].forEach(args => { | ||
expect(rewriteLocation(args)).toBe(args.location) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Proxy | ||
|
||
When developing against a remote instance, some browsers may block cookies due | ||
to the cross-site nature of requests. | ||
|
||
As a workaround, the [`start`](scripts/start.md) command provides a `--proxy` | ||
option. The value of this option is the remote instance that a local proxy will | ||
route requests to. | ||
|
||
## Usage | ||
|
||
``` | ||
d2-app-scripts start --proxy <remote instance URL> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4851,6 +4851,11 @@ buffer@^5.1.0, buffer@^5.5.0: | |
base64-js "^1.3.1" | ||
ieee754 "^1.1.13" | ||
|
||
bufferhelper@^0.2.1: | ||
version "0.2.1" | ||
resolved "https://registry.yarnpkg.com/bufferhelper/-/bufferhelper-0.2.1.tgz#fa74a385724a58e242f04ad6646c2366f83b913e" | ||
integrity sha1-+nSjhXJKWOJC8ErWZGwjZvg7kT4= | ||
|
||
builtin-modules@^3.1.0: | ||
version "3.1.0" | ||
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.1.0.tgz#aad97c15131eb76b65b50ef208e7584cd76a7484" | ||
|
@@ -5541,7 +5546,7 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" | ||
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= | ||
|
||
concat-stream@^1.5.0: | ||
concat-stream@^1.5.0, concat-stream@^1.5.1: | ||
version "1.6.2" | ||
resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.2.tgz#904bdf194cd3122fc675c77fc4ac3d4ff0fd1a34" | ||
integrity sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw== | ||
|
@@ -8389,6 +8394,15 @@ http-proxy@^1.17.0: | |
follow-redirects "^1.0.0" | ||
requires-port "^1.0.0" | ||
|
||
http-proxy@^1.18.1: | ||
version "1.18.1" | ||
resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.18.1.tgz#401541f0534884bbf95260334e72f88ee3976549" | ||
integrity sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ== | ||
dependencies: | ||
eventemitter3 "^4.0.0" | ||
follow-redirects "^1.0.0" | ||
requires-port "^1.0.0" | ||
|
||
http-signature@~1.2.0: | ||
version "1.2.0" | ||
resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.2.0.tgz#9aecd925114772f3d95b65a60abb8f7c18fbace1" | ||
|
@@ -11215,6 +11229,14 @@ node-gettext@^2.0.0: | |
dependencies: | ||
lodash.get "^4.4.2" | ||
|
||
node-http-proxy-json@^0.1.9: | ||
version "0.1.9" | ||
resolved "https://registry.yarnpkg.com/node-http-proxy-json/-/node-http-proxy-json-0.1.9.tgz#5e744138c189ebd7e0105fe92d035a5486478cd4" | ||
integrity sha512-WrKAR/y09BWaz5WqgbxuE6D/XsdhQFkLkSdnRk0a5uBKSINtApMV085MN7JMh+stiyBBltvgSR9SYVIZIpKKKQ== | ||
dependencies: | ||
bufferhelper "^0.2.1" | ||
concat-stream "^1.5.1" | ||
|
||
node-int64@^0.4.0: | ||
version "0.4.0" | ||
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b" | ||
|