@defra/lis-hubs-infra-core 0.2.0 → 0.3.0
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/package.json +6 -4
- package/src/index.js +1 -0
- package/src/proxy-plugin.js +77 -0
- package/src/proxy-plugin.test.js +101 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defra/lis-hubs-infra-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Shared hub mechanics for livestock platform.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -31,11 +31,13 @@
|
|
|
31
31
|
"author": "Defra DDTS",
|
|
32
32
|
"license": "OGL-UK-3.0",
|
|
33
33
|
"dependencies": {
|
|
34
|
+
"@defra/lis-hubs-infra-registry": "^0.3.0",
|
|
35
|
+
"@hapi/h2o2": "10.0.4",
|
|
36
|
+
"convict": "6.2.5",
|
|
37
|
+
"convict-format-with-validator": "6.2.0",
|
|
34
38
|
"eslint-plugin-jsdoc": "^63.0.8",
|
|
35
39
|
"eslint-plugin-sonarjs": "^4.1.0",
|
|
36
|
-
"neostandard": "0.13.0"
|
|
37
|
-
"convict": "6.2.5",
|
|
38
|
-
"convict-format-with-validator": "6.2.0"
|
|
40
|
+
"neostandard": "0.13.0"
|
|
39
41
|
},
|
|
40
42
|
"peerDependencies": {
|
|
41
43
|
"eslint": "^9.0.0"
|
package/src/index.js
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import h2o2 from '@hapi/h2o2'
|
|
2
|
+
|
|
3
|
+
import { getModulesForHub } from '@defra/lis-hubs-infra-registry'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {{ hubId: string, environment: string }} options
|
|
7
|
+
* @returns {{ plugin: { name: string, register: Function } }}
|
|
8
|
+
*/
|
|
9
|
+
export function createProxyPlugin({ hubId, environment }) {
|
|
10
|
+
const modules = getModulesForHub(hubId).toSorted(
|
|
11
|
+
(a, b) => b.path.split('/').length - a.path.split('/').length
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
plugin: {
|
|
16
|
+
name: 'proxy',
|
|
17
|
+
async register(server) {
|
|
18
|
+
await server.register(h2o2)
|
|
19
|
+
|
|
20
|
+
for (const { id: moduleName, path, port } of modules) {
|
|
21
|
+
let host, protocol
|
|
22
|
+
|
|
23
|
+
switch (environment) {
|
|
24
|
+
case 'local':
|
|
25
|
+
host = 'localhost'
|
|
26
|
+
protocol = 'http'
|
|
27
|
+
break
|
|
28
|
+
case 'docker_compose':
|
|
29
|
+
host = moduleName
|
|
30
|
+
protocol = 'http'
|
|
31
|
+
break
|
|
32
|
+
case 'dev':
|
|
33
|
+
case 'test':
|
|
34
|
+
case 'perf-test':
|
|
35
|
+
case 'prod':
|
|
36
|
+
host = `lis-apps-${moduleName}.${environment}.cdp-int.defra.cloud`
|
|
37
|
+
protocol = 'https'
|
|
38
|
+
break
|
|
39
|
+
default:
|
|
40
|
+
throw new Error(`Unhandled environment: ${environment}`)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let baseUri = `${protocol}://${host}`
|
|
44
|
+
|
|
45
|
+
if (environment === 'local' || environment === 'docker_compose') {
|
|
46
|
+
baseUri = `${baseUri}:${port}`
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
server.route({
|
|
50
|
+
method: '*',
|
|
51
|
+
path: `${path}/{path*}`,
|
|
52
|
+
handler: {
|
|
53
|
+
proxy: {
|
|
54
|
+
passThrough: true,
|
|
55
|
+
xforward: true,
|
|
56
|
+
mapUri(request) {
|
|
57
|
+
const subPath = request.params.path ?? ''
|
|
58
|
+
const uri = subPath ? `${baseUri}/${subPath}` : baseUri
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
uri,
|
|
62
|
+
headers: {
|
|
63
|
+
'x-forwarded-prefix': path,
|
|
64
|
+
...(request.headers.cookie && {
|
|
65
|
+
cookie: request.headers.cookie
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { getModulesForHub } from '@defra/lis-hubs-infra-registry'
|
|
4
|
+
import { createProxyPlugin } from './proxy-plugin.js'
|
|
5
|
+
|
|
6
|
+
vi.mock('@defra/lis-hubs-infra-registry')
|
|
7
|
+
|
|
8
|
+
const mocks = {
|
|
9
|
+
getModulesForHub: vi.mocked(getModulesForHub)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
describe('createProxyPlugin()', () => {
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
vi.clearAllMocks()
|
|
15
|
+
mocks.getModulesForHub.mockReturnValue([
|
|
16
|
+
{ id: 'cattle-home', path: '/cattle', port: 3222 }
|
|
17
|
+
])
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test.each([
|
|
21
|
+
// Local proxying intentionally uses HTTP because the services run locally.
|
|
22
|
+
['local', 'http://localhost:3222'],
|
|
23
|
+
// eslint-disable-next-line sonarjs/no-clear-text-protocols
|
|
24
|
+
['docker_compose', 'http://cattle-home:3222'],
|
|
25
|
+
['test', 'https://lis-apps-cattle-home.test.cdp-int.defra.cloud'],
|
|
26
|
+
['prod', 'https://lis-apps-cattle-home.prod.cdp-int.defra.cloud']
|
|
27
|
+
])('registers the %s proxy target', async (environment, expectedBaseUri) => {
|
|
28
|
+
// Arrange
|
|
29
|
+
const server = { route: vi.fn(), register: vi.fn() }
|
|
30
|
+
const proxy = createProxyPlugin({ hubId: 'back-office', environment })
|
|
31
|
+
|
|
32
|
+
// Act
|
|
33
|
+
await proxy.plugin.register(server)
|
|
34
|
+
|
|
35
|
+
// Assert
|
|
36
|
+
expect(server.register).toHaveBeenCalled()
|
|
37
|
+
expect(mocks.getModulesForHub).toHaveBeenCalledWith('back-office')
|
|
38
|
+
const route = server.route.mock.calls[0][0]
|
|
39
|
+
expect(route).toMatchObject({ method: '*', path: '/cattle/{path*}' })
|
|
40
|
+
expect(
|
|
41
|
+
route.handler.proxy.mapUri({
|
|
42
|
+
params: { path: 'summary-data' },
|
|
43
|
+
headers: { cookie: 'session=abc' }
|
|
44
|
+
})
|
|
45
|
+
).toEqual({
|
|
46
|
+
uri: `${expectedBaseUri}/summary-data`,
|
|
47
|
+
headers: {
|
|
48
|
+
'x-forwarded-prefix': '/cattle',
|
|
49
|
+
cookie: 'session=abc'
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
expect(route.handler.proxy.mapUri({ params: {}, headers: {} })).toEqual({
|
|
53
|
+
uri: expectedBaseUri,
|
|
54
|
+
headers: { 'x-forwarded-prefix': '/cattle' }
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('rejects an unsupported environment', async () => {
|
|
59
|
+
// Arrange
|
|
60
|
+
const server = { route: vi.fn(), register: vi.fn() }
|
|
61
|
+
const proxy = createProxyPlugin({
|
|
62
|
+
hubId: 'back-office',
|
|
63
|
+
environment: 'unknown'
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
// Act
|
|
67
|
+
let error
|
|
68
|
+
try {
|
|
69
|
+
await proxy.plugin.register(server)
|
|
70
|
+
} catch (e) {
|
|
71
|
+
error = e
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Assert
|
|
75
|
+
expect(error).toBeDefined()
|
|
76
|
+
expect(error.message).toBe('Unhandled environment: unknown')
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('sorts modules with the longest path first so a shorter prefix does not swallow a longer one', async () => {
|
|
80
|
+
// Arrange
|
|
81
|
+
mocks.getModulesForHub.mockReturnValue([
|
|
82
|
+
{ id: 'cattle-home', path: '/cattle', port: 3221 },
|
|
83
|
+
{ id: 'cattle-register', path: '/cattle/register', port: 3201 }
|
|
84
|
+
])
|
|
85
|
+
const server = { route: vi.fn(), register: vi.fn() }
|
|
86
|
+
const proxy = createProxyPlugin({
|
|
87
|
+
hubId: 'front-office',
|
|
88
|
+
environment: 'local'
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
// Act
|
|
92
|
+
await proxy.plugin.register(server)
|
|
93
|
+
|
|
94
|
+
// Assert
|
|
95
|
+
const registeredPaths = server.route.mock.calls.map(([{ path }]) => path)
|
|
96
|
+
expect(registeredPaths).toEqual([
|
|
97
|
+
'/cattle/register/{path*}',
|
|
98
|
+
'/cattle/{path*}'
|
|
99
|
+
])
|
|
100
|
+
})
|
|
101
|
+
})
|